ufshcd.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. *
  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. #ifndef _UFSHCD_H
  36. #define _UFSHCD_H
  37. #include <linux/module.h>
  38. #include <linux/kernel.h>
  39. #include <linux/init.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/io.h>
  42. #include <linux/delay.h>
  43. #include <linux/slab.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/errno.h>
  47. #include <linux/types.h>
  48. #include <linux/wait.h>
  49. #include <linux/bitops.h>
  50. #include <linux/pm_runtime.h>
  51. #include <linux/clk.h>
  52. #include <linux/completion.h>
  53. #include <asm/irq.h>
  54. #include <asm/byteorder.h>
  55. #include <scsi/scsi.h>
  56. #include <scsi/scsi_cmnd.h>
  57. #include <scsi/scsi_host.h>
  58. #include <scsi/scsi_tcq.h>
  59. #include <scsi/scsi_dbg.h>
  60. #include <scsi/scsi_eh.h>
  61. #include "ufs.h"
  62. #include "ufshci.h"
  63. #define UFSHCD "ufshcd"
  64. #define UFSHCD_DRIVER_VERSION "0.2"
  65. enum dev_cmd_type {
  66. DEV_CMD_TYPE_NOP = 0x0,
  67. DEV_CMD_TYPE_QUERY = 0x1,
  68. };
  69. /**
  70. * struct uic_command - UIC command structure
  71. * @command: UIC command
  72. * @argument1: UIC command argument 1
  73. * @argument2: UIC command argument 2
  74. * @argument3: UIC command argument 3
  75. * @cmd_active: Indicate if UIC command is outstanding
  76. * @result: UIC command result
  77. * @done: UIC command completion
  78. */
  79. struct uic_command {
  80. u32 command;
  81. u32 argument1;
  82. u32 argument2;
  83. u32 argument3;
  84. int cmd_active;
  85. int result;
  86. struct completion done;
  87. };
  88. /**
  89. * struct ufshcd_lrb - local reference block
  90. * @utr_descriptor_ptr: UTRD address of the command
  91. * @ucd_req_ptr: UCD address of the command
  92. * @ucd_rsp_ptr: Response UPIU address for this command
  93. * @ucd_prdt_ptr: PRDT address of the command
  94. * @cmd: pointer to SCSI command
  95. * @sense_buffer: pointer to sense buffer address of the SCSI command
  96. * @sense_bufflen: Length of the sense buffer
  97. * @scsi_status: SCSI status of the command
  98. * @command_type: SCSI, UFS, Query.
  99. * @task_tag: Task tag of the command
  100. * @lun: LUN of the command
  101. * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
  102. */
  103. struct ufshcd_lrb {
  104. struct utp_transfer_req_desc *utr_descriptor_ptr;
  105. struct utp_upiu_req *ucd_req_ptr;
  106. struct utp_upiu_rsp *ucd_rsp_ptr;
  107. struct ufshcd_sg_entry *ucd_prdt_ptr;
  108. struct scsi_cmnd *cmd;
  109. u8 *sense_buffer;
  110. unsigned int sense_bufflen;
  111. int scsi_status;
  112. int command_type;
  113. int task_tag;
  114. unsigned int lun;
  115. bool intr_cmd;
  116. };
  117. /**
  118. * struct ufs_query - holds relevent data structures for query request
  119. * @request: request upiu and function
  120. * @descriptor: buffer for sending/receiving descriptor
  121. * @response: response upiu and response
  122. */
  123. struct ufs_query {
  124. struct ufs_query_req request;
  125. u8 *descriptor;
  126. struct ufs_query_res response;
  127. };
  128. /**
  129. * struct ufs_dev_cmd - all assosiated fields with device management commands
  130. * @type: device management command type - Query, NOP OUT
  131. * @lock: lock to allow one command at a time
  132. * @complete: internal commands completion
  133. * @tag_wq: wait queue until free command slot is available
  134. */
  135. struct ufs_dev_cmd {
  136. enum dev_cmd_type type;
  137. struct mutex lock;
  138. struct completion *complete;
  139. wait_queue_head_t tag_wq;
  140. struct ufs_query query;
  141. };
  142. /**
  143. * struct ufs_hba - per adapter private structure
  144. * @mmio_base: UFSHCI base register address
  145. * @ucdl_base_addr: UFS Command Descriptor base address
  146. * @utrdl_base_addr: UTP Transfer Request Descriptor base address
  147. * @utmrdl_base_addr: UTP Task Management Descriptor base address
  148. * @ucdl_dma_addr: UFS Command Descriptor DMA address
  149. * @utrdl_dma_addr: UTRDL DMA address
  150. * @utmrdl_dma_addr: UTMRDL DMA address
  151. * @host: Scsi_Host instance of the driver
  152. * @dev: device handle
  153. * @lrb: local reference block
  154. * @lrb_in_use: lrb in use
  155. * @outstanding_tasks: Bits representing outstanding task requests
  156. * @outstanding_reqs: Bits representing outstanding transfer requests
  157. * @capabilities: UFS Controller Capabilities
  158. * @nutrs: Transfer Request Queue depth supported by controller
  159. * @nutmrs: Task Management Queue depth supported by controller
  160. * @ufs_version: UFS Version to which controller complies
  161. * @irq: Irq number of the controller
  162. * @active_uic_cmd: handle of active UIC command
  163. * @uic_cmd_mutex: mutex for uic command
  164. * @ufshcd_tm_wait_queue: wait queue for task management
  165. * @pwr_done: completion for power mode change
  166. * @tm_condition: condition variable for task management
  167. * @ufshcd_state: UFSHCD states
  168. * @intr_mask: Interrupt Mask Bits
  169. * @ee_ctrl_mask: Exception event control mask
  170. * @feh_workq: Work queue for fatal controller error handling
  171. * @eeh_work: Worker to handle exception events
  172. * @errors: HBA errors
  173. * @dev_cmd: ufs device management command information
  174. * @auto_bkops_enabled: to track whether bkops is enabled in device
  175. */
  176. struct ufs_hba {
  177. void __iomem *mmio_base;
  178. /* Virtual memory reference */
  179. struct utp_transfer_cmd_desc *ucdl_base_addr;
  180. struct utp_transfer_req_desc *utrdl_base_addr;
  181. struct utp_task_req_desc *utmrdl_base_addr;
  182. /* DMA memory reference */
  183. dma_addr_t ucdl_dma_addr;
  184. dma_addr_t utrdl_dma_addr;
  185. dma_addr_t utmrdl_dma_addr;
  186. struct Scsi_Host *host;
  187. struct device *dev;
  188. struct ufshcd_lrb *lrb;
  189. unsigned long lrb_in_use;
  190. unsigned long outstanding_tasks;
  191. unsigned long outstanding_reqs;
  192. u32 capabilities;
  193. int nutrs;
  194. int nutmrs;
  195. u32 ufs_version;
  196. unsigned int irq;
  197. struct uic_command *active_uic_cmd;
  198. struct mutex uic_cmd_mutex;
  199. wait_queue_head_t ufshcd_tm_wait_queue;
  200. unsigned long tm_condition;
  201. struct completion *pwr_done;
  202. u32 ufshcd_state;
  203. u32 intr_mask;
  204. u16 ee_ctrl_mask;
  205. /* Work Queues */
  206. struct work_struct feh_workq;
  207. struct work_struct eeh_work;
  208. /* HBA Errors */
  209. u32 errors;
  210. /* Device management request data */
  211. struct ufs_dev_cmd dev_cmd;
  212. bool auto_bkops_enabled;
  213. };
  214. #define ufshcd_writel(hba, val, reg) \
  215. writel((val), (hba)->mmio_base + (reg))
  216. #define ufshcd_readl(hba, reg) \
  217. readl((hba)->mmio_base + (reg))
  218. int ufshcd_init(struct device *, struct ufs_hba ** , void __iomem * ,
  219. unsigned int);
  220. void ufshcd_remove(struct ufs_hba *);
  221. /**
  222. * ufshcd_hba_stop - Send controller to reset state
  223. * @hba: per adapter instance
  224. */
  225. static inline void ufshcd_hba_stop(struct ufs_hba *hba)
  226. {
  227. ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
  228. }
  229. static inline void check_upiu_size(void)
  230. {
  231. BUILD_BUG_ON(ALIGNED_UPIU_SIZE <
  232. GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE);
  233. }
  234. extern int ufshcd_runtime_suspend(struct ufs_hba *hba);
  235. extern int ufshcd_runtime_resume(struct ufs_hba *hba);
  236. extern int ufshcd_runtime_idle(struct ufs_hba *hba);
  237. extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
  238. u8 attr_set, u32 mib_val, u8 peer);
  239. extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
  240. u32 *mib_val, u8 peer);
  241. /* UIC command interfaces for DME primitives */
  242. #define DME_LOCAL 0
  243. #define DME_PEER 1
  244. #define ATTR_SET_NOR 0 /* NORMAL */
  245. #define ATTR_SET_ST 1 /* STATIC */
  246. static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
  247. u32 mib_val)
  248. {
  249. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
  250. mib_val, DME_LOCAL);
  251. }
  252. static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,
  253. u32 mib_val)
  254. {
  255. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
  256. mib_val, DME_LOCAL);
  257. }
  258. static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
  259. u32 mib_val)
  260. {
  261. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
  262. mib_val, DME_PEER);
  263. }
  264. static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,
  265. u32 mib_val)
  266. {
  267. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
  268. mib_val, DME_PEER);
  269. }
  270. static inline int ufshcd_dme_get(struct ufs_hba *hba,
  271. u32 attr_sel, u32 *mib_val)
  272. {
  273. return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
  274. }
  275. static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
  276. u32 attr_sel, u32 *mib_val)
  277. {
  278. return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
  279. }
  280. #endif /* End of Header */