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