xref: /openbmc/linux/drivers/ufs/core/ufshcd-priv.h (revision 0ae8dc1a)
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 int ufshcd_mcq_decide_queue_depth(struct ufs_hba *hba);
68 int ufshcd_mcq_memory_alloc(struct ufs_hba *hba);
69 void ufshcd_mcq_make_queues_operational(struct ufs_hba *hba);
70 void ufshcd_mcq_config_mac(struct ufs_hba *hba, u32 max_active_cmds);
71 void ufshcd_mcq_select_mcq_mode(struct ufs_hba *hba);
72 u32 ufshcd_mcq_read_cqis(struct ufs_hba *hba, int i);
73 void ufshcd_mcq_write_cqis(struct ufs_hba *hba, u32 val, int i);
74 unsigned long ufshcd_mcq_poll_cqe_nolock(struct ufs_hba *hba,
75 					 struct ufs_hw_queue *hwq);
76 struct ufs_hw_queue *ufshcd_mcq_req_to_hwq(struct ufs_hba *hba,
77 					   struct request *req);
78 unsigned long ufshcd_mcq_poll_cqe_lock(struct ufs_hba *hba,
79 				       struct ufs_hw_queue *hwq);
80 
81 #define UFSHCD_MCQ_IO_QUEUE_OFFSET	1
82 #define SD_ASCII_STD true
83 #define SD_RAW false
84 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
85 			    u8 **buf, bool ascii);
86 
87 int ufshcd_hold(struct ufs_hba *hba, bool async);
88 void ufshcd_release(struct ufs_hba *hba);
89 
90 int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd);
91 
92 int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
93 			     struct utp_upiu_req *req_upiu,
94 			     struct utp_upiu_req *rsp_upiu,
95 			     int msgcode,
96 			     u8 *desc_buff, int *buff_len,
97 			     enum query_opcode desc_op);
98 
99 int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable);
100 
101 /* Wrapper functions for safely calling variant operations */
102 static inline const char *ufshcd_get_var_name(struct ufs_hba *hba)
103 {
104 	if (hba->vops)
105 		return hba->vops->name;
106 	return "";
107 }
108 
109 static inline void ufshcd_vops_exit(struct ufs_hba *hba)
110 {
111 	if (hba->vops && hba->vops->exit)
112 		return hba->vops->exit(hba);
113 }
114 
115 static inline u32 ufshcd_vops_get_ufs_hci_version(struct ufs_hba *hba)
116 {
117 	if (hba->vops && hba->vops->get_ufs_hci_version)
118 		return hba->vops->get_ufs_hci_version(hba);
119 
120 	return ufshcd_readl(hba, REG_UFS_VERSION);
121 }
122 
123 static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba,
124 			bool up, enum ufs_notify_change_status status)
125 {
126 	if (hba->vops && hba->vops->clk_scale_notify)
127 		return hba->vops->clk_scale_notify(hba, up, status);
128 	return 0;
129 }
130 
131 static inline void ufshcd_vops_event_notify(struct ufs_hba *hba,
132 					    enum ufs_event_type evt,
133 					    void *data)
134 {
135 	if (hba->vops && hba->vops->event_notify)
136 		hba->vops->event_notify(hba, evt, data);
137 }
138 
139 static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on,
140 					enum ufs_notify_change_status status)
141 {
142 	if (hba->vops && hba->vops->setup_clocks)
143 		return hba->vops->setup_clocks(hba, on, status);
144 	return 0;
145 }
146 
147 static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba,
148 						bool status)
149 {
150 	if (hba->vops && hba->vops->hce_enable_notify)
151 		return hba->vops->hce_enable_notify(hba, status);
152 
153 	return 0;
154 }
155 static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba,
156 						bool status)
157 {
158 	if (hba->vops && hba->vops->link_startup_notify)
159 		return hba->vops->link_startup_notify(hba, status);
160 
161 	return 0;
162 }
163 
164 static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba,
165 				  enum ufs_notify_change_status status,
166 				  struct ufs_pa_layer_attr *dev_max_params,
167 				  struct ufs_pa_layer_attr *dev_req_params)
168 {
169 	if (hba->vops && hba->vops->pwr_change_notify)
170 		return hba->vops->pwr_change_notify(hba, status,
171 					dev_max_params, dev_req_params);
172 
173 	return -ENOTSUPP;
174 }
175 
176 static inline void ufshcd_vops_setup_task_mgmt(struct ufs_hba *hba,
177 					int tag, u8 tm_function)
178 {
179 	if (hba->vops && hba->vops->setup_task_mgmt)
180 		return hba->vops->setup_task_mgmt(hba, tag, tm_function);
181 }
182 
183 static inline void ufshcd_vops_hibern8_notify(struct ufs_hba *hba,
184 					enum uic_cmd_dme cmd,
185 					enum ufs_notify_change_status status)
186 {
187 	if (hba->vops && hba->vops->hibern8_notify)
188 		return hba->vops->hibern8_notify(hba, cmd, status);
189 }
190 
191 static inline int ufshcd_vops_apply_dev_quirks(struct ufs_hba *hba)
192 {
193 	if (hba->vops && hba->vops->apply_dev_quirks)
194 		return hba->vops->apply_dev_quirks(hba);
195 	return 0;
196 }
197 
198 static inline void ufshcd_vops_fixup_dev_quirks(struct ufs_hba *hba)
199 {
200 	if (hba->vops && hba->vops->fixup_dev_quirks)
201 		hba->vops->fixup_dev_quirks(hba);
202 }
203 
204 static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op,
205 				enum ufs_notify_change_status status)
206 {
207 	if (hba->vops && hba->vops->suspend)
208 		return hba->vops->suspend(hba, op, status);
209 
210 	return 0;
211 }
212 
213 static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op)
214 {
215 	if (hba->vops && hba->vops->resume)
216 		return hba->vops->resume(hba, op);
217 
218 	return 0;
219 }
220 
221 static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba)
222 {
223 	if (hba->vops && hba->vops->dbg_register_dump)
224 		hba->vops->dbg_register_dump(hba);
225 }
226 
227 static inline int ufshcd_vops_device_reset(struct ufs_hba *hba)
228 {
229 	if (hba->vops && hba->vops->device_reset)
230 		return hba->vops->device_reset(hba);
231 
232 	return -EOPNOTSUPP;
233 }
234 
235 static inline void ufshcd_vops_config_scaling_param(struct ufs_hba *hba,
236 		struct devfreq_dev_profile *p,
237 		struct devfreq_simple_ondemand_data *data)
238 {
239 	if (hba->vops && hba->vops->config_scaling_param)
240 		hba->vops->config_scaling_param(hba, p, data);
241 }
242 
243 static inline void ufshcd_vops_reinit_notify(struct ufs_hba *hba)
244 {
245 	if (hba->vops && hba->vops->reinit_notify)
246 		hba->vops->reinit_notify(hba);
247 }
248 
249 static inline int ufshcd_vops_mcq_config_resource(struct ufs_hba *hba)
250 {
251 	if (hba->vops && hba->vops->mcq_config_resource)
252 		return hba->vops->mcq_config_resource(hba);
253 
254 	return -EOPNOTSUPP;
255 }
256 
257 static inline int ufshcd_mcq_vops_get_hba_mac(struct ufs_hba *hba)
258 {
259 	if (hba->vops && hba->vops->get_hba_mac)
260 		return hba->vops->get_hba_mac(hba);
261 
262 	return -EOPNOTSUPP;
263 }
264 
265 static inline int ufshcd_mcq_vops_op_runtime_config(struct ufs_hba *hba)
266 {
267 	if (hba->vops && hba->vops->op_runtime_config)
268 		return hba->vops->op_runtime_config(hba);
269 
270 	return -EOPNOTSUPP;
271 }
272 
273 static inline int ufshcd_vops_get_outstanding_cqs(struct ufs_hba *hba,
274 						  unsigned long *ocqs)
275 {
276 	if (hba->vops && hba->vops->get_outstanding_cqs)
277 		return hba->vops->get_outstanding_cqs(hba, ocqs);
278 
279 	return -EOPNOTSUPP;
280 }
281 
282 static inline int ufshcd_mcq_vops_config_esi(struct ufs_hba *hba)
283 {
284 	if (hba->vops && hba->vops->config_esi)
285 		return hba->vops->config_esi(hba);
286 
287 	return -EOPNOTSUPP;
288 }
289 
290 extern const struct ufs_pm_lvl_states ufs_pm_lvl_states[];
291 
292 /**
293  * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
294  * @scsi_lun: scsi LUN id
295  *
296  * Returns UPIU LUN id
297  */
298 static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
299 {
300 	if (scsi_is_wlun(scsi_lun))
301 		return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
302 			| UFS_UPIU_WLUN_ID;
303 	else
304 		return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
305 }
306 
307 int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask);
308 int ufshcd_write_ee_control(struct ufs_hba *hba);
309 int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask,
310 			     const u16 *other_mask, u16 set, u16 clr);
311 
312 static inline int ufshcd_update_ee_drv_mask(struct ufs_hba *hba,
313 					    u16 set, u16 clr)
314 {
315 	return ufshcd_update_ee_control(hba, &hba->ee_drv_mask,
316 					&hba->ee_usr_mask, set, clr);
317 }
318 
319 static inline int ufshcd_update_ee_usr_mask(struct ufs_hba *hba,
320 					    u16 set, u16 clr)
321 {
322 	return ufshcd_update_ee_control(hba, &hba->ee_usr_mask,
323 					&hba->ee_drv_mask, set, clr);
324 }
325 
326 static inline int ufshcd_rpm_get_sync(struct ufs_hba *hba)
327 {
328 	return pm_runtime_get_sync(&hba->ufs_device_wlun->sdev_gendev);
329 }
330 
331 static inline int ufshcd_rpm_put_sync(struct ufs_hba *hba)
332 {
333 	return pm_runtime_put_sync(&hba->ufs_device_wlun->sdev_gendev);
334 }
335 
336 static inline void ufshcd_rpm_get_noresume(struct ufs_hba *hba)
337 {
338 	pm_runtime_get_noresume(&hba->ufs_device_wlun->sdev_gendev);
339 }
340 
341 static inline int ufshcd_rpm_resume(struct ufs_hba *hba)
342 {
343 	return pm_runtime_resume(&hba->ufs_device_wlun->sdev_gendev);
344 }
345 
346 static inline int ufshcd_rpm_put(struct ufs_hba *hba)
347 {
348 	return pm_runtime_put(&hba->ufs_device_wlun->sdev_gendev);
349 }
350 
351 /**
352  * ufs_is_valid_unit_desc_lun - checks if the given LUN has a unit descriptor
353  * @dev_info: pointer of instance of struct ufs_dev_info
354  * @lun: LU number to check
355  * @return: true if the lun has a matching unit descriptor, false otherwise
356  */
357 static inline bool ufs_is_valid_unit_desc_lun(struct ufs_dev_info *dev_info, u8 lun)
358 {
359 	if (!dev_info || !dev_info->max_lu_supported) {
360 		pr_err("Max General LU supported by UFS isn't initialized\n");
361 		return false;
362 	}
363 	return lun == UFS_UPIU_RPMB_WLUN || (lun < dev_info->max_lu_supported);
364 }
365 
366 static inline void ufshcd_inc_sq_tail(struct ufs_hw_queue *q)
367 	__must_hold(&q->sq_lock)
368 {
369 	u32 mask = q->max_entries - 1;
370 	u32 val;
371 
372 	q->sq_tail_slot = (q->sq_tail_slot + 1) & mask;
373 	val = q->sq_tail_slot * sizeof(struct utp_transfer_req_desc);
374 	writel(val, q->mcq_sq_tail);
375 }
376 
377 static inline void ufshcd_mcq_update_cq_tail_slot(struct ufs_hw_queue *q)
378 {
379 	u32 val = readl(q->mcq_cq_tail);
380 
381 	q->cq_tail_slot = val / sizeof(struct cq_entry);
382 }
383 
384 static inline bool ufshcd_mcq_is_cq_empty(struct ufs_hw_queue *q)
385 {
386 	return q->cq_head_slot == q->cq_tail_slot;
387 }
388 
389 static inline void ufshcd_mcq_inc_cq_head_slot(struct ufs_hw_queue *q)
390 {
391 	q->cq_head_slot++;
392 	if (q->cq_head_slot == q->max_entries)
393 		q->cq_head_slot = 0;
394 }
395 
396 static inline void ufshcd_mcq_update_cq_head(struct ufs_hw_queue *q)
397 {
398 	writel(q->cq_head_slot * sizeof(struct cq_entry), q->mcq_cq_head);
399 }
400 
401 static inline struct cq_entry *ufshcd_mcq_cur_cqe(struct ufs_hw_queue *q)
402 {
403 	struct cq_entry *cqe = q->cqe_base_addr;
404 
405 	return cqe + q->cq_head_slot;
406 }
407 #endif /* _UFSHCD_PRIV_H_ */
408