From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 65F7571151 for ; Wed, 7 Apr 2021 12:24:13 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 63E5CF3BC for ; Wed, 7 Apr 2021 12:24:13 +0200 (CEST) Received: from elsa.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP id 42C63F34D for ; Wed, 7 Apr 2021 12:24:07 +0200 (CEST) Received: by elsa.proxmox.com (Postfix, from userid 0) id 18F10AEAF27; Wed, 7 Apr 2021 12:24:07 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 7 Apr 2021 12:23:05 +0200 Message-Id: <20210407102308.9750-9-dietmar@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210407102308.9750-1-dietmar@proxmox.com> References: <20210407102308.9750-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 1 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RDNS_NONE 1.274 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [sgutils2.rs] Subject: [pbs-devel] [PATCH 08/11] sgutils2: add scsi_mode_sense helper X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Apr 2021 10:24:13 -0000 --- src/tools/sgutils2.rs | 99 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/src/tools/sgutils2.rs b/src/tools/sgutils2.rs index 4edfd9d9..af6ab6de 100644 --- a/src/tools/sgutils2.rs +++ b/src/tools/sgutils2.rs @@ -242,6 +242,46 @@ pub struct InquiryInfo { pub revision: String, } +#[repr(C, packed)] +#[derive(Endian, Debug, Copy, Clone)] +pub struct ModeParameterHeader { + pub mode_data_len: u16, + pub medium_type: u8, + pub flags3: u8, + reserved4: [u8;2], + pub block_descriptior_len: u16, +} + +#[repr(C, packed)] +#[derive(Endian, Debug, Copy, Clone)] +/// SCSI ModeBlockDescriptor for Tape devices +pub struct ModeBlockDescriptor { + pub density_code: u8, + pub number_of_blocks: [u8;3], + reserverd: u8, + pub block_length: [u8; 3], +} + +impl ModeBlockDescriptor { + + pub fn block_length(&self) -> u32 { + ((self.block_length[0] as u32) << 16) + + ((self.block_length[1] as u32) << 8) + + (self.block_length[2] as u32) + + } + + pub fn set_block_length(&mut self, length: u32) -> Result<(), Error> { + if length > 0x80_00_00 { + bail!("block length '{}' is too large", length); + } + self.block_length[0] = ((length & 0x00ff0000) >> 16) as u8; + self.block_length[1] = ((length & 0x0000ff00) >> 8) as u8; + self.block_length[2] = (length & 0x000000ff) as u8; + Ok(()) + } +} + pub const SCSI_PT_DO_START_OK:c_int = 0; pub const SCSI_PT_DO_BAD_PARAMS:c_int = 1; pub const SCSI_PT_DO_TIMEOUT:c_int = 2; @@ -654,3 +694,62 @@ pub fn scsi_inquiry( Ok(info) }).map_err(|err: Error| format_err!("decode inquiry page failed - {}", err)) } + +/// Run SCSI Mode Sense +/// +/// Warning: P needs to be repr(C, packed)] +pub fn scsi_mode_sense( + file: &mut F, + disable_block_descriptor: bool, + page_code: u8, + sub_page_code: u8, +) -> Result<(ModeParameterHeader, Option, P), Error> { + + let allocation_len: u16 = 4096; + let mut sg_raw = SgRaw::new(file, allocation_len as usize)?; + + let mut cmd = Vec::new(); + cmd.push(0x5A); // MODE SENSE(10) + if disable_block_descriptor { + cmd.push(8); // DBD=1 (Disable Block Descriptors) + } else { + cmd.push(0); // DBD=0 (Include Block Descriptors) + } + cmd.push(page_code & 63); // report current values for page_code + cmd.push(sub_page_code); + + cmd.extend(&[0, 0, 0]); // reserved + cmd.extend(&allocation_len.to_be_bytes()); // allocation len + cmd.push(0); //control + + let data = sg_raw.do_command(&cmd) + .map_err(|err| format_err!("mode sense failed - {}", err))?; + + proxmox::try_block!({ + let mut reader = &data[..]; + + let head: ModeParameterHeader = unsafe { reader.read_be_value()? }; + + if (head.mode_data_len as usize + 2) != data.len() { + bail!("wrong mode_data_len"); + } + + if disable_block_descriptor && head.block_descriptior_len != 0 { + bail!("wrong block_descriptior_len"); + } + + let mut block_descriptor: Option = None; + + if !disable_block_descriptor { + if head.block_descriptior_len != 8 { + bail!("wrong block_descriptior_len"); + } + + block_descriptor = Some(unsafe { reader.read_be_value()? }); + } + + let page: P = unsafe { reader.read_be_value()? }; + + Ok((head, block_descriptor, page)) + }).map_err(|err: Error| format_err!("decode mode sense failed - {}", err)) +} -- 2.20.1