1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2018 Western Digital Corporation 3 4 #include <linux/err.h> 5 #include <linux/string.h> 6 #include <linux/bitfield.h> 7 #include <asm/unaligned.h> 8 9 #include <ufs/ufs.h> 10 #include "ufs-sysfs.h" 11 #include "ufshcd-priv.h" 12 13 static const char *ufshcd_uic_link_state_to_string( 14 enum uic_link_state state) 15 { 16 switch (state) { 17 case UIC_LINK_OFF_STATE: return "OFF"; 18 case UIC_LINK_ACTIVE_STATE: return "ACTIVE"; 19 case UIC_LINK_HIBERN8_STATE: return "HIBERN8"; 20 case UIC_LINK_BROKEN_STATE: return "BROKEN"; 21 default: return "UNKNOWN"; 22 } 23 } 24 25 static const char *ufshcd_ufs_dev_pwr_mode_to_string( 26 enum ufs_dev_pwr_mode state) 27 { 28 switch (state) { 29 case UFS_ACTIVE_PWR_MODE: return "ACTIVE"; 30 case UFS_SLEEP_PWR_MODE: return "SLEEP"; 31 case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN"; 32 case UFS_DEEPSLEEP_PWR_MODE: return "DEEPSLEEP"; 33 default: return "UNKNOWN"; 34 } 35 } 36 37 static inline ssize_t ufs_sysfs_pm_lvl_store(struct device *dev, 38 struct device_attribute *attr, 39 const char *buf, size_t count, 40 bool rpm) 41 { 42 struct ufs_hba *hba = dev_get_drvdata(dev); 43 struct ufs_dev_info *dev_info = &hba->dev_info; 44 unsigned long flags, value; 45 46 if (kstrtoul(buf, 0, &value)) 47 return -EINVAL; 48 49 if (value >= UFS_PM_LVL_MAX) 50 return -EINVAL; 51 52 if (ufs_pm_lvl_states[value].dev_state == UFS_DEEPSLEEP_PWR_MODE && 53 (!(hba->caps & UFSHCD_CAP_DEEPSLEEP) || 54 !(dev_info->wspecversion >= 0x310))) 55 return -EINVAL; 56 57 spin_lock_irqsave(hba->host->host_lock, flags); 58 if (rpm) 59 hba->rpm_lvl = value; 60 else 61 hba->spm_lvl = value; 62 spin_unlock_irqrestore(hba->host->host_lock, flags); 63 return count; 64 } 65 66 static ssize_t rpm_lvl_show(struct device *dev, 67 struct device_attribute *attr, char *buf) 68 { 69 struct ufs_hba *hba = dev_get_drvdata(dev); 70 71 return sysfs_emit(buf, "%d\n", hba->rpm_lvl); 72 } 73 74 static ssize_t rpm_lvl_store(struct device *dev, 75 struct device_attribute *attr, const char *buf, size_t count) 76 { 77 return ufs_sysfs_pm_lvl_store(dev, attr, buf, count, true); 78 } 79 80 static ssize_t rpm_target_dev_state_show(struct device *dev, 81 struct device_attribute *attr, char *buf) 82 { 83 struct ufs_hba *hba = dev_get_drvdata(dev); 84 85 return sysfs_emit(buf, "%s\n", ufshcd_ufs_dev_pwr_mode_to_string( 86 ufs_pm_lvl_states[hba->rpm_lvl].dev_state)); 87 } 88 89 static ssize_t rpm_target_link_state_show(struct device *dev, 90 struct device_attribute *attr, char *buf) 91 { 92 struct ufs_hba *hba = dev_get_drvdata(dev); 93 94 return sysfs_emit(buf, "%s\n", ufshcd_uic_link_state_to_string( 95 ufs_pm_lvl_states[hba->rpm_lvl].link_state)); 96 } 97 98 static ssize_t spm_lvl_show(struct device *dev, 99 struct device_attribute *attr, char *buf) 100 { 101 struct ufs_hba *hba = dev_get_drvdata(dev); 102 103 return sysfs_emit(buf, "%d\n", hba->spm_lvl); 104 } 105 106 static ssize_t spm_lvl_store(struct device *dev, 107 struct device_attribute *attr, const char *buf, size_t count) 108 { 109 return ufs_sysfs_pm_lvl_store(dev, attr, buf, count, false); 110 } 111 112 static ssize_t spm_target_dev_state_show(struct device *dev, 113 struct device_attribute *attr, char *buf) 114 { 115 struct ufs_hba *hba = dev_get_drvdata(dev); 116 117 return sysfs_emit(buf, "%s\n", ufshcd_ufs_dev_pwr_mode_to_string( 118 ufs_pm_lvl_states[hba->spm_lvl].dev_state)); 119 } 120 121 static ssize_t spm_target_link_state_show(struct device *dev, 122 struct device_attribute *attr, char *buf) 123 { 124 struct ufs_hba *hba = dev_get_drvdata(dev); 125 126 return sysfs_emit(buf, "%s\n", ufshcd_uic_link_state_to_string( 127 ufs_pm_lvl_states[hba->spm_lvl].link_state)); 128 } 129 130 /* Convert Auto-Hibernate Idle Timer register value to microseconds */ 131 static int ufshcd_ahit_to_us(u32 ahit) 132 { 133 int timer = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, ahit); 134 int scale = FIELD_GET(UFSHCI_AHIBERN8_SCALE_MASK, ahit); 135 136 for (; scale > 0; --scale) 137 timer *= UFSHCI_AHIBERN8_SCALE_FACTOR; 138 139 return timer; 140 } 141 142 /* Convert microseconds to Auto-Hibernate Idle Timer register value */ 143 static u32 ufshcd_us_to_ahit(unsigned int timer) 144 { 145 unsigned int scale; 146 147 for (scale = 0; timer > UFSHCI_AHIBERN8_TIMER_MASK; ++scale) 148 timer /= UFSHCI_AHIBERN8_SCALE_FACTOR; 149 150 return FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, timer) | 151 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, scale); 152 } 153 154 static ssize_t auto_hibern8_show(struct device *dev, 155 struct device_attribute *attr, char *buf) 156 { 157 u32 ahit; 158 int ret; 159 struct ufs_hba *hba = dev_get_drvdata(dev); 160 161 if (!ufshcd_is_auto_hibern8_supported(hba)) 162 return -EOPNOTSUPP; 163 164 down(&hba->host_sem); 165 if (!ufshcd_is_user_access_allowed(hba)) { 166 ret = -EBUSY; 167 goto out; 168 } 169 170 pm_runtime_get_sync(hba->dev); 171 ufshcd_hold(hba, false); 172 ahit = ufshcd_readl(hba, REG_AUTO_HIBERNATE_IDLE_TIMER); 173 ufshcd_release(hba); 174 pm_runtime_put_sync(hba->dev); 175 176 ret = sysfs_emit(buf, "%d\n", ufshcd_ahit_to_us(ahit)); 177 178 out: 179 up(&hba->host_sem); 180 return ret; 181 } 182 183 static ssize_t auto_hibern8_store(struct device *dev, 184 struct device_attribute *attr, 185 const char *buf, size_t count) 186 { 187 struct ufs_hba *hba = dev_get_drvdata(dev); 188 unsigned int timer; 189 int ret = 0; 190 191 if (!ufshcd_is_auto_hibern8_supported(hba)) 192 return -EOPNOTSUPP; 193 194 if (kstrtouint(buf, 0, &timer)) 195 return -EINVAL; 196 197 if (timer > UFSHCI_AHIBERN8_MAX) 198 return -EINVAL; 199 200 down(&hba->host_sem); 201 if (!ufshcd_is_user_access_allowed(hba)) { 202 ret = -EBUSY; 203 goto out; 204 } 205 206 ufshcd_auto_hibern8_update(hba, ufshcd_us_to_ahit(timer)); 207 208 out: 209 up(&hba->host_sem); 210 return ret ? ret : count; 211 } 212 213 static ssize_t wb_on_show(struct device *dev, struct device_attribute *attr, 214 char *buf) 215 { 216 struct ufs_hba *hba = dev_get_drvdata(dev); 217 218 return sysfs_emit(buf, "%d\n", hba->dev_info.wb_enabled); 219 } 220 221 static ssize_t wb_on_store(struct device *dev, struct device_attribute *attr, 222 const char *buf, size_t count) 223 { 224 struct ufs_hba *hba = dev_get_drvdata(dev); 225 unsigned int wb_enable; 226 ssize_t res; 227 228 if (!ufshcd_is_wb_allowed(hba) || (ufshcd_is_clkscaling_supported(hba) 229 && ufshcd_enable_wb_if_scaling_up(hba))) { 230 /* 231 * If the platform supports UFSHCD_CAP_CLK_SCALING, turn WB 232 * on/off will be done while clock scaling up/down. 233 */ 234 dev_warn(dev, "It is not allowed to configure WB!\n"); 235 return -EOPNOTSUPP; 236 } 237 238 if (kstrtouint(buf, 0, &wb_enable)) 239 return -EINVAL; 240 241 if (wb_enable != 0 && wb_enable != 1) 242 return -EINVAL; 243 244 down(&hba->host_sem); 245 if (!ufshcd_is_user_access_allowed(hba)) { 246 res = -EBUSY; 247 goto out; 248 } 249 250 ufshcd_rpm_get_sync(hba); 251 res = ufshcd_wb_toggle(hba, wb_enable); 252 ufshcd_rpm_put_sync(hba); 253 out: 254 up(&hba->host_sem); 255 return res < 0 ? res : count; 256 } 257 258 static ssize_t enable_wb_buf_flush_show(struct device *dev, 259 struct device_attribute *attr, 260 char *buf) 261 { 262 struct ufs_hba *hba = dev_get_drvdata(dev); 263 264 return sysfs_emit(buf, "%d\n", hba->dev_info.wb_buf_flush_enabled); 265 } 266 267 static ssize_t enable_wb_buf_flush_store(struct device *dev, 268 struct device_attribute *attr, 269 const char *buf, size_t count) 270 { 271 struct ufs_hba *hba = dev_get_drvdata(dev); 272 unsigned int enable_wb_buf_flush; 273 ssize_t res; 274 275 if (!ufshcd_is_wb_buf_flush_allowed(hba)) { 276 dev_warn(dev, "It is not allowed to configure WB buf flushing!\n"); 277 return -EOPNOTSUPP; 278 } 279 280 if (kstrtouint(buf, 0, &enable_wb_buf_flush)) 281 return -EINVAL; 282 283 if (enable_wb_buf_flush != 0 && enable_wb_buf_flush != 1) 284 return -EINVAL; 285 286 down(&hba->host_sem); 287 if (!ufshcd_is_user_access_allowed(hba)) { 288 res = -EBUSY; 289 goto out; 290 } 291 292 ufshcd_rpm_get_sync(hba); 293 res = ufshcd_wb_toggle_buf_flush(hba, enable_wb_buf_flush); 294 ufshcd_rpm_put_sync(hba); 295 296 out: 297 up(&hba->host_sem); 298 return res < 0 ? res : count; 299 } 300 301 static DEVICE_ATTR_RW(rpm_lvl); 302 static DEVICE_ATTR_RO(rpm_target_dev_state); 303 static DEVICE_ATTR_RO(rpm_target_link_state); 304 static DEVICE_ATTR_RW(spm_lvl); 305 static DEVICE_ATTR_RO(spm_target_dev_state); 306 static DEVICE_ATTR_RO(spm_target_link_state); 307 static DEVICE_ATTR_RW(auto_hibern8); 308 static DEVICE_ATTR_RW(wb_on); 309 static DEVICE_ATTR_RW(enable_wb_buf_flush); 310 311 static struct attribute *ufs_sysfs_ufshcd_attrs[] = { 312 &dev_attr_rpm_lvl.attr, 313 &dev_attr_rpm_target_dev_state.attr, 314 &dev_attr_rpm_target_link_state.attr, 315 &dev_attr_spm_lvl.attr, 316 &dev_attr_spm_target_dev_state.attr, 317 &dev_attr_spm_target_link_state.attr, 318 &dev_attr_auto_hibern8.attr, 319 &dev_attr_wb_on.attr, 320 &dev_attr_enable_wb_buf_flush.attr, 321 NULL 322 }; 323 324 static const struct attribute_group ufs_sysfs_default_group = { 325 .attrs = ufs_sysfs_ufshcd_attrs, 326 }; 327 328 static ssize_t monitor_enable_show(struct device *dev, 329 struct device_attribute *attr, char *buf) 330 { 331 struct ufs_hba *hba = dev_get_drvdata(dev); 332 333 return sysfs_emit(buf, "%d\n", hba->monitor.enabled); 334 } 335 336 static ssize_t monitor_enable_store(struct device *dev, 337 struct device_attribute *attr, 338 const char *buf, size_t count) 339 { 340 struct ufs_hba *hba = dev_get_drvdata(dev); 341 unsigned long value, flags; 342 343 if (kstrtoul(buf, 0, &value)) 344 return -EINVAL; 345 346 value = !!value; 347 spin_lock_irqsave(hba->host->host_lock, flags); 348 if (value == hba->monitor.enabled) 349 goto out_unlock; 350 351 if (!value) { 352 memset(&hba->monitor, 0, sizeof(hba->monitor)); 353 } else { 354 hba->monitor.enabled = true; 355 hba->monitor.enabled_ts = ktime_get(); 356 } 357 358 out_unlock: 359 spin_unlock_irqrestore(hba->host->host_lock, flags); 360 return count; 361 } 362 363 static ssize_t monitor_chunk_size_show(struct device *dev, 364 struct device_attribute *attr, char *buf) 365 { 366 struct ufs_hba *hba = dev_get_drvdata(dev); 367 368 return sysfs_emit(buf, "%lu\n", hba->monitor.chunk_size); 369 } 370 371 static ssize_t monitor_chunk_size_store(struct device *dev, 372 struct device_attribute *attr, 373 const char *buf, size_t count) 374 { 375 struct ufs_hba *hba = dev_get_drvdata(dev); 376 unsigned long value, flags; 377 378 if (kstrtoul(buf, 0, &value)) 379 return -EINVAL; 380 381 spin_lock_irqsave(hba->host->host_lock, flags); 382 /* Only allow chunk size change when monitor is disabled */ 383 if (!hba->monitor.enabled) 384 hba->monitor.chunk_size = value; 385 spin_unlock_irqrestore(hba->host->host_lock, flags); 386 return count; 387 } 388 389 static ssize_t read_total_sectors_show(struct device *dev, 390 struct device_attribute *attr, char *buf) 391 { 392 struct ufs_hba *hba = dev_get_drvdata(dev); 393 394 return sysfs_emit(buf, "%lu\n", hba->monitor.nr_sec_rw[READ]); 395 } 396 397 static ssize_t read_total_busy_show(struct device *dev, 398 struct device_attribute *attr, char *buf) 399 { 400 struct ufs_hba *hba = dev_get_drvdata(dev); 401 402 return sysfs_emit(buf, "%llu\n", 403 ktime_to_us(hba->monitor.total_busy[READ])); 404 } 405 406 static ssize_t read_nr_requests_show(struct device *dev, 407 struct device_attribute *attr, char *buf) 408 { 409 struct ufs_hba *hba = dev_get_drvdata(dev); 410 411 return sysfs_emit(buf, "%lu\n", hba->monitor.nr_req[READ]); 412 } 413 414 static ssize_t read_req_latency_avg_show(struct device *dev, 415 struct device_attribute *attr, 416 char *buf) 417 { 418 struct ufs_hba *hba = dev_get_drvdata(dev); 419 struct ufs_hba_monitor *m = &hba->monitor; 420 421 return sysfs_emit(buf, "%llu\n", div_u64(ktime_to_us(m->lat_sum[READ]), 422 m->nr_req[READ])); 423 } 424 425 static ssize_t read_req_latency_max_show(struct device *dev, 426 struct device_attribute *attr, 427 char *buf) 428 { 429 struct ufs_hba *hba = dev_get_drvdata(dev); 430 431 return sysfs_emit(buf, "%llu\n", 432 ktime_to_us(hba->monitor.lat_max[READ])); 433 } 434 435 static ssize_t read_req_latency_min_show(struct device *dev, 436 struct device_attribute *attr, 437 char *buf) 438 { 439 struct ufs_hba *hba = dev_get_drvdata(dev); 440 441 return sysfs_emit(buf, "%llu\n", 442 ktime_to_us(hba->monitor.lat_min[READ])); 443 } 444 445 static ssize_t read_req_latency_sum_show(struct device *dev, 446 struct device_attribute *attr, 447 char *buf) 448 { 449 struct ufs_hba *hba = dev_get_drvdata(dev); 450 451 return sysfs_emit(buf, "%llu\n", 452 ktime_to_us(hba->monitor.lat_sum[READ])); 453 } 454 455 static ssize_t write_total_sectors_show(struct device *dev, 456 struct device_attribute *attr, 457 char *buf) 458 { 459 struct ufs_hba *hba = dev_get_drvdata(dev); 460 461 return sysfs_emit(buf, "%lu\n", hba->monitor.nr_sec_rw[WRITE]); 462 } 463 464 static ssize_t write_total_busy_show(struct device *dev, 465 struct device_attribute *attr, char *buf) 466 { 467 struct ufs_hba *hba = dev_get_drvdata(dev); 468 469 return sysfs_emit(buf, "%llu\n", 470 ktime_to_us(hba->monitor.total_busy[WRITE])); 471 } 472 473 static ssize_t write_nr_requests_show(struct device *dev, 474 struct device_attribute *attr, char *buf) 475 { 476 struct ufs_hba *hba = dev_get_drvdata(dev); 477 478 return sysfs_emit(buf, "%lu\n", hba->monitor.nr_req[WRITE]); 479 } 480 481 static ssize_t write_req_latency_avg_show(struct device *dev, 482 struct device_attribute *attr, 483 char *buf) 484 { 485 struct ufs_hba *hba = dev_get_drvdata(dev); 486 struct ufs_hba_monitor *m = &hba->monitor; 487 488 return sysfs_emit(buf, "%llu\n", div_u64(ktime_to_us(m->lat_sum[WRITE]), 489 m->nr_req[WRITE])); 490 } 491 492 static ssize_t write_req_latency_max_show(struct device *dev, 493 struct device_attribute *attr, 494 char *buf) 495 { 496 struct ufs_hba *hba = dev_get_drvdata(dev); 497 498 return sysfs_emit(buf, "%llu\n", 499 ktime_to_us(hba->monitor.lat_max[WRITE])); 500 } 501 502 static ssize_t write_req_latency_min_show(struct device *dev, 503 struct device_attribute *attr, 504 char *buf) 505 { 506 struct ufs_hba *hba = dev_get_drvdata(dev); 507 508 return sysfs_emit(buf, "%llu\n", 509 ktime_to_us(hba->monitor.lat_min[WRITE])); 510 } 511 512 static ssize_t write_req_latency_sum_show(struct device *dev, 513 struct device_attribute *attr, 514 char *buf) 515 { 516 struct ufs_hba *hba = dev_get_drvdata(dev); 517 518 return sysfs_emit(buf, "%llu\n", 519 ktime_to_us(hba->monitor.lat_sum[WRITE])); 520 } 521 522 static DEVICE_ATTR_RW(monitor_enable); 523 static DEVICE_ATTR_RW(monitor_chunk_size); 524 static DEVICE_ATTR_RO(read_total_sectors); 525 static DEVICE_ATTR_RO(read_total_busy); 526 static DEVICE_ATTR_RO(read_nr_requests); 527 static DEVICE_ATTR_RO(read_req_latency_avg); 528 static DEVICE_ATTR_RO(read_req_latency_max); 529 static DEVICE_ATTR_RO(read_req_latency_min); 530 static DEVICE_ATTR_RO(read_req_latency_sum); 531 static DEVICE_ATTR_RO(write_total_sectors); 532 static DEVICE_ATTR_RO(write_total_busy); 533 static DEVICE_ATTR_RO(write_nr_requests); 534 static DEVICE_ATTR_RO(write_req_latency_avg); 535 static DEVICE_ATTR_RO(write_req_latency_max); 536 static DEVICE_ATTR_RO(write_req_latency_min); 537 static DEVICE_ATTR_RO(write_req_latency_sum); 538 539 static struct attribute *ufs_sysfs_monitor_attrs[] = { 540 &dev_attr_monitor_enable.attr, 541 &dev_attr_monitor_chunk_size.attr, 542 &dev_attr_read_total_sectors.attr, 543 &dev_attr_read_total_busy.attr, 544 &dev_attr_read_nr_requests.attr, 545 &dev_attr_read_req_latency_avg.attr, 546 &dev_attr_read_req_latency_max.attr, 547 &dev_attr_read_req_latency_min.attr, 548 &dev_attr_read_req_latency_sum.attr, 549 &dev_attr_write_total_sectors.attr, 550 &dev_attr_write_total_busy.attr, 551 &dev_attr_write_nr_requests.attr, 552 &dev_attr_write_req_latency_avg.attr, 553 &dev_attr_write_req_latency_max.attr, 554 &dev_attr_write_req_latency_min.attr, 555 &dev_attr_write_req_latency_sum.attr, 556 NULL 557 }; 558 559 static const struct attribute_group ufs_sysfs_monitor_group = { 560 .name = "monitor", 561 .attrs = ufs_sysfs_monitor_attrs, 562 }; 563 564 static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba, 565 enum desc_idn desc_id, 566 u8 desc_index, 567 u8 param_offset, 568 u8 *sysfs_buf, 569 u8 param_size) 570 { 571 u8 desc_buf[8] = {0}; 572 int ret; 573 574 if (param_size > 8) 575 return -EINVAL; 576 577 down(&hba->host_sem); 578 if (!ufshcd_is_user_access_allowed(hba)) { 579 ret = -EBUSY; 580 goto out; 581 } 582 583 ufshcd_rpm_get_sync(hba); 584 ret = ufshcd_read_desc_param(hba, desc_id, desc_index, 585 param_offset, desc_buf, param_size); 586 ufshcd_rpm_put_sync(hba); 587 if (ret) { 588 ret = -EINVAL; 589 goto out; 590 } 591 592 switch (param_size) { 593 case 1: 594 ret = sysfs_emit(sysfs_buf, "0x%02X\n", *desc_buf); 595 break; 596 case 2: 597 ret = sysfs_emit(sysfs_buf, "0x%04X\n", 598 get_unaligned_be16(desc_buf)); 599 break; 600 case 4: 601 ret = sysfs_emit(sysfs_buf, "0x%08X\n", 602 get_unaligned_be32(desc_buf)); 603 break; 604 case 8: 605 ret = sysfs_emit(sysfs_buf, "0x%016llX\n", 606 get_unaligned_be64(desc_buf)); 607 break; 608 } 609 610 out: 611 up(&hba->host_sem); 612 return ret; 613 } 614 615 #define UFS_DESC_PARAM(_name, _puname, _duname, _size) \ 616 static ssize_t _name##_show(struct device *dev, \ 617 struct device_attribute *attr, char *buf) \ 618 { \ 619 struct ufs_hba *hba = dev_get_drvdata(dev); \ 620 return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname, \ 621 0, _duname##_DESC_PARAM##_puname, buf, _size); \ 622 } \ 623 static DEVICE_ATTR_RO(_name) 624 625 #define UFS_DEVICE_DESC_PARAM(_name, _uname, _size) \ 626 UFS_DESC_PARAM(_name, _uname, DEVICE, _size) 627 628 UFS_DEVICE_DESC_PARAM(device_type, _DEVICE_TYPE, 1); 629 UFS_DEVICE_DESC_PARAM(device_class, _DEVICE_CLASS, 1); 630 UFS_DEVICE_DESC_PARAM(device_sub_class, _DEVICE_SUB_CLASS, 1); 631 UFS_DEVICE_DESC_PARAM(protocol, _PRTCL, 1); 632 UFS_DEVICE_DESC_PARAM(number_of_luns, _NUM_LU, 1); 633 UFS_DEVICE_DESC_PARAM(number_of_wluns, _NUM_WLU, 1); 634 UFS_DEVICE_DESC_PARAM(boot_enable, _BOOT_ENBL, 1); 635 UFS_DEVICE_DESC_PARAM(descriptor_access_enable, _DESC_ACCSS_ENBL, 1); 636 UFS_DEVICE_DESC_PARAM(initial_power_mode, _INIT_PWR_MODE, 1); 637 UFS_DEVICE_DESC_PARAM(high_priority_lun, _HIGH_PR_LUN, 1); 638 UFS_DEVICE_DESC_PARAM(secure_removal_type, _SEC_RMV_TYPE, 1); 639 UFS_DEVICE_DESC_PARAM(support_security_lun, _SEC_LU, 1); 640 UFS_DEVICE_DESC_PARAM(bkops_termination_latency, _BKOP_TERM_LT, 1); 641 UFS_DEVICE_DESC_PARAM(initial_active_icc_level, _ACTVE_ICC_LVL, 1); 642 UFS_DEVICE_DESC_PARAM(specification_version, _SPEC_VER, 2); 643 UFS_DEVICE_DESC_PARAM(manufacturing_date, _MANF_DATE, 2); 644 UFS_DEVICE_DESC_PARAM(manufacturer_id, _MANF_ID, 2); 645 UFS_DEVICE_DESC_PARAM(rtt_capability, _RTT_CAP, 1); 646 UFS_DEVICE_DESC_PARAM(rtc_update, _FRQ_RTC, 2); 647 UFS_DEVICE_DESC_PARAM(ufs_features, _UFS_FEAT, 1); 648 UFS_DEVICE_DESC_PARAM(ffu_timeout, _FFU_TMT, 1); 649 UFS_DEVICE_DESC_PARAM(queue_depth, _Q_DPTH, 1); 650 UFS_DEVICE_DESC_PARAM(device_version, _DEV_VER, 2); 651 UFS_DEVICE_DESC_PARAM(number_of_secure_wpa, _NUM_SEC_WPA, 1); 652 UFS_DEVICE_DESC_PARAM(psa_max_data_size, _PSA_MAX_DATA, 4); 653 UFS_DEVICE_DESC_PARAM(psa_state_timeout, _PSA_TMT, 1); 654 UFS_DEVICE_DESC_PARAM(hpb_version, _HPB_VER, 2); 655 UFS_DEVICE_DESC_PARAM(hpb_control, _HPB_CONTROL, 1); 656 UFS_DEVICE_DESC_PARAM(ext_feature_sup, _EXT_UFS_FEATURE_SUP, 4); 657 UFS_DEVICE_DESC_PARAM(wb_presv_us_en, _WB_PRESRV_USRSPC_EN, 1); 658 UFS_DEVICE_DESC_PARAM(wb_type, _WB_TYPE, 1); 659 UFS_DEVICE_DESC_PARAM(wb_shared_alloc_units, _WB_SHARED_ALLOC_UNITS, 4); 660 661 static struct attribute *ufs_sysfs_device_descriptor[] = { 662 &dev_attr_device_type.attr, 663 &dev_attr_device_class.attr, 664 &dev_attr_device_sub_class.attr, 665 &dev_attr_protocol.attr, 666 &dev_attr_number_of_luns.attr, 667 &dev_attr_number_of_wluns.attr, 668 &dev_attr_boot_enable.attr, 669 &dev_attr_descriptor_access_enable.attr, 670 &dev_attr_initial_power_mode.attr, 671 &dev_attr_high_priority_lun.attr, 672 &dev_attr_secure_removal_type.attr, 673 &dev_attr_support_security_lun.attr, 674 &dev_attr_bkops_termination_latency.attr, 675 &dev_attr_initial_active_icc_level.attr, 676 &dev_attr_specification_version.attr, 677 &dev_attr_manufacturing_date.attr, 678 &dev_attr_manufacturer_id.attr, 679 &dev_attr_rtt_capability.attr, 680 &dev_attr_rtc_update.attr, 681 &dev_attr_ufs_features.attr, 682 &dev_attr_ffu_timeout.attr, 683 &dev_attr_queue_depth.attr, 684 &dev_attr_device_version.attr, 685 &dev_attr_number_of_secure_wpa.attr, 686 &dev_attr_psa_max_data_size.attr, 687 &dev_attr_psa_state_timeout.attr, 688 &dev_attr_hpb_version.attr, 689 &dev_attr_hpb_control.attr, 690 &dev_attr_ext_feature_sup.attr, 691 &dev_attr_wb_presv_us_en.attr, 692 &dev_attr_wb_type.attr, 693 &dev_attr_wb_shared_alloc_units.attr, 694 NULL, 695 }; 696 697 static const struct attribute_group ufs_sysfs_device_descriptor_group = { 698 .name = "device_descriptor", 699 .attrs = ufs_sysfs_device_descriptor, 700 }; 701 702 #define UFS_INTERCONNECT_DESC_PARAM(_name, _uname, _size) \ 703 UFS_DESC_PARAM(_name, _uname, INTERCONNECT, _size) 704 705 UFS_INTERCONNECT_DESC_PARAM(unipro_version, _UNIPRO_VER, 2); 706 UFS_INTERCONNECT_DESC_PARAM(mphy_version, _MPHY_VER, 2); 707 708 static struct attribute *ufs_sysfs_interconnect_descriptor[] = { 709 &dev_attr_unipro_version.attr, 710 &dev_attr_mphy_version.attr, 711 NULL, 712 }; 713 714 static const struct attribute_group ufs_sysfs_interconnect_descriptor_group = { 715 .name = "interconnect_descriptor", 716 .attrs = ufs_sysfs_interconnect_descriptor, 717 }; 718 719 #define UFS_GEOMETRY_DESC_PARAM(_name, _uname, _size) \ 720 UFS_DESC_PARAM(_name, _uname, GEOMETRY, _size) 721 722 UFS_GEOMETRY_DESC_PARAM(raw_device_capacity, _DEV_CAP, 8); 723 UFS_GEOMETRY_DESC_PARAM(max_number_of_luns, _MAX_NUM_LUN, 1); 724 UFS_GEOMETRY_DESC_PARAM(segment_size, _SEG_SIZE, 4); 725 UFS_GEOMETRY_DESC_PARAM(allocation_unit_size, _ALLOC_UNIT_SIZE, 1); 726 UFS_GEOMETRY_DESC_PARAM(min_addressable_block_size, _MIN_BLK_SIZE, 1); 727 UFS_GEOMETRY_DESC_PARAM(optimal_read_block_size, _OPT_RD_BLK_SIZE, 1); 728 UFS_GEOMETRY_DESC_PARAM(optimal_write_block_size, _OPT_WR_BLK_SIZE, 1); 729 UFS_GEOMETRY_DESC_PARAM(max_in_buffer_size, _MAX_IN_BUF_SIZE, 1); 730 UFS_GEOMETRY_DESC_PARAM(max_out_buffer_size, _MAX_OUT_BUF_SIZE, 1); 731 UFS_GEOMETRY_DESC_PARAM(rpmb_rw_size, _RPMB_RW_SIZE, 1); 732 UFS_GEOMETRY_DESC_PARAM(dyn_capacity_resource_policy, _DYN_CAP_RSRC_PLC, 1); 733 UFS_GEOMETRY_DESC_PARAM(data_ordering, _DATA_ORDER, 1); 734 UFS_GEOMETRY_DESC_PARAM(max_number_of_contexts, _MAX_NUM_CTX, 1); 735 UFS_GEOMETRY_DESC_PARAM(sys_data_tag_unit_size, _TAG_UNIT_SIZE, 1); 736 UFS_GEOMETRY_DESC_PARAM(sys_data_tag_resource_size, _TAG_RSRC_SIZE, 1); 737 UFS_GEOMETRY_DESC_PARAM(secure_removal_types, _SEC_RM_TYPES, 1); 738 UFS_GEOMETRY_DESC_PARAM(memory_types, _MEM_TYPES, 2); 739 UFS_GEOMETRY_DESC_PARAM(sys_code_memory_max_alloc_units, 740 _SCM_MAX_NUM_UNITS, 4); 741 UFS_GEOMETRY_DESC_PARAM(sys_code_memory_capacity_adjustment_factor, 742 _SCM_CAP_ADJ_FCTR, 2); 743 UFS_GEOMETRY_DESC_PARAM(non_persist_memory_max_alloc_units, 744 _NPM_MAX_NUM_UNITS, 4); 745 UFS_GEOMETRY_DESC_PARAM(non_persist_memory_capacity_adjustment_factor, 746 _NPM_CAP_ADJ_FCTR, 2); 747 UFS_GEOMETRY_DESC_PARAM(enh1_memory_max_alloc_units, 748 _ENM1_MAX_NUM_UNITS, 4); 749 UFS_GEOMETRY_DESC_PARAM(enh1_memory_capacity_adjustment_factor, 750 _ENM1_CAP_ADJ_FCTR, 2); 751 UFS_GEOMETRY_DESC_PARAM(enh2_memory_max_alloc_units, 752 _ENM2_MAX_NUM_UNITS, 4); 753 UFS_GEOMETRY_DESC_PARAM(enh2_memory_capacity_adjustment_factor, 754 _ENM2_CAP_ADJ_FCTR, 2); 755 UFS_GEOMETRY_DESC_PARAM(enh3_memory_max_alloc_units, 756 _ENM3_MAX_NUM_UNITS, 4); 757 UFS_GEOMETRY_DESC_PARAM(enh3_memory_capacity_adjustment_factor, 758 _ENM3_CAP_ADJ_FCTR, 2); 759 UFS_GEOMETRY_DESC_PARAM(enh4_memory_max_alloc_units, 760 _ENM4_MAX_NUM_UNITS, 4); 761 UFS_GEOMETRY_DESC_PARAM(enh4_memory_capacity_adjustment_factor, 762 _ENM4_CAP_ADJ_FCTR, 2); 763 UFS_GEOMETRY_DESC_PARAM(hpb_region_size, _HPB_REGION_SIZE, 1); 764 UFS_GEOMETRY_DESC_PARAM(hpb_number_lu, _HPB_NUMBER_LU, 1); 765 UFS_GEOMETRY_DESC_PARAM(hpb_subregion_size, _HPB_SUBREGION_SIZE, 1); 766 UFS_GEOMETRY_DESC_PARAM(hpb_max_active_regions, _HPB_MAX_ACTIVE_REGS, 2); 767 UFS_GEOMETRY_DESC_PARAM(wb_max_alloc_units, _WB_MAX_ALLOC_UNITS, 4); 768 UFS_GEOMETRY_DESC_PARAM(wb_max_wb_luns, _WB_MAX_WB_LUNS, 1); 769 UFS_GEOMETRY_DESC_PARAM(wb_buff_cap_adj, _WB_BUFF_CAP_ADJ, 1); 770 UFS_GEOMETRY_DESC_PARAM(wb_sup_red_type, _WB_SUP_RED_TYPE, 1); 771 UFS_GEOMETRY_DESC_PARAM(wb_sup_wb_type, _WB_SUP_WB_TYPE, 1); 772 773 774 static struct attribute *ufs_sysfs_geometry_descriptor[] = { 775 &dev_attr_raw_device_capacity.attr, 776 &dev_attr_max_number_of_luns.attr, 777 &dev_attr_segment_size.attr, 778 &dev_attr_allocation_unit_size.attr, 779 &dev_attr_min_addressable_block_size.attr, 780 &dev_attr_optimal_read_block_size.attr, 781 &dev_attr_optimal_write_block_size.attr, 782 &dev_attr_max_in_buffer_size.attr, 783 &dev_attr_max_out_buffer_size.attr, 784 &dev_attr_rpmb_rw_size.attr, 785 &dev_attr_dyn_capacity_resource_policy.attr, 786 &dev_attr_data_ordering.attr, 787 &dev_attr_max_number_of_contexts.attr, 788 &dev_attr_sys_data_tag_unit_size.attr, 789 &dev_attr_sys_data_tag_resource_size.attr, 790 &dev_attr_secure_removal_types.attr, 791 &dev_attr_memory_types.attr, 792 &dev_attr_sys_code_memory_max_alloc_units.attr, 793 &dev_attr_sys_code_memory_capacity_adjustment_factor.attr, 794 &dev_attr_non_persist_memory_max_alloc_units.attr, 795 &dev_attr_non_persist_memory_capacity_adjustment_factor.attr, 796 &dev_attr_enh1_memory_max_alloc_units.attr, 797 &dev_attr_enh1_memory_capacity_adjustment_factor.attr, 798 &dev_attr_enh2_memory_max_alloc_units.attr, 799 &dev_attr_enh2_memory_capacity_adjustment_factor.attr, 800 &dev_attr_enh3_memory_max_alloc_units.attr, 801 &dev_attr_enh3_memory_capacity_adjustment_factor.attr, 802 &dev_attr_enh4_memory_max_alloc_units.attr, 803 &dev_attr_enh4_memory_capacity_adjustment_factor.attr, 804 &dev_attr_hpb_region_size.attr, 805 &dev_attr_hpb_number_lu.attr, 806 &dev_attr_hpb_subregion_size.attr, 807 &dev_attr_hpb_max_active_regions.attr, 808 &dev_attr_wb_max_alloc_units.attr, 809 &dev_attr_wb_max_wb_luns.attr, 810 &dev_attr_wb_buff_cap_adj.attr, 811 &dev_attr_wb_sup_red_type.attr, 812 &dev_attr_wb_sup_wb_type.attr, 813 NULL, 814 }; 815 816 static const struct attribute_group ufs_sysfs_geometry_descriptor_group = { 817 .name = "geometry_descriptor", 818 .attrs = ufs_sysfs_geometry_descriptor, 819 }; 820 821 #define UFS_HEALTH_DESC_PARAM(_name, _uname, _size) \ 822 UFS_DESC_PARAM(_name, _uname, HEALTH, _size) 823 824 UFS_HEALTH_DESC_PARAM(eol_info, _EOL_INFO, 1); 825 UFS_HEALTH_DESC_PARAM(life_time_estimation_a, _LIFE_TIME_EST_A, 1); 826 UFS_HEALTH_DESC_PARAM(life_time_estimation_b, _LIFE_TIME_EST_B, 1); 827 828 static struct attribute *ufs_sysfs_health_descriptor[] = { 829 &dev_attr_eol_info.attr, 830 &dev_attr_life_time_estimation_a.attr, 831 &dev_attr_life_time_estimation_b.attr, 832 NULL, 833 }; 834 835 static const struct attribute_group ufs_sysfs_health_descriptor_group = { 836 .name = "health_descriptor", 837 .attrs = ufs_sysfs_health_descriptor, 838 }; 839 840 #define UFS_POWER_DESC_PARAM(_name, _uname, _index) \ 841 static ssize_t _name##_index##_show(struct device *dev, \ 842 struct device_attribute *attr, char *buf) \ 843 { \ 844 struct ufs_hba *hba = dev_get_drvdata(dev); \ 845 return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, \ 846 PWR_DESC##_uname##_0 + _index * 2, buf, 2); \ 847 } \ 848 static DEVICE_ATTR_RO(_name##_index) 849 850 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 0); 851 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 1); 852 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 2); 853 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 3); 854 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 4); 855 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 5); 856 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 6); 857 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 7); 858 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 8); 859 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 9); 860 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 10); 861 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 11); 862 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 12); 863 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 13); 864 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 14); 865 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 15); 866 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 0); 867 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 1); 868 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 2); 869 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 3); 870 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 4); 871 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 5); 872 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 6); 873 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 7); 874 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 8); 875 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 9); 876 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 10); 877 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 11); 878 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 12); 879 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 13); 880 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 14); 881 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 15); 882 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 0); 883 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 1); 884 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 2); 885 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 3); 886 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 4); 887 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 5); 888 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 6); 889 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 7); 890 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 8); 891 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 9); 892 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 10); 893 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 11); 894 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 12); 895 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 13); 896 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 14); 897 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 15); 898 899 static struct attribute *ufs_sysfs_power_descriptor[] = { 900 &dev_attr_active_icc_levels_vcc0.attr, 901 &dev_attr_active_icc_levels_vcc1.attr, 902 &dev_attr_active_icc_levels_vcc2.attr, 903 &dev_attr_active_icc_levels_vcc3.attr, 904 &dev_attr_active_icc_levels_vcc4.attr, 905 &dev_attr_active_icc_levels_vcc5.attr, 906 &dev_attr_active_icc_levels_vcc6.attr, 907 &dev_attr_active_icc_levels_vcc7.attr, 908 &dev_attr_active_icc_levels_vcc8.attr, 909 &dev_attr_active_icc_levels_vcc9.attr, 910 &dev_attr_active_icc_levels_vcc10.attr, 911 &dev_attr_active_icc_levels_vcc11.attr, 912 &dev_attr_active_icc_levels_vcc12.attr, 913 &dev_attr_active_icc_levels_vcc13.attr, 914 &dev_attr_active_icc_levels_vcc14.attr, 915 &dev_attr_active_icc_levels_vcc15.attr, 916 &dev_attr_active_icc_levels_vccq0.attr, 917 &dev_attr_active_icc_levels_vccq1.attr, 918 &dev_attr_active_icc_levels_vccq2.attr, 919 &dev_attr_active_icc_levels_vccq3.attr, 920 &dev_attr_active_icc_levels_vccq4.attr, 921 &dev_attr_active_icc_levels_vccq5.attr, 922 &dev_attr_active_icc_levels_vccq6.attr, 923 &dev_attr_active_icc_levels_vccq7.attr, 924 &dev_attr_active_icc_levels_vccq8.attr, 925 &dev_attr_active_icc_levels_vccq9.attr, 926 &dev_attr_active_icc_levels_vccq10.attr, 927 &dev_attr_active_icc_levels_vccq11.attr, 928 &dev_attr_active_icc_levels_vccq12.attr, 929 &dev_attr_active_icc_levels_vccq13.attr, 930 &dev_attr_active_icc_levels_vccq14.attr, 931 &dev_attr_active_icc_levels_vccq15.attr, 932 &dev_attr_active_icc_levels_vccq20.attr, 933 &dev_attr_active_icc_levels_vccq21.attr, 934 &dev_attr_active_icc_levels_vccq22.attr, 935 &dev_attr_active_icc_levels_vccq23.attr, 936 &dev_attr_active_icc_levels_vccq24.attr, 937 &dev_attr_active_icc_levels_vccq25.attr, 938 &dev_attr_active_icc_levels_vccq26.attr, 939 &dev_attr_active_icc_levels_vccq27.attr, 940 &dev_attr_active_icc_levels_vccq28.attr, 941 &dev_attr_active_icc_levels_vccq29.attr, 942 &dev_attr_active_icc_levels_vccq210.attr, 943 &dev_attr_active_icc_levels_vccq211.attr, 944 &dev_attr_active_icc_levels_vccq212.attr, 945 &dev_attr_active_icc_levels_vccq213.attr, 946 &dev_attr_active_icc_levels_vccq214.attr, 947 &dev_attr_active_icc_levels_vccq215.attr, 948 NULL, 949 }; 950 951 static const struct attribute_group ufs_sysfs_power_descriptor_group = { 952 .name = "power_descriptor", 953 .attrs = ufs_sysfs_power_descriptor, 954 }; 955 956 #define UFS_STRING_DESCRIPTOR(_name, _pname) \ 957 static ssize_t _name##_show(struct device *dev, \ 958 struct device_attribute *attr, char *buf) \ 959 { \ 960 u8 index; \ 961 struct ufs_hba *hba = dev_get_drvdata(dev); \ 962 int ret; \ 963 int desc_len = QUERY_DESC_MAX_SIZE; \ 964 u8 *desc_buf; \ 965 \ 966 down(&hba->host_sem); \ 967 if (!ufshcd_is_user_access_allowed(hba)) { \ 968 up(&hba->host_sem); \ 969 return -EBUSY; \ 970 } \ 971 desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_ATOMIC); \ 972 if (!desc_buf) { \ 973 up(&hba->host_sem); \ 974 return -ENOMEM; \ 975 } \ 976 ufshcd_rpm_get_sync(hba); \ 977 ret = ufshcd_query_descriptor_retry(hba, \ 978 UPIU_QUERY_OPCODE_READ_DESC, QUERY_DESC_IDN_DEVICE, \ 979 0, 0, desc_buf, &desc_len); \ 980 if (ret) { \ 981 ret = -EINVAL; \ 982 goto out; \ 983 } \ 984 index = desc_buf[DEVICE_DESC_PARAM##_pname]; \ 985 kfree(desc_buf); \ 986 desc_buf = NULL; \ 987 ret = ufshcd_read_string_desc(hba, index, &desc_buf, \ 988 SD_ASCII_STD); \ 989 if (ret < 0) \ 990 goto out; \ 991 ret = sysfs_emit(buf, "%s\n", desc_buf); \ 992 out: \ 993 ufshcd_rpm_put_sync(hba); \ 994 kfree(desc_buf); \ 995 up(&hba->host_sem); \ 996 return ret; \ 997 } \ 998 static DEVICE_ATTR_RO(_name) 999 1000 UFS_STRING_DESCRIPTOR(manufacturer_name, _MANF_NAME); 1001 UFS_STRING_DESCRIPTOR(product_name, _PRDCT_NAME); 1002 UFS_STRING_DESCRIPTOR(oem_id, _OEM_ID); 1003 UFS_STRING_DESCRIPTOR(serial_number, _SN); 1004 UFS_STRING_DESCRIPTOR(product_revision, _PRDCT_REV); 1005 1006 static struct attribute *ufs_sysfs_string_descriptors[] = { 1007 &dev_attr_manufacturer_name.attr, 1008 &dev_attr_product_name.attr, 1009 &dev_attr_oem_id.attr, 1010 &dev_attr_serial_number.attr, 1011 &dev_attr_product_revision.attr, 1012 NULL, 1013 }; 1014 1015 static const struct attribute_group ufs_sysfs_string_descriptors_group = { 1016 .name = "string_descriptors", 1017 .attrs = ufs_sysfs_string_descriptors, 1018 }; 1019 1020 static inline bool ufshcd_is_wb_flags(enum flag_idn idn) 1021 { 1022 return idn >= QUERY_FLAG_IDN_WB_EN && 1023 idn <= QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8; 1024 } 1025 1026 #define UFS_FLAG(_name, _uname) \ 1027 static ssize_t _name##_show(struct device *dev, \ 1028 struct device_attribute *attr, char *buf) \ 1029 { \ 1030 bool flag; \ 1031 u8 index = 0; \ 1032 int ret; \ 1033 struct ufs_hba *hba = dev_get_drvdata(dev); \ 1034 \ 1035 down(&hba->host_sem); \ 1036 if (!ufshcd_is_user_access_allowed(hba)) { \ 1037 up(&hba->host_sem); \ 1038 return -EBUSY; \ 1039 } \ 1040 if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname)) \ 1041 index = ufshcd_wb_get_query_index(hba); \ 1042 ufshcd_rpm_get_sync(hba); \ 1043 ret = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG, \ 1044 QUERY_FLAG_IDN##_uname, index, &flag); \ 1045 ufshcd_rpm_put_sync(hba); \ 1046 if (ret) { \ 1047 ret = -EINVAL; \ 1048 goto out; \ 1049 } \ 1050 ret = sysfs_emit(buf, "%s\n", flag ? "true" : "false"); \ 1051 out: \ 1052 up(&hba->host_sem); \ 1053 return ret; \ 1054 } \ 1055 static DEVICE_ATTR_RO(_name) 1056 1057 UFS_FLAG(device_init, _FDEVICEINIT); 1058 UFS_FLAG(permanent_wpe, _PERMANENT_WPE); 1059 UFS_FLAG(power_on_wpe, _PWR_ON_WPE); 1060 UFS_FLAG(bkops_enable, _BKOPS_EN); 1061 UFS_FLAG(life_span_mode_enable, _LIFE_SPAN_MODE_ENABLE); 1062 UFS_FLAG(phy_resource_removal, _FPHYRESOURCEREMOVAL); 1063 UFS_FLAG(busy_rtc, _BUSY_RTC); 1064 UFS_FLAG(disable_fw_update, _PERMANENTLY_DISABLE_FW_UPDATE); 1065 UFS_FLAG(wb_enable, _WB_EN); 1066 UFS_FLAG(wb_flush_en, _WB_BUFF_FLUSH_EN); 1067 UFS_FLAG(wb_flush_during_h8, _WB_BUFF_FLUSH_DURING_HIBERN8); 1068 UFS_FLAG(hpb_enable, _HPB_EN); 1069 1070 static struct attribute *ufs_sysfs_device_flags[] = { 1071 &dev_attr_device_init.attr, 1072 &dev_attr_permanent_wpe.attr, 1073 &dev_attr_power_on_wpe.attr, 1074 &dev_attr_bkops_enable.attr, 1075 &dev_attr_life_span_mode_enable.attr, 1076 &dev_attr_phy_resource_removal.attr, 1077 &dev_attr_busy_rtc.attr, 1078 &dev_attr_disable_fw_update.attr, 1079 &dev_attr_wb_enable.attr, 1080 &dev_attr_wb_flush_en.attr, 1081 &dev_attr_wb_flush_during_h8.attr, 1082 &dev_attr_hpb_enable.attr, 1083 NULL, 1084 }; 1085 1086 static const struct attribute_group ufs_sysfs_flags_group = { 1087 .name = "flags", 1088 .attrs = ufs_sysfs_device_flags, 1089 }; 1090 1091 static inline bool ufshcd_is_wb_attrs(enum attr_idn idn) 1092 { 1093 return idn >= QUERY_ATTR_IDN_WB_FLUSH_STATUS && 1094 idn <= QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE; 1095 } 1096 1097 #define UFS_ATTRIBUTE(_name, _uname) \ 1098 static ssize_t _name##_show(struct device *dev, \ 1099 struct device_attribute *attr, char *buf) \ 1100 { \ 1101 struct ufs_hba *hba = dev_get_drvdata(dev); \ 1102 u32 value; \ 1103 int ret; \ 1104 u8 index = 0; \ 1105 \ 1106 down(&hba->host_sem); \ 1107 if (!ufshcd_is_user_access_allowed(hba)) { \ 1108 up(&hba->host_sem); \ 1109 return -EBUSY; \ 1110 } \ 1111 if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname)) \ 1112 index = ufshcd_wb_get_query_index(hba); \ 1113 ufshcd_rpm_get_sync(hba); \ 1114 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, \ 1115 QUERY_ATTR_IDN##_uname, index, 0, &value); \ 1116 ufshcd_rpm_put_sync(hba); \ 1117 if (ret) { \ 1118 ret = -EINVAL; \ 1119 goto out; \ 1120 } \ 1121 ret = sysfs_emit(buf, "0x%08X\n", value); \ 1122 out: \ 1123 up(&hba->host_sem); \ 1124 return ret; \ 1125 } \ 1126 static DEVICE_ATTR_RO(_name) 1127 1128 UFS_ATTRIBUTE(boot_lun_enabled, _BOOT_LU_EN); 1129 UFS_ATTRIBUTE(max_data_size_hpb_single_cmd, _MAX_HPB_SINGLE_CMD); 1130 UFS_ATTRIBUTE(current_power_mode, _POWER_MODE); 1131 UFS_ATTRIBUTE(active_icc_level, _ACTIVE_ICC_LVL); 1132 UFS_ATTRIBUTE(ooo_data_enabled, _OOO_DATA_EN); 1133 UFS_ATTRIBUTE(bkops_status, _BKOPS_STATUS); 1134 UFS_ATTRIBUTE(purge_status, _PURGE_STATUS); 1135 UFS_ATTRIBUTE(max_data_in_size, _MAX_DATA_IN); 1136 UFS_ATTRIBUTE(max_data_out_size, _MAX_DATA_OUT); 1137 UFS_ATTRIBUTE(reference_clock_frequency, _REF_CLK_FREQ); 1138 UFS_ATTRIBUTE(configuration_descriptor_lock, _CONF_DESC_LOCK); 1139 UFS_ATTRIBUTE(max_number_of_rtt, _MAX_NUM_OF_RTT); 1140 UFS_ATTRIBUTE(exception_event_control, _EE_CONTROL); 1141 UFS_ATTRIBUTE(exception_event_status, _EE_STATUS); 1142 UFS_ATTRIBUTE(ffu_status, _FFU_STATUS); 1143 UFS_ATTRIBUTE(psa_state, _PSA_STATE); 1144 UFS_ATTRIBUTE(psa_data_size, _PSA_DATA_SIZE); 1145 UFS_ATTRIBUTE(wb_flush_status, _WB_FLUSH_STATUS); 1146 UFS_ATTRIBUTE(wb_avail_buf, _AVAIL_WB_BUFF_SIZE); 1147 UFS_ATTRIBUTE(wb_life_time_est, _WB_BUFF_LIFE_TIME_EST); 1148 UFS_ATTRIBUTE(wb_cur_buf, _CURR_WB_BUFF_SIZE); 1149 1150 1151 static struct attribute *ufs_sysfs_attributes[] = { 1152 &dev_attr_boot_lun_enabled.attr, 1153 &dev_attr_max_data_size_hpb_single_cmd.attr, 1154 &dev_attr_current_power_mode.attr, 1155 &dev_attr_active_icc_level.attr, 1156 &dev_attr_ooo_data_enabled.attr, 1157 &dev_attr_bkops_status.attr, 1158 &dev_attr_purge_status.attr, 1159 &dev_attr_max_data_in_size.attr, 1160 &dev_attr_max_data_out_size.attr, 1161 &dev_attr_reference_clock_frequency.attr, 1162 &dev_attr_configuration_descriptor_lock.attr, 1163 &dev_attr_max_number_of_rtt.attr, 1164 &dev_attr_exception_event_control.attr, 1165 &dev_attr_exception_event_status.attr, 1166 &dev_attr_ffu_status.attr, 1167 &dev_attr_psa_state.attr, 1168 &dev_attr_psa_data_size.attr, 1169 &dev_attr_wb_flush_status.attr, 1170 &dev_attr_wb_avail_buf.attr, 1171 &dev_attr_wb_life_time_est.attr, 1172 &dev_attr_wb_cur_buf.attr, 1173 NULL, 1174 }; 1175 1176 static const struct attribute_group ufs_sysfs_attributes_group = { 1177 .name = "attributes", 1178 .attrs = ufs_sysfs_attributes, 1179 }; 1180 1181 static const struct attribute_group *ufs_sysfs_groups[] = { 1182 &ufs_sysfs_default_group, 1183 &ufs_sysfs_monitor_group, 1184 &ufs_sysfs_device_descriptor_group, 1185 &ufs_sysfs_interconnect_descriptor_group, 1186 &ufs_sysfs_geometry_descriptor_group, 1187 &ufs_sysfs_health_descriptor_group, 1188 &ufs_sysfs_power_descriptor_group, 1189 &ufs_sysfs_string_descriptors_group, 1190 &ufs_sysfs_flags_group, 1191 &ufs_sysfs_attributes_group, 1192 NULL, 1193 }; 1194 1195 #define UFS_LUN_DESC_PARAM(_pname, _puname, _duname, _size) \ 1196 static ssize_t _pname##_show(struct device *dev, \ 1197 struct device_attribute *attr, char *buf) \ 1198 { \ 1199 struct scsi_device *sdev = to_scsi_device(dev); \ 1200 struct ufs_hba *hba = shost_priv(sdev->host); \ 1201 u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun); \ 1202 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun, \ 1203 _duname##_DESC_PARAM##_puname)) \ 1204 return -EINVAL; \ 1205 return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname, \ 1206 lun, _duname##_DESC_PARAM##_puname, buf, _size); \ 1207 } \ 1208 static DEVICE_ATTR_RO(_pname) 1209 1210 #define UFS_UNIT_DESC_PARAM(_name, _uname, _size) \ 1211 UFS_LUN_DESC_PARAM(_name, _uname, UNIT, _size) 1212 1213 UFS_UNIT_DESC_PARAM(lu_enable, _LU_ENABLE, 1); 1214 UFS_UNIT_DESC_PARAM(boot_lun_id, _BOOT_LUN_ID, 1); 1215 UFS_UNIT_DESC_PARAM(lun_write_protect, _LU_WR_PROTECT, 1); 1216 UFS_UNIT_DESC_PARAM(lun_queue_depth, _LU_Q_DEPTH, 1); 1217 UFS_UNIT_DESC_PARAM(psa_sensitive, _PSA_SENSITIVE, 1); 1218 UFS_UNIT_DESC_PARAM(lun_memory_type, _MEM_TYPE, 1); 1219 UFS_UNIT_DESC_PARAM(data_reliability, _DATA_RELIABILITY, 1); 1220 UFS_UNIT_DESC_PARAM(logical_block_size, _LOGICAL_BLK_SIZE, 1); 1221 UFS_UNIT_DESC_PARAM(logical_block_count, _LOGICAL_BLK_COUNT, 8); 1222 UFS_UNIT_DESC_PARAM(erase_block_size, _ERASE_BLK_SIZE, 4); 1223 UFS_UNIT_DESC_PARAM(provisioning_type, _PROVISIONING_TYPE, 1); 1224 UFS_UNIT_DESC_PARAM(physical_memory_resourse_count, _PHY_MEM_RSRC_CNT, 8); 1225 UFS_UNIT_DESC_PARAM(context_capabilities, _CTX_CAPABILITIES, 2); 1226 UFS_UNIT_DESC_PARAM(large_unit_granularity, _LARGE_UNIT_SIZE_M1, 1); 1227 UFS_UNIT_DESC_PARAM(hpb_lu_max_active_regions, _HPB_LU_MAX_ACTIVE_RGNS, 2); 1228 UFS_UNIT_DESC_PARAM(hpb_pinned_region_start_offset, _HPB_PIN_RGN_START_OFF, 2); 1229 UFS_UNIT_DESC_PARAM(hpb_number_pinned_regions, _HPB_NUM_PIN_RGNS, 2); 1230 UFS_UNIT_DESC_PARAM(wb_buf_alloc_units, _WB_BUF_ALLOC_UNITS, 4); 1231 1232 static struct attribute *ufs_sysfs_unit_descriptor[] = { 1233 &dev_attr_lu_enable.attr, 1234 &dev_attr_boot_lun_id.attr, 1235 &dev_attr_lun_write_protect.attr, 1236 &dev_attr_lun_queue_depth.attr, 1237 &dev_attr_psa_sensitive.attr, 1238 &dev_attr_lun_memory_type.attr, 1239 &dev_attr_data_reliability.attr, 1240 &dev_attr_logical_block_size.attr, 1241 &dev_attr_logical_block_count.attr, 1242 &dev_attr_erase_block_size.attr, 1243 &dev_attr_provisioning_type.attr, 1244 &dev_attr_physical_memory_resourse_count.attr, 1245 &dev_attr_context_capabilities.attr, 1246 &dev_attr_large_unit_granularity.attr, 1247 &dev_attr_hpb_lu_max_active_regions.attr, 1248 &dev_attr_hpb_pinned_region_start_offset.attr, 1249 &dev_attr_hpb_number_pinned_regions.attr, 1250 &dev_attr_wb_buf_alloc_units.attr, 1251 NULL, 1252 }; 1253 1254 const struct attribute_group ufs_sysfs_unit_descriptor_group = { 1255 .name = "unit_descriptor", 1256 .attrs = ufs_sysfs_unit_descriptor, 1257 }; 1258 1259 static ssize_t dyn_cap_needed_attribute_show(struct device *dev, 1260 struct device_attribute *attr, char *buf) 1261 { 1262 u32 value; 1263 struct scsi_device *sdev = to_scsi_device(dev); 1264 struct ufs_hba *hba = shost_priv(sdev->host); 1265 u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun); 1266 int ret; 1267 1268 down(&hba->host_sem); 1269 if (!ufshcd_is_user_access_allowed(hba)) { 1270 ret = -EBUSY; 1271 goto out; 1272 } 1273 1274 ufshcd_rpm_get_sync(hba); 1275 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, 1276 QUERY_ATTR_IDN_DYN_CAP_NEEDED, lun, 0, &value); 1277 ufshcd_rpm_put_sync(hba); 1278 if (ret) { 1279 ret = -EINVAL; 1280 goto out; 1281 } 1282 1283 ret = sysfs_emit(buf, "0x%08X\n", value); 1284 1285 out: 1286 up(&hba->host_sem); 1287 return ret; 1288 } 1289 static DEVICE_ATTR_RO(dyn_cap_needed_attribute); 1290 1291 static struct attribute *ufs_sysfs_lun_attributes[] = { 1292 &dev_attr_dyn_cap_needed_attribute.attr, 1293 NULL, 1294 }; 1295 1296 const struct attribute_group ufs_sysfs_lun_attributes_group = { 1297 .attrs = ufs_sysfs_lun_attributes, 1298 }; 1299 1300 void ufs_sysfs_add_nodes(struct device *dev) 1301 { 1302 int ret; 1303 1304 ret = sysfs_create_groups(&dev->kobj, ufs_sysfs_groups); 1305 if (ret) 1306 dev_err(dev, 1307 "%s: sysfs groups creation failed (err = %d)\n", 1308 __func__, ret); 1309 } 1310 1311 void ufs_sysfs_remove_nodes(struct device *dev) 1312 { 1313 sysfs_remove_groups(&dev->kobj, ufs_sysfs_groups); 1314 } 1315