1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright IBM Corp 2019 3 4 #include <linux/device.h> 5 #include <linux/hwmon.h> 6 #include <linux/hwmon-sysfs.h> 7 #include <linux/jiffies.h> 8 #include <linux/kernel.h> 9 #include <linux/math64.h> 10 #include <linux/mutex.h> 11 #include <linux/sysfs.h> 12 #include <asm/unaligned.h> 13 14 #include "common.h" 15 16 #define EXTN_FLAG_SENSOR_ID BIT(7) 17 18 #define OCC_ERROR_COUNT_THRESHOLD 2 /* required by OCC spec */ 19 20 #define OCC_STATE_SAFE 4 21 #define OCC_SAFE_TIMEOUT msecs_to_jiffies(60000) /* 1 min */ 22 23 #define OCC_UPDATE_FREQUENCY msecs_to_jiffies(1000) 24 25 #define OCC_TEMP_SENSOR_FAULT 0xFF 26 27 #define OCC_FRU_TYPE_VRM 3 28 29 /* OCC sensor type and version definitions */ 30 31 struct temp_sensor_1 { 32 u16 sensor_id; 33 u16 value; 34 } __packed; 35 36 struct temp_sensor_2 { 37 u32 sensor_id; 38 u8 fru_type; 39 u8 value; 40 } __packed; 41 42 struct freq_sensor_1 { 43 u16 sensor_id; 44 u16 value; 45 } __packed; 46 47 struct freq_sensor_2 { 48 u32 sensor_id; 49 u16 value; 50 } __packed; 51 52 struct power_sensor_1 { 53 u16 sensor_id; 54 u32 update_tag; 55 u32 accumulator; 56 u16 value; 57 } __packed; 58 59 struct power_sensor_2 { 60 u32 sensor_id; 61 u8 function_id; 62 u8 apss_channel; 63 u16 reserved; 64 u32 update_tag; 65 u64 accumulator; 66 u16 value; 67 } __packed; 68 69 struct power_sensor_data { 70 u16 value; 71 u32 update_tag; 72 u64 accumulator; 73 } __packed; 74 75 struct power_sensor_data_and_time { 76 u16 update_time; 77 u16 value; 78 u32 update_tag; 79 u64 accumulator; 80 } __packed; 81 82 struct power_sensor_a0 { 83 u32 sensor_id; 84 struct power_sensor_data_and_time system; 85 u32 reserved; 86 struct power_sensor_data_and_time proc; 87 struct power_sensor_data vdd; 88 struct power_sensor_data vdn; 89 } __packed; 90 91 struct caps_sensor_2 { 92 u16 cap; 93 u16 system_power; 94 u16 n_cap; 95 u16 max; 96 u16 min; 97 u16 user; 98 u8 user_source; 99 } __packed; 100 101 struct caps_sensor_3 { 102 u16 cap; 103 u16 system_power; 104 u16 n_cap; 105 u16 max; 106 u16 hard_min; 107 u16 soft_min; 108 u16 user; 109 u8 user_source; 110 } __packed; 111 112 struct extended_sensor { 113 union { 114 u8 name[4]; 115 u32 sensor_id; 116 }; 117 u8 flags; 118 u8 reserved; 119 u8 data[6]; 120 } __packed; 121 122 static int occ_poll(struct occ *occ) 123 { 124 int rc; 125 u16 checksum = occ->poll_cmd_data + 1; 126 u8 cmd[8]; 127 struct occ_poll_response_header *header; 128 129 /* big endian */ 130 cmd[0] = 0; /* sequence number */ 131 cmd[1] = 0; /* cmd type */ 132 cmd[2] = 0; /* data length msb */ 133 cmd[3] = 1; /* data length lsb */ 134 cmd[4] = occ->poll_cmd_data; /* data */ 135 cmd[5] = checksum >> 8; /* checksum msb */ 136 cmd[6] = checksum & 0xFF; /* checksum lsb */ 137 cmd[7] = 0; 138 139 /* mutex should already be locked if necessary */ 140 rc = occ->send_cmd(occ, cmd); 141 if (rc) { 142 if (occ->error_count++ > OCC_ERROR_COUNT_THRESHOLD) 143 occ->error = rc; 144 145 goto done; 146 } 147 148 /* clear error since communication was successful */ 149 occ->error_count = 0; 150 occ->error = 0; 151 152 /* check for safe state */ 153 header = (struct occ_poll_response_header *)occ->resp.data; 154 if (header->occ_state == OCC_STATE_SAFE) { 155 if (occ->last_safe) { 156 if (time_after(jiffies, 157 occ->last_safe + OCC_SAFE_TIMEOUT)) 158 occ->error = -EHOSTDOWN; 159 } else { 160 occ->last_safe = jiffies; 161 } 162 } else { 163 occ->last_safe = 0; 164 } 165 166 done: 167 occ_sysfs_poll_done(occ); 168 return rc; 169 } 170 171 static int occ_set_user_power_cap(struct occ *occ, u16 user_power_cap) 172 { 173 int rc; 174 u8 cmd[8]; 175 u16 checksum = 0x24; 176 __be16 user_power_cap_be = cpu_to_be16(user_power_cap); 177 178 cmd[0] = 0; 179 cmd[1] = 0x22; 180 cmd[2] = 0; 181 cmd[3] = 2; 182 183 memcpy(&cmd[4], &user_power_cap_be, 2); 184 185 checksum += cmd[4] + cmd[5]; 186 cmd[6] = checksum >> 8; 187 cmd[7] = checksum & 0xFF; 188 189 rc = mutex_lock_interruptible(&occ->lock); 190 if (rc) 191 return rc; 192 193 rc = occ->send_cmd(occ, cmd); 194 195 mutex_unlock(&occ->lock); 196 197 return rc; 198 } 199 200 int occ_update_response(struct occ *occ) 201 { 202 int rc = mutex_lock_interruptible(&occ->lock); 203 204 if (rc) 205 return rc; 206 207 /* limit the maximum rate of polling the OCC */ 208 if (time_after(jiffies, occ->last_update + OCC_UPDATE_FREQUENCY)) { 209 rc = occ_poll(occ); 210 occ->last_update = jiffies; 211 } 212 213 mutex_unlock(&occ->lock); 214 return rc; 215 } 216 217 static ssize_t occ_show_temp_1(struct device *dev, 218 struct device_attribute *attr, char *buf) 219 { 220 int rc; 221 u32 val = 0; 222 struct temp_sensor_1 *temp; 223 struct occ *occ = dev_get_drvdata(dev); 224 struct occ_sensors *sensors = &occ->sensors; 225 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 226 227 rc = occ_update_response(occ); 228 if (rc) 229 return rc; 230 231 temp = ((struct temp_sensor_1 *)sensors->temp.data) + sattr->index; 232 233 switch (sattr->nr) { 234 case 0: 235 val = get_unaligned_be16(&temp->sensor_id); 236 break; 237 case 1: 238 val = get_unaligned_be16(&temp->value) * 1000; 239 break; 240 default: 241 return -EINVAL; 242 } 243 244 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val); 245 } 246 247 static ssize_t occ_show_temp_2(struct device *dev, 248 struct device_attribute *attr, char *buf) 249 { 250 int rc; 251 u32 val = 0; 252 struct temp_sensor_2 *temp; 253 struct occ *occ = dev_get_drvdata(dev); 254 struct occ_sensors *sensors = &occ->sensors; 255 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 256 257 rc = occ_update_response(occ); 258 if (rc) 259 return rc; 260 261 temp = ((struct temp_sensor_2 *)sensors->temp.data) + sattr->index; 262 263 switch (sattr->nr) { 264 case 0: 265 val = get_unaligned_be32(&temp->sensor_id); 266 break; 267 case 1: 268 val = temp->value; 269 if (val == OCC_TEMP_SENSOR_FAULT) 270 return -EREMOTEIO; 271 272 /* 273 * VRM doesn't return temperature, only alarm bit. This 274 * attribute maps to tempX_alarm instead of tempX_input for 275 * VRM 276 */ 277 if (temp->fru_type != OCC_FRU_TYPE_VRM) { 278 /* sensor not ready */ 279 if (val == 0) 280 return -EAGAIN; 281 282 val *= 1000; 283 } 284 break; 285 case 2: 286 val = temp->fru_type; 287 break; 288 case 3: 289 val = temp->value == OCC_TEMP_SENSOR_FAULT; 290 break; 291 default: 292 return -EINVAL; 293 } 294 295 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val); 296 } 297 298 static ssize_t occ_show_freq_1(struct device *dev, 299 struct device_attribute *attr, char *buf) 300 { 301 int rc; 302 u16 val = 0; 303 struct freq_sensor_1 *freq; 304 struct occ *occ = dev_get_drvdata(dev); 305 struct occ_sensors *sensors = &occ->sensors; 306 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 307 308 rc = occ_update_response(occ); 309 if (rc) 310 return rc; 311 312 freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index; 313 314 switch (sattr->nr) { 315 case 0: 316 val = get_unaligned_be16(&freq->sensor_id); 317 break; 318 case 1: 319 val = get_unaligned_be16(&freq->value); 320 break; 321 default: 322 return -EINVAL; 323 } 324 325 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val); 326 } 327 328 static ssize_t occ_show_freq_2(struct device *dev, 329 struct device_attribute *attr, char *buf) 330 { 331 int rc; 332 u32 val = 0; 333 struct freq_sensor_2 *freq; 334 struct occ *occ = dev_get_drvdata(dev); 335 struct occ_sensors *sensors = &occ->sensors; 336 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 337 338 rc = occ_update_response(occ); 339 if (rc) 340 return rc; 341 342 freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index; 343 344 switch (sattr->nr) { 345 case 0: 346 val = get_unaligned_be32(&freq->sensor_id); 347 break; 348 case 1: 349 val = get_unaligned_be16(&freq->value); 350 break; 351 default: 352 return -EINVAL; 353 } 354 355 return snprintf(buf, PAGE_SIZE - 1, "%u\n", val); 356 } 357 358 static ssize_t occ_show_power_1(struct device *dev, 359 struct device_attribute *attr, char *buf) 360 { 361 int rc; 362 u64 val = 0; 363 struct power_sensor_1 *power; 364 struct occ *occ = dev_get_drvdata(dev); 365 struct occ_sensors *sensors = &occ->sensors; 366 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 367 368 rc = occ_update_response(occ); 369 if (rc) 370 return rc; 371 372 power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index; 373 374 switch (sattr->nr) { 375 case 0: 376 val = get_unaligned_be16(&power->sensor_id); 377 break; 378 case 1: 379 val = get_unaligned_be32(&power->accumulator) / 380 get_unaligned_be32(&power->update_tag); 381 val *= 1000000ULL; 382 break; 383 case 2: 384 val = (u64)get_unaligned_be32(&power->update_tag) * 385 occ->powr_sample_time_us; 386 break; 387 case 3: 388 val = get_unaligned_be16(&power->value) * 1000000ULL; 389 break; 390 default: 391 return -EINVAL; 392 } 393 394 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val); 395 } 396 397 static u64 occ_get_powr_avg(u64 *accum, u32 *samples) 398 { 399 return div64_u64(get_unaligned_be64(accum) * 1000000ULL, 400 get_unaligned_be32(samples)); 401 } 402 403 static ssize_t occ_show_power_2(struct device *dev, 404 struct device_attribute *attr, char *buf) 405 { 406 int rc; 407 u64 val = 0; 408 struct power_sensor_2 *power; 409 struct occ *occ = dev_get_drvdata(dev); 410 struct occ_sensors *sensors = &occ->sensors; 411 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 412 413 rc = occ_update_response(occ); 414 if (rc) 415 return rc; 416 417 power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index; 418 419 switch (sattr->nr) { 420 case 0: 421 return snprintf(buf, PAGE_SIZE - 1, "%u_%u_%u\n", 422 get_unaligned_be32(&power->sensor_id), 423 power->function_id, power->apss_channel); 424 case 1: 425 val = occ_get_powr_avg(&power->accumulator, 426 &power->update_tag); 427 break; 428 case 2: 429 val = (u64)get_unaligned_be32(&power->update_tag) * 430 occ->powr_sample_time_us; 431 break; 432 case 3: 433 val = get_unaligned_be16(&power->value) * 1000000ULL; 434 break; 435 default: 436 return -EINVAL; 437 } 438 439 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val); 440 } 441 442 static ssize_t occ_show_power_a0(struct device *dev, 443 struct device_attribute *attr, char *buf) 444 { 445 int rc; 446 u64 val = 0; 447 struct power_sensor_a0 *power; 448 struct occ *occ = dev_get_drvdata(dev); 449 struct occ_sensors *sensors = &occ->sensors; 450 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 451 452 rc = occ_update_response(occ); 453 if (rc) 454 return rc; 455 456 power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index; 457 458 switch (sattr->nr) { 459 case 0: 460 return snprintf(buf, PAGE_SIZE - 1, "%u_system\n", 461 get_unaligned_be32(&power->sensor_id)); 462 case 1: 463 val = occ_get_powr_avg(&power->system.accumulator, 464 &power->system.update_tag); 465 break; 466 case 2: 467 val = (u64)get_unaligned_be32(&power->system.update_tag) * 468 occ->powr_sample_time_us; 469 break; 470 case 3: 471 val = get_unaligned_be16(&power->system.value) * 1000000ULL; 472 break; 473 case 4: 474 return snprintf(buf, PAGE_SIZE - 1, "%u_proc\n", 475 get_unaligned_be32(&power->sensor_id)); 476 case 5: 477 val = occ_get_powr_avg(&power->proc.accumulator, 478 &power->proc.update_tag); 479 break; 480 case 6: 481 val = (u64)get_unaligned_be32(&power->proc.update_tag) * 482 occ->powr_sample_time_us; 483 break; 484 case 7: 485 val = get_unaligned_be16(&power->proc.value) * 1000000ULL; 486 break; 487 case 8: 488 return snprintf(buf, PAGE_SIZE - 1, "%u_vdd\n", 489 get_unaligned_be32(&power->sensor_id)); 490 case 9: 491 val = occ_get_powr_avg(&power->vdd.accumulator, 492 &power->vdd.update_tag); 493 break; 494 case 10: 495 val = (u64)get_unaligned_be32(&power->vdd.update_tag) * 496 occ->powr_sample_time_us; 497 break; 498 case 11: 499 val = get_unaligned_be16(&power->vdd.value) * 1000000ULL; 500 break; 501 case 12: 502 return snprintf(buf, PAGE_SIZE - 1, "%u_vdn\n", 503 get_unaligned_be32(&power->sensor_id)); 504 case 13: 505 val = occ_get_powr_avg(&power->vdn.accumulator, 506 &power->vdn.update_tag); 507 break; 508 case 14: 509 val = (u64)get_unaligned_be32(&power->vdn.update_tag) * 510 occ->powr_sample_time_us; 511 break; 512 case 15: 513 val = get_unaligned_be16(&power->vdn.value) * 1000000ULL; 514 break; 515 default: 516 return -EINVAL; 517 } 518 519 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val); 520 } 521 522 static ssize_t occ_show_caps_1_2(struct device *dev, 523 struct device_attribute *attr, char *buf) 524 { 525 int rc; 526 u64 val = 0; 527 struct caps_sensor_2 *caps; 528 struct occ *occ = dev_get_drvdata(dev); 529 struct occ_sensors *sensors = &occ->sensors; 530 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 531 532 rc = occ_update_response(occ); 533 if (rc) 534 return rc; 535 536 caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index; 537 538 switch (sattr->nr) { 539 case 0: 540 return snprintf(buf, PAGE_SIZE - 1, "system\n"); 541 case 1: 542 val = get_unaligned_be16(&caps->cap) * 1000000ULL; 543 break; 544 case 2: 545 val = get_unaligned_be16(&caps->system_power) * 1000000ULL; 546 break; 547 case 3: 548 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL; 549 break; 550 case 4: 551 val = get_unaligned_be16(&caps->max) * 1000000ULL; 552 break; 553 case 5: 554 val = get_unaligned_be16(&caps->min) * 1000000ULL; 555 break; 556 case 6: 557 val = get_unaligned_be16(&caps->user) * 1000000ULL; 558 break; 559 case 7: 560 if (occ->sensors.caps.version == 1) 561 return -EINVAL; 562 563 val = caps->user_source; 564 break; 565 default: 566 return -EINVAL; 567 } 568 569 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val); 570 } 571 572 static ssize_t occ_show_caps_3(struct device *dev, 573 struct device_attribute *attr, char *buf) 574 { 575 int rc; 576 u64 val = 0; 577 struct caps_sensor_3 *caps; 578 struct occ *occ = dev_get_drvdata(dev); 579 struct occ_sensors *sensors = &occ->sensors; 580 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 581 582 rc = occ_update_response(occ); 583 if (rc) 584 return rc; 585 586 caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index; 587 588 switch (sattr->nr) { 589 case 0: 590 return snprintf(buf, PAGE_SIZE - 1, "system\n"); 591 case 1: 592 val = get_unaligned_be16(&caps->cap) * 1000000ULL; 593 break; 594 case 2: 595 val = get_unaligned_be16(&caps->system_power) * 1000000ULL; 596 break; 597 case 3: 598 val = get_unaligned_be16(&caps->n_cap) * 1000000ULL; 599 break; 600 case 4: 601 val = get_unaligned_be16(&caps->max) * 1000000ULL; 602 break; 603 case 5: 604 val = get_unaligned_be16(&caps->hard_min) * 1000000ULL; 605 break; 606 case 6: 607 val = get_unaligned_be16(&caps->user) * 1000000ULL; 608 break; 609 case 7: 610 val = caps->user_source; 611 break; 612 default: 613 return -EINVAL; 614 } 615 616 return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val); 617 } 618 619 static ssize_t occ_store_caps_user(struct device *dev, 620 struct device_attribute *attr, 621 const char *buf, size_t count) 622 { 623 int rc; 624 u16 user_power_cap; 625 unsigned long long value; 626 struct occ *occ = dev_get_drvdata(dev); 627 628 rc = kstrtoull(buf, 0, &value); 629 if (rc) 630 return rc; 631 632 user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */ 633 634 rc = occ_set_user_power_cap(occ, user_power_cap); 635 if (rc) 636 return rc; 637 638 return count; 639 } 640 641 static ssize_t occ_show_extended(struct device *dev, 642 struct device_attribute *attr, char *buf) 643 { 644 int rc; 645 struct extended_sensor *extn; 646 struct occ *occ = dev_get_drvdata(dev); 647 struct occ_sensors *sensors = &occ->sensors; 648 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); 649 650 rc = occ_update_response(occ); 651 if (rc) 652 return rc; 653 654 extn = ((struct extended_sensor *)sensors->extended.data) + 655 sattr->index; 656 657 switch (sattr->nr) { 658 case 0: 659 if (extn->flags & EXTN_FLAG_SENSOR_ID) 660 rc = snprintf(buf, PAGE_SIZE - 1, "%u", 661 get_unaligned_be32(&extn->sensor_id)); 662 else 663 rc = snprintf(buf, PAGE_SIZE - 1, "%02x%02x%02x%02x\n", 664 extn->name[0], extn->name[1], 665 extn->name[2], extn->name[3]); 666 break; 667 case 1: 668 rc = snprintf(buf, PAGE_SIZE - 1, "%02x\n", extn->flags); 669 break; 670 case 2: 671 rc = snprintf(buf, PAGE_SIZE - 1, "%02x%02x%02x%02x%02x%02x\n", 672 extn->data[0], extn->data[1], extn->data[2], 673 extn->data[3], extn->data[4], extn->data[5]); 674 break; 675 default: 676 return -EINVAL; 677 } 678 679 return rc; 680 } 681 682 /* 683 * Some helper macros to make it easier to define an occ_attribute. Since these 684 * are dynamically allocated, we shouldn't use the existing kernel macros which 685 * stringify the name argument. 686 */ 687 #define ATTR_OCC(_name, _mode, _show, _store) { \ 688 .attr = { \ 689 .name = _name, \ 690 .mode = VERIFY_OCTAL_PERMISSIONS(_mode), \ 691 }, \ 692 .show = _show, \ 693 .store = _store, \ 694 } 695 696 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) { \ 697 .dev_attr = ATTR_OCC(_name, _mode, _show, _store), \ 698 .index = _index, \ 699 .nr = _nr, \ 700 } 701 702 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index) \ 703 ((struct sensor_device_attribute_2) \ 704 SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index)) 705 706 /* 707 * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to 708 * use our own instead of the built-in hwmon attribute types. 709 */ 710 static int occ_setup_sensor_attrs(struct occ *occ) 711 { 712 unsigned int i, s, num_attrs = 0; 713 struct device *dev = occ->bus_dev; 714 struct occ_sensors *sensors = &occ->sensors; 715 struct occ_attribute *attr; 716 struct temp_sensor_2 *temp; 717 ssize_t (*show_temp)(struct device *, struct device_attribute *, 718 char *) = occ_show_temp_1; 719 ssize_t (*show_freq)(struct device *, struct device_attribute *, 720 char *) = occ_show_freq_1; 721 ssize_t (*show_power)(struct device *, struct device_attribute *, 722 char *) = occ_show_power_1; 723 ssize_t (*show_caps)(struct device *, struct device_attribute *, 724 char *) = occ_show_caps_1_2; 725 726 switch (sensors->temp.version) { 727 case 1: 728 num_attrs += (sensors->temp.num_sensors * 2); 729 break; 730 case 2: 731 num_attrs += (sensors->temp.num_sensors * 4); 732 show_temp = occ_show_temp_2; 733 break; 734 default: 735 sensors->temp.num_sensors = 0; 736 } 737 738 switch (sensors->freq.version) { 739 case 2: 740 show_freq = occ_show_freq_2; 741 /* fall through */ 742 case 1: 743 num_attrs += (sensors->freq.num_sensors * 2); 744 break; 745 default: 746 sensors->freq.num_sensors = 0; 747 } 748 749 switch (sensors->power.version) { 750 case 2: 751 show_power = occ_show_power_2; 752 /* fall through */ 753 case 1: 754 num_attrs += (sensors->power.num_sensors * 4); 755 break; 756 case 0xA0: 757 num_attrs += (sensors->power.num_sensors * 16); 758 show_power = occ_show_power_a0; 759 break; 760 default: 761 sensors->power.num_sensors = 0; 762 } 763 764 switch (sensors->caps.version) { 765 case 1: 766 num_attrs += (sensors->caps.num_sensors * 7); 767 break; 768 case 3: 769 show_caps = occ_show_caps_3; 770 /* fall through */ 771 case 2: 772 num_attrs += (sensors->caps.num_sensors * 8); 773 break; 774 default: 775 sensors->caps.num_sensors = 0; 776 } 777 778 switch (sensors->extended.version) { 779 case 1: 780 num_attrs += (sensors->extended.num_sensors * 3); 781 break; 782 default: 783 sensors->extended.num_sensors = 0; 784 } 785 786 occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs, 787 GFP_KERNEL); 788 if (!occ->attrs) 789 return -ENOMEM; 790 791 /* null-terminated list */ 792 occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) * 793 num_attrs + 1, GFP_KERNEL); 794 if (!occ->group.attrs) 795 return -ENOMEM; 796 797 attr = occ->attrs; 798 799 for (i = 0; i < sensors->temp.num_sensors; ++i) { 800 s = i + 1; 801 temp = ((struct temp_sensor_2 *)sensors->temp.data) + i; 802 803 snprintf(attr->name, sizeof(attr->name), "temp%d_label", s); 804 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL, 805 0, i); 806 attr++; 807 808 if (sensors->temp.version > 1 && 809 temp->fru_type == OCC_FRU_TYPE_VRM) { 810 snprintf(attr->name, sizeof(attr->name), 811 "temp%d_alarm", s); 812 } else { 813 snprintf(attr->name, sizeof(attr->name), 814 "temp%d_input", s); 815 } 816 817 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL, 818 1, i); 819 attr++; 820 821 if (sensors->temp.version > 1) { 822 snprintf(attr->name, sizeof(attr->name), 823 "temp%d_fru_type", s); 824 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 825 show_temp, NULL, 2, i); 826 attr++; 827 828 snprintf(attr->name, sizeof(attr->name), 829 "temp%d_fault", s); 830 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 831 show_temp, NULL, 3, i); 832 attr++; 833 } 834 } 835 836 for (i = 0; i < sensors->freq.num_sensors; ++i) { 837 s = i + 1; 838 839 snprintf(attr->name, sizeof(attr->name), "freq%d_label", s); 840 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL, 841 0, i); 842 attr++; 843 844 snprintf(attr->name, sizeof(attr->name), "freq%d_input", s); 845 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL, 846 1, i); 847 attr++; 848 } 849 850 if (sensors->power.version == 0xA0) { 851 /* 852 * Special case for many-attribute power sensor. Split it into 853 * a sensor number per power type, emulating several sensors. 854 */ 855 for (i = 0; i < sensors->power.num_sensors; ++i) { 856 unsigned int j; 857 unsigned int nr = 0; 858 859 s = (i * 4) + 1; 860 861 for (j = 0; j < 4; ++j) { 862 snprintf(attr->name, sizeof(attr->name), 863 "power%d_label", s); 864 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 865 show_power, NULL, 866 nr++, i); 867 attr++; 868 869 snprintf(attr->name, sizeof(attr->name), 870 "power%d_average", s); 871 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 872 show_power, NULL, 873 nr++, i); 874 attr++; 875 876 snprintf(attr->name, sizeof(attr->name), 877 "power%d_average_interval", s); 878 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 879 show_power, NULL, 880 nr++, i); 881 attr++; 882 883 snprintf(attr->name, sizeof(attr->name), 884 "power%d_input", s); 885 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 886 show_power, NULL, 887 nr++, i); 888 attr++; 889 890 s++; 891 } 892 } 893 } else { 894 for (i = 0; i < sensors->power.num_sensors; ++i) { 895 s = i + 1; 896 897 snprintf(attr->name, sizeof(attr->name), 898 "power%d_label", s); 899 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 900 show_power, NULL, 0, i); 901 attr++; 902 903 snprintf(attr->name, sizeof(attr->name), 904 "power%d_average", s); 905 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 906 show_power, NULL, 1, i); 907 attr++; 908 909 snprintf(attr->name, sizeof(attr->name), 910 "power%d_average_interval", s); 911 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 912 show_power, NULL, 2, i); 913 attr++; 914 915 snprintf(attr->name, sizeof(attr->name), 916 "power%d_input", s); 917 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 918 show_power, NULL, 3, i); 919 attr++; 920 } 921 } 922 923 if (sensors->caps.num_sensors >= 1) { 924 s = sensors->power.num_sensors + 1; 925 926 snprintf(attr->name, sizeof(attr->name), "power%d_label", s); 927 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL, 928 0, 0); 929 attr++; 930 931 snprintf(attr->name, sizeof(attr->name), "power%d_cap", s); 932 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL, 933 1, 0); 934 attr++; 935 936 snprintf(attr->name, sizeof(attr->name), "power%d_input", s); 937 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL, 938 2, 0); 939 attr++; 940 941 snprintf(attr->name, sizeof(attr->name), 942 "power%d_cap_not_redundant", s); 943 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL, 944 3, 0); 945 attr++; 946 947 snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s); 948 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL, 949 4, 0); 950 attr++; 951 952 snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s); 953 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL, 954 5, 0); 955 attr++; 956 957 snprintf(attr->name, sizeof(attr->name), "power%d_cap_user", 958 s); 959 attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps, 960 occ_store_caps_user, 6, 0); 961 attr++; 962 963 if (sensors->caps.version > 1) { 964 snprintf(attr->name, sizeof(attr->name), 965 "power%d_cap_user_source", s); 966 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 967 show_caps, NULL, 7, 0); 968 attr++; 969 } 970 } 971 972 for (i = 0; i < sensors->extended.num_sensors; ++i) { 973 s = i + 1; 974 975 snprintf(attr->name, sizeof(attr->name), "extn%d_label", s); 976 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 977 occ_show_extended, NULL, 0, i); 978 attr++; 979 980 snprintf(attr->name, sizeof(attr->name), "extn%d_flags", s); 981 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 982 occ_show_extended, NULL, 1, i); 983 attr++; 984 985 snprintf(attr->name, sizeof(attr->name), "extn%d_input", s); 986 attr->sensor = OCC_INIT_ATTR(attr->name, 0444, 987 occ_show_extended, NULL, 2, i); 988 attr++; 989 } 990 991 /* put the sensors in the group */ 992 for (i = 0; i < num_attrs; ++i) { 993 sysfs_attr_init(&occ->attrs[i].sensor.dev_attr.attr); 994 occ->group.attrs[i] = &occ->attrs[i].sensor.dev_attr.attr; 995 } 996 997 return 0; 998 } 999 1000 /* only need to do this once at startup, as OCC won't change sensors on us */ 1001 static void occ_parse_poll_response(struct occ *occ) 1002 { 1003 unsigned int i, old_offset, offset = 0, size = 0; 1004 struct occ_sensor *sensor; 1005 struct occ_sensors *sensors = &occ->sensors; 1006 struct occ_response *resp = &occ->resp; 1007 struct occ_poll_response *poll = 1008 (struct occ_poll_response *)&resp->data[0]; 1009 struct occ_poll_response_header *header = &poll->header; 1010 struct occ_sensor_data_block *block = &poll->block; 1011 1012 dev_info(occ->bus_dev, "OCC found, code level: %.16s\n", 1013 header->occ_code_level); 1014 1015 for (i = 0; i < header->num_sensor_data_blocks; ++i) { 1016 block = (struct occ_sensor_data_block *)((u8 *)block + offset); 1017 old_offset = offset; 1018 offset = (block->header.num_sensors * 1019 block->header.sensor_length) + sizeof(block->header); 1020 size += offset; 1021 1022 /* validate all the length/size fields */ 1023 if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) { 1024 dev_warn(occ->bus_dev, "exceeded response buffer\n"); 1025 return; 1026 } 1027 1028 dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n", 1029 old_offset, offset - 1, block->header.eye_catcher, 1030 block->header.num_sensors); 1031 1032 /* match sensor block type */ 1033 if (strncmp(block->header.eye_catcher, "TEMP", 4) == 0) 1034 sensor = &sensors->temp; 1035 else if (strncmp(block->header.eye_catcher, "FREQ", 4) == 0) 1036 sensor = &sensors->freq; 1037 else if (strncmp(block->header.eye_catcher, "POWR", 4) == 0) 1038 sensor = &sensors->power; 1039 else if (strncmp(block->header.eye_catcher, "CAPS", 4) == 0) 1040 sensor = &sensors->caps; 1041 else if (strncmp(block->header.eye_catcher, "EXTN", 4) == 0) 1042 sensor = &sensors->extended; 1043 else { 1044 dev_warn(occ->bus_dev, "sensor not supported %.4s\n", 1045 block->header.eye_catcher); 1046 continue; 1047 } 1048 1049 sensor->num_sensors = block->header.num_sensors; 1050 sensor->version = block->header.sensor_format; 1051 sensor->data = &block->data; 1052 } 1053 1054 dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size, 1055 sizeof(*header), size + sizeof(*header)); 1056 } 1057 1058 int occ_setup(struct occ *occ, const char *name) 1059 { 1060 int rc; 1061 1062 mutex_init(&occ->lock); 1063 occ->groups[0] = &occ->group; 1064 1065 /* no need to lock */ 1066 rc = occ_poll(occ); 1067 if (rc == -ESHUTDOWN) { 1068 dev_info(occ->bus_dev, "host is not ready\n"); 1069 return rc; 1070 } else if (rc < 0) { 1071 dev_err(occ->bus_dev, "failed to get OCC poll response: %d\n", 1072 rc); 1073 return rc; 1074 } 1075 1076 occ_parse_poll_response(occ); 1077 1078 rc = occ_setup_sensor_attrs(occ); 1079 if (rc) { 1080 dev_err(occ->bus_dev, "failed to setup sensor attrs: %d\n", 1081 rc); 1082 return rc; 1083 } 1084 1085 occ->hwmon = devm_hwmon_device_register_with_groups(occ->bus_dev, name, 1086 occ, occ->groups); 1087 if (IS_ERR(occ->hwmon)) { 1088 rc = PTR_ERR(occ->hwmon); 1089 dev_err(occ->bus_dev, "failed to register hwmon device: %d\n", 1090 rc); 1091 return rc; 1092 } 1093 1094 rc = occ_setup_sysfs(occ); 1095 if (rc) 1096 dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc); 1097 1098 return rc; 1099 } 1100