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