blob: 54e0d1760c2ec9406c095fa735fb9f91026066d7 [file] [log] [blame]
Bean Huo67351112020-06-05 22:05:19 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Vinayak Holikattie0eca632013-02-25 21:44:33 +05302/*
3 * Universal Flash Storage Host controller driver
Vinayak Holikattie0eca632013-02-25 21:44:33 +05304 * Copyright (C) 2011-2013 Samsung India Software Operations
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306 *
7 * Authors:
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053010 */
11
12#ifndef _UFSHCD_H
13#define _UFSHCD_H
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -080023#include <linux/rwsem.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053024#include <linux/workqueue.h>
25#include <linux/errno.h>
26#include <linux/types.h>
27#include <linux/wait.h>
28#include <linux/bitops.h>
29#include <linux/pm_runtime.h>
30#include <linux/clk.h>
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053031#include <linux/completion.h>
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030032#include <linux/regulator/consumer.h>
Stanley Chu5a244e02020-01-29 18:52:50 +080033#include <linux/bitfield.h>
Asutosh Das2c75f9a2020-03-25 11:29:01 -070034#include <linux/devfreq.h>
Satya Tangiralac2b86b72020-06-16 14:33:37 -070035#include <linux/keyslot-manager.h>
Yaniv Gardif37aabc2016-03-10 17:37:20 +020036#include "unipro.h"
Vinayak Holikattie0eca632013-02-25 21:44:33 +053037
38#include <asm/irq.h>
39#include <asm/byteorder.h>
40#include <scsi/scsi.h>
41#include <scsi/scsi_cmnd.h>
42#include <scsi/scsi_host.h>
43#include <scsi/scsi_tcq.h>
44#include <scsi/scsi_dbg.h>
45#include <scsi/scsi_eh.h>
46
47#include "ufs.h"
Stanley Chuc28c00b2020-05-08 16:01:09 +080048#include "ufs_quirks.h"
Vinayak Holikattie0eca632013-02-25 21:44:33 +053049#include "ufshci.h"
50
51#define UFSHCD "ufshcd"
52#define UFSHCD_DRIVER_VERSION "0.2"
53
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030054struct ufs_hba;
55
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053056enum dev_cmd_type {
57 DEV_CMD_TYPE_NOP = 0x0,
Dolev Raviv68078d52013-07-30 00:35:58 +053058 DEV_CMD_TYPE_QUERY = 0x1,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053059};
60
Vinayak Holikattie0eca632013-02-25 21:44:33 +053061/**
62 * struct uic_command - UIC command structure
63 * @command: UIC command
64 * @argument1: UIC command argument 1
65 * @argument2: UIC command argument 2
66 * @argument3: UIC command argument 3
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053067 * @done: UIC command completion
Vinayak Holikattie0eca632013-02-25 21:44:33 +053068 */
69struct uic_command {
70 u32 command;
71 u32 argument1;
72 u32 argument2;
73 u32 argument3;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053074 struct completion done;
Vinayak Holikattie0eca632013-02-25 21:44:33 +053075};
76
Subhash Jadavani57d104c2014-09-25 15:32:30 +030077/* Used to differentiate the power management options */
78enum ufs_pm_op {
79 UFS_RUNTIME_PM,
80 UFS_SYSTEM_PM,
81 UFS_SHUTDOWN_PM,
82};
83
84#define ufshcd_is_runtime_pm(op) ((op) == UFS_RUNTIME_PM)
85#define ufshcd_is_system_pm(op) ((op) == UFS_SYSTEM_PM)
86#define ufshcd_is_shutdown_pm(op) ((op) == UFS_SHUTDOWN_PM)
87
88/* Host <-> Device UniPro Link state */
89enum uic_link_state {
90 UIC_LINK_OFF_STATE = 0, /* Link powered down or disabled */
91 UIC_LINK_ACTIVE_STATE = 1, /* Link is in Fast/Slow/Sleep state */
92 UIC_LINK_HIBERN8_STATE = 2, /* Link is in Hibernate state */
93};
94
95#define ufshcd_is_link_off(hba) ((hba)->uic_link_state == UIC_LINK_OFF_STATE)
96#define ufshcd_is_link_active(hba) ((hba)->uic_link_state == \
97 UIC_LINK_ACTIVE_STATE)
98#define ufshcd_is_link_hibern8(hba) ((hba)->uic_link_state == \
99 UIC_LINK_HIBERN8_STATE)
100#define ufshcd_set_link_off(hba) ((hba)->uic_link_state = UIC_LINK_OFF_STATE)
101#define ufshcd_set_link_active(hba) ((hba)->uic_link_state = \
102 UIC_LINK_ACTIVE_STATE)
103#define ufshcd_set_link_hibern8(hba) ((hba)->uic_link_state = \
104 UIC_LINK_HIBERN8_STATE)
105
Stanley Chu1764fa22020-03-27 17:58:35 +0800106#define ufshcd_set_ufs_dev_active(h) \
107 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
108#define ufshcd_set_ufs_dev_sleep(h) \
109 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
110#define ufshcd_set_ufs_dev_poweroff(h) \
111 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
112#define ufshcd_is_ufs_dev_active(h) \
113 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
114#define ufshcd_is_ufs_dev_sleep(h) \
115 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
116#define ufshcd_is_ufs_dev_poweroff(h) \
117 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
118
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300119/*
120 * UFS Power management levels.
121 * Each level is in increasing order of power savings.
122 */
123enum ufs_pm_level {
124 UFS_PM_LVL_0, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE */
125 UFS_PM_LVL_1, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE */
126 UFS_PM_LVL_2, /* UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE */
127 UFS_PM_LVL_3, /* UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE */
128 UFS_PM_LVL_4, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE */
129 UFS_PM_LVL_5, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE */
130 UFS_PM_LVL_MAX
131};
132
133struct ufs_pm_lvl_states {
134 enum ufs_dev_pwr_mode dev_state;
135 enum uic_link_state link_state;
136};
137
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530138/**
139 * struct ufshcd_lrb - local reference block
140 * @utr_descriptor_ptr: UTRD address of the command
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530141 * @ucd_req_ptr: UCD address of the command
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530142 * @ucd_rsp_ptr: Response UPIU address for this command
143 * @ucd_prdt_ptr: PRDT address of the command
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800144 * @utrd_dma_addr: UTRD dma address for debug
145 * @ucd_prdt_dma_addr: PRDT dma address for debug
146 * @ucd_rsp_dma_addr: UPIU response dma address for debug
147 * @ucd_req_dma_addr: UPIU request dma address for debug
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530148 * @cmd: pointer to SCSI command
149 * @sense_buffer: pointer to sense buffer address of the SCSI command
150 * @sense_bufflen: Length of the sense buffer
151 * @scsi_status: SCSI status of the command
152 * @command_type: SCSI, UFS, Query.
153 * @task_tag: Task tag of the command
154 * @lun: LUN of the command
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530155 * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800156 * @issue_time_stamp: time stamp for debug purposes
Zang Leigang09017182017-09-27 10:06:06 +0800157 * @compl_time_stamp: time stamp for statistics
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700158 * @crypto_key_slot: the key slot to use for inline crypto (-1 if none)
Satya Tangirala53d5b292019-10-24 14:44:28 -0700159 * @data_unit_num: the data unit number for the first block for inline crypto
Gilad Bronere0b299e2017-02-03 16:56:40 -0800160 * @req_abort_skip: skip request abort task flag
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530161 */
162struct ufshcd_lrb {
163 struct utp_transfer_req_desc *utr_descriptor_ptr;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530164 struct utp_upiu_req *ucd_req_ptr;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530165 struct utp_upiu_rsp *ucd_rsp_ptr;
166 struct ufshcd_sg_entry *ucd_prdt_ptr;
167
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800168 dma_addr_t utrd_dma_addr;
169 dma_addr_t ucd_req_dma_addr;
170 dma_addr_t ucd_rsp_dma_addr;
171 dma_addr_t ucd_prdt_dma_addr;
172
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530173 struct scsi_cmnd *cmd;
174 u8 *sense_buffer;
175 unsigned int sense_bufflen;
176 int scsi_status;
177
178 int command_type;
179 int task_tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +0300180 u8 lun; /* UPIU LUN id field is only 8-bit wide */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530181 bool intr_cmd;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800182 ktime_t issue_time_stamp;
Zang Leigang09017182017-09-27 10:06:06 +0800183 ktime_t compl_time_stamp;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700184#ifdef CONFIG_SCSI_UFS_CRYPTO
185 int crypto_key_slot;
Satya Tangirala53d5b292019-10-24 14:44:28 -0700186 u64 data_unit_num;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700187#endif
Gilad Bronere0b299e2017-02-03 16:56:40 -0800188
189 bool req_abort_skip;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530190};
191
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530192/**
Tomas Winklera230c2f2016-02-09 10:25:41 +0200193 * struct ufs_query - holds relevant data structures for query request
Dolev Raviv68078d52013-07-30 00:35:58 +0530194 * @request: request upiu and function
195 * @descriptor: buffer for sending/receiving descriptor
196 * @response: response upiu and response
197 */
198struct ufs_query {
199 struct ufs_query_req request;
200 u8 *descriptor;
201 struct ufs_query_res response;
202};
203
204/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530205 * struct ufs_dev_cmd - all assosiated fields with device management commands
206 * @type: device management command type - Query, NOP OUT
207 * @lock: lock to allow one command at a time
208 * @complete: internal commands completion
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530209 */
210struct ufs_dev_cmd {
211 enum dev_cmd_type type;
212 struct mutex lock;
213 struct completion *complete;
Dolev Raviv68078d52013-07-30 00:35:58 +0530214 struct ufs_query query;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530215};
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530216
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300217/**
218 * struct ufs_clk_info - UFS clock related info
219 * @list: list headed by hba->clk_list_head
220 * @clk: clock node
221 * @name: clock name
222 * @max_freq: maximum frequency supported by the clock
Sahitya Tummala4cff6d992014-09-25 15:32:33 +0300223 * @min_freq: min frequency that can be used for clock scaling
Sahitya Tummala856b3482014-09-25 15:32:34 +0300224 * @curr_freq: indicates the current frequency that it is set to
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300225 * @enabled: variable to check against multiple enable/disable
226 */
227struct ufs_clk_info {
228 struct list_head list;
229 struct clk *clk;
230 const char *name;
231 u32 max_freq;
Sahitya Tummala4cff6d992014-09-25 15:32:33 +0300232 u32 min_freq;
Sahitya Tummala856b3482014-09-25 15:32:34 +0300233 u32 curr_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300234 bool enabled;
235};
236
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200237enum ufs_notify_change_status {
238 PRE_CHANGE,
239 POST_CHANGE,
240};
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300241
242struct ufs_pa_layer_attr {
243 u32 gear_rx;
244 u32 gear_tx;
245 u32 lane_rx;
246 u32 lane_tx;
247 u32 pwr_rx;
248 u32 pwr_tx;
249 u32 hs_rate;
250};
251
252struct ufs_pwr_mode_info {
253 bool is_valid;
254 struct ufs_pa_layer_attr info;
255};
256
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300257/**
258 * struct ufs_hba_variant_ops - variant specific callbacks
259 * @name: variant name
260 * @init: called when the driver is initialized
261 * @exit: called to cleanup everything done in init
Yaniv Gardi9949e702015-05-17 18:55:05 +0300262 * @get_ufs_hci_version: called to get UFS HCI version
Sahitya Tummala856b3482014-09-25 15:32:34 +0300263 * @clk_scale_notify: notifies that clks are scaled up/down
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300264 * @setup_clocks: called before touching any of the controller registers
265 * @setup_regulators: called before accessing the host controller
266 * @hce_enable_notify: called before and after HCE enable bit is set to allow
267 * variant specific Uni-Pro initialization.
268 * @link_startup_notify: called before and after Link startup is carried out
269 * to allow variant specific Uni-Pro initialization.
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300270 * @pwr_change_notify: called before and after a power mode change
271 * is carried out to allow vendor spesific capabilities
272 * to be set.
Kiwoong Kim0e675ef2016-11-10 21:14:36 +0900273 * @setup_xfer_req: called before any transfer request is issued
274 * to set some things
Kiwoong Kimd2877be2016-11-10 21:16:15 +0900275 * @setup_task_mgmt: called before any task management request is issued
276 * to set some things
Kiwoong Kimee32c902016-11-10 21:17:43 +0900277 * @hibern8_notify: called around hibern8 enter/exit
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800278 * @apply_dev_quirks: called to apply device specific quirks
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300279 * @suspend: called during host controller PM callback
280 * @resume: called during host controller PM callback
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200281 * @dbg_register_dump: used to dump controller debug information
Joao Pinto4b9ffb52016-05-11 12:21:30 +0100282 * @phy_initialization: used to initialize phys
Bjorn Anderssond8d9f792019-08-28 12:17:54 -0700283 * @device_reset: called to issue a reset pulse on the UFS device
Eric Biggers27b2ed42020-07-14 09:31:24 -0700284 * @program_key: program or evict an inline encryption key
Eric Biggers617dda82020-08-22 10:47:33 -0700285 * @fill_prdt: called after initializing the standard PRDT fields so that any
286 * variant-specific PRDT fields can be initialized too
Jaegeuk Kim9670c1f2020-09-16 10:59:18 -0700287 * @prepare_command: called when receiving a request in the first place
Jaegeuk Kim0b0468f2020-09-13 22:17:05 -0700288 * @update_sysfs: adds vendor-specific sysfs entries
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300289 */
290struct ufs_hba_variant_ops {
291 const char *name;
292 int (*init)(struct ufs_hba *);
293 void (*exit)(struct ufs_hba *);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300294 u32 (*get_ufs_hci_version)(struct ufs_hba *);
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200295 int (*clk_scale_notify)(struct ufs_hba *, bool,
296 enum ufs_notify_change_status);
Subhash Jadavani1e879e82016-10-06 21:48:22 -0700297 int (*setup_clocks)(struct ufs_hba *, bool,
298 enum ufs_notify_change_status);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300299 int (*setup_regulators)(struct ufs_hba *, bool);
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200300 int (*hce_enable_notify)(struct ufs_hba *,
301 enum ufs_notify_change_status);
302 int (*link_startup_notify)(struct ufs_hba *,
303 enum ufs_notify_change_status);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300304 int (*pwr_change_notify)(struct ufs_hba *,
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200305 enum ufs_notify_change_status status,
306 struct ufs_pa_layer_attr *,
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300307 struct ufs_pa_layer_attr *);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +0900308 void (*setup_xfer_req)(struct ufs_hba *, int, bool);
Kiwoong Kimd2877be2016-11-10 21:16:15 +0900309 void (*setup_task_mgmt)(struct ufs_hba *, int, u8);
Kiwoong Kimee32c902016-11-10 21:17:43 +0900310 void (*hibern8_notify)(struct ufs_hba *, enum uic_cmd_dme,
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800311 enum ufs_notify_change_status);
Bean Huo09750062020-01-20 14:08:14 +0100312 int (*apply_dev_quirks)(struct ufs_hba *hba);
Stanley Chuc28c00b2020-05-08 16:01:09 +0800313 void (*fixup_dev_quirks)(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300314 int (*suspend)(struct ufs_hba *, enum ufs_pm_op);
315 int (*resume)(struct ufs_hba *, enum ufs_pm_op);
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200316 void (*dbg_register_dump)(struct ufs_hba *hba);
Joao Pinto4b9ffb52016-05-11 12:21:30 +0100317 int (*phy_initialization)(struct ufs_hba *);
Bjorn Anderssond8d9f792019-08-28 12:17:54 -0700318 void (*device_reset)(struct ufs_hba *hba);
Asutosh Das2c75f9a2020-03-25 11:29:01 -0700319 void (*config_scaling_param)(struct ufs_hba *hba,
320 struct devfreq_dev_profile *profile,
321 void *data);
Eric Biggers884e3642020-01-09 21:10:06 -0800322 int (*program_key)(struct ufs_hba *hba,
323 const union ufs_crypto_cfg_entry *cfg, int slot);
Eric Biggers617dda82020-08-22 10:47:33 -0700324 int (*fill_prdt)(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
325 unsigned int segments);
Jaegeuk Kim9670c1f2020-09-16 10:59:18 -0700326 void (*prepare_command)(struct ufs_hba *hba,
327 struct request *rq, struct ufshcd_lrb *lrbp);
Jaegeuk Kim0b0468f2020-09-13 22:17:05 -0700328 int (*update_sysfs)(struct ufs_hba *hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300329};
330
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300331/* clock gating state */
332enum clk_gating_state {
333 CLKS_OFF,
334 CLKS_ON,
335 REQ_CLKS_OFF,
336 REQ_CLKS_ON,
337};
338
339/**
340 * struct ufs_clk_gating - UFS clock gating related info
341 * @gate_work: worker to turn off clocks after some delay as specified in
342 * delay_ms
343 * @ungate_work: worker to turn on clocks that will be used in case of
344 * interrupt context
345 * @state: the current clocks state
346 * @delay_ms: gating delay in ms
347 * @is_suspended: clk gating is suspended when set to 1 which can be used
348 * during suspend/resume
349 * @delay_attr: sysfs attribute to control delay_attr
Sahitya Tummalab4274112016-12-22 18:40:39 -0800350 * @enable_attr: sysfs attribute to enable/disable clock gating
351 * @is_enabled: Indicates the current status of clock gating
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300352 * @active_reqs: number of requests that are pending and should be waited for
353 * completion before gating clocks.
354 */
355struct ufs_clk_gating {
356 struct delayed_work gate_work;
357 struct work_struct ungate_work;
358 enum clk_gating_state state;
359 unsigned long delay_ms;
360 bool is_suspended;
361 struct device_attribute delay_attr;
Sahitya Tummalab4274112016-12-22 18:40:39 -0800362 struct device_attribute enable_attr;
363 bool is_enabled;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300364 int active_reqs;
Vijay Viswanath10e5e372018-05-03 16:37:22 +0530365 struct workqueue_struct *clk_gating_workq;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300366};
367
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800368struct ufs_saved_pwr_info {
369 struct ufs_pa_layer_attr info;
370 bool is_valid;
371};
372
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800373/**
374 * struct ufs_clk_scaling - UFS clock scaling related data
375 * @active_reqs: number of requests that are pending. If this is zero when
376 * devfreq ->target() function is called then schedule "suspend_work" to
377 * suspend devfreq.
378 * @tot_busy_t: Total busy time in current polling window
379 * @window_start_t: Start time (in jiffies) of the current polling window
380 * @busy_start_t: Start time of current busy period
381 * @enable_attr: sysfs attribute to enable/disable clock scaling
382 * @saved_pwr_info: UFS power mode may also be changed during scaling and this
383 * one keeps track of previous power mode.
384 * @workq: workqueue to schedule devfreq suspend/resume work
385 * @suspend_work: worker to suspend devfreq
386 * @resume_work: worker to resume devfreq
387 * @is_allowed: tracks if scaling is currently allowed or not
388 * @is_busy_started: tracks if busy period has started or not
389 * @is_suspended: tracks if devfreq is suspended or not
390 */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300391struct ufs_clk_scaling {
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800392 int active_reqs;
393 unsigned long tot_busy_t;
Stanley Chub1bf66d2020-06-11 18:10:43 +0800394 ktime_t window_start_t;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800395 ktime_t busy_start_t;
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800396 struct device_attribute enable_attr;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800397 struct ufs_saved_pwr_info saved_pwr_info;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800398 struct workqueue_struct *workq;
399 struct work_struct suspend_work;
400 struct work_struct resume_work;
401 bool is_allowed;
402 bool is_busy_started;
403 bool is_suspended;
Sahitya Tummala856b3482014-09-25 15:32:34 +0300404};
405
Stanley Chu48d5b972019-07-10 21:38:18 +0800406#define UFS_ERR_REG_HIST_LENGTH 8
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800407/**
Stanley Chud3c615b2019-07-10 21:38:19 +0800408 * struct ufs_err_reg_hist - keeps history of errors
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800409 * @pos: index to indicate cyclic buffer position
410 * @reg: cyclic buffer for registers value
411 * @tstamp: cyclic buffer for time stamp
412 */
Stanley Chu48d5b972019-07-10 21:38:18 +0800413struct ufs_err_reg_hist {
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800414 int pos;
Stanley Chu48d5b972019-07-10 21:38:18 +0800415 u32 reg[UFS_ERR_REG_HIST_LENGTH];
416 ktime_t tstamp[UFS_ERR_REG_HIST_LENGTH];
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800417};
418
419/**
420 * struct ufs_stats - keeps usage/err statistics
Can Guo08076c72020-08-09 05:15:50 -0700421 * @last_intr_status: record the last interrupt status.
422 * @last_intr_ts: record the last interrupt timestamp.
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800423 * @hibern8_exit_cnt: Counter to keep track of number of exits,
424 * reset this after link-startup.
425 * @last_hibern8_exit_tstamp: Set time after the hibern8 exit.
426 * Clear after the first successful command completion.
427 * @pa_err: tracks pa-uic errors
428 * @dl_err: tracks dl-uic errors
429 * @nl_err: tracks nl-uic errors
430 * @tl_err: tracks tl-uic errors
431 * @dme_err: tracks dme errors
Stanley Chud3c615b2019-07-10 21:38:19 +0800432 * @auto_hibern8_err: tracks auto-hibernate errors
Stanley Chu8808b4e2019-07-10 21:38:21 +0800433 * @fatal_err: tracks fatal errors
434 * @linkup_err: tracks link-startup errors
435 * @resume_err: tracks resume errors
436 * @suspend_err: tracks suspend errors
437 * @dev_reset: tracks device reset events
438 * @host_reset: tracks host reset events
439 * @tsk_abort: tracks task abort events
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800440 */
441struct ufs_stats {
Can Guo08076c72020-08-09 05:15:50 -0700442 u32 last_intr_status;
443 ktime_t last_intr_ts;
444
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800445 u32 hibern8_exit_cnt;
446 ktime_t last_hibern8_exit_tstamp;
Stanley Chud3c615b2019-07-10 21:38:19 +0800447
448 /* uic specific errors */
Stanley Chu48d5b972019-07-10 21:38:18 +0800449 struct ufs_err_reg_hist pa_err;
450 struct ufs_err_reg_hist dl_err;
451 struct ufs_err_reg_hist nl_err;
452 struct ufs_err_reg_hist tl_err;
453 struct ufs_err_reg_hist dme_err;
Stanley Chud3c615b2019-07-10 21:38:19 +0800454
455 /* fatal errors */
Stanley Chud3c615b2019-07-10 21:38:19 +0800456 struct ufs_err_reg_hist auto_hibern8_err;
Stanley Chu8808b4e2019-07-10 21:38:21 +0800457 struct ufs_err_reg_hist fatal_err;
458 struct ufs_err_reg_hist link_startup_err;
459 struct ufs_err_reg_hist resume_err;
460 struct ufs_err_reg_hist suspend_err;
461
462 /* abnormal events */
463 struct ufs_err_reg_hist dev_reset;
464 struct ufs_err_reg_hist host_reset;
465 struct ufs_err_reg_hist task_abort;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800466};
467
Christoph Hellwigc3f7d1f2020-02-21 06:08:12 -0800468enum ufshcd_quirks {
469 /* Interrupt aggregation support is broken */
470 UFSHCD_QUIRK_BROKEN_INTR_AGGR = 1 << 0,
471
472 /*
473 * delay before each dme command is required as the unipro
474 * layer has shown instabilities
475 */
476 UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS = 1 << 1,
477
478 /*
479 * If UFS host controller is having issue in processing LCC (Line
480 * Control Command) coming from device then enable this quirk.
481 * When this quirk is enabled, host controller driver should disable
482 * the LCC transmission on UFS device (by clearing TX_LCC_ENABLE
483 * attribute of device to 0).
484 */
485 UFSHCD_QUIRK_BROKEN_LCC = 1 << 2,
486
487 /*
488 * The attribute PA_RXHSUNTERMCAP specifies whether or not the
489 * inbound Link supports unterminated line in HS mode. Setting this
490 * attribute to 1 fixes moving to HS gear.
491 */
492 UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP = 1 << 3,
493
494 /*
495 * This quirk needs to be enabled if the host controller only allows
496 * accessing the peer dme attributes in AUTO mode (FAST AUTO or
497 * SLOW AUTO).
498 */
499 UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE = 1 << 4,
500
501 /*
502 * This quirk needs to be enabled if the host controller doesn't
503 * advertise the correct version in UFS_VER register. If this quirk
504 * is enabled, standard UFS host driver will call the vendor specific
505 * ops (get_ufs_hci_version) to get the correct version.
506 */
507 UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION = 1 << 5,
Greg Kroah-Hartman7719f752020-04-07 18:33:53 +0200508
509 /*
Alim Akhtar87183842020-05-28 06:46:49 +0530510 * Clear handling for transfer/task request list is just opposite.
511 */
512 UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR = 1 << 6,
Alim Akhtarb638b5e2020-05-28 06:46:50 +0530513
514 /*
515 * This quirk needs to be enabled if host controller doesn't allow
516 * that the interrupt aggregation timer and counter are reset by s/w.
517 */
518 UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR = 1 << 7,
Alim Akhtar39bf2d82020-05-28 06:46:51 +0530519
520 /*
521 * This quirks needs to be enabled if host controller cannot be
522 * enabled via HCE register.
523 */
524 UFSHCI_QUIRK_BROKEN_HCE = 1 << 8,
Alim Akhtar26f968d2020-05-28 06:46:52 +0530525
526 /*
527 * This quirk needs to be enabled if the host controller regards
528 * resolution of the values of PRDTO and PRDTL in UTRD as byte.
529 */
530 UFSHCD_QUIRK_PRDT_BYTE_GRAN = 1 << 9,
Kiwoong Kimd779a6e2020-05-28 06:46:53 +0530531
532 /*
Greg Kroah-Hartman7719f752020-04-07 18:33:53 +0200533 * This quirk needs to be enabled if the host controller reports
534 * OCS FATAL ERROR with device error through sense data
535 */
Kiwoong Kimd779a6e2020-05-28 06:46:53 +0530536 UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR = 1 << 10,
Adrian Hunter8da76f72020-08-10 17:10:24 +0300537
538 /*
539 * This quirk needs to be enabled if the host controller has
540 * auto-hibernate capability but it doesn't work.
541 */
542 UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8 = 1 << 11,
Eric Biggers73372c92020-08-22 10:47:33 -0700543
544 /*
545 * This quirk needs to be enabled if the host controller supports inline
546 * encryption, but it uses a nonstandard mechanism where the standard
547 * crypto registers aren't used and there is no concept of keyslots.
548 * ufs_hba_variant_ops::init() is expected to initialize ufs_hba::ksm as
549 * a passthrough keyslot manager.
550 */
551 UFSHCD_QUIRK_NO_KEYSLOTS = 1 << 12,
Eric Biggers4ad2fd52020-08-22 10:47:34 -0700552
553 /*
554 * This quirk needs to be enabled if the host controller requires that
555 * the PRDT be cleared after each encrypted request because encryption
556 * keys were stored in it.
557 */
558 UFSHCD_QUIRK_KEYS_IN_PRDT = 1 << 13,
Christoph Hellwigc3f7d1f2020-02-21 06:08:12 -0800559};
560
Stanley Chuc2014682020-03-18 18:40:11 +0800561enum ufshcd_caps {
562 /* Allow dynamic clk gating */
563 UFSHCD_CAP_CLK_GATING = 1 << 0,
564
565 /* Allow hiberb8 with clk gating */
566 UFSHCD_CAP_HIBERN8_WITH_CLK_GATING = 1 << 1,
567
568 /* Allow dynamic clk scaling */
569 UFSHCD_CAP_CLK_SCALING = 1 << 2,
570
571 /* Allow auto bkops to enabled during runtime suspend */
572 UFSHCD_CAP_AUTO_BKOPS_SUSPEND = 1 << 3,
573
574 /*
575 * This capability allows host controller driver to use the UFS HCI's
576 * interrupt aggregation capability.
577 * CAUTION: Enabling this might reduce overall UFS throughput.
578 */
579 UFSHCD_CAP_INTR_AGGR = 1 << 4,
580
581 /*
582 * This capability allows the device auto-bkops to be always enabled
583 * except during suspend (both runtime and suspend).
584 * Enabling this capability means that device will always be allowed
585 * to do background operation when it's active but it might degrade
586 * the performance of ongoing read/write operations.
587 */
588 UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND = 1 << 5,
589
590 /*
591 * This capability allows host controller driver to automatically
592 * enable runtime power management by itself instead of waiting
593 * for userspace to control the power management.
594 */
595 UFSHCD_CAP_RPM_AUTOSUSPEND = 1 << 6,
Greg Kroah-Hartman7719f752020-04-07 18:33:53 +0200596
597 /*
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700598 * This capability allows the host controller driver to turn-on
599 * WriteBooster, if the underlying device supports it and is
600 * provisioned to be used. This would increase the write performance.
601 */
602 UFSHCD_CAP_WB_EN = 1 << 7,
Greg Kroah-Hartmanb3783282020-06-24 07:11:16 +0200603
604 /*
Greg Kroah-Hartman7719f752020-04-07 18:33:53 +0200605 * This capability allows the host controller driver to use the
606 * inline crypto engine, if it is present
607 */
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700608 UFSHCD_CAP_CRYPTO = 1 << 8,
Stanley Chuc2014682020-03-18 18:40:11 +0800609};
610
Stanley Chu90b84912020-05-09 17:37:13 +0800611struct ufs_hba_variant_params {
612 struct devfreq_dev_profile devfreq_profile;
613 struct devfreq_simple_ondemand_data ondemand_data;
614 u16 hba_enable_delay_us;
Stanley Chud14734ae2020-05-09 17:37:15 +0800615 u32 wb_flush_threshold;
Stanley Chu90b84912020-05-09 17:37:13 +0800616};
617
Yaniv Gardi3a4bf062014-09-25 15:32:27 +0300618/**
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530619 * struct ufs_hba - per adapter private structure
620 * @mmio_base: UFSHCI base register address
621 * @ucdl_base_addr: UFS Command Descriptor base address
622 * @utrdl_base_addr: UTP Transfer Request Descriptor base address
623 * @utmrdl_base_addr: UTP Task Management Descriptor base address
624 * @ucdl_dma_addr: UFS Command Descriptor DMA address
625 * @utrdl_dma_addr: UTRDL DMA address
626 * @utmrdl_dma_addr: UTMRDL DMA address
627 * @host: Scsi_Host instance of the driver
628 * @dev: device handle
629 * @lrb: local reference block
Bart Van Assche7252a362019-12-09 10:13:08 -0800630 * @cmd_queue: Used to allocate command tags from hba->host->tag_set.
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530631 * @outstanding_tasks: Bits representing outstanding task requests
632 * @outstanding_reqs: Bits representing outstanding transfer requests
633 * @capabilities: UFS Controller Capabilities
634 * @nutrs: Transfer Request Queue depth supported by controller
635 * @nutmrs: Task Management Queue depth supported by controller
636 * @ufs_version: UFS Version to which controller complies
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300637 * @vops: pointer to variant specific operations
638 * @priv: pointer to variant specific private data
Eric Biggers8de80df2020-02-24 14:37:12 -0800639 * @sg_entry_size: size of struct ufshcd_sg_entry (may include variant fields)
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530640 * @irq: Irq number of the controller
641 * @active_uic_cmd: handle of active UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530642 * @uic_cmd_mutex: mutex for uic command
Bart Van Assche69a6c262019-12-09 10:13:09 -0800643 * @tmf_tag_set: TMF tag set.
644 * @tmf_queue: Used to allocate TMF tags.
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530645 * @pwr_done: completion for power mode change
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530646 * @ufshcd_state: UFSHCD states
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530647 * @eh_flags: Error handling flags
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530648 * @intr_mask: Interrupt Mask Bits
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530649 * @ee_ctrl_mask: Exception event control mask
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300650 * @is_powered: flag to check if HBA is powered
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530651 * @eh_work: Worker to handle UFS errors that require s/w attention
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530652 * @eeh_work: Worker to handle exception events
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530653 * @errors: HBA errors
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530654 * @uic_error: UFS interconnect layer error status
655 * @saved_err: sticky error mask
656 * @saved_uic_err: sticky UIC error mask
Can Guo46c09122019-11-25 22:53:33 -0800657 * @silence_err_logs: flag to silence error logs
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530658 * @dev_cmd: ufs device management command information
Yaniv Gardicad2e032015-03-31 17:37:14 +0300659 * @last_dme_cmd_tstamp: time stamp of the last completed DME command
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530660 * @auto_bkops_enabled: to track whether bkops is enabled in device
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300661 * @vreg_info: UFS device voltage regulator information
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300662 * @clk_list_head: UFS host controller clocks list node head
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300663 * @pwr_info: holds current power mode
664 * @max_pwr_info: keeps the device max valid pwm
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +0000665 * @desc_size: descriptor sizes reported by device
Yaniv Gardiafdfff52016-03-10 17:37:15 +0200666 * @urgent_bkops_lvl: keeps track of urgent bkops level for device
667 * @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for
668 * device is known or not.
Subhash Jadavani38135532018-05-03 16:37:18 +0530669 * @scsi_block_reqs_cnt: reference counting for scsi block requests
Satya Tangirala4bac1102019-10-24 14:44:27 -0700670 * @crypto_capabilities: Content of crypto capabilities register (0x100)
671 * @crypto_cap_array: Array of crypto capabilities
672 * @crypto_cfg_register: Start of the crypto cfg array
Satya Tangirala4bac1102019-10-24 14:44:27 -0700673 * @ksm: the keyslot manager tied to this hba
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530674 */
675struct ufs_hba {
676 void __iomem *mmio_base;
677
678 /* Virtual memory reference */
679 struct utp_transfer_cmd_desc *ucdl_base_addr;
680 struct utp_transfer_req_desc *utrdl_base_addr;
681 struct utp_task_req_desc *utmrdl_base_addr;
682
683 /* DMA memory reference */
684 dma_addr_t ucdl_dma_addr;
685 dma_addr_t utrdl_dma_addr;
686 dma_addr_t utmrdl_dma_addr;
687
688 struct Scsi_Host *host;
689 struct device *dev;
Bart Van Assche7252a362019-12-09 10:13:08 -0800690 struct request_queue *cmd_queue;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +0300691 /*
692 * This field is to keep a reference to "scsi_device" corresponding to
693 * "UFS device" W-LU.
694 */
695 struct scsi_device *sdev_ufs_device;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530696
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300697 enum ufs_dev_pwr_mode curr_dev_pwr_mode;
698 enum uic_link_state uic_link_state;
699 /* Desired UFS power management level during runtime PM */
700 enum ufs_pm_level rpm_lvl;
701 /* Desired UFS power management level during system PM */
702 enum ufs_pm_level spm_lvl;
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -0800703 struct device_attribute rpm_lvl_attr;
704 struct device_attribute spm_lvl_attr;
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300705 int pm_op_in_progress;
706
Adrian Hunterad448372018-03-20 15:07:38 +0200707 /* Auto-Hibernate Idle Timer register value */
708 u32 ahit;
709
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530710 struct ufshcd_lrb *lrb;
711
712 unsigned long outstanding_tasks;
713 unsigned long outstanding_reqs;
714
715 u32 capabilities;
716 int nutrs;
717 int nutmrs;
718 u32 ufs_version;
Arnd Bergmann176eb922019-03-04 20:39:11 +0100719 const struct ufs_hba_variant_ops *vops;
Stanley Chu90b84912020-05-09 17:37:13 +0800720 struct ufs_hba_variant_params *vps;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300721 void *priv;
Eric Biggers8de80df2020-02-24 14:37:12 -0800722 size_t sg_entry_size;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530723 unsigned int irq;
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300724 bool is_irq_enabled;
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +0530725 enum ufs_ref_clk_freq dev_ref_clk_freq;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530726
Yaniv Gardicad2e032015-03-31 17:37:14 +0300727 unsigned int quirks; /* Deviations from standard UFSHCI spec. */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530728
Yaniv Gardic58ab7a2016-03-10 17:37:10 +0200729 /* Device deviations from standard UFS device spec. */
730 unsigned int dev_quirks;
731
Bart Van Assche69a6c262019-12-09 10:13:09 -0800732 struct blk_mq_tag_set tmf_tag_set;
733 struct request_queue *tmf_queue;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530734
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300735 struct uic_command *active_uic_cmd;
736 struct mutex uic_cmd_mutex;
737 struct completion *uic_async_done;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530738
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530739 u32 ufshcd_state;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530740 u32 eh_flags;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530741 u32 intr_mask;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530742 u16 ee_ctrl_mask;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300743 bool is_powered;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530744
745 /* Work Queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530746 struct work_struct eh_work;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530747 struct work_struct eeh_work;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530748
749 /* HBA Errors */
750 u32 errors;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530751 u32 uic_error;
752 u32 saved_err;
753 u32 saved_uic_err;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800754 struct ufs_stats ufs_stats;
Can Guo46c09122019-11-25 22:53:33 -0800755 bool silence_err_logs;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530756
757 /* Device management request data */
758 struct ufs_dev_cmd dev_cmd;
Yaniv Gardicad2e032015-03-31 17:37:14 +0300759 ktime_t last_dme_cmd_tstamp;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530760
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300761 /* Keeps information of the UFS device connected to this host */
762 struct ufs_dev_info dev_info;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530763 bool auto_bkops_enabled;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300764 struct ufs_vreg_info vreg_info;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300765 struct list_head clk_list_head;
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300766
767 bool wlun_dev_clr_ua;
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300768
Gilad Broner7fabb772017-02-03 16:56:50 -0800769 /* Number of requests aborts */
770 int req_abort_count;
771
Yaniv Gardi54b879b2016-03-10 17:37:05 +0200772 /* Number of lanes available (1 or 2) for Rx/Tx */
773 u32 lanes_per_direction;
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300774 struct ufs_pa_layer_attr pwr_info;
775 struct ufs_pwr_mode_info max_pwr_info;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300776
777 struct ufs_clk_gating clk_gating;
778 /* Control to enable/disable host capabilities */
779 u32 caps;
Sahitya Tummala856b3482014-09-25 15:32:34 +0300780
781 struct devfreq *devfreq;
782 struct ufs_clk_scaling clk_scaling;
Dolev Ravive7850602014-09-25 15:32:36 +0300783 bool is_sys_suspended;
Yaniv Gardiafdfff52016-03-10 17:37:15 +0200784
785 enum bkops_status urgent_bkops_lvl;
786 bool is_urgent_bkops_lvl_checked;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800787
788 struct rw_semaphore clk_scaling_lock;
Bean Huo7a0bf852020-06-03 11:19:58 +0200789 unsigned char desc_size[QUERY_DESC_IDN_MAX];
Subhash Jadavani38135532018-05-03 16:37:18 +0530790 atomic_t scsi_block_reqs_cnt;
Avri Altmandf032bf2018-10-07 17:30:35 +0300791
792 struct device bsg_dev;
793 struct request_queue *bsg_queue;
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700794 bool wb_buf_flush_enabled;
795 bool wb_enabled;
Stanley Chu51dd9052020-05-22 16:32:12 +0800796 struct delayed_work rpm_dev_flush_recheck_work;
Satya Tangirala4bac1102019-10-24 14:44:27 -0700797
798#ifdef CONFIG_SCSI_UFS_CRYPTO
Satya Tangirala4bac1102019-10-24 14:44:27 -0700799 union ufs_crypto_capabilities crypto_capabilities;
800 union ufs_crypto_cap_entry *crypto_cap_array;
801 u32 crypto_cfg_register;
Satya Tangiralac2b86b72020-06-16 14:33:37 -0700802 struct blk_keyslot_manager ksm;
803#endif
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530804};
805
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300806/* Returns true if clocks can be gated. Otherwise false */
807static inline bool ufshcd_is_clkgating_allowed(struct ufs_hba *hba)
808{
809 return hba->caps & UFSHCD_CAP_CLK_GATING;
810}
811static inline bool ufshcd_can_hibern8_during_gating(struct ufs_hba *hba)
812{
813 return hba->caps & UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
814}
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800815static inline int ufshcd_is_clkscaling_supported(struct ufs_hba *hba)
Sahitya Tummala856b3482014-09-25 15:32:34 +0300816{
817 return hba->caps & UFSHCD_CAP_CLK_SCALING;
818}
Subhash Jadavani374a2462014-09-25 15:32:35 +0300819static inline bool ufshcd_can_autobkops_during_suspend(struct ufs_hba *hba)
820{
821 return hba->caps & UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
822}
Stanley Chua30de0e2019-09-16 23:56:50 +0800823static inline bool ufshcd_is_rpm_autosuspend_allowed(struct ufs_hba *hba)
824{
825 return hba->caps & UFSHCD_CAP_RPM_AUTOSUSPEND;
826}
Subhash Jadavani374a2462014-09-25 15:32:35 +0300827
Yaniv Gardib8521902015-05-17 18:54:57 +0300828static inline bool ufshcd_is_intr_aggr_allowed(struct ufs_hba *hba)
829{
Joao Pinto4b9ffb52016-05-11 12:21:30 +0100830/* DWC UFS Core has the Interrupt aggregation feature but is not detectable*/
831#ifndef CONFIG_SCSI_UFS_DWC
Yaniv Gardib8521902015-05-17 18:54:57 +0300832 if ((hba->caps & UFSHCD_CAP_INTR_AGGR) &&
833 !(hba->quirks & UFSHCD_QUIRK_BROKEN_INTR_AGGR))
834 return true;
835 else
836 return false;
Joao Pinto4b9ffb52016-05-11 12:21:30 +0100837#else
838return true;
839#endif
Yaniv Gardib8521902015-05-17 18:54:57 +0300840}
841
Stanley Chuee5f1042019-05-21 14:44:52 +0800842static inline bool ufshcd_is_auto_hibern8_supported(struct ufs_hba *hba)
843{
Adrian Hunter8da76f72020-08-10 17:10:24 +0300844 return (hba->capabilities & MASK_AUTO_HIBERN8_SUPPORT) &&
845 !(hba->quirks & UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8);
Stanley Chuee5f1042019-05-21 14:44:52 +0800846}
847
Stanley Chu5a244e02020-01-29 18:52:50 +0800848static inline bool ufshcd_is_auto_hibern8_enabled(struct ufs_hba *hba)
849{
850 return FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, hba->ahit) ? true : false;
851}
852
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700853static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba)
854{
855 return hba->caps & UFSHCD_CAP_WB_EN;
856}
857
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530858#define ufshcd_writel(hba, val, reg) \
859 writel((val), (hba)->mmio_base + (reg))
860#define ufshcd_readl(hba, reg) \
861 readl((hba)->mmio_base + (reg))
862
Dolev Ravive7850602014-09-25 15:32:36 +0300863/**
864 * ufshcd_rmwl - read modify write into a register
865 * @hba - per adapter instance
866 * @mask - mask to apply on read value
867 * @val - actual value to write
868 * @reg - register address
869 */
870static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg)
871{
872 u32 tmp;
873
874 tmp = ufshcd_readl(hba, reg);
875 tmp &= ~mask;
876 tmp |= (val & mask);
877 ufshcd_writel(hba, tmp, reg);
878}
879
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300880int ufshcd_alloc_host(struct device *, struct ufs_hba **);
Yaniv Gardi47555a52015-10-28 13:15:49 +0200881void ufshcd_dealloc_host(struct ufs_hba *);
Stanley Chu9d19bf7a2020-01-17 11:51:07 +0800882int ufshcd_hba_enable(struct ufs_hba *hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300883int ufshcd_init(struct ufs_hba * , void __iomem * , unsigned int);
Stanley Chu087c5ef2020-03-27 17:53:28 +0800884int ufshcd_link_recovery(struct ufs_hba *hba);
Stanley Chu9d19bf7a2020-01-17 11:51:07 +0800885int ufshcd_make_hba_operational(struct ufs_hba *hba);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530886void ufshcd_remove(struct ufs_hba *);
Stanley Chu9d19bf7a2020-01-17 11:51:07 +0800887int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
Stanley Chu5c955c12020-03-18 18:40:12 +0800888void ufshcd_delay_us(unsigned long us, unsigned long tolerance);
Yaniv Gardi596585a2016-03-10 17:37:08 +0200889int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
890 u32 val, unsigned long interval_us,
Bart Van Assche5cac1092020-05-07 15:27:50 -0700891 unsigned long timeout_ms);
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +0530892void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk);
Stanley Chua5fe372d2020-01-04 22:26:07 +0800893void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist,
894 u32 reg);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530895
Dolev Raviv68078d52013-07-30 00:35:58 +0530896static inline void check_upiu_size(void)
897{
898 BUILD_BUG_ON(ALIGNED_UPIU_SIZE <
899 GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE);
900}
901
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200902/**
903 * ufshcd_set_variant - set variant specific data to the hba
904 * @hba - per adapter instance
905 * @variant - pointer to variant specific data
906 */
907static inline void ufshcd_set_variant(struct ufs_hba *hba, void *variant)
908{
909 BUG_ON(!hba);
910 hba->priv = variant;
911}
912
913/**
914 * ufshcd_get_variant - get variant specific data from the hba
915 * @hba - per adapter instance
916 */
917static inline void *ufshcd_get_variant(struct ufs_hba *hba)
918{
919 BUG_ON(!hba);
920 return hba->priv;
921}
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -0800922static inline bool ufshcd_keep_autobkops_enabled_except_suspend(
923 struct ufs_hba *hba)
924{
925 return hba->caps & UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND;
926}
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200927
Stanley Chue31011a2020-05-22 16:32:11 +0800928static inline u8 ufshcd_wb_get_query_index(struct ufs_hba *hba)
Stanley Chu6f8d5a62020-05-08 16:01:13 +0800929{
930 if (hba->dev_info.b_wb_buffer_type == WB_BUF_MODE_LU_DEDICATED)
931 return hba->dev_info.wb_dedicated_lu;
932 return 0;
933}
934
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530935extern int ufshcd_runtime_suspend(struct ufs_hba *hba);
936extern int ufshcd_runtime_resume(struct ufs_hba *hba);
937extern int ufshcd_runtime_idle(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300938extern int ufshcd_system_suspend(struct ufs_hba *hba);
939extern int ufshcd_system_resume(struct ufs_hba *hba);
940extern int ufshcd_shutdown(struct ufs_hba *hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530941extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
942 u8 attr_set, u32 mib_val, u8 peer);
943extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
944 u32 *mib_val, u8 peer);
Alim Akhtar0d846e72018-05-06 15:44:18 +0530945extern int ufshcd_config_pwr_mode(struct ufs_hba *hba,
946 struct ufs_pa_layer_attr *desired_pwr_mode);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530947
948/* UIC command interfaces for DME primitives */
949#define DME_LOCAL 0
950#define DME_PEER 1
951#define ATTR_SET_NOR 0 /* NORMAL */
952#define ATTR_SET_ST 1 /* STATIC */
953
954static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
955 u32 mib_val)
956{
957 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
958 mib_val, DME_LOCAL);
959}
960
961static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,
962 u32 mib_val)
963{
964 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
965 mib_val, DME_LOCAL);
966}
967
968static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
969 u32 mib_val)
970{
971 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
972 mib_val, DME_PEER);
973}
974
975static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,
976 u32 mib_val)
977{
978 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
979 mib_val, DME_PEER);
980}
981
982static inline int ufshcd_dme_get(struct ufs_hba *hba,
983 u32 attr_sel, u32 *mib_val)
984{
985 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
986}
987
988static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
989 u32 attr_sel, u32 *mib_val)
990{
991 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
992}
993
Yaniv Gardif37aabc2016-03-10 17:37:20 +0200994static inline bool ufshcd_is_hs_mode(struct ufs_pa_layer_attr *pwr_info)
995{
996 return (pwr_info->pwr_rx == FAST_MODE ||
997 pwr_info->pwr_rx == FASTAUTO_MODE) &&
998 (pwr_info->pwr_tx == FAST_MODE ||
999 pwr_info->pwr_tx == FASTAUTO_MODE);
1000}
1001
Stanley Chu984eaac2020-02-07 15:03:57 +08001002static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba)
1003{
1004 return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0);
1005}
1006
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001007/* Expose Query-Request API */
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02001008int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
1009 enum query_opcode opcode,
1010 enum desc_idn idn, u8 index,
1011 u8 selector,
1012 u8 *desc_buf, int *buf_len);
Stanislav Nijnikov45bced82018-02-15 14:14:02 +02001013int ufshcd_read_desc_param(struct ufs_hba *hba,
1014 enum desc_idn desc_id,
1015 int desc_index,
1016 u8 param_offset,
1017 u8 *param_read_buf,
1018 u8 param_size);
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02001019int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
1020 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val);
Jaegeuk Kim72ac4442020-09-13 23:21:59 -07001021int ufshcd_query_attr_retry(struct ufs_hba *hba,
1022 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1023 u32 *attr_val);
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001024int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Stanley Chu1f34eed2020-05-08 16:01:12 +08001025 enum flag_idn idn, u8 index, bool *flag_res);
Jaegeuk Kim72ac4442020-09-13 23:21:59 -07001026int ufshcd_query_flag_retry(struct ufs_hba *hba,
1027 enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res);
1028int ufshcd_bkops_ctrl(struct ufs_hba *hba, enum bkops_status status);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03001029
Can Guo71d848b2019-11-14 22:09:26 -08001030void ufshcd_auto_hibern8_enable(struct ufs_hba *hba);
Stanley Chuba7af5e2019-12-30 13:32:28 +08001031void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit);
Stanley Chu8db269a2020-05-08 16:01:10 +08001032void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03001033#define SD_ASCII_STD true
1034#define SD_RAW false
1035int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
1036 u8 **buf, bool ascii);
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02001037
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001038int ufshcd_hold(struct ufs_hba *hba, bool async);
1039void ufshcd_release(struct ufs_hba *hba);
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00001040
Bean Huo7a0bf852020-06-03 11:19:58 +02001041void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
1042 int *desc_length);
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00001043
Yaniv Gardi37113102016-03-10 17:37:16 +02001044u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba);
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02001045
Avri Altmane77044c52018-10-07 17:30:39 +03001046int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd);
1047
Avri Altman5e0a86e2018-10-07 17:30:37 +03001048int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
1049 struct utp_upiu_req *req_upiu,
1050 struct utp_upiu_req *rsp_upiu,
1051 int msgcode,
1052 u8 *desc_buff, int *buff_len,
1053 enum query_opcode desc_op);
1054
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02001055/* Wrapper functions for safely calling variant operations */
1056static inline const char *ufshcd_get_var_name(struct ufs_hba *hba)
1057{
1058 if (hba->vops)
1059 return hba->vops->name;
1060 return "";
1061}
1062
1063static inline int ufshcd_vops_init(struct ufs_hba *hba)
1064{
1065 if (hba->vops && hba->vops->init)
1066 return hba->vops->init(hba);
1067
1068 return 0;
1069}
1070
1071static inline void ufshcd_vops_exit(struct ufs_hba *hba)
1072{
1073 if (hba->vops && hba->vops->exit)
1074 return hba->vops->exit(hba);
1075}
1076
1077static inline u32 ufshcd_vops_get_ufs_hci_version(struct ufs_hba *hba)
1078{
1079 if (hba->vops && hba->vops->get_ufs_hci_version)
1080 return hba->vops->get_ufs_hci_version(hba);
1081
1082 return ufshcd_readl(hba, REG_UFS_VERSION);
1083}
1084
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001085static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba,
1086 bool up, enum ufs_notify_change_status status)
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02001087{
1088 if (hba->vops && hba->vops->clk_scale_notify)
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001089 return hba->vops->clk_scale_notify(hba, up, status);
1090 return 0;
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02001091}
1092
Subhash Jadavani1e879e82016-10-06 21:48:22 -07001093static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on,
1094 enum ufs_notify_change_status status)
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02001095{
1096 if (hba->vops && hba->vops->setup_clocks)
Subhash Jadavani1e879e82016-10-06 21:48:22 -07001097 return hba->vops->setup_clocks(hba, on, status);
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02001098 return 0;
1099}
1100
1101static inline int ufshcd_vops_setup_regulators(struct ufs_hba *hba, bool status)
1102{
1103 if (hba->vops && hba->vops->setup_regulators)
1104 return hba->vops->setup_regulators(hba, status);
1105
1106 return 0;
1107}
1108
1109static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba,
1110 bool status)
1111{
1112 if (hba->vops && hba->vops->hce_enable_notify)
1113 return hba->vops->hce_enable_notify(hba, status);
1114
1115 return 0;
1116}
1117static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba,
1118 bool status)
1119{
1120 if (hba->vops && hba->vops->link_startup_notify)
1121 return hba->vops->link_startup_notify(hba, status);
1122
1123 return 0;
1124}
1125
1126static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba,
1127 bool status,
1128 struct ufs_pa_layer_attr *dev_max_params,
1129 struct ufs_pa_layer_attr *dev_req_params)
1130{
1131 if (hba->vops && hba->vops->pwr_change_notify)
1132 return hba->vops->pwr_change_notify(hba, status,
1133 dev_max_params, dev_req_params);
1134
1135 return -ENOTSUPP;
1136}
1137
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001138static inline void ufshcd_vops_setup_xfer_req(struct ufs_hba *hba, int tag,
1139 bool is_scsi_cmd)
1140{
1141 if (hba->vops && hba->vops->setup_xfer_req)
1142 return hba->vops->setup_xfer_req(hba, tag, is_scsi_cmd);
1143}
1144
Kiwoong Kimd2877be2016-11-10 21:16:15 +09001145static inline void ufshcd_vops_setup_task_mgmt(struct ufs_hba *hba,
1146 int tag, u8 tm_function)
1147{
1148 if (hba->vops && hba->vops->setup_task_mgmt)
1149 return hba->vops->setup_task_mgmt(hba, tag, tm_function);
1150}
1151
Kiwoong Kimee32c902016-11-10 21:17:43 +09001152static inline void ufshcd_vops_hibern8_notify(struct ufs_hba *hba,
1153 enum uic_cmd_dme cmd,
1154 enum ufs_notify_change_status status)
1155{
1156 if (hba->vops && hba->vops->hibern8_notify)
1157 return hba->vops->hibern8_notify(hba, cmd, status);
1158}
1159
Subhash Jadavani56d4a182016-12-05 19:25:32 -08001160static inline int ufshcd_vops_apply_dev_quirks(struct ufs_hba *hba)
1161{
1162 if (hba->vops && hba->vops->apply_dev_quirks)
1163 return hba->vops->apply_dev_quirks(hba);
1164 return 0;
1165}
1166
Stanley Chuc28c00b2020-05-08 16:01:09 +08001167static inline void ufshcd_vops_fixup_dev_quirks(struct ufs_hba *hba)
1168{
1169 if (hba->vops && hba->vops->fixup_dev_quirks)
1170 hba->vops->fixup_dev_quirks(hba);
1171}
1172
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02001173static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op)
1174{
1175 if (hba->vops && hba->vops->suspend)
1176 return hba->vops->suspend(hba, op);
1177
1178 return 0;
1179}
1180
1181static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op)
1182{
1183 if (hba->vops && hba->vops->resume)
1184 return hba->vops->resume(hba, op);
1185
1186 return 0;
1187}
1188
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001189static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba)
1190{
1191 if (hba->vops && hba->vops->dbg_register_dump)
1192 hba->vops->dbg_register_dump(hba);
1193}
1194
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07001195static inline void ufshcd_vops_device_reset(struct ufs_hba *hba)
1196{
Stanley Chua5fe372d2020-01-04 22:26:07 +08001197 if (hba->vops && hba->vops->device_reset) {
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07001198 hba->vops->device_reset(hba);
Stanley Chu1764fa22020-03-27 17:58:35 +08001199 ufshcd_set_ufs_dev_active(hba);
Stanley Chua5fe372d2020-01-04 22:26:07 +08001200 ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, 0);
1201 }
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07001202}
1203
Asutosh Das2c75f9a2020-03-25 11:29:01 -07001204static inline void ufshcd_vops_config_scaling_param(struct ufs_hba *hba,
1205 struct devfreq_dev_profile
1206 *profile, void *data)
1207{
1208 if (hba->vops && hba->vops->config_scaling_param)
1209 hba->vops->config_scaling_param(hba, profile, data);
1210}
1211
Eric Biggers617dda82020-08-22 10:47:33 -07001212static inline int ufshcd_vops_fill_prdt(struct ufs_hba *hba,
1213 struct ufshcd_lrb *lrbp,
1214 unsigned int segments)
1215{
1216 if (hba->vops && hba->vops->fill_prdt)
1217 return hba->vops->fill_prdt(hba, lrbp, segments);
1218
1219 return 0;
1220}
1221
Jaegeuk Kim9670c1f2020-09-16 10:59:18 -07001222static inline void ufshcd_vops_prepare_command(struct ufs_hba *hba,
1223 struct request *rq, struct ufshcd_lrb *lrbp)
1224{
1225 if (hba->vops && hba->vops->prepare_command)
1226 hba->vops->prepare_command(hba, rq, lrbp);
1227}
1228
Jaegeuk Kim0b0468f2020-09-13 22:17:05 -07001229static inline int ufshcd_vops_update_sysfs(struct ufs_hba *hba)
1230{
1231 if (hba->vops && hba->vops->update_sysfs)
1232 return hba->vops->update_sysfs(hba);
1233 return 0;
1234}
1235
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02001236extern struct ufs_pm_lvl_states ufs_pm_lvl_states[];
1237
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02001238/*
1239 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1240 * @scsi_lun: scsi LUN id
1241 *
1242 * Returns UPIU LUN id
1243 */
1244static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1245{
1246 if (scsi_is_wlun(scsi_lun))
1247 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1248 | UFS_UPIU_WLUN_ID;
1249 else
1250 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1251}
1252
Tomas Winklerba809172018-06-14 11:14:09 +03001253int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
1254 const char *prefix);
Greg Kroah-Hartman4f920402020-02-03 14:11:50 +00001255int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
1256int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
Vinayak Holikattie0eca632013-02-25 21:44:33 +05301257#endif /* End of Header */