blob: 577679a2d1898f919826ec8f27e18213414b908d [file] [log] [blame]
Vinayak Holikattie0eca632013-02-25 21:44:33 +05301/*
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 *
7 * Authors:
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 * See the COPYING file in the top-level directory or visit
16 * <http://www.gnu.org/licenses/gpl-2.0.html>
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This program is provided "AS IS" and "WITH ALL FAULTS" and
24 * without warranty of any kind. You are solely responsible for
25 * determining the appropriateness of using and distributing
26 * the program and assume all risks associated with your exercise
27 * of rights with respect to the program, including but not limited
28 * to infringement of third party rights, the risks and costs of
29 * program errors, damage to or loss of data, programs or equipment,
30 * and unavailability or interruption of operations. Under no
31 * circumstances will the contributor of this Program be liable for
32 * any damages of any kind arising from your use or distribution of
33 * this program.
34 */
35
36#ifndef _UFSHCD_H
37#define _UFSHCD_H
38
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/init.h>
42#include <linux/interrupt.h>
43#include <linux/io.h>
44#include <linux/delay.h>
45#include <linux/slab.h>
46#include <linux/spinlock.h>
47#include <linux/workqueue.h>
48#include <linux/errno.h>
49#include <linux/types.h>
50#include <linux/wait.h>
51#include <linux/bitops.h>
52#include <linux/pm_runtime.h>
53#include <linux/clk.h>
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053054#include <linux/completion.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053055
56#include <asm/irq.h>
57#include <asm/byteorder.h>
58#include <scsi/scsi.h>
59#include <scsi/scsi_cmnd.h>
60#include <scsi/scsi_host.h>
61#include <scsi/scsi_tcq.h>
62#include <scsi/scsi_dbg.h>
63#include <scsi/scsi_eh.h>
64
65#include "ufs.h"
66#include "ufshci.h"
67
68#define UFSHCD "ufshcd"
69#define UFSHCD_DRIVER_VERSION "0.2"
70
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053071enum dev_cmd_type {
72 DEV_CMD_TYPE_NOP = 0x0,
Dolev Raviv68078d52013-07-30 00:35:58 +053073 DEV_CMD_TYPE_QUERY = 0x1,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053074};
75
Vinayak Holikattie0eca632013-02-25 21:44:33 +053076/**
77 * struct uic_command - UIC command structure
78 * @command: UIC command
79 * @argument1: UIC command argument 1
80 * @argument2: UIC command argument 2
81 * @argument3: UIC command argument 3
82 * @cmd_active: Indicate if UIC command is outstanding
83 * @result: UIC command result
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053084 * @done: UIC command completion
Vinayak Holikattie0eca632013-02-25 21:44:33 +053085 */
86struct uic_command {
87 u32 command;
88 u32 argument1;
89 u32 argument2;
90 u32 argument3;
91 int cmd_active;
92 int result;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053093 struct completion done;
Vinayak Holikattie0eca632013-02-25 21:44:33 +053094};
95
96/**
97 * struct ufshcd_lrb - local reference block
98 * @utr_descriptor_ptr: UTRD address of the command
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053099 * @ucd_req_ptr: UCD address of the command
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530100 * @ucd_rsp_ptr: Response UPIU address for this command
101 * @ucd_prdt_ptr: PRDT address of the command
102 * @cmd: pointer to SCSI command
103 * @sense_buffer: pointer to sense buffer address of the SCSI command
104 * @sense_bufflen: Length of the sense buffer
105 * @scsi_status: SCSI status of the command
106 * @command_type: SCSI, UFS, Query.
107 * @task_tag: Task tag of the command
108 * @lun: LUN of the command
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530109 * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530110 */
111struct ufshcd_lrb {
112 struct utp_transfer_req_desc *utr_descriptor_ptr;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530113 struct utp_upiu_req *ucd_req_ptr;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530114 struct utp_upiu_rsp *ucd_rsp_ptr;
115 struct ufshcd_sg_entry *ucd_prdt_ptr;
116
117 struct scsi_cmnd *cmd;
118 u8 *sense_buffer;
119 unsigned int sense_bufflen;
120 int scsi_status;
121
122 int command_type;
123 int task_tag;
124 unsigned int lun;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530125 bool intr_cmd;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530126};
127
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530128/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530129 * struct ufs_query - holds relevent data structures for query request
130 * @request: request upiu and function
131 * @descriptor: buffer for sending/receiving descriptor
132 * @response: response upiu and response
133 */
134struct ufs_query {
135 struct ufs_query_req request;
136 u8 *descriptor;
137 struct ufs_query_res response;
138};
139
140/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530141 * struct ufs_dev_cmd - all assosiated fields with device management commands
142 * @type: device management command type - Query, NOP OUT
143 * @lock: lock to allow one command at a time
144 * @complete: internal commands completion
145 * @tag_wq: wait queue until free command slot is available
146 */
147struct ufs_dev_cmd {
148 enum dev_cmd_type type;
149 struct mutex lock;
150 struct completion *complete;
151 wait_queue_head_t tag_wq;
Dolev Raviv68078d52013-07-30 00:35:58 +0530152 struct ufs_query query;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530153};
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530154
155/**
156 * struct ufs_hba - per adapter private structure
157 * @mmio_base: UFSHCI base register address
158 * @ucdl_base_addr: UFS Command Descriptor base address
159 * @utrdl_base_addr: UTP Transfer Request Descriptor base address
160 * @utmrdl_base_addr: UTP Task Management Descriptor base address
161 * @ucdl_dma_addr: UFS Command Descriptor DMA address
162 * @utrdl_dma_addr: UTRDL DMA address
163 * @utmrdl_dma_addr: UTMRDL DMA address
164 * @host: Scsi_Host instance of the driver
165 * @dev: device handle
166 * @lrb: local reference block
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530167 * @lrb_in_use: lrb in use
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530168 * @outstanding_tasks: Bits representing outstanding task requests
169 * @outstanding_reqs: Bits representing outstanding transfer requests
170 * @capabilities: UFS Controller Capabilities
171 * @nutrs: Transfer Request Queue depth supported by controller
172 * @nutmrs: Task Management Queue depth supported by controller
173 * @ufs_version: UFS Version to which controller complies
174 * @irq: Irq number of the controller
175 * @active_uic_cmd: handle of active UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530176 * @uic_cmd_mutex: mutex for uic command
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530177 * @ufshcd_tm_wait_queue: wait queue for task management
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530178 * @pwr_done: completion for power mode change
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530179 * @tm_condition: condition variable for task management
180 * @ufshcd_state: UFSHCD states
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530181 * @intr_mask: Interrupt Mask Bits
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530182 * @ee_ctrl_mask: Exception event control mask
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530183 * @feh_workq: Work queue for fatal controller error handling
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530184 * @eeh_work: Worker to handle exception events
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530185 * @errors: HBA errors
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530186 * @dev_cmd: ufs device management command information
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530187 * @auto_bkops_enabled: to track whether bkops is enabled in device
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530188 */
189struct ufs_hba {
190 void __iomem *mmio_base;
191
192 /* Virtual memory reference */
193 struct utp_transfer_cmd_desc *ucdl_base_addr;
194 struct utp_transfer_req_desc *utrdl_base_addr;
195 struct utp_task_req_desc *utmrdl_base_addr;
196
197 /* DMA memory reference */
198 dma_addr_t ucdl_dma_addr;
199 dma_addr_t utrdl_dma_addr;
200 dma_addr_t utmrdl_dma_addr;
201
202 struct Scsi_Host *host;
203 struct device *dev;
204
205 struct ufshcd_lrb *lrb;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530206 unsigned long lrb_in_use;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530207
208 unsigned long outstanding_tasks;
209 unsigned long outstanding_reqs;
210
211 u32 capabilities;
212 int nutrs;
213 int nutmrs;
214 u32 ufs_version;
215 unsigned int irq;
216
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530217 struct uic_command *active_uic_cmd;
218 struct mutex uic_cmd_mutex;
219
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530220 wait_queue_head_t ufshcd_tm_wait_queue;
221 unsigned long tm_condition;
222
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530223 struct completion *pwr_done;
224
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530225 u32 ufshcd_state;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530226 u32 intr_mask;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530227 u16 ee_ctrl_mask;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530228
229 /* Work Queues */
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530230 struct work_struct feh_workq;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530231 struct work_struct eeh_work;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530232
233 /* HBA Errors */
234 u32 errors;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530235
236 /* Device management request data */
237 struct ufs_dev_cmd dev_cmd;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530238
239 bool auto_bkops_enabled;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530240};
241
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530242#define ufshcd_writel(hba, val, reg) \
243 writel((val), (hba)->mmio_base + (reg))
244#define ufshcd_readl(hba, reg) \
245 readl((hba)->mmio_base + (reg))
246
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530247int ufshcd_init(struct device *, struct ufs_hba ** , void __iomem * ,
248 unsigned int);
249void ufshcd_remove(struct ufs_hba *);
250
251/**
252 * ufshcd_hba_stop - Send controller to reset state
253 * @hba: per adapter instance
254 */
255static inline void ufshcd_hba_stop(struct ufs_hba *hba)
256{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530257 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530258}
259
Dolev Raviv68078d52013-07-30 00:35:58 +0530260static inline void check_upiu_size(void)
261{
262 BUILD_BUG_ON(ALIGNED_UPIU_SIZE <
263 GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE);
264}
265
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530266extern int ufshcd_runtime_suspend(struct ufs_hba *hba);
267extern int ufshcd_runtime_resume(struct ufs_hba *hba);
268extern int ufshcd_runtime_idle(struct ufs_hba *hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530269extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
270 u8 attr_set, u32 mib_val, u8 peer);
271extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
272 u32 *mib_val, u8 peer);
273
274/* UIC command interfaces for DME primitives */
275#define DME_LOCAL 0
276#define DME_PEER 1
277#define ATTR_SET_NOR 0 /* NORMAL */
278#define ATTR_SET_ST 1 /* STATIC */
279
280static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
281 u32 mib_val)
282{
283 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
284 mib_val, DME_LOCAL);
285}
286
287static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,
288 u32 mib_val)
289{
290 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
291 mib_val, DME_LOCAL);
292}
293
294static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
295 u32 mib_val)
296{
297 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
298 mib_val, DME_PEER);
299}
300
301static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,
302 u32 mib_val)
303{
304 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
305 mib_val, DME_PEER);
306}
307
308static inline int ufshcd_dme_get(struct ufs_hba *hba,
309 u32 attr_sel, u32 *mib_val)
310{
311 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
312}
313
314static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
315 u32 attr_sel, u32 *mib_val)
316{
317 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
318}
319
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530320#endif /* End of Header */