SCSI
SATA
ztex2020,08,19
The Small Computer System Interface (SCSI) is a set of parallel interface standards developed by the American National Standards Institute (ANSI) for attaching printers, disk drives, scanners and other peripherals to computers.
Parallel ATA (PATA), originally AT Attachment, is an interface standard for the connection of storage devices such as hard disk drives, floppy disk drives, and optical disc drives in computers. The standard is maintained by the X3/INCITS committee.[1] It uses the underlying AT Attachment (ATA) and AT Attachment Packet Interface (ATAPI) standards.
The Parallel ATA standard is the result of a long history of incremental technical development, which began with the original AT Attachment interface, developed for use in early PC AT equipment. The ATA interface itself evolved in several stages from Western Digital's original Integrated Drive Electronics (IDE) interface. As a result, many near-synonyms for ATA/ATAPI and its previous incarnations are still in common informal use, in particular Extended IDE (EIDE) and Ultra ATA (UATA). After the introduction of Serial ATA (SATA) in 2003, the original ATA was renamed to Parallel ATA, or PATA for short.
Parallel ATA cables have a maximum allowable length of 18 in (457 mm).[2][3] Because of this limit, the technology normally appears as an internal computer storage interface. For many years, ATA provided the most common and the least expensive interface for this application. It has largely been replaced by SATA in newer systems.
Serial ATA (SATA, abbreviated from Serial AT Attachment) is a computer bus interface that connects host bus adapters to mass storage devices such as hard disk drives, optical drives, and solid-state drives. Serial ATA succeeded the earlier Parallel ATA (PATA) standard to become the predominant interface for storage devices.
SCSI and ATA are entirely different standards. They are currently both developed under the aegis of the INCITS standards organization but by different groups. SCSI is under technical committee T10, while ATA is under T13.1
ATA was designed with hard disk drives in mind, only. SCSI is both broader and older, being a standard way of controlling mass storage devices, tape drives, removable optical media drives (CD, DVD, Blu-Ray…), scanners, and many other device types.
It wasn't obvious in the mid-1980s — when IDE was introduced to the PC world — that SCSI would get pushed to the margins of the computing world. SCSI was well-established and more capable. Unix workstations and Macintosh computers shipped with SCSI hard disk drives for decades. High-end PCs often had a SCSI card for peripherals at least, and often for the system HDD, too. The early CD-ROM and tape drives for personal computers came out in SCSI form first.
The PC industry being what it is, though, there was a push to use the less expensive ATA standard instead of SCSI. The initial compromise was called ATAPI, an extension to ATA that allows a device that understands SCSI internally to receive those SCSI commands over an ATA interface. More on this below.
SCSI command, ATA interface
Several years later, SCSI got the ATA command pass-through feature, basically the inverse of ATAPI, allowing ATA commands over a SCSI bus. One use for this facility is to tunnel ATA SMART commands over SCSI. smartmontools does this, for example.
ATA command, SCSI interface
Later still, the INCITS T10 committee developed a standard called the SCSI/ATA Translation (SAT), which translates SCSI commands to ATA commands and vice versa.2 The Linux kernel's libata
library provides a SAT implementation for Linux, among other things.
T10: SCSI/ATA Translation(SAT), SCSI commands -> ATA commands
There is some logical overlap in the SCSI and ATA protocols, since they both control hard disk drives. Both obviously need a way to seek to a particular hard drive sector, retrieve that sector's contents, etc. Nevertheless, the command formats are entirely different; otherwise, we wouldn't need these translation and pass-through mechanisms.
SCSI / ATA Translation (SAT) is a set of standards developed by the T10 subcommittee, defining how to communicate with ATA devices through a SCSI application layer. The standard attempts to be consistent with the SCSI architectural model, the SCSI Primary Commands, and the SCSI Block Commands standards.
The standard allows for translation of common SCSI Block Commands such as:
SCSI Read Commands
SCSI Write Commands
The standard also provides the ability to control exactly what ATA operations are executed on a target device by defining two new SCSI operation codes:
ATA PASS THROUGH (Ax, 12-byte) – 28-bit ATA command without AUXILIARY or ICC fields
ATA PASS THROUGH (8x, 16-byte) – 28- or 48-bit ATA command without AUXILIARY or ICC fields
Both of them are interface.
The main difference between them is that SAS drives are faster and more reliable than SATA drives.
reference: https://www.pickaweb.co.uk/kb/difference-between-sata-sas-hard-drives/#:~:text=SATA stands for Serial Advanced,pronounced as “scuzzy”).&text=The%20main%20difference%20between%20them,more%20reliable%20than%20SATA%20drives.
SCSI is broader and older -> IDE was introduced -> ATA became important -> Need ATA command over SCSI -> ATAPI (scsi command over ATA device) -> ATA: Passthrough feature set, smart command (ata command over scsi) -> T10: SAT (SCSI/ATA translation)
總而言之就是, SCSI 歷史悠久, 包括 CD, DVD …, 大家都用 SCSI 相安無事.
IDE 出現, ATA 變得重要了, 但是 SCSI 這個東西又太廣泛了, 怎麼辦?
ATAPI: 給我 SCSI command 我讓她 run over ATA device
ATA: 出了個 Passthrough feature set, 只要是 SMART command 都可以 run over SCSI
T10 委員會跳出來喊了個SAT
(SCSI/ATA Translation
) 從此可以轉換了SCSI為此多了兩種格式, 28 bits, 48bits, 這可以在 ATA spec 上看到
這也是為甚麼sg_raw
可以送 ATA command
ztex2020,08,19
sg_raw
references:
This utility sends an arbitrary SCSI command (between 6 and 256 bytes) to the DEVICE.
這邊給出 ATA command 的格式
FEATURE: 你要用哪個 Feature set. 像如果我們要 send 一條 ATA command over SCSI, 我們就可以使用上面提到的
passthrough feature set
,0x85
COUNT: 我們這邊都使用 Non data
COMMAND: 我們要送的 command, 比如 check power mode 即是
0xe5
sg_raw /dev/scd0 1b 00 00 00 02 00
Eject the medium in CD drive /dev/scd0.
sg_raw -r 1k /dev/sg0 12 00 00 00 60 00
Perform an INQUIRY on /dev/sg0 and dump the response data (up to 1024 bytes) to stdout.
sg_raw -s 512 -i i512.bin /dev/sda 3b 02 00 00 00 00 00 02 00 00
Showing an example of writing 512 bytes to a sector on a disk is a little dangerous. Instead this example will read i512.bin (assumed to be 512 bytes long) and use the SCSI WRITE BUFFER command to send it to the "data" buffer (that is mode 2). This is a safe operation.
sg_raw -r 512 -o o512.bin /dev/sda 3c 02 00 00 00 00 00 02 00 00
This will use the SCSI READ BUFFER command to read 512 bytes from the "data" buffer (i.e. mode 2) then write it to the o512.bin file. When used in conjunction with the previous example, if both commands work then 'cmp i512.bin o512.bin' should show a match.
standby immediate
$> sg_raw /dev/sata2 A1 06 00 00 00 00 00 00 00 E0 00 00
SCSI Status: Good
Sense Information:
sense buffer empty
power check mode
4.16 Power Management feature set
6 Normal and Error Output field descriptions
Field |
---|
STATUS |
ERROR |
INTERRUPT REASON |
COUNT |
SACTIVE |
SATA STATUS |
LBA |
$> sg_raw /dev/sata2 85 06 2C 00 00 00 00 00 00 00 00 00 00 00 E5 00
SCSI Status: Check Condition
Sense Information:
Descriptor format, current; Sense key: Recovered Error
Additional sense: ATA pass through information available
Descriptor type: ATA Status Return
extend=0 error=0x0 sector_count=0x0
lba=0x000000
device=0x40 status=0x50
Table 313 — Check Power Mode Normal Output
, to understand the normal output
PM2:Standby state (see 4.16.4) and the EPC feature set (see 4.9) is not enabled;
orPM2:Standby state, the EPC feature set is enabled, and the device is in the Standby_z power condition (see 4.9.2).
Device is in the PM0:Active state or PM1:Idle state.
85
: SAT ATA PASS THROUGH
06
:
2C
: chk_cond
= 1, set to 1 to read register(s) back; t_dir
= 1, 0 -> to device, 1 -> from device; byte_block
= 1, 0 -> bytes, 1 -> 512 byte blocks; t_length
= 0, 0 -> no data transferred, 2 -> sector count. (chk_cond << 5) | (t_dir << 3) | (byte_block << 2) | t_length;
E5
: ATA CHECK POWER MODE, see 7.3
in spec
reference: https://www.kernel.org/doc/html/latest/userspace-api/ioctl/hdio.html#HDIO_DRIVE_CMD
also see:
This is implemented in drivers/ide/ide.c
, linux/hdreg.h
In project hdparm
: https://github.com/Distrotech/hdparm, I learn this way to control a HD/IDE.
Here is my application
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define HDIO_DRIVE_CMD 0x031f
#define ATA_OP_STANDBYNOW 0xe0
#define ATA_OP_CHECKPOWERMODE 0xe5
#define ARGS_STANDBYNOW(x) uint8_t x[4] = {ATA_OP_STANDBYNOW,0,0,0};
#define ARGS_CHECKPOWERMODE(x) uint8_t x[4] = {ATA_OP_CHECKPOWERMODE,0,0,0};
dev_fd = open(dev_file, O_RDWR);
if (dev_fd == -1) {
printf("open %s failed.\n", dev_file);
return -1;
}
ARGS_STANDBYNOW(args_standbynow);
ARGS_CHECKPOWERMODE(args_checkpowermode);
ret = ioctl(dev_fd, HDIO_DRIVE_CMD, args_standbynow);
if (ret == -1) {
printf("ioctl standby immediately failed\n");
return -1;
}
ret = ioctl(dev_fd, HDIO_DRIVE_CMD, args_checkpowermode);
if (ret == -1) {
printf("ioctl standby immediately failed\n");
return -1;
}
switch (args_checkpowermode[2]) {
case 0x00:
printf("The disk is in standby mode\n");
break;
case 0x40:
printf("The disk is in NVcache_spindown mode\n");
return -1;
break;
case 0x41:
printf("The disk is in NVcache_spinup mode\n");
return -1;
break;
case 0x80:
printf("The disk is in idle mode\n");
return -1;
break;
case 0xff:
printf("The disk is in active/idle mode\n");
return -1;
break;
default:
printf("Unknown state\n");
return -1;
break;
}
Linux 對於 HD/IDE 有對應的 support ioctl
調用open
得到對應 device 的 descriptor
ioctl command,HDIO_DRIVE_CMD
定義在https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/hdreg.h#L355
args 給定u8 args[4]
: 分別是: [0]command [1]nsector [2]feature [3]nsector
return 的 register value 透過copy_to_user
fill inargs
: [0]status [1]error [2]nsector [3] undefined [4+]NSECTOR * 512 bytes of data returned by the command.對應的實作在: https://elixir.bootlin.com/linux/latest/source/drivers/ide/ide-ioctls.c#L127
跟: https://elixir.bootlin.com/linux/latest/source/drivers/ata/libata-scsi.c#L460最後轉換成 scsi command
對應的實作在: https://elixir.bootlin.com/linux/latest/source/drivers/scsi/scsi_lib.c#L241
ztex
hdparm 對應的使用在: https://github.com/Distrotech/hdparm/blob/master/hdparm.c 2136 行, get power mode 的 ioctl
HD_DRIVE_CMD 定義在: https://github.com/Distrotech/hdparm/blob/master/hdparm.h
ATA command 定義在: https://github.com/Distrotech/hdparm/blob/master/sgio.h
ztex
reference: https://en.wikipedia.org/wiki/Power-up_in_standby
Power-up in standby (PUIS) or power management 2 mode (PM2; Western Digital specific) is a SATA or Parallel ATA (aka PATA) hard disk configuration which prevents the drive from automatic spinup when power is applied. The spinup occurs later by an ATA command, only when the disk is needed, to conserve electric power and to avoid a power consumption peak caused by a simultaneous spin-up of multiple disks. Typical power consumption of desktop drives is several watts when active/idle and several tenths of a watt when in standby.
當電源 apply 的時候不讓硬碟轉起來, 這樣可以省電