xref: /openbmc/linux/drivers/hwmon/occ/common.c (revision 278d3ba6)
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), &occ->resp, sizeof(occ->resp));
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 	u8 resp[8];
186 	__be16 user_power_cap_be = cpu_to_be16(user_power_cap);
187 
188 	cmd[0] = 0;	/* sequence number */
189 	cmd[1] = 0x22;	/* cmd type */
190 	cmd[2] = 0;	/* data length msb */
191 	cmd[3] = 2;	/* data length lsb */
192 
193 	memcpy(&cmd[4], &user_power_cap_be, 2);
194 
195 	cmd[6] = 0;	/* checksum msb */
196 	cmd[7] = 0;	/* checksum lsb */
197 
198 	rc = mutex_lock_interruptible(&occ->lock);
199 	if (rc)
200 		return rc;
201 
202 	rc = occ->send_cmd(occ, cmd, sizeof(cmd), resp, sizeof(resp));
203 
204 	mutex_unlock(&occ->lock);
205 
206 	return rc;
207 }
208 
209 int occ_update_response(struct occ *occ)
210 {
211 	int rc = mutex_lock_interruptible(&occ->lock);
212 
213 	if (rc)
214 		return rc;
215 
216 	/* limit the maximum rate of polling the OCC */
217 	if (time_after(jiffies, occ->next_update)) {
218 		rc = occ_poll(occ);
219 		occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
220 	} else {
221 		rc = occ->last_error;
222 	}
223 
224 	mutex_unlock(&occ->lock);
225 	return rc;
226 }
227 
228 static ssize_t occ_show_temp_1(struct device *dev,
229 			       struct device_attribute *attr, char *buf)
230 {
231 	int rc;
232 	u32 val = 0;
233 	struct temp_sensor_1 *temp;
234 	struct occ *occ = dev_get_drvdata(dev);
235 	struct occ_sensors *sensors = &occ->sensors;
236 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
237 
238 	rc = occ_update_response(occ);
239 	if (rc)
240 		return rc;
241 
242 	temp = ((struct temp_sensor_1 *)sensors->temp.data) + sattr->index;
243 
244 	switch (sattr->nr) {
245 	case 0:
246 		val = get_unaligned_be16(&temp->sensor_id);
247 		break;
248 	case 1:
249 		/*
250 		 * If a sensor reading has expired and couldn't be refreshed,
251 		 * OCC returns 0xFFFF for that sensor.
252 		 */
253 		if (temp->value == 0xFFFF)
254 			return -EREMOTEIO;
255 		val = get_unaligned_be16(&temp->value) * 1000;
256 		break;
257 	default:
258 		return -EINVAL;
259 	}
260 
261 	return sysfs_emit(buf, "%u\n", val);
262 }
263 
264 static ssize_t occ_show_temp_2(struct device *dev,
265 			       struct device_attribute *attr, char *buf)
266 {
267 	int rc;
268 	u32 val = 0;
269 	struct temp_sensor_2 *temp;
270 	struct occ *occ = dev_get_drvdata(dev);
271 	struct occ_sensors *sensors = &occ->sensors;
272 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
273 
274 	rc = occ_update_response(occ);
275 	if (rc)
276 		return rc;
277 
278 	temp = ((struct temp_sensor_2 *)sensors->temp.data) + sattr->index;
279 
280 	switch (sattr->nr) {
281 	case 0:
282 		val = get_unaligned_be32(&temp->sensor_id);
283 		break;
284 	case 1:
285 		val = temp->value;
286 		if (val == OCC_TEMP_SENSOR_FAULT)
287 			return -EREMOTEIO;
288 
289 		/*
290 		 * VRM doesn't return temperature, only alarm bit. This
291 		 * attribute maps to tempX_alarm instead of tempX_input for
292 		 * VRM
293 		 */
294 		if (temp->fru_type != OCC_FRU_TYPE_VRM) {
295 			/* sensor not ready */
296 			if (val == 0)
297 				return -EAGAIN;
298 
299 			val *= 1000;
300 		}
301 		break;
302 	case 2:
303 		val = temp->fru_type;
304 		break;
305 	case 3:
306 		val = temp->value == OCC_TEMP_SENSOR_FAULT;
307 		break;
308 	default:
309 		return -EINVAL;
310 	}
311 
312 	return sysfs_emit(buf, "%u\n", val);
313 }
314 
315 static ssize_t occ_show_temp_10(struct device *dev,
316 				struct device_attribute *attr, char *buf)
317 {
318 	int rc;
319 	u32 val = 0;
320 	struct temp_sensor_10 *temp;
321 	struct occ *occ = dev_get_drvdata(dev);
322 	struct occ_sensors *sensors = &occ->sensors;
323 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
324 
325 	rc = occ_update_response(occ);
326 	if (rc)
327 		return rc;
328 
329 	temp = ((struct temp_sensor_10 *)sensors->temp.data) + sattr->index;
330 
331 	switch (sattr->nr) {
332 	case 0:
333 		val = get_unaligned_be32(&temp->sensor_id);
334 		break;
335 	case 1:
336 		val = temp->value;
337 		if (val == OCC_TEMP_SENSOR_FAULT)
338 			return -EREMOTEIO;
339 
340 		/* sensor not ready */
341 		if (val == 0)
342 			return -EAGAIN;
343 
344 		val *= 1000;
345 		break;
346 	case 2:
347 		val = temp->fru_type;
348 		break;
349 	case 3:
350 		val = temp->value == OCC_TEMP_SENSOR_FAULT;
351 		break;
352 	case 4:
353 		val = temp->throttle * 1000;
354 		break;
355 	default:
356 		return -EINVAL;
357 	}
358 
359 	return sysfs_emit(buf, "%u\n", val);
360 }
361 
362 static ssize_t occ_show_freq_1(struct device *dev,
363 			       struct device_attribute *attr, char *buf)
364 {
365 	int rc;
366 	u16 val = 0;
367 	struct freq_sensor_1 *freq;
368 	struct occ *occ = dev_get_drvdata(dev);
369 	struct occ_sensors *sensors = &occ->sensors;
370 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
371 
372 	rc = occ_update_response(occ);
373 	if (rc)
374 		return rc;
375 
376 	freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index;
377 
378 	switch (sattr->nr) {
379 	case 0:
380 		val = get_unaligned_be16(&freq->sensor_id);
381 		break;
382 	case 1:
383 		val = get_unaligned_be16(&freq->value);
384 		break;
385 	default:
386 		return -EINVAL;
387 	}
388 
389 	return sysfs_emit(buf, "%u\n", val);
390 }
391 
392 static ssize_t occ_show_freq_2(struct device *dev,
393 			       struct device_attribute *attr, char *buf)
394 {
395 	int rc;
396 	u32 val = 0;
397 	struct freq_sensor_2 *freq;
398 	struct occ *occ = dev_get_drvdata(dev);
399 	struct occ_sensors *sensors = &occ->sensors;
400 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
401 
402 	rc = occ_update_response(occ);
403 	if (rc)
404 		return rc;
405 
406 	freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index;
407 
408 	switch (sattr->nr) {
409 	case 0:
410 		val = get_unaligned_be32(&freq->sensor_id);
411 		break;
412 	case 1:
413 		val = get_unaligned_be16(&freq->value);
414 		break;
415 	default:
416 		return -EINVAL;
417 	}
418 
419 	return sysfs_emit(buf, "%u\n", val);
420 }
421 
422 static ssize_t occ_show_power_1(struct device *dev,
423 				struct device_attribute *attr, char *buf)
424 {
425 	int rc;
426 	u64 val = 0;
427 	struct power_sensor_1 *power;
428 	struct occ *occ = dev_get_drvdata(dev);
429 	struct occ_sensors *sensors = &occ->sensors;
430 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
431 
432 	rc = occ_update_response(occ);
433 	if (rc)
434 		return rc;
435 
436 	power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index;
437 
438 	switch (sattr->nr) {
439 	case 0:
440 		val = get_unaligned_be16(&power->sensor_id);
441 		break;
442 	case 1:
443 		val = get_unaligned_be32(&power->accumulator) /
444 			get_unaligned_be32(&power->update_tag);
445 		val *= 1000000ULL;
446 		break;
447 	case 2:
448 		val = (u64)get_unaligned_be32(&power->update_tag) *
449 			   occ->powr_sample_time_us;
450 		break;
451 	case 3:
452 		val = get_unaligned_be16(&power->value) * 1000000ULL;
453 		break;
454 	default:
455 		return -EINVAL;
456 	}
457 
458 	return sysfs_emit(buf, "%llu\n", val);
459 }
460 
461 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
462 {
463 	u64 divisor = get_unaligned_be32(samples);
464 
465 	return (divisor == 0) ? 0 :
466 		div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
467 }
468 
469 static ssize_t occ_show_power_2(struct device *dev,
470 				struct device_attribute *attr, char *buf)
471 {
472 	int rc;
473 	u64 val = 0;
474 	struct power_sensor_2 *power;
475 	struct occ *occ = dev_get_drvdata(dev);
476 	struct occ_sensors *sensors = &occ->sensors;
477 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
478 
479 	rc = occ_update_response(occ);
480 	if (rc)
481 		return rc;
482 
483 	power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index;
484 
485 	switch (sattr->nr) {
486 	case 0:
487 		return sysfs_emit(buf, "%u_%u_%u\n",
488 				  get_unaligned_be32(&power->sensor_id),
489 				  power->function_id, power->apss_channel);
490 	case 1:
491 		val = occ_get_powr_avg(&power->accumulator,
492 				       &power->update_tag);
493 		break;
494 	case 2:
495 		val = (u64)get_unaligned_be32(&power->update_tag) *
496 			   occ->powr_sample_time_us;
497 		break;
498 	case 3:
499 		val = get_unaligned_be16(&power->value) * 1000000ULL;
500 		break;
501 	default:
502 		return -EINVAL;
503 	}
504 
505 	return sysfs_emit(buf, "%llu\n", val);
506 }
507 
508 static ssize_t occ_show_power_a0(struct device *dev,
509 				 struct device_attribute *attr, char *buf)
510 {
511 	int rc;
512 	u64 val = 0;
513 	struct power_sensor_a0 *power;
514 	struct occ *occ = dev_get_drvdata(dev);
515 	struct occ_sensors *sensors = &occ->sensors;
516 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
517 
518 	rc = occ_update_response(occ);
519 	if (rc)
520 		return rc;
521 
522 	power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index;
523 
524 	switch (sattr->nr) {
525 	case 0:
526 		return sysfs_emit(buf, "%u_system\n",
527 				  get_unaligned_be32(&power->sensor_id));
528 	case 1:
529 		val = occ_get_powr_avg(&power->system.accumulator,
530 				       &power->system.update_tag);
531 		break;
532 	case 2:
533 		val = (u64)get_unaligned_be32(&power->system.update_tag) *
534 			   occ->powr_sample_time_us;
535 		break;
536 	case 3:
537 		val = get_unaligned_be16(&power->system.value) * 1000000ULL;
538 		break;
539 	case 4:
540 		return sysfs_emit(buf, "%u_proc\n",
541 				  get_unaligned_be32(&power->sensor_id));
542 	case 5:
543 		val = occ_get_powr_avg(&power->proc.accumulator,
544 				       &power->proc.update_tag);
545 		break;
546 	case 6:
547 		val = (u64)get_unaligned_be32(&power->proc.update_tag) *
548 			   occ->powr_sample_time_us;
549 		break;
550 	case 7:
551 		val = get_unaligned_be16(&power->proc.value) * 1000000ULL;
552 		break;
553 	case 8:
554 		return sysfs_emit(buf, "%u_vdd\n",
555 				  get_unaligned_be32(&power->sensor_id));
556 	case 9:
557 		val = occ_get_powr_avg(&power->vdd.accumulator,
558 				       &power->vdd.update_tag);
559 		break;
560 	case 10:
561 		val = (u64)get_unaligned_be32(&power->vdd.update_tag) *
562 			   occ->powr_sample_time_us;
563 		break;
564 	case 11:
565 		val = get_unaligned_be16(&power->vdd.value) * 1000000ULL;
566 		break;
567 	case 12:
568 		return sysfs_emit(buf, "%u_vdn\n",
569 				  get_unaligned_be32(&power->sensor_id));
570 	case 13:
571 		val = occ_get_powr_avg(&power->vdn.accumulator,
572 				       &power->vdn.update_tag);
573 		break;
574 	case 14:
575 		val = (u64)get_unaligned_be32(&power->vdn.update_tag) *
576 			   occ->powr_sample_time_us;
577 		break;
578 	case 15:
579 		val = get_unaligned_be16(&power->vdn.value) * 1000000ULL;
580 		break;
581 	default:
582 		return -EINVAL;
583 	}
584 
585 	return sysfs_emit(buf, "%llu\n", val);
586 }
587 
588 static ssize_t occ_show_caps_1_2(struct device *dev,
589 				 struct device_attribute *attr, char *buf)
590 {
591 	int rc;
592 	u64 val = 0;
593 	struct caps_sensor_2 *caps;
594 	struct occ *occ = dev_get_drvdata(dev);
595 	struct occ_sensors *sensors = &occ->sensors;
596 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
597 
598 	rc = occ_update_response(occ);
599 	if (rc)
600 		return rc;
601 
602 	caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index;
603 
604 	switch (sattr->nr) {
605 	case 0:
606 		return sysfs_emit(buf, "system\n");
607 	case 1:
608 		val = get_unaligned_be16(&caps->cap) * 1000000ULL;
609 		break;
610 	case 2:
611 		val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
612 		break;
613 	case 3:
614 		val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
615 		break;
616 	case 4:
617 		val = get_unaligned_be16(&caps->max) * 1000000ULL;
618 		break;
619 	case 5:
620 		val = get_unaligned_be16(&caps->min) * 1000000ULL;
621 		break;
622 	case 6:
623 		val = get_unaligned_be16(&caps->user) * 1000000ULL;
624 		break;
625 	case 7:
626 		if (occ->sensors.caps.version == 1)
627 			return -EINVAL;
628 
629 		val = caps->user_source;
630 		break;
631 	default:
632 		return -EINVAL;
633 	}
634 
635 	return sysfs_emit(buf, "%llu\n", val);
636 }
637 
638 static ssize_t occ_show_caps_3(struct device *dev,
639 			       struct device_attribute *attr, char *buf)
640 {
641 	int rc;
642 	u64 val = 0;
643 	struct caps_sensor_3 *caps;
644 	struct occ *occ = dev_get_drvdata(dev);
645 	struct occ_sensors *sensors = &occ->sensors;
646 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
647 
648 	rc = occ_update_response(occ);
649 	if (rc)
650 		return rc;
651 
652 	caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index;
653 
654 	switch (sattr->nr) {
655 	case 0:
656 		return sysfs_emit(buf, "system\n");
657 	case 1:
658 		val = get_unaligned_be16(&caps->cap) * 1000000ULL;
659 		break;
660 	case 2:
661 		val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
662 		break;
663 	case 3:
664 		val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
665 		break;
666 	case 4:
667 		val = get_unaligned_be16(&caps->max) * 1000000ULL;
668 		break;
669 	case 5:
670 		val = get_unaligned_be16(&caps->hard_min) * 1000000ULL;
671 		break;
672 	case 6:
673 		val = get_unaligned_be16(&caps->user) * 1000000ULL;
674 		break;
675 	case 7:
676 		val = caps->user_source;
677 		break;
678 	case 8:
679 		val = get_unaligned_be16(&caps->soft_min) * 1000000ULL;
680 		break;
681 	default:
682 		return -EINVAL;
683 	}
684 
685 	return sysfs_emit(buf, "%llu\n", val);
686 }
687 
688 static 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 
710 static 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, "%4phN\n", extn->name);
733 		}
734 		break;
735 	case 1:
736 		rc = sysfs_emit(buf, "%02x\n", extn->flags);
737 		break;
738 	case 2:
739 		rc = sysfs_emit(buf, "%6phN\n", extn->data);
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 2:
839 		num_attrs += (sensors->caps.num_sensors * 8);
840 		break;
841 	case 3:
842 		show_caps = occ_show_caps_3;
843 		num_attrs += (sensors->caps.num_sensors * 9);
844 		break;
845 	default:
846 		sensors->caps.num_sensors = 0;
847 	}
848 
849 	switch (sensors->extended.version) {
850 	case 1:
851 		num_attrs += (sensors->extended.num_sensors * 3);
852 		break;
853 	default:
854 		sensors->extended.num_sensors = 0;
855 	}
856 
857 	occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
858 				  GFP_KERNEL);
859 	if (!occ->attrs)
860 		return -ENOMEM;
861 
862 	/* null-terminated list */
863 	occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) *
864 					num_attrs + 1, GFP_KERNEL);
865 	if (!occ->group.attrs)
866 		return -ENOMEM;
867 
868 	attr = occ->attrs;
869 
870 	for (i = 0; i < sensors->temp.num_sensors; ++i) {
871 		s = i + 1;
872 		temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
873 
874 		snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
875 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
876 					     0, i);
877 		attr++;
878 
879 		if (sensors->temp.version == 2 &&
880 		    temp->fru_type == OCC_FRU_TYPE_VRM) {
881 			snprintf(attr->name, sizeof(attr->name),
882 				 "temp%d_alarm", s);
883 		} else {
884 			snprintf(attr->name, sizeof(attr->name),
885 				 "temp%d_input", s);
886 		}
887 
888 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
889 					     1, i);
890 		attr++;
891 
892 		if (sensors->temp.version > 1) {
893 			snprintf(attr->name, sizeof(attr->name),
894 				 "temp%d_fru_type", s);
895 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
896 						     show_temp, NULL, 2, i);
897 			attr++;
898 
899 			snprintf(attr->name, sizeof(attr->name),
900 				 "temp%d_fault", s);
901 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
902 						     show_temp, NULL, 3, i);
903 			attr++;
904 
905 			if (sensors->temp.version == 0x10) {
906 				snprintf(attr->name, sizeof(attr->name),
907 					 "temp%d_max", s);
908 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
909 							     show_temp, NULL,
910 							     4, i);
911 				attr++;
912 			}
913 		}
914 	}
915 
916 	for (i = 0; i < sensors->freq.num_sensors; ++i) {
917 		s = i + 1;
918 
919 		snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
920 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
921 					     0, i);
922 		attr++;
923 
924 		snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
925 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
926 					     1, i);
927 		attr++;
928 	}
929 
930 	if (sensors->power.version == 0xA0) {
931 		/*
932 		 * Special case for many-attribute power sensor. Split it into
933 		 * a sensor number per power type, emulating several sensors.
934 		 */
935 		for (i = 0; i < sensors->power.num_sensors; ++i) {
936 			unsigned int j;
937 			unsigned int nr = 0;
938 
939 			s = (i * 4) + 1;
940 
941 			for (j = 0; j < 4; ++j) {
942 				snprintf(attr->name, sizeof(attr->name),
943 					 "power%d_label", s);
944 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
945 							     show_power, NULL,
946 							     nr++, i);
947 				attr++;
948 
949 				snprintf(attr->name, sizeof(attr->name),
950 					 "power%d_average", s);
951 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
952 							     show_power, NULL,
953 							     nr++, i);
954 				attr++;
955 
956 				snprintf(attr->name, sizeof(attr->name),
957 					 "power%d_average_interval", s);
958 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
959 							     show_power, NULL,
960 							     nr++, i);
961 				attr++;
962 
963 				snprintf(attr->name, sizeof(attr->name),
964 					 "power%d_input", s);
965 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
966 							     show_power, NULL,
967 							     nr++, i);
968 				attr++;
969 
970 				s++;
971 			}
972 		}
973 
974 		s = (sensors->power.num_sensors * 4) + 1;
975 	} else {
976 		for (i = 0; i < sensors->power.num_sensors; ++i) {
977 			s = i + 1;
978 
979 			snprintf(attr->name, sizeof(attr->name),
980 				 "power%d_label", s);
981 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
982 						     show_power, NULL, 0, i);
983 			attr++;
984 
985 			snprintf(attr->name, sizeof(attr->name),
986 				 "power%d_average", s);
987 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
988 						     show_power, NULL, 1, i);
989 			attr++;
990 
991 			snprintf(attr->name, sizeof(attr->name),
992 				 "power%d_average_interval", s);
993 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
994 						     show_power, NULL, 2, i);
995 			attr++;
996 
997 			snprintf(attr->name, sizeof(attr->name),
998 				 "power%d_input", s);
999 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1000 						     show_power, NULL, 3, i);
1001 			attr++;
1002 		}
1003 
1004 		s = sensors->power.num_sensors + 1;
1005 	}
1006 
1007 	if (sensors->caps.num_sensors >= 1) {
1008 		snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
1009 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1010 					     0, 0);
1011 		attr++;
1012 
1013 		snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
1014 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1015 					     1, 0);
1016 		attr++;
1017 
1018 		snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
1019 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1020 					     2, 0);
1021 		attr++;
1022 
1023 		snprintf(attr->name, sizeof(attr->name),
1024 			 "power%d_cap_not_redundant", s);
1025 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1026 					     3, 0);
1027 		attr++;
1028 
1029 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
1030 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1031 					     4, 0);
1032 		attr++;
1033 
1034 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
1035 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1036 					     5, 0);
1037 		attr++;
1038 
1039 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
1040 			 s);
1041 		attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
1042 					     occ_store_caps_user, 6, 0);
1043 		attr++;
1044 
1045 		if (sensors->caps.version > 1) {
1046 			snprintf(attr->name, sizeof(attr->name),
1047 				 "power%d_cap_user_source", s);
1048 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1049 						     show_caps, NULL, 7, 0);
1050 			attr++;
1051 
1052 			if (sensors->caps.version > 2) {
1053 				snprintf(attr->name, sizeof(attr->name),
1054 					 "power%d_cap_min_soft", s);
1055 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1056 							     show_caps, NULL,
1057 							     8, 0);
1058 				attr++;
1059 			}
1060 		}
1061 	}
1062 
1063 	for (i = 0; i < sensors->extended.num_sensors; ++i) {
1064 		s = i + 1;
1065 
1066 		snprintf(attr->name, sizeof(attr->name), "extn%d_label", s);
1067 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1068 					     occ_show_extended, NULL, 0, i);
1069 		attr++;
1070 
1071 		snprintf(attr->name, sizeof(attr->name), "extn%d_flags", s);
1072 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1073 					     occ_show_extended, NULL, 1, i);
1074 		attr++;
1075 
1076 		snprintf(attr->name, sizeof(attr->name), "extn%d_input", s);
1077 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1078 					     occ_show_extended, NULL, 2, i);
1079 		attr++;
1080 	}
1081 
1082 	/* put the sensors in the group */
1083 	for (i = 0; i < num_attrs; ++i) {
1084 		sysfs_attr_init(&occ->attrs[i].sensor.dev_attr.attr);
1085 		occ->group.attrs[i] = &occ->attrs[i].sensor.dev_attr.attr;
1086 	}
1087 
1088 	return 0;
1089 }
1090 
1091 /* only need to do this once at startup, as OCC won't change sensors on us */
1092 static void occ_parse_poll_response(struct occ *occ)
1093 {
1094 	unsigned int i, old_offset, offset = 0, size = 0;
1095 	struct occ_sensor *sensor;
1096 	struct occ_sensors *sensors = &occ->sensors;
1097 	struct occ_response *resp = &occ->resp;
1098 	struct occ_poll_response *poll =
1099 		(struct occ_poll_response *)&resp->data[0];
1100 	struct occ_poll_response_header *header = &poll->header;
1101 	struct occ_sensor_data_block *block = &poll->block;
1102 
1103 	dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
1104 		 header->occ_code_level);
1105 
1106 	for (i = 0; i < header->num_sensor_data_blocks; ++i) {
1107 		block = (struct occ_sensor_data_block *)((u8 *)block + offset);
1108 		old_offset = offset;
1109 		offset = (block->header.num_sensors *
1110 			  block->header.sensor_length) + sizeof(block->header);
1111 		size += offset;
1112 
1113 		/* validate all the length/size fields */
1114 		if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) {
1115 			dev_warn(occ->bus_dev, "exceeded response buffer\n");
1116 			return;
1117 		}
1118 
1119 		dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
1120 			old_offset, offset - 1, block->header.eye_catcher,
1121 			block->header.num_sensors);
1122 
1123 		/* match sensor block type */
1124 		if (strncmp(block->header.eye_catcher, "TEMP", 4) == 0)
1125 			sensor = &sensors->temp;
1126 		else if (strncmp(block->header.eye_catcher, "FREQ", 4) == 0)
1127 			sensor = &sensors->freq;
1128 		else if (strncmp(block->header.eye_catcher, "POWR", 4) == 0)
1129 			sensor = &sensors->power;
1130 		else if (strncmp(block->header.eye_catcher, "CAPS", 4) == 0)
1131 			sensor = &sensors->caps;
1132 		else if (strncmp(block->header.eye_catcher, "EXTN", 4) == 0)
1133 			sensor = &sensors->extended;
1134 		else {
1135 			dev_warn(occ->bus_dev, "sensor not supported %.4s\n",
1136 				 block->header.eye_catcher);
1137 			continue;
1138 		}
1139 
1140 		sensor->num_sensors = block->header.num_sensors;
1141 		sensor->version = block->header.sensor_format;
1142 		sensor->data = &block->data;
1143 	}
1144 
1145 	dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
1146 		sizeof(*header), size + sizeof(*header));
1147 }
1148 
1149 int occ_active(struct occ *occ, bool active)
1150 {
1151 	int rc = mutex_lock_interruptible(&occ->lock);
1152 
1153 	if (rc)
1154 		return rc;
1155 
1156 	if (active) {
1157 		if (occ->active) {
1158 			rc = -EALREADY;
1159 			goto unlock;
1160 		}
1161 
1162 		occ->error_count = 0;
1163 		occ->last_safe = 0;
1164 
1165 		rc = occ_poll(occ);
1166 		if (rc < 0) {
1167 			dev_err(occ->bus_dev,
1168 				"failed to get OCC poll response=%02x: %d\n",
1169 				occ->resp.return_status, rc);
1170 			goto unlock;
1171 		}
1172 
1173 		occ->active = true;
1174 		occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
1175 		occ_parse_poll_response(occ);
1176 
1177 		rc = occ_setup_sensor_attrs(occ);
1178 		if (rc) {
1179 			dev_err(occ->bus_dev,
1180 				"failed to setup sensor attrs: %d\n", rc);
1181 			goto unlock;
1182 		}
1183 
1184 		occ->hwmon = hwmon_device_register_with_groups(occ->bus_dev,
1185 							       "occ", occ,
1186 							       occ->groups);
1187 		if (IS_ERR(occ->hwmon)) {
1188 			rc = PTR_ERR(occ->hwmon);
1189 			occ->hwmon = NULL;
1190 			dev_err(occ->bus_dev,
1191 				"failed to register hwmon device: %d\n", rc);
1192 			goto unlock;
1193 		}
1194 	} else {
1195 		if (!occ->active) {
1196 			rc = -EALREADY;
1197 			goto unlock;
1198 		}
1199 
1200 		if (occ->hwmon)
1201 			hwmon_device_unregister(occ->hwmon);
1202 		occ->active = false;
1203 		occ->hwmon = NULL;
1204 	}
1205 
1206 unlock:
1207 	mutex_unlock(&occ->lock);
1208 	return rc;
1209 }
1210 
1211 int occ_setup(struct occ *occ)
1212 {
1213 	int rc;
1214 
1215 	mutex_init(&occ->lock);
1216 	occ->groups[0] = &occ->group;
1217 
1218 	rc = occ_setup_sysfs(occ);
1219 	if (rc)
1220 		dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);
1221 
1222 	return rc;
1223 }
1224 EXPORT_SYMBOL_GPL(occ_setup);
1225 
1226 void occ_shutdown(struct occ *occ)
1227 {
1228 	mutex_lock(&occ->lock);
1229 
1230 	occ_shutdown_sysfs(occ);
1231 
1232 	if (occ->hwmon)
1233 		hwmon_device_unregister(occ->hwmon);
1234 	occ->hwmon = NULL;
1235 
1236 	mutex_unlock(&occ->lock);
1237 }
1238 EXPORT_SYMBOL_GPL(occ_shutdown);
1239 
1240 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
1241 MODULE_DESCRIPTION("Common OCC hwmon code");
1242 MODULE_LICENSE("GPL");
1243