xref: /linux/drivers/hwmon/occ/common.c (revision bdd1a21b52557ea8f61d0a5dc2f77151b576eb70)
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 temp_sensor_10 {
45 	u32 sensor_id;
46 	u8 fru_type;
47 	u8 value;
48 	u8 throttle;
49 	u8 reserved;
50 } __packed;
51 
52 struct freq_sensor_1 {
53 	u16 sensor_id;
54 	u16 value;
55 } __packed;
56 
57 struct freq_sensor_2 {
58 	u32 sensor_id;
59 	u16 value;
60 } __packed;
61 
62 struct power_sensor_1 {
63 	u16 sensor_id;
64 	u32 update_tag;
65 	u32 accumulator;
66 	u16 value;
67 } __packed;
68 
69 struct 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 
79 struct power_sensor_data {
80 	u16 value;
81 	u32 update_tag;
82 	u64 accumulator;
83 } __packed;
84 
85 struct power_sensor_data_and_time {
86 	u16 update_time;
87 	u16 value;
88 	u32 update_tag;
89 	u64 accumulator;
90 } __packed;
91 
92 struct 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 
101 struct 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 
111 struct 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 
122 struct 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 
132 static 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 
178 done:
179 	occ_sysfs_poll_done(occ);
180 	return rc;
181 }
182 
183 static 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 
212 int 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 
231 static 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 
267 static 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 
318 static 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 		/*
344 		 * VRM doesn't return temperature, only alarm bit. This
345 		 * attribute maps to tempX_alarm instead of tempX_input for
346 		 * VRM
347 		 */
348 		if (temp->fru_type != OCC_FRU_TYPE_VRM) {
349 			/* sensor not ready */
350 			if (val == 0)
351 				return -EAGAIN;
352 
353 			val *= 1000;
354 		}
355 		break;
356 	case 2:
357 		val = temp->fru_type;
358 		break;
359 	case 3:
360 		val = temp->value == OCC_TEMP_SENSOR_FAULT;
361 		break;
362 	case 4:
363 		val = temp->throttle * 1000;
364 		break;
365 	default:
366 		return -EINVAL;
367 	}
368 
369 	return sysfs_emit(buf, "%u\n", val);
370 }
371 
372 static ssize_t occ_show_freq_1(struct device *dev,
373 			       struct device_attribute *attr, char *buf)
374 {
375 	int rc;
376 	u16 val = 0;
377 	struct freq_sensor_1 *freq;
378 	struct occ *occ = dev_get_drvdata(dev);
379 	struct occ_sensors *sensors = &occ->sensors;
380 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
381 
382 	rc = occ_update_response(occ);
383 	if (rc)
384 		return rc;
385 
386 	freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index;
387 
388 	switch (sattr->nr) {
389 	case 0:
390 		val = get_unaligned_be16(&freq->sensor_id);
391 		break;
392 	case 1:
393 		val = get_unaligned_be16(&freq->value);
394 		break;
395 	default:
396 		return -EINVAL;
397 	}
398 
399 	return sysfs_emit(buf, "%u\n", val);
400 }
401 
402 static ssize_t occ_show_freq_2(struct device *dev,
403 			       struct device_attribute *attr, char *buf)
404 {
405 	int rc;
406 	u32 val = 0;
407 	struct freq_sensor_2 *freq;
408 	struct occ *occ = dev_get_drvdata(dev);
409 	struct occ_sensors *sensors = &occ->sensors;
410 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
411 
412 	rc = occ_update_response(occ);
413 	if (rc)
414 		return rc;
415 
416 	freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index;
417 
418 	switch (sattr->nr) {
419 	case 0:
420 		val = get_unaligned_be32(&freq->sensor_id);
421 		break;
422 	case 1:
423 		val = get_unaligned_be16(&freq->value);
424 		break;
425 	default:
426 		return -EINVAL;
427 	}
428 
429 	return sysfs_emit(buf, "%u\n", val);
430 }
431 
432 static ssize_t occ_show_power_1(struct device *dev,
433 				struct device_attribute *attr, char *buf)
434 {
435 	int rc;
436 	u64 val = 0;
437 	struct power_sensor_1 *power;
438 	struct occ *occ = dev_get_drvdata(dev);
439 	struct occ_sensors *sensors = &occ->sensors;
440 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
441 
442 	rc = occ_update_response(occ);
443 	if (rc)
444 		return rc;
445 
446 	power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index;
447 
448 	switch (sattr->nr) {
449 	case 0:
450 		val = get_unaligned_be16(&power->sensor_id);
451 		break;
452 	case 1:
453 		val = get_unaligned_be32(&power->accumulator) /
454 			get_unaligned_be32(&power->update_tag);
455 		val *= 1000000ULL;
456 		break;
457 	case 2:
458 		val = (u64)get_unaligned_be32(&power->update_tag) *
459 			   occ->powr_sample_time_us;
460 		break;
461 	case 3:
462 		val = get_unaligned_be16(&power->value) * 1000000ULL;
463 		break;
464 	default:
465 		return -EINVAL;
466 	}
467 
468 	return sysfs_emit(buf, "%llu\n", val);
469 }
470 
471 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
472 {
473 	u64 divisor = get_unaligned_be32(samples);
474 
475 	return (divisor == 0) ? 0 :
476 		div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
477 }
478 
479 static ssize_t occ_show_power_2(struct device *dev,
480 				struct device_attribute *attr, char *buf)
481 {
482 	int rc;
483 	u64 val = 0;
484 	struct power_sensor_2 *power;
485 	struct occ *occ = dev_get_drvdata(dev);
486 	struct occ_sensors *sensors = &occ->sensors;
487 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
488 
489 	rc = occ_update_response(occ);
490 	if (rc)
491 		return rc;
492 
493 	power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index;
494 
495 	switch (sattr->nr) {
496 	case 0:
497 		return sysfs_emit(buf, "%u_%u_%u\n",
498 				  get_unaligned_be32(&power->sensor_id),
499 				  power->function_id, power->apss_channel);
500 	case 1:
501 		val = occ_get_powr_avg(&power->accumulator,
502 				       &power->update_tag);
503 		break;
504 	case 2:
505 		val = (u64)get_unaligned_be32(&power->update_tag) *
506 			   occ->powr_sample_time_us;
507 		break;
508 	case 3:
509 		val = get_unaligned_be16(&power->value) * 1000000ULL;
510 		break;
511 	default:
512 		return -EINVAL;
513 	}
514 
515 	return sysfs_emit(buf, "%llu\n", val);
516 }
517 
518 static ssize_t occ_show_power_a0(struct device *dev,
519 				 struct device_attribute *attr, char *buf)
520 {
521 	int rc;
522 	u64 val = 0;
523 	struct power_sensor_a0 *power;
524 	struct occ *occ = dev_get_drvdata(dev);
525 	struct occ_sensors *sensors = &occ->sensors;
526 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
527 
528 	rc = occ_update_response(occ);
529 	if (rc)
530 		return rc;
531 
532 	power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index;
533 
534 	switch (sattr->nr) {
535 	case 0:
536 		return sysfs_emit(buf, "%u_system\n",
537 				  get_unaligned_be32(&power->sensor_id));
538 	case 1:
539 		val = occ_get_powr_avg(&power->system.accumulator,
540 				       &power->system.update_tag);
541 		break;
542 	case 2:
543 		val = (u64)get_unaligned_be32(&power->system.update_tag) *
544 			   occ->powr_sample_time_us;
545 		break;
546 	case 3:
547 		val = get_unaligned_be16(&power->system.value) * 1000000ULL;
548 		break;
549 	case 4:
550 		return sysfs_emit(buf, "%u_proc\n",
551 				  get_unaligned_be32(&power->sensor_id));
552 	case 5:
553 		val = occ_get_powr_avg(&power->proc.accumulator,
554 				       &power->proc.update_tag);
555 		break;
556 	case 6:
557 		val = (u64)get_unaligned_be32(&power->proc.update_tag) *
558 			   occ->powr_sample_time_us;
559 		break;
560 	case 7:
561 		val = get_unaligned_be16(&power->proc.value) * 1000000ULL;
562 		break;
563 	case 8:
564 		return sysfs_emit(buf, "%u_vdd\n",
565 				  get_unaligned_be32(&power->sensor_id));
566 	case 9:
567 		val = occ_get_powr_avg(&power->vdd.accumulator,
568 				       &power->vdd.update_tag);
569 		break;
570 	case 10:
571 		val = (u64)get_unaligned_be32(&power->vdd.update_tag) *
572 			   occ->powr_sample_time_us;
573 		break;
574 	case 11:
575 		val = get_unaligned_be16(&power->vdd.value) * 1000000ULL;
576 		break;
577 	case 12:
578 		return sysfs_emit(buf, "%u_vdn\n",
579 				  get_unaligned_be32(&power->sensor_id));
580 	case 13:
581 		val = occ_get_powr_avg(&power->vdn.accumulator,
582 				       &power->vdn.update_tag);
583 		break;
584 	case 14:
585 		val = (u64)get_unaligned_be32(&power->vdn.update_tag) *
586 			   occ->powr_sample_time_us;
587 		break;
588 	case 15:
589 		val = get_unaligned_be16(&power->vdn.value) * 1000000ULL;
590 		break;
591 	default:
592 		return -EINVAL;
593 	}
594 
595 	return sysfs_emit(buf, "%llu\n", val);
596 }
597 
598 static ssize_t occ_show_caps_1_2(struct device *dev,
599 				 struct device_attribute *attr, char *buf)
600 {
601 	int rc;
602 	u64 val = 0;
603 	struct caps_sensor_2 *caps;
604 	struct occ *occ = dev_get_drvdata(dev);
605 	struct occ_sensors *sensors = &occ->sensors;
606 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
607 
608 	rc = occ_update_response(occ);
609 	if (rc)
610 		return rc;
611 
612 	caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index;
613 
614 	switch (sattr->nr) {
615 	case 0:
616 		return sysfs_emit(buf, "system\n");
617 	case 1:
618 		val = get_unaligned_be16(&caps->cap) * 1000000ULL;
619 		break;
620 	case 2:
621 		val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
622 		break;
623 	case 3:
624 		val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
625 		break;
626 	case 4:
627 		val = get_unaligned_be16(&caps->max) * 1000000ULL;
628 		break;
629 	case 5:
630 		val = get_unaligned_be16(&caps->min) * 1000000ULL;
631 		break;
632 	case 6:
633 		val = get_unaligned_be16(&caps->user) * 1000000ULL;
634 		break;
635 	case 7:
636 		if (occ->sensors.caps.version == 1)
637 			return -EINVAL;
638 
639 		val = caps->user_source;
640 		break;
641 	default:
642 		return -EINVAL;
643 	}
644 
645 	return sysfs_emit(buf, "%llu\n", val);
646 }
647 
648 static ssize_t occ_show_caps_3(struct device *dev,
649 			       struct device_attribute *attr, char *buf)
650 {
651 	int rc;
652 	u64 val = 0;
653 	struct caps_sensor_3 *caps;
654 	struct occ *occ = dev_get_drvdata(dev);
655 	struct occ_sensors *sensors = &occ->sensors;
656 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
657 
658 	rc = occ_update_response(occ);
659 	if (rc)
660 		return rc;
661 
662 	caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index;
663 
664 	switch (sattr->nr) {
665 	case 0:
666 		return sysfs_emit(buf, "system\n");
667 	case 1:
668 		val = get_unaligned_be16(&caps->cap) * 1000000ULL;
669 		break;
670 	case 2:
671 		val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
672 		break;
673 	case 3:
674 		val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
675 		break;
676 	case 4:
677 		val = get_unaligned_be16(&caps->max) * 1000000ULL;
678 		break;
679 	case 5:
680 		val = get_unaligned_be16(&caps->hard_min) * 1000000ULL;
681 		break;
682 	case 6:
683 		val = get_unaligned_be16(&caps->user) * 1000000ULL;
684 		break;
685 	case 7:
686 		val = caps->user_source;
687 		break;
688 	default:
689 		return -EINVAL;
690 	}
691 
692 	return sysfs_emit(buf, "%llu\n", val);
693 }
694 
695 static ssize_t occ_store_caps_user(struct device *dev,
696 				   struct device_attribute *attr,
697 				   const char *buf, size_t count)
698 {
699 	int rc;
700 	u16 user_power_cap;
701 	unsigned long long value;
702 	struct occ *occ = dev_get_drvdata(dev);
703 
704 	rc = kstrtoull(buf, 0, &value);
705 	if (rc)
706 		return rc;
707 
708 	user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */
709 
710 	rc = occ_set_user_power_cap(occ, user_power_cap);
711 	if (rc)
712 		return rc;
713 
714 	return count;
715 }
716 
717 static ssize_t occ_show_extended(struct device *dev,
718 				 struct device_attribute *attr, char *buf)
719 {
720 	int rc;
721 	struct extended_sensor *extn;
722 	struct occ *occ = dev_get_drvdata(dev);
723 	struct occ_sensors *sensors = &occ->sensors;
724 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
725 
726 	rc = occ_update_response(occ);
727 	if (rc)
728 		return rc;
729 
730 	extn = ((struct extended_sensor *)sensors->extended.data) +
731 		sattr->index;
732 
733 	switch (sattr->nr) {
734 	case 0:
735 		if (extn->flags & EXTN_FLAG_SENSOR_ID) {
736 			rc = sysfs_emit(buf, "%u",
737 					get_unaligned_be32(&extn->sensor_id));
738 		} else {
739 			rc = sysfs_emit(buf, "%02x%02x%02x%02x\n",
740 					extn->name[0], extn->name[1],
741 					extn->name[2], extn->name[3]);
742 		}
743 		break;
744 	case 1:
745 		rc = sysfs_emit(buf, "%02x\n", extn->flags);
746 		break;
747 	case 2:
748 		rc = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
749 				extn->data[0], extn->data[1], extn->data[2],
750 				extn->data[3], extn->data[4], extn->data[5]);
751 		break;
752 	default:
753 		return -EINVAL;
754 	}
755 
756 	return rc;
757 }
758 
759 /*
760  * Some helper macros to make it easier to define an occ_attribute. Since these
761  * are dynamically allocated, we shouldn't use the existing kernel macros which
762  * stringify the name argument.
763  */
764 #define ATTR_OCC(_name, _mode, _show, _store) {				\
765 	.attr	= {							\
766 		.name = _name,						\
767 		.mode = VERIFY_OCTAL_PERMISSIONS(_mode),		\
768 	},								\
769 	.show	= _show,						\
770 	.store	= _store,						\
771 }
772 
773 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) {	\
774 	.dev_attr	= ATTR_OCC(_name, _mode, _show, _store),	\
775 	.index		= _index,					\
776 	.nr		= _nr,						\
777 }
778 
779 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index)		\
780 	((struct sensor_device_attribute_2)				\
781 		SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
782 
783 /*
784  * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
785  * use our own instead of the built-in hwmon attribute types.
786  */
787 static int occ_setup_sensor_attrs(struct occ *occ)
788 {
789 	unsigned int i, s, num_attrs = 0;
790 	struct device *dev = occ->bus_dev;
791 	struct occ_sensors *sensors = &occ->sensors;
792 	struct occ_attribute *attr;
793 	struct temp_sensor_2 *temp;
794 	ssize_t (*show_temp)(struct device *, struct device_attribute *,
795 			     char *) = occ_show_temp_1;
796 	ssize_t (*show_freq)(struct device *, struct device_attribute *,
797 			     char *) = occ_show_freq_1;
798 	ssize_t (*show_power)(struct device *, struct device_attribute *,
799 			      char *) = occ_show_power_1;
800 	ssize_t (*show_caps)(struct device *, struct device_attribute *,
801 			     char *) = occ_show_caps_1_2;
802 
803 	switch (sensors->temp.version) {
804 	case 1:
805 		num_attrs += (sensors->temp.num_sensors * 2);
806 		break;
807 	case 2:
808 		num_attrs += (sensors->temp.num_sensors * 4);
809 		show_temp = occ_show_temp_2;
810 		break;
811 	case 0x10:
812 		num_attrs += (sensors->temp.num_sensors * 5);
813 		show_temp = occ_show_temp_10;
814 		break;
815 	default:
816 		sensors->temp.num_sensors = 0;
817 	}
818 
819 	switch (sensors->freq.version) {
820 	case 2:
821 		show_freq = occ_show_freq_2;
822 		fallthrough;
823 	case 1:
824 		num_attrs += (sensors->freq.num_sensors * 2);
825 		break;
826 	default:
827 		sensors->freq.num_sensors = 0;
828 	}
829 
830 	switch (sensors->power.version) {
831 	case 2:
832 		show_power = occ_show_power_2;
833 		fallthrough;
834 	case 1:
835 		num_attrs += (sensors->power.num_sensors * 4);
836 		break;
837 	case 0xA0:
838 		num_attrs += (sensors->power.num_sensors * 16);
839 		show_power = occ_show_power_a0;
840 		break;
841 	default:
842 		sensors->power.num_sensors = 0;
843 	}
844 
845 	switch (sensors->caps.version) {
846 	case 1:
847 		num_attrs += (sensors->caps.num_sensors * 7);
848 		break;
849 	case 3:
850 		show_caps = occ_show_caps_3;
851 		fallthrough;
852 	case 2:
853 		num_attrs += (sensors->caps.num_sensors * 8);
854 		break;
855 	default:
856 		sensors->caps.num_sensors = 0;
857 	}
858 
859 	switch (sensors->extended.version) {
860 	case 1:
861 		num_attrs += (sensors->extended.num_sensors * 3);
862 		break;
863 	default:
864 		sensors->extended.num_sensors = 0;
865 	}
866 
867 	occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
868 				  GFP_KERNEL);
869 	if (!occ->attrs)
870 		return -ENOMEM;
871 
872 	/* null-terminated list */
873 	occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) *
874 					num_attrs + 1, GFP_KERNEL);
875 	if (!occ->group.attrs)
876 		return -ENOMEM;
877 
878 	attr = occ->attrs;
879 
880 	for (i = 0; i < sensors->temp.num_sensors; ++i) {
881 		s = i + 1;
882 		temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
883 
884 		snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
885 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
886 					     0, i);
887 		attr++;
888 
889 		if (sensors->temp.version > 1 &&
890 		    temp->fru_type == OCC_FRU_TYPE_VRM) {
891 			snprintf(attr->name, sizeof(attr->name),
892 				 "temp%d_alarm", s);
893 		} else {
894 			snprintf(attr->name, sizeof(attr->name),
895 				 "temp%d_input", s);
896 		}
897 
898 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
899 					     1, i);
900 		attr++;
901 
902 		if (sensors->temp.version > 1) {
903 			snprintf(attr->name, sizeof(attr->name),
904 				 "temp%d_fru_type", s);
905 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
906 						     show_temp, NULL, 2, i);
907 			attr++;
908 
909 			snprintf(attr->name, sizeof(attr->name),
910 				 "temp%d_fault", s);
911 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
912 						     show_temp, NULL, 3, i);
913 			attr++;
914 
915 			if (sensors->temp.version == 0x10) {
916 				snprintf(attr->name, sizeof(attr->name),
917 					 "temp%d_max", s);
918 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
919 							     show_temp, NULL,
920 							     4, i);
921 				attr++;
922 			}
923 		}
924 	}
925 
926 	for (i = 0; i < sensors->freq.num_sensors; ++i) {
927 		s = i + 1;
928 
929 		snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
930 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
931 					     0, i);
932 		attr++;
933 
934 		snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
935 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
936 					     1, i);
937 		attr++;
938 	}
939 
940 	if (sensors->power.version == 0xA0) {
941 		/*
942 		 * Special case for many-attribute power sensor. Split it into
943 		 * a sensor number per power type, emulating several sensors.
944 		 */
945 		for (i = 0; i < sensors->power.num_sensors; ++i) {
946 			unsigned int j;
947 			unsigned int nr = 0;
948 
949 			s = (i * 4) + 1;
950 
951 			for (j = 0; j < 4; ++j) {
952 				snprintf(attr->name, sizeof(attr->name),
953 					 "power%d_label", 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", 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_average_interval", s);
968 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
969 							     show_power, NULL,
970 							     nr++, i);
971 				attr++;
972 
973 				snprintf(attr->name, sizeof(attr->name),
974 					 "power%d_input", s);
975 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
976 							     show_power, NULL,
977 							     nr++, i);
978 				attr++;
979 
980 				s++;
981 			}
982 		}
983 
984 		s = (sensors->power.num_sensors * 4) + 1;
985 	} else {
986 		for (i = 0; i < sensors->power.num_sensors; ++i) {
987 			s = i + 1;
988 
989 			snprintf(attr->name, sizeof(attr->name),
990 				 "power%d_label", s);
991 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
992 						     show_power, NULL, 0, i);
993 			attr++;
994 
995 			snprintf(attr->name, sizeof(attr->name),
996 				 "power%d_average", s);
997 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
998 						     show_power, NULL, 1, i);
999 			attr++;
1000 
1001 			snprintf(attr->name, sizeof(attr->name),
1002 				 "power%d_average_interval", s);
1003 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1004 						     show_power, NULL, 2, i);
1005 			attr++;
1006 
1007 			snprintf(attr->name, sizeof(attr->name),
1008 				 "power%d_input", s);
1009 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1010 						     show_power, NULL, 3, i);
1011 			attr++;
1012 		}
1013 
1014 		s = sensors->power.num_sensors + 1;
1015 	}
1016 
1017 	if (sensors->caps.num_sensors >= 1) {
1018 		snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
1019 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1020 					     0, 0);
1021 		attr++;
1022 
1023 		snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
1024 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1025 					     1, 0);
1026 		attr++;
1027 
1028 		snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
1029 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1030 					     2, 0);
1031 		attr++;
1032 
1033 		snprintf(attr->name, sizeof(attr->name),
1034 			 "power%d_cap_not_redundant", s);
1035 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1036 					     3, 0);
1037 		attr++;
1038 
1039 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
1040 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1041 					     4, 0);
1042 		attr++;
1043 
1044 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
1045 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1046 					     5, 0);
1047 		attr++;
1048 
1049 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
1050 			 s);
1051 		attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
1052 					     occ_store_caps_user, 6, 0);
1053 		attr++;
1054 
1055 		if (sensors->caps.version > 1) {
1056 			snprintf(attr->name, sizeof(attr->name),
1057 				 "power%d_cap_user_source", s);
1058 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1059 						     show_caps, NULL, 7, 0);
1060 			attr++;
1061 		}
1062 	}
1063 
1064 	for (i = 0; i < sensors->extended.num_sensors; ++i) {
1065 		s = i + 1;
1066 
1067 		snprintf(attr->name, sizeof(attr->name), "extn%d_label", s);
1068 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1069 					     occ_show_extended, NULL, 0, i);
1070 		attr++;
1071 
1072 		snprintf(attr->name, sizeof(attr->name), "extn%d_flags", s);
1073 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1074 					     occ_show_extended, NULL, 1, i);
1075 		attr++;
1076 
1077 		snprintf(attr->name, sizeof(attr->name), "extn%d_input", s);
1078 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1079 					     occ_show_extended, NULL, 2, i);
1080 		attr++;
1081 	}
1082 
1083 	/* put the sensors in the group */
1084 	for (i = 0; i < num_attrs; ++i) {
1085 		sysfs_attr_init(&occ->attrs[i].sensor.dev_attr.attr);
1086 		occ->group.attrs[i] = &occ->attrs[i].sensor.dev_attr.attr;
1087 	}
1088 
1089 	return 0;
1090 }
1091 
1092 /* only need to do this once at startup, as OCC won't change sensors on us */
1093 static void occ_parse_poll_response(struct occ *occ)
1094 {
1095 	unsigned int i, old_offset, offset = 0, size = 0;
1096 	struct occ_sensor *sensor;
1097 	struct occ_sensors *sensors = &occ->sensors;
1098 	struct occ_response *resp = &occ->resp;
1099 	struct occ_poll_response *poll =
1100 		(struct occ_poll_response *)&resp->data[0];
1101 	struct occ_poll_response_header *header = &poll->header;
1102 	struct occ_sensor_data_block *block = &poll->block;
1103 
1104 	dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
1105 		 header->occ_code_level);
1106 
1107 	for (i = 0; i < header->num_sensor_data_blocks; ++i) {
1108 		block = (struct occ_sensor_data_block *)((u8 *)block + offset);
1109 		old_offset = offset;
1110 		offset = (block->header.num_sensors *
1111 			  block->header.sensor_length) + sizeof(block->header);
1112 		size += offset;
1113 
1114 		/* validate all the length/size fields */
1115 		if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) {
1116 			dev_warn(occ->bus_dev, "exceeded response buffer\n");
1117 			return;
1118 		}
1119 
1120 		dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
1121 			old_offset, offset - 1, block->header.eye_catcher,
1122 			block->header.num_sensors);
1123 
1124 		/* match sensor block type */
1125 		if (strncmp(block->header.eye_catcher, "TEMP", 4) == 0)
1126 			sensor = &sensors->temp;
1127 		else if (strncmp(block->header.eye_catcher, "FREQ", 4) == 0)
1128 			sensor = &sensors->freq;
1129 		else if (strncmp(block->header.eye_catcher, "POWR", 4) == 0)
1130 			sensor = &sensors->power;
1131 		else if (strncmp(block->header.eye_catcher, "CAPS", 4) == 0)
1132 			sensor = &sensors->caps;
1133 		else if (strncmp(block->header.eye_catcher, "EXTN", 4) == 0)
1134 			sensor = &sensors->extended;
1135 		else {
1136 			dev_warn(occ->bus_dev, "sensor not supported %.4s\n",
1137 				 block->header.eye_catcher);
1138 			continue;
1139 		}
1140 
1141 		sensor->num_sensors = block->header.num_sensors;
1142 		sensor->version = block->header.sensor_format;
1143 		sensor->data = &block->data;
1144 	}
1145 
1146 	dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
1147 		sizeof(*header), size + sizeof(*header));
1148 }
1149 
1150 int occ_setup(struct occ *occ, const char *name)
1151 {
1152 	int rc;
1153 
1154 	/* start with 1 to avoid false match with zero-initialized SRAM buffer */
1155 	occ->seq_no = 1;
1156 	mutex_init(&occ->lock);
1157 	occ->groups[0] = &occ->group;
1158 
1159 	/* no need to lock */
1160 	rc = occ_poll(occ);
1161 	if (rc == -ESHUTDOWN) {
1162 		dev_info(occ->bus_dev, "host is not ready\n");
1163 		return rc;
1164 	} else if (rc < 0) {
1165 		dev_err(occ->bus_dev,
1166 			"failed to get OCC poll response=%02x: %d\n",
1167 			occ->resp.return_status, rc);
1168 		return rc;
1169 	}
1170 
1171 	occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
1172 	occ_parse_poll_response(occ);
1173 
1174 	rc = occ_setup_sensor_attrs(occ);
1175 	if (rc) {
1176 		dev_err(occ->bus_dev, "failed to setup sensor attrs: %d\n",
1177 			rc);
1178 		return rc;
1179 	}
1180 
1181 	occ->hwmon = devm_hwmon_device_register_with_groups(occ->bus_dev, name,
1182 							    occ, occ->groups);
1183 	if (IS_ERR(occ->hwmon)) {
1184 		rc = PTR_ERR(occ->hwmon);
1185 		dev_err(occ->bus_dev, "failed to register hwmon device: %d\n",
1186 			rc);
1187 		return rc;
1188 	}
1189 
1190 	rc = occ_setup_sysfs(occ);
1191 	if (rc)
1192 		dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);
1193 
1194 	return rc;
1195 }
1196 EXPORT_SYMBOL_GPL(occ_setup);
1197 
1198 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
1199 MODULE_DESCRIPTION("Common OCC hwmon code");
1200 MODULE_LICENSE("GPL");
1201