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 void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs); 10 11 static inline bool ufshcd_is_user_access_allowed(struct ufs_hba *hba) 12 { 13 return !hba->shutting_down; 14 } 15 16 void ufshcd_schedule_eh_work(struct ufs_hba *hba); 17 18 static inline bool ufshcd_keep_autobkops_enabled_except_suspend( 19 struct ufs_hba *hba) 20 { 21 return hba->caps & UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND; 22 } 23 24 static inline u8 ufshcd_wb_get_query_index(struct ufs_hba *hba) 25 { 26 if (hba->dev_info.wb_buffer_type == WB_BUF_MODE_LU_DEDICATED) 27 return hba->dev_info.wb_dedicated_lu; 28 return 0; 29 } 30 31 static inline bool ufshcd_is_wb_buf_flush_allowed(struct ufs_hba *hba) 32 { 33 return ufshcd_is_wb_allowed(hba) && 34 !(hba->quirks & UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL); 35 } 36 37 #ifdef CONFIG_SCSI_UFS_HWMON 38 void ufs_hwmon_probe(struct ufs_hba *hba, u8 mask); 39 void ufs_hwmon_remove(struct ufs_hba *hba); 40 void ufs_hwmon_notify_event(struct ufs_hba *hba, u8 ee_mask); 41 #else 42 static inline void ufs_hwmon_probe(struct ufs_hba *hba, u8 mask) {} 43 static inline void ufs_hwmon_remove(struct ufs_hba *hba) {} 44 static inline void ufs_hwmon_notify_event(struct ufs_hba *hba, u8 ee_mask) {} 45 #endif 46 47 int ufshcd_query_descriptor_retry(struct ufs_hba *hba, 48 enum query_opcode opcode, 49 enum desc_idn idn, u8 index, 50 u8 selector, 51 u8 *desc_buf, int *buf_len); 52 int ufshcd_read_desc_param(struct ufs_hba *hba, 53 enum desc_idn desc_id, 54 int desc_index, 55 u8 param_offset, 56 u8 *param_read_buf, 57 u8 param_size); 58 int ufshcd_query_attr_retry(struct ufs_hba *hba, enum query_opcode opcode, 59 enum attr_idn idn, u8 index, u8 selector, 60 u32 *attr_val); 61 int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, 62 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val); 63 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, 64 enum flag_idn idn, u8 index, bool *flag_res); 65 void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit); 66 void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag, 67 struct cq_entry *cqe); 68 int ufshcd_mcq_init(struct ufs_hba *hba); 69 void ufshcd_mcq_disable(struct ufs_hba *hba); 70 int ufshcd_get_hba_mac(struct ufs_hba *hba); 71 int ufshcd_mcq_memory_alloc(struct ufs_hba *hba); 72 struct ufs_hw_queue *ufshcd_mcq_req_to_hwq(struct ufs_hba *hba, 73 struct request *req); 74 void ufshcd_mcq_compl_all_cqes_lock(struct ufs_hba *hba, 75 struct ufs_hw_queue *hwq); 76 bool ufshcd_cmd_inflight(struct scsi_cmnd *cmd); 77 int ufshcd_mcq_sq_cleanup(struct ufs_hba *hba, int task_tag); 78 int ufshcd_mcq_abort(struct scsi_cmnd *cmd); 79 int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag); 80 void ufshcd_release_scsi_cmd(struct ufs_hba *hba, struct scsi_cmnd *cmd); 81 82 /** 83 * enum ufs_descr_fmt - UFS string descriptor format 84 * @SD_RAW: Raw UTF-16 format 85 * @SD_ASCII_STD: Convert to null-terminated ASCII string 86 */ 87 enum ufs_descr_fmt { 88 SD_RAW = 0, 89 SD_ASCII_STD = 1, 90 }; 91 92 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, u8 **buf, enum ufs_descr_fmt fmt); 93 int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd); 94 int ufshcd_send_bsg_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd); 95 96 int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, 97 struct utp_upiu_req *req_upiu, 98 struct utp_upiu_req *rsp_upiu, 99 enum upiu_request_transaction msgcode, 100 u8 *desc_buff, int *buff_len, 101 enum query_opcode desc_op); 102 103 int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable); 104 int ufshcd_read_device_lvl_exception_id(struct ufs_hba *hba, u64 *exception_id); 105 106 /* Wrapper functions for safely calling variant operations */ 107 static inline const char *ufshcd_get_var_name(struct ufs_hba *hba) 108 { 109 if (hba->vops) 110 return hba->vops->name; 111 return ""; 112 } 113 114 static inline void ufshcd_vops_exit(struct ufs_hba *hba) 115 { 116 if (hba->vops && hba->vops->exit) 117 return hba->vops->exit(hba); 118 } 119 120 static inline u32 ufshcd_vops_get_ufs_hci_version(struct ufs_hba *hba) 121 { 122 if (hba->vops && hba->vops->get_ufs_hci_version) 123 return hba->vops->get_ufs_hci_version(hba); 124 125 return ufshcd_readl(hba, REG_UFS_VERSION); 126 } 127 128 static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba, bool up, 129 unsigned long target_freq, 130 enum ufs_notify_change_status status) 131 { 132 if (hba->vops && hba->vops->clk_scale_notify) 133 return hba->vops->clk_scale_notify(hba, up, target_freq, status); 134 return 0; 135 } 136 137 static inline void ufshcd_vops_event_notify(struct ufs_hba *hba, 138 enum ufs_event_type evt, 139 void *data) 140 { 141 if (hba->vops && hba->vops->event_notify) 142 hba->vops->event_notify(hba, evt, data); 143 } 144 145 static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on, 146 enum ufs_notify_change_status status) 147 { 148 if (hba->vops && hba->vops->setup_clocks) 149 return hba->vops->setup_clocks(hba, on, status); 150 return 0; 151 } 152 153 static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba, 154 bool status) 155 { 156 if (hba->vops && hba->vops->hce_enable_notify) 157 return hba->vops->hce_enable_notify(hba, status); 158 159 return 0; 160 } 161 static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba, 162 bool status) 163 { 164 if (hba->vops && hba->vops->link_startup_notify) 165 return hba->vops->link_startup_notify(hba, status); 166 167 return 0; 168 } 169 170 static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba, 171 enum ufs_notify_change_status status, 172 const struct ufs_pa_layer_attr *dev_max_params, 173 struct ufs_pa_layer_attr *dev_req_params) 174 { 175 if (hba->vops && hba->vops->pwr_change_notify) 176 return hba->vops->pwr_change_notify(hba, status, 177 dev_max_params, dev_req_params); 178 179 return -ENOTSUPP; 180 } 181 182 static inline void ufshcd_vops_setup_task_mgmt(struct ufs_hba *hba, 183 int tag, u8 tm_function) 184 { 185 if (hba->vops && hba->vops->setup_task_mgmt) 186 return hba->vops->setup_task_mgmt(hba, tag, tm_function); 187 } 188 189 static inline void ufshcd_vops_hibern8_notify(struct ufs_hba *hba, 190 enum uic_cmd_dme cmd, 191 enum ufs_notify_change_status status) 192 { 193 if (hba->vops && hba->vops->hibern8_notify) 194 return hba->vops->hibern8_notify(hba, cmd, status); 195 } 196 197 static inline int ufshcd_vops_apply_dev_quirks(struct ufs_hba *hba) 198 { 199 if (hba->vops && hba->vops->apply_dev_quirks) 200 return hba->vops->apply_dev_quirks(hba); 201 return 0; 202 } 203 204 static inline void ufshcd_vops_fixup_dev_quirks(struct ufs_hba *hba) 205 { 206 if (hba->vops && hba->vops->fixup_dev_quirks) 207 hba->vops->fixup_dev_quirks(hba); 208 } 209 210 static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op, 211 enum ufs_notify_change_status status) 212 { 213 if (hba->vops && hba->vops->suspend) 214 return hba->vops->suspend(hba, op, status); 215 216 return 0; 217 } 218 219 static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op) 220 { 221 if (hba->vops && hba->vops->resume) 222 return hba->vops->resume(hba, op); 223 224 return 0; 225 } 226 227 static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba) 228 { 229 if (hba->vops && hba->vops->dbg_register_dump) 230 hba->vops->dbg_register_dump(hba); 231 } 232 233 static inline int ufshcd_vops_device_reset(struct ufs_hba *hba) 234 { 235 if (hba->vops && hba->vops->device_reset) 236 return hba->vops->device_reset(hba); 237 238 return -EOPNOTSUPP; 239 } 240 241 static inline void ufshcd_vops_config_scaling_param(struct ufs_hba *hba, 242 struct devfreq_dev_profile *p, 243 struct devfreq_simple_ondemand_data *data) 244 { 245 if (hba->vops && hba->vops->config_scaling_param) 246 hba->vops->config_scaling_param(hba, p, data); 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_op_runtime_config(struct ufs_hba *hba) 258 { 259 if (hba->vops && hba->vops->op_runtime_config) 260 return hba->vops->op_runtime_config(hba); 261 262 return -EOPNOTSUPP; 263 } 264 265 static inline int ufshcd_vops_get_outstanding_cqs(struct ufs_hba *hba, 266 unsigned long *ocqs) 267 { 268 if (hba->vops && hba->vops->get_outstanding_cqs) 269 return hba->vops->get_outstanding_cqs(hba, ocqs); 270 271 return -EOPNOTSUPP; 272 } 273 274 static inline int ufshcd_mcq_vops_config_esi(struct ufs_hba *hba) 275 { 276 if (hba->vops && hba->vops->config_esi) 277 return hba->vops->config_esi(hba); 278 279 return -EOPNOTSUPP; 280 } 281 282 static inline u32 ufshcd_vops_freq_to_gear_speed(struct ufs_hba *hba, unsigned long freq) 283 { 284 if (hba->vops && hba->vops->freq_to_gear_speed) 285 return hba->vops->freq_to_gear_speed(hba, freq); 286 287 return 0; 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 * Return: 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_get_if_active(struct ufs_hba *hba) 332 { 333 return pm_runtime_get_if_active(&hba->ufs_device_wlun->sdev_gendev); 334 } 335 336 static inline int ufshcd_rpm_put_sync(struct ufs_hba *hba) 337 { 338 return pm_runtime_put_sync(&hba->ufs_device_wlun->sdev_gendev); 339 } 340 341 static inline void ufshcd_rpm_get_noresume(struct ufs_hba *hba) 342 { 343 pm_runtime_get_noresume(&hba->ufs_device_wlun->sdev_gendev); 344 } 345 346 static inline int ufshcd_rpm_resume(struct ufs_hba *hba) 347 { 348 return pm_runtime_resume(&hba->ufs_device_wlun->sdev_gendev); 349 } 350 351 static inline int ufshcd_rpm_put(struct ufs_hba *hba) 352 { 353 return pm_runtime_put(&hba->ufs_device_wlun->sdev_gendev); 354 } 355 356 /** 357 * ufs_is_valid_unit_desc_lun - checks if the given LUN has a unit descriptor 358 * @dev_info: pointer of instance of struct ufs_dev_info 359 * @lun: LU number to check 360 * @return: true if the lun has a matching unit descriptor, false otherwise 361 */ 362 static inline bool ufs_is_valid_unit_desc_lun(struct ufs_dev_info *dev_info, u8 lun) 363 { 364 if (!dev_info || !dev_info->max_lu_supported) { 365 pr_err("Max General LU supported by UFS isn't initialized\n"); 366 return false; 367 } 368 return lun == UFS_UPIU_RPMB_WLUN || (lun < dev_info->max_lu_supported); 369 } 370 371 /* 372 * Convert a block layer tag into a SCSI command pointer. This function is 373 * called once per I/O completion path and is also called from error paths. 374 */ 375 static inline struct scsi_cmnd *ufshcd_tag_to_cmd(struct ufs_hba *hba, u32 tag) 376 { 377 /* 378 * Host-wide tags are enabled in MCQ mode only. See also the 379 * host->host_tagset assignment in ufs-mcq.c. 380 */ 381 struct blk_mq_tags *tags = hba->host->tag_set.shared_tags ?: 382 hba->host->tag_set.tags[0]; 383 struct request *rq = blk_mq_tag_to_rq(tags, tag); 384 385 if (WARN_ON_ONCE(!rq)) 386 return NULL; 387 388 return blk_mq_rq_to_pdu(rq); 389 } 390 391 static inline void ufshcd_inc_sq_tail(struct ufs_hw_queue *q) 392 __must_hold(&q->sq_lock) 393 { 394 u32 val; 395 396 q->sq_tail_slot++; 397 if (q->sq_tail_slot == q->max_entries) 398 q->sq_tail_slot = 0; 399 val = q->sq_tail_slot * sizeof(struct utp_transfer_req_desc); 400 writel(val, q->mcq_sq_tail); 401 } 402 403 static inline void ufshcd_mcq_update_cq_tail_slot(struct ufs_hw_queue *q) 404 { 405 u32 val = readl(q->mcq_cq_tail); 406 407 q->cq_tail_slot = val / sizeof(struct cq_entry); 408 } 409 410 static inline bool ufshcd_mcq_is_cq_empty(struct ufs_hw_queue *q) 411 { 412 return q->cq_head_slot == q->cq_tail_slot; 413 } 414 415 static inline void ufshcd_mcq_inc_cq_head_slot(struct ufs_hw_queue *q) 416 { 417 q->cq_head_slot++; 418 if (q->cq_head_slot == q->max_entries) 419 q->cq_head_slot = 0; 420 } 421 422 static inline void ufshcd_mcq_update_cq_head(struct ufs_hw_queue *q) 423 { 424 writel(q->cq_head_slot * sizeof(struct cq_entry), q->mcq_cq_head); 425 } 426 427 static inline struct cq_entry *ufshcd_mcq_cur_cqe(struct ufs_hw_queue *q) 428 { 429 struct cq_entry *cqe = q->cqe_base_addr; 430 431 return cqe + q->cq_head_slot; 432 } 433 434 static inline u32 ufshcd_mcq_get_sq_head_slot(struct ufs_hw_queue *q) 435 { 436 u32 val = readl(q->mcq_sq_head); 437 438 return val / sizeof(struct utp_transfer_req_desc); 439 } 440 441 #if IS_ENABLED(CONFIG_RPMB) 442 int ufs_rpmb_probe(struct ufs_hba *hba); 443 void ufs_rpmb_remove(struct ufs_hba *hba); 444 #else 445 static inline int ufs_rpmb_probe(struct ufs_hba *hba) 446 { 447 return 0; 448 } 449 static inline void ufs_rpmb_remove(struct ufs_hba *hba) 450 { 451 } 452 #endif 453 454 #endif /* _UFSHCD_PRIV_H_ */ 455