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