xref: /aosp_15_r20/external/sg3_utils/examples/sg_sat_chk_power.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1 /*
2  * Copyright (c) 2006-2018 Douglas Gilbert.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * SPDX-License-Identifier: BSD-2-Clause
27  */
28 
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 
40 #include "sg_lib.h"
41 #include "sg_pr2serr.h"
42 #include "sg_io_linux.h"
43 
44 /* This program performs a ATA PASS-THROUGH (16) SCSI command in order
45    to perform an ATA CHECK POWER MODE command. See http://www.t10.org
46    SAT draft at time of writing: sat-r09.pdf
47 
48    Invocation: sg_sat_chk_power [-v] [-V] <device>
49 
50 */
51 
52 #define SAT_ATA_PASS_THROUGH16 0x85
53 #define SAT_ATA_PASS_THROUGH16_LEN 16
54 #define SAT_ATA_RETURN_DESC 9  /* ATA Return (sense) Descriptor */
55 #define ASCQ_ATA_PT_INFO_AVAILABLE 0x1d
56 
57 #define ATA_CHECK_POWER_MODE 0xe5
58 
59 #define EBUFF_SZ 256
60 
61 static const char * version_str = "1.08 20181207";
62 
63 
64 #if 0
65 /* Returns length of decoded fixed format sense for SAT ATA pass-through
66  * command, else returns 0. If returns 0 (expected sense data not found)
67  * then '\0' placed in first byte of bp. */
68 static int
69 sg_sat_decode_fixed_sense(const uint8_t * sp, int slen, char * bp,
70                           int max_blen, int verbose)
71 {
72     int n;
73 
74     if ((NULL == bp) || (NULL == sp) || (max_blen < 1) || (slen < 14))
75         return 0;
76     bp[0] = '\0';
77     if ((0x70 != (0x7f & sp[0])) ||
78         (SPC_SK_RECOVERED_ERROR != (0xf & sp[2])) ||
79         (0 != sp[12]) || (ASCQ_ATA_PT_INFO_AVAILABLE != sp[13]))
80         return 0;
81     n = sg_scnpr(bp, max_blen, "error=0x%x, status=0x%x, device=0x%x, "
82                  "sector_count(7:0)=0x%x%c\n", sp[3], sp[4], sp[5], sp[6],
83                  ((0x40 & sp[8]) ? '+' : ' '));
84     if (n >= max_blen)
85         return max_blen - 1;
86     n += sg_scnpr(bp + n, max_blen - n, "extend=%d, log_index=0x%x, "
87                   "lba_high,mid,low(7:0)=0x%x,0x%x,0x%x%c\n",
88                   (!!(0x80 & sp[8])), (0xf & sp[8]), sp[9], sp[10], sp[11],
89                   ((0x20 & sp[8]) ? '+' : ' '));
90     if (n >= max_blen)
91         return max_blen - 1;
92     if (verbose)
93         n += sg_scnpr(bp + n, max_blen - n, "  sector_count_upper_nonzero="
94                       "%d, lba_upper_nonzero=%d\n", !!(0x40 & sp[8]),
95                       !!(0x20 & sp[8]));
96     return (n >= max_blen) ? max_blen - 1 : n;
97 }
98 #endif
99 
main(int argc,char * argv[])100 int main(int argc, char * argv[])
101 {
102     int sg_fd, k;
103     uint8_t apt_cdb[SAT_ATA_PASS_THROUGH16_LEN] =
104                 {SAT_ATA_PASS_THROUGH16, 0, 0, 0, 0, 0, 0, 0,
105                  0, 0, 0, 0, 0, 0, 0, 0};
106     sg_io_hdr_t io_hdr;
107     char * file_name = 0;
108     char ebuff[EBUFF_SZ];
109     uint8_t sense_buffer[64];
110     int verbose = 0;
111     int extend = 0;
112     int chk_cond = 1;   /* set to 1 to read register(s) back */
113     int protocol = 3;   /* non-dat data-in */
114     int t_dir = 1;      /* 0 -> to device, 1 -> from device */
115     int byte_block = 1; /* 0 -> bytes, 1 -> 512 byte blocks */
116     int t_length = 0;   /* 0 -> no data transferred, 2 -> sector count */
117     const uint8_t * bp = NULL;
118 
119     for (k = 1; k < argc; ++k) {
120         if (0 == strcmp(argv[k], "-v"))
121             ++verbose;
122         else if (0 == strcmp(argv[k], "-vv"))
123             verbose += 2;
124         else if (0 == strcmp(argv[k], "-vvv"))
125             verbose += 3;
126         else if (0 == strcmp(argv[k], "-V")) {
127             fprintf(stderr, "version: %s\n", version_str);
128             exit(0);
129         } else if (*argv[k] == '-') {
130             printf("Unrecognized switch: %s\n", argv[k]);
131             file_name = 0;
132             break;
133         }
134         else if (0 == file_name)
135             file_name = argv[k];
136         else {
137             printf("too many arguments\n");
138             file_name = 0;
139             break;
140         }
141     }
142     if (0 == file_name) {
143         printf("Usage: 'sg_sat_chk_power [-v] [-V] <device>'\n");
144         return 1;
145     }
146 
147     if ((sg_fd = open(file_name, O_RDWR)) < 0) {
148         snprintf(ebuff, EBUFF_SZ,
149                  "sg_sat_chk_power: error opening file: %s", file_name);
150         perror(ebuff);
151         return 1;
152     }
153 
154     /* Prepare ATA PASS-THROUGH COMMAND (16) command */
155     apt_cdb[14] = ATA_CHECK_POWER_MODE;
156     apt_cdb[1] = (protocol << 1) | extend;
157     apt_cdb[2] = (chk_cond << 5) | (t_dir << 3) |
158                    (byte_block << 2) | t_length;
159     if (verbose) {
160         fprintf(stderr, "    ata pass through(16) cdb: ");
161         for (k = 0; k < SAT_ATA_PASS_THROUGH16_LEN; ++k)
162             fprintf(stderr, "%02x ", apt_cdb[k]);
163         fprintf(stderr, "\n");
164     }
165 
166     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
167     io_hdr.interface_id = 'S';
168     io_hdr.cmd_len = sizeof(apt_cdb);
169     /* io_hdr.iovec_count = 0; */  /* memset takes care of this */
170     io_hdr.mx_sb_len = sizeof(sense_buffer);
171     io_hdr.dxfer_direction = SG_DXFER_NONE;
172     io_hdr.dxfer_len = 0;
173     io_hdr.dxferp = NULL;
174     io_hdr.cmdp = apt_cdb;
175     io_hdr.sbp = sense_buffer;
176     io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */
177     /* io_hdr.flags = 0; */     /* take defaults: indirect IO, etc */
178     /* io_hdr.pack_id = 0; */
179     /* io_hdr.usr_ptr = NULL; */
180 
181     if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
182         perror("sg_sat_chk_power: SG_IO ioctl error");
183         close(sg_fd);
184         return 1;
185     }
186 
187     /* error processing: N.B. expect check condition, no sense ... !! */
188     switch (sg_err_category3(&io_hdr)) {
189     case SG_LIB_CAT_CLEAN:
190         break;
191     case SG_LIB_CAT_RECOVERED:  /* sat-r09 (latest) uses this sk */
192     case SG_LIB_CAT_NO_SENSE:   /* earlier SAT drafts used this */
193         /* XXX: Until the spec decides which one to go with. 20060607 */
194         bp = sg_scsi_sense_desc_find(sense_buffer, sizeof(sense_buffer),
195                                      SAT_ATA_RETURN_DESC);
196         if (NULL == bp) {
197             if (verbose > 1)
198                 printf("ATA Return Descriptor expected in sense but not "
199                        "found\n");
200             sg_chk_n_print3("ATA_16 command error", &io_hdr, 1);
201         } else if (verbose)
202             sg_chk_n_print3("ATA Return Descriptor, as expected",
203                              &io_hdr, 1);
204         if (bp && bp[3]) {
205             if (bp[3] & 0x4)
206                 printf("error in returned FIS: aborted command\n");
207             else
208                 printf("error=0x%x, status=0x%x\n", bp[3], bp[13]);
209         }
210         break;
211     default:
212         fprintf(stderr, "unexpected SCSI sense category\n");
213         bp = sg_scsi_sense_desc_find(sense_buffer, sizeof(sense_buffer),
214                                      SAT_ATA_RETURN_DESC);
215         if (NULL == bp)
216             sg_chk_n_print3("ATA_16 command error", &io_hdr, 1);
217         else if (verbose)
218             sg_chk_n_print3("ATA Return Descriptor, as expected",
219                              &io_hdr, 1);
220         if (bp && bp[3]) {
221             if (bp[3] & 0x4)
222                 printf("error in returned FIS: aborted command\n");
223             else
224                 printf("error=0x%x, status=0x%x\n", bp[3], bp[13]);
225         }
226         break;
227     }
228 
229     if (bp) {
230         switch (bp[5]) {       /* sector_count (7:0) */
231         case 0xff:
232             printf("In active mode or idle mode\n");
233             break;
234         case 0x80:
235             printf("In idle mode\n");
236             break;
237         case 0x41:
238             printf("In NV power mode and spindle is spun or spinning up\n");
239             break;
240         case 0x40:
241             printf("In NV power mode and spindle is spun or spinning down\n");
242             break;
243         case 0x0:
244             printf("In standby mode\n");
245             break;
246         default:
247             printf("unknown power mode (sector count) value=0x%x\n", bp[5]);
248             break;
249         }
250     } else
251         fprintf(stderr, "Expecting a ATA Return Descriptor in sense and "
252                 "didn't receive it\n");
253 
254     close(sg_fd);
255     return 0;
256 }
257