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