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_read_desc_param(struct ufs_hba *hba,
46 enum desc_idn desc_id,
47 int desc_index,
48 u8 param_offset,
49 u8 *param_read_buf,
50 u8 param_size);
51 int ufshcd_query_attr_retry(struct ufs_hba *hba, enum query_opcode opcode,
52 enum attr_idn idn, u8 index, u8 selector,
53 u32 *attr_val);
54 int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
55 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val);
56 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
57 enum flag_idn idn, u8 index, bool *flag_res);
58 void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit);
59 bool ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag,
60 struct cq_entry *cqe, struct scsi_cmnd *compl_cmd);
61 int ufshcd_mcq_init(struct ufs_hba *hba);
62 int ufshcd_mcq_decide_queue_depth(struct ufs_hba *hba);
63 int ufshcd_mcq_memory_alloc(struct ufs_hba *hba);
64 void ufshcd_mcq_make_queues_operational(struct ufs_hba *hba);
65 void ufshcd_mcq_config_mac(struct ufs_hba *hba, u32 max_active_cmds);
66 void ufshcd_mcq_select_mcq_mode(struct ufs_hba *hba);
67 u32 ufshcd_mcq_read_cqis(struct ufs_hba *hba, int i);
68 void ufshcd_mcq_write_cqis(struct ufs_hba *hba, u32 val, int i);
69 unsigned long ufshcd_mcq_poll_cqe_nolock(struct ufs_hba *hba,
70 struct ufs_hw_queue *hwq,
71 struct scsi_cmnd **compl_cmd);
72 struct ufs_hw_queue *ufshcd_mcq_req_to_hwq(struct ufs_hba *hba,
73 struct request *req);
74 unsigned long ufshcd_mcq_poll_cqe_lock(struct ufs_hba *hba,
75 struct ufs_hw_queue *hwq,
76 struct scsi_cmnd **compl_cmd);
77
78 #define UFSHCD_MCQ_IO_QUEUE_OFFSET 1
79 #define SD_ASCII_STD true
80 #define SD_RAW false
81 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
82 u8 **buf, bool ascii);
83
84 void ufshcd_hold(struct ufs_hba *hba, bool async);
85 void ufshcd_release(struct ufs_hba *hba);
86
87 int ufshcd_send_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 int 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,enum ufs_notify_change_status status)120 static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba,
121 bool up, enum ufs_notify_change_status status)
122 {
123 if (hba->vops && hba->vops->clk_scale_notify)
124 return hba->vops->clk_scale_notify(hba, up, status);
125 return 0;
126 }
127
ufshcd_vops_event_notify(struct ufs_hba * hba,enum ufs_event_type evt,void * data)128 static inline void ufshcd_vops_event_notify(struct ufs_hba *hba,
129 enum ufs_event_type evt,
130 void *data)
131 {
132 if (hba->vops && hba->vops->event_notify)
133 hba->vops->event_notify(hba, evt, data);
134 }
135
ufshcd_vops_setup_clocks(struct ufs_hba * hba,bool on,enum ufs_notify_change_status status)136 static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on,
137 enum ufs_notify_change_status status)
138 {
139 if (hba->vops && hba->vops->setup_clocks)
140 return hba->vops->setup_clocks(hba, on, status);
141 return 0;
142 }
143
ufshcd_vops_hce_enable_notify(struct ufs_hba * hba,bool status)144 static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba,
145 bool status)
146 {
147 if (hba->vops && hba->vops->hce_enable_notify)
148 return hba->vops->hce_enable_notify(hba, status);
149
150 return 0;
151 }
ufshcd_vops_link_startup_notify(struct ufs_hba * hba,bool status)152 static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba,
153 bool status)
154 {
155 if (hba->vops && hba->vops->link_startup_notify)
156 return hba->vops->link_startup_notify(hba, status);
157
158 return 0;
159 }
160
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)161 static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba,
162 enum ufs_notify_change_status status,
163 struct ufs_pa_layer_attr *dev_max_params,
164 struct ufs_pa_layer_attr *dev_req_params)
165 {
166 if (hba->vops && hba->vops->pwr_change_notify)
167 return hba->vops->pwr_change_notify(hba, status,
168 dev_max_params, dev_req_params);
169
170 return -ENOTSUPP;
171 }
172
ufshcd_vops_setup_task_mgmt(struct ufs_hba * hba,int tag,u8 tm_function)173 static inline void ufshcd_vops_setup_task_mgmt(struct ufs_hba *hba,
174 int tag, u8 tm_function)
175 {
176 if (hba->vops && hba->vops->setup_task_mgmt)
177 return hba->vops->setup_task_mgmt(hba, tag, tm_function);
178 }
179
ufshcd_vops_hibern8_notify(struct ufs_hba * hba,enum uic_cmd_dme cmd,enum ufs_notify_change_status status)180 static inline void ufshcd_vops_hibern8_notify(struct ufs_hba *hba,
181 enum uic_cmd_dme cmd,
182 enum ufs_notify_change_status status)
183 {
184 if (hba->vops && hba->vops->hibern8_notify)
185 return hba->vops->hibern8_notify(hba, cmd, status);
186 }
187
ufshcd_vops_apply_dev_quirks(struct ufs_hba * hba)188 static inline int ufshcd_vops_apply_dev_quirks(struct ufs_hba *hba)
189 {
190 if (hba->vops && hba->vops->apply_dev_quirks)
191 return hba->vops->apply_dev_quirks(hba);
192 return 0;
193 }
194
ufshcd_vops_fixup_dev_quirks(struct ufs_hba * hba)195 static inline void ufshcd_vops_fixup_dev_quirks(struct ufs_hba *hba)
196 {
197 if (hba->vops && hba->vops->fixup_dev_quirks)
198 hba->vops->fixup_dev_quirks(hba);
199 }
200
ufshcd_vops_suspend(struct ufs_hba * hba,enum ufs_pm_op op,enum ufs_notify_change_status status)201 static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op,
202 enum ufs_notify_change_status status)
203 {
204 if (hba->vops && hba->vops->suspend)
205 return hba->vops->suspend(hba, op, status);
206
207 return 0;
208 }
209
ufshcd_vops_resume(struct ufs_hba * hba,enum ufs_pm_op op)210 static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op)
211 {
212 if (hba->vops && hba->vops->resume)
213 return hba->vops->resume(hba, op);
214
215 return 0;
216 }
217
ufshcd_vops_dbg_register_dump(struct ufs_hba * hba)218 static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba)
219 {
220 if (hba->vops && hba->vops->dbg_register_dump)
221 hba->vops->dbg_register_dump(hba);
222 }
223
ufshcd_vops_device_reset(struct ufs_hba * hba)224 static inline int ufshcd_vops_device_reset(struct ufs_hba *hba)
225 {
226 if (hba->vops && hba->vops->device_reset)
227 return hba->vops->device_reset(hba);
228
229 return -EOPNOTSUPP;
230 }
231
ufshcd_vops_config_scaling_param(struct ufs_hba * hba,struct devfreq_dev_profile * p,struct devfreq_simple_ondemand_data * data)232 static inline void ufshcd_vops_config_scaling_param(struct ufs_hba *hba,
233 struct devfreq_dev_profile *p,
234 struct devfreq_simple_ondemand_data *data)
235 {
236 if (hba->vops && hba->vops->config_scaling_param)
237 hba->vops->config_scaling_param(hba, p, data);
238 }
239
ufshcd_vops_reinit_notify(struct ufs_hba * hba)240 static inline void ufshcd_vops_reinit_notify(struct ufs_hba *hba)
241 {
242 if (hba->vops && hba->vops->reinit_notify)
243 hba->vops->reinit_notify(hba);
244 }
245
ufshcd_vops_mcq_config_resource(struct ufs_hba * hba)246 static inline int ufshcd_vops_mcq_config_resource(struct ufs_hba *hba)
247 {
248 if (hba->vops && hba->vops->mcq_config_resource)
249 return hba->vops->mcq_config_resource(hba);
250
251 return -EOPNOTSUPP;
252 }
253
ufshcd_mcq_vops_get_hba_mac(struct ufs_hba * hba)254 static inline int ufshcd_mcq_vops_get_hba_mac(struct ufs_hba *hba)
255 {
256 if (hba->vops && hba->vops->get_hba_mac)
257 return hba->vops->get_hba_mac(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
287 extern const struct ufs_pm_lvl_states ufs_pm_lvl_states[];
288
289 /**
290 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
291 * @scsi_lun: scsi LUN id
292 *
293 * Returns UPIU LUN id
294 */
ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)295 static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
296 {
297 if (scsi_is_wlun(scsi_lun))
298 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
299 | UFS_UPIU_WLUN_ID;
300 else
301 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
302 }
303
304 int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask);
305 int ufshcd_write_ee_control(struct ufs_hba *hba);
306 int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask,
307 const u16 *other_mask, u16 set, u16 clr);
308
ufshcd_update_ee_drv_mask(struct ufs_hba * hba,u16 set,u16 clr)309 static inline int ufshcd_update_ee_drv_mask(struct ufs_hba *hba,
310 u16 set, u16 clr)
311 {
312 return ufshcd_update_ee_control(hba, &hba->ee_drv_mask,
313 &hba->ee_usr_mask, set, clr);
314 }
315
ufshcd_update_ee_usr_mask(struct ufs_hba * hba,u16 set,u16 clr)316 static inline int ufshcd_update_ee_usr_mask(struct ufs_hba *hba,
317 u16 set, u16 clr)
318 {
319 return ufshcd_update_ee_control(hba, &hba->ee_usr_mask,
320 &hba->ee_drv_mask, set, clr);
321 }
322
ufshcd_rpm_get_sync(struct ufs_hba * hba)323 static inline int ufshcd_rpm_get_sync(struct ufs_hba *hba)
324 {
325 return pm_runtime_get_sync(&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 {
365 u32 mask = q->max_entries - 1;
366 u32 val;
367
368 q->sq_tail_slot = (q->sq_tail_slot + 1) & mask;
369 val = q->sq_tail_slot * sizeof(struct utp_transfer_req_desc);
370 writel(val, q->mcq_sq_tail);
371 }
372
ufshcd_mcq_update_cq_tail_slot(struct ufs_hw_queue * q)373 static inline void ufshcd_mcq_update_cq_tail_slot(struct ufs_hw_queue *q)
374 {
375 u32 val = readl(q->mcq_cq_tail);
376
377 q->cq_tail_slot = val / sizeof(struct cq_entry);
378 }
379
ufshcd_mcq_is_cq_empty(struct ufs_hw_queue * q)380 static inline bool ufshcd_mcq_is_cq_empty(struct ufs_hw_queue *q)
381 {
382 return q->cq_head_slot == q->cq_tail_slot;
383 }
384
ufshcd_mcq_inc_cq_head_slot(struct ufs_hw_queue * q)385 static inline void ufshcd_mcq_inc_cq_head_slot(struct ufs_hw_queue *q)
386 {
387 q->cq_head_slot++;
388 if (q->cq_head_slot == q->max_entries)
389 q->cq_head_slot = 0;
390 }
391
ufshcd_mcq_update_cq_head(struct ufs_hw_queue * q)392 static inline void ufshcd_mcq_update_cq_head(struct ufs_hw_queue *q)
393 {
394 writel(q->cq_head_slot * sizeof(struct cq_entry), q->mcq_cq_head);
395 }
396
ufshcd_mcq_cur_cqe(struct ufs_hw_queue * q)397 static inline struct cq_entry *ufshcd_mcq_cur_cqe(struct ufs_hw_queue *q)
398 {
399 struct cq_entry *cqe = q->cqe_base_addr;
400
401 return cqe + q->cq_head_slot;
402 }
403 #endif /* _UFSHCD_PRIV_H_ */
404