1 #ifndef SG_CMDS_BASIC_H 2 #define SG_CMDS_BASIC_H 3 4 /* 5 * Copyright (c) 2004-2014 Douglas Gilbert. 6 * All rights reserved. 7 * Use of this source code is governed by a BSD-style 8 * license that can be found in the BSD_LICENSE file. 9 */ 10 11 /* 12 * Error, warning and verbose output is sent to the file pointed to by 13 * sg_warnings_strm which is declared in sg_lib.h and can be set with 14 * the sg_set_warnings_strm() function. If not given sg_warnings_strm 15 * defaults to stderr. 16 * If 'noisy' and 'verbose' are both zero then following functions should 17 * not output anything to sg_warnings_strm. If 'noisy' is non-zero and 18 * 'verbose' is zero then Unit Attention, Recovered, Medium and Hardware 19 * errors (sense keys) send output to sg_warnings_strm. Increasing values 20 * of 'verbose' send increasing amounts of (debug) output to 21 * sg_warnings_strm. 22 */ 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 29 /* Invokes a SCSI INQUIRY command and yields the response 30 * Returns 0 when successful, SG_LIB_CAT_INVALID_OP -> not supported, 31 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, 32 * SG_LIB_CAT_ABORTED_COMMAND, -1 -> other errors */ 33 int sg_ll_inquiry(int sg_fd, int cmddt, int evpd, int pg_op, void * resp, 34 int mx_resp_len, int noisy, int verbose); 35 36 /* Invokes a SCSI LOG SELECT command. Return of 0 -> success, 37 * SG_LIB_CAT_INVALID_OP -> Log Select not supported, 38 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, SG_LIB_CAT_UNIT_ATTENTION, 39 * SG_LIB_CAT_ABORTED_COMMAND, * SG_LIB_CAT_NOT_READY -> device not ready, 40 * -1 -> other failure */ 41 int sg_ll_log_select(int sg_fd, int pcr, int sp, int pc, int pg_code, 42 int subpg_code, unsigned char * paramp, int param_len, 43 int noisy, int verbose); 44 45 /* Invokes a SCSI LOG SENSE command. Return of 0 -> success, 46 * SG_LIB_CAT_INVALID_OP -> Log Sense not supported, 47 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, SG_LIB_CAT_UNIT_ATTENTION, 48 * SG_LIB_CAT_NOT_READY -> device not ready, SG_LIB_CAT_ABORTED_COMMAND, 49 * -1 -> other failure */ 50 int sg_ll_log_sense(int sg_fd, int ppc, int sp, int pc, int pg_code, 51 int subpg_code, int paramp, unsigned char * resp, 52 int mx_resp_len, int noisy, int verbose); 53 54 /* Invokes a SCSI MODE SELECT (6) command. Return of 0 -> success, 55 * SG_LIB_CAT_INVALID_OP -> invalid opcode, SG_LIB_CAT_ILLEGAL_REQ -> 56 * bad field in cdb, * SG_LIB_CAT_NOT_READY -> device not ready, 57 * SG_LIB_CAT_UNIT_ATTENTION, SG_LIB_CAT_ABORTED_COMMAND, 58 * -1 -> other failure */ 59 int sg_ll_mode_select6(int sg_fd, int pf, int sp, void * paramp, 60 int param_len, int noisy, int verbose); 61 62 /* Invokes a SCSI MODE SELECT (10) command. Return of 0 -> success, 63 * SG_LIB_CAT_INVALID_OP -> invalid opcode, SG_LIB_CAT_ILLEGAL_REQ -> 64 * bad field in cdb, * SG_LIB_CAT_NOT_READY -> device not ready, 65 * SG_LIB_CAT_UNIT_ATTENTION, SG_LIB_CAT_ABORTED_COMMAND, 66 * -1 -> other failure */ 67 int sg_ll_mode_select10(int sg_fd, int pf, int sp, void * paramp, 68 int param_len, int noisy, int verbose); 69 70 /* Invokes a SCSI MODE SENSE (6) command. Return of 0 -> success, 71 * SG_LIB_CAT_INVALID_OP -> invalid opcode, SG_LIB_CAT_ILLEGAL_REQ -> 72 * bad field in cdb, * SG_LIB_CAT_NOT_READY -> device not ready, 73 * SG_LIB_CAT_UNIT_ATTENTION, SG_LIB_CAT_ABORTED_COMMAND, 74 * -1 -> other failure */ 75 int sg_ll_mode_sense6(int sg_fd, int dbd, int pc, int pg_code, 76 int sub_pg_code, void * resp, int mx_resp_len, 77 int noisy, int verbose); 78 79 /* Invokes a SCSI MODE SENSE (10) command. Return of 0 -> success, 80 * SG_LIB_CAT_INVALID_OP -> invalid opcode, SG_LIB_CAT_ILLEGAL_REQ -> 81 * bad field in cdb, * SG_LIB_CAT_NOT_READY -> device not ready, 82 * SG_LIB_CAT_UNIT_ATTENTION, SG_LIB_CAT_ABORTED_COMMAND, 83 * -1 -> other failure */ 84 int sg_ll_mode_sense10(int sg_fd, int llbaa, int dbd, int pc, int pg_code, 85 int sub_pg_code, void * resp, int mx_resp_len, 86 int noisy, int verbose); 87 88 /* Invokes a SCSI PREVENT ALLOW MEDIUM REMOVAL command (SPC-3) 89 * prevent==0 allows removal, prevent==1 prevents removal ... 90 * Return of 0 -> success, 91 * SG_LIB_CAT_INVALID_OP -> command not supported 92 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, SG_LIB_CAT_UNIT_ATTENTION, 93 * SG_LIB_CAT_NOT_READY -> device not ready, SG_LIB_CAT_ABORTED_COMMAND, 94 * -1 -> other failure */ 95 int sg_ll_prevent_allow(int sg_fd, int prevent, int noisy, int verbose); 96 97 /* Invokes a SCSI READ CAPACITY (10) command. Return of 0 -> success, 98 * SG_LIB_CAT_INVALID_OP -> invalid opcode, SG_LIB_CAT_UNIT_ATTENTION 99 * -> perhaps media changed, SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, 100 * SG_LIB_CAT_NOT_READY -> device not ready, SG_LIB_CAT_ABORTED_COMMAND, 101 * -1 -> other failure */ 102 int sg_ll_readcap_10(int sg_fd, int pmi, unsigned int lba, void * resp, 103 int mx_resp_len, int noisy, int verbose); 104 105 /* Invokes a SCSI READ CAPACITY (16) command. Returns 0 -> success, 106 * SG_LIB_CAT_UNIT_ATTENTION -> media changed??, SG_LIB_CAT_INVALID_OP 107 * -> cdb not supported, SG_LIB_CAT_IlLEGAL_REQ -> bad field in cdb 108 * SG_LIB_CAT_NOT_READY -> device not ready, SG_LIB_CAT_ABORTED_COMMAND, 109 * -1 -> other failure */ 110 int sg_ll_readcap_16(int sg_fd, int pmi, uint64_t llba, void * resp, 111 int mx_resp_len, int noisy, int verbose); 112 113 /* Invokes a SCSI REPORT LUNS command. Return of 0 -> success, 114 * SG_LIB_CAT_INVALID_OP -> Report Luns not supported, 115 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, SG_LIB_CAT_ABORTED_COMMAND, 116 * SG_LIB_NOT_READY (shouldn't happen), -1 -> other failure */ 117 int sg_ll_report_luns(int sg_fd, int select_report, void * resp, 118 int mx_resp_len, int noisy, int verbose); 119 120 /* Invokes a SCSI REQUEST SENSE command. Return of 0 -> success, 121 * SG_LIB_CAT_INVALID_OP -> Request Sense not supported??, 122 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, SG_LIB_CAT_ABORTED_COMMAND, 123 * -1 -> other failure */ 124 int sg_ll_request_sense(int sg_fd, int desc, void * resp, int mx_resp_len, 125 int noisy, int verbose); 126 127 /* Invokes a SCSI START STOP UNIT command (SBC + MMC). 128 * Return of 0 -> success, 129 * SG_LIB_CAT_INVALID_OP -> Start stop unit not supported, 130 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, SG_LIB_CAT_UNIT_ATTENTION, 131 * SG_LIB_CAT_NOT_READY -> device not ready, SG_LIB_CAT_ABORTED_COMMAND, 132 * -1 -> other failure 133 * SBC-3 and MMC partially overlap on the power_condition_modifier(sbc) and 134 * format_layer_number(mmc) fields. They also overlap on the noflush(sbc) 135 * and fl(mmc) one bit field. This is the cause of the awkardly named 136 * pc_mod__fl_num and noflush__fl arguments to this function. */ 137 int sg_ll_start_stop_unit(int sg_fd, int immed, int pc_mod__fl_num, 138 int power_cond, int noflush__fl, int loej, 139 int start, int noisy, int verbose); 140 141 /* Invokes a SCSI SYNCHRONIZE CACHE (10) command. Return of 0 -> success, 142 * SG_LIB_CAT_UNIT_ATTENTION, SG_LIB_CAT_ABORTED_COMMAND, 143 * SG_LIB_CAT_INVALID_OP -> cdb not supported, 144 * SG_LIB_CAT_IlLEGAL_REQ -> bad field in cdb 145 * SG_LIB_CAT_NOT_READY -> device not ready, -1 -> other failure */ 146 int sg_ll_sync_cache_10(int sg_fd, int sync_nv, int immed, int group, 147 unsigned int lba, unsigned int count, int noisy, 148 int verbose); 149 150 /* Invokes a SCSI TEST UNIT READY command. 151 * 'pack_id' is just for diagnostics, safe to set to 0. 152 * Return of 0 -> success, SG_LIB_CAT_UNIT_ATTENTION, 153 * SG_LIB_CAT_NOT_READY -> device not ready, 154 * SG_LIB_CAT_ABORTED_COMMAND, -1 -> other failure */ 155 int sg_ll_test_unit_ready(int sg_fd, int pack_id, int noisy, int verbose); 156 157 /* Invokes a SCSI TEST UNIT READY command. 158 * 'pack_id' is just for diagnostics, safe to set to 0. 159 * Looks for progress indicator if 'progress' non-NULL; 160 * if found writes value [0..65535] else write -1. 161 * Return of 0 -> success, SG_LIB_CAT_UNIT_ATTENTION, 162 * SG_LIB_CAT_ABORTED_COMMAND, SG_LIB_CAT_NOT_READY -> 163 * device not ready, -1 -> other failure */ 164 int sg_ll_test_unit_ready_progress(int sg_fd, int pack_id, int * progress, 165 int noisy, int verbose); 166 167 168 struct sg_simple_inquiry_resp { 169 unsigned char peripheral_qualifier; 170 unsigned char peripheral_type; 171 unsigned char byte_1; /* was 'rmb' prior to version 1.39 */ 172 /* now rmb == !!(0x80 & byte_1) */ 173 unsigned char version; /* as per recent drafts: whole of byte 2 */ 174 unsigned char byte_3; 175 unsigned char byte_5; 176 unsigned char byte_6; 177 unsigned char byte_7; 178 char vendor[9]; /* T10 field is 8 bytes, NUL char appended */ 179 char product[17]; 180 char revision[5]; 181 }; 182 183 /* Yields most of first 36 bytes of a standard INQUIRY (evpd==0) response. 184 * Returns 0 when successful, SG_LIB_CAT_INVALID_OP -> not supported, 185 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, SG_LIB_CAT_ABORTED_COMMAND, 186 * -1 -> other errors */ 187 int sg_simple_inquiry(int sg_fd, struct sg_simple_inquiry_resp * inq_data, 188 int noisy, int verbose); 189 190 /* MODE SENSE commands yield a response that has block descriptors followed 191 * by mode pages. In most cases users are interested in the first mode page. 192 * This function returns the (byte) offset of the start of the first mode 193 * page. Set mode_sense_6 to 1 for MODE SENSE (6) and 0 for MODE SENSE (10). 194 * Returns >= 0 is successful or -1 if failure. If there is a failure 195 * a message is written to err_buff. */ 196 int sg_mode_page_offset(const unsigned char * resp, int resp_len, 197 int mode_sense_6, char * err_buff, int err_buff_len); 198 199 /* Fetches current, changeable, default and/or saveable modes pages as 200 * indicated by pcontrol_arr for given pg_code and sub_pg_code. If 201 * mode6==0 then use MODE SENSE (10) else use MODE SENSE (6). If 202 * flexible set and mode data length seems wrong then try and 203 * fix (compensating hack for bad device or driver). pcontrol_arr 204 * should have 4 elements for output of current, changeable, default 205 * and saved values respectively. Each element should be NULL or 206 * at least mx_mpage_len bytes long. 207 * Return of 0 -> overall success, SG_LIB_CAT_INVALID_OP -> invalid opcode, 208 * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb, SG_LIB_CAT_UNIT_ATTENTION, 209 * SG_LIB_CAT_NOT_READY -> device not ready, 210 * SG_LIB_CAT_MALFORMED -> bad response, -1 -> other failure. 211 * If success_mask pointer is not NULL then first zeros it. Then set bits 212 * 0, 1, 2 and/or 3 if the current, changeable, default and saved values 213 * respectively have been fetched. If error on current page 214 * then stops and returns that error; otherwise continues if an error is 215 * detected but returns the first error encountered. */ 216 int sg_get_mode_page_controls(int sg_fd, int mode6, int pg_code, 217 int sub_pg_code, int dbd, int flexible, 218 int mx_mpage_len, int * success_mask, 219 void * pcontrol_arr[], int * reported_len, 220 int verbose); 221 222 /* Returns file descriptor >= 0 if successful. If error in Unix returns 223 negated errno. Implementation calls scsi_pt_open_device(). */ 224 int sg_cmds_open_device(const char * device_name, int read_only, int verbose); 225 226 /* Returns file descriptor >= 0 if successful. If error in Unix returns 227 negated errno. Implementation calls scsi_pt_open_flags(). */ 228 int sg_cmds_open_flags(const char * device_name, int flags, int verbose); 229 230 /* Returns 0 if successful. If error in Unix returns negated errno. 231 Implementation calls scsi_pt_close_device(). */ 232 int sg_cmds_close_device(int device_fd); 233 234 const char * sg_cmds_version(); 235 236 237 struct sg_pt_base; 238 239 /* This is a helper function used by sg_cmds_* implementations after the 240 * call to the pass-through. pt_res is returned from do_scsi_pt(). If valid 241 * sense data is found it is decoded and output to sg_warnings_strm (def: 242 * stderr); depending on the 'noisy' and 'verbose' settings. Returns -2 for 243 * sense data (may not be fatal), -1 for failed, 0, or a positive number. If 244 * 'mx_di_len > 0' then asks pass-through for resid and returns 245 * (mx_di_len - resid); otherwise returns 0. So for data-in it should return 246 * the actual number of bytes received. For data-out (to device) or no data 247 * call with 'mx_di_len' set to 0 or less. If -2 returned then sense category 248 * output via 'o_sense_cat' pointer (if not NULL). Note that several sense 249 * categories also have data in bytes received; -2 is still returned. */ 250 int sg_cmds_process_resp(struct sg_pt_base * ptvp, const char * leadin, 251 int pt_res, int mx_di_len, 252 const unsigned char * sense_b, int noisy, 253 int verbose, int * o_sense_cat); 254 255 #ifdef __cplusplus 256 } 257 #endif 258 259 #endif 260