From 5c1518cee6576e4ac5e627af94be1594f5b03765 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 9 Jan 2023 19:04:47 +0800 Subject: [PATCH] block reads work. --- services/usb-device-xous/src/block_device.rs | 2 +- services/usb-device-xous/src/hw.rs | 67 +++++++++---------- .../bulk_only_transport.rs | 11 +-- .../firmware/usbd_scsi/src/scsi/scsi.rs | 9 ++- 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/services/usb-device-xous/src/block_device.rs b/services/usb-device-xous/src/block_device.rs index 5e2fc5257..874fcfffb 100644 --- a/services/usb-device-xous/src/block_device.rs +++ b/services/usb-device-xous/src/block_device.rs @@ -37,6 +37,6 @@ impl usbd_scsi::BlockDevice for BlockDevice { } fn max_lba(&self) -> u32 { - 512 + 511 } } \ No newline at end of file diff --git a/services/usb-device-xous/src/hw.rs b/services/usb-device-xous/src/hw.rs index 9603e8222..bb32d8980 100644 --- a/services/usb-device-xous/src/hw.rs +++ b/services/usb-device-xous/src/hw.rs @@ -200,6 +200,8 @@ pub struct SpinalUsbDevice { address: AtomicUsize, // bit vector to track if a read is allowed. This prevents a race condition between polled reads and interrupted reads. read_allowed: AtomicU16, + // last descriptor in chain. Used to check if a transaction is in progress. + last_descriptor: Arc::; 16]>>, } impl SpinalUsbDevice { pub fn new(sid: xous::SID) -> SpinalUsbDevice { @@ -235,6 +237,7 @@ impl SpinalUsbDevice { tt: ticktimer_server::Ticktimer::new().unwrap(), address: AtomicUsize::new(0), read_allowed: AtomicU16::new(0), + last_descriptor: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])), }; xous::claim_interrupt( @@ -269,6 +272,7 @@ impl SpinalUsbDevice { tt: ticktimer_server::Ticktimer::new().unwrap(), address: AtomicUsize::new(0), read_allowed: AtomicU16::new(0), + last_descriptor: Arc::new(Mutex::new([None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,])), } } pub fn get_iface(&self) -> SpinalUsbMgmt { @@ -678,41 +682,30 @@ impl UsbBus for SpinalUsbDevice { let mut ep_status = self.status_read_volatile(ep_addr.index()); if ep_addr.index() != 0 { - let now = self.tt.elapsed_ms(); - let mut last_print = 10; - while (self.tt.elapsed_ms() - now < WRITE_TIMEOUT_MS) - && ep_status.head_offset() != 0 { - xous::yield_slice(); - let secs = (self.tt.elapsed_ms() - now) / 1_000; - if secs != last_print { - log::info!("write wait!"); - last_print = secs; + if let Some(final_descriptor) = &self.last_descriptor.lock().unwrap()[ep_addr.index()] { + let now = self.tt.elapsed_ms(); + log::info!("checking final descriptor {:x?}", final_descriptor); + let mut last_print = 10; + while (self.tt.elapsed_ms() - now < WRITE_TIMEOUT_MS) + && final_descriptor.in_progress() { + xous::yield_slice(); + let secs = (self.tt.elapsed_ms() - now) / 1_000; + if secs != last_print { + log::info!("write wait!"); + last_print = secs; + } } - ep_status = self.status_read_volatile(ep_addr.index()); - } - if ep_status.head_offset() != 0 { - log::warn!("head offset is not 0 even after waiting"); - return Err(UsbError::WouldBlock); + // TODO: report WouldBlock timeout } } // this is reset to 0 after every transaction by the hardware, so we must reset it ep_status.set_head_offset(head_offset as u32); - let descriptor = self.descriptor_from_status(&ep_status); /*if descriptor.in_progress() && ep_addr.index() != 0 { log::warn!("WouldBlock {:?}", ep_addr); return Err(UsbError::WouldBlock); }*/ - if descriptor.in_progress() && ep_addr.index() != 0 { - // refresh the ep_status after waiting - ep_status = self.status_read_volatile(ep_addr.index()); - ep_status.set_head_offset(head_offset as u32); - // note that the *descriptor* doesn't need to be updated because it refers directly to a memory location - if descriptor.in_progress() { - log::warn!("in progress despite waiting on write"); - } - } if ep_addr.index() != 0 { descriptor.set_desc_flags(UsbDirection::In, false, false, false); @@ -739,6 +732,7 @@ impl UsbBus for SpinalUsbDevice { false, true, false); } descriptor.set_offset(0); // reset the write pointer to 0, also sets in_progress + self.last_descriptor.lock().unwrap()[ep_addr.index()] = Some(descriptor); //if ep_addr.index() != 0 { // log::info!("WR PREdesc{}: {:?}", ep_addr.index(), descriptor); //} @@ -766,19 +760,21 @@ impl UsbBus for SpinalUsbDevice { //} Ok(buf.len()) } else { + if let Some(final_descriptor) = &self.last_descriptor.lock().unwrap()[ep_addr.index()] { + log::info!("ll checking final descriptor {:x?}", final_descriptor); + let now = self.tt.elapsed_ms(); + let mut last_secs = 5; + while final_descriptor.in_progress() && (self.tt.elapsed_ms() - now < WRITE_TIMEOUT_MS) { + xous::yield_slice(); + let secs = (self.tt.elapsed_ms() - now) / 1_0000; + if last_secs != secs { + log::info!("waiting for linked list to complete..."); + last_secs = secs; + } + } + } let mut ep_status = self.status_read_volatile(ep_addr.index()); - let now = self.tt.elapsed_ms(); - while (self.tt.elapsed_ms() - now < WRITE_TIMEOUT_MS) - && ep_status.head_offset() != 0 { - xous::yield_slice(); - ep_status = self.status_read_volatile(ep_addr.index()); - } - if ep_status.head_offset() != 0 { - log::warn!("head offset is not 0 even after waiting"); - return Err(UsbError::WouldBlock); - } - for (chain_offset, sub_buf) in buf.chunks(packet_len).enumerate() { let descriptor = self.descriptor_from_head_and_chain(head_offset, chain_offset, packet_len); descriptor.set_desc_flags(UsbDirection::In, @@ -807,6 +803,7 @@ impl UsbBus for SpinalUsbDevice { } descriptor.set_offset(0); // reset the write pointer to 0, also sets in_progress log::info!("bulk descriptor {} setup: {:x?}", chain_offset, descriptor); + self.last_descriptor.lock().unwrap()[ep_addr.index()] = Some(descriptor); } ep_status.set_max_packet_size(packet_len as _); diff --git a/services/usb-device-xous/stm32-usb.rs/firmware/usbd_bulk_only_transport/src/bulk_only_transport/bulk_only_transport.rs b/services/usb-device-xous/stm32-usb.rs/firmware/usbd_bulk_only_transport/src/bulk_only_transport/bulk_only_transport.rs index 50a4acba9..df9a57d7a 100644 --- a/services/usb-device-xous/stm32-usb.rs/firmware/usbd_bulk_only_transport/src/bulk_only_transport/bulk_only_transport.rs +++ b/services/usb-device-xous/stm32-usb.rs/firmware/usbd_bulk_only_transport/src/bulk_only_transport/bulk_only_transport.rs @@ -382,7 +382,7 @@ impl BulkOnlyTransport<'_, B> { Ok(&self.buffer[s..e]) } - fn flush(&mut self) -> Result<(), Error> { + pub fn flush(&mut self) -> Result<(), Error> { // let packet_size = self.max_packet_size() as usize; let packet_size = 512; // TODO: link this to the buffer length constant if it the lower-level rework works let residue = self.command_status_wrapper.data_residue as usize; @@ -461,9 +461,12 @@ impl BulkOnlyTransport<'_, B> { fn end_data_transfer(&mut self) -> Result<(), Error> { // Get the csw ready to send - log::debug!("data_i: {}, buffer_i: {}, buf: {:x?}", self.data_i, self.buffer_i, self.buffer); + //log::debug!("data_i: {}, buffer_i: {}, buf: {:x?}", self.data_i, self.buffer_i, self.buffer); self.pack_csw(); - log::debug!("data_i: {}, buffer_i: {}, buf: {:x?}", self.data_i, self.buffer_i, self.buffer); + log::info!("data_residue: {}", self.command_status_wrapper.data_residue); + self.flush()?; + //log::debug!("data_i: {}, buffer_i: {}, buf: {:x?}", self.data_i, self.buffer_i, self.buffer); + log::info!("data_residue: {}", self.command_status_wrapper.data_residue); // We only send a zero length packet if the last write was a full packet AND we are sending // less total bytes than the command header asked for @@ -477,9 +480,7 @@ impl BulkOnlyTransport<'_, B> { if needs_zlp { trace_bot_zlp!("ZLP> required"); self.change_state(State::NeedZlp); - self.flush()?; self.send_zlp()?; - self.flush()?; } else { trace_bot_zlp!("ZLP> not required"); self.change_state(State::NeedToSendStatus); diff --git a/services/usb-device-xous/stm32-usb.rs/firmware/usbd_scsi/src/scsi/scsi.rs b/services/usb-device-xous/stm32-usb.rs/firmware/usbd_scsi/src/scsi/scsi.rs index 2a89f8757..a8708ab4b 100644 --- a/services/usb-device-xous/stm32-usb.rs/firmware/usbd_scsi/src/scsi/scsi.rs +++ b/services/usb-device-xous/stm32-usb.rs/firmware/usbd_scsi/src/scsi/scsi.rs @@ -199,9 +199,12 @@ impl Scsi<'_, B, BD> { new_command, self.lba, self.lba_end, self.lba == self.lba_end); // We only get here if the buffer is empty - let buf = self.inner.take_buffer_space(BD::BLOCK_BYTES)?; - self.block_device.read_block(self.lba, buf)?; - self.lba += 1; + while self.lba <= self.lba_end { + let buf = self.inner.take_buffer_space(BD::BLOCK_BYTES)?; + self.block_device.read_block(self.lba, buf)?; + self.inner.flush()?; + self.lba += 1; + } if self.lba <= self.lba_end { Ongoing