Ztex
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • Sharing URL Link copied
        • /edit
        • View mode
          • Edit mode
          • View mode
          • Book mode
          • Slide mode
          Edit mode View mode Book mode Slide mode
        • Customize slides
        • Note Permission
        • Read
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invite by email
        Invitee

        This note has no invitees

      • Publish Note

        Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

        Your note will be visible on your profile and discoverable by anyone.
        Your note is now live.
        This note is visible on your profile and discoverable online.
        Everyone on the web can find and read all notes of this public team.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        See published notes
        Unpublish note
        Please check the box to agree to the Community Guidelines.
        View profile
      • Commenting
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Suggest edit
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
      • Emoji Reply
      • Enable
      • Versions and GitHub Sync
      • Note settings
      • Note Insights New
      • Engagement control
      • Make a copy
      • Transfer ownership
      • Delete this note
      • Save as template
      • Insert from template
      • Import from
        • Dropbox
        • Google Drive
        • Gist
        • Clipboard
      • Export to
        • Dropbox
        • Google Drive
        • Gist
      • Download
        • Markdown
        • HTML
        • Raw HTML
    Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Engagement control Make a copy Transfer ownership Delete this note
    Import from
    Dropbox Google Drive Gist Clipboard
    Export to
    Dropbox Google Drive Gist
    Download
    Markdown HTML Raw HTML
    Back
    Sharing URL Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Customize slides
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # SCSI (simple computer system interface) and SATA (serial advanced technology attachment) ###### tags: `SCSI` ###### tags: `SATA` > >[name=ztex][time=2020,08,19] ## :memo: What is SCSI? 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. ## :memo: original ATA(a.k.a PATA, parallel ATA) 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. ## :memo: What is SATA 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. ## :memo: SCSI vs. ATA ### :date: In what sense does SATA “talk” SCSI? How much is shared between SCSI and ATA? https://unix.stackexchange.com/questions/144561/in-what-sense-does-sata-talk-scsi-how-much-is-shared-between-scsi-and-ata **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. ### :date: SCSI / ATA Translation (SAT) 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 code**s: 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%20stands%20for%20Serial%20Advanced,pronounced%20as%20%E2%80%9Cscuzzy%E2%80%9D).&text=The%20main%20difference%20between%20them,more%20reliable%20than%20SATA%20drives. ## :memo: ztex conclusion 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 >[name=ztex][time=2020,08,19] ## :memo: Linux kernel storage stack ![image alt](https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/The_Linux_Storage_Stack_Diagram.svg/1052px-The_Linux_Storage_Stack_Diagram.svg.png) ## :memo: What is `sg_raw` references: * [sg_raw](https://linux.die.net/man/8/sg_raw) * [tutorial](https://www.itdaan.com/tw/eac29e11fc52cd358118fc8d1b4d347d) * [ATA command set](http://www.t13.org/Documents/UploadedDocuments/docs2016/di529r14-ATAATAPI_Command_Set_-_4.pdf) * [sg_sat_chk_power.c](https://salsa.debian.org/linux-blocks-team/sg3-utils/-/blob/master/examples/sg_sat_chk_power.c) * [Linux Storage Stack Diagram](https://developpaper.com/linux-storage-stack-diagram/) * [Storage Stack](https://wxdublin.gitbooks.io/deep-into-linux-and-beyond/content/io.html) ### Description This utility sends an **arbitrary SCSI command** (between 6 and 256 bytes) to the **DEVICE**. ### :question: inputs ![](https://i.imgur.com/KZDiHcz.png) ![](https://i.imgur.com/L2mcQLP.png) >這邊給出 ATA command 的格式 >>FEATURE: 你要用哪個 Feature set. 像如果我們要 send 一條 ATA command over SCSI, 我們就可以使用上面提到的`passthrough feature set`, `0x85` > >>COUNT: 我們這邊都使用 Non data > >>COMMAND: 我們要送的 command, 比如 check power mode 即是 `0xe5` ### Examples ```= 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. ``` ### Put HD in DS920+ in `standby immediate` ```shell= $> sg_raw /dev/sata2 A1 06 00 00 00 00 00 00 00 E0 00 00 SCSI Status: Good Sense Information: sense buffer empty ``` ### HD in DS920+ in `power check mode` * see spec: `4.16 Power Management feature set` * In spec: `6 Normal and Error Output field descriptions` | **Field** | |:---------:| |STATUS| |ERROR| |INTERRUPT REASON| |COUNT| |SACTIVE| |SATA STATUS| |LBA| ```shell= $> 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 ``` * see spec: `Table 313 — Check Power Mode Normal Output`, to understand the normal output * count=0x00, means: * `PM2:Standby state (see 4.16.4) and the EPC feature set (see 4.9) is not enabled;` or * `PM2:Standby state, the EPC feature set is enabled, and the device is in the Standby_z power condition (see 4.9.2).` * count=0xff: * `Device is in the PM0:Active state or PM1:Idle state.` - [ ] `85`: SAT ATA PASS THROUGH - [ ] see: https://elixir.bootlin.com/linux/latest/source/include/scsi/scsi_proto.h#L167 - [ ] `06`: - [ ] Non-data: see: https://elixir.bootlin.com/linux/latest/source/drivers/ata/libata-scsi.c#L389 - [ ] ![](https://i.imgur.com/d1tSMQc.png) - [ ] `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 ## :memo: HDIO_ ioctl calls reference: https://www.kernel.org/doc/html/latest/userspace-api/ioctl/hdio.html#HDIO_DRIVE_CMD also see: 1. https://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/sg_io.html 2. https://man7.org/linux/man-pages/man8/hdparm.8.html 3. SCSI/ATA Translation: https://www.t10.org/ftp/t10/document.04/04-136r0.pdf 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 ```cpp #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 in `args`: [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 > > [name=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 > [name=ztex] ## :memo: What is PUIS 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 的時候不讓硬碟轉起來, 這樣可以省電 >

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully