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