xref: /OK3568_Linux_fs/kernel/drivers/hwmon/drivetemp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Hwmon client for disk and solid state drives with temperature sensors
4*4882a593Smuzhiyun  * Copyright (C) 2019 Zodiac Inflight Innovations
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * With input from:
7*4882a593Smuzhiyun  *    Hwmon client for S.M.A.R.T. hard disk drives with temperature sensors.
8*4882a593Smuzhiyun  *    (C) 2018 Linus Walleij
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *    hwmon: Driver for SCSI/ATA temperature sensors
11*4882a593Smuzhiyun  *    by Constantin Baranov <const@mimas.ru>, submitted September 2009
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This drive supports reporting the temperatire of SATA drives. It can be
14*4882a593Smuzhiyun  * easily extended to report the temperature of SCSI drives.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * The primary means to read drive temperatures and temperature limits
17*4882a593Smuzhiyun  * for ATA drives is the SCT Command Transport feature set as specified in
18*4882a593Smuzhiyun  * ATA8-ACS.
19*4882a593Smuzhiyun  * It can be used to read the current drive temperature, temperature limits,
20*4882a593Smuzhiyun  * and historic minimum and maximum temperatures. The SCT Command Transport
21*4882a593Smuzhiyun  * feature set is documented in "AT Attachment 8 - ATA/ATAPI Command Set
22*4882a593Smuzhiyun  * (ATA8-ACS)".
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * If the SCT Command Transport feature set is not available, drive temperatures
25*4882a593Smuzhiyun  * may be readable through SMART attributes. Since SMART attributes are not well
26*4882a593Smuzhiyun  * defined, this method is only used as fallback mechanism.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * There are three SMART attributes which may report drive temperatures.
29*4882a593Smuzhiyun  * Those are defined as follows (from
30*4882a593Smuzhiyun  * http://www.cropel.com/library/smart-attribute-list.aspx).
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * 190	Temperature	Temperature, monitored by a sensor somewhere inside
33*4882a593Smuzhiyun  *			the drive. Raw value typicaly holds the actual
34*4882a593Smuzhiyun  *			temperature (hexadecimal) in its rightmost two digits.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * 194	Temperature	Temperature, monitored by a sensor somewhere inside
37*4882a593Smuzhiyun  *			the drive. Raw value typicaly holds the actual
38*4882a593Smuzhiyun  *			temperature (hexadecimal) in its rightmost two digits.
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * 231	Temperature	Temperature, monitored by a sensor somewhere inside
41*4882a593Smuzhiyun  *			the drive. Raw value typicaly holds the actual
42*4882a593Smuzhiyun  *			temperature (hexadecimal) in its rightmost two digits.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * Wikipedia defines attributes a bit differently.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * 190	Temperature	Value is equal to (100-temp. °C), allowing manufacturer
47*4882a593Smuzhiyun  *	Difference or	to set a minimum threshold which corresponds to a
48*4882a593Smuzhiyun  *	Airflow		maximum temperature. This also follows the convention of
49*4882a593Smuzhiyun  *	Temperature	100 being a best-case value and lower values being
50*4882a593Smuzhiyun  *			undesirable. However, some older drives may instead
51*4882a593Smuzhiyun  *			report raw Temperature (identical to 0xC2) or
52*4882a593Smuzhiyun  *			Temperature minus 50 here.
53*4882a593Smuzhiyun  * 194	Temperature or	Indicates the device temperature, if the appropriate
54*4882a593Smuzhiyun  *	Temperature	sensor is fitted. Lowest byte of the raw value contains
55*4882a593Smuzhiyun  *	Celsius		the exact temperature value (Celsius degrees).
56*4882a593Smuzhiyun  * 231	Life Left	Indicates the approximate SSD life left, in terms of
57*4882a593Smuzhiyun  *	(SSDs) or	program/erase cycles or available reserved blocks.
58*4882a593Smuzhiyun  *	Temperature	A normalized value of 100 represents a new drive, with
59*4882a593Smuzhiyun  *			a threshold value at 10 indicating a need for
60*4882a593Smuzhiyun  *			replacement. A value of 0 may mean that the drive is
61*4882a593Smuzhiyun  *			operating in read-only mode to allow data recovery.
62*4882a593Smuzhiyun  *			Previously (pre-2010) occasionally used for Drive
63*4882a593Smuzhiyun  *			Temperature (more typically reported at 0xC2).
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * Common denominator is that the first raw byte reports the temperature
66*4882a593Smuzhiyun  * in degrees C on almost all drives. Some drives may report a fractional
67*4882a593Smuzhiyun  * temperature in the second raw byte.
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * Known exceptions (from libatasmart):
70*4882a593Smuzhiyun  * - SAMSUNG SV0412H and SAMSUNG SV1204H) report the temperature in 10th
71*4882a593Smuzhiyun  *   degrees C in the first two raw bytes.
72*4882a593Smuzhiyun  * - A few Maxtor drives report an unknown or bad value in attribute 194.
73*4882a593Smuzhiyun  * - Certain Apple SSD drives report an unknown value in attribute 190.
74*4882a593Smuzhiyun  *   Only certain firmware versions are affected.
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * Those exceptions affect older ATA drives and are currently ignored.
77*4882a593Smuzhiyun  * Also, the second raw byte (possibly reporting the fractional temperature)
78*4882a593Smuzhiyun  * is currently ignored.
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * Many drives also report temperature limits in additional SMART data raw
81*4882a593Smuzhiyun  * bytes. The format of those is not well defined and varies widely.
82*4882a593Smuzhiyun  * The driver does not currently attempt to report those limits.
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * According to data in smartmontools, attribute 231 is rarely used to report
85*4882a593Smuzhiyun  * drive temperatures. At the same time, several drives report SSD life left
86*4882a593Smuzhiyun  * in attribute 231, but do not support temperature sensors. For this reason,
87*4882a593Smuzhiyun  * attribute 231 is currently ignored.
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * Following above definitions, temperatures are reported as follows.
90*4882a593Smuzhiyun  *   If SCT Command Transport is supported, it is used to read the
91*4882a593Smuzhiyun  *   temperature and, if available, temperature limits.
92*4882a593Smuzhiyun  * - Otherwise, if SMART attribute 194 is supported, it is used to read
93*4882a593Smuzhiyun  *   the temperature.
94*4882a593Smuzhiyun  * - Otherwise, if SMART attribute 190 is supported, it is used to read
95*4882a593Smuzhiyun  *   the temperature.
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun #include <linux/ata.h>
99*4882a593Smuzhiyun #include <linux/bits.h>
100*4882a593Smuzhiyun #include <linux/device.h>
101*4882a593Smuzhiyun #include <linux/hwmon.h>
102*4882a593Smuzhiyun #include <linux/kernel.h>
103*4882a593Smuzhiyun #include <linux/list.h>
104*4882a593Smuzhiyun #include <linux/module.h>
105*4882a593Smuzhiyun #include <linux/mutex.h>
106*4882a593Smuzhiyun #include <scsi/scsi_cmnd.h>
107*4882a593Smuzhiyun #include <scsi/scsi_device.h>
108*4882a593Smuzhiyun #include <scsi/scsi_driver.h>
109*4882a593Smuzhiyun #include <scsi/scsi_proto.h>
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun struct drivetemp_data {
112*4882a593Smuzhiyun 	struct list_head list;		/* list of instantiated devices */
113*4882a593Smuzhiyun 	struct mutex lock;		/* protect data buffer accesses */
114*4882a593Smuzhiyun 	struct scsi_device *sdev;	/* SCSI device */
115*4882a593Smuzhiyun 	struct device *dev;		/* instantiating device */
116*4882a593Smuzhiyun 	struct device *hwdev;		/* hardware monitoring device */
117*4882a593Smuzhiyun 	u8 smartdata[ATA_SECT_SIZE];	/* local buffer */
118*4882a593Smuzhiyun 	int (*get_temp)(struct drivetemp_data *st, u32 attr, long *val);
119*4882a593Smuzhiyun 	bool have_temp_lowest;		/* lowest temp in SCT status */
120*4882a593Smuzhiyun 	bool have_temp_highest;		/* highest temp in SCT status */
121*4882a593Smuzhiyun 	bool have_temp_min;		/* have min temp */
122*4882a593Smuzhiyun 	bool have_temp_max;		/* have max temp */
123*4882a593Smuzhiyun 	bool have_temp_lcrit;		/* have lower critical limit */
124*4882a593Smuzhiyun 	bool have_temp_crit;		/* have critical limit */
125*4882a593Smuzhiyun 	int temp_min;			/* min temp */
126*4882a593Smuzhiyun 	int temp_max;			/* max temp */
127*4882a593Smuzhiyun 	int temp_lcrit;			/* lower critical limit */
128*4882a593Smuzhiyun 	int temp_crit;			/* critical limit */
129*4882a593Smuzhiyun };
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun static LIST_HEAD(drivetemp_devlist);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun #define ATA_MAX_SMART_ATTRS	30
134*4882a593Smuzhiyun #define SMART_TEMP_PROP_190	190
135*4882a593Smuzhiyun #define SMART_TEMP_PROP_194	194
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun #define SCT_STATUS_REQ_ADDR	0xe0
138*4882a593Smuzhiyun #define  SCT_STATUS_VERSION_LOW		0	/* log byte offsets */
139*4882a593Smuzhiyun #define  SCT_STATUS_VERSION_HIGH	1
140*4882a593Smuzhiyun #define  SCT_STATUS_TEMP		200
141*4882a593Smuzhiyun #define  SCT_STATUS_TEMP_LOWEST		201
142*4882a593Smuzhiyun #define  SCT_STATUS_TEMP_HIGHEST	202
143*4882a593Smuzhiyun #define SCT_READ_LOG_ADDR	0xe1
144*4882a593Smuzhiyun #define  SMART_READ_LOG			0xd5
145*4882a593Smuzhiyun #define  SMART_WRITE_LOG		0xd6
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun #define INVALID_TEMP		0x80
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun #define temp_is_valid(temp)	((temp) != INVALID_TEMP)
150*4882a593Smuzhiyun #define temp_from_sct(temp)	(((s8)(temp)) * 1000)
151*4882a593Smuzhiyun 
ata_id_smart_supported(u16 * id)152*4882a593Smuzhiyun static inline bool ata_id_smart_supported(u16 *id)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	return id[ATA_ID_COMMAND_SET_1] & BIT(0);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
ata_id_smart_enabled(u16 * id)157*4882a593Smuzhiyun static inline bool ata_id_smart_enabled(u16 *id)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	return id[ATA_ID_CFS_ENABLE_1] & BIT(0);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
drivetemp_scsi_command(struct drivetemp_data * st,u8 ata_command,u8 feature,u8 lba_low,u8 lba_mid,u8 lba_high)162*4882a593Smuzhiyun static int drivetemp_scsi_command(struct drivetemp_data *st,
163*4882a593Smuzhiyun 				 u8 ata_command, u8 feature,
164*4882a593Smuzhiyun 				 u8 lba_low, u8 lba_mid, u8 lba_high)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	u8 scsi_cmd[MAX_COMMAND_SIZE];
167*4882a593Smuzhiyun 	int data_dir;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	memset(scsi_cmd, 0, sizeof(scsi_cmd));
170*4882a593Smuzhiyun 	scsi_cmd[0] = ATA_16;
171*4882a593Smuzhiyun 	if (ata_command == ATA_CMD_SMART && feature == SMART_WRITE_LOG) {
172*4882a593Smuzhiyun 		scsi_cmd[1] = (5 << 1);	/* PIO Data-out */
173*4882a593Smuzhiyun 		/*
174*4882a593Smuzhiyun 		 * No off.line or cc, write to dev, block count in sector count
175*4882a593Smuzhiyun 		 * field.
176*4882a593Smuzhiyun 		 */
177*4882a593Smuzhiyun 		scsi_cmd[2] = 0x06;
178*4882a593Smuzhiyun 		data_dir = DMA_TO_DEVICE;
179*4882a593Smuzhiyun 	} else {
180*4882a593Smuzhiyun 		scsi_cmd[1] = (4 << 1);	/* PIO Data-in */
181*4882a593Smuzhiyun 		/*
182*4882a593Smuzhiyun 		 * No off.line or cc, read from dev, block count in sector count
183*4882a593Smuzhiyun 		 * field.
184*4882a593Smuzhiyun 		 */
185*4882a593Smuzhiyun 		scsi_cmd[2] = 0x0e;
186*4882a593Smuzhiyun 		data_dir = DMA_FROM_DEVICE;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 	scsi_cmd[4] = feature;
189*4882a593Smuzhiyun 	scsi_cmd[6] = 1;	/* 1 sector */
190*4882a593Smuzhiyun 	scsi_cmd[8] = lba_low;
191*4882a593Smuzhiyun 	scsi_cmd[10] = lba_mid;
192*4882a593Smuzhiyun 	scsi_cmd[12] = lba_high;
193*4882a593Smuzhiyun 	scsi_cmd[14] = ata_command;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	return scsi_execute_req(st->sdev, scsi_cmd, data_dir,
196*4882a593Smuzhiyun 				st->smartdata, ATA_SECT_SIZE, NULL, HZ, 5,
197*4882a593Smuzhiyun 				NULL);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
drivetemp_ata_command(struct drivetemp_data * st,u8 feature,u8 select)200*4882a593Smuzhiyun static int drivetemp_ata_command(struct drivetemp_data *st, u8 feature,
201*4882a593Smuzhiyun 				 u8 select)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	return drivetemp_scsi_command(st, ATA_CMD_SMART, feature, select,
204*4882a593Smuzhiyun 				     ATA_SMART_LBAM_PASS, ATA_SMART_LBAH_PASS);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
drivetemp_get_smarttemp(struct drivetemp_data * st,u32 attr,long * temp)207*4882a593Smuzhiyun static int drivetemp_get_smarttemp(struct drivetemp_data *st, u32 attr,
208*4882a593Smuzhiyun 				  long *temp)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	u8 *buf = st->smartdata;
211*4882a593Smuzhiyun 	bool have_temp = false;
212*4882a593Smuzhiyun 	u8 temp_raw;
213*4882a593Smuzhiyun 	u8 csum;
214*4882a593Smuzhiyun 	int err;
215*4882a593Smuzhiyun 	int i;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	err = drivetemp_ata_command(st, ATA_SMART_READ_VALUES, 0);
218*4882a593Smuzhiyun 	if (err)
219*4882a593Smuzhiyun 		return err;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/* Checksum the read value table */
222*4882a593Smuzhiyun 	csum = 0;
223*4882a593Smuzhiyun 	for (i = 0; i < ATA_SECT_SIZE; i++)
224*4882a593Smuzhiyun 		csum += buf[i];
225*4882a593Smuzhiyun 	if (csum) {
226*4882a593Smuzhiyun 		dev_dbg(&st->sdev->sdev_gendev,
227*4882a593Smuzhiyun 			"checksum error reading SMART values\n");
228*4882a593Smuzhiyun 		return -EIO;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	for (i = 0; i < ATA_MAX_SMART_ATTRS; i++) {
232*4882a593Smuzhiyun 		u8 *attr = buf + i * 12;
233*4882a593Smuzhiyun 		int id = attr[2];
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 		if (!id)
236*4882a593Smuzhiyun 			continue;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 		if (id == SMART_TEMP_PROP_190) {
239*4882a593Smuzhiyun 			temp_raw = attr[7];
240*4882a593Smuzhiyun 			have_temp = true;
241*4882a593Smuzhiyun 		}
242*4882a593Smuzhiyun 		if (id == SMART_TEMP_PROP_194) {
243*4882a593Smuzhiyun 			temp_raw = attr[7];
244*4882a593Smuzhiyun 			have_temp = true;
245*4882a593Smuzhiyun 			break;
246*4882a593Smuzhiyun 		}
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	if (have_temp) {
250*4882a593Smuzhiyun 		*temp = temp_raw * 1000;
251*4882a593Smuzhiyun 		return 0;
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	return -ENXIO;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
drivetemp_get_scttemp(struct drivetemp_data * st,u32 attr,long * val)257*4882a593Smuzhiyun static int drivetemp_get_scttemp(struct drivetemp_data *st, u32 attr, long *val)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	u8 *buf = st->smartdata;
260*4882a593Smuzhiyun 	int err;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	err = drivetemp_ata_command(st, SMART_READ_LOG, SCT_STATUS_REQ_ADDR);
263*4882a593Smuzhiyun 	if (err)
264*4882a593Smuzhiyun 		return err;
265*4882a593Smuzhiyun 	switch (attr) {
266*4882a593Smuzhiyun 	case hwmon_temp_input:
267*4882a593Smuzhiyun 		if (!temp_is_valid(buf[SCT_STATUS_TEMP]))
268*4882a593Smuzhiyun 			return -ENODATA;
269*4882a593Smuzhiyun 		*val = temp_from_sct(buf[SCT_STATUS_TEMP]);
270*4882a593Smuzhiyun 		break;
271*4882a593Smuzhiyun 	case hwmon_temp_lowest:
272*4882a593Smuzhiyun 		if (!temp_is_valid(buf[SCT_STATUS_TEMP_LOWEST]))
273*4882a593Smuzhiyun 			return -ENODATA;
274*4882a593Smuzhiyun 		*val = temp_from_sct(buf[SCT_STATUS_TEMP_LOWEST]);
275*4882a593Smuzhiyun 		break;
276*4882a593Smuzhiyun 	case hwmon_temp_highest:
277*4882a593Smuzhiyun 		if (!temp_is_valid(buf[SCT_STATUS_TEMP_HIGHEST]))
278*4882a593Smuzhiyun 			return -ENODATA;
279*4882a593Smuzhiyun 		*val = temp_from_sct(buf[SCT_STATUS_TEMP_HIGHEST]);
280*4882a593Smuzhiyun 		break;
281*4882a593Smuzhiyun 	default:
282*4882a593Smuzhiyun 		err = -EINVAL;
283*4882a593Smuzhiyun 		break;
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun 	return err;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun static const char * const sct_avoid_models[] = {
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun  * These drives will have WRITE FPDMA QUEUED command timeouts and sometimes just
291*4882a593Smuzhiyun  * freeze until power-cycled under heavy write loads when their temperature is
292*4882a593Smuzhiyun  * getting polled in SCT mode. The SMART mode seems to be fine, though.
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  * While only the 3 TB model (DT01ACA3) was actually caught exhibiting the
295*4882a593Smuzhiyun  * problem let's play safe here to avoid data corruption and ban the whole
296*4882a593Smuzhiyun  * DT01ACAx family.
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun  * The models from this array are prefix-matched.
299*4882a593Smuzhiyun  */
300*4882a593Smuzhiyun 	"TOSHIBA DT01ACA",
301*4882a593Smuzhiyun };
302*4882a593Smuzhiyun 
drivetemp_sct_avoid(struct drivetemp_data * st)303*4882a593Smuzhiyun static bool drivetemp_sct_avoid(struct drivetemp_data *st)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	struct scsi_device *sdev = st->sdev;
306*4882a593Smuzhiyun 	unsigned int ctr;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	if (!sdev->model)
309*4882a593Smuzhiyun 		return false;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	/*
312*4882a593Smuzhiyun 	 * The "model" field contains just the raw SCSI INQUIRY response
313*4882a593Smuzhiyun 	 * "product identification" field, which has a width of 16 bytes.
314*4882a593Smuzhiyun 	 * This field is space-filled, but is NOT NULL-terminated.
315*4882a593Smuzhiyun 	 */
316*4882a593Smuzhiyun 	for (ctr = 0; ctr < ARRAY_SIZE(sct_avoid_models); ctr++)
317*4882a593Smuzhiyun 		if (!strncmp(sdev->model, sct_avoid_models[ctr],
318*4882a593Smuzhiyun 			     strlen(sct_avoid_models[ctr])))
319*4882a593Smuzhiyun 			return true;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	return false;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
drivetemp_identify_sata(struct drivetemp_data * st)324*4882a593Smuzhiyun static int drivetemp_identify_sata(struct drivetemp_data *st)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct scsi_device *sdev = st->sdev;
327*4882a593Smuzhiyun 	u8 *buf = st->smartdata;
328*4882a593Smuzhiyun 	struct scsi_vpd *vpd;
329*4882a593Smuzhiyun 	bool is_ata, is_sata;
330*4882a593Smuzhiyun 	bool have_sct_data_table;
331*4882a593Smuzhiyun 	bool have_sct_temp;
332*4882a593Smuzhiyun 	bool have_smart;
333*4882a593Smuzhiyun 	bool have_sct;
334*4882a593Smuzhiyun 	u16 *ata_id;
335*4882a593Smuzhiyun 	u16 version;
336*4882a593Smuzhiyun 	long temp;
337*4882a593Smuzhiyun 	int err;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	/* SCSI-ATA Translation present? */
340*4882a593Smuzhiyun 	rcu_read_lock();
341*4882a593Smuzhiyun 	vpd = rcu_dereference(sdev->vpd_pg89);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	/*
344*4882a593Smuzhiyun 	 * Verify that ATA IDENTIFY DEVICE data is included in ATA Information
345*4882a593Smuzhiyun 	 * VPD and that the drive implements the SATA protocol.
346*4882a593Smuzhiyun 	 */
347*4882a593Smuzhiyun 	if (!vpd || vpd->len < 572 || vpd->data[56] != ATA_CMD_ID_ATA ||
348*4882a593Smuzhiyun 	    vpd->data[36] != 0x34) {
349*4882a593Smuzhiyun 		rcu_read_unlock();
350*4882a593Smuzhiyun 		return -ENODEV;
351*4882a593Smuzhiyun 	}
352*4882a593Smuzhiyun 	ata_id = (u16 *)&vpd->data[60];
353*4882a593Smuzhiyun 	is_ata = ata_id_is_ata(ata_id);
354*4882a593Smuzhiyun 	is_sata = ata_id_is_sata(ata_id);
355*4882a593Smuzhiyun 	have_sct = ata_id_sct_supported(ata_id);
356*4882a593Smuzhiyun 	have_sct_data_table = ata_id_sct_data_tables(ata_id);
357*4882a593Smuzhiyun 	have_smart = ata_id_smart_supported(ata_id) &&
358*4882a593Smuzhiyun 				ata_id_smart_enabled(ata_id);
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	rcu_read_unlock();
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	/* bail out if this is not a SATA device */
363*4882a593Smuzhiyun 	if (!is_ata || !is_sata)
364*4882a593Smuzhiyun 		return -ENODEV;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	if (have_sct && drivetemp_sct_avoid(st)) {
367*4882a593Smuzhiyun 		dev_notice(&sdev->sdev_gendev,
368*4882a593Smuzhiyun 			   "will avoid using SCT for temperature monitoring\n");
369*4882a593Smuzhiyun 		have_sct = false;
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if (!have_sct)
373*4882a593Smuzhiyun 		goto skip_sct;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	err = drivetemp_ata_command(st, SMART_READ_LOG, SCT_STATUS_REQ_ADDR);
376*4882a593Smuzhiyun 	if (err)
377*4882a593Smuzhiyun 		goto skip_sct;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	version = (buf[SCT_STATUS_VERSION_HIGH] << 8) |
380*4882a593Smuzhiyun 		  buf[SCT_STATUS_VERSION_LOW];
381*4882a593Smuzhiyun 	if (version != 2 && version != 3)
382*4882a593Smuzhiyun 		goto skip_sct;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	have_sct_temp = temp_is_valid(buf[SCT_STATUS_TEMP]);
385*4882a593Smuzhiyun 	if (!have_sct_temp)
386*4882a593Smuzhiyun 		goto skip_sct;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	st->have_temp_lowest = temp_is_valid(buf[SCT_STATUS_TEMP_LOWEST]);
389*4882a593Smuzhiyun 	st->have_temp_highest = temp_is_valid(buf[SCT_STATUS_TEMP_HIGHEST]);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	if (!have_sct_data_table)
392*4882a593Smuzhiyun 		goto skip_sct_data;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* Request and read temperature history table */
395*4882a593Smuzhiyun 	memset(buf, '\0', sizeof(st->smartdata));
396*4882a593Smuzhiyun 	buf[0] = 5;	/* data table command */
397*4882a593Smuzhiyun 	buf[2] = 1;	/* read table */
398*4882a593Smuzhiyun 	buf[4] = 2;	/* temperature history table */
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	err = drivetemp_ata_command(st, SMART_WRITE_LOG, SCT_STATUS_REQ_ADDR);
401*4882a593Smuzhiyun 	if (err)
402*4882a593Smuzhiyun 		goto skip_sct_data;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	err = drivetemp_ata_command(st, SMART_READ_LOG, SCT_READ_LOG_ADDR);
405*4882a593Smuzhiyun 	if (err)
406*4882a593Smuzhiyun 		goto skip_sct_data;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/*
409*4882a593Smuzhiyun 	 * Temperature limits per AT Attachment 8 -
410*4882a593Smuzhiyun 	 * ATA/ATAPI Command Set (ATA8-ACS)
411*4882a593Smuzhiyun 	 */
412*4882a593Smuzhiyun 	st->have_temp_max = temp_is_valid(buf[6]);
413*4882a593Smuzhiyun 	st->have_temp_crit = temp_is_valid(buf[7]);
414*4882a593Smuzhiyun 	st->have_temp_min = temp_is_valid(buf[8]);
415*4882a593Smuzhiyun 	st->have_temp_lcrit = temp_is_valid(buf[9]);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	st->temp_max = temp_from_sct(buf[6]);
418*4882a593Smuzhiyun 	st->temp_crit = temp_from_sct(buf[7]);
419*4882a593Smuzhiyun 	st->temp_min = temp_from_sct(buf[8]);
420*4882a593Smuzhiyun 	st->temp_lcrit = temp_from_sct(buf[9]);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun skip_sct_data:
423*4882a593Smuzhiyun 	if (have_sct_temp) {
424*4882a593Smuzhiyun 		st->get_temp = drivetemp_get_scttemp;
425*4882a593Smuzhiyun 		return 0;
426*4882a593Smuzhiyun 	}
427*4882a593Smuzhiyun skip_sct:
428*4882a593Smuzhiyun 	if (!have_smart)
429*4882a593Smuzhiyun 		return -ENODEV;
430*4882a593Smuzhiyun 	st->get_temp = drivetemp_get_smarttemp;
431*4882a593Smuzhiyun 	return drivetemp_get_smarttemp(st, hwmon_temp_input, &temp);
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun 
drivetemp_identify(struct drivetemp_data * st)434*4882a593Smuzhiyun static int drivetemp_identify(struct drivetemp_data *st)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun 	struct scsi_device *sdev = st->sdev;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	/* Bail out immediately if there is no inquiry data */
439*4882a593Smuzhiyun 	if (!sdev->inquiry || sdev->inquiry_len < 16)
440*4882a593Smuzhiyun 		return -ENODEV;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	/* Disk device? */
443*4882a593Smuzhiyun 	if (sdev->type != TYPE_DISK && sdev->type != TYPE_ZBC)
444*4882a593Smuzhiyun 		return -ENODEV;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	return drivetemp_identify_sata(st);
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun 
drivetemp_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)449*4882a593Smuzhiyun static int drivetemp_read(struct device *dev, enum hwmon_sensor_types type,
450*4882a593Smuzhiyun 			 u32 attr, int channel, long *val)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	struct drivetemp_data *st = dev_get_drvdata(dev);
453*4882a593Smuzhiyun 	int err = 0;
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	if (type != hwmon_temp)
456*4882a593Smuzhiyun 		return -EINVAL;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	switch (attr) {
459*4882a593Smuzhiyun 	case hwmon_temp_input:
460*4882a593Smuzhiyun 	case hwmon_temp_lowest:
461*4882a593Smuzhiyun 	case hwmon_temp_highest:
462*4882a593Smuzhiyun 		mutex_lock(&st->lock);
463*4882a593Smuzhiyun 		err = st->get_temp(st, attr, val);
464*4882a593Smuzhiyun 		mutex_unlock(&st->lock);
465*4882a593Smuzhiyun 		break;
466*4882a593Smuzhiyun 	case hwmon_temp_lcrit:
467*4882a593Smuzhiyun 		*val = st->temp_lcrit;
468*4882a593Smuzhiyun 		break;
469*4882a593Smuzhiyun 	case hwmon_temp_min:
470*4882a593Smuzhiyun 		*val = st->temp_min;
471*4882a593Smuzhiyun 		break;
472*4882a593Smuzhiyun 	case hwmon_temp_max:
473*4882a593Smuzhiyun 		*val = st->temp_max;
474*4882a593Smuzhiyun 		break;
475*4882a593Smuzhiyun 	case hwmon_temp_crit:
476*4882a593Smuzhiyun 		*val = st->temp_crit;
477*4882a593Smuzhiyun 		break;
478*4882a593Smuzhiyun 	default:
479*4882a593Smuzhiyun 		err = -EINVAL;
480*4882a593Smuzhiyun 		break;
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun 	return err;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
drivetemp_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)485*4882a593Smuzhiyun static umode_t drivetemp_is_visible(const void *data,
486*4882a593Smuzhiyun 				   enum hwmon_sensor_types type,
487*4882a593Smuzhiyun 				   u32 attr, int channel)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	const struct drivetemp_data *st = data;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	switch (type) {
492*4882a593Smuzhiyun 	case hwmon_temp:
493*4882a593Smuzhiyun 		switch (attr) {
494*4882a593Smuzhiyun 		case hwmon_temp_input:
495*4882a593Smuzhiyun 			return 0444;
496*4882a593Smuzhiyun 		case hwmon_temp_lowest:
497*4882a593Smuzhiyun 			if (st->have_temp_lowest)
498*4882a593Smuzhiyun 				return 0444;
499*4882a593Smuzhiyun 			break;
500*4882a593Smuzhiyun 		case hwmon_temp_highest:
501*4882a593Smuzhiyun 			if (st->have_temp_highest)
502*4882a593Smuzhiyun 				return 0444;
503*4882a593Smuzhiyun 			break;
504*4882a593Smuzhiyun 		case hwmon_temp_min:
505*4882a593Smuzhiyun 			if (st->have_temp_min)
506*4882a593Smuzhiyun 				return 0444;
507*4882a593Smuzhiyun 			break;
508*4882a593Smuzhiyun 		case hwmon_temp_max:
509*4882a593Smuzhiyun 			if (st->have_temp_max)
510*4882a593Smuzhiyun 				return 0444;
511*4882a593Smuzhiyun 			break;
512*4882a593Smuzhiyun 		case hwmon_temp_lcrit:
513*4882a593Smuzhiyun 			if (st->have_temp_lcrit)
514*4882a593Smuzhiyun 				return 0444;
515*4882a593Smuzhiyun 			break;
516*4882a593Smuzhiyun 		case hwmon_temp_crit:
517*4882a593Smuzhiyun 			if (st->have_temp_crit)
518*4882a593Smuzhiyun 				return 0444;
519*4882a593Smuzhiyun 			break;
520*4882a593Smuzhiyun 		default:
521*4882a593Smuzhiyun 			break;
522*4882a593Smuzhiyun 		}
523*4882a593Smuzhiyun 		break;
524*4882a593Smuzhiyun 	default:
525*4882a593Smuzhiyun 		break;
526*4882a593Smuzhiyun 	}
527*4882a593Smuzhiyun 	return 0;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun static const struct hwmon_channel_info *drivetemp_info[] = {
531*4882a593Smuzhiyun 	HWMON_CHANNEL_INFO(chip,
532*4882a593Smuzhiyun 			   HWMON_C_REGISTER_TZ),
533*4882a593Smuzhiyun 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT |
534*4882a593Smuzhiyun 			   HWMON_T_LOWEST | HWMON_T_HIGHEST |
535*4882a593Smuzhiyun 			   HWMON_T_MIN | HWMON_T_MAX |
536*4882a593Smuzhiyun 			   HWMON_T_LCRIT | HWMON_T_CRIT),
537*4882a593Smuzhiyun 	NULL
538*4882a593Smuzhiyun };
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun static const struct hwmon_ops drivetemp_ops = {
541*4882a593Smuzhiyun 	.is_visible = drivetemp_is_visible,
542*4882a593Smuzhiyun 	.read = drivetemp_read,
543*4882a593Smuzhiyun };
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun static const struct hwmon_chip_info drivetemp_chip_info = {
546*4882a593Smuzhiyun 	.ops = &drivetemp_ops,
547*4882a593Smuzhiyun 	.info = drivetemp_info,
548*4882a593Smuzhiyun };
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun /*
551*4882a593Smuzhiyun  * The device argument points to sdev->sdev_dev. Its parent is
552*4882a593Smuzhiyun  * sdev->sdev_gendev, which we can use to get the scsi_device pointer.
553*4882a593Smuzhiyun  */
drivetemp_add(struct device * dev,struct class_interface * intf)554*4882a593Smuzhiyun static int drivetemp_add(struct device *dev, struct class_interface *intf)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	struct scsi_device *sdev = to_scsi_device(dev->parent);
557*4882a593Smuzhiyun 	struct drivetemp_data *st;
558*4882a593Smuzhiyun 	int err;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	st = kzalloc(sizeof(*st), GFP_KERNEL);
561*4882a593Smuzhiyun 	if (!st)
562*4882a593Smuzhiyun 		return -ENOMEM;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	st->sdev = sdev;
565*4882a593Smuzhiyun 	st->dev = dev;
566*4882a593Smuzhiyun 	mutex_init(&st->lock);
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	if (drivetemp_identify(st)) {
569*4882a593Smuzhiyun 		err = -ENODEV;
570*4882a593Smuzhiyun 		goto abort;
571*4882a593Smuzhiyun 	}
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	st->hwdev = hwmon_device_register_with_info(dev->parent, "drivetemp",
574*4882a593Smuzhiyun 						    st, &drivetemp_chip_info,
575*4882a593Smuzhiyun 						    NULL);
576*4882a593Smuzhiyun 	if (IS_ERR(st->hwdev)) {
577*4882a593Smuzhiyun 		err = PTR_ERR(st->hwdev);
578*4882a593Smuzhiyun 		goto abort;
579*4882a593Smuzhiyun 	}
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	list_add(&st->list, &drivetemp_devlist);
582*4882a593Smuzhiyun 	return 0;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun abort:
585*4882a593Smuzhiyun 	kfree(st);
586*4882a593Smuzhiyun 	return err;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun 
drivetemp_remove(struct device * dev,struct class_interface * intf)589*4882a593Smuzhiyun static void drivetemp_remove(struct device *dev, struct class_interface *intf)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	struct drivetemp_data *st, *tmp;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	list_for_each_entry_safe(st, tmp, &drivetemp_devlist, list) {
594*4882a593Smuzhiyun 		if (st->dev == dev) {
595*4882a593Smuzhiyun 			list_del(&st->list);
596*4882a593Smuzhiyun 			hwmon_device_unregister(st->hwdev);
597*4882a593Smuzhiyun 			kfree(st);
598*4882a593Smuzhiyun 			break;
599*4882a593Smuzhiyun 		}
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun static struct class_interface drivetemp_interface = {
604*4882a593Smuzhiyun 	.add_dev = drivetemp_add,
605*4882a593Smuzhiyun 	.remove_dev = drivetemp_remove,
606*4882a593Smuzhiyun };
607*4882a593Smuzhiyun 
drivetemp_init(void)608*4882a593Smuzhiyun static int __init drivetemp_init(void)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	return scsi_register_interface(&drivetemp_interface);
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
drivetemp_exit(void)613*4882a593Smuzhiyun static void __exit drivetemp_exit(void)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun 	scsi_unregister_interface(&drivetemp_interface);
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun module_init(drivetemp_init);
619*4882a593Smuzhiyun module_exit(drivetemp_exit);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun MODULE_AUTHOR("Guenter Roeck <linus@roeck-us.net>");
622*4882a593Smuzhiyun MODULE_DESCRIPTION("Hard drive temperature monitor");
623*4882a593Smuzhiyun MODULE_LICENSE("GPL");
624*4882a593Smuzhiyun MODULE_ALIAS("platform:drivetemp");
625