1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
// SPDX-License-Identifier: GPL-2.0
/*
* Hwmon client for disk and solid state drives with temperature sensors
* Copyright (C) 2019 Zodiac Inflight Innovations
*
* With input from:
* Hwmon client for S.M.A.R.T. hard disk drives with temperature sensors.
* (C) 2018 Linus Walleij
*
* hwmon: Driver for SCSI/ATA temperature sensors
* by Constantin Baranov <const@mimas.ru>, submitted September 2009
*
* This drive supports reporting the temperatire of SATA drives. It can be
* easily extended to report the temperature of SCSI drives.
*
* The primary means to read drive temperatures and temperature limits
* for ATA drives is the SCT Command Transport feature set as specified in
* ATA8-ACS.
* It can be used to read the current drive temperature, temperature limits,
* and historic minimum and maximum temperatures. The SCT Command Transport
* feature set is documented in "AT Attachment 8 - ATA/ATAPI Command Set
* (ATA8-ACS)".
*
* If the SCT Command Transport feature set is not available, drive temperatures
* may be readable through SMART attributes. Since SMART attributes are not well
* defined, this method is only used as fallback mechanism.
*
* There are three SMART attributes which may report drive temperatures.
* Those are defined as follows (from
* http://www.cropel.com/library/smart-attribute-list.aspx).
*
* 190 Temperature Temperature, monitored by a sensor somewhere inside
* the drive. Raw value typicaly holds the actual
* temperature (hexadecimal) in its rightmost two digits.
*
* 194 Temperature Temperature, monitored by a sensor somewhere inside
* the drive. Raw value typicaly holds the actual
* temperature (hexadecimal) in its rightmost two digits.
*
* 231 Temperature Temperature, monitored by a sensor somewhere inside
* the drive. Raw value typicaly holds the actual
* temperature (hexadecimal) in its rightmost two digits.
*
* Wikipedia defines attributes a bit differently.
*
* 190 Temperature Value is equal to (100-temp. °C), allowing manufacturer
* Difference or to set a minimum threshold which corresponds to a
* Airflow maximum temperature. This also follows the convention of
* Temperature 100 being a best-case value and lower values being
* undesirable. However, some older drives may instead
* report raw Temperature (identical to 0xC2) or
* Temperature minus 50 here.
* 194 Temperature or Indicates the device temperature, if the appropriate
* Temperature sensor is fitted. Lowest byte of the raw value contains
* Celsius the exact temperature value (Celsius degrees).
* 231 Life Left Indicates the approximate SSD life left, in terms of
* (SSDs) or program/erase cycles or available reserved blocks.
* Temperature A normalized value of 100 represents a new drive, with
* a threshold value at 10 indicating a need for
* replacement. A value of 0 may mean that the drive is
* operating in read-only mode to allow data recovery.
* Previously (pre-2010) occasionally used for Drive
* Temperature (more typically reported at 0xC2).
*
* Common denominator is that the first raw byte reports the temperature
* in degrees C on almost all drives. Some drives may report a fractional
* temperature in the second raw byte.
*
* Known exceptions (from libatasmart):
* - SAMSUNG SV0412H and SAMSUNG SV1204H) report the temperature in 10th
* degrees C in the first two raw bytes.
* - A few Maxtor drives report an unknown or bad value in attribute 194.
* - Certain Apple SSD drives report an unknown value in attribute 190.
* Only certain firmware versions are affected.
*
* Those exceptions affect older ATA drives and are currently ignored.
* Also, the second raw byte (possibly reporting the fractional temperature)
* is currently ignored.
*
* Many drives also report temperature limits in additional SMART data raw
* bytes. The format of those is not well defined and varies widely.
* The driver does not currently attempt to report those limits.
*
* According to data in smartmontools, attribute 231 is rarely used to report
* drive temperatures. At the same time, several drives report SSD life left
* in attribute 231, but do not support temperature sensors. For this reason,
* attribute 231 is currently ignored.
*
* Following above definitions, temperatures are reported as follows.
* If SCT Command Transport is supported, it is used to read the
* temperature and, if available, temperature limits.
* - Otherwise, if SMART attribute 194 is supported, it is used to read
* the temperature.
* - Otherwise, if SMART attribute 190 is supported, it is used to read
* the temperature.
*/
#include <linux/ata.h>
#include <linux/bits.h>
#include <linux/device.h>
#include <linux/hwmon.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_driver.h>
#include <scsi/scsi_proto.h>
struct drivetemp_data {
struct list_head list; /* list of instantiated devices */
struct mutex lock; /* protect data buffer accesses */
struct scsi_device *sdev; /* SCSI device */
struct device *dev; /* instantiating device */
struct device *hwdev; /* hardware monitoring device */
u8 smartdata[ATA_SECT_SIZE]; /* local buffer */
int (*get_temp)(struct drivetemp_data *st, u32 attr, long *val);
bool have_temp_lowest; /* lowest temp in SCT status */
bool have_temp_highest; /* highest temp in SCT status */
bool have_temp_min; /* have min temp */
bool have_temp_max; /* have max temp */
bool have_temp_lcrit; /* have lower critical limit */
bool have_temp_crit; /* have critical limit */
int temp_min; /* min temp */
int temp_max; /* max temp */
int temp_lcrit; /* lower critical limit */
int temp_crit; /* critical limit */
};
static LIST_HEAD(drivetemp_devlist);
#define ATA_MAX_SMART_ATTRS 30
#define SMART_TEMP_PROP_190 190
#define SMART_TEMP_PROP_194 194
#define SCT_STATUS_REQ_ADDR 0xe0
#define SCT_STATUS_VERSION_LOW 0 /* log byte offsets */
#define SCT_STATUS_VERSION_HIGH 1
#define SCT_STATUS_TEMP 200
#define SCT_STATUS_TEMP_LOWEST 201
#define SCT_STATUS_TEMP_HIGHEST 202
#define SCT_READ_LOG_ADDR 0xe1
#define SMART_READ_LOG 0xd5
#define SMART_WRITE_LOG 0xd6
#define INVALID_TEMP 0x80
#define temp_is_valid(temp) ((temp) != INVALID_TEMP)
#define temp_from_sct(temp) (((s8)(temp)) * 1000)
static inline bool ata_id_smart_supported(u16 *id)
{
return id[ATA_ID_COMMAND_SET_1] & BIT(0);
}
static inline bool ata_id_smart_enabled(u16 *id)
{
return id[ATA_ID_CFS_ENABLE_1] & BIT(0);
}
static int drivetemp_scsi_command(struct drivetemp_data *st,
u8 ata_command, u8 feature,
u8 lba_low, u8 lba_mid, u8 lba_high)
{
u8 scsi_cmd[MAX_COMMAND_SIZE];
int data_dir;
memset(scsi_cmd, 0, sizeof(scsi_cmd));
scsi_cmd[0] = ATA_16;
if (ata_command == ATA_CMD_SMART && feature == SMART_WRITE_LOG) {
scsi_cmd[1] = (5 << 1); /* PIO Data-out */
/*
* No off.line or cc, write to dev, block count in sector count
* field.
*/
scsi_cmd[2] = 0x06;
data_dir = DMA_TO_DEVICE;
} else {
scsi_cmd[1] = (4 << 1); /* PIO Data-in */
/*
* No off.line or cc, read from dev, block count in sector count
* field.
*/
scsi_cmd[2] = 0x0e;
data_dir = DMA_FROM_DEVICE;
}
scsi_cmd[4] = feature;
scsi_cmd[6] = 1; /* 1 sector */
scsi_cmd[8] = lba_low;
scsi_cmd[10] = lba_mid;
scsi_cmd[12] = lba_high;
scsi_cmd[14] = ata_command;
return scsi_execute_req(st->sdev, scsi_cmd, data_dir,
st->smartdata, ATA_SECT_SIZE, NULL, HZ, 5,
NULL);
}
static int drivetemp_ata_command(struct drivetemp_data *st, u8 feature,
u8 select)
{
return drivetemp_scsi_command(st, ATA_CMD_SMART, feature, select,
ATA_SMART_LBAM_PASS, ATA_SMART_LBAH_PASS);
}
static int drivetemp_get_smarttemp(struct drivetemp_data *st, u32 attr,
long *temp)
{
u8 *buf = st->smartdata;
bool have_temp = false;
u8 temp_raw;
u8 csum;
int err;
int i;
err = drivetemp_ata_command(st, ATA_SMART_READ_VALUES, 0);
if (err)
return err;
/* Checksum the read value table */
csum = 0;
for (i = 0; i < ATA_SECT_SIZE; i++)
csum += buf[i];
if (csum) {
dev_dbg(&st->sdev->sdev_gendev,
"checksum error reading SMART values\n");
return -EIO;
}
for (i = 0; i < ATA_MAX_SMART_ATTRS; i++) {
u8 *attr = buf + i * 12;
int id = attr[2];
if (!id)
continue;
if (id == SMART_TEMP_PROP_190) {
temp_raw = attr[7];
have_temp = true;
}
if (id == SMART_TEMP_PROP_194) {
temp_raw = attr[7];
have_temp = true;
break;
}
}
if (have_temp) {
*temp = temp_raw * 1000;
return 0;
}
return -ENXIO;
}
static int drivetemp_get_scttemp(struct drivetemp_data *st, u32 attr, long *val)
{
u8 *buf = st->smartdata;
int err;
err = drivetemp_ata_command(st, SMART_READ_LOG, SCT_STATUS_REQ_ADDR);
if (err)
return err;
switch (attr) {
case hwmon_temp_input:
*val = temp_from_sct(buf[SCT_STATUS_TEMP]);
break;
case hwmon_temp_lowest:
*val = temp_from_sct(buf[SCT_STATUS_TEMP_LOWEST]);
break;
case hwmon_temp_highest:
*val = temp_from_sct(buf[SCT_STATUS_TEMP_HIGHEST]);
break;
default:
err = -EINVAL;
break;
}
return err;
}
static int drivetemp_identify_sata(struct drivetemp_data *st)
{
struct scsi_device *sdev = st->sdev;
u8
|