xref: /aosp_15_r20/external/sg3_utils/include/sg_io_linux.h (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1*44704f69SBart Van Assche #ifndef SG_IO_LINUX_H
2*44704f69SBart Van Assche #define SG_IO_LINUX_H
3*44704f69SBart Van Assche 
4*44704f69SBart Van Assche /*
5*44704f69SBart Van Assche  * Copyright (c) 2004-2020 Douglas Gilbert.
6*44704f69SBart Van Assche  * All rights reserved.
7*44704f69SBart Van Assche  * Use of this source code is governed by a BSD-style
8*44704f69SBart Van Assche  * license that can be found in the BSD_LICENSE file.
9*44704f69SBart Van Assche  *
10*44704f69SBart Van Assche  * SPDX-License-Identifier: BSD-2-Clause
11*44704f69SBart Van Assche  */
12*44704f69SBart Van Assche 
13*44704f69SBart Van Assche /*
14*44704f69SBart Van Assche  * Version 1.08 [20201102]
15*44704f69SBart Van Assche  */
16*44704f69SBart Van Assche 
17*44704f69SBart Van Assche /*
18*44704f69SBart Van Assche  * This header file contains Linux specific information related to the SCSI
19*44704f69SBart Van Assche  * command pass through in the SCSI generic (sg) driver and the Linux
20*44704f69SBart Van Assche  * block layer.
21*44704f69SBart Van Assche  */
22*44704f69SBart Van Assche 
23*44704f69SBart Van Assche #include "sg_lib.h"
24*44704f69SBart Van Assche #include "sg_linux_inc.h"
25*44704f69SBart Van Assche 
26*44704f69SBart Van Assche #ifdef __cplusplus
27*44704f69SBart Van Assche extern "C" {
28*44704f69SBart Van Assche #endif
29*44704f69SBart Van Assche 
30*44704f69SBart Van Assche /* host_bytes: DID_* are Linux SCSI result (a 32 bit variable) bits 16:23 */
31*44704f69SBart Van Assche #ifndef DID_OK
32*44704f69SBart Van Assche #define DID_OK 0x00
33*44704f69SBart Van Assche #endif
34*44704f69SBart Van Assche #ifndef DID_NO_CONNECT
35*44704f69SBart Van Assche #define DID_NO_CONNECT 0x01     /* Unable to connect before timeout */
36*44704f69SBart Van Assche #define DID_BUS_BUSY 0x02       /* Bus remain busy until timeout */
37*44704f69SBart Van Assche #define DID_TIME_OUT 0x03       /* Timed out for some other reason */
38*44704f69SBart Van Assche #define DID_BAD_TARGET 0x04     /* Bad target (id?) */
39*44704f69SBart Van Assche #define DID_ABORT 0x05          /* Told to abort for some other reason */
40*44704f69SBart Van Assche #define DID_PARITY 0x06         /* Parity error (on SCSI bus) */
41*44704f69SBart Van Assche #define DID_ERROR 0x07          /* Internal error */
42*44704f69SBart Van Assche #define DID_RESET 0x08          /* Reset by somebody */
43*44704f69SBart Van Assche #define DID_BAD_INTR 0x09       /* Received an unexpected interrupt */
44*44704f69SBart Van Assche #define DID_PASSTHROUGH 0x0a    /* Force command past mid-level */
45*44704f69SBart Van Assche #define DID_SOFT_ERROR 0x0b     /* The low-level driver wants a retry */
46*44704f69SBart Van Assche #endif
47*44704f69SBart Van Assche #ifndef DID_IMM_RETRY
48*44704f69SBart Van Assche #define DID_IMM_RETRY 0x0c      /* Retry without decrementing retry count  */
49*44704f69SBart Van Assche #endif
50*44704f69SBart Van Assche #ifndef DID_REQUEUE
51*44704f69SBart Van Assche #define DID_REQUEUE 0x0d        /* Requeue command (no immediate retry) also
52*44704f69SBart Van Assche                                  * without decrementing the retry count    */
53*44704f69SBart Van Assche #endif
54*44704f69SBart Van Assche #ifndef DID_TRANSPORT_DISRUPTED
55*44704f69SBart Van Assche #define DID_TRANSPORT_DISRUPTED 0xe
56*44704f69SBart Van Assche #endif
57*44704f69SBart Van Assche #ifndef DID_TRANSPORT_FAILFAST
58*44704f69SBart Van Assche #define DID_TRANSPORT_FAILFAST 0xf
59*44704f69SBart Van Assche #endif
60*44704f69SBart Van Assche #ifndef DID_TARGET_FAILURE
61*44704f69SBart Van Assche #define DID_TARGET_FAILURE 0x10
62*44704f69SBart Van Assche #endif
63*44704f69SBart Van Assche #ifndef DID_NEXUS_FAILURE
64*44704f69SBart Van Assche #define DID_NEXUS_FAILURE 0x11
65*44704f69SBart Van Assche #endif
66*44704f69SBart Van Assche 
67*44704f69SBart Van Assche /* These defines are to isolate applications from kernel define changes */
68*44704f69SBart Van Assche #define SG_LIB_DID_OK           DID_OK
69*44704f69SBart Van Assche #define SG_LIB_DID_NO_CONNECT   DID_NO_CONNECT
70*44704f69SBart Van Assche #define SG_LIB_DID_BUS_BUSY     DID_BUS_BUSY
71*44704f69SBart Van Assche #define SG_LIB_DID_TIME_OUT     DID_TIME_OUT
72*44704f69SBart Van Assche #define SG_LIB_DID_BAD_TARGET   DID_BAD_TARGET
73*44704f69SBart Van Assche #define SG_LIB_DID_ABORT        DID_ABORT
74*44704f69SBart Van Assche #define SG_LIB_DID_PARITY       DID_PARITY
75*44704f69SBart Van Assche #define SG_LIB_DID_ERROR        DID_ERROR
76*44704f69SBart Van Assche #define SG_LIB_DID_RESET        DID_RESET
77*44704f69SBart Van Assche #define SG_LIB_DID_BAD_INTR     DID_BAD_INTR
78*44704f69SBart Van Assche #define SG_LIB_DID_PASSTHROUGH  DID_PASSTHROUGH
79*44704f69SBart Van Assche #define SG_LIB_DID_SOFT_ERROR   DID_SOFT_ERROR
80*44704f69SBart Van Assche #define SG_LIB_DID_IMM_RETRY    DID_IMM_RETRY
81*44704f69SBart Van Assche #define SG_LIB_DID_REQUEUE      DID_REQUEUE
82*44704f69SBart Van Assche #define SG_LIB_TRANSPORT_DISRUPTED      DID_TRANSPORT_DISRUPTED
83*44704f69SBart Van Assche #define SG_LIB_DID_TRANSPORT_FAILFAST   DID_TRANSPORT_FAILFAST
84*44704f69SBart Van Assche #define SG_LIB_DID_TARGET_FAILURE       DID_TARGET_FAILURE
85*44704f69SBart Van Assche #define SG_LIB_DID_NEXUS_FAILURE        DID_NEXUS_FAILURE
86*44704f69SBart Van Assche 
87*44704f69SBart Van Assche /* DRIVER_* are Linux SCSI result (a 32 bit variable) bits 24:27 */
88*44704f69SBart Van Assche #ifndef DRIVER_OK
89*44704f69SBart Van Assche #define DRIVER_OK 0x00
90*44704f69SBart Van Assche #endif
91*44704f69SBart Van Assche #ifndef DRIVER_BUSY
92*44704f69SBart Van Assche #define DRIVER_BUSY 0x01
93*44704f69SBart Van Assche #define DRIVER_SOFT 0x02
94*44704f69SBart Van Assche #define DRIVER_MEDIA 0x03
95*44704f69SBart Van Assche #define DRIVER_ERROR 0x04
96*44704f69SBart Van Assche #define DRIVER_INVALID 0x05
97*44704f69SBart Van Assche #define DRIVER_TIMEOUT 0x06
98*44704f69SBart Van Assche #define DRIVER_HARD 0x07
99*44704f69SBart Van Assche #define DRIVER_SENSE 0x08       /* Sense_buffer has been set */
100*44704f69SBart Van Assche 
101*44704f69SBart Van Assche /* SUGGEST_* are Linux SCSI result (a 32 bit variable) bits 28:31 */
102*44704f69SBart Van Assche /* N.B. the SUGGEST_* codes are no longer used in Linux and are only kept
103*44704f69SBart Van Assche  * to stop compilation breakages.
104*44704f69SBart Van Assche  * Following "suggests" are "or-ed" with one of previous 8 entries */
105*44704f69SBart Van Assche #define SUGGEST_RETRY 0x10
106*44704f69SBart Van Assche #define SUGGEST_ABORT 0x20
107*44704f69SBart Van Assche #define SUGGEST_REMAP 0x30
108*44704f69SBart Van Assche #define SUGGEST_DIE 0x40
109*44704f69SBart Van Assche #define SUGGEST_SENSE 0x80
110*44704f69SBart Van Assche #define SUGGEST_IS_OK 0xff
111*44704f69SBart Van Assche #endif
112*44704f69SBart Van Assche 
113*44704f69SBart Van Assche #ifndef DRIVER_MASK
114*44704f69SBart Van Assche #define DRIVER_MASK 0x0f
115*44704f69SBart Van Assche #endif
116*44704f69SBart Van Assche #ifndef SUGGEST_MASK
117*44704f69SBart Van Assche #define SUGGEST_MASK 0xf0
118*44704f69SBart Van Assche #endif
119*44704f69SBart Van Assche 
120*44704f69SBart Van Assche /* These defines are to isolate applications from kernel define changes */
121*44704f69SBart Van Assche #define SG_LIB_DRIVER_OK        DRIVER_OK
122*44704f69SBart Van Assche #define SG_LIB_DRIVER_BUSY      DRIVER_BUSY
123*44704f69SBart Van Assche #define SG_LIB_DRIVER_SOFT      DRIVER_SOFT
124*44704f69SBart Van Assche #define SG_LIB_DRIVER_MEDIA     DRIVER_MEDIA
125*44704f69SBart Van Assche #define SG_LIB_DRIVER_ERROR     DRIVER_ERROR
126*44704f69SBart Van Assche #define SG_LIB_DRIVER_INVALID   DRIVER_INVALID
127*44704f69SBart Van Assche #define SG_LIB_DRIVER_TIMEOUT   DRIVER_TIMEOUT
128*44704f69SBart Van Assche #define SG_LIB_DRIVER_HARD      DRIVER_HARD
129*44704f69SBart Van Assche #define SG_LIB_DRIVER_SENSE     DRIVER_SENSE
130*44704f69SBart Van Assche 
131*44704f69SBart Van Assche 
132*44704f69SBart Van Assche /* N.B. the SUGGEST_* codes are no longer used in Linux and are only kept
133*44704f69SBart Van Assche  * to stop compilation breakages. */
134*44704f69SBart Van Assche #define SG_LIB_SUGGEST_RETRY    SUGGEST_RETRY
135*44704f69SBart Van Assche #define SG_LIB_SUGGEST_ABORT    SUGGEST_ABORT
136*44704f69SBart Van Assche #define SG_LIB_SUGGEST_REMAP    SUGGEST_REMAP
137*44704f69SBart Van Assche #define SG_LIB_SUGGEST_DIE      SUGGEST_DIE
138*44704f69SBart Van Assche #define SG_LIB_SUGGEST_SENSE    SUGGEST_SENSE
139*44704f69SBart Van Assche #define SG_LIB_SUGGEST_IS_OK    SUGGEST_IS_OK
140*44704f69SBart Van Assche #define SG_LIB_DRIVER_MASK      DRIVER_MASK
141*44704f69SBart Van Assche #define SG_LIB_SUGGEST_MASK     SUGGEST_MASK
142*44704f69SBart Van Assche 
143*44704f69SBart Van Assche void sg_print_masked_status(int masked_status);
144*44704f69SBart Van Assche void sg_print_host_status(int host_status);
145*44704f69SBart Van Assche void sg_print_driver_status(int driver_status);
146*44704f69SBart Van Assche 
147*44704f69SBart Van Assche /* sg_chk_n_print() returns 1 quietly if there are no errors/warnings
148*44704f69SBart Van Assche  * else it prints errors/warnings (prefixed by 'leadin') to
149*44704f69SBart Van Assche  * 'sg_warnings_fd' and returns 0. raw_sinfo indicates whether the
150*44704f69SBart Van Assche  * raw sense buffer (in ASCII hex) should be printed. */
151*44704f69SBart Van Assche int sg_chk_n_print(const char * leadin, int masked_status, int host_status,
152*44704f69SBart Van Assche                    int driver_status, const uint8_t * sense_buffer,
153*44704f69SBart Van Assche                    int sb_len, bool raw_sinfo);
154*44704f69SBart Van Assche 
155*44704f69SBart Van Assche /* The following function declaration is for the sg version 3 driver. */
156*44704f69SBart Van Assche struct sg_io_hdr;
157*44704f69SBart Van Assche 
158*44704f69SBart Van Assche /* sg_chk_n_print3() returns 1 quietly if there are no errors/warnings;
159*44704f69SBart Van Assche  * else it prints errors/warnings (prefixed by 'leadin') to
160*44704f69SBart Van Assche  * 'sg_warnings_fd' and returns 0. For sg_io_v4 interface use
161*44704f69SBart Van Assche  * sg_linux_sense_print() instead. */
162*44704f69SBart Van Assche int sg_chk_n_print3(const char * leadin, struct sg_io_hdr * hp,
163*44704f69SBart Van Assche                     bool raw_sinfo);
164*44704f69SBart Van Assche 
165*44704f69SBart Van Assche /* Returns 1 if no errors found and thus nothing printed; otherwise
166*44704f69SBart Van Assche  * prints error/warning (prefix by 'leadin') to stderr (pr2ws) and
167*44704f69SBart Van Assche  * returns 0. */
168*44704f69SBart Van Assche int sg_linux_sense_print(const char * leadin, int scsi_status,
169*44704f69SBart Van Assche                          int host_status, int driver_status,
170*44704f69SBart Van Assche                          const uint8_t * sense_buffer, int sb_len,
171*44704f69SBart Van Assche                          bool raw_sinfo);
172*44704f69SBart Van Assche 
173*44704f69SBart Van Assche /* Calls sg_scsi_normalize_sense() after obtaining the sense buffer and
174*44704f69SBart Van Assche  * its length from the struct sg_io_hdr pointer. If these cannot be
175*44704f69SBart Van Assche  * obtained, false is returned. For sg_io_v4 interface use
176*44704f69SBart Van Assche  * sg_scsi_normalize_sense() function instead [see sg_lib.h].  */
177*44704f69SBart Van Assche bool sg_normalize_sense(const struct sg_io_hdr * hp,
178*44704f69SBart Van Assche                         struct sg_scsi_sense_hdr * sshp);
179*44704f69SBart Van Assche 
180*44704f69SBart Van Assche /* Returns SG_LIB_CAT_* value. */
181*44704f69SBart Van Assche int sg_err_category(int masked_status, int host_status, int driver_status,
182*44704f69SBart Van Assche                     const uint8_t * sense_buffer, int sb_len);
183*44704f69SBart Van Assche 
184*44704f69SBart Van Assche /* Returns SG_LIB_CAT_* value. */
185*44704f69SBart Van Assche int sg_err_category_new(int scsi_status, int host_status, int driver_status,
186*44704f69SBart Van Assche                         const uint8_t * sense_buffer, int sb_len);
187*44704f69SBart Van Assche 
188*44704f69SBart Van Assche /* The following function declaration is for the sg version 3 driver. for
189*44704f69SBart Van Assche  * sg_io_v4 interface use sg_err_category_new() function instead */
190*44704f69SBart Van Assche int sg_err_category3(struct sg_io_hdr * hp);
191*44704f69SBart Van Assche 
192*44704f69SBart Van Assche 
193*44704f69SBart Van Assche /* Note about SCSI status codes found in older versions of Linux.
194*44704f69SBart Van Assche  * Linux has traditionally used a 1 bit right shifted and masked
195*44704f69SBart Van Assche  * version of SCSI standard status codes. Now CHECK_CONDITION
196*44704f69SBart Van Assche  * and friends (in <scsi/scsi.h>) are deprecated. */
197*44704f69SBart Van Assche 
198*44704f69SBart Van Assche #ifdef __cplusplus
199*44704f69SBart Van Assche }
200*44704f69SBart Van Assche #endif
201*44704f69SBart Van Assche 
202*44704f69SBart Van Assche #endif
203