Skip to content

Commit

Permalink
network: Use an outbound queue
Browse files Browse the repository at this point in the history
This should speed up freeing the SCSI bus since we're not waiting
for packets to send, but I'm not sure how much it helps.
  • Loading branch information
jcs committed Aug 18, 2023
1 parent bef630b commit a2a91ae
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 32 deletions.
7 changes: 5 additions & 2 deletions lib/BlueSCSI_platform_RP2040/BlueSCSI_platform_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ bool platform_network_wifi_join(char *ssid, char *password)

void platform_network_poll()
{
if (network_in_use)
cyw43_arch_poll();
if (!network_in_use)
return;

scsiNetworkPurge();
cyw43_arch_poll();
}

int platform_network_send(uint8_t *buf, size_t len)
Expand Down
93 changes: 64 additions & 29 deletions lib/SCSI2SD/src/firmware/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ extern bool g_log_debug;

static bool scsiNetworkEnabled = false;

static uint8_t scsiNetworkPackets[NETWORK_PACKET_QUEUE_SIZE][NETWORK_PACKET_MAX_SIZE];
static uint16_t scsiNetworkPacketSizes[NETWORK_PACKET_QUEUE_SIZE];
static uint8_t scsiNetworkPacketWriteIndex = 0;
static uint8_t scsiNetworkPacketReadIndex = 0;
static uint8_t scsiNetworkPacketsInbound[NETWORK_PACKET_QUEUE_SIZE][NETWORK_PACKET_MAX_SIZE];
static uint16_t scsiNetworkPacketInboundSizes[NETWORK_PACKET_QUEUE_SIZE];
static uint8_t scsiNetworkPacketInboundWriteIndex = 0;
static uint8_t scsiNetworkPacketInboundReadIndex = 0;

static uint8_t scsiNetworkPacketsOutbound[NETWORK_PACKET_QUEUE_SIZE][NETWORK_PACKET_MAX_SIZE];
static uint16_t scsiNetworkPacketOutboundSizes[NETWORK_PACKET_QUEUE_SIZE];
static uint8_t scsiNetworkPacketOutboundWriteIndex = 0;
static uint8_t scsiNetworkPacketOutboundReadIndex = 0;

struct __attribute__((packed)) wifi_network_entry wifi_network_list[WIFI_NETWORK_LIST_ENTRY_COUNT] = { 0 };

Expand Down Expand Up @@ -95,9 +100,9 @@ int scsiNetworkCommand()
int handled = 1;
int off = 0;
int parityError = 0;
uint8_t command = scsiDev.cdb[0];
uint32_t size = scsiDev.cdb[4] + (scsiDev.cdb[3] << 8);
long psize;
uint32_t size = scsiDev.cdb[4] + (scsiDev.cdb[3] << 8);
uint8_t command = scsiDev.cdb[0];
uint8_t cont = (scsiDev.cdb[5] == 0x80);

if (g_log_debug)
Expand All @@ -115,15 +120,15 @@ int scsiNetworkCommand()
break;
}

if (scsiNetworkPacketReadIndex == scsiNetworkPacketWriteIndex)
if (scsiNetworkPacketInboundReadIndex == scsiNetworkPacketInboundWriteIndex)
{
// nothing available
memset(scsiDev.data, 0, 6);
scsiDev.dataLen = 6;
}
else
{
psize = scsiNetworkPacketSizes[scsiNetworkPacketReadIndex];
psize = scsiNetworkPacketInboundSizes[scsiNetworkPacketInboundReadIndex];

// pad smaller packets
if (psize < 64)
Expand All @@ -138,25 +143,25 @@ int scsiNetworkCommand()

if (g_log_debug)
{
log_f("%s: sending packet[%d] to host of size %zu + 6", __func__, scsiNetworkPacketReadIndex, psize);
log_f("%s: sending packet[%d] to host of size %zu + 6", __func__, scsiNetworkPacketInboundReadIndex, psize);
}

scsiDev.dataLen = psize + 6; // 2-byte length + 4-byte flag + packet
memcpy(scsiDev.data + 6, scsiNetworkPackets[scsiNetworkPacketReadIndex], psize);
memcpy(scsiDev.data + 6, scsiNetworkPacketsInbound[scsiNetworkPacketInboundReadIndex], psize);
scsiDev.data[0] = (psize >> 8) & 0xff;
scsiDev.data[1] = psize & 0xff;

if (scsiNetworkPacketReadIndex == NETWORK_PACKET_QUEUE_SIZE - 1)
scsiNetworkPacketReadIndex = 0;
if (scsiNetworkPacketInboundReadIndex == NETWORK_PACKET_QUEUE_SIZE - 1)
scsiNetworkPacketInboundReadIndex = 0;
else
scsiNetworkPacketReadIndex++;
scsiNetworkPacketInboundReadIndex++;

// flags
scsiDev.data[2] = 0;
scsiDev.data[3] = 0;
scsiDev.data[4] = 0;
// more data to read?
scsiDev.data[5] = (scsiNetworkPacketReadIndex == scsiNetworkPacketWriteIndex ? 0 : 0x10);
scsiDev.data[5] = (scsiNetworkPacketInboundReadIndex == scsiNetworkPacketInboundWriteIndex ? 0 : 0x10);

if (g_log_debug)
{
Expand Down Expand Up @@ -225,7 +230,13 @@ int scsiNetworkCommand()
off = 4;
}

platform_network_send(scsiDev.data + off, size);
memcpy(&scsiNetworkPacketsOutbound[scsiNetworkPacketOutboundWriteIndex], scsiDev.data + off, size);
scsiNetworkPacketOutboundSizes[scsiNetworkPacketOutboundWriteIndex] = size;

if (scsiNetworkPacketOutboundWriteIndex == NETWORK_PACKET_QUEUE_SIZE - 1)
scsiNetworkPacketOutboundWriteIndex = 0;
else
scsiNetworkPacketOutboundWriteIndex++;

scsiDev.status = GOOD;
scsiDev.phase = STATUS;
Expand Down Expand Up @@ -264,8 +275,10 @@ int scsiNetworkCommand()
log_f("%s: enable interface", __func__);
}
scsiNetworkEnabled = true;
scsiNetworkPacketWriteIndex = 0;
scsiNetworkPacketReadIndex = 0;
scsiNetworkPacketInboundWriteIndex = 0;
scsiNetworkPacketInboundReadIndex = 0;
scsiNetworkPacketOutboundWriteIndex = 0;
scsiNetworkPacketOutboundReadIndex = 0;
}
else
{
Expand Down Expand Up @@ -421,32 +434,54 @@ int scsiNetworkEnqueue(const uint8_t *buf, size_t len)
if (!scsiNetworkEnabled)
return 0;

if (len + 4 > sizeof(scsiNetworkPackets[0]))
if (len + 4 > sizeof(scsiNetworkPacketsInbound[0]))
{
if (g_log_debug)
log_f("%s: dropping incoming network packet, too large (%zu > %zu)", __func__, len, sizeof(scsiNetworkPackets[0]));
log_f("%s: dropping incoming network packet, too large (%zu > %zu)", __func__, len, sizeof(scsiNetworkPacketsInbound[0]));
return 0;
}

memcpy(scsiNetworkPackets[scsiNetworkPacketWriteIndex], buf, len);
memcpy(scsiNetworkPacketsInbound[scsiNetworkPacketInboundWriteIndex], buf, len);
uint32_t crc = crc32(buf, len);
scsiNetworkPackets[scsiNetworkPacketWriteIndex][len] = crc & 0xff;
scsiNetworkPackets[scsiNetworkPacketWriteIndex][len + 1] = (crc >> 8) & 0xff;
scsiNetworkPackets[scsiNetworkPacketWriteIndex][len + 2] = (crc >> 16) & 0xff;
scsiNetworkPackets[scsiNetworkPacketWriteIndex][len + 3] = (crc >> 24) & 0xff;
scsiNetworkPacketsInbound[scsiNetworkPacketInboundWriteIndex][len] = crc & 0xff;
scsiNetworkPacketsInbound[scsiNetworkPacketInboundWriteIndex][len + 1] = (crc >> 8) & 0xff;
scsiNetworkPacketsInbound[scsiNetworkPacketInboundWriteIndex][len + 2] = (crc >> 16) & 0xff;
scsiNetworkPacketsInbound[scsiNetworkPacketInboundWriteIndex][len + 3] = (crc >> 24) & 0xff;

scsiNetworkPacketSizes[scsiNetworkPacketWriteIndex] = len + 4;
scsiNetworkPacketInboundSizes[scsiNetworkPacketInboundWriteIndex] = len + 4;

if (scsiNetworkPacketWriteIndex == NETWORK_PACKET_QUEUE_SIZE - 1)
scsiNetworkPacketWriteIndex = 0;
if (scsiNetworkPacketInboundWriteIndex == NETWORK_PACKET_QUEUE_SIZE - 1)
scsiNetworkPacketInboundWriteIndex = 0;
else
scsiNetworkPacketWriteIndex++;
scsiNetworkPacketInboundWriteIndex++;

if (scsiNetworkPacketWriteIndex == scsiNetworkPacketReadIndex)
if (scsiNetworkPacketInboundWriteIndex == scsiNetworkPacketInboundReadIndex)
{
if (g_log_debug)
log_f("%s: dropping packets in ring, write index caught up to read index", __func__);
}

return 1;
}

int scsiNetworkPurge(void)
{
int sent = 0;

if (!scsiNetworkEnabled)
return 0;

while (scsiNetworkPacketOutboundReadIndex != scsiNetworkPacketOutboundWriteIndex)
{
platform_network_send(scsiNetworkPacketsOutbound[scsiNetworkPacketOutboundReadIndex], scsiNetworkPacketOutboundSizes[scsiNetworkPacketOutboundReadIndex]);

if (scsiNetworkPacketOutboundReadIndex == NETWORK_PACKET_QUEUE_SIZE - 1)
scsiNetworkPacketOutboundReadIndex = 0;
else
scsiNetworkPacketOutboundReadIndex++;

sent++;
}

return sent;
}
3 changes: 2 additions & 1 deletion lib/SCSI2SD/src/firmware/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C" {
#define SCSI_NETWORK_WIFI_CMD_INFO 0x04
#define SCSI_NETWORK_WIFI_CMD_JOIN 0x05

#define NETWORK_PACKET_QUEUE_SIZE 30 // must be <= 255
#define NETWORK_PACKET_QUEUE_SIZE 20 // must be <= 255
#define NETWORK_PACKET_MAX_SIZE 1520

struct __attribute__((packed)) wifi_network_entry {
Expand All @@ -54,6 +54,7 @@ struct __attribute__((packed)) wifi_join_request {

int scsiNetworkCommand(void);
int scsiNetworkEnqueue(const uint8_t *buf, size_t len);
int scsiNetworkPurge(void);

extern int platform_network_send(uint8_t *buf, size_t len);

Expand Down

0 comments on commit a2a91ae

Please sign in to comment.