]> git.proxmox.com Git - proxmox-backup.git/blob - src/tools/sgutils2.rs
sgutils2: support RequestSense Descriptor format
[proxmox-backup.git] / src / tools / sgutils2.rs
1 //! Bindings for libsgutils2
2 //!
3 //! Incomplete, but we currently do not need more.
4 //!
5 //! See: `/usr/include/scsi/sg_pt.h`
6 //!
7 //! The SCSI Commands Reference Manual also contains some usefull information.
8
9 use std::os::unix::io::AsRawFd;
10 use std::ptr::NonNull;
11
12 use anyhow::{bail, format_err, Error};
13 use endian_trait::Endian;
14 use serde::{Deserialize, Serialize};
15 use libc::{c_char, c_int};
16
17 use proxmox::tools::io::ReadExt;
18
19 #[derive(Debug)]
20 pub struct SenseInfo {
21 pub sense_key: u8,
22 pub asc: u8,
23 pub ascq: u8,
24 }
25
26 impl ToString for SenseInfo {
27
28 fn to_string(&self) -> String {
29
30 // Added codes from IBM TS4300 Tape Library SCSI reference manual
31 // Added codes from Quantum Intelligent Libraries SCSI Reference Guide
32 match (self.sense_key, self.asc, self.ascq) {
33 (0x00, asc, ascq) => format!("no sense, ASC = 0x{:02x}, ASCQ = 0x{:02x}", asc, ascq),
34 (0x01, asc, ascq) => format!("recevered error, ASC = 0x{:02x}, ASCQ = 0x{:02x}", asc, ascq),
35 // Not Ready
36 (0x02, 0x04, 0x00) => String::from("Not ready, cause not reportable"),
37 (0x02, 0x04, 0x01) => String::from("Not ready, operation in progress"),
38 (0x02, 0x04, 0x03) => String::from("Not ready, manual intervention required"),
39 (0x02, 0x04, 0x12) => String::from("Not ready, offline"),
40 (0x02, 0x04, 0x83) => String::from("The library is not ready due to aisle power being disabled"),
41 (0x02, 0x04, 0x8D) => String::from(" The library is not ready because it is offline"),
42 (0x02, 0x3B, 0x12) => String::from("Not ready, magazine removed"),
43 (0x02, asc, ascq) => format!("not ready, ASC = 0x{:02x}, ASCQ = 0x{:02x}", asc, ascq),
44 // Media Error
45 (0x03, 0x30, 0x00) => String::from("Media error"),
46 (0x03, 0x30, 0x07) => String::from("Cleaning failure"),
47 (0x03, asc, ascq) => format!("media error, ASC = 0x{:02x}, ASCQ = 0x{:02x}", asc, ascq),
48 // Hardware Error
49 (0x04, 0x15, 0x01) => String::from("A mechanical positioning error occurred"),
50 (0x04, 0x3B, 0x0D) => String::from("Medium destination element full"),
51 (0x04, 0x3B, 0x0E) => String::from("Medium source element empty"),
52 (0x04, 0x3F, 0x0F) => String::from("Echo buffer overwritten"),
53 (0x04, 0x40, 0x80) => String::from("Component failure"),
54 (0x04, 0x44, 0x00) => String::from("Firmware detected an internal logic failure"),
55 (0x04, 0x53, 0x00) => String::from("A drive did not load or unload a tape"),
56 (0x04, 0x53, 0x01) => String::from("A drive did not unload a cartridge"),
57 (0x04, 0x53, 0x82) => String::from("Cannot lock the I/E station"),
58 (0x04, 0x53, 0x83) => String::from("Cannot unlock the I/E station"),
59 (0x04, 0x80, 0xD7) => String::from("Internal software error"),
60 (0x04, 0x80, 0xD8) => String::from("Database access error"),
61 (0x04, 0x81, 0xB0) => String::from("Internal system communication failed"),
62 (0x04, 0x81, 0xB2) => String::from("Robotic controller communication failed"),
63 (0x04, 0x81, 0xB3) => String::from("Mechanical positioning error"),
64 (0x04, 0x81, 0xB4) => String::from("Cartridge did not transport completely."),
65 (0x04, 0x82, 0xFC) => String::from("Drive configuration failed"),
66 (0x04, 0x83, 0x00) => String::from("Label too short or too long"),
67 (0x04, asc, ascq) => format!("hardware error, ASC = 0x{:02x}, ASCQ = 0x{:02x}", asc, ascq),
68 // Illegal Request
69 (0x05, 0x04, 0x83) => String::from("Door open"),
70 (0x05, 0x1A, 0x00) => String::from("Parameter length error"),
71 (0x05, 0x20, 0x00) => String::from("Invalid command operation code"),
72 (0x05, 0x21, 0x01) => String::from("Invalid element address"),
73 (0x05, 0x24, 0x00) => String::from("Invalid field CDB"),
74 (0x05, 0x25, 0x00) => String::from("Invalid LUN"),
75 (0x05, 0x26, 0x00) => String::from("Invalid field in parameter list"),
76 (0x05, 0x26, 0x01) => String::from("Parameter list error: parameter not supported"),
77 (0x05, 0x26, 0x02) => String::from("Parameter value invalid"),
78 (0x05, 0x26, 0x04) => String::from("Invalid release of persistent reservation"),
79 (0x05, 0x2C, 0x00) => String::from("Saving parameters is not supported"),
80 (0x05, 0x30, 0x00) => String::from("Incompatible medium installed"),
81 (0x05, 0x30, 0x12) => String::from("Incompatible Media loaded to drive"),
82 (0x05, 0x39, 0x00) => String::from("Saving parameters is not supported"),
83 (0x05, 0x3B, 0x0D) => String::from("Medium destination element full"),
84 (0x05, 0x3B, 0x0E) => String::from("Medium source element empty"),
85 (0x05, 0x3B, 0x11) => String::from("Magazine not accessible"),
86 (0x05, 0x3B, 0x12) => String::from("Magazine not installed"),
87 (0x05, 0x3B, 0x18) => String::from("Element disabled"),
88 (0x05, 0x3B, 0x1A) => String::from("Data transfer element removed"),
89 (0x05, 0x3B, 0xA0) => String::from("Media type does not match destination media type"),
90 (0x05, 0x3F, 0x01) => String::from("New firmware loaded"),
91 (0x05, 0x3F, 0x03) => String::from("Inquiry data changed"),
92 (0x05, 0x44, 0x81) => String::from("Source element not ready"),
93 (0x05, 0x44, 0x82) => String::from("Destination element not ready"),
94 (0x05, 0x53, 0x02) => String::from("Library media removal prevented state set."),
95 (0x05, 0x53, 0x03) => String::from("Drive media removal prevented state set"),
96 (0x05, 0x53, 0x81) => String::from("Insert/Eject station door is open"),
97 (0x05, 0x82, 0x93) => String::from("Failure session sequence error"),
98 (0x05, 0x82, 0x94) => String::from("Failover command sequence error"),
99 (0x05, 0x82, 0x95) => String::from("Duplicate failover session key"),
100 (0x05, 0x82, 0x96) => String::from("Invalid failover key"),
101 (0x05, 0x82, 0x97) => String::from("Failover session that is released"),
102 (0x05, 0x83, 0x02) => String::from("Barcode label questionable"),
103 (0x05, 0x83, 0x03) => String::from("Cell status and barcode label questionable"),
104 (0x05, 0x83, 0x04) => String::from("Data transfer element not installed"),
105 (0x05, 0x83, 0x05) => String::from("Data transfer element is varied off and not accessible for library operations"),
106 (0x05, 0x83, 0x06) => String::from("Element is contained within an offline tower or I/E station and is not accessible for library operations"),
107 (0x05, asc, ascq) => format!("illegal request, ASC = 0x{:02x}, ASCQ = 0x{:02x}", asc, ascq),
108 // Unit Attention
109 (0x06, 0x28, 0x00) => String::from("Not ready to change, medium changed"),
110 (0x06, 0x28, 0x01) => String::from("Import/export element that is accessed"),
111 (0x06, 0x29, 0x00) => String::from("Power-on or reset occurred"),
112 (0x06, 0x29, 0x01) => String::from("Power on occurred"),
113 (0x06, 0x29, 0x02) => String::from("SCSI Bus reset occurred"),
114 (0x06, 0x29, 0x03) => String::from("Device reset occurred"),
115 (0x06, 0x29, 0x04) => String::from("Internal reset occurred"),
116 (0x06, 0x2A, 0x01) => String::from("Mode parameters have been changed"),
117 (0x06, 0x2A, 0x03) => String::from("Reservations preempted"),
118 (0x06, 0x2A, 0x04) => String::from("Reservations released"),
119 (0x06, 0x2A, 0x05) => String::from("Registrations preempted"),
120 (0x06, asc, ascq) => format!("unit attention, ASC = 0x{:02x}, ASCQ = 0x{:02x}", asc, ascq),
121 // Aborted Command
122 (0x0B, 0x3F, 0x0F) => String::from("ECHO buffer overwritten"),
123 (0x0B, 0x44, 0x00) => String::from("Firmware detected an internal logic failure"),
124 (0x0B, 0x45, 0x00) => String::from("Select or reselect failure"),
125 (0x0B, 0x47, 0x00) => String::from("SCSI parity error"),
126 (0x0B, 0x48, 0x00) => String::from("Initiator detected error message received"),
127 (0x0B, 0x49, 0x00) => String::from("Invalid message error"),
128 (0x0B, 0x4A, 0x00) => String::from("Command phase error"),
129 (0x0B, 0x4B, 0x00) => String::from("Data phase error"),
130 (0x0B, 0x4E, 0x00) => String::from("Overlapped command attempt"),
131 (0x0B, asc, ascq) => format!("aborted command, ASC = 0x{:02x}, ASCQ = 0x{:02x}", asc, ascq),
132 // Else, simply report values
133 _ => format!("sense_key = 0x{:02x}, ASC = 0x{:02x}, ASCQ = 0x{:02x}", self.sense_key, self.asc, self.ascq),
134 }
135 }
136 }
137
138 #[derive(Debug)]
139 pub struct ScsiError {
140 pub error: Error,
141 pub sense: Option<SenseInfo>,
142 }
143
144 impl std::fmt::Display for ScsiError {
145 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146 write!(f, "{}", self.error)
147 }
148 }
149
150 impl std::error::Error for ScsiError {
151 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
152 self.error.source()
153 }
154 }
155
156 impl From<anyhow::Error> for ScsiError {
157 fn from(error: anyhow::Error) -> Self {
158 Self { error, sense: None }
159 }
160 }
161
162 impl From<std::io::Error> for ScsiError {
163 fn from(error: std::io::Error) -> Self {
164 Self { error: error.into(), sense: None }
165 }
166 }
167
168 // Opaque wrapper for sg_pt_base
169 #[repr(C)]
170 struct SgPtBase { _private: [u8; 0] }
171
172 #[repr(transparent)]
173 struct SgPt {
174 raw: NonNull<SgPtBase>,
175 }
176
177 impl Drop for SgPt {
178 fn drop(&mut self) {
179 unsafe { destruct_scsi_pt_obj(self.as_mut_ptr()) };
180 }
181 }
182
183 impl SgPt {
184 fn new() -> Result<Self, Error> {
185 Ok(Self {
186 raw: NonNull::new(unsafe { construct_scsi_pt_obj() })
187 .ok_or_else(|| format_err!("construct_scsi_pt_ob failed"))?,
188 })
189 }
190
191 fn as_ptr(&self) -> *const SgPtBase {
192 self.raw.as_ptr()
193 }
194
195 fn as_mut_ptr(&mut self) -> *mut SgPtBase {
196 self.raw.as_ptr()
197 }
198 }
199
200 /// Peripheral device type text (see `inquiry` command)
201 ///
202 /// see [https://en.wikipedia.org/wiki/SCSI_Peripheral_Device_Type]
203 pub const PERIPHERAL_DEVICE_TYPE_TEXT: [&'static str; 32] = [
204 "Disk Drive",
205 "Tape Drive",
206 "Printer",
207 "Processor",
208 "Write-once",
209 "CD-ROM", // 05h
210 "Scanner",
211 "Optical",
212 "Medium Changer", // 08h
213 "Communications",
214 "ASC IT8",
215 "ASC IT8",
216 "RAID Array",
217 "Enclosure Services",
218 "Simplified direct-access",
219 "Optical card reader/writer",
220 "Bridging Expander",
221 "Object-based Storage",
222 "Automation/Drive Interface",
223 "Security manager",
224 "Reserved",
225 "Reserved",
226 "Reserved",
227 "Reserved",
228 "Reserved",
229 "Reserved",
230 "Reserved",
231 "Reserved",
232 "Reserved",
233 "Reserved",
234 "Reserved",
235 "Unknown",
236 ];
237
238 // SENSE KEYS
239 pub const SENSE_KEY_NO_SENSE: u8 = 0x00;
240 pub const SENSE_KEY_RECOVERED_ERROR: u8 = 0x01;
241 pub const SENSE_KEY_NOT_READY: u8 = 0x02;
242 pub const SENSE_KEY_MEDIUM_ERROR: u8 = 0x03;
243 pub const SENSE_KEY_HARDWARE_ERROR: u8 = 0x04;
244 pub const SENSE_KEY_ILLEGAL_REQUEST: u8 = 0x05;
245 pub const SENSE_KEY_UNIT_ATTENTION: u8 = 0x06;
246 pub const SENSE_KEY_DATA_PROTECT: u8 = 0x07;
247 pub const SENSE_KEY_BLANK_CHECK: u8 = 0x08;
248 pub const SENSE_KEY_COPY_ABORTED: u8 = 0x0a;
249 pub const SENSE_KEY_ABORTED_COMMAND: u8 = 0x0b;
250 pub const SENSE_KEY_VOLUME_OVERFLOW: u8 = 0x0d;
251 pub const SENSE_KEY_MISCOMPARE: u8 = 0x0e;
252
253
254 #[repr(C, packed)]
255 #[derive(Endian)]
256 struct InquiryPage {
257 peripheral_type: u8,
258 rmb: u8,
259 version: u8,
260 flags3: u8,
261 additional_length: u8,
262 flags5: u8,
263 flags6: u8,
264 flags7: u8,
265 vendor: [u8; 8],
266 product: [u8; 16],
267 revision: [u8; 4],
268 // additional data follows, but we do not need that
269 }
270
271 #[repr(C, packed)]
272 #[derive(Endian, Debug)]
273 struct RequestSenseFixed {
274 response_code: u8,
275 obsolete: u8,
276 flags2: u8,
277 information: [u8;4],
278 additional_sense_len: u8,
279 command_specific_information: [u8;4],
280 additional_sense_code: u8,
281 additional_sense_code_qualifier: u8,
282 field_replacable_unit_code: u8,
283 sense_key_specific: [u8; 3],
284 }
285
286 #[repr(C, packed)]
287 #[derive(Endian, Debug)]
288 struct RequestSenseDescriptor{
289 response_code: u8,
290 sense_key: u8,
291 additional_sense_code: u8,
292 additional_sense_code_qualifier: u8,
293 reserved: [u8;4],
294 additional_sense_len: u8,
295 }
296
297 /// Inquiry result
298 #[derive(Serialize, Deserialize, Debug)]
299 pub struct InquiryInfo {
300 /// Peripheral device type (0-31)
301 pub peripheral_type: u8,
302 /// Peripheral device type as string
303 pub peripheral_type_text: String,
304 /// Vendor
305 pub vendor: String,
306 /// Product
307 pub product: String,
308 /// Revision
309 pub revision: String,
310 }
311
312 pub const SCSI_PT_DO_START_OK:c_int = 0;
313 pub const SCSI_PT_DO_BAD_PARAMS:c_int = 1;
314 pub const SCSI_PT_DO_TIMEOUT:c_int = 2;
315
316 pub const SCSI_PT_RESULT_GOOD:c_int = 0;
317 pub const SCSI_PT_RESULT_STATUS:c_int = 1;
318 pub const SCSI_PT_RESULT_SENSE:c_int = 2;
319 pub const SCSI_PT_RESULT_TRANSPORT_ERR:c_int = 3;
320 pub const SCSI_PT_RESULT_OS_ERR:c_int = 4;
321
322 #[link(name = "sgutils2")]
323 extern "C" {
324
325 #[allow(dead_code)]
326 fn scsi_pt_open_device(
327 device_name: * const c_char,
328 read_only: bool,
329 verbose: c_int,
330 ) -> c_int;
331
332 fn sg_is_scsi_cdb(
333 cdbp: *const u8,
334 clen: c_int,
335 ) -> bool;
336
337 fn construct_scsi_pt_obj() -> *mut SgPtBase;
338 fn destruct_scsi_pt_obj(objp: *mut SgPtBase);
339
340 fn set_scsi_pt_data_in(
341 objp: *mut SgPtBase,
342 dxferp: *mut u8,
343 dxfer_ilen: c_int,
344 );
345
346 fn set_scsi_pt_data_out(
347 objp: *mut SgPtBase,
348 dxferp: *const u8,
349 dxfer_olen: c_int,
350 );
351
352 fn set_scsi_pt_cdb(
353 objp: *mut SgPtBase,
354 cdb: *const u8,
355 cdb_len: c_int,
356 );
357
358 fn set_scsi_pt_sense(
359 objp: *mut SgPtBase,
360 sense: *mut u8,
361 max_sense_len: c_int,
362 );
363
364 fn do_scsi_pt(
365 objp: *mut SgPtBase,
366 fd: c_int,
367 timeout_secs: c_int,
368 verbose: c_int,
369 ) -> c_int;
370
371 fn get_scsi_pt_resid(objp: *const SgPtBase) -> c_int;
372
373 fn get_scsi_pt_sense_len(objp: *const SgPtBase) -> c_int;
374
375 fn get_scsi_pt_status_response(objp: *const SgPtBase) -> c_int;
376
377 fn get_scsi_pt_result_category(objp: *const SgPtBase) -> c_int;
378
379 fn get_scsi_pt_os_err(objp: *const SgPtBase) -> c_int;
380 }
381
382 /// Safe interface to run RAW SCSI commands
383 pub struct SgRaw<'a, F> {
384 file: &'a mut F,
385 buffer: Box<[u8]>,
386 sense_buffer: [u8; 32],
387 timeout: i32,
388 }
389
390 /// Allocate a page aligned buffer
391 ///
392 /// SG RAWIO commands needs page aligned transfer buffers.
393 pub fn alloc_page_aligned_buffer(buffer_size: usize) -> Result<Box<[u8]> , Error> {
394 let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
395 let layout = std::alloc::Layout::from_size_align(buffer_size, page_size)?;
396 let dinp = unsafe { std::alloc::alloc_zeroed(layout) };
397 if dinp.is_null() {
398 bail!("alloc SCSI output buffer failed");
399 }
400
401 let buffer_slice = unsafe { std::slice::from_raw_parts_mut(dinp, buffer_size)};
402 Ok(unsafe { Box::from_raw(buffer_slice) })
403 }
404
405 impl <'a, F: AsRawFd> SgRaw<'a, F> {
406
407 /// Create a new instance to run commands
408 ///
409 /// The file must be a handle to a SCSI device.
410 pub fn new(file: &'a mut F, buffer_size: usize) -> Result<Self, Error> {
411
412 let buffer;
413
414 if buffer_size > 0 {
415 buffer = alloc_page_aligned_buffer(buffer_size)?;
416 } else {
417 buffer = Box::new([]);
418 }
419
420 let sense_buffer = [0u8; 32];
421
422 Ok(Self { file, buffer, sense_buffer, timeout: 0 })
423 }
424
425 /// Set the command timeout in seconds (0 means default (60 seconds))
426 pub fn set_timeout(&mut self, seconds: usize) {
427 if seconds > (i32::MAX as usize) {
428 self.timeout = i32::MAX; // don't care about larger values
429 } else {
430 self.timeout = seconds as i32;
431 }
432 }
433
434 // create new object with initialized data_in and sense buffer
435 fn create_scsi_pt_obj(&mut self) -> Result<SgPt, Error> {
436
437 let mut ptvp = SgPt::new()?;
438
439 if self.buffer.len() > 0 {
440 unsafe {
441 set_scsi_pt_data_in(
442 ptvp.as_mut_ptr(),
443 self.buffer.as_mut_ptr(),
444 self.buffer.len() as c_int,
445 )
446 };
447 }
448
449 unsafe {
450 set_scsi_pt_sense(
451 ptvp.as_mut_ptr(),
452 self.sense_buffer.as_mut_ptr(),
453 self.sense_buffer.len() as c_int,
454 )
455 };
456
457 Ok(ptvp)
458 }
459
460 fn do_scsi_pt_checked(&mut self, ptvp: &mut SgPt) -> Result<(), ScsiError> {
461
462 let res = unsafe { do_scsi_pt(ptvp.as_mut_ptr(), self.file.as_raw_fd(), self.timeout, 0) };
463 match res {
464 SCSI_PT_DO_START_OK => { /* Ok */ },
465 SCSI_PT_DO_BAD_PARAMS => return Err(format_err!("do_scsi_pt failed - bad pass through setup").into()),
466 SCSI_PT_DO_TIMEOUT => return Err(format_err!("do_scsi_pt failed - timeout").into()),
467 code if code < 0 => {
468 let errno = unsafe { get_scsi_pt_os_err(ptvp.as_ptr()) };
469 let err = nix::Error::from_errno(nix::errno::Errno::from_i32(errno));
470 return Err(format_err!("do_scsi_pt failed with err {}", err).into());
471 }
472 unknown => return Err(format_err!("do_scsi_pt failed: unknown error {}", unknown).into()),
473 }
474
475 if res < 0 {
476 let err = nix::Error::last();
477 return Err(format_err!("do_scsi_pt failed - {}", err).into());
478 }
479 if res != 0 {
480 return Err(format_err!("do_scsi_pt failed {}", res).into());
481 }
482
483 let sense_len = unsafe { get_scsi_pt_sense_len(ptvp.as_ptr()) };
484
485 let res_cat = unsafe { get_scsi_pt_result_category(ptvp.as_ptr()) };
486 match res_cat {
487 SCSI_PT_RESULT_GOOD => { /* Ok */ }
488 SCSI_PT_RESULT_STATUS => { /* test below */ }
489 SCSI_PT_RESULT_SENSE => {
490 if sense_len == 0 {
491 return Err(format_err!("scsi command failed: no Sense").into());
492 }
493
494 let code = self.sense_buffer[0] & 0x7f;
495
496 let mut reader = &self.sense_buffer[..(sense_len as usize)];
497
498 let sense = match code {
499 0x70 => {
500 let sense: RequestSenseFixed = unsafe { reader.read_be_value()? };
501 SenseInfo {
502 sense_key: sense.flags2 & 0xf,
503 asc: sense.additional_sense_code,
504 ascq: sense.additional_sense_code_qualifier,
505 }
506 }
507 0x72 => {
508 let sense: RequestSenseDescriptor = unsafe { reader.read_be_value()? };
509 SenseInfo {
510 sense_key: sense.sense_key & 0xf,
511 asc: sense.additional_sense_code,
512 ascq: sense.additional_sense_code_qualifier,
513 }
514 }
515 0x71 | 0x73 => {
516 return Err(format_err!("scsi command failed: received deferred Sense").into());
517 }
518 unknown => {
519 return Err(format_err!("scsi command failed: invalid Sense response code {:x}", unknown).into());
520 }
521 };
522
523 return Err(ScsiError {
524 error: format_err!("scsi command failed: {}", sense.to_string()),
525 sense: Some(sense),
526 });
527 }
528 SCSI_PT_RESULT_TRANSPORT_ERR => return Err(format_err!("scsi command failed: transport error").into()),
529 SCSI_PT_RESULT_OS_ERR => {
530 let errno = unsafe { get_scsi_pt_os_err(ptvp.as_ptr()) };
531 let err = nix::Error::from_errno(nix::errno::Errno::from_i32(errno));
532 return Err(format_err!("scsi command failed with err {}", err).into());
533 }
534 unknown => return Err(format_err!("scsi command failed: unknown result category {}", unknown).into()),
535 }
536
537 let status = unsafe { get_scsi_pt_status_response(ptvp.as_ptr()) };
538 if status != 0 {
539 return Err(format_err!("unknown scsi error - status response {}", status).into());
540 }
541
542 Ok(())
543 }
544
545 /// Run the specified RAW SCSI command
546 pub fn do_command(&mut self, cmd: &[u8]) -> Result<&[u8], ScsiError> {
547
548 if !unsafe { sg_is_scsi_cdb(cmd.as_ptr(), cmd.len() as c_int) } {
549 return Err(format_err!("no valid SCSI command").into());
550 }
551
552 if self.buffer.len() < 16 {
553 return Err(format_err!("output buffer too small").into());
554 }
555
556 let mut ptvp = self.create_scsi_pt_obj()?;
557
558 unsafe {
559 set_scsi_pt_cdb(
560 ptvp.as_mut_ptr(),
561 cmd.as_ptr(),
562 cmd.len() as c_int,
563 )
564 };
565
566 self.do_scsi_pt_checked(&mut ptvp)?;
567
568 let resid = unsafe { get_scsi_pt_resid(ptvp.as_ptr()) } as usize;
569 if resid > self.buffer.len() {
570 return Err(format_err!("do_scsi_pt failed - got strange resid (value too big)").into());
571 }
572 let data_len = self.buffer.len() - resid;
573
574 Ok(&self.buffer[..data_len])
575 }
576
577 /// Run dataout command
578 ///
579 /// Note: use alloc_page_aligned_buffer to alloc data transfer buffer
580 pub fn do_out_command(&mut self, cmd: &[u8], data: &[u8]) -> Result<(), Error> {
581
582 if !unsafe { sg_is_scsi_cdb(cmd.as_ptr(), cmd.len() as c_int) } {
583 bail!("no valid SCSI command");
584 }
585
586 let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) } as usize;
587 if ((data.as_ptr() as usize) & (page_size -1)) != 0 {
588 bail!("wrong transfer buffer alignment");
589 }
590
591 let mut ptvp = self.create_scsi_pt_obj()?;
592
593 unsafe {
594 set_scsi_pt_data_out(
595 ptvp.as_mut_ptr(),
596 data.as_ptr(),
597 data.len() as c_int,
598 );
599
600 set_scsi_pt_cdb(
601 ptvp.as_mut_ptr(),
602 cmd.as_ptr(),
603 cmd.len() as c_int,
604 );
605 };
606
607 self.do_scsi_pt_checked(&mut ptvp)?;
608
609 Ok(())
610 }
611 }
612
613 // Useful helpers
614
615 /// Converts SCSI ASCII text into String, trim zero and spaces
616 pub fn scsi_ascii_to_string(data: &[u8]) -> String {
617 String::from_utf8_lossy(data)
618 .trim_matches(char::from(0))
619 .trim()
620 .to_string()
621 }
622
623 /// Read SCSI Inquiry page
624 ///
625 /// Returns Product/Vendor/Revision and device type.
626 pub fn scsi_inquiry<F: AsRawFd>(
627 file: &mut F,
628 ) -> Result<InquiryInfo, Error> {
629
630 let allocation_len: u8 = u8::MAX;
631 let mut sg_raw = SgRaw::new(file, allocation_len as usize)?;
632 sg_raw.set_timeout(30); // use short timeout
633
634 let mut cmd = Vec::new();
635 cmd.extend(&[0x12, 0, 0, 0, allocation_len, 0]); // INQUIRY
636
637 let data = sg_raw.do_command(&cmd)
638 .map_err(|err| format_err!("SCSI inquiry failed - {}", err))?;
639
640 proxmox::try_block!({
641 let mut reader = &data[..];
642
643 let page: InquiryPage = unsafe { reader.read_be_value()? };
644
645 let peripheral_type = page.peripheral_type & 31;
646
647 let info = InquiryInfo {
648 peripheral_type,
649 peripheral_type_text: PERIPHERAL_DEVICE_TYPE_TEXT[peripheral_type as usize].to_string(),
650 vendor: scsi_ascii_to_string(&page.vendor),
651 product: scsi_ascii_to_string(&page.product),
652 revision: scsi_ascii_to_string(&page.revision),
653 };
654
655 Ok(info)
656 }).map_err(|err: Error| format_err!("decode inquiry page failed - {}", err))
657 }