ufshcd-priv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _UFSHCD_PRIV_H_
  3. #define _UFSHCD_PRIV_H_
  4. #include <linux/pm_runtime.h>
  5. #include <ufs/ufshcd.h>
  6. static inline bool ufshcd_is_user_access_allowed(struct ufs_hba *hba)
  7. {
  8. return !hba->shutting_down;
  9. }
  10. void ufshcd_schedule_eh_work(struct ufs_hba *hba);
  11. static inline bool ufshcd_keep_autobkops_enabled_except_suspend(
  12. struct ufs_hba *hba)
  13. {
  14. return hba->caps & UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND;
  15. }
  16. static inline u8 ufshcd_wb_get_query_index(struct ufs_hba *hba)
  17. {
  18. if (hba->dev_info.wb_buffer_type == WB_BUF_MODE_LU_DEDICATED)
  19. return hba->dev_info.wb_dedicated_lu;
  20. return 0;
  21. }
  22. static inline bool ufshcd_is_wb_buf_flush_allowed(struct ufs_hba *hba)
  23. {
  24. return ufshcd_is_wb_allowed(hba) &&
  25. !(hba->quirks & UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL);
  26. }
  27. #ifdef CONFIG_SCSI_UFS_HWMON
  28. void ufs_hwmon_probe(struct ufs_hba *hba, u8 mask);
  29. void ufs_hwmon_remove(struct ufs_hba *hba);
  30. void ufs_hwmon_notify_event(struct ufs_hba *hba, u8 ee_mask);
  31. #else
  32. static inline void ufs_hwmon_probe(struct ufs_hba *hba, u8 mask) {}
  33. static inline void ufs_hwmon_remove(struct ufs_hba *hba) {}
  34. static inline void ufs_hwmon_notify_event(struct ufs_hba *hba, u8 ee_mask) {}
  35. #endif
  36. int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
  37. enum query_opcode opcode,
  38. enum desc_idn idn, u8 index,
  39. u8 selector,
  40. u8 *desc_buf, int *buf_len);
  41. int ufshcd_read_desc_param(struct ufs_hba *hba,
  42. enum desc_idn desc_id,
  43. int desc_index,
  44. u8 param_offset,
  45. u8 *param_read_buf,
  46. u8 param_size);
  47. int ufshcd_query_attr_retry(struct ufs_hba *hba, enum query_opcode opcode,
  48. enum attr_idn idn, u8 index, u8 selector,
  49. u32 *attr_val);
  50. int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
  51. enum attr_idn idn, u8 index, u8 selector, u32 *attr_val);
  52. int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
  53. enum flag_idn idn, u8 index, bool *flag_res);
  54. void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit);
  55. void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag,
  56. struct cq_entry *cqe);
  57. int ufshcd_mcq_init(struct ufs_hba *hba);
  58. void ufshcd_mcq_disable(struct ufs_hba *hba);
  59. int ufshcd_mcq_decide_queue_depth(struct ufs_hba *hba);
  60. int ufshcd_mcq_memory_alloc(struct ufs_hba *hba);
  61. struct ufs_hw_queue *ufshcd_mcq_req_to_hwq(struct ufs_hba *hba,
  62. struct request *req);
  63. void ufshcd_mcq_compl_all_cqes_lock(struct ufs_hba *hba,
  64. struct ufs_hw_queue *hwq);
  65. bool ufshcd_cmd_inflight(struct scsi_cmnd *cmd);
  66. int ufshcd_mcq_sq_cleanup(struct ufs_hba *hba, int task_tag);
  67. int ufshcd_mcq_abort(struct scsi_cmnd *cmd);
  68. int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag);
  69. void ufshcd_release_scsi_cmd(struct ufs_hba *hba,
  70. struct ufshcd_lrb *lrbp);
  71. #define SD_ASCII_STD true
  72. #define SD_RAW false
  73. int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
  74. u8 **buf, bool ascii);
  75. int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd);
  76. int ufshcd_send_bsg_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd);
  77. int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
  78. struct utp_upiu_req *req_upiu,
  79. struct utp_upiu_req *rsp_upiu,
  80. enum upiu_request_transaction msgcode,
  81. u8 *desc_buff, int *buff_len,
  82. enum query_opcode desc_op);
  83. int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable);
  84. /* Wrapper functions for safely calling variant operations */
  85. static inline const char *ufshcd_get_var_name(struct ufs_hba *hba)
  86. {
  87. if (hba->vops)
  88. return hba->vops->name;
  89. return "";
  90. }
  91. static inline void ufshcd_vops_exit(struct ufs_hba *hba)
  92. {
  93. if (hba->vops && hba->vops->exit)
  94. return hba->vops->exit(hba);
  95. }
  96. static inline u32 ufshcd_vops_get_ufs_hci_version(struct ufs_hba *hba)
  97. {
  98. if (hba->vops && hba->vops->get_ufs_hci_version)
  99. return hba->vops->get_ufs_hci_version(hba);
  100. return ufshcd_readl(hba, REG_UFS_VERSION);
  101. }
  102. static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba,
  103. bool up, enum ufs_notify_change_status status)
  104. {
  105. if (hba->vops && hba->vops->clk_scale_notify)
  106. return hba->vops->clk_scale_notify(hba, up, status);
  107. return 0;
  108. }
  109. static inline void ufshcd_vops_event_notify(struct ufs_hba *hba,
  110. enum ufs_event_type evt,
  111. void *data)
  112. {
  113. if (hba->vops && hba->vops->event_notify)
  114. hba->vops->event_notify(hba, evt, data);
  115. }
  116. static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on,
  117. enum ufs_notify_change_status status)
  118. {
  119. if (hba->vops && hba->vops->setup_clocks)
  120. return hba->vops->setup_clocks(hba, on, status);
  121. return 0;
  122. }
  123. static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba,
  124. bool status)
  125. {
  126. if (hba->vops && hba->vops->hce_enable_notify)
  127. return hba->vops->hce_enable_notify(hba, status);
  128. return 0;
  129. }
  130. static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba,
  131. bool status)
  132. {
  133. if (hba->vops && hba->vops->link_startup_notify)
  134. return hba->vops->link_startup_notify(hba, status);
  135. return 0;
  136. }
  137. static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba,
  138. enum ufs_notify_change_status status,
  139. struct ufs_pa_layer_attr *dev_max_params,
  140. struct ufs_pa_layer_attr *dev_req_params)
  141. {
  142. if (hba->vops && hba->vops->pwr_change_notify)
  143. return hba->vops->pwr_change_notify(hba, status,
  144. dev_max_params, dev_req_params);
  145. return -ENOTSUPP;
  146. }
  147. static inline void ufshcd_vops_setup_task_mgmt(struct ufs_hba *hba,
  148. int tag, u8 tm_function)
  149. {
  150. if (hba->vops && hba->vops->setup_task_mgmt)
  151. return hba->vops->setup_task_mgmt(hba, tag, tm_function);
  152. }
  153. static inline void ufshcd_vops_hibern8_notify(struct ufs_hba *hba,
  154. enum uic_cmd_dme cmd,
  155. enum ufs_notify_change_status status)
  156. {
  157. if (hba->vops && hba->vops->hibern8_notify)
  158. return hba->vops->hibern8_notify(hba, cmd, status);
  159. }
  160. static inline int ufshcd_vops_apply_dev_quirks(struct ufs_hba *hba)
  161. {
  162. if (hba->vops && hba->vops->apply_dev_quirks)
  163. return hba->vops->apply_dev_quirks(hba);
  164. return 0;
  165. }
  166. static inline void ufshcd_vops_fixup_dev_quirks(struct ufs_hba *hba)
  167. {
  168. if (hba->vops && hba->vops->fixup_dev_quirks)
  169. hba->vops->fixup_dev_quirks(hba);
  170. }
  171. static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op,
  172. enum ufs_notify_change_status status)
  173. {
  174. if (hba->vops && hba->vops->suspend)
  175. return hba->vops->suspend(hba, op, status);
  176. return 0;
  177. }
  178. static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op)
  179. {
  180. if (hba->vops && hba->vops->resume)
  181. return hba->vops->resume(hba, op);
  182. return 0;
  183. }
  184. static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba)
  185. {
  186. if (hba->vops && hba->vops->dbg_register_dump)
  187. hba->vops->dbg_register_dump(hba);
  188. }
  189. static inline int ufshcd_vops_device_reset(struct ufs_hba *hba)
  190. {
  191. if (hba->vops && hba->vops->device_reset)
  192. return hba->vops->device_reset(hba);
  193. return -EOPNOTSUPP;
  194. }
  195. static inline void ufshcd_vops_config_scaling_param(struct ufs_hba *hba,
  196. struct devfreq_dev_profile *p,
  197. struct devfreq_simple_ondemand_data *data)
  198. {
  199. if (hba->vops && hba->vops->config_scaling_param)
  200. hba->vops->config_scaling_param(hba, p, data);
  201. }
  202. static inline int ufshcd_vops_mcq_config_resource(struct ufs_hba *hba)
  203. {
  204. if (hba->vops && hba->vops->mcq_config_resource)
  205. return hba->vops->mcq_config_resource(hba);
  206. return -EOPNOTSUPP;
  207. }
  208. static inline int ufshcd_mcq_vops_op_runtime_config(struct ufs_hba *hba)
  209. {
  210. if (hba->vops && hba->vops->op_runtime_config)
  211. return hba->vops->op_runtime_config(hba);
  212. return -EOPNOTSUPP;
  213. }
  214. static inline int ufshcd_vops_get_outstanding_cqs(struct ufs_hba *hba,
  215. unsigned long *ocqs)
  216. {
  217. if (hba->vops && hba->vops->get_outstanding_cqs)
  218. return hba->vops->get_outstanding_cqs(hba, ocqs);
  219. return -EOPNOTSUPP;
  220. }
  221. static inline int ufshcd_mcq_vops_config_esi(struct ufs_hba *hba)
  222. {
  223. if (hba->vops && hba->vops->config_esi)
  224. return hba->vops->config_esi(hba);
  225. return -EOPNOTSUPP;
  226. }
  227. extern const struct ufs_pm_lvl_states ufs_pm_lvl_states[];
  228. /**
  229. * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
  230. * @scsi_lun: scsi LUN id
  231. *
  232. * Return: UPIU LUN id
  233. */
  234. static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
  235. {
  236. if (scsi_is_wlun(scsi_lun))
  237. return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
  238. | UFS_UPIU_WLUN_ID;
  239. else
  240. return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
  241. }
  242. int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask);
  243. int ufshcd_write_ee_control(struct ufs_hba *hba);
  244. int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask,
  245. const u16 *other_mask, u16 set, u16 clr);
  246. static inline int ufshcd_update_ee_drv_mask(struct ufs_hba *hba,
  247. u16 set, u16 clr)
  248. {
  249. return ufshcd_update_ee_control(hba, &hba->ee_drv_mask,
  250. &hba->ee_usr_mask, set, clr);
  251. }
  252. static inline int ufshcd_update_ee_usr_mask(struct ufs_hba *hba,
  253. u16 set, u16 clr)
  254. {
  255. return ufshcd_update_ee_control(hba, &hba->ee_usr_mask,
  256. &hba->ee_drv_mask, set, clr);
  257. }
  258. static inline int ufshcd_rpm_get_sync(struct ufs_hba *hba)
  259. {
  260. return pm_runtime_get_sync(&hba->ufs_device_wlun->sdev_gendev);
  261. }
  262. static inline int ufshcd_rpm_get_if_active(struct ufs_hba *hba)
  263. {
  264. return pm_runtime_get_if_active(&hba->ufs_device_wlun->sdev_gendev);
  265. }
  266. static inline int ufshcd_rpm_put_sync(struct ufs_hba *hba)
  267. {
  268. return pm_runtime_put_sync(&hba->ufs_device_wlun->sdev_gendev);
  269. }
  270. static inline void ufshcd_rpm_get_noresume(struct ufs_hba *hba)
  271. {
  272. pm_runtime_get_noresume(&hba->ufs_device_wlun->sdev_gendev);
  273. }
  274. static inline int ufshcd_rpm_resume(struct ufs_hba *hba)
  275. {
  276. return pm_runtime_resume(&hba->ufs_device_wlun->sdev_gendev);
  277. }
  278. static inline int ufshcd_rpm_put(struct ufs_hba *hba)
  279. {
  280. return pm_runtime_put(&hba->ufs_device_wlun->sdev_gendev);
  281. }
  282. /**
  283. * ufs_is_valid_unit_desc_lun - checks if the given LUN has a unit descriptor
  284. * @dev_info: pointer of instance of struct ufs_dev_info
  285. * @lun: LU number to check
  286. * @return: true if the lun has a matching unit descriptor, false otherwise
  287. */
  288. static inline bool ufs_is_valid_unit_desc_lun(struct ufs_dev_info *dev_info, u8 lun)
  289. {
  290. if (!dev_info || !dev_info->max_lu_supported) {
  291. pr_err("Max General LU supported by UFS isn't initialized\n");
  292. return false;
  293. }
  294. return lun == UFS_UPIU_RPMB_WLUN || (lun < dev_info->max_lu_supported);
  295. }
  296. static inline void ufshcd_inc_sq_tail(struct ufs_hw_queue *q)
  297. __must_hold(&q->sq_lock)
  298. {
  299. u32 val;
  300. q->sq_tail_slot++;
  301. if (q->sq_tail_slot == q->max_entries)
  302. q->sq_tail_slot = 0;
  303. val = q->sq_tail_slot * sizeof(struct utp_transfer_req_desc);
  304. writel(val, q->mcq_sq_tail);
  305. }
  306. static inline void ufshcd_mcq_update_cq_tail_slot(struct ufs_hw_queue *q)
  307. {
  308. u32 val = readl(q->mcq_cq_tail);
  309. q->cq_tail_slot = val / sizeof(struct cq_entry);
  310. }
  311. static inline bool ufshcd_mcq_is_cq_empty(struct ufs_hw_queue *q)
  312. {
  313. return q->cq_head_slot == q->cq_tail_slot;
  314. }
  315. static inline void ufshcd_mcq_inc_cq_head_slot(struct ufs_hw_queue *q)
  316. {
  317. q->cq_head_slot++;
  318. if (q->cq_head_slot == q->max_entries)
  319. q->cq_head_slot = 0;
  320. }
  321. static inline void ufshcd_mcq_update_cq_head(struct ufs_hw_queue *q)
  322. {
  323. writel(q->cq_head_slot * sizeof(struct cq_entry), q->mcq_cq_head);
  324. }
  325. static inline struct cq_entry *ufshcd_mcq_cur_cqe(struct ufs_hw_queue *q)
  326. {
  327. struct cq_entry *cqe = q->cqe_base_addr;
  328. return cqe + q->cq_head_slot;
  329. }
  330. static inline u32 ufshcd_mcq_get_sq_head_slot(struct ufs_hw_queue *q)
  331. {
  332. u32 val = readl(q->mcq_sq_head);
  333. return val / sizeof(struct utp_transfer_req_desc);
  334. }
  335. #endif /* _UFSHCD_PRIV_H_ */