scsi: ufs: Add LU Dedicated buffer mode support for WriteBooster
[sfrench/cifs-2.6.git] / drivers / scsi / ufs / ufshcd.h
1 /*
2  * Universal Flash Storage Host controller driver
3  *
4  * This code is based on drivers/scsi/ufs/ufshcd.h
5  * Copyright (C) 2011-2013 Samsung India Software Operations
6  * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
7  *
8  * Authors:
9  *      Santosh Yaraganavi <santosh.sy@samsung.com>
10  *      Vinayak Holikatti <h.vinayak@samsung.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * See the COPYING file in the top-level directory or visit
17  * <http://www.gnu.org/licenses/gpl-2.0.html>
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * This program is provided "AS IS" and "WITH ALL FAULTS" and
25  * without warranty of any kind. You are solely responsible for
26  * determining the appropriateness of using and distributing
27  * the program and assume all risks associated with your exercise
28  * of rights with respect to the program, including but not limited
29  * to infringement of third party rights, the risks and costs of
30  * program errors, damage to or loss of data, programs or equipment,
31  * and unavailability or interruption of operations. Under no
32  * circumstances will the contributor of this Program be liable for
33  * any damages of any kind arising from your use or distribution of
34  * this program.
35  */
36
37 #ifndef _UFSHCD_H
38 #define _UFSHCD_H
39
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/init.h>
43 #include <linux/interrupt.h>
44 #include <linux/io.h>
45 #include <linux/delay.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48 #include <linux/rwsem.h>
49 #include <linux/workqueue.h>
50 #include <linux/errno.h>
51 #include <linux/types.h>
52 #include <linux/wait.h>
53 #include <linux/bitops.h>
54 #include <linux/pm_runtime.h>
55 #include <linux/clk.h>
56 #include <linux/completion.h>
57 #include <linux/regulator/consumer.h>
58 #include <linux/bitfield.h>
59 #include <linux/devfreq.h>
60 #include "unipro.h"
61
62 #include <asm/irq.h>
63 #include <asm/byteorder.h>
64 #include <scsi/scsi.h>
65 #include <scsi/scsi_cmnd.h>
66 #include <scsi/scsi_host.h>
67 #include <scsi/scsi_tcq.h>
68 #include <scsi/scsi_dbg.h>
69 #include <scsi/scsi_eh.h>
70
71 #include "ufs.h"
72 #include "ufs_quirks.h"
73 #include "ufshci.h"
74
75 #define UFSHCD "ufshcd"
76 #define UFSHCD_DRIVER_VERSION "0.2"
77
78 struct ufs_hba;
79
80 enum dev_cmd_type {
81         DEV_CMD_TYPE_NOP                = 0x0,
82         DEV_CMD_TYPE_QUERY              = 0x1,
83 };
84
85 /**
86  * struct uic_command - UIC command structure
87  * @command: UIC command
88  * @argument1: UIC command argument 1
89  * @argument2: UIC command argument 2
90  * @argument3: UIC command argument 3
91  * @cmd_active: Indicate if UIC command is outstanding
92  * @result: UIC command result
93  * @done: UIC command completion
94  */
95 struct uic_command {
96         u32 command;
97         u32 argument1;
98         u32 argument2;
99         u32 argument3;
100         int cmd_active;
101         int result;
102         struct completion done;
103 };
104
105 /* Used to differentiate the power management options */
106 enum ufs_pm_op {
107         UFS_RUNTIME_PM,
108         UFS_SYSTEM_PM,
109         UFS_SHUTDOWN_PM,
110 };
111
112 #define ufshcd_is_runtime_pm(op) ((op) == UFS_RUNTIME_PM)
113 #define ufshcd_is_system_pm(op) ((op) == UFS_SYSTEM_PM)
114 #define ufshcd_is_shutdown_pm(op) ((op) == UFS_SHUTDOWN_PM)
115
116 /* Host <-> Device UniPro Link state */
117 enum uic_link_state {
118         UIC_LINK_OFF_STATE      = 0, /* Link powered down or disabled */
119         UIC_LINK_ACTIVE_STATE   = 1, /* Link is in Fast/Slow/Sleep state */
120         UIC_LINK_HIBERN8_STATE  = 2, /* Link is in Hibernate state */
121 };
122
123 #define ufshcd_is_link_off(hba) ((hba)->uic_link_state == UIC_LINK_OFF_STATE)
124 #define ufshcd_is_link_active(hba) ((hba)->uic_link_state == \
125                                     UIC_LINK_ACTIVE_STATE)
126 #define ufshcd_is_link_hibern8(hba) ((hba)->uic_link_state == \
127                                     UIC_LINK_HIBERN8_STATE)
128 #define ufshcd_set_link_off(hba) ((hba)->uic_link_state = UIC_LINK_OFF_STATE)
129 #define ufshcd_set_link_active(hba) ((hba)->uic_link_state = \
130                                     UIC_LINK_ACTIVE_STATE)
131 #define ufshcd_set_link_hibern8(hba) ((hba)->uic_link_state = \
132                                     UIC_LINK_HIBERN8_STATE)
133
134 #define ufshcd_set_ufs_dev_active(h) \
135         ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
136 #define ufshcd_set_ufs_dev_sleep(h) \
137         ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
138 #define ufshcd_set_ufs_dev_poweroff(h) \
139         ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
140 #define ufshcd_is_ufs_dev_active(h) \
141         ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
142 #define ufshcd_is_ufs_dev_sleep(h) \
143         ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
144 #define ufshcd_is_ufs_dev_poweroff(h) \
145         ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
146
147 /*
148  * UFS Power management levels.
149  * Each level is in increasing order of power savings.
150  */
151 enum ufs_pm_level {
152         UFS_PM_LVL_0, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE */
153         UFS_PM_LVL_1, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE */
154         UFS_PM_LVL_2, /* UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE */
155         UFS_PM_LVL_3, /* UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE */
156         UFS_PM_LVL_4, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE */
157         UFS_PM_LVL_5, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE */
158         UFS_PM_LVL_MAX
159 };
160
161 struct ufs_pm_lvl_states {
162         enum ufs_dev_pwr_mode dev_state;
163         enum uic_link_state link_state;
164 };
165
166 /**
167  * struct ufshcd_lrb - local reference block
168  * @utr_descriptor_ptr: UTRD address of the command
169  * @ucd_req_ptr: UCD address of the command
170  * @ucd_rsp_ptr: Response UPIU address for this command
171  * @ucd_prdt_ptr: PRDT address of the command
172  * @utrd_dma_addr: UTRD dma address for debug
173  * @ucd_prdt_dma_addr: PRDT dma address for debug
174  * @ucd_rsp_dma_addr: UPIU response dma address for debug
175  * @ucd_req_dma_addr: UPIU request dma address for debug
176  * @cmd: pointer to SCSI command
177  * @sense_buffer: pointer to sense buffer address of the SCSI command
178  * @sense_bufflen: Length of the sense buffer
179  * @scsi_status: SCSI status of the command
180  * @command_type: SCSI, UFS, Query.
181  * @task_tag: Task tag of the command
182  * @lun: LUN of the command
183  * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
184  * @issue_time_stamp: time stamp for debug purposes
185  * @compl_time_stamp: time stamp for statistics
186  * @req_abort_skip: skip request abort task flag
187  */
188 struct ufshcd_lrb {
189         struct utp_transfer_req_desc *utr_descriptor_ptr;
190         struct utp_upiu_req *ucd_req_ptr;
191         struct utp_upiu_rsp *ucd_rsp_ptr;
192         struct ufshcd_sg_entry *ucd_prdt_ptr;
193
194         dma_addr_t utrd_dma_addr;
195         dma_addr_t ucd_req_dma_addr;
196         dma_addr_t ucd_rsp_dma_addr;
197         dma_addr_t ucd_prdt_dma_addr;
198
199         struct scsi_cmnd *cmd;
200         u8 *sense_buffer;
201         unsigned int sense_bufflen;
202         int scsi_status;
203
204         int command_type;
205         int task_tag;
206         u8 lun; /* UPIU LUN id field is only 8-bit wide */
207         bool intr_cmd;
208         ktime_t issue_time_stamp;
209         ktime_t compl_time_stamp;
210
211         bool req_abort_skip;
212 };
213
214 /**
215  * struct ufs_query - holds relevant data structures for query request
216  * @request: request upiu and function
217  * @descriptor: buffer for sending/receiving descriptor
218  * @response: response upiu and response
219  */
220 struct ufs_query {
221         struct ufs_query_req request;
222         u8 *descriptor;
223         struct ufs_query_res response;
224 };
225
226 /**
227  * struct ufs_dev_cmd - all assosiated fields with device management commands
228  * @type: device management command type - Query, NOP OUT
229  * @lock: lock to allow one command at a time
230  * @complete: internal commands completion
231  */
232 struct ufs_dev_cmd {
233         enum dev_cmd_type type;
234         struct mutex lock;
235         struct completion *complete;
236         struct ufs_query query;
237 };
238
239 struct ufs_desc_size {
240         int dev_desc;
241         int pwr_desc;
242         int geom_desc;
243         int interc_desc;
244         int unit_desc;
245         int conf_desc;
246         int hlth_desc;
247 };
248
249 /**
250  * struct ufs_clk_info - UFS clock related info
251  * @list: list headed by hba->clk_list_head
252  * @clk: clock node
253  * @name: clock name
254  * @max_freq: maximum frequency supported by the clock
255  * @min_freq: min frequency that can be used for clock scaling
256  * @curr_freq: indicates the current frequency that it is set to
257  * @enabled: variable to check against multiple enable/disable
258  */
259 struct ufs_clk_info {
260         struct list_head list;
261         struct clk *clk;
262         const char *name;
263         u32 max_freq;
264         u32 min_freq;
265         u32 curr_freq;
266         bool enabled;
267 };
268
269 enum ufs_notify_change_status {
270         PRE_CHANGE,
271         POST_CHANGE,
272 };
273
274 struct ufs_pa_layer_attr {
275         u32 gear_rx;
276         u32 gear_tx;
277         u32 lane_rx;
278         u32 lane_tx;
279         u32 pwr_rx;
280         u32 pwr_tx;
281         u32 hs_rate;
282 };
283
284 struct ufs_pwr_mode_info {
285         bool is_valid;
286         struct ufs_pa_layer_attr info;
287 };
288
289 /**
290  * struct ufs_hba_variant_ops - variant specific callbacks
291  * @name: variant name
292  * @init: called when the driver is initialized
293  * @exit: called to cleanup everything done in init
294  * @get_ufs_hci_version: called to get UFS HCI version
295  * @clk_scale_notify: notifies that clks are scaled up/down
296  * @setup_clocks: called before touching any of the controller registers
297  * @setup_regulators: called before accessing the host controller
298  * @hce_enable_notify: called before and after HCE enable bit is set to allow
299  *                     variant specific Uni-Pro initialization.
300  * @link_startup_notify: called before and after Link startup is carried out
301  *                       to allow variant specific Uni-Pro initialization.
302  * @pwr_change_notify: called before and after a power mode change
303  *                      is carried out to allow vendor spesific capabilities
304  *                      to be set.
305  * @setup_xfer_req: called before any transfer request is issued
306  *                  to set some things
307  * @setup_task_mgmt: called before any task management request is issued
308  *                  to set some things
309  * @hibern8_notify: called around hibern8 enter/exit
310  * @apply_dev_quirks: called to apply device specific quirks
311  * @suspend: called during host controller PM callback
312  * @resume: called during host controller PM callback
313  * @dbg_register_dump: used to dump controller debug information
314  * @phy_initialization: used to initialize phys
315  * @device_reset: called to issue a reset pulse on the UFS device
316  */
317 struct ufs_hba_variant_ops {
318         const char *name;
319         int     (*init)(struct ufs_hba *);
320         void    (*exit)(struct ufs_hba *);
321         u32     (*get_ufs_hci_version)(struct ufs_hba *);
322         int     (*clk_scale_notify)(struct ufs_hba *, bool,
323                                     enum ufs_notify_change_status);
324         int     (*setup_clocks)(struct ufs_hba *, bool,
325                                 enum ufs_notify_change_status);
326         int     (*setup_regulators)(struct ufs_hba *, bool);
327         int     (*hce_enable_notify)(struct ufs_hba *,
328                                      enum ufs_notify_change_status);
329         int     (*link_startup_notify)(struct ufs_hba *,
330                                        enum ufs_notify_change_status);
331         int     (*pwr_change_notify)(struct ufs_hba *,
332                                         enum ufs_notify_change_status status,
333                                         struct ufs_pa_layer_attr *,
334                                         struct ufs_pa_layer_attr *);
335         void    (*setup_xfer_req)(struct ufs_hba *, int, bool);
336         void    (*setup_task_mgmt)(struct ufs_hba *, int, u8);
337         void    (*hibern8_notify)(struct ufs_hba *, enum uic_cmd_dme,
338                                         enum ufs_notify_change_status);
339         int     (*apply_dev_quirks)(struct ufs_hba *hba);
340         void    (*fixup_dev_quirks)(struct ufs_hba *hba);
341         int     (*suspend)(struct ufs_hba *, enum ufs_pm_op);
342         int     (*resume)(struct ufs_hba *, enum ufs_pm_op);
343         void    (*dbg_register_dump)(struct ufs_hba *hba);
344         int     (*phy_initialization)(struct ufs_hba *);
345         void    (*device_reset)(struct ufs_hba *hba);
346         void    (*config_scaling_param)(struct ufs_hba *hba,
347                                         struct devfreq_dev_profile *profile,
348                                         void *data);
349 };
350
351 /* clock gating state  */
352 enum clk_gating_state {
353         CLKS_OFF,
354         CLKS_ON,
355         REQ_CLKS_OFF,
356         REQ_CLKS_ON,
357 };
358
359 /**
360  * struct ufs_clk_gating - UFS clock gating related info
361  * @gate_work: worker to turn off clocks after some delay as specified in
362  * delay_ms
363  * @ungate_work: worker to turn on clocks that will be used in case of
364  * interrupt context
365  * @state: the current clocks state
366  * @delay_ms: gating delay in ms
367  * @is_suspended: clk gating is suspended when set to 1 which can be used
368  * during suspend/resume
369  * @delay_attr: sysfs attribute to control delay_attr
370  * @enable_attr: sysfs attribute to enable/disable clock gating
371  * @is_enabled: Indicates the current status of clock gating
372  * @active_reqs: number of requests that are pending and should be waited for
373  * completion before gating clocks.
374  */
375 struct ufs_clk_gating {
376         struct delayed_work gate_work;
377         struct work_struct ungate_work;
378         enum clk_gating_state state;
379         unsigned long delay_ms;
380         bool is_suspended;
381         struct device_attribute delay_attr;
382         struct device_attribute enable_attr;
383         bool is_enabled;
384         int active_reqs;
385         struct workqueue_struct *clk_gating_workq;
386 };
387
388 struct ufs_saved_pwr_info {
389         struct ufs_pa_layer_attr info;
390         bool is_valid;
391 };
392
393 /**
394  * struct ufs_clk_scaling - UFS clock scaling related data
395  * @active_reqs: number of requests that are pending. If this is zero when
396  * devfreq ->target() function is called then schedule "suspend_work" to
397  * suspend devfreq.
398  * @tot_busy_t: Total busy time in current polling window
399  * @window_start_t: Start time (in jiffies) of the current polling window
400  * @busy_start_t: Start time of current busy period
401  * @enable_attr: sysfs attribute to enable/disable clock scaling
402  * @saved_pwr_info: UFS power mode may also be changed during scaling and this
403  * one keeps track of previous power mode.
404  * @workq: workqueue to schedule devfreq suspend/resume work
405  * @suspend_work: worker to suspend devfreq
406  * @resume_work: worker to resume devfreq
407  * @is_allowed: tracks if scaling is currently allowed or not
408  * @is_busy_started: tracks if busy period has started or not
409  * @is_suspended: tracks if devfreq is suspended or not
410  */
411 struct ufs_clk_scaling {
412         int active_reqs;
413         unsigned long tot_busy_t;
414         unsigned long window_start_t;
415         ktime_t busy_start_t;
416         struct device_attribute enable_attr;
417         struct ufs_saved_pwr_info saved_pwr_info;
418         struct workqueue_struct *workq;
419         struct work_struct suspend_work;
420         struct work_struct resume_work;
421         bool is_allowed;
422         bool is_busy_started;
423         bool is_suspended;
424 };
425
426 #define UFS_ERR_REG_HIST_LENGTH 8
427 /**
428  * struct ufs_err_reg_hist - keeps history of errors
429  * @pos: index to indicate cyclic buffer position
430  * @reg: cyclic buffer for registers value
431  * @tstamp: cyclic buffer for time stamp
432  */
433 struct ufs_err_reg_hist {
434         int pos;
435         u32 reg[UFS_ERR_REG_HIST_LENGTH];
436         ktime_t tstamp[UFS_ERR_REG_HIST_LENGTH];
437 };
438
439 /**
440  * struct ufs_stats - keeps usage/err statistics
441  * @hibern8_exit_cnt: Counter to keep track of number of exits,
442  *              reset this after link-startup.
443  * @last_hibern8_exit_tstamp: Set time after the hibern8 exit.
444  *              Clear after the first successful command completion.
445  * @pa_err: tracks pa-uic errors
446  * @dl_err: tracks dl-uic errors
447  * @nl_err: tracks nl-uic errors
448  * @tl_err: tracks tl-uic errors
449  * @dme_err: tracks dme errors
450  * @auto_hibern8_err: tracks auto-hibernate errors
451  * @fatal_err: tracks fatal errors
452  * @linkup_err: tracks link-startup errors
453  * @resume_err: tracks resume errors
454  * @suspend_err: tracks suspend errors
455  * @dev_reset: tracks device reset events
456  * @host_reset: tracks host reset events
457  * @tsk_abort: tracks task abort events
458  */
459 struct ufs_stats {
460         u32 hibern8_exit_cnt;
461         ktime_t last_hibern8_exit_tstamp;
462
463         /* uic specific errors */
464         struct ufs_err_reg_hist pa_err;
465         struct ufs_err_reg_hist dl_err;
466         struct ufs_err_reg_hist nl_err;
467         struct ufs_err_reg_hist tl_err;
468         struct ufs_err_reg_hist dme_err;
469
470         /* fatal errors */
471         struct ufs_err_reg_hist auto_hibern8_err;
472         struct ufs_err_reg_hist fatal_err;
473         struct ufs_err_reg_hist link_startup_err;
474         struct ufs_err_reg_hist resume_err;
475         struct ufs_err_reg_hist suspend_err;
476
477         /* abnormal events */
478         struct ufs_err_reg_hist dev_reset;
479         struct ufs_err_reg_hist host_reset;
480         struct ufs_err_reg_hist task_abort;
481 };
482
483 enum ufshcd_quirks {
484         /* Interrupt aggregation support is broken */
485         UFSHCD_QUIRK_BROKEN_INTR_AGGR                   = 1 << 0,
486
487         /*
488          * delay before each dme command is required as the unipro
489          * layer has shown instabilities
490          */
491         UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS              = 1 << 1,
492
493         /*
494          * If UFS host controller is having issue in processing LCC (Line
495          * Control Command) coming from device then enable this quirk.
496          * When this quirk is enabled, host controller driver should disable
497          * the LCC transmission on UFS device (by clearing TX_LCC_ENABLE
498          * attribute of device to 0).
499          */
500         UFSHCD_QUIRK_BROKEN_LCC                         = 1 << 2,
501
502         /*
503          * The attribute PA_RXHSUNTERMCAP specifies whether or not the
504          * inbound Link supports unterminated line in HS mode. Setting this
505          * attribute to 1 fixes moving to HS gear.
506          */
507         UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP            = 1 << 3,
508
509         /*
510          * This quirk needs to be enabled if the host controller only allows
511          * accessing the peer dme attributes in AUTO mode (FAST AUTO or
512          * SLOW AUTO).
513          */
514         UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE          = 1 << 4,
515
516         /*
517          * This quirk needs to be enabled if the host controller doesn't
518          * advertise the correct version in UFS_VER register. If this quirk
519          * is enabled, standard UFS host driver will call the vendor specific
520          * ops (get_ufs_hci_version) to get the correct version.
521          */
522         UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION             = 1 << 5,
523 };
524
525 enum ufshcd_caps {
526         /* Allow dynamic clk gating */
527         UFSHCD_CAP_CLK_GATING                           = 1 << 0,
528
529         /* Allow hiberb8 with clk gating */
530         UFSHCD_CAP_HIBERN8_WITH_CLK_GATING              = 1 << 1,
531
532         /* Allow dynamic clk scaling */
533         UFSHCD_CAP_CLK_SCALING                          = 1 << 2,
534
535         /* Allow auto bkops to enabled during runtime suspend */
536         UFSHCD_CAP_AUTO_BKOPS_SUSPEND                   = 1 << 3,
537
538         /*
539          * This capability allows host controller driver to use the UFS HCI's
540          * interrupt aggregation capability.
541          * CAUTION: Enabling this might reduce overall UFS throughput.
542          */
543         UFSHCD_CAP_INTR_AGGR                            = 1 << 4,
544
545         /*
546          * This capability allows the device auto-bkops to be always enabled
547          * except during suspend (both runtime and suspend).
548          * Enabling this capability means that device will always be allowed
549          * to do background operation when it's active but it might degrade
550          * the performance of ongoing read/write operations.
551          */
552         UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND = 1 << 5,
553
554         /*
555          * This capability allows host controller driver to automatically
556          * enable runtime power management by itself instead of waiting
557          * for userspace to control the power management.
558          */
559         UFSHCD_CAP_RPM_AUTOSUSPEND                      = 1 << 6,
560
561         /*
562          * This capability allows the host controller driver to turn-on
563          * WriteBooster, if the underlying device supports it and is
564          * provisioned to be used. This would increase the write performance.
565          */
566         UFSHCD_CAP_WB_EN                                = 1 << 7,
567 };
568
569 /**
570  * struct ufs_hba - per adapter private structure
571  * @mmio_base: UFSHCI base register address
572  * @ucdl_base_addr: UFS Command Descriptor base address
573  * @utrdl_base_addr: UTP Transfer Request Descriptor base address
574  * @utmrdl_base_addr: UTP Task Management Descriptor base address
575  * @ucdl_dma_addr: UFS Command Descriptor DMA address
576  * @utrdl_dma_addr: UTRDL DMA address
577  * @utmrdl_dma_addr: UTMRDL DMA address
578  * @host: Scsi_Host instance of the driver
579  * @dev: device handle
580  * @lrb: local reference block
581  * @cmd_queue: Used to allocate command tags from hba->host->tag_set.
582  * @outstanding_tasks: Bits representing outstanding task requests
583  * @outstanding_reqs: Bits representing outstanding transfer requests
584  * @capabilities: UFS Controller Capabilities
585  * @nutrs: Transfer Request Queue depth supported by controller
586  * @nutmrs: Task Management Queue depth supported by controller
587  * @ufs_version: UFS Version to which controller complies
588  * @vops: pointer to variant specific operations
589  * @priv: pointer to variant specific private data
590  * @irq: Irq number of the controller
591  * @active_uic_cmd: handle of active UIC command
592  * @uic_cmd_mutex: mutex for uic command
593  * @tmf_tag_set: TMF tag set.
594  * @tmf_queue: Used to allocate TMF tags.
595  * @pwr_done: completion for power mode change
596  * @ufshcd_state: UFSHCD states
597  * @eh_flags: Error handling flags
598  * @intr_mask: Interrupt Mask Bits
599  * @ee_ctrl_mask: Exception event control mask
600  * @is_powered: flag to check if HBA is powered
601  * @eh_work: Worker to handle UFS errors that require s/w attention
602  * @eeh_work: Worker to handle exception events
603  * @errors: HBA errors
604  * @uic_error: UFS interconnect layer error status
605  * @saved_err: sticky error mask
606  * @saved_uic_err: sticky UIC error mask
607  * @silence_err_logs: flag to silence error logs
608  * @dev_cmd: ufs device management command information
609  * @last_dme_cmd_tstamp: time stamp of the last completed DME command
610  * @auto_bkops_enabled: to track whether bkops is enabled in device
611  * @vreg_info: UFS device voltage regulator information
612  * @clk_list_head: UFS host controller clocks list node head
613  * @pwr_info: holds current power mode
614  * @max_pwr_info: keeps the device max valid pwm
615  * @desc_size: descriptor sizes reported by device
616  * @urgent_bkops_lvl: keeps track of urgent bkops level for device
617  * @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for
618  *  device is known or not.
619  * @scsi_block_reqs_cnt: reference counting for scsi block requests
620  */
621 struct ufs_hba {
622         void __iomem *mmio_base;
623
624         /* Virtual memory reference */
625         struct utp_transfer_cmd_desc *ucdl_base_addr;
626         struct utp_transfer_req_desc *utrdl_base_addr;
627         struct utp_task_req_desc *utmrdl_base_addr;
628
629         /* DMA memory reference */
630         dma_addr_t ucdl_dma_addr;
631         dma_addr_t utrdl_dma_addr;
632         dma_addr_t utmrdl_dma_addr;
633
634         struct Scsi_Host *host;
635         struct device *dev;
636         struct request_queue *cmd_queue;
637         /*
638          * This field is to keep a reference to "scsi_device" corresponding to
639          * "UFS device" W-LU.
640          */
641         struct scsi_device *sdev_ufs_device;
642
643         enum ufs_dev_pwr_mode curr_dev_pwr_mode;
644         enum uic_link_state uic_link_state;
645         /* Desired UFS power management level during runtime PM */
646         enum ufs_pm_level rpm_lvl;
647         /* Desired UFS power management level during system PM */
648         enum ufs_pm_level spm_lvl;
649         struct device_attribute rpm_lvl_attr;
650         struct device_attribute spm_lvl_attr;
651         int pm_op_in_progress;
652
653         /* Auto-Hibernate Idle Timer register value */
654         u32 ahit;
655
656         struct ufshcd_lrb *lrb;
657
658         unsigned long outstanding_tasks;
659         unsigned long outstanding_reqs;
660
661         u32 capabilities;
662         int nutrs;
663         int nutmrs;
664         u32 ufs_version;
665         const struct ufs_hba_variant_ops *vops;
666         void *priv;
667         unsigned int irq;
668         bool is_irq_enabled;
669         enum ufs_ref_clk_freq dev_ref_clk_freq;
670
671         unsigned int quirks;    /* Deviations from standard UFSHCI spec. */
672
673         /* Device deviations from standard UFS device spec. */
674         unsigned int dev_quirks;
675
676         struct blk_mq_tag_set tmf_tag_set;
677         struct request_queue *tmf_queue;
678
679         struct uic_command *active_uic_cmd;
680         struct mutex uic_cmd_mutex;
681         struct completion *uic_async_done;
682
683         u32 ufshcd_state;
684         u32 eh_flags;
685         u32 intr_mask;
686         u16 ee_ctrl_mask;
687         u16 hba_enable_delay_us;
688         bool is_powered;
689
690         /* Work Queues */
691         struct work_struct eh_work;
692         struct work_struct eeh_work;
693
694         /* HBA Errors */
695         u32 errors;
696         u32 uic_error;
697         u32 saved_err;
698         u32 saved_uic_err;
699         struct ufs_stats ufs_stats;
700         bool silence_err_logs;
701
702         /* Device management request data */
703         struct ufs_dev_cmd dev_cmd;
704         ktime_t last_dme_cmd_tstamp;
705
706         /* Keeps information of the UFS device connected to this host */
707         struct ufs_dev_info dev_info;
708         bool auto_bkops_enabled;
709         struct ufs_vreg_info vreg_info;
710         struct list_head clk_list_head;
711
712         bool wlun_dev_clr_ua;
713
714         /* Number of requests aborts */
715         int req_abort_count;
716
717         /* Number of lanes available (1 or 2) for Rx/Tx */
718         u32 lanes_per_direction;
719         struct ufs_pa_layer_attr pwr_info;
720         struct ufs_pwr_mode_info max_pwr_info;
721
722         struct ufs_clk_gating clk_gating;
723         /* Control to enable/disable host capabilities */
724         u32 caps;
725
726         struct devfreq *devfreq;
727         struct ufs_clk_scaling clk_scaling;
728         bool is_sys_suspended;
729
730         enum bkops_status urgent_bkops_lvl;
731         bool is_urgent_bkops_lvl_checked;
732
733         struct rw_semaphore clk_scaling_lock;
734         struct ufs_desc_size desc_size;
735         atomic_t scsi_block_reqs_cnt;
736
737         struct device           bsg_dev;
738         struct request_queue    *bsg_queue;
739         bool wb_buf_flush_enabled;
740         bool wb_enabled;
741 };
742
743 /* Returns true if clocks can be gated. Otherwise false */
744 static inline bool ufshcd_is_clkgating_allowed(struct ufs_hba *hba)
745 {
746         return hba->caps & UFSHCD_CAP_CLK_GATING;
747 }
748 static inline bool ufshcd_can_hibern8_during_gating(struct ufs_hba *hba)
749 {
750         return hba->caps & UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
751 }
752 static inline int ufshcd_is_clkscaling_supported(struct ufs_hba *hba)
753 {
754         return hba->caps & UFSHCD_CAP_CLK_SCALING;
755 }
756 static inline bool ufshcd_can_autobkops_during_suspend(struct ufs_hba *hba)
757 {
758         return hba->caps & UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
759 }
760 static inline bool ufshcd_is_rpm_autosuspend_allowed(struct ufs_hba *hba)
761 {
762         return hba->caps & UFSHCD_CAP_RPM_AUTOSUSPEND;
763 }
764
765 static inline bool ufshcd_is_intr_aggr_allowed(struct ufs_hba *hba)
766 {
767 /* DWC UFS Core has the Interrupt aggregation feature but is not detectable*/
768 #ifndef CONFIG_SCSI_UFS_DWC
769         if ((hba->caps & UFSHCD_CAP_INTR_AGGR) &&
770             !(hba->quirks & UFSHCD_QUIRK_BROKEN_INTR_AGGR))
771                 return true;
772         else
773                 return false;
774 #else
775 return true;
776 #endif
777 }
778
779 static inline bool ufshcd_is_auto_hibern8_supported(struct ufs_hba *hba)
780 {
781         return (hba->capabilities & MASK_AUTO_HIBERN8_SUPPORT);
782 }
783
784 static inline bool ufshcd_is_auto_hibern8_enabled(struct ufs_hba *hba)
785 {
786         return FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, hba->ahit) ? true : false;
787 }
788
789 static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba)
790 {
791         return hba->caps & UFSHCD_CAP_WB_EN;
792 }
793
794 #define ufshcd_writel(hba, val, reg)    \
795         writel((val), (hba)->mmio_base + (reg))
796 #define ufshcd_readl(hba, reg)  \
797         readl((hba)->mmio_base + (reg))
798
799 /**
800  * ufshcd_rmwl - read modify write into a register
801  * @hba - per adapter instance
802  * @mask - mask to apply on read value
803  * @val - actual value to write
804  * @reg - register address
805  */
806 static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg)
807 {
808         u32 tmp;
809
810         tmp = ufshcd_readl(hba, reg);
811         tmp &= ~mask;
812         tmp |= (val & mask);
813         ufshcd_writel(hba, tmp, reg);
814 }
815
816 int ufshcd_alloc_host(struct device *, struct ufs_hba **);
817 void ufshcd_dealloc_host(struct ufs_hba *);
818 int ufshcd_hba_enable(struct ufs_hba *hba);
819 int ufshcd_init(struct ufs_hba * , void __iomem * , unsigned int);
820 int ufshcd_link_recovery(struct ufs_hba *hba);
821 int ufshcd_make_hba_operational(struct ufs_hba *hba);
822 void ufshcd_remove(struct ufs_hba *);
823 int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
824 void ufshcd_delay_us(unsigned long us, unsigned long tolerance);
825 int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
826                                 u32 val, unsigned long interval_us,
827                                 unsigned long timeout_ms, bool can_sleep);
828 void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk);
829 void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist,
830                             u32 reg);
831
832 static inline void check_upiu_size(void)
833 {
834         BUILD_BUG_ON(ALIGNED_UPIU_SIZE <
835                 GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE);
836 }
837
838 /**
839  * ufshcd_set_variant - set variant specific data to the hba
840  * @hba - per adapter instance
841  * @variant - pointer to variant specific data
842  */
843 static inline void ufshcd_set_variant(struct ufs_hba *hba, void *variant)
844 {
845         BUG_ON(!hba);
846         hba->priv = variant;
847 }
848
849 /**
850  * ufshcd_get_variant - get variant specific data from the hba
851  * @hba - per adapter instance
852  */
853 static inline void *ufshcd_get_variant(struct ufs_hba *hba)
854 {
855         BUG_ON(!hba);
856         return hba->priv;
857 }
858 static inline bool ufshcd_keep_autobkops_enabled_except_suspend(
859                                                         struct ufs_hba *hba)
860 {
861         return hba->caps & UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND;
862 }
863
864 static inline u8 ufshcd_wb_get_flag_index(struct ufs_hba *hba)
865 {
866         if (hba->dev_info.b_wb_buffer_type == WB_BUF_MODE_LU_DEDICATED)
867                 return hba->dev_info.wb_dedicated_lu;
868         return 0;
869 }
870
871 extern int ufshcd_runtime_suspend(struct ufs_hba *hba);
872 extern int ufshcd_runtime_resume(struct ufs_hba *hba);
873 extern int ufshcd_runtime_idle(struct ufs_hba *hba);
874 extern int ufshcd_system_suspend(struct ufs_hba *hba);
875 extern int ufshcd_system_resume(struct ufs_hba *hba);
876 extern int ufshcd_shutdown(struct ufs_hba *hba);
877 extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
878                                u8 attr_set, u32 mib_val, u8 peer);
879 extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
880                                u32 *mib_val, u8 peer);
881 extern int ufshcd_config_pwr_mode(struct ufs_hba *hba,
882                         struct ufs_pa_layer_attr *desired_pwr_mode);
883
884 /* UIC command interfaces for DME primitives */
885 #define DME_LOCAL       0
886 #define DME_PEER        1
887 #define ATTR_SET_NOR    0       /* NORMAL */
888 #define ATTR_SET_ST     1       /* STATIC */
889
890 static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
891                                  u32 mib_val)
892 {
893         return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
894                                    mib_val, DME_LOCAL);
895 }
896
897 static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,
898                                     u32 mib_val)
899 {
900         return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
901                                    mib_val, DME_LOCAL);
902 }
903
904 static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
905                                       u32 mib_val)
906 {
907         return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
908                                    mib_val, DME_PEER);
909 }
910
911 static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,
912                                          u32 mib_val)
913 {
914         return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
915                                    mib_val, DME_PEER);
916 }
917
918 static inline int ufshcd_dme_get(struct ufs_hba *hba,
919                                  u32 attr_sel, u32 *mib_val)
920 {
921         return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
922 }
923
924 static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
925                                       u32 attr_sel, u32 *mib_val)
926 {
927         return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
928 }
929
930 static inline bool ufshcd_is_hs_mode(struct ufs_pa_layer_attr *pwr_info)
931 {
932         return (pwr_info->pwr_rx == FAST_MODE ||
933                 pwr_info->pwr_rx == FASTAUTO_MODE) &&
934                 (pwr_info->pwr_tx == FAST_MODE ||
935                 pwr_info->pwr_tx == FASTAUTO_MODE);
936 }
937
938 static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba)
939 {
940         return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0);
941 }
942
943 /* Expose Query-Request API */
944 int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
945                                   enum query_opcode opcode,
946                                   enum desc_idn idn, u8 index,
947                                   u8 selector,
948                                   u8 *desc_buf, int *buf_len);
949 int ufshcd_read_desc_param(struct ufs_hba *hba,
950                            enum desc_idn desc_id,
951                            int desc_index,
952                            u8 param_offset,
953                            u8 *param_read_buf,
954                            u8 param_size);
955 int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
956                       enum attr_idn idn, u8 index, u8 selector, u32 *attr_val);
957 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
958         enum flag_idn idn, u8 index, bool *flag_res);
959
960 void ufshcd_auto_hibern8_enable(struct ufs_hba *hba);
961 void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit);
962 void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups);
963 #define SD_ASCII_STD true
964 #define SD_RAW false
965 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
966                             u8 **buf, bool ascii);
967
968 int ufshcd_hold(struct ufs_hba *hba, bool async);
969 void ufshcd_release(struct ufs_hba *hba);
970
971 int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
972         int *desc_length);
973
974 u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba);
975
976 int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd);
977
978 int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
979                              struct utp_upiu_req *req_upiu,
980                              struct utp_upiu_req *rsp_upiu,
981                              int msgcode,
982                              u8 *desc_buff, int *buff_len,
983                              enum query_opcode desc_op);
984
985 /* Wrapper functions for safely calling variant operations */
986 static inline const char *ufshcd_get_var_name(struct ufs_hba *hba)
987 {
988         if (hba->vops)
989                 return hba->vops->name;
990         return "";
991 }
992
993 static inline int ufshcd_vops_init(struct ufs_hba *hba)
994 {
995         if (hba->vops && hba->vops->init)
996                 return hba->vops->init(hba);
997
998         return 0;
999 }
1000
1001 static inline void ufshcd_vops_exit(struct ufs_hba *hba)
1002 {
1003         if (hba->vops && hba->vops->exit)
1004                 return hba->vops->exit(hba);
1005 }
1006
1007 static inline u32 ufshcd_vops_get_ufs_hci_version(struct ufs_hba *hba)
1008 {
1009         if (hba->vops && hba->vops->get_ufs_hci_version)
1010                 return hba->vops->get_ufs_hci_version(hba);
1011
1012         return ufshcd_readl(hba, REG_UFS_VERSION);
1013 }
1014
1015 static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba,
1016                         bool up, enum ufs_notify_change_status status)
1017 {
1018         if (hba->vops && hba->vops->clk_scale_notify)
1019                 return hba->vops->clk_scale_notify(hba, up, status);
1020         return 0;
1021 }
1022
1023 static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on,
1024                                         enum ufs_notify_change_status status)
1025 {
1026         if (hba->vops && hba->vops->setup_clocks)
1027                 return hba->vops->setup_clocks(hba, on, status);
1028         return 0;
1029 }
1030
1031 static inline int ufshcd_vops_setup_regulators(struct ufs_hba *hba, bool status)
1032 {
1033         if (hba->vops && hba->vops->setup_regulators)
1034                 return hba->vops->setup_regulators(hba, status);
1035
1036         return 0;
1037 }
1038
1039 static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba,
1040                                                 bool status)
1041 {
1042         if (hba->vops && hba->vops->hce_enable_notify)
1043                 return hba->vops->hce_enable_notify(hba, status);
1044
1045         return 0;
1046 }
1047 static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba,
1048                                                 bool status)
1049 {
1050         if (hba->vops && hba->vops->link_startup_notify)
1051                 return hba->vops->link_startup_notify(hba, status);
1052
1053         return 0;
1054 }
1055
1056 static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba,
1057                                   bool status,
1058                                   struct ufs_pa_layer_attr *dev_max_params,
1059                                   struct ufs_pa_layer_attr *dev_req_params)
1060 {
1061         if (hba->vops && hba->vops->pwr_change_notify)
1062                 return hba->vops->pwr_change_notify(hba, status,
1063                                         dev_max_params, dev_req_params);
1064
1065         return -ENOTSUPP;
1066 }
1067
1068 static inline void ufshcd_vops_setup_xfer_req(struct ufs_hba *hba, int tag,
1069                                         bool is_scsi_cmd)
1070 {
1071         if (hba->vops && hba->vops->setup_xfer_req)
1072                 return hba->vops->setup_xfer_req(hba, tag, is_scsi_cmd);
1073 }
1074
1075 static inline void ufshcd_vops_setup_task_mgmt(struct ufs_hba *hba,
1076                                         int tag, u8 tm_function)
1077 {
1078         if (hba->vops && hba->vops->setup_task_mgmt)
1079                 return hba->vops->setup_task_mgmt(hba, tag, tm_function);
1080 }
1081
1082 static inline void ufshcd_vops_hibern8_notify(struct ufs_hba *hba,
1083                                         enum uic_cmd_dme cmd,
1084                                         enum ufs_notify_change_status status)
1085 {
1086         if (hba->vops && hba->vops->hibern8_notify)
1087                 return hba->vops->hibern8_notify(hba, cmd, status);
1088 }
1089
1090 static inline int ufshcd_vops_apply_dev_quirks(struct ufs_hba *hba)
1091 {
1092         if (hba->vops && hba->vops->apply_dev_quirks)
1093                 return hba->vops->apply_dev_quirks(hba);
1094         return 0;
1095 }
1096
1097 static inline void ufshcd_vops_fixup_dev_quirks(struct ufs_hba *hba)
1098 {
1099         if (hba->vops && hba->vops->fixup_dev_quirks)
1100                 hba->vops->fixup_dev_quirks(hba);
1101 }
1102
1103 static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op)
1104 {
1105         if (hba->vops && hba->vops->suspend)
1106                 return hba->vops->suspend(hba, op);
1107
1108         return 0;
1109 }
1110
1111 static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op)
1112 {
1113         if (hba->vops && hba->vops->resume)
1114                 return hba->vops->resume(hba, op);
1115
1116         return 0;
1117 }
1118
1119 static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba)
1120 {
1121         if (hba->vops && hba->vops->dbg_register_dump)
1122                 hba->vops->dbg_register_dump(hba);
1123 }
1124
1125 static inline void ufshcd_vops_device_reset(struct ufs_hba *hba)
1126 {
1127         if (hba->vops && hba->vops->device_reset) {
1128                 hba->vops->device_reset(hba);
1129                 ufshcd_set_ufs_dev_active(hba);
1130                 ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, 0);
1131         }
1132 }
1133
1134 static inline void ufshcd_vops_config_scaling_param(struct ufs_hba *hba,
1135                                                     struct devfreq_dev_profile
1136                                                     *profile, void *data)
1137 {
1138         if (hba->vops && hba->vops->config_scaling_param)
1139                 hba->vops->config_scaling_param(hba, profile, data);
1140 }
1141
1142 extern struct ufs_pm_lvl_states ufs_pm_lvl_states[];
1143
1144 /*
1145  * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1146  * @scsi_lun: scsi LUN id
1147  *
1148  * Returns UPIU LUN id
1149  */
1150 static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1151 {
1152         if (scsi_is_wlun(scsi_lun))
1153                 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1154                         | UFS_UPIU_WLUN_ID;
1155         else
1156                 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1157 }
1158
1159 int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
1160                      const char *prefix);
1161
1162 #endif /* End of Header */