* Home page of code is: http://www.smartmontools.org
*
* Copyright (C) 2002-11 Bruce Allen
- * Copyright (C) 2008-15 Christian Franke
+ * Copyright (C) 2008-17 Christian Franke
* Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
* Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org>
*
#include "utility.h"
#include "dev_ata_cmd_set.h" // for parsed_ata_device
-const char * atacmds_cpp_cvsid = "$Id: atacmds.cpp 4301 2016-04-16 20:48:29Z chrfranke $"
+const char * atacmds_cpp_cvsid = "$Id: atacmds.cpp 4582 2017-11-03 20:54:56Z chrfranke $"
ATACMDS_H_CVSID;
// Print ATA debug messages?
return;
}
+// When using the overloaded swapx() function with member of packed ATA structs,
+// it is required to pass a possibly unaligned pointer as argument.
+// Clang++ 4.0 prints -Waddress-of-packed-member warning in this case.
+// The SWAPV() macro below is a replacement which prevents the use of such pointers.
+template <typename T>
+static T get_swapx_val(T x)
+ { swapx(&x); return x; }
+
+#define SWAPV(x) ((x) = get_swapx_val(x))
+
// Invalidate serial number and WWN and adjust checksum in IDENTIFY data
static void invalidate_serno(ata_identify_device * id)
{
#ifndef __NetBSD__
bool must_swap = !!isbigendian();
if (must_swap)
- swapx(id->words088_255+255-88);
+ SWAPV(id->words088_255[255-88]);
#endif
if ((id->words088_255[255-88] & 0x00ff) == 0x00a5)
id->words088_255[255-88] += sum << 8;
#ifndef __NetBSD__
if (must_swap)
- swapx(id->words088_255+255-88);
+ SWAPV(id->words088_255[255-88]);
#endif
}
break;
case STATUS_CHECK:
in.out_needed.lba_high = in.out_needed.lba_mid = true; // Status returned here
+ /* FALLTHRU */
case STATUS:
in.in_regs.features = ATA_SMART_STATUS;
break;
swap2((char *)&(data->revnumber));
swap2((char *)&(data->total_time_to_complete_off_line));
swap2((char *)&(data->smart_capability));
- swapx(&data->extend_test_completion_time_w);
+ SWAPV(data->extend_test_completion_time_w);
for (i=0; i<NUMBER_ATA_SMART_ATTRIBUTES; i++){
struct ata_smart_attribute *x=data->vendor_attributes+i;
swap2((char *)&(x->flags));
check_multi_sector_sum(log, nsectors, "SMART Extended Self-test Log Structure");
if (isbigendian()) {
- swapx(&log->log_desc_index);
+ SWAPV(log->log_desc_index);
for (unsigned i = 0; i < nsectors; i++) {
for (unsigned j = 0; j < 19; j++)
- swapx(&log->log_descs[i].timestamp);
+ SWAPV(log->log_descs[i].timestamp);
}
}
return true;
}
+// Write GP Log page(s)
+bool ataWriteLogExt(ata_device * device, unsigned char logaddr,
+ unsigned page, void * data, unsigned nsectors)
+{
+ ata_cmd_in in;
+ in.in_regs.command = ATA_WRITE_LOG_EXT;
+ in.set_data_out(data, nsectors);
+ in.in_regs.lba_low = logaddr;
+ in.in_regs.lba_mid_16 = page;
+ in.set_data_out(data, nsectors);
+
+ ata_cmd_out out;
+ if (!device->ata_pass_through(in, out)) { // TODO: Debug output
+ if (nsectors <= 1) {
+ pout("ATA_WRITE_LOG_EXT (addr=0x%02x, page=%u, n=%u) failed: %s\n",
+ logaddr, page, nsectors, device->get_errmsg());
+ return false;
+ }
+
+ // Recurse to retry with single sectors,
+ // multi-sector reads may not be supported by ioctl.
+ for (unsigned i = 0; i < nsectors; i++) {
+ if (!ataWriteLogExt(device, logaddr, page + i,
+ (char *)data + 512*i, 1))
+ return false;
+ }
+ }
+
+ return true;
+}
// Read GP Log page(s)
bool ataReadLogExt(ata_device * device, unsigned char logaddr,
// swap endian order if needed
if (isbigendian())
- swapx(&data->logversion);
+ SWAPV(data->logversion);
return 0;
}
check_multi_sector_sum(log, nsectors, "SMART Extended Comprehensive Error Log Structure");
if (isbigendian()) {
- swapx(&log->device_error_count);
- swapx(&log->error_log_index);
+ SWAPV(log->device_error_count);
+ SWAPV(log->error_log_index);
for (unsigned i = 0; i < nsectors; i++) {
for (unsigned j = 0; j < 4; j++) {
for (unsigned k = 0; k < 5; k++)
- swapx(&log[i].error_logs[j].commands[k].timestamp);
- swapx(&log[i].error_logs[j].error.timestamp);
+ SWAPV(log[i].error_logs[j].commands[k].timestamp);
+ SWAPV(log[i].error_logs[j].error.timestamp);
}
}
}
// swap endian order if needed
if (isbigendian()){
- swapx(&sts->format_version);
- swapx(&sts->sct_version);
- swapx(&sts->sct_spec);
- swapx(&sts->ext_status_code);
- swapx(&sts->action_code);
- swapx(&sts->function_code);
- swapx(&sts->over_limit_count);
- swapx(&sts->under_limit_count);
- swapx(&sts->smart_status);
+ SWAPV(sts->format_version);
+ SWAPV(sts->sct_version);
+ SWAPV(sts->sct_spec);
+ SWAPV(sts->ext_status_code);
+ SWAPV(sts->action_code);
+ SWAPV(sts->function_code);
+ SWAPV(sts->over_limit_count);
+ SWAPV(sts->under_limit_count);
+ SWAPV(sts->smart_status);
+ SWAPV(sts->min_erc_time);
}
// Check format version
// swap endian order if needed
if (isbigendian()) {
- swapx(&cmd.action_code);
- swapx(&cmd.function_code);
- swapx(&cmd.table_id);
+ SWAPV(cmd.action_code);
+ SWAPV(cmd.function_code);
+ SWAPV(cmd.table_id);
}
// write command via SMART log page 0xe0
// swap endian order if needed
if (isbigendian()){
- swapx(&tmh->format_version);
- swapx(&tmh->sampling_period);
- swapx(&tmh->interval);
- swapx(&tmh->cb_index);
- swapx(&tmh->cb_size);
+ SWAPV(tmh->format_version);
+ SWAPV(tmh->sampling_period);
+ SWAPV(tmh->interval);
+ SWAPV(tmh->cb_index);
+ SWAPV(tmh->cb_size);
}
return 0;
}
-// Get/Set Write Cache Reordering
-int ataGetSetSCTWriteCacheReordering(ata_device * device, bool enable, bool persistent, bool set)
+// Common function for Get/Set SCT Feature Control:
+// Write Cache, Write Cache Reordering, etc.
+static int ataGetSetSCTFeatureControl(ata_device * device, unsigned short feature_code,
+ unsigned short state, bool persistent, bool set)
{
// Check initial status
ata_sct_status_response sts;
// CAUTION: DO NOT CHANGE THIS VALUE (SOME ACTION CODES MAY ERASE DISK)
cmd.action_code = 4; // Feature Control command
cmd.function_code = (set ? 1 : 2); // 1=Set, 2=Get
- cmd.feature_code = 2; // Enable/Disable Write Cache Reordering
- cmd.state = (enable ? 1 : 2); // 1 enable, 2 disable
+ cmd.feature_code = feature_code;
+ cmd.state = state;
cmd.option_flags = (persistent ? 0x01 : 0x00);
// swap endian order if needed
if (isbigendian()) {
- swapx(&cmd.action_code);
- swapx(&cmd.function_code);
- swapx(&cmd.feature_code);
- swapx(&cmd.state);
- swapx(&cmd.option_flags);
+ SWAPV(cmd.action_code);
+ SWAPV(cmd.function_code);
+ SWAPV(cmd.feature_code);
+ SWAPV(cmd.state);
+ SWAPV(cmd.option_flags);
}
// write command via SMART log page 0xe0
(!set ? 'G' : 'S'), device->get_errmsg());
return -1;
}
- int state = out.out_regs.sector_count | (out.out_regs.lba_low << 8);
+ state = out.out_regs.sector_count | (out.out_regs.lba_low << 8);
// re-read and check SCT status
if (ataReadSCTStatus(device, &sts))
return state;
}
+// Get/Set Write Cache Reordering
+int ataGetSetSCTWriteCacheReordering(ata_device * device, bool enable, bool persistent, bool set)
+{
+ return ataGetSetSCTFeatureControl(device, 2 /* Enable/Disable Write Cache Reordering */,
+ (enable ? 1 : 2), persistent, set);
+}
+
+// Get/Set Write Cache (force enable, force disable,
+int ataGetSetSCTWriteCache(ata_device * device, unsigned short state, bool persistent, bool set)
+{
+ return ataGetSetSCTFeatureControl(device, 1 /* Enable/Disable Write Cache */,
+ state, persistent, set);
+}
// Set SCT Temperature Logging Interval
int ataSetSCTTempInterval(ata_device * device, unsigned interval, bool persistent)
// swap endian order if needed
if (isbigendian()) {
- swapx(&cmd.action_code);
- swapx(&cmd.function_code);
- swapx(&cmd.feature_code);
- swapx(&cmd.state);
- swapx(&cmd.option_flags);
+ SWAPV(cmd.action_code);
+ SWAPV(cmd.function_code);
+ SWAPV(cmd.feature_code);
+ SWAPV(cmd.state);
+ SWAPV(cmd.option_flags);
}
// write command via SMART log page 0xe0
// swap endian order if needed
if (isbigendian()) {
- swapx(&cmd.action_code);
- swapx(&cmd.function_code);
- swapx(&cmd.selection_code);
- swapx(&cmd.time_limit);
+ SWAPV(cmd.action_code);
+ SWAPV(cmd.function_code);
+ SWAPV(cmd.selection_code);
+ SWAPV(cmd.time_limit);
}
// write command via SMART log page 0xe0