xref: /openbmc/linux/drivers/hwmon/occ/common.c (revision d3741027)
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 	case 8:
678 		val = get_unaligned_be16(&caps->soft_min) * 1000000ULL;
679 		break;
680 	default:
681 		return -EINVAL;
682 	}
683 
684 	return sysfs_emit(buf, "%llu\n", val);
685 }
686 
687 static ssize_t occ_store_caps_user(struct device *dev,
688 				   struct device_attribute *attr,
689 				   const char *buf, size_t count)
690 {
691 	int rc;
692 	u16 user_power_cap;
693 	unsigned long long value;
694 	struct occ *occ = dev_get_drvdata(dev);
695 
696 	rc = kstrtoull(buf, 0, &value);
697 	if (rc)
698 		return rc;
699 
700 	user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */
701 
702 	rc = occ_set_user_power_cap(occ, user_power_cap);
703 	if (rc)
704 		return rc;
705 
706 	return count;
707 }
708 
709 static ssize_t occ_show_extended(struct device *dev,
710 				 struct device_attribute *attr, char *buf)
711 {
712 	int rc;
713 	struct extended_sensor *extn;
714 	struct occ *occ = dev_get_drvdata(dev);
715 	struct occ_sensors *sensors = &occ->sensors;
716 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
717 
718 	rc = occ_update_response(occ);
719 	if (rc)
720 		return rc;
721 
722 	extn = ((struct extended_sensor *)sensors->extended.data) +
723 		sattr->index;
724 
725 	switch (sattr->nr) {
726 	case 0:
727 		if (extn->flags & EXTN_FLAG_SENSOR_ID) {
728 			rc = sysfs_emit(buf, "%u",
729 					get_unaligned_be32(&extn->sensor_id));
730 		} else {
731 			rc = sysfs_emit(buf, "%02x%02x%02x%02x\n",
732 					extn->name[0], extn->name[1],
733 					extn->name[2], extn->name[3]);
734 		}
735 		break;
736 	case 1:
737 		rc = sysfs_emit(buf, "%02x\n", extn->flags);
738 		break;
739 	case 2:
740 		rc = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
741 				extn->data[0], extn->data[1], extn->data[2],
742 				extn->data[3], extn->data[4], extn->data[5]);
743 		break;
744 	default:
745 		return -EINVAL;
746 	}
747 
748 	return rc;
749 }
750 
751 /*
752  * Some helper macros to make it easier to define an occ_attribute. Since these
753  * are dynamically allocated, we shouldn't use the existing kernel macros which
754  * stringify the name argument.
755  */
756 #define ATTR_OCC(_name, _mode, _show, _store) {				\
757 	.attr	= {							\
758 		.name = _name,						\
759 		.mode = VERIFY_OCTAL_PERMISSIONS(_mode),		\
760 	},								\
761 	.show	= _show,						\
762 	.store	= _store,						\
763 }
764 
765 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) {	\
766 	.dev_attr	= ATTR_OCC(_name, _mode, _show, _store),	\
767 	.index		= _index,					\
768 	.nr		= _nr,						\
769 }
770 
771 #define OCC_INIT_ATTR(_name, _mode, _show, _store, _nr, _index)		\
772 	((struct sensor_device_attribute_2)				\
773 		SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index))
774 
775 /*
776  * Allocate and instatiate sensor_device_attribute_2s. It's most efficient to
777  * use our own instead of the built-in hwmon attribute types.
778  */
779 static int occ_setup_sensor_attrs(struct occ *occ)
780 {
781 	unsigned int i, s, num_attrs = 0;
782 	struct device *dev = occ->bus_dev;
783 	struct occ_sensors *sensors = &occ->sensors;
784 	struct occ_attribute *attr;
785 	struct temp_sensor_2 *temp;
786 	ssize_t (*show_temp)(struct device *, struct device_attribute *,
787 			     char *) = occ_show_temp_1;
788 	ssize_t (*show_freq)(struct device *, struct device_attribute *,
789 			     char *) = occ_show_freq_1;
790 	ssize_t (*show_power)(struct device *, struct device_attribute *,
791 			      char *) = occ_show_power_1;
792 	ssize_t (*show_caps)(struct device *, struct device_attribute *,
793 			     char *) = occ_show_caps_1_2;
794 
795 	switch (sensors->temp.version) {
796 	case 1:
797 		num_attrs += (sensors->temp.num_sensors * 2);
798 		break;
799 	case 2:
800 		num_attrs += (sensors->temp.num_sensors * 4);
801 		show_temp = occ_show_temp_2;
802 		break;
803 	case 0x10:
804 		num_attrs += (sensors->temp.num_sensors * 5);
805 		show_temp = occ_show_temp_10;
806 		break;
807 	default:
808 		sensors->temp.num_sensors = 0;
809 	}
810 
811 	switch (sensors->freq.version) {
812 	case 2:
813 		show_freq = occ_show_freq_2;
814 		fallthrough;
815 	case 1:
816 		num_attrs += (sensors->freq.num_sensors * 2);
817 		break;
818 	default:
819 		sensors->freq.num_sensors = 0;
820 	}
821 
822 	switch (sensors->power.version) {
823 	case 2:
824 		show_power = occ_show_power_2;
825 		fallthrough;
826 	case 1:
827 		num_attrs += (sensors->power.num_sensors * 4);
828 		break;
829 	case 0xA0:
830 		num_attrs += (sensors->power.num_sensors * 16);
831 		show_power = occ_show_power_a0;
832 		break;
833 	default:
834 		sensors->power.num_sensors = 0;
835 	}
836 
837 	switch (sensors->caps.version) {
838 	case 1:
839 		num_attrs += (sensors->caps.num_sensors * 7);
840 		break;
841 	case 2:
842 		num_attrs += (sensors->caps.num_sensors * 8);
843 		break;
844 	case 3:
845 		show_caps = occ_show_caps_3;
846 		num_attrs += (sensors->caps.num_sensors * 9);
847 		break;
848 	default:
849 		sensors->caps.num_sensors = 0;
850 	}
851 
852 	switch (sensors->extended.version) {
853 	case 1:
854 		num_attrs += (sensors->extended.num_sensors * 3);
855 		break;
856 	default:
857 		sensors->extended.num_sensors = 0;
858 	}
859 
860 	occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
861 				  GFP_KERNEL);
862 	if (!occ->attrs)
863 		return -ENOMEM;
864 
865 	/* null-terminated list */
866 	occ->group.attrs = devm_kzalloc(dev, sizeof(*occ->group.attrs) *
867 					num_attrs + 1, GFP_KERNEL);
868 	if (!occ->group.attrs)
869 		return -ENOMEM;
870 
871 	attr = occ->attrs;
872 
873 	for (i = 0; i < sensors->temp.num_sensors; ++i) {
874 		s = i + 1;
875 		temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
876 
877 		snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
878 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
879 					     0, i);
880 		attr++;
881 
882 		if (sensors->temp.version == 2 &&
883 		    temp->fru_type == OCC_FRU_TYPE_VRM) {
884 			snprintf(attr->name, sizeof(attr->name),
885 				 "temp%d_alarm", s);
886 		} else {
887 			snprintf(attr->name, sizeof(attr->name),
888 				 "temp%d_input", s);
889 		}
890 
891 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
892 					     1, i);
893 		attr++;
894 
895 		if (sensors->temp.version > 1) {
896 			snprintf(attr->name, sizeof(attr->name),
897 				 "temp%d_fru_type", s);
898 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
899 						     show_temp, NULL, 2, i);
900 			attr++;
901 
902 			snprintf(attr->name, sizeof(attr->name),
903 				 "temp%d_fault", s);
904 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
905 						     show_temp, NULL, 3, i);
906 			attr++;
907 
908 			if (sensors->temp.version == 0x10) {
909 				snprintf(attr->name, sizeof(attr->name),
910 					 "temp%d_max", s);
911 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
912 							     show_temp, NULL,
913 							     4, i);
914 				attr++;
915 			}
916 		}
917 	}
918 
919 	for (i = 0; i < sensors->freq.num_sensors; ++i) {
920 		s = i + 1;
921 
922 		snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
923 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
924 					     0, i);
925 		attr++;
926 
927 		snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
928 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
929 					     1, i);
930 		attr++;
931 	}
932 
933 	if (sensors->power.version == 0xA0) {
934 		/*
935 		 * Special case for many-attribute power sensor. Split it into
936 		 * a sensor number per power type, emulating several sensors.
937 		 */
938 		for (i = 0; i < sensors->power.num_sensors; ++i) {
939 			unsigned int j;
940 			unsigned int nr = 0;
941 
942 			s = (i * 4) + 1;
943 
944 			for (j = 0; j < 4; ++j) {
945 				snprintf(attr->name, sizeof(attr->name),
946 					 "power%d_label", s);
947 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
948 							     show_power, NULL,
949 							     nr++, i);
950 				attr++;
951 
952 				snprintf(attr->name, sizeof(attr->name),
953 					 "power%d_average", s);
954 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
955 							     show_power, NULL,
956 							     nr++, i);
957 				attr++;
958 
959 				snprintf(attr->name, sizeof(attr->name),
960 					 "power%d_average_interval", s);
961 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
962 							     show_power, NULL,
963 							     nr++, i);
964 				attr++;
965 
966 				snprintf(attr->name, sizeof(attr->name),
967 					 "power%d_input", s);
968 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
969 							     show_power, NULL,
970 							     nr++, i);
971 				attr++;
972 
973 				s++;
974 			}
975 		}
976 
977 		s = (sensors->power.num_sensors * 4) + 1;
978 	} else {
979 		for (i = 0; i < sensors->power.num_sensors; ++i) {
980 			s = i + 1;
981 
982 			snprintf(attr->name, sizeof(attr->name),
983 				 "power%d_label", s);
984 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
985 						     show_power, NULL, 0, i);
986 			attr++;
987 
988 			snprintf(attr->name, sizeof(attr->name),
989 				 "power%d_average", s);
990 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
991 						     show_power, NULL, 1, i);
992 			attr++;
993 
994 			snprintf(attr->name, sizeof(attr->name),
995 				 "power%d_average_interval", s);
996 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
997 						     show_power, NULL, 2, i);
998 			attr++;
999 
1000 			snprintf(attr->name, sizeof(attr->name),
1001 				 "power%d_input", s);
1002 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1003 						     show_power, NULL, 3, i);
1004 			attr++;
1005 		}
1006 
1007 		s = sensors->power.num_sensors + 1;
1008 	}
1009 
1010 	if (sensors->caps.num_sensors >= 1) {
1011 		snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
1012 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1013 					     0, 0);
1014 		attr++;
1015 
1016 		snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
1017 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1018 					     1, 0);
1019 		attr++;
1020 
1021 		snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
1022 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1023 					     2, 0);
1024 		attr++;
1025 
1026 		snprintf(attr->name, sizeof(attr->name),
1027 			 "power%d_cap_not_redundant", s);
1028 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1029 					     3, 0);
1030 		attr++;
1031 
1032 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
1033 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1034 					     4, 0);
1035 		attr++;
1036 
1037 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
1038 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
1039 					     5, 0);
1040 		attr++;
1041 
1042 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
1043 			 s);
1044 		attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
1045 					     occ_store_caps_user, 6, 0);
1046 		attr++;
1047 
1048 		if (sensors->caps.version > 1) {
1049 			snprintf(attr->name, sizeof(attr->name),
1050 				 "power%d_cap_user_source", s);
1051 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1052 						     show_caps, NULL, 7, 0);
1053 			attr++;
1054 
1055 			if (sensors->caps.version > 2) {
1056 				snprintf(attr->name, sizeof(attr->name),
1057 					 "power%d_cap_min_soft", s);
1058 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1059 							     show_caps, NULL,
1060 							     8, 0);
1061 				attr++;
1062 			}
1063 		}
1064 	}
1065 
1066 	for (i = 0; i < sensors->extended.num_sensors; ++i) {
1067 		s = i + 1;
1068 
1069 		snprintf(attr->name, sizeof(attr->name), "extn%d_label", s);
1070 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1071 					     occ_show_extended, NULL, 0, i);
1072 		attr++;
1073 
1074 		snprintf(attr->name, sizeof(attr->name), "extn%d_flags", s);
1075 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1076 					     occ_show_extended, NULL, 1, i);
1077 		attr++;
1078 
1079 		snprintf(attr->name, sizeof(attr->name), "extn%d_input", s);
1080 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
1081 					     occ_show_extended, NULL, 2, i);
1082 		attr++;
1083 	}
1084 
1085 	/* put the sensors in the group */
1086 	for (i = 0; i < num_attrs; ++i) {
1087 		sysfs_attr_init(&occ->attrs[i].sensor.dev_attr.attr);
1088 		occ->group.attrs[i] = &occ->attrs[i].sensor.dev_attr.attr;
1089 	}
1090 
1091 	return 0;
1092 }
1093 
1094 /* only need to do this once at startup, as OCC won't change sensors on us */
1095 static void occ_parse_poll_response(struct occ *occ)
1096 {
1097 	unsigned int i, old_offset, offset = 0, size = 0;
1098 	struct occ_sensor *sensor;
1099 	struct occ_sensors *sensors = &occ->sensors;
1100 	struct occ_response *resp = &occ->resp;
1101 	struct occ_poll_response *poll =
1102 		(struct occ_poll_response *)&resp->data[0];
1103 	struct occ_poll_response_header *header = &poll->header;
1104 	struct occ_sensor_data_block *block = &poll->block;
1105 
1106 	dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
1107 		 header->occ_code_level);
1108 
1109 	for (i = 0; i < header->num_sensor_data_blocks; ++i) {
1110 		block = (struct occ_sensor_data_block *)((u8 *)block + offset);
1111 		old_offset = offset;
1112 		offset = (block->header.num_sensors *
1113 			  block->header.sensor_length) + sizeof(block->header);
1114 		size += offset;
1115 
1116 		/* validate all the length/size fields */
1117 		if ((size + sizeof(*header)) >= OCC_RESP_DATA_BYTES) {
1118 			dev_warn(occ->bus_dev, "exceeded response buffer\n");
1119 			return;
1120 		}
1121 
1122 		dev_dbg(occ->bus_dev, " %04x..%04x: %.4s (%d sensors)\n",
1123 			old_offset, offset - 1, block->header.eye_catcher,
1124 			block->header.num_sensors);
1125 
1126 		/* match sensor block type */
1127 		if (strncmp(block->header.eye_catcher, "TEMP", 4) == 0)
1128 			sensor = &sensors->temp;
1129 		else if (strncmp(block->header.eye_catcher, "FREQ", 4) == 0)
1130 			sensor = &sensors->freq;
1131 		else if (strncmp(block->header.eye_catcher, "POWR", 4) == 0)
1132 			sensor = &sensors->power;
1133 		else if (strncmp(block->header.eye_catcher, "CAPS", 4) == 0)
1134 			sensor = &sensors->caps;
1135 		else if (strncmp(block->header.eye_catcher, "EXTN", 4) == 0)
1136 			sensor = &sensors->extended;
1137 		else {
1138 			dev_warn(occ->bus_dev, "sensor not supported %.4s\n",
1139 				 block->header.eye_catcher);
1140 			continue;
1141 		}
1142 
1143 		sensor->num_sensors = block->header.num_sensors;
1144 		sensor->version = block->header.sensor_format;
1145 		sensor->data = &block->data;
1146 	}
1147 
1148 	dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
1149 		sizeof(*header), size + sizeof(*header));
1150 }
1151 
1152 int occ_active(struct occ *occ, bool active)
1153 {
1154 	int rc = mutex_lock_interruptible(&occ->lock);
1155 
1156 	if (rc)
1157 		return rc;
1158 
1159 	if (active) {
1160 		if (occ->active) {
1161 			rc = -EALREADY;
1162 			goto unlock;
1163 		}
1164 
1165 		occ->error_count = 0;
1166 		occ->last_safe = 0;
1167 
1168 		rc = occ_poll(occ);
1169 		if (rc < 0) {
1170 			dev_err(occ->bus_dev,
1171 				"failed to get OCC poll response=%02x: %d\n",
1172 				occ->resp.return_status, rc);
1173 			goto unlock;
1174 		}
1175 
1176 		occ->active = true;
1177 		occ->next_update = jiffies + OCC_UPDATE_FREQUENCY;
1178 		occ_parse_poll_response(occ);
1179 
1180 		rc = occ_setup_sensor_attrs(occ);
1181 		if (rc) {
1182 			dev_err(occ->bus_dev,
1183 				"failed to setup sensor attrs: %d\n", rc);
1184 			goto unlock;
1185 		}
1186 
1187 		occ->hwmon = hwmon_device_register_with_groups(occ->bus_dev,
1188 							       "occ", occ,
1189 							       occ->groups);
1190 		if (IS_ERR(occ->hwmon)) {
1191 			rc = PTR_ERR(occ->hwmon);
1192 			occ->hwmon = NULL;
1193 			dev_err(occ->bus_dev,
1194 				"failed to register hwmon device: %d\n", rc);
1195 			goto unlock;
1196 		}
1197 	} else {
1198 		if (!occ->active) {
1199 			rc = -EALREADY;
1200 			goto unlock;
1201 		}
1202 
1203 		if (occ->hwmon)
1204 			hwmon_device_unregister(occ->hwmon);
1205 		occ->active = false;
1206 		occ->hwmon = NULL;
1207 	}
1208 
1209 unlock:
1210 	mutex_unlock(&occ->lock);
1211 	return rc;
1212 }
1213 
1214 int occ_setup(struct occ *occ)
1215 {
1216 	int rc;
1217 
1218 	mutex_init(&occ->lock);
1219 	occ->groups[0] = &occ->group;
1220 
1221 	rc = occ_setup_sysfs(occ);
1222 	if (rc)
1223 		dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);
1224 
1225 	return rc;
1226 }
1227 EXPORT_SYMBOL_GPL(occ_setup);
1228 
1229 void occ_shutdown(struct occ *occ)
1230 {
1231 	mutex_lock(&occ->lock);
1232 
1233 	occ_shutdown_sysfs(occ);
1234 
1235 	if (occ->hwmon)
1236 		hwmon_device_unregister(occ->hwmon);
1237 	occ->hwmon = NULL;
1238 
1239 	mutex_unlock(&occ->lock);
1240 }
1241 EXPORT_SYMBOL_GPL(occ_shutdown);
1242 
1243 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
1244 MODULE_DESCRIPTION("Common OCC hwmon code");
1245 MODULE_LICENSE("GPL");
1246