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