boards/BRINGUP.md A1, FABRIC-3 SXLII. xhci.c keeps a table of controllers (up to 8) and services each; kernel_main brings up index 0, 1, ... until there is none. repl.c's and blkio_usb.c's per-slot state is per controller now (a slot ID is a controller's own), and each USB keyboard keeps its own last report and lights; an unplugged keyboard releases what it held. Test first: keyboard and identity on a second controller behind a bridge, Zuse on the first -- before, only Zuse; after, all three, on all three ISAs. Hot-plug and unplug on the second controller. Acceptance unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
183 lines
6.8 KiB
C
183 lines
6.8 KiB
C
/*
|
|
* 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 "starkernel/kmalloc.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;
|
|
|
|
/* FABRIC-3.md §VII (2026-09-05): was a single static instance ("one USB MSC
|
|
* device, single-outstanding-transaction scope, matching the xHCI driver
|
|
* this backend sits on") -- now a per-slot registry, same shape and same
|
|
* sizing precedent as xhci_dev_t's own msc_slots (registry sized off the
|
|
* controller's own max_slots, allocated once on first use here since this
|
|
* file has no bringup-time hook of its own to do it earlier). Indexed
|
|
* directly by xHCI slot ID, index 0 unused, matching msc_slots[]. */
|
|
|
|
/* One registry per controller (2026-10-14: every xHCI controller is now
|
|
* driven, and a slot ID is a controller's own -- slot 1 on two
|
|
* controllers is two devices). Found by the controller's own pointer. */
|
|
static struct {
|
|
xhci_dev_t *xdev;
|
|
BlkioUsbState *slots;
|
|
uint32_t count;
|
|
} g_usb_blk[XHCI_MAX_CONTROLLERS];
|
|
|
|
static BlkioUsbState *usb_blk_state_for(xhci_dev_t *xdev, uint32_t slot_id) {
|
|
uint32_t c;
|
|
if (!xdev || slot_id < 1 || slot_id > xdev->max_slots) return NULL;
|
|
for (c = 0; c < XHCI_MAX_CONTROLLERS; c++) {
|
|
if (g_usb_blk[c].xdev == xdev || !g_usb_blk[c].xdev) break;
|
|
}
|
|
if (c == XHCI_MAX_CONTROLLERS) return NULL;
|
|
if (!g_usb_blk[c].slots || g_usb_blk[c].count < xdev->max_slots + 1) {
|
|
size_t bytes = (size_t)(xdev->max_slots + 1) * sizeof(BlkioUsbState);
|
|
BlkioUsbState *fresh = (BlkioUsbState *)kmalloc_aligned(bytes, 64);
|
|
if (!fresh) return NULL;
|
|
memset(fresh, 0, bytes);
|
|
g_usb_blk[c].xdev = xdev;
|
|
g_usb_blk[c].slots = fresh;
|
|
g_usb_blk[c].count = xdev->max_slots + 1;
|
|
}
|
|
return &g_usb_blk[c].slots[slot_id];
|
|
}
|
|
|
|
static int usb_blk_open(blkio_dev_t *dev, const blkio_params_t *p) {
|
|
/* FABRIC-3.md §VII (2026-09-05): blkio_open() zeroes dev->state before
|
|
* calling this (see blkio.h), so the per-slot BlkioUsbState* this
|
|
* device should use travels in through p->opaque -- set by
|
|
* blkio_usb_open_msc() below -- rather than a single module-global. */
|
|
if (!dev || !p || !p->opaque) return BLKIO_EINVAL;
|
|
BlkioUsbState *s = (BlkioUsbState *)p->opaque;
|
|
dev->state = s;
|
|
dev->forth_block_size = BLKIO_FORTH_BLOCK_SIZE;
|
|
dev->total_blocks = s->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) {
|
|
if (!dev || !src) return BLKIO_EINVAL;
|
|
BlkioUsbState *s = (BlkioUsbState *)dev->state;
|
|
if (fblock >= s->total_forth_blocks) return BLKIO_EINVAL;
|
|
|
|
memcpy(s->xdev->bot_data_buf, src, BLKIO_FORTH_BLOCK_SIZE);
|
|
|
|
uint32_t lba = fblock * s->scsi_blocks_per_fblock;
|
|
if (xhci_bot_write_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;
|
|
}
|
|
return BLKIO_OK;
|
|
}
|
|
|
|
static int usb_blk_flush(blkio_dev_t *dev) {
|
|
(void)dev;
|
|
return BLKIO_OK; /* every write above is already synchronous -- see
|
|
* usb_blk_write()'s own xhci_bot_wait_for_idle()
|
|
* call -- nothing buffered here to flush. */
|
|
}
|
|
|
|
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 = 0; /* WRITE(10) verified live end-to-end on amd64,
|
|
* 2026-08-28 (FABRIC-2.md §F.1): BLK-CONFIRM-FORMAT's
|
|
* BAM/reloc writes and an explicit content write both
|
|
* survived a cold reboot and read back correctly. */
|
|
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;
|
|
|
|
BlkioUsbState *s = usb_blk_state_for(xdev, slot_id);
|
|
if (!s) 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;
|
|
|
|
s->xdev = xdev;
|
|
s->slot_id = slot_id;
|
|
s->scsi_block_size = block_size;
|
|
s->scsi_blocks_per_fblock = blocks_per_fblock;
|
|
s->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 = s->total_forth_blocks;
|
|
params.opaque = s;
|
|
|
|
return blkio_open(dev_out, &g_usb_blk_vtable, ¶ms);
|
|
}
|