Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Universal Flash Storage Host controller driver
0004  * Copyright (C) 2011-2013 Samsung India Software Operations
0005  * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
0006  *
0007  * Authors:
0008  *  Santosh Yaraganavi <santosh.sy@samsung.com>
0009  *  Vinayak Holikatti <h.vinayak@samsung.com>
0010  */
0011 
0012 #ifndef _UFSHCD_H
0013 #define _UFSHCD_H
0014 
0015 #include <linux/bitfield.h>
0016 #include <linux/blk-crypto-profile.h>
0017 #include <linux/blk-mq.h>
0018 #include <linux/devfreq.h>
0019 #include <linux/pm_runtime.h>
0020 #include <scsi/scsi_device.h>
0021 #include <ufs/unipro.h>
0022 #include <ufs/ufs.h>
0023 #include <ufs/ufs_quirks.h>
0024 #include <ufs/ufshci.h>
0025 
0026 #define UFSHCD "ufshcd"
0027 
0028 struct ufs_hba;
0029 
0030 enum dev_cmd_type {
0031     DEV_CMD_TYPE_NOP        = 0x0,
0032     DEV_CMD_TYPE_QUERY      = 0x1,
0033 };
0034 
0035 enum ufs_event_type {
0036     /* uic specific errors */
0037     UFS_EVT_PA_ERR = 0,
0038     UFS_EVT_DL_ERR,
0039     UFS_EVT_NL_ERR,
0040     UFS_EVT_TL_ERR,
0041     UFS_EVT_DME_ERR,
0042 
0043     /* fatal errors */
0044     UFS_EVT_AUTO_HIBERN8_ERR,
0045     UFS_EVT_FATAL_ERR,
0046     UFS_EVT_LINK_STARTUP_FAIL,
0047     UFS_EVT_RESUME_ERR,
0048     UFS_EVT_SUSPEND_ERR,
0049     UFS_EVT_WL_SUSP_ERR,
0050     UFS_EVT_WL_RES_ERR,
0051 
0052     /* abnormal events */
0053     UFS_EVT_DEV_RESET,
0054     UFS_EVT_HOST_RESET,
0055     UFS_EVT_ABORT,
0056 
0057     UFS_EVT_CNT,
0058 };
0059 
0060 /**
0061  * struct uic_command - UIC command structure
0062  * @command: UIC command
0063  * @argument1: UIC command argument 1
0064  * @argument2: UIC command argument 2
0065  * @argument3: UIC command argument 3
0066  * @cmd_active: Indicate if UIC command is outstanding
0067  * @done: UIC command completion
0068  */
0069 struct uic_command {
0070     u32 command;
0071     u32 argument1;
0072     u32 argument2;
0073     u32 argument3;
0074     int cmd_active;
0075     struct completion done;
0076 };
0077 
0078 /* Used to differentiate the power management options */
0079 enum ufs_pm_op {
0080     UFS_RUNTIME_PM,
0081     UFS_SYSTEM_PM,
0082     UFS_SHUTDOWN_PM,
0083 };
0084 
0085 /* Host <-> Device UniPro Link state */
0086 enum uic_link_state {
0087     UIC_LINK_OFF_STATE  = 0, /* Link powered down or disabled */
0088     UIC_LINK_ACTIVE_STATE   = 1, /* Link is in Fast/Slow/Sleep state */
0089     UIC_LINK_HIBERN8_STATE  = 2, /* Link is in Hibernate state */
0090     UIC_LINK_BROKEN_STATE   = 3, /* Link is in broken state */
0091 };
0092 
0093 #define ufshcd_is_link_off(hba) ((hba)->uic_link_state == UIC_LINK_OFF_STATE)
0094 #define ufshcd_is_link_active(hba) ((hba)->uic_link_state == \
0095                     UIC_LINK_ACTIVE_STATE)
0096 #define ufshcd_is_link_hibern8(hba) ((hba)->uic_link_state == \
0097                     UIC_LINK_HIBERN8_STATE)
0098 #define ufshcd_is_link_broken(hba) ((hba)->uic_link_state == \
0099                    UIC_LINK_BROKEN_STATE)
0100 #define ufshcd_set_link_off(hba) ((hba)->uic_link_state = UIC_LINK_OFF_STATE)
0101 #define ufshcd_set_link_active(hba) ((hba)->uic_link_state = \
0102                     UIC_LINK_ACTIVE_STATE)
0103 #define ufshcd_set_link_hibern8(hba) ((hba)->uic_link_state = \
0104                     UIC_LINK_HIBERN8_STATE)
0105 #define ufshcd_set_link_broken(hba) ((hba)->uic_link_state = \
0106                     UIC_LINK_BROKEN_STATE)
0107 
0108 #define ufshcd_set_ufs_dev_active(h) \
0109     ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
0110 #define ufshcd_set_ufs_dev_sleep(h) \
0111     ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
0112 #define ufshcd_set_ufs_dev_poweroff(h) \
0113     ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
0114 #define ufshcd_set_ufs_dev_deepsleep(h) \
0115     ((h)->curr_dev_pwr_mode = UFS_DEEPSLEEP_PWR_MODE)
0116 #define ufshcd_is_ufs_dev_active(h) \
0117     ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
0118 #define ufshcd_is_ufs_dev_sleep(h) \
0119     ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
0120 #define ufshcd_is_ufs_dev_poweroff(h) \
0121     ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
0122 #define ufshcd_is_ufs_dev_deepsleep(h) \
0123     ((h)->curr_dev_pwr_mode == UFS_DEEPSLEEP_PWR_MODE)
0124 
0125 /*
0126  * UFS Power management levels.
0127  * Each level is in increasing order of power savings, except DeepSleep
0128  * which is lower than PowerDown with power on but not PowerDown with
0129  * power off.
0130  */
0131 enum ufs_pm_level {
0132     UFS_PM_LVL_0,
0133     UFS_PM_LVL_1,
0134     UFS_PM_LVL_2,
0135     UFS_PM_LVL_3,
0136     UFS_PM_LVL_4,
0137     UFS_PM_LVL_5,
0138     UFS_PM_LVL_6,
0139     UFS_PM_LVL_MAX
0140 };
0141 
0142 struct ufs_pm_lvl_states {
0143     enum ufs_dev_pwr_mode dev_state;
0144     enum uic_link_state link_state;
0145 };
0146 
0147 /**
0148  * struct ufshcd_lrb - local reference block
0149  * @utr_descriptor_ptr: UTRD address of the command
0150  * @ucd_req_ptr: UCD address of the command
0151  * @ucd_rsp_ptr: Response UPIU address for this command
0152  * @ucd_prdt_ptr: PRDT address of the command
0153  * @utrd_dma_addr: UTRD dma address for debug
0154  * @ucd_prdt_dma_addr: PRDT dma address for debug
0155  * @ucd_rsp_dma_addr: UPIU response dma address for debug
0156  * @ucd_req_dma_addr: UPIU request dma address for debug
0157  * @cmd: pointer to SCSI command
0158  * @scsi_status: SCSI status of the command
0159  * @command_type: SCSI, UFS, Query.
0160  * @task_tag: Task tag of the command
0161  * @lun: LUN of the command
0162  * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
0163  * @issue_time_stamp: time stamp for debug purposes
0164  * @compl_time_stamp: time stamp for statistics
0165  * @crypto_key_slot: the key slot to use for inline crypto (-1 if none)
0166  * @data_unit_num: the data unit number for the first block for inline crypto
0167  * @req_abort_skip: skip request abort task flag
0168  */
0169 struct ufshcd_lrb {
0170     struct utp_transfer_req_desc *utr_descriptor_ptr;
0171     struct utp_upiu_req *ucd_req_ptr;
0172     struct utp_upiu_rsp *ucd_rsp_ptr;
0173     struct ufshcd_sg_entry *ucd_prdt_ptr;
0174 
0175     dma_addr_t utrd_dma_addr;
0176     dma_addr_t ucd_req_dma_addr;
0177     dma_addr_t ucd_rsp_dma_addr;
0178     dma_addr_t ucd_prdt_dma_addr;
0179 
0180     struct scsi_cmnd *cmd;
0181     int scsi_status;
0182 
0183     int command_type;
0184     int task_tag;
0185     u8 lun; /* UPIU LUN id field is only 8-bit wide */
0186     bool intr_cmd;
0187     ktime_t issue_time_stamp;
0188     ktime_t compl_time_stamp;
0189 #ifdef CONFIG_SCSI_UFS_CRYPTO
0190     int crypto_key_slot;
0191     u64 data_unit_num;
0192 #endif
0193 
0194     bool req_abort_skip;
0195 };
0196 
0197 /**
0198  * struct ufs_query - holds relevant data structures for query request
0199  * @request: request upiu and function
0200  * @descriptor: buffer for sending/receiving descriptor
0201  * @response: response upiu and response
0202  */
0203 struct ufs_query {
0204     struct ufs_query_req request;
0205     u8 *descriptor;
0206     struct ufs_query_res response;
0207 };
0208 
0209 /**
0210  * struct ufs_dev_cmd - all assosiated fields with device management commands
0211  * @type: device management command type - Query, NOP OUT
0212  * @lock: lock to allow one command at a time
0213  * @complete: internal commands completion
0214  * @query: Device management query information
0215  */
0216 struct ufs_dev_cmd {
0217     enum dev_cmd_type type;
0218     struct mutex lock;
0219     struct completion *complete;
0220     struct ufs_query query;
0221 };
0222 
0223 /**
0224  * struct ufs_clk_info - UFS clock related info
0225  * @list: list headed by hba->clk_list_head
0226  * @clk: clock node
0227  * @name: clock name
0228  * @max_freq: maximum frequency supported by the clock
0229  * @min_freq: min frequency that can be used for clock scaling
0230  * @curr_freq: indicates the current frequency that it is set to
0231  * @keep_link_active: indicates that the clk should not be disabled if
0232  *            link is active
0233  * @enabled: variable to check against multiple enable/disable
0234  */
0235 struct ufs_clk_info {
0236     struct list_head list;
0237     struct clk *clk;
0238     const char *name;
0239     u32 max_freq;
0240     u32 min_freq;
0241     u32 curr_freq;
0242     bool keep_link_active;
0243     bool enabled;
0244 };
0245 
0246 enum ufs_notify_change_status {
0247     PRE_CHANGE,
0248     POST_CHANGE,
0249 };
0250 
0251 struct ufs_pa_layer_attr {
0252     u32 gear_rx;
0253     u32 gear_tx;
0254     u32 lane_rx;
0255     u32 lane_tx;
0256     u32 pwr_rx;
0257     u32 pwr_tx;
0258     u32 hs_rate;
0259 };
0260 
0261 struct ufs_pwr_mode_info {
0262     bool is_valid;
0263     struct ufs_pa_layer_attr info;
0264 };
0265 
0266 /**
0267  * struct ufs_hba_variant_ops - variant specific callbacks
0268  * @name: variant name
0269  * @init: called when the driver is initialized
0270  * @exit: called to cleanup everything done in init
0271  * @get_ufs_hci_version: called to get UFS HCI version
0272  * @clk_scale_notify: notifies that clks are scaled up/down
0273  * @setup_clocks: called before touching any of the controller registers
0274  * @hce_enable_notify: called before and after HCE enable bit is set to allow
0275  *                     variant specific Uni-Pro initialization.
0276  * @link_startup_notify: called before and after Link startup is carried out
0277  *                       to allow variant specific Uni-Pro initialization.
0278  * @pwr_change_notify: called before and after a power mode change
0279  *          is carried out to allow vendor spesific capabilities
0280  *          to be set.
0281  * @setup_xfer_req: called before any transfer request is issued
0282  *                  to set some things
0283  * @setup_task_mgmt: called before any task management request is issued
0284  *                  to set some things
0285  * @hibern8_notify: called around hibern8 enter/exit
0286  * @apply_dev_quirks: called to apply device specific quirks
0287  * @fixup_dev_quirks: called to modify device specific quirks
0288  * @suspend: called during host controller PM callback
0289  * @resume: called during host controller PM callback
0290  * @dbg_register_dump: used to dump controller debug information
0291  * @phy_initialization: used to initialize phys
0292  * @device_reset: called to issue a reset pulse on the UFS device
0293  * @config_scaling_param: called to configure clock scaling parameters
0294  * @program_key: program or evict an inline encryption key
0295  * @event_notify: called to notify important events
0296  */
0297 struct ufs_hba_variant_ops {
0298     const char *name;
0299     int (*init)(struct ufs_hba *);
0300     void    (*exit)(struct ufs_hba *);
0301     u32 (*get_ufs_hci_version)(struct ufs_hba *);
0302     int (*clk_scale_notify)(struct ufs_hba *, bool,
0303                     enum ufs_notify_change_status);
0304     int (*setup_clocks)(struct ufs_hba *, bool,
0305                 enum ufs_notify_change_status);
0306     int (*hce_enable_notify)(struct ufs_hba *,
0307                      enum ufs_notify_change_status);
0308     int (*link_startup_notify)(struct ufs_hba *,
0309                        enum ufs_notify_change_status);
0310     int (*pwr_change_notify)(struct ufs_hba *,
0311                     enum ufs_notify_change_status status,
0312                     struct ufs_pa_layer_attr *,
0313                     struct ufs_pa_layer_attr *);
0314     void    (*setup_xfer_req)(struct ufs_hba *hba, int tag,
0315                   bool is_scsi_cmd);
0316     void    (*setup_task_mgmt)(struct ufs_hba *, int, u8);
0317     void    (*hibern8_notify)(struct ufs_hba *, enum uic_cmd_dme,
0318                     enum ufs_notify_change_status);
0319     int (*apply_dev_quirks)(struct ufs_hba *hba);
0320     void    (*fixup_dev_quirks)(struct ufs_hba *hba);
0321     int     (*suspend)(struct ufs_hba *, enum ufs_pm_op,
0322                     enum ufs_notify_change_status);
0323     int     (*resume)(struct ufs_hba *, enum ufs_pm_op);
0324     void    (*dbg_register_dump)(struct ufs_hba *hba);
0325     int (*phy_initialization)(struct ufs_hba *);
0326     int (*device_reset)(struct ufs_hba *hba);
0327     void    (*config_scaling_param)(struct ufs_hba *hba,
0328                 struct devfreq_dev_profile *profile,
0329                 struct devfreq_simple_ondemand_data *data);
0330     int (*program_key)(struct ufs_hba *hba,
0331                    const union ufs_crypto_cfg_entry *cfg, int slot);
0332     void    (*event_notify)(struct ufs_hba *hba,
0333                 enum ufs_event_type evt, void *data);
0334 };
0335 
0336 /* clock gating state  */
0337 enum clk_gating_state {
0338     CLKS_OFF,
0339     CLKS_ON,
0340     REQ_CLKS_OFF,
0341     REQ_CLKS_ON,
0342 };
0343 
0344 /**
0345  * struct ufs_clk_gating - UFS clock gating related info
0346  * @gate_work: worker to turn off clocks after some delay as specified in
0347  * delay_ms
0348  * @ungate_work: worker to turn on clocks that will be used in case of
0349  * interrupt context
0350  * @state: the current clocks state
0351  * @delay_ms: gating delay in ms
0352  * @is_suspended: clk gating is suspended when set to 1 which can be used
0353  * during suspend/resume
0354  * @delay_attr: sysfs attribute to control delay_attr
0355  * @enable_attr: sysfs attribute to enable/disable clock gating
0356  * @is_enabled: Indicates the current status of clock gating
0357  * @is_initialized: Indicates whether clock gating is initialized or not
0358  * @active_reqs: number of requests that are pending and should be waited for
0359  * completion before gating clocks.
0360  * @clk_gating_workq: workqueue for clock gating work.
0361  */
0362 struct ufs_clk_gating {
0363     struct delayed_work gate_work;
0364     struct work_struct ungate_work;
0365     enum clk_gating_state state;
0366     unsigned long delay_ms;
0367     bool is_suspended;
0368     struct device_attribute delay_attr;
0369     struct device_attribute enable_attr;
0370     bool is_enabled;
0371     bool is_initialized;
0372     int active_reqs;
0373     struct workqueue_struct *clk_gating_workq;
0374 };
0375 
0376 struct ufs_saved_pwr_info {
0377     struct ufs_pa_layer_attr info;
0378     bool is_valid;
0379 };
0380 
0381 /**
0382  * struct ufs_clk_scaling - UFS clock scaling related data
0383  * @active_reqs: number of requests that are pending. If this is zero when
0384  * devfreq ->target() function is called then schedule "suspend_work" to
0385  * suspend devfreq.
0386  * @tot_busy_t: Total busy time in current polling window
0387  * @window_start_t: Start time (in jiffies) of the current polling window
0388  * @busy_start_t: Start time of current busy period
0389  * @enable_attr: sysfs attribute to enable/disable clock scaling
0390  * @saved_pwr_info: UFS power mode may also be changed during scaling and this
0391  * one keeps track of previous power mode.
0392  * @workq: workqueue to schedule devfreq suspend/resume work
0393  * @suspend_work: worker to suspend devfreq
0394  * @resume_work: worker to resume devfreq
0395  * @min_gear: lowest HS gear to scale down to
0396  * @is_enabled: tracks if scaling is currently enabled or not, controlled by
0397  *      clkscale_enable sysfs node
0398  * @is_allowed: tracks if scaling is currently allowed or not, used to block
0399  *      clock scaling which is not invoked from devfreq governor
0400  * @is_initialized: Indicates whether clock scaling is initialized or not
0401  * @is_busy_started: tracks if busy period has started or not
0402  * @is_suspended: tracks if devfreq is suspended or not
0403  */
0404 struct ufs_clk_scaling {
0405     int active_reqs;
0406     unsigned long tot_busy_t;
0407     ktime_t window_start_t;
0408     ktime_t busy_start_t;
0409     struct device_attribute enable_attr;
0410     struct ufs_saved_pwr_info saved_pwr_info;
0411     struct workqueue_struct *workq;
0412     struct work_struct suspend_work;
0413     struct work_struct resume_work;
0414     u32 min_gear;
0415     bool is_enabled;
0416     bool is_allowed;
0417     bool is_initialized;
0418     bool is_busy_started;
0419     bool is_suspended;
0420 };
0421 
0422 #define UFS_EVENT_HIST_LENGTH 8
0423 /**
0424  * struct ufs_event_hist - keeps history of errors
0425  * @pos: index to indicate cyclic buffer position
0426  * @val: cyclic buffer for registers value
0427  * @tstamp: cyclic buffer for time stamp
0428  * @cnt: error counter
0429  */
0430 struct ufs_event_hist {
0431     int pos;
0432     u32 val[UFS_EVENT_HIST_LENGTH];
0433     ktime_t tstamp[UFS_EVENT_HIST_LENGTH];
0434     unsigned long long cnt;
0435 };
0436 
0437 /**
0438  * struct ufs_stats - keeps usage/err statistics
0439  * @last_intr_status: record the last interrupt status.
0440  * @last_intr_ts: record the last interrupt timestamp.
0441  * @hibern8_exit_cnt: Counter to keep track of number of exits,
0442  *      reset this after link-startup.
0443  * @last_hibern8_exit_tstamp: Set time after the hibern8 exit.
0444  *      Clear after the first successful command completion.
0445  * @event: array with event history.
0446  */
0447 struct ufs_stats {
0448     u32 last_intr_status;
0449     ktime_t last_intr_ts;
0450 
0451     u32 hibern8_exit_cnt;
0452     ktime_t last_hibern8_exit_tstamp;
0453     struct ufs_event_hist event[UFS_EVT_CNT];
0454 };
0455 
0456 /**
0457  * enum ufshcd_state - UFS host controller state
0458  * @UFSHCD_STATE_RESET: Link is not operational. Postpone SCSI command
0459  *  processing.
0460  * @UFSHCD_STATE_OPERATIONAL: The host controller is operational and can process
0461  *  SCSI commands.
0462  * @UFSHCD_STATE_EH_SCHEDULED_NON_FATAL: The error handler has been scheduled.
0463  *  SCSI commands may be submitted to the controller.
0464  * @UFSHCD_STATE_EH_SCHEDULED_FATAL: The error handler has been scheduled. Fail
0465  *  newly submitted SCSI commands with error code DID_BAD_TARGET.
0466  * @UFSHCD_STATE_ERROR: An unrecoverable error occurred, e.g. link recovery
0467  *  failed. Fail all SCSI commands with error code DID_ERROR.
0468  */
0469 enum ufshcd_state {
0470     UFSHCD_STATE_RESET,
0471     UFSHCD_STATE_OPERATIONAL,
0472     UFSHCD_STATE_EH_SCHEDULED_NON_FATAL,
0473     UFSHCD_STATE_EH_SCHEDULED_FATAL,
0474     UFSHCD_STATE_ERROR,
0475 };
0476 
0477 enum ufshcd_quirks {
0478     /* Interrupt aggregation support is broken */
0479     UFSHCD_QUIRK_BROKEN_INTR_AGGR           = 1 << 0,
0480 
0481     /*
0482      * delay before each dme command is required as the unipro
0483      * layer has shown instabilities
0484      */
0485     UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS      = 1 << 1,
0486 
0487     /*
0488      * If UFS host controller is having issue in processing LCC (Line
0489      * Control Command) coming from device then enable this quirk.
0490      * When this quirk is enabled, host controller driver should disable
0491      * the LCC transmission on UFS device (by clearing TX_LCC_ENABLE
0492      * attribute of device to 0).
0493      */
0494     UFSHCD_QUIRK_BROKEN_LCC             = 1 << 2,
0495 
0496     /*
0497      * The attribute PA_RXHSUNTERMCAP specifies whether or not the
0498      * inbound Link supports unterminated line in HS mode. Setting this
0499      * attribute to 1 fixes moving to HS gear.
0500      */
0501     UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP        = 1 << 3,
0502 
0503     /*
0504      * This quirk needs to be enabled if the host controller only allows
0505      * accessing the peer dme attributes in AUTO mode (FAST AUTO or
0506      * SLOW AUTO).
0507      */
0508     UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE      = 1 << 4,
0509 
0510     /*
0511      * This quirk needs to be enabled if the host controller doesn't
0512      * advertise the correct version in UFS_VER register. If this quirk
0513      * is enabled, standard UFS host driver will call the vendor specific
0514      * ops (get_ufs_hci_version) to get the correct version.
0515      */
0516     UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION     = 1 << 5,
0517 
0518     /*
0519      * Clear handling for transfer/task request list is just opposite.
0520      */
0521     UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR        = 1 << 6,
0522 
0523     /*
0524      * This quirk needs to be enabled if host controller doesn't allow
0525      * that the interrupt aggregation timer and counter are reset by s/w.
0526      */
0527     UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR       = 1 << 7,
0528 
0529     /*
0530      * This quirks needs to be enabled if host controller cannot be
0531      * enabled via HCE register.
0532      */
0533     UFSHCI_QUIRK_BROKEN_HCE             = 1 << 8,
0534 
0535     /*
0536      * This quirk needs to be enabled if the host controller regards
0537      * resolution of the values of PRDTO and PRDTL in UTRD as byte.
0538      */
0539     UFSHCD_QUIRK_PRDT_BYTE_GRAN         = 1 << 9,
0540 
0541     /*
0542      * This quirk needs to be enabled if the host controller reports
0543      * OCS FATAL ERROR with device error through sense data
0544      */
0545     UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR     = 1 << 10,
0546 
0547     /*
0548      * This quirk needs to be enabled if the host controller has
0549      * auto-hibernate capability but it doesn't work.
0550      */
0551     UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8        = 1 << 11,
0552 
0553     /*
0554      * This quirk needs to disable manual flush for write booster
0555      */
0556     UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL      = 1 << 12,
0557 
0558     /*
0559      * This quirk needs to disable unipro timeout values
0560      * before power mode change
0561      */
0562     UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING = 1 << 13,
0563 
0564     /*
0565      * This quirk allows only sg entries aligned with page size.
0566      */
0567     UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE        = 1 << 14,
0568 
0569     /*
0570      * This quirk needs to be enabled if the host controller does not
0571      * support UIC command
0572      */
0573     UFSHCD_QUIRK_BROKEN_UIC_CMD         = 1 << 15,
0574 
0575     /*
0576      * This quirk needs to be enabled if the host controller cannot
0577      * support physical host configuration.
0578      */
0579     UFSHCD_QUIRK_SKIP_PH_CONFIGURATION      = 1 << 16,
0580 
0581     /*
0582      * This quirk needs to be enabled if the host controller has
0583      * 64-bit addressing supported capability but it doesn't work.
0584      */
0585     UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS       = 1 << 17,
0586 
0587     /*
0588      * This quirk needs to be enabled if the host controller has
0589      * auto-hibernate capability but it's FASTAUTO only.
0590      */
0591     UFSHCD_QUIRK_HIBERN_FASTAUTO            = 1 << 18,
0592 };
0593 
0594 enum ufshcd_caps {
0595     /* Allow dynamic clk gating */
0596     UFSHCD_CAP_CLK_GATING               = 1 << 0,
0597 
0598     /* Allow hiberb8 with clk gating */
0599     UFSHCD_CAP_HIBERN8_WITH_CLK_GATING      = 1 << 1,
0600 
0601     /* Allow dynamic clk scaling */
0602     UFSHCD_CAP_CLK_SCALING              = 1 << 2,
0603 
0604     /* Allow auto bkops to enabled during runtime suspend */
0605     UFSHCD_CAP_AUTO_BKOPS_SUSPEND           = 1 << 3,
0606 
0607     /*
0608      * This capability allows host controller driver to use the UFS HCI's
0609      * interrupt aggregation capability.
0610      * CAUTION: Enabling this might reduce overall UFS throughput.
0611      */
0612     UFSHCD_CAP_INTR_AGGR                = 1 << 4,
0613 
0614     /*
0615      * This capability allows the device auto-bkops to be always enabled
0616      * except during suspend (both runtime and suspend).
0617      * Enabling this capability means that device will always be allowed
0618      * to do background operation when it's active but it might degrade
0619      * the performance of ongoing read/write operations.
0620      */
0621     UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND = 1 << 5,
0622 
0623     /*
0624      * This capability allows host controller driver to automatically
0625      * enable runtime power management by itself instead of waiting
0626      * for userspace to control the power management.
0627      */
0628     UFSHCD_CAP_RPM_AUTOSUSPEND          = 1 << 6,
0629 
0630     /*
0631      * This capability allows the host controller driver to turn-on
0632      * WriteBooster, if the underlying device supports it and is
0633      * provisioned to be used. This would increase the write performance.
0634      */
0635     UFSHCD_CAP_WB_EN                = 1 << 7,
0636 
0637     /*
0638      * This capability allows the host controller driver to use the
0639      * inline crypto engine, if it is present
0640      */
0641     UFSHCD_CAP_CRYPTO               = 1 << 8,
0642 
0643     /*
0644      * This capability allows the controller regulators to be put into
0645      * lpm mode aggressively during clock gating.
0646      * This would increase power savings.
0647      */
0648     UFSHCD_CAP_AGGR_POWER_COLLAPSE          = 1 << 9,
0649 
0650     /*
0651      * This capability allows the host controller driver to use DeepSleep,
0652      * if it is supported by the UFS device. The host controller driver must
0653      * support device hardware reset via the hba->device_reset() callback,
0654      * in order to exit DeepSleep state.
0655      */
0656     UFSHCD_CAP_DEEPSLEEP                = 1 << 10,
0657 
0658     /*
0659      * This capability allows the host controller driver to use temperature
0660      * notification if it is supported by the UFS device.
0661      */
0662     UFSHCD_CAP_TEMP_NOTIF               = 1 << 11,
0663 };
0664 
0665 struct ufs_hba_variant_params {
0666     struct devfreq_dev_profile devfreq_profile;
0667     struct devfreq_simple_ondemand_data ondemand_data;
0668     u16 hba_enable_delay_us;
0669     u32 wb_flush_threshold;
0670 };
0671 
0672 #ifdef CONFIG_SCSI_UFS_HPB
0673 /**
0674  * struct ufshpb_dev_info - UFSHPB device related info
0675  * @num_lu: the number of user logical unit to check whether all lu finished
0676  *          initialization
0677  * @rgn_size: device reported HPB region size
0678  * @srgn_size: device reported HPB sub-region size
0679  * @slave_conf_cnt: counter to check all lu finished initialization
0680  * @hpb_disabled: flag to check if HPB is disabled
0681  * @max_hpb_single_cmd: device reported bMAX_DATA_SIZE_FOR_SINGLE_CMD value
0682  * @is_legacy: flag to check HPB 1.0
0683  * @control_mode: either host or device
0684  */
0685 struct ufshpb_dev_info {
0686     int num_lu;
0687     int rgn_size;
0688     int srgn_size;
0689     atomic_t slave_conf_cnt;
0690     bool hpb_disabled;
0691     u8 max_hpb_single_cmd;
0692     bool is_legacy;
0693     u8 control_mode;
0694 };
0695 #endif
0696 
0697 struct ufs_hba_monitor {
0698     unsigned long chunk_size;
0699 
0700     unsigned long nr_sec_rw[2];
0701     ktime_t total_busy[2];
0702 
0703     unsigned long nr_req[2];
0704     /* latencies*/
0705     ktime_t lat_sum[2];
0706     ktime_t lat_max[2];
0707     ktime_t lat_min[2];
0708 
0709     u32 nr_queued[2];
0710     ktime_t busy_start_ts[2];
0711 
0712     ktime_t enabled_ts;
0713     bool enabled;
0714 };
0715 
0716 /**
0717  * struct ufs_hba - per adapter private structure
0718  * @mmio_base: UFSHCI base register address
0719  * @ucdl_base_addr: UFS Command Descriptor base address
0720  * @utrdl_base_addr: UTP Transfer Request Descriptor base address
0721  * @utmrdl_base_addr: UTP Task Management Descriptor base address
0722  * @ucdl_dma_addr: UFS Command Descriptor DMA address
0723  * @utrdl_dma_addr: UTRDL DMA address
0724  * @utmrdl_dma_addr: UTMRDL DMA address
0725  * @host: Scsi_Host instance of the driver
0726  * @dev: device handle
0727  * @ufs_device_wlun: WLUN that controls the entire UFS device.
0728  * @hwmon_device: device instance registered with the hwmon core.
0729  * @curr_dev_pwr_mode: active UFS device power mode.
0730  * @uic_link_state: active state of the link to the UFS device.
0731  * @rpm_lvl: desired UFS power management level during runtime PM.
0732  * @spm_lvl: desired UFS power management level during system PM.
0733  * @pm_op_in_progress: whether or not a PM operation is in progress.
0734  * @ahit: value of Auto-Hibernate Idle Timer register.
0735  * @lrb: local reference block
0736  * @outstanding_tasks: Bits representing outstanding task requests
0737  * @outstanding_lock: Protects @outstanding_reqs.
0738  * @outstanding_reqs: Bits representing outstanding transfer requests
0739  * @capabilities: UFS Controller Capabilities
0740  * @nutrs: Transfer Request Queue depth supported by controller
0741  * @nutmrs: Task Management Queue depth supported by controller
0742  * @reserved_slot: Used to submit device commands. Protected by @dev_cmd.lock.
0743  * @ufs_version: UFS Version to which controller complies
0744  * @vops: pointer to variant specific operations
0745  * @vps: pointer to variant specific parameters
0746  * @priv: pointer to variant specific private data
0747  * @irq: Irq number of the controller
0748  * @is_irq_enabled: whether or not the UFS controller interrupt is enabled.
0749  * @dev_ref_clk_freq: reference clock frequency
0750  * @quirks: bitmask with information about deviations from the UFSHCI standard.
0751  * @dev_quirks: bitmask with information about deviations from the UFS standard.
0752  * @tmf_tag_set: TMF tag set.
0753  * @tmf_queue: Used to allocate TMF tags.
0754  * @tmf_rqs: array with pointers to TMF requests while these are in progress.
0755  * @active_uic_cmd: handle of active UIC command
0756  * @uic_cmd_mutex: mutex for UIC command
0757  * @uic_async_done: completion used during UIC processing
0758  * @ufshcd_state: UFSHCD state
0759  * @eh_flags: Error handling flags
0760  * @intr_mask: Interrupt Mask Bits
0761  * @ee_ctrl_mask: Exception event control mask
0762  * @ee_drv_mask: Exception event mask for driver
0763  * @ee_usr_mask: Exception event mask for user (set via debugfs)
0764  * @ee_ctrl_mutex: Used to serialize exception event information.
0765  * @is_powered: flag to check if HBA is powered
0766  * @shutting_down: flag to check if shutdown has been invoked
0767  * @host_sem: semaphore used to serialize concurrent contexts
0768  * @eh_wq: Workqueue that eh_work works on
0769  * @eh_work: Worker to handle UFS errors that require s/w attention
0770  * @eeh_work: Worker to handle exception events
0771  * @errors: HBA errors
0772  * @uic_error: UFS interconnect layer error status
0773  * @saved_err: sticky error mask
0774  * @saved_uic_err: sticky UIC error mask
0775  * @ufs_stats: various error counters
0776  * @force_reset: flag to force eh_work perform a full reset
0777  * @force_pmc: flag to force a power mode change
0778  * @silence_err_logs: flag to silence error logs
0779  * @dev_cmd: ufs device management command information
0780  * @last_dme_cmd_tstamp: time stamp of the last completed DME command
0781  * @nop_out_timeout: NOP OUT timeout value
0782  * @dev_info: information about the UFS device
0783  * @auto_bkops_enabled: to track whether bkops is enabled in device
0784  * @vreg_info: UFS device voltage regulator information
0785  * @clk_list_head: UFS host controller clocks list node head
0786  * @req_abort_count: number of times ufshcd_abort() has been called
0787  * @lanes_per_direction: number of lanes per data direction between the UFS
0788  *  controller and the UFS device.
0789  * @pwr_info: holds current power mode
0790  * @max_pwr_info: keeps the device max valid pwm
0791  * @clk_gating: information related to clock gating
0792  * @caps: bitmask with information about UFS controller capabilities
0793  * @devfreq: frequency scaling information owned by the devfreq core
0794  * @clk_scaling: frequency scaling information owned by the UFS driver
0795  * @is_sys_suspended: whether or not the entire system has been suspended
0796  * @urgent_bkops_lvl: keeps track of urgent bkops level for device
0797  * @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for
0798  *  device is known or not.
0799  * @clk_scaling_lock: used to serialize device commands and clock scaling
0800  * @desc_size: descriptor sizes reported by device
0801  * @scsi_block_reqs_cnt: reference counting for scsi block requests
0802  * @bsg_dev: struct device associated with the BSG queue
0803  * @bsg_queue: BSG queue associated with the UFS controller
0804  * @rpm_dev_flush_recheck_work: used to suspend from RPM (runtime power
0805  *  management) after the UFS device has finished a WriteBooster buffer
0806  *  flush or auto BKOP.
0807  * @ufshpb_dev: information related to HPB (Host Performance Booster).
0808  * @monitor: statistics about UFS commands
0809  * @crypto_capabilities: Content of crypto capabilities register (0x100)
0810  * @crypto_cap_array: Array of crypto capabilities
0811  * @crypto_cfg_register: Start of the crypto cfg array
0812  * @crypto_profile: the crypto profile of this hba (if applicable)
0813  * @debugfs_root: UFS controller debugfs root directory
0814  * @debugfs_ee_work: used to restore ee_ctrl_mask after a delay
0815  * @debugfs_ee_rate_limit_ms: user configurable delay after which to restore
0816  *  ee_ctrl_mask
0817  * @luns_avail: number of regular and well known LUNs supported by the UFS
0818  *  device
0819  * @complete_put: whether or not to call ufshcd_rpm_put() from inside
0820  *  ufshcd_resume_complete()
0821  */
0822 struct ufs_hba {
0823     void __iomem *mmio_base;
0824 
0825     /* Virtual memory reference */
0826     struct utp_transfer_cmd_desc *ucdl_base_addr;
0827     struct utp_transfer_req_desc *utrdl_base_addr;
0828     struct utp_task_req_desc *utmrdl_base_addr;
0829 
0830     /* DMA memory reference */
0831     dma_addr_t ucdl_dma_addr;
0832     dma_addr_t utrdl_dma_addr;
0833     dma_addr_t utmrdl_dma_addr;
0834 
0835     struct Scsi_Host *host;
0836     struct device *dev;
0837     struct scsi_device *ufs_device_wlun;
0838 
0839 #ifdef CONFIG_SCSI_UFS_HWMON
0840     struct device *hwmon_device;
0841 #endif
0842 
0843     enum ufs_dev_pwr_mode curr_dev_pwr_mode;
0844     enum uic_link_state uic_link_state;
0845     /* Desired UFS power management level during runtime PM */
0846     enum ufs_pm_level rpm_lvl;
0847     /* Desired UFS power management level during system PM */
0848     enum ufs_pm_level spm_lvl;
0849     int pm_op_in_progress;
0850 
0851     /* Auto-Hibernate Idle Timer register value */
0852     u32 ahit;
0853 
0854     struct ufshcd_lrb *lrb;
0855 
0856     unsigned long outstanding_tasks;
0857     spinlock_t outstanding_lock;
0858     unsigned long outstanding_reqs;
0859 
0860     u32 capabilities;
0861     int nutrs;
0862     int nutmrs;
0863     u32 reserved_slot;
0864     u32 ufs_version;
0865     const struct ufs_hba_variant_ops *vops;
0866     struct ufs_hba_variant_params *vps;
0867     void *priv;
0868     unsigned int irq;
0869     bool is_irq_enabled;
0870     enum ufs_ref_clk_freq dev_ref_clk_freq;
0871 
0872     unsigned int quirks;    /* Deviations from standard UFSHCI spec. */
0873 
0874     /* Device deviations from standard UFS device spec. */
0875     unsigned int dev_quirks;
0876 
0877     struct blk_mq_tag_set tmf_tag_set;
0878     struct request_queue *tmf_queue;
0879     struct request **tmf_rqs;
0880 
0881     struct uic_command *active_uic_cmd;
0882     struct mutex uic_cmd_mutex;
0883     struct completion *uic_async_done;
0884 
0885     enum ufshcd_state ufshcd_state;
0886     u32 eh_flags;
0887     u32 intr_mask;
0888     u16 ee_ctrl_mask;
0889     u16 ee_drv_mask;
0890     u16 ee_usr_mask;
0891     struct mutex ee_ctrl_mutex;
0892     bool is_powered;
0893     bool shutting_down;
0894     struct semaphore host_sem;
0895 
0896     /* Work Queues */
0897     struct workqueue_struct *eh_wq;
0898     struct work_struct eh_work;
0899     struct work_struct eeh_work;
0900 
0901     /* HBA Errors */
0902     u32 errors;
0903     u32 uic_error;
0904     u32 saved_err;
0905     u32 saved_uic_err;
0906     struct ufs_stats ufs_stats;
0907     bool force_reset;
0908     bool force_pmc;
0909     bool silence_err_logs;
0910 
0911     /* Device management request data */
0912     struct ufs_dev_cmd dev_cmd;
0913     ktime_t last_dme_cmd_tstamp;
0914     int nop_out_timeout;
0915 
0916     /* Keeps information of the UFS device connected to this host */
0917     struct ufs_dev_info dev_info;
0918     bool auto_bkops_enabled;
0919     struct ufs_vreg_info vreg_info;
0920     struct list_head clk_list_head;
0921 
0922     /* Number of requests aborts */
0923     int req_abort_count;
0924 
0925     /* Number of lanes available (1 or 2) for Rx/Tx */
0926     u32 lanes_per_direction;
0927     struct ufs_pa_layer_attr pwr_info;
0928     struct ufs_pwr_mode_info max_pwr_info;
0929 
0930     struct ufs_clk_gating clk_gating;
0931     /* Control to enable/disable host capabilities */
0932     u32 caps;
0933 
0934     struct devfreq *devfreq;
0935     struct ufs_clk_scaling clk_scaling;
0936     bool is_sys_suspended;
0937 
0938     enum bkops_status urgent_bkops_lvl;
0939     bool is_urgent_bkops_lvl_checked;
0940 
0941     struct rw_semaphore clk_scaling_lock;
0942     unsigned char desc_size[QUERY_DESC_IDN_MAX];
0943     atomic_t scsi_block_reqs_cnt;
0944 
0945     struct device       bsg_dev;
0946     struct request_queue    *bsg_queue;
0947     struct delayed_work rpm_dev_flush_recheck_work;
0948 
0949 #ifdef CONFIG_SCSI_UFS_HPB
0950     struct ufshpb_dev_info ufshpb_dev;
0951 #endif
0952 
0953     struct ufs_hba_monitor  monitor;
0954 
0955 #ifdef CONFIG_SCSI_UFS_CRYPTO
0956     union ufs_crypto_capabilities crypto_capabilities;
0957     union ufs_crypto_cap_entry *crypto_cap_array;
0958     u32 crypto_cfg_register;
0959     struct blk_crypto_profile crypto_profile;
0960 #endif
0961 #ifdef CONFIG_DEBUG_FS
0962     struct dentry *debugfs_root;
0963     struct delayed_work debugfs_ee_work;
0964     u32 debugfs_ee_rate_limit_ms;
0965 #endif
0966     u32 luns_avail;
0967     bool complete_put;
0968 };
0969 
0970 /* Returns true if clocks can be gated. Otherwise false */
0971 static inline bool ufshcd_is_clkgating_allowed(struct ufs_hba *hba)
0972 {
0973     return hba->caps & UFSHCD_CAP_CLK_GATING;
0974 }
0975 static inline bool ufshcd_can_hibern8_during_gating(struct ufs_hba *hba)
0976 {
0977     return hba->caps & UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
0978 }
0979 static inline int ufshcd_is_clkscaling_supported(struct ufs_hba *hba)
0980 {
0981     return hba->caps & UFSHCD_CAP_CLK_SCALING;
0982 }
0983 static inline bool ufshcd_can_autobkops_during_suspend(struct ufs_hba *hba)
0984 {
0985     return hba->caps & UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
0986 }
0987 static inline bool ufshcd_is_rpm_autosuspend_allowed(struct ufs_hba *hba)
0988 {
0989     return hba->caps & UFSHCD_CAP_RPM_AUTOSUSPEND;
0990 }
0991 
0992 static inline bool ufshcd_is_intr_aggr_allowed(struct ufs_hba *hba)
0993 {
0994     return (hba->caps & UFSHCD_CAP_INTR_AGGR) &&
0995         !(hba->quirks & UFSHCD_QUIRK_BROKEN_INTR_AGGR);
0996 }
0997 
0998 static inline bool ufshcd_can_aggressive_pc(struct ufs_hba *hba)
0999 {
1000     return !!(ufshcd_is_link_hibern8(hba) &&
1001           (hba->caps & UFSHCD_CAP_AGGR_POWER_COLLAPSE));
1002 }
1003 
1004 static inline bool ufshcd_is_auto_hibern8_supported(struct ufs_hba *hba)
1005 {
1006     return (hba->capabilities & MASK_AUTO_HIBERN8_SUPPORT) &&
1007         !(hba->quirks & UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8);
1008 }
1009 
1010 static inline bool ufshcd_is_auto_hibern8_enabled(struct ufs_hba *hba)
1011 {
1012     return FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, hba->ahit);
1013 }
1014 
1015 static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba)
1016 {
1017     return hba->caps & UFSHCD_CAP_WB_EN;
1018 }
1019 
1020 #define ufshcd_writel(hba, val, reg)    \
1021     writel((val), (hba)->mmio_base + (reg))
1022 #define ufshcd_readl(hba, reg)  \
1023     readl((hba)->mmio_base + (reg))
1024 
1025 /**
1026  * ufshcd_rmwl - perform read/modify/write for a controller register
1027  * @hba: per adapter instance
1028  * @mask: mask to apply on read value
1029  * @val: actual value to write
1030  * @reg: register address
1031  */
1032 static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg)
1033 {
1034     u32 tmp;
1035 
1036     tmp = ufshcd_readl(hba, reg);
1037     tmp &= ~mask;
1038     tmp |= (val & mask);
1039     ufshcd_writel(hba, tmp, reg);
1040 }
1041 
1042 int ufshcd_alloc_host(struct device *, struct ufs_hba **);
1043 void ufshcd_dealloc_host(struct ufs_hba *);
1044 int ufshcd_hba_enable(struct ufs_hba *hba);
1045 int ufshcd_init(struct ufs_hba *, void __iomem *, unsigned int);
1046 int ufshcd_link_recovery(struct ufs_hba *hba);
1047 int ufshcd_make_hba_operational(struct ufs_hba *hba);
1048 void ufshcd_remove(struct ufs_hba *);
1049 int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
1050 int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
1051 void ufshcd_delay_us(unsigned long us, unsigned long tolerance);
1052 void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk);
1053 void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val);
1054 void ufshcd_hba_stop(struct ufs_hba *hba);
1055 void ufshcd_schedule_eh_work(struct ufs_hba *hba);
1056 
1057 static inline void check_upiu_size(void)
1058 {
1059     BUILD_BUG_ON(ALIGNED_UPIU_SIZE <
1060         GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE);
1061 }
1062 
1063 /**
1064  * ufshcd_set_variant - set variant specific data to the hba
1065  * @hba: per adapter instance
1066  * @variant: pointer to variant specific data
1067  */
1068 static inline void ufshcd_set_variant(struct ufs_hba *hba, void *variant)
1069 {
1070     BUG_ON(!hba);
1071     hba->priv = variant;
1072 }
1073 
1074 /**
1075  * ufshcd_get_variant - get variant specific data from the hba
1076  * @hba: per adapter instance
1077  */
1078 static inline void *ufshcd_get_variant(struct ufs_hba *hba)
1079 {
1080     BUG_ON(!hba);
1081     return hba->priv;
1082 }
1083 
1084 #ifdef CONFIG_PM
1085 extern int ufshcd_runtime_suspend(struct device *dev);
1086 extern int ufshcd_runtime_resume(struct device *dev);
1087 #endif
1088 #ifdef CONFIG_PM_SLEEP
1089 extern int ufshcd_system_suspend(struct device *dev);
1090 extern int ufshcd_system_resume(struct device *dev);
1091 #endif
1092 extern int ufshcd_shutdown(struct ufs_hba *hba);
1093 extern int ufshcd_dme_configure_adapt(struct ufs_hba *hba,
1094                       int agreed_gear,
1095                       int adapt_val);
1096 extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
1097                    u8 attr_set, u32 mib_val, u8 peer);
1098 extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
1099                    u32 *mib_val, u8 peer);
1100 extern int ufshcd_config_pwr_mode(struct ufs_hba *hba,
1101             struct ufs_pa_layer_attr *desired_pwr_mode);
1102 extern int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode);
1103 
1104 /* UIC command interfaces for DME primitives */
1105 #define DME_LOCAL   0
1106 #define DME_PEER    1
1107 #define ATTR_SET_NOR    0   /* NORMAL */
1108 #define ATTR_SET_ST 1   /* STATIC */
1109 
1110 static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
1111                  u32 mib_val)
1112 {
1113     return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
1114                    mib_val, DME_LOCAL);
1115 }
1116 
1117 static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,
1118                     u32 mib_val)
1119 {
1120     return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
1121                    mib_val, DME_LOCAL);
1122 }
1123 
1124 static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
1125                       u32 mib_val)
1126 {
1127     return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
1128                    mib_val, DME_PEER);
1129 }
1130 
1131 static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,
1132                      u32 mib_val)
1133 {
1134     return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
1135                    mib_val, DME_PEER);
1136 }
1137 
1138 static inline int ufshcd_dme_get(struct ufs_hba *hba,
1139                  u32 attr_sel, u32 *mib_val)
1140 {
1141     return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
1142 }
1143 
1144 static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
1145                       u32 attr_sel, u32 *mib_val)
1146 {
1147     return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
1148 }
1149 
1150 static inline bool ufshcd_is_hs_mode(struct ufs_pa_layer_attr *pwr_info)
1151 {
1152     return (pwr_info->pwr_rx == FAST_MODE ||
1153         pwr_info->pwr_rx == FASTAUTO_MODE) &&
1154         (pwr_info->pwr_tx == FAST_MODE ||
1155         pwr_info->pwr_tx == FASTAUTO_MODE);
1156 }
1157 
1158 static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba)
1159 {
1160     return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0);
1161 }
1162 
1163 /* Expose Query-Request API */
1164 int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
1165                   enum query_opcode opcode,
1166                   enum desc_idn idn, u8 index,
1167                   u8 selector,
1168                   u8 *desc_buf, int *buf_len);
1169 int ufshcd_read_desc_param(struct ufs_hba *hba,
1170                enum desc_idn desc_id,
1171                int desc_index,
1172                u8 param_offset,
1173                u8 *param_read_buf,
1174                u8 param_size);
1175 int ufshcd_query_attr_retry(struct ufs_hba *hba, enum query_opcode opcode,
1176                 enum attr_idn idn, u8 index, u8 selector,
1177                 u32 *attr_val);
1178 int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
1179               enum attr_idn idn, u8 index, u8 selector, u32 *attr_val);
1180 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1181     enum flag_idn idn, u8 index, bool *flag_res);
1182 
1183 void ufshcd_auto_hibern8_enable(struct ufs_hba *hba);
1184 void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit);
1185 void ufshcd_fixup_dev_quirks(struct ufs_hba *hba,
1186                  const struct ufs_dev_quirk *fixups);
1187 #define SD_ASCII_STD true
1188 #define SD_RAW false
1189 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
1190                 u8 **buf, bool ascii);
1191 
1192 int ufshcd_hold(struct ufs_hba *hba, bool async);
1193 void ufshcd_release(struct ufs_hba *hba);
1194 
1195 void ufshcd_clkgate_delay_set(struct device *dev, unsigned long value);
1196 
1197 void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
1198                   int *desc_length);
1199 
1200 u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba);
1201 
1202 int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg);
1203 
1204 int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd);
1205 
1206 int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
1207                  struct utp_upiu_req *req_upiu,
1208                  struct utp_upiu_req *rsp_upiu,
1209                  int msgcode,
1210                  u8 *desc_buff, int *buff_len,
1211                  enum query_opcode desc_op);
1212 
1213 int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable);
1214 int ufshcd_suspend_prepare(struct device *dev);
1215 int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm);
1216 void ufshcd_resume_complete(struct device *dev);
1217 
1218 /* Wrapper functions for safely calling variant operations */
1219 static inline int ufshcd_vops_init(struct ufs_hba *hba)
1220 {
1221     if (hba->vops && hba->vops->init)
1222         return hba->vops->init(hba);
1223 
1224     return 0;
1225 }
1226 
1227 static inline int ufshcd_vops_phy_initialization(struct ufs_hba *hba)
1228 {
1229     if (hba->vops && hba->vops->phy_initialization)
1230         return hba->vops->phy_initialization(hba);
1231 
1232     return 0;
1233 }
1234 
1235 extern const struct ufs_pm_lvl_states ufs_pm_lvl_states[];
1236 
1237 int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
1238              const char *prefix);
1239 
1240 int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask);
1241 int ufshcd_write_ee_control(struct ufs_hba *hba);
1242 int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask,
1243                  const u16 *other_mask, u16 set, u16 clr);
1244 
1245 #endif /* End of Header */