One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
171 lines
4.7 KiB
C
171 lines
4.7 KiB
C
#include "rx_window.h"
|
|
#include "wire.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct transfer {
|
|
unsigned slot;
|
|
size_t offset;
|
|
size_t capacity;
|
|
uint32_t tag;
|
|
uint32_t handle;
|
|
struct transfer *next;
|
|
};
|
|
|
|
struct rx_receiver {
|
|
struct rx_port *port;
|
|
struct transfer *head;
|
|
size_t count;
|
|
};
|
|
|
|
static struct transfer **find_transfer(struct rx_receiver *receiver, unsigned slot)
|
|
{
|
|
struct transfer **link = &receiver->head;
|
|
while (*link != NULL && (*link)->slot != slot)
|
|
link = &(*link)->next;
|
|
return link;
|
|
}
|
|
|
|
static int valid_window(unsigned slot, size_t offset, size_t capacity)
|
|
{
|
|
if (slot >= PORT_SLOTS || offset >= PORT_BANK_SIZE)
|
|
return 0;
|
|
if (capacity == 0 || capacity > PORT_CAPACITY_MAX)
|
|
return 0;
|
|
return capacity <= PORT_BANK_SIZE - offset;
|
|
}
|
|
|
|
struct rx_receiver *rx_create(struct rx_port *port)
|
|
{
|
|
struct rx_receiver *receiver;
|
|
if (port == NULL)
|
|
return NULL;
|
|
receiver = calloc(1, sizeof(*receiver));
|
|
if (receiver != NULL)
|
|
receiver->port = port;
|
|
return receiver;
|
|
}
|
|
|
|
size_t rx_outstanding(const struct rx_receiver *receiver)
|
|
{
|
|
return receiver != NULL ? receiver->count : 0;
|
|
}
|
|
|
|
static int retire(struct rx_receiver *receiver, struct transfer **link, int abort)
|
|
{
|
|
struct transfer *request = *link;
|
|
int code;
|
|
if (abort) {
|
|
code = port_abort(receiver->port, request->handle);
|
|
if (code != RX_OK)
|
|
return code;
|
|
}
|
|
code = port_release(receiver->port, request->handle);
|
|
if (code != RX_OK)
|
|
return code;
|
|
*link = request->next;
|
|
--receiver->count;
|
|
free(request);
|
|
return RX_OK;
|
|
}
|
|
|
|
void rx_destroy(struct rx_receiver *receiver)
|
|
{
|
|
if (receiver == NULL)
|
|
return;
|
|
while (receiver->head != NULL) {
|
|
if (retire(receiver, &receiver->head, 1) != RX_OK)
|
|
break;
|
|
}
|
|
free(receiver);
|
|
}
|
|
|
|
int rx_submit(struct rx_receiver *receiver, unsigned slot, size_t offset,
|
|
size_t capacity, uint32_t tag)
|
|
{
|
|
struct transfer *request;
|
|
int code;
|
|
if (receiver == NULL || !valid_window(slot, offset, capacity))
|
|
return RX_ARGUMENT;
|
|
if (*find_transfer(receiver, slot) != NULL)
|
|
return RX_STATE;
|
|
request = calloc(1, sizeof(*request));
|
|
if (request == NULL)
|
|
return RX_DEVICE;
|
|
code = port_acquire(receiver->port, slot, offset, capacity, tag, &request->handle);
|
|
if (code != RX_OK) {
|
|
free(request);
|
|
return code;
|
|
}
|
|
request->slot = slot;
|
|
request->offset = offset;
|
|
request->capacity = capacity;
|
|
request->tag = tag;
|
|
request->next = receiver->head;
|
|
receiver->head = request;
|
|
++receiver->count;
|
|
return RX_OK;
|
|
}
|
|
|
|
static int receive_frame(struct rx_receiver *receiver, const struct transfer *request,
|
|
size_t actual, struct rx_result *out)
|
|
{
|
|
uint8_t bytes[PORT_CAPACITY_MAX];
|
|
struct wire_packet packet;
|
|
size_t first, end;
|
|
int code;
|
|
if (actual == 0)
|
|
return RX_FORMAT;
|
|
first = request->offset / PORT_LINE_SIZE * PORT_LINE_SIZE;
|
|
end = first + (actual + PORT_LINE_SIZE - 1) /
|
|
PORT_LINE_SIZE * PORT_LINE_SIZE;
|
|
code = port_invalidate(receiver->port, request->handle, first, end - first);
|
|
if (code != RX_OK)
|
|
return code;
|
|
code = port_cpu_read(receiver->port, request->handle, request->offset, bytes, actual);
|
|
if (code != RX_OK)
|
|
return code;
|
|
if (wire_decode(bytes, actual, &packet) != 0 || packet.tag != request->tag)
|
|
return RX_FORMAT;
|
|
out->tag = packet.tag;
|
|
out->flags = packet.flags;
|
|
out->size = packet.size;
|
|
if (packet.size != 0)
|
|
memcpy(out->payload, packet.payload, packet.size);
|
|
return RX_OK;
|
|
}
|
|
|
|
int rx_collect(struct rx_receiver *receiver, unsigned slot, struct rx_result *out)
|
|
{
|
|
struct transfer **link;
|
|
struct port_completion completion;
|
|
int code, cleanup;
|
|
if (receiver == NULL || out == NULL || slot >= PORT_SLOTS)
|
|
return RX_ARGUMENT;
|
|
link = find_transfer(receiver, slot);
|
|
if (*link == NULL)
|
|
return RX_STATE;
|
|
code = port_completion(receiver->port, (*link)->handle, &completion);
|
|
if (code != RX_OK)
|
|
return code;
|
|
if (completion.state == PORT_FAILED)
|
|
return RX_DEVICE;
|
|
else if (completion.state == PORT_DONE)
|
|
code = receive_frame(receiver, *link, completion.actual, out);
|
|
else
|
|
code = RX_STATE;
|
|
cleanup = retire(receiver, link, 0);
|
|
return cleanup == RX_OK ? code : cleanup;
|
|
}
|
|
|
|
int rx_cancel(struct rx_receiver *receiver, unsigned slot)
|
|
{
|
|
struct transfer **link;
|
|
if (receiver == NULL || slot >= PORT_SLOTS)
|
|
return RX_ARGUMENT;
|
|
link = find_transfer(receiver, slot);
|
|
if (*link == NULL)
|
|
return RX_STATE;
|
|
return retire(receiver, link, 1);
|
|
}
|