Artemis Milestone 2h: blkio_usb.c backend -- USB thumb drive is now a real block device

Wires a hot-plugged USB Mass Storage device into the block subsystem's
unified LBN chain. blkio_usb.c/blkio_usb.h mirror virtio_blk.c/
virtio_blk.h's established shape exactly (singleton state, blkio_vtable_t,
a blkio_usb_open_msc() "find" function playing virtio_blk_find_artemis()'s
role): read() translates a Forth block into a SCSI LBA/count pair and
calls xhci_bot_read_block() + xhci_bot_wait_for_idle(); write() returns
BLKIO_ENOSUP (no SCSI WRITE(10) exists yet, and blk_format_or_load_disk()
never writes at attach time, so read-only is sufficient -- confirmed by
reading that function first, not assumed). Refuses (-2) if the reported
SCSI block size doesn't evenly divide the 1024-byte Forth block size.

Connect-time wiring reuses the bot_msc_attach_pending/consume-in-
sk_repl_idle() shape the prior increment's temp probe already validated,
now made permanent: SET_CONFIGURATION sets the flag, sk_repl_idle()
(strictly after its own xhci_poll_events() call returns) calls
blkio_usb_open_msc() then blk_subsys_attach_device().

Verified live via hot-attach: full chain from USB connect through
'blkio_usb: MSC device ready' to 'blk: disk 'StarForth Volume' v2 LBN
26074..75184 (49111 user blocks)' -- real attachment, disk image confirmed
byte-for-byte untouched after. Chased a real debugging detour along the
way: the attach initially appeared silent (no blk: log line) -- traced to
LOG_INFO filtering at the default LOG_WARN boot level, not a functional
bug (settled via a temporary log-level bump, reverted after capture; also
found and reported, but did not fix, a pre-existing unrelated
Makefile.starkernel bug where --log-level=info via KERNEL_ARGS breaks
printf parsing). All three architectures re-verified clean. FABRIC-2.md
Section X 2h updated -- only hot-detach remains for 2h.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CXjAPTEKrgY2Mrk25KoLDn
This commit is contained in:
Robert Allan James
2026-08-25 12:55:53 -04:00
co-authored by Claude Sonnet 5
parent d686f28853
commit 3b085dd875
17 changed files with 46077 additions and 9 deletions
+23
View File
@@ -35,6 +35,8 @@
#include "starkernel/timer.h"
#include "starkernel/arch.h"
#include "starkernel/xhci_driver.h"
#include "starkernel/blkio_usb.h"
#include "block_subsystem.h"
#include "word_source/include/keyboard_words.h"
#include <stdint.h>
#include <string.h>
@@ -87,6 +89,27 @@ static void sk_repl_idle(void)
* controller was found/brought up (xhci_bringup() never latched a
* device). */
xhci_poll_events();
/* Milestone 2h: a Mass Storage/BOT device finished SET_CONFIGURATION
* during the xhci_poll_events() call just above -- run the
* synchronous capacity query + block-subsystem attach here, strictly
* after that call has already returned (see bot_msc_attach_pending's
* own doc comment in xhci_driver.h for why: xhci_bot_wait_for_idle()'s
* busy-wait -- which blkio_usb_open_msc() uses internally -- must
* never run from inside xhci_poll_events()'s own call frame). */
xhci_dev_t *xdev = xhci_get_dev();
if (xdev && xdev->bot_msc_attach_pending) {
xdev->bot_msc_attach_pending = 0;
uint32_t slot_id = xdev->bot_msc_attach_slot_id;
static blkio_dev_t usb_blk_dev;
int rc = blkio_usb_open_msc(&usb_blk_dev, xdev, slot_id);
if (rc == 0) {
blk_subsys_attach_device(&usb_blk_dev);
} else {
console_println("xhci: USB MSC block-subsystem attach failed");
}
}
}
/*===========================================================================
+127
View File
@@ -0,0 +1,127 @@
/*
* blkio_usb.c — USB Mass Storage (Bulk-Only Transport) blkio_dev backend,
* see blkio_usb.h. Milestone 2h.
*/
#ifndef __STARKERNEL__
#error "blkio_usb.c is kernel-only"
#endif
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "starkernel/blkio_usb.h"
#include "console.h"
typedef struct {
xhci_dev_t *xdev;
uint32_t slot_id;
uint32_t scsi_block_size;
uint32_t scsi_blocks_per_fblock; /* BLKIO_FORTH_BLOCK_SIZE / scsi_block_size */
uint32_t total_forth_blocks;
} BlkioUsbState;
/* Singleton — one USB MSC device (single-outstanding-transaction scope,
* matching the xHCI driver this backend sits on). */
static BlkioUsbState g_usb_blk;
static int usb_blk_open(blkio_dev_t *dev, const blkio_params_t *p) {
(void)p;
if (!dev) return BLKIO_EINVAL;
dev->state = &g_usb_blk;
dev->forth_block_size = BLKIO_FORTH_BLOCK_SIZE;
dev->total_blocks = g_usb_blk.total_forth_blocks;
return BLKIO_OK;
}
static int usb_blk_close(blkio_dev_t *dev) {
(void)dev;
return BLKIO_OK;
}
static int usb_blk_read(blkio_dev_t *dev, uint32_t fblock, void *dst) {
if (!dev || !dst) return BLKIO_EINVAL;
BlkioUsbState *s = (BlkioUsbState *)dev->state;
if (fblock >= s->total_forth_blocks) return BLKIO_EINVAL;
uint32_t lba = fblock * s->scsi_blocks_per_fblock;
if (xhci_bot_read_block(s->xdev, s->slot_id, lba,
(uint16_t)s->scsi_blocks_per_fblock,
s->scsi_block_size) != 0) {
return BLKIO_EIO;
}
if (xhci_bot_wait_for_idle(s->xdev, 100000u) != BOT_STATUS_PASS) {
return BLKIO_EIO;
}
memcpy(dst, s->xdev->bot_data_buf, BLKIO_FORTH_BLOCK_SIZE);
return BLKIO_OK;
}
static int usb_blk_write(blkio_dev_t *dev, uint32_t fblock, const void *src) {
(void)dev;
(void)fblock;
(void)src;
/* No SCSI WRITE(10) in the xHCI driver yet — read-only this increment,
* see blkio_usb.h's own doc comment. */
return BLKIO_ENOSUP;
}
static int usb_blk_flush(blkio_dev_t *dev) {
(void)dev;
return BLKIO_OK; /* nothing buffered to flush — read-only backend */
}
static int usb_blk_info(blkio_dev_t *dev, blkio_info_t *out) {
if (!dev || !out) return BLKIO_EINVAL;
BlkioUsbState *s = (BlkioUsbState *)dev->state;
out->forth_block_size = BLKIO_FORTH_BLOCK_SIZE;
out->total_blocks = s->total_forth_blocks;
out->phys_sector_size = s->scsi_block_size;
out->phys_size_bytes = (uint64_t)s->total_forth_blocks * BLKIO_FORTH_BLOCK_SIZE;
out->read_only = 1;
return BLKIO_OK;
}
static const blkio_vtable_t g_usb_blk_vtable = {
.open = usb_blk_open,
.close = usb_blk_close,
.read = usb_blk_read,
.write = usb_blk_write,
.flush = usb_blk_flush,
.info = usb_blk_info
};
int blkio_usb_open_msc(blkio_dev_t *dev_out, xhci_dev_t *xdev, uint32_t slot_id) {
if (!dev_out || !xdev) return -1;
if (xhci_bot_get_capacity(xdev, slot_id) != 0) return -1;
if (xhci_bot_wait_for_idle(xdev, 100000u) != BOT_STATUS_PASS) return -1;
uint32_t block_size = xdev->bot_cap_block_size;
if (block_size == 0 || (BLKIO_FORTH_BLOCK_SIZE % block_size) != 0) {
console_println("blkio_usb: SCSI block size doesn't divide Forth block size -- refusing");
return -2;
}
uint64_t total_scsi_blocks = (uint64_t)xdev->bot_cap_last_lba + 1u;
uint32_t blocks_per_fblock = BLKIO_FORTH_BLOCK_SIZE / block_size;
uint64_t total_forth_blocks = total_scsi_blocks / blocks_per_fblock;
g_usb_blk.xdev = xdev;
g_usb_blk.slot_id = slot_id;
g_usb_blk.scsi_block_size = block_size;
g_usb_blk.scsi_blocks_per_fblock = blocks_per_fblock;
g_usb_blk.total_forth_blocks = (total_forth_blocks > 0xFFFFFFFFull)
? 0xFFFFFFFFu
: (uint32_t)total_forth_blocks;
console_println("blkio_usb: MSC device ready");
blkio_params_t params;
params.forth_block_size = BLKIO_FORTH_BLOCK_SIZE;
params.total_blocks = g_usb_blk.total_forth_blocks;
params.opaque = NULL;
return blkio_open(dev_out, &g_usb_blk_vtable, &params);
}
+9
View File
@@ -325,6 +325,8 @@ int xhci_bringup(xhci_dev_t *dev)
dev->bot_read10_block_size = 0;
dev->bot_cap_last_lba = 0;
dev->bot_cap_block_size = 0;
dev->bot_msc_attach_pending = 0;
dev->bot_msc_attach_slot_id = 0;
dev->next_action = XHCI_NEXT_ACTION_NONE;
dev->next_action_slot_id = 0;
dev->next_action_length = 0;
@@ -1403,6 +1405,13 @@ void xhci_poll_events(void)
}
case XHCI_XFER_SET_CONFIG: {
console_println("xhci: device configured");
/* Milestone 2h: hand off to sk_repl_idle(),
* the only safe place to run the synchronous
* capacity query + block-subsystem attach --
* see bot_msc_attach_pending's own doc
* comment in xhci_driver.h. */
dev->bot_msc_attach_pending = 1;
dev->bot_msc_attach_slot_id = xfer_slot_id;
break;
}
case XHCI_XFER_CBW_SENT: {