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