32a629772f670b3e4dd2a03f605366620ba60fea
[openwrt/staging/ldir.git] /
1 From 5b46903d8bf372e563bf2150d46b87fff197a109 Mon Sep 17 00:00:00 2001
2 From: Guenter Roeck <linux@roeck-us.net>
3 Date: Thu, 28 Nov 2019 21:34:40 -0800
4 Subject: [PATCH] hwmon: Driver for disk and solid state drives with
5 temperature sensors
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 Reading the temperature of ATA drives has been supported for years
11 by userspace tools such as smarttools or hddtemp. The downside of
12 such tools is that they need to run with super-user privilege, that
13 the temperatures are not reported by standard tools such as 'sensors'
14 or 'libsensors', and that drive temperatures are not available for use
15 in the kernel's thermal subsystem.
16
17 This driver solves this problem by adding support for reading the
18 temperature of ATA drives from the kernel using the hwmon API and
19 by adding a temperature zone for each drive.
20
21 With this driver, the hard disk temperature can be read using the
22 unprivileged 'sensors' application:
23
24 $ sensors drivetemp-scsi-1-0
25 drivetemp-scsi-1-0
26 Adapter: SCSI adapter
27 temp1: +23.0°C
28
29 or directly from sysfs:
30
31 $ grep . /sys/class/hwmon/hwmon9/{name,temp1_input}
32 /sys/class/hwmon/hwmon9/name:drivetemp
33 /sys/class/hwmon/hwmon9/temp1_input:23000
34
35 If the drive supports SCT transport and reports temperature limits,
36 those are reported as well.
37
38 drivetemp-scsi-0-0
39 Adapter: SCSI adapter
40 temp1: +27.0°C (low = +0.0°C, high = +60.0°C)
41 (crit low = -41.0°C, crit = +85.0°C)
42 (lowest = +23.0°C, highest = +34.0°C)
43
44 The driver attempts to use SCT Command Transport to read the drive
45 temperature. If the SCT Command Transport feature set is not available,
46 or if it does not report the drive temperature, drive temperatures may
47 be readable through SMART attributes. Since SMART attributes are not well
48 defined, this method is only used as fallback mechanism.
49
50 Cc: Chris Healy <cphealy@gmail.com>
51 Cc: Linus Walleij <linus.walleij@linaro.org>
52 Cc: Martin K. Petersen <martin.petersen@oracle.com>
53 Cc: Bart Van Assche <bvanassche@acm.org>
54 Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
55 Tested-by: Linus Walleij <linus.walleij@linaro.org>
56 Signed-off-by: Guenter Roeck <linux@roeck-us.net>
57 ---
58 Documentation/hwmon/drivetemp.rst | 52 +++
59 Documentation/hwmon/index.rst | 1 +
60 drivers/hwmon/Kconfig | 10 +
61 drivers/hwmon/Makefile | 1 +
62 drivers/hwmon/drivetemp.c | 574 ++++++++++++++++++++++++++++++
63 5 files changed, 638 insertions(+)
64 create mode 100644 Documentation/hwmon/drivetemp.rst
65 create mode 100644 drivers/hwmon/drivetemp.c
66
67 --- /dev/null
68 +++ b/Documentation/hwmon/drivetemp.rst
69 @@ -0,0 +1,52 @@
70 +.. SPDX-License-Identifier: GPL-2.0
71 +
72 +Kernel driver drivetemp
73 +=======================
74 +
75 +
76 +References
77 +----------
78 +
79 +ANS T13/1699-D
80 +Information technology - AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
81 +
82 +ANS Project T10/BSR INCITS 513
83 +Information technology - SCSI Primary Commands - 4 (SPC-4)
84 +
85 +ANS Project INCITS 557
86 +Information technology - SCSI / ATA Translation - 5 (SAT-5)
87 +
88 +
89 +Description
90 +-----------
91 +
92 +This driver supports reporting the temperature of disk and solid state
93 +drives with temperature sensors.
94 +
95 +If supported, it uses the ATA SCT Command Transport feature to read
96 +the current drive temperature and, if available, temperature limits
97 +as well as historic minimum and maximum temperatures. If SCT Command
98 +Transport is not supported, the driver uses SMART attributes to read
99 +the drive temperature.
100 +
101 +
102 +Sysfs entries
103 +-------------
104 +
105 +Only the temp1_input attribute is always available. Other attributes are
106 +available only if reported by the drive. All temperatures are reported in
107 +milli-degrees Celsius.
108 +
109 +======================= =====================================================
110 +temp1_input Current drive temperature
111 +temp1_lcrit Minimum temperature limit. Operating the device below
112 + this temperature may cause physical damage to the
113 + device.
114 +temp1_min Minimum recommended continuous operating limit
115 +temp1_max Maximum recommended continuous operating temperature
116 +temp1_crit Maximum temperature limit. Operating the device above
117 + this temperature may cause physical damage to the
118 + device.
119 +temp1_lowest Minimum temperature seen this power cycle
120 +temp1_highest Maximum temperature seen this power cycle
121 +======================= =====================================================
122 --- a/Documentation/hwmon/index.rst
123 +++ b/Documentation/hwmon/index.rst
124 @@ -45,6 +45,7 @@ Hardware Monitoring Kernel Drivers
125 da9052
126 da9055
127 dme1737
128 + drivetemp
129 ds1621
130 ds620
131 emc1403
132 --- a/drivers/hwmon/Kconfig
133 +++ b/drivers/hwmon/Kconfig
134 @@ -385,6 +385,16 @@ config SENSORS_ATXP1
135 This driver can also be built as a module. If so, the module
136 will be called atxp1.
137
138 +config SENSORS_DRIVETEMP
139 + tristate "Hard disk drives with temperature sensors"
140 + depends on SCSI && ATA
141 + help
142 + If you say yes you get support for the temperature sensor on
143 + hard disk drives.
144 +
145 + This driver can also be built as a module. If so, the module
146 + will be called satatemp.
147 +
148 config SENSORS_DS620
149 tristate "Dallas Semiconductor DS620"
150 depends on I2C
151 --- a/drivers/hwmon/Makefile
152 +++ b/drivers/hwmon/Makefile
153 @@ -56,6 +56,7 @@ obj-$(CONFIG_SENSORS_DA9052_ADC)+= da905
154 obj-$(CONFIG_SENSORS_DA9055)+= da9055-hwmon.o
155 obj-$(CONFIG_SENSORS_DELL_SMM) += dell-smm-hwmon.o
156 obj-$(CONFIG_SENSORS_DME1737) += dme1737.o
157 +obj-$(CONFIG_SENSORS_DRIVETEMP) += drivetemp.o
158 obj-$(CONFIG_SENSORS_DS620) += ds620.o
159 obj-$(CONFIG_SENSORS_DS1621) += ds1621.o
160 obj-$(CONFIG_SENSORS_EMC1403) += emc1403.o
161 --- /dev/null
162 +++ b/drivers/hwmon/drivetemp.c
163 @@ -0,0 +1,574 @@
164 +// SPDX-License-Identifier: GPL-2.0
165 +/*
166 + * Hwmon client for disk and solid state drives with temperature sensors
167 + * Copyright (C) 2019 Zodiac Inflight Innovations
168 + *
169 + * With input from:
170 + * Hwmon client for S.M.A.R.T. hard disk drives with temperature sensors.
171 + * (C) 2018 Linus Walleij
172 + *
173 + * hwmon: Driver for SCSI/ATA temperature sensors
174 + * by Constantin Baranov <const@mimas.ru>, submitted September 2009
175 + *
176 + * This drive supports reporting the temperatire of SATA drives. It can be
177 + * easily extended to report the temperature of SCSI drives.
178 + *
179 + * The primary means to read drive temperatures and temperature limits
180 + * for ATA drives is the SCT Command Transport feature set as specified in
181 + * ATA8-ACS.
182 + * It can be used to read the current drive temperature, temperature limits,
183 + * and historic minimum and maximum temperatures. The SCT Command Transport
184 + * feature set is documented in "AT Attachment 8 - ATA/ATAPI Command Set
185 + * (ATA8-ACS)".
186 + *
187 + * If the SCT Command Transport feature set is not available, drive temperatures
188 + * may be readable through SMART attributes. Since SMART attributes are not well
189 + * defined, this method is only used as fallback mechanism.
190 + *
191 + * There are three SMART attributes which may report drive temperatures.
192 + * Those are defined as follows (from
193 + * http://www.cropel.com/library/smart-attribute-list.aspx).
194 + *
195 + * 190 Temperature Temperature, monitored by a sensor somewhere inside
196 + * the drive. Raw value typicaly holds the actual
197 + * temperature (hexadecimal) in its rightmost two digits.
198 + *
199 + * 194 Temperature Temperature, monitored by a sensor somewhere inside
200 + * the drive. Raw value typicaly holds the actual
201 + * temperature (hexadecimal) in its rightmost two digits.
202 + *
203 + * 231 Temperature Temperature, monitored by a sensor somewhere inside
204 + * the drive. Raw value typicaly holds the actual
205 + * temperature (hexadecimal) in its rightmost two digits.
206 + *
207 + * Wikipedia defines attributes a bit differently.
208 + *
209 + * 190 Temperature Value is equal to (100-temp. °C), allowing manufacturer
210 + * Difference or to set a minimum threshold which corresponds to a
211 + * Airflow maximum temperature. This also follows the convention of
212 + * Temperature 100 being a best-case value and lower values being
213 + * undesirable. However, some older drives may instead
214 + * report raw Temperature (identical to 0xC2) or
215 + * Temperature minus 50 here.
216 + * 194 Temperature or Indicates the device temperature, if the appropriate
217 + * Temperature sensor is fitted. Lowest byte of the raw value contains
218 + * Celsius the exact temperature value (Celsius degrees).
219 + * 231 Life Left Indicates the approximate SSD life left, in terms of
220 + * (SSDs) or program/erase cycles or available reserved blocks.
221 + * Temperature A normalized value of 100 represents a new drive, with
222 + * a threshold value at 10 indicating a need for
223 + * replacement. A value of 0 may mean that the drive is
224 + * operating in read-only mode to allow data recovery.
225 + * Previously (pre-2010) occasionally used for Drive
226 + * Temperature (more typically reported at 0xC2).
227 + *
228 + * Common denominator is that the first raw byte reports the temperature
229 + * in degrees C on almost all drives. Some drives may report a fractional
230 + * temperature in the second raw byte.
231 + *
232 + * Known exceptions (from libatasmart):
233 + * - SAMSUNG SV0412H and SAMSUNG SV1204H) report the temperature in 10th
234 + * degrees C in the first two raw bytes.
235 + * - A few Maxtor drives report an unknown or bad value in attribute 194.
236 + * - Certain Apple SSD drives report an unknown value in attribute 190.
237 + * Only certain firmware versions are affected.
238 + *
239 + * Those exceptions affect older ATA drives and are currently ignored.
240 + * Also, the second raw byte (possibly reporting the fractional temperature)
241 + * is currently ignored.
242 + *
243 + * Many drives also report temperature limits in additional SMART data raw
244 + * bytes. The format of those is not well defined and varies widely.
245 + * The driver does not currently attempt to report those limits.
246 + *
247 + * According to data in smartmontools, attribute 231 is rarely used to report
248 + * drive temperatures. At the same time, several drives report SSD life left
249 + * in attribute 231, but do not support temperature sensors. For this reason,
250 + * attribute 231 is currently ignored.
251 + *
252 + * Following above definitions, temperatures are reported as follows.
253 + * If SCT Command Transport is supported, it is used to read the
254 + * temperature and, if available, temperature limits.
255 + * - Otherwise, if SMART attribute 194 is supported, it is used to read
256 + * the temperature.
257 + * - Otherwise, if SMART attribute 190 is supported, it is used to read
258 + * the temperature.
259 + */
260 +
261 +#include <linux/ata.h>
262 +#include <linux/bits.h>
263 +#include <linux/device.h>
264 +#include <linux/hwmon.h>
265 +#include <linux/kernel.h>
266 +#include <linux/list.h>
267 +#include <linux/module.h>
268 +#include <linux/mutex.h>
269 +#include <scsi/scsi_cmnd.h>
270 +#include <scsi/scsi_device.h>
271 +#include <scsi/scsi_driver.h>
272 +#include <scsi/scsi_proto.h>
273 +
274 +struct drivetemp_data {
275 + struct list_head list; /* list of instantiated devices */
276 + struct mutex lock; /* protect data buffer accesses */
277 + struct scsi_device *sdev; /* SCSI device */
278 + struct device *dev; /* instantiating device */
279 + struct device *hwdev; /* hardware monitoring device */
280 + u8 smartdata[ATA_SECT_SIZE]; /* local buffer */
281 + int (*get_temp)(struct drivetemp_data *st, u32 attr, long *val);
282 + bool have_temp_lowest; /* lowest temp in SCT status */
283 + bool have_temp_highest; /* highest temp in SCT status */
284 + bool have_temp_min; /* have min temp */
285 + bool have_temp_max; /* have max temp */
286 + bool have_temp_lcrit; /* have lower critical limit */
287 + bool have_temp_crit; /* have critical limit */
288 + int temp_min; /* min temp */
289 + int temp_max; /* max temp */
290 + int temp_lcrit; /* lower critical limit */
291 + int temp_crit; /* critical limit */
292 +};
293 +
294 +static LIST_HEAD(drivetemp_devlist);
295 +
296 +#define ATA_MAX_SMART_ATTRS 30
297 +#define SMART_TEMP_PROP_190 190
298 +#define SMART_TEMP_PROP_194 194
299 +
300 +#define SCT_STATUS_REQ_ADDR 0xe0
301 +#define SCT_STATUS_VERSION_LOW 0 /* log byte offsets */
302 +#define SCT_STATUS_VERSION_HIGH 1
303 +#define SCT_STATUS_TEMP 200
304 +#define SCT_STATUS_TEMP_LOWEST 201
305 +#define SCT_STATUS_TEMP_HIGHEST 202
306 +#define SCT_READ_LOG_ADDR 0xe1
307 +#define SMART_READ_LOG 0xd5
308 +#define SMART_WRITE_LOG 0xd6
309 +
310 +#define INVALID_TEMP 0x80
311 +
312 +#define temp_is_valid(temp) ((temp) != INVALID_TEMP)
313 +#define temp_from_sct(temp) (((s8)(temp)) * 1000)
314 +
315 +static inline bool ata_id_smart_supported(u16 *id)
316 +{
317 + return id[ATA_ID_COMMAND_SET_1] & BIT(0);
318 +}
319 +
320 +static inline bool ata_id_smart_enabled(u16 *id)
321 +{
322 + return id[ATA_ID_CFS_ENABLE_1] & BIT(0);
323 +}
324 +
325 +static int drivetemp_scsi_command(struct drivetemp_data *st,
326 + u8 ata_command, u8 feature,
327 + u8 lba_low, u8 lba_mid, u8 lba_high)
328 +{
329 + u8 scsi_cmd[MAX_COMMAND_SIZE];
330 + int data_dir;
331 +
332 + memset(scsi_cmd, 0, sizeof(scsi_cmd));
333 + scsi_cmd[0] = ATA_16;
334 + if (ata_command == ATA_CMD_SMART && feature == SMART_WRITE_LOG) {
335 + scsi_cmd[1] = (5 << 1); /* PIO Data-out */
336 + /*
337 + * No off.line or cc, write to dev, block count in sector count
338 + * field.
339 + */
340 + scsi_cmd[2] = 0x06;
341 + data_dir = DMA_TO_DEVICE;
342 + } else {
343 + scsi_cmd[1] = (4 << 1); /* PIO Data-in */
344 + /*
345 + * No off.line or cc, read from dev, block count in sector count
346 + * field.
347 + */
348 + scsi_cmd[2] = 0x0e;
349 + data_dir = DMA_FROM_DEVICE;
350 + }
351 + scsi_cmd[4] = feature;
352 + scsi_cmd[6] = 1; /* 1 sector */
353 + scsi_cmd[8] = lba_low;
354 + scsi_cmd[10] = lba_mid;
355 + scsi_cmd[12] = lba_high;
356 + scsi_cmd[14] = ata_command;
357 +
358 + return scsi_execute_req(st->sdev, scsi_cmd, data_dir,
359 + st->smartdata, ATA_SECT_SIZE, NULL, HZ, 5,
360 + NULL);
361 +}
362 +
363 +static int drivetemp_ata_command(struct drivetemp_data *st, u8 feature,
364 + u8 select)
365 +{
366 + return drivetemp_scsi_command(st, ATA_CMD_SMART, feature, select,
367 + ATA_SMART_LBAM_PASS, ATA_SMART_LBAH_PASS);
368 +}
369 +
370 +static int drivetemp_get_smarttemp(struct drivetemp_data *st, u32 attr,
371 + long *temp)
372 +{
373 + u8 *buf = st->smartdata;
374 + bool have_temp = false;
375 + u8 temp_raw;
376 + u8 csum;
377 + int err;
378 + int i;
379 +
380 + err = drivetemp_ata_command(st, ATA_SMART_READ_VALUES, 0);
381 + if (err)
382 + return err;
383 +
384 + /* Checksum the read value table */
385 + csum = 0;
386 + for (i = 0; i < ATA_SECT_SIZE; i++)
387 + csum += buf[i];
388 + if (csum) {
389 + dev_dbg(&st->sdev->sdev_gendev,
390 + "checksum error reading SMART values\n");
391 + return -EIO;
392 + }
393 +
394 + for (i = 0; i < ATA_MAX_SMART_ATTRS; i++) {
395 + u8 *attr = buf + i * 12;
396 + int id = attr[2];
397 +
398 + if (!id)
399 + continue;
400 +
401 + if (id == SMART_TEMP_PROP_190) {
402 + temp_raw = attr[7];
403 + have_temp = true;
404 + }
405 + if (id == SMART_TEMP_PROP_194) {
406 + temp_raw = attr[7];
407 + have_temp = true;
408 + break;
409 + }
410 + }
411 +
412 + if (have_temp) {
413 + *temp = temp_raw * 1000;
414 + return 0;
415 + }
416 +
417 + return -ENXIO;
418 +}
419 +
420 +static int drivetemp_get_scttemp(struct drivetemp_data *st, u32 attr, long *val)
421 +{
422 + u8 *buf = st->smartdata;
423 + int err;
424 +
425 + err = drivetemp_ata_command(st, SMART_READ_LOG, SCT_STATUS_REQ_ADDR);
426 + if (err)
427 + return err;
428 + switch (attr) {
429 + case hwmon_temp_input:
430 + *val = temp_from_sct(buf[SCT_STATUS_TEMP]);
431 + break;
432 + case hwmon_temp_lowest:
433 + *val = temp_from_sct(buf[SCT_STATUS_TEMP_LOWEST]);
434 + break;
435 + case hwmon_temp_highest:
436 + *val = temp_from_sct(buf[SCT_STATUS_TEMP_HIGHEST]);
437 + break;
438 + default:
439 + err = -EINVAL;
440 + break;
441 + }
442 + return err;
443 +}
444 +
445 +static int drivetemp_identify_sata(struct drivetemp_data *st)
446 +{
447 + struct scsi_device *sdev = st->sdev;
448 + u8 *buf = st->smartdata;
449 + struct scsi_vpd *vpd;
450 + bool is_ata, is_sata;
451 + bool have_sct_data_table;
452 + bool have_sct_temp;
453 + bool have_smart;
454 + bool have_sct;
455 + u16 *ata_id;
456 + u16 version;
457 + long temp;
458 + int err;
459 +
460 + /* SCSI-ATA Translation present? */
461 + rcu_read_lock();
462 + vpd = rcu_dereference(sdev->vpd_pg89);
463 +
464 + /*
465 + * Verify that ATA IDENTIFY DEVICE data is included in ATA Information
466 + * VPD and that the drive implements the SATA protocol.
467 + */
468 + if (!vpd || vpd->len < 572 || vpd->data[56] != ATA_CMD_ID_ATA ||
469 + vpd->data[36] != 0x34) {
470 + rcu_read_unlock();
471 + return -ENODEV;
472 + }
473 + ata_id = (u16 *)&vpd->data[60];
474 + is_ata = ata_id_is_ata(ata_id);
475 + is_sata = ata_id_is_sata(ata_id);
476 + have_sct = ata_id_sct_supported(ata_id);
477 + have_sct_data_table = ata_id_sct_data_tables(ata_id);
478 + have_smart = ata_id_smart_supported(ata_id) &&
479 + ata_id_smart_enabled(ata_id);
480 +
481 + rcu_read_unlock();
482 +
483 + /* bail out if this is not a SATA device */
484 + if (!is_ata || !is_sata)
485 + return -ENODEV;
486 + if (!have_sct)
487 + goto skip_sct;
488 +
489 + err = drivetemp_ata_command(st, SMART_READ_LOG, SCT_STATUS_REQ_ADDR);
490 + if (err)
491 + goto skip_sct;
492 +
493 + version = (buf[SCT_STATUS_VERSION_HIGH] << 8) |
494 + buf[SCT_STATUS_VERSION_LOW];
495 + if (version != 2 && version != 3)
496 + goto skip_sct;
497 +
498 + have_sct_temp = temp_is_valid(buf[SCT_STATUS_TEMP]);
499 + if (!have_sct_temp)
500 + goto skip_sct;
501 +
502 + st->have_temp_lowest = temp_is_valid(buf[SCT_STATUS_TEMP_LOWEST]);
503 + st->have_temp_highest = temp_is_valid(buf[SCT_STATUS_TEMP_HIGHEST]);
504 +
505 + if (!have_sct_data_table)
506 + goto skip_sct;
507 +
508 + /* Request and read temperature history table */
509 + memset(buf, '\0', sizeof(st->smartdata));
510 + buf[0] = 5; /* data table command */
511 + buf[2] = 1; /* read table */
512 + buf[4] = 2; /* temperature history table */
513 +
514 + err = drivetemp_ata_command(st, SMART_WRITE_LOG, SCT_STATUS_REQ_ADDR);
515 + if (err)
516 + goto skip_sct_data;
517 +
518 + err = drivetemp_ata_command(st, SMART_READ_LOG, SCT_READ_LOG_ADDR);
519 + if (err)
520 + goto skip_sct_data;
521 +
522 + /*
523 + * Temperature limits per AT Attachment 8 -
524 + * ATA/ATAPI Command Set (ATA8-ACS)
525 + */
526 + st->have_temp_max = temp_is_valid(buf[6]);
527 + st->have_temp_crit = temp_is_valid(buf[7]);
528 + st->have_temp_min = temp_is_valid(buf[8]);
529 + st->have_temp_lcrit = temp_is_valid(buf[9]);
530 +
531 + st->temp_max = temp_from_sct(buf[6]);
532 + st->temp_crit = temp_from_sct(buf[7]);
533 + st->temp_min = temp_from_sct(buf[8]);
534 + st->temp_lcrit = temp_from_sct(buf[9]);
535 +
536 +skip_sct_data:
537 + if (have_sct_temp) {
538 + st->get_temp = drivetemp_get_scttemp;
539 + return 0;
540 + }
541 +skip_sct:
542 + if (!have_smart)
543 + return -ENODEV;
544 + st->get_temp = drivetemp_get_smarttemp;
545 + return drivetemp_get_smarttemp(st, hwmon_temp_input, &temp);
546 +}
547 +
548 +static int drivetemp_identify(struct drivetemp_data *st)
549 +{
550 + struct scsi_device *sdev = st->sdev;
551 +
552 + /* Bail out immediately if there is no inquiry data */
553 + if (!sdev->inquiry || sdev->inquiry_len < 16)
554 + return -ENODEV;
555 +
556 + /* Disk device? */
557 + if (sdev->type != TYPE_DISK && sdev->type != TYPE_ZBC)
558 + return -ENODEV;
559 +
560 + return drivetemp_identify_sata(st);
561 +}
562 +
563 +static int drivetemp_read(struct device *dev, enum hwmon_sensor_types type,
564 + u32 attr, int channel, long *val)
565 +{
566 + struct drivetemp_data *st = dev_get_drvdata(dev);
567 + int err = 0;
568 +
569 + if (type != hwmon_temp)
570 + return -EINVAL;
571 +
572 + switch (attr) {
573 + case hwmon_temp_input:
574 + case hwmon_temp_lowest:
575 + case hwmon_temp_highest:
576 + mutex_lock(&st->lock);
577 + err = st->get_temp(st, attr, val);
578 + mutex_unlock(&st->lock);
579 + break;
580 + case hwmon_temp_lcrit:
581 + *val = st->temp_lcrit;
582 + break;
583 + case hwmon_temp_min:
584 + *val = st->temp_min;
585 + break;
586 + case hwmon_temp_max:
587 + *val = st->temp_max;
588 + break;
589 + case hwmon_temp_crit:
590 + *val = st->temp_crit;
591 + break;
592 + default:
593 + err = -EINVAL;
594 + break;
595 + }
596 + return err;
597 +}
598 +
599 +static umode_t drivetemp_is_visible(const void *data,
600 + enum hwmon_sensor_types type,
601 + u32 attr, int channel)
602 +{
603 + const struct drivetemp_data *st = data;
604 +
605 + switch (type) {
606 + case hwmon_temp:
607 + switch (attr) {
608 + case hwmon_temp_input:
609 + return 0444;
610 + case hwmon_temp_lowest:
611 + if (st->have_temp_lowest)
612 + return 0444;
613 + break;
614 + case hwmon_temp_highest:
615 + if (st->have_temp_highest)
616 + return 0444;
617 + break;
618 + case hwmon_temp_min:
619 + if (st->have_temp_min)
620 + return 0444;
621 + break;
622 + case hwmon_temp_max:
623 + if (st->have_temp_max)
624 + return 0444;
625 + break;
626 + case hwmon_temp_lcrit:
627 + if (st->have_temp_lcrit)
628 + return 0444;
629 + break;
630 + case hwmon_temp_crit:
631 + if (st->have_temp_crit)
632 + return 0444;
633 + break;
634 + default:
635 + break;
636 + }
637 + break;
638 + default:
639 + break;
640 + }
641 + return 0;
642 +}
643 +
644 +static const struct hwmon_channel_info *drivetemp_info[] = {
645 + HWMON_CHANNEL_INFO(chip,
646 + HWMON_C_REGISTER_TZ),
647 + HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT |
648 + HWMON_T_LOWEST | HWMON_T_HIGHEST |
649 + HWMON_T_MIN | HWMON_T_MAX |
650 + HWMON_T_LCRIT | HWMON_T_CRIT),
651 + NULL
652 +};
653 +
654 +static const struct hwmon_ops drivetemp_ops = {
655 + .is_visible = drivetemp_is_visible,
656 + .read = drivetemp_read,
657 +};
658 +
659 +static const struct hwmon_chip_info drivetemp_chip_info = {
660 + .ops = &drivetemp_ops,
661 + .info = drivetemp_info,
662 +};
663 +
664 +/*
665 + * The device argument points to sdev->sdev_dev. Its parent is
666 + * sdev->sdev_gendev, which we can use to get the scsi_device pointer.
667 + */
668 +static int drivetemp_add(struct device *dev, struct class_interface *intf)
669 +{
670 + struct scsi_device *sdev = to_scsi_device(dev->parent);
671 + struct drivetemp_data *st;
672 + int err;
673 +
674 + st = kzalloc(sizeof(*st), GFP_KERNEL);
675 + if (!st)
676 + return -ENOMEM;
677 +
678 + st->sdev = sdev;
679 + st->dev = dev;
680 + mutex_init(&st->lock);
681 +
682 + if (drivetemp_identify(st)) {
683 + err = -ENODEV;
684 + goto abort;
685 + }
686 +
687 + st->hwdev = hwmon_device_register_with_info(dev->parent, "drivetemp",
688 + st, &drivetemp_chip_info,
689 + NULL);
690 + if (IS_ERR(st->hwdev)) {
691 + err = PTR_ERR(st->hwdev);
692 + goto abort;
693 + }
694 +
695 + list_add(&st->list, &drivetemp_devlist);
696 + return 0;
697 +
698 +abort:
699 + kfree(st);
700 + return err;
701 +}
702 +
703 +static void drivetemp_remove(struct device *dev, struct class_interface *intf)
704 +{
705 + struct drivetemp_data *st, *tmp;
706 +
707 + list_for_each_entry_safe(st, tmp, &drivetemp_devlist, list) {
708 + if (st->dev == dev) {
709 + list_del(&st->list);
710 + hwmon_device_unregister(st->hwdev);
711 + kfree(st);
712 + break;
713 + }
714 + }
715 +}
716 +
717 +static struct class_interface drivetemp_interface = {
718 + .add_dev = drivetemp_add,
719 + .remove_dev = drivetemp_remove,
720 +};
721 +
722 +static int __init drivetemp_init(void)
723 +{
724 + return scsi_register_interface(&drivetemp_interface);
725 +}
726 +
727 +static void __exit drivetemp_exit(void)
728 +{
729 + scsi_unregister_interface(&drivetemp_interface);
730 +}
731 +
732 +module_init(drivetemp_init);
733 +module_exit(drivetemp_exit);
734 +
735 +MODULE_AUTHOR("Guenter Roeck <linus@roeck-us.net>");
736 +MODULE_DESCRIPTION("Hard drive temperature monitor");
737 +MODULE_LICENSE("GPL");