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