-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdevice.c
833 lines (651 loc) · 18.8 KB
/
device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/*
* Copyright (c) 2020 Niklas Ekström
*
* Thanks to Christian Vogelgsang and Mike Sterling for inspiration gained from their SANA-II drivers:
* - https://github.com/cnvogelg/plipbox
* - https://github.com/mikestir/k1208-drivers
*/
#include <exec/types.h>
#include <exec/execbase.h>
#include <exec/devices.h>
#include <exec/errors.h>
#include <exec/ports.h>
#include <libraries/dos.h>
#include <devices/sana2.h>
#include <proto/alib.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/timer.h>
#include <string.h>
#include "../a314device/a314.h"
#include "../a314device/proto_a314.h"
// Defines.
#define DEVICE_NAME "a314eth.device"
#define SERVICE_NAME "ethernet"
#define TASK_PRIO 10
#define MACADDR_SIZE 6
#define NIC_BPS 10000000
#define ETH_MTU 1500
#define RAW_MTU 1518
#define READ_FRAME_REQ 1
#define WRITE_FRAME_REQ 2
#define READ_FRAME_RES 3
#define WRITE_FRAME_RES 4
#define ET_RBUF_CNT 2
#define ET_WBUF_CNT 2
#define ET_BUF_CNT (ET_RBUF_CNT + ET_WBUF_CNT)
// Typedefs.
typedef BOOL (*buf_copy_func_t)(__reg("a0") void *dst, __reg("a1") void *src, __reg("d0") LONG size);
// Structs.
#pragma pack(push, 1)
struct EthHdr
{
unsigned char eh_Dst[MACADDR_SIZE];
unsigned char eh_Src[MACADDR_SIZE];
unsigned short eh_Type;
};
#pragma pack(pop)
#pragma pack(push, 1)
struct ServiceMsg
{
ULONG sm_Address;
UWORD sm_Length;
UWORD sm_Kind;
};
#pragma pack(pop)
struct BufDesc
{
struct MinNode bd_Node;
void *bd_Buffer;
ULONG bd_BufferAddress;
int bd_Length;
};
// Constants.
const char device_name[] = DEVICE_NAME;
const char id_string[] = DEVICE_NAME " 1.1 (3.3.2024)";
static const char service_name[] = SERVICE_NAME;
static const char a314_device_name[] = A314_NAME;
static const unsigned char macaddr[MACADDR_SIZE] = { 0x40, 0x61, 0x33, 0x31, 0x34, 0x65 };
// Global variables.
BPTR saved_seg_list;
struct ExecBase *SysBase;
struct DosLibrary *DOSBase;
struct Library *A314Base;
buf_copy_func_t copyfrom;
buf_copy_func_t copyto;
volatile struct List ut_rbuf_list;
volatile struct List ut_wbuf_list;
struct BufDesc et_bufs[ET_BUF_CNT];
struct List et_rbuf_free_list;
struct List et_rbuf_pending_list;
struct List et_rbuf_has_data_list;
struct List et_wbuf_free_list;
struct List et_wbuf_pending_list;
LONG a314_socket;
short last_req_kind;
struct MsgPort a314_mp;
struct A314_IORequest read_ior;
struct A314_IORequest write_ior;
struct A314_IORequest reset_ior;
BOOL pending_a314_read;
BOOL pending_a314_write;
BOOL pending_a314_reset;
struct ServiceMsg a314_read_buf;
struct ServiceMsg a314_write_buf;
volatile ULONG sana2_sigmask;
volatile ULONG shutdown_sigmask;
volatile struct Task *init_task;
struct Process *device_process;
volatile int device_start_error;
struct Sana2DeviceStats global_stats;
BOOL is_online;
// External declarations.
extern void device_process_seglist();
// Procedures.
static struct Library *init_device(__reg("a6") struct ExecBase *sys_base, __reg("a0") BPTR seg_list, __reg("d0") struct Library *dev)
{
saved_seg_list = seg_list;
dev->lib_Node.ln_Type = NT_DEVICE;
dev->lib_Node.ln_Name = (char *)device_name;
dev->lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
dev->lib_Version = 1;
dev->lib_Revision = 0;
dev->lib_IdString = (APTR)id_string;
SysBase = *(struct ExecBase **)4;
DOSBase = (struct DosLibrary *)OpenLibrary(DOSNAME, 0);
return dev;
}
static BPTR expunge(__reg("a6") struct Library *dev)
{
if (dev->lib_OpenCnt)
{
dev->lib_Flags |= LIBF_DELEXP;
return 0;
}
// Shady way of waiting for device process to terminate before unloading.
Delay(10);
CloseLibrary((struct Library *)DOSBase);
Remove(&dev->lib_Node);
FreeMem((char *)dev - dev->lib_NegSize, dev->lib_NegSize + dev->lib_PosSize);
return saved_seg_list;
}
static void send_a314_cmd(struct A314_IORequest *ior, UWORD cmd, char *buffer, int length)
{
ior->a314_Request.io_Command = cmd;
ior->a314_Request.io_Error = 0;
ior->a314_Socket = a314_socket;
ior->a314_Buffer = buffer;
ior->a314_Length = length;
SendIO((struct IORequest *)ior);
}
static void do_a314_cmd(struct A314_IORequest *ior, UWORD cmd, char *buffer, int length)
{
ior->a314_Request.io_Command = cmd;
ior->a314_Request.io_Error = 0;
ior->a314_Socket = a314_socket;
ior->a314_Buffer = buffer;
ior->a314_Length = length;
DoIO((struct IORequest *)ior);
}
static void copy_from_bd_and_reply(struct IOSana2Req *ios2, struct BufDesc *bd)
{
// TODO: Use S2_DMACopyToBuff32 instead of S2_CopyToBuff (if possible).
// This will avoid copying from A314 shared memory to shadow buffer
// before copying to stack's buffer.
ReadMemA314(bd->bd_Buffer, bd->bd_BufferAddress, bd->bd_Length);
struct EthHdr *eh = bd->bd_Buffer;
if (ios2->ios2_Req.io_Flags & SANA2IOF_RAW)
{
ios2->ios2_DataLength = bd->bd_Length;
copyto(ios2->ios2_Data, bd->bd_Buffer, ios2->ios2_DataLength);
ios2->ios2_Req.io_Flags = SANA2IOF_RAW;
}
else
{
ios2->ios2_DataLength = bd->bd_Length - sizeof(struct EthHdr);
copyto(ios2->ios2_Data, &eh[1], ios2->ios2_DataLength);
ios2->ios2_Req.io_Flags = 0;
}
memcpy(ios2->ios2_SrcAddr, eh->eh_Src, MACADDR_SIZE);
memcpy(ios2->ios2_DstAddr, eh->eh_Dst, MACADDR_SIZE);
BOOL bcast = TRUE;
for (int i = 0; i < MACADDR_SIZE; i++)
{
if (eh->eh_Dst[i] != 0xff)
{
bcast = FALSE;
break;
}
}
if (bcast)
ios2->ios2_Req.io_Flags |= SANA2IOF_BCAST;
ios2->ios2_PacketType = eh->eh_Type;
ios2->ios2_Req.io_Error = 0;
ReplyMsg(&ios2->ios2_Req.io_Message);
global_stats.PacketsReceived++;
}
static void copy_to_bd_and_reply(struct BufDesc *bd, struct IOSana2Req *ios2)
{
// TODO: Use S2_DMACopyFromBuff32 instead of S2_CopyFromBuff (if possible).
// This will avoid copying to shadow buffer before writing to
// A314 shared memory.
struct EthHdr *eh = bd->bd_Buffer;
if (ios2->ios2_Req.io_Flags & SANA2IOF_RAW)
{
copyfrom(bd->bd_Buffer, ios2->ios2_Data, ios2->ios2_DataLength);
bd->bd_Length = ios2->ios2_DataLength;
}
else
{
eh->eh_Type = ios2->ios2_PacketType;
memcpy(eh->eh_Src, macaddr, sizeof(macaddr));
memcpy(eh->eh_Dst, ios2->ios2_DstAddr, MACADDR_SIZE);
copyfrom(&eh[1], ios2->ios2_Data, ios2->ios2_DataLength);
bd->bd_Length = ios2->ios2_DataLength + sizeof(struct EthHdr);
}
WriteMemA314(bd->bd_BufferAddress, bd->bd_Buffer, bd->bd_Length);
ios2->ios2_Req.io_Error = 0;
ReplyMsg(&ios2->ios2_Req.io_Message);
global_stats.PacketsSent++;
}
static void handle_a314_reply(struct A314_IORequest *ior)
{
if (ior == &write_ior)
{
pending_a314_write = FALSE;
if (ior->a314_Request.io_Error == A314_WRITE_OK)
{
// Start new write later.
}
else // A314_WRITE_RESET
{
// TODO: Handle. What if pi-side is shutting down.
}
}
else if (ior == &read_ior)
{
pending_a314_read = FALSE;
if (ior->a314_Request.io_Error == A314_READ_OK)
{
if (a314_read_buf.sm_Kind == WRITE_FRAME_RES)
{
struct BufDesc *bd = (struct BufDesc *)RemHead(&et_wbuf_pending_list);
AddTail(&et_wbuf_free_list, (struct Node *)bd);
}
else // READ_FRAME_RES
{
struct BufDesc *bd = (struct BufDesc *)RemHead(&et_rbuf_pending_list);
bd->bd_Length = a314_read_buf.sm_Length;
AddTail(&et_rbuf_has_data_list, (struct Node *)bd);
}
send_a314_cmd(&read_ior, A314_READ, (void *)&a314_read_buf, sizeof(a314_read_buf));
pending_a314_read = TRUE;
}
else // A314_READ_RESET
{
// TODO: Handle. What if pi-side is shutting down.
}
}
else if (ior == &reset_ior)
{
pending_a314_reset = FALSE;
}
}
static struct IOSana2Req *remove_matching_rbuf(ULONG type)
{
struct Node *node = ut_rbuf_list.lh_Head;
while (node->ln_Succ)
{
struct IOSana2Req *ios2 = (struct IOSana2Req *)node;
if (ios2->ios2_PacketType == type)
{
Remove(node);
return ios2;
}
node = node->ln_Succ;
}
return NULL;
}
static void complete_read_reqs()
{
struct Node *node = et_rbuf_has_data_list.lh_Head;
if (!node->ln_Succ)
return;
Forbid();
while (node->ln_Succ)
{
struct BufDesc *bd = (struct BufDesc *)node;
UWORD eh_type;
// Hardcoded offset (12) and size (2) of eh_Type in EthHdr.
ReadMemA314(&eh_type, bd->bd_BufferAddress + 12, 2);
node = node->ln_Succ;
struct IOSana2Req *ios2 = remove_matching_rbuf(eh_type);
if (ios2)
copy_from_bd_and_reply(ios2, bd);
else
global_stats.UnknownTypesReceived++;
Remove((struct Node *)bd);
AddTail(&et_rbuf_free_list, (struct Node *)bd);
}
Permit();
}
static void maybe_write_req()
{
if (pending_a314_write)
return;
BOOL free_et_wbuf = et_wbuf_free_list.lh_Head->ln_Succ != NULL;
BOOL idle_et_rbuf = et_rbuf_free_list.lh_Head->ln_Succ != NULL;
Forbid();
BOOL waiting_ut_wbuf = ut_wbuf_list.lh_Head->ln_Succ != NULL;
BOOL want_wbuf = free_et_wbuf && waiting_ut_wbuf;
BOOL want_rbuf = idle_et_rbuf;
if (!want_rbuf && !want_wbuf)
{
Permit();
return;
}
short next_req_kind = 0;
if (last_req_kind == WRITE_FRAME_REQ)
next_req_kind = want_rbuf ? READ_FRAME_REQ : WRITE_FRAME_REQ;
else
next_req_kind = want_wbuf ? WRITE_FRAME_REQ : READ_FRAME_REQ;
struct IOSana2Req *ios2 = NULL;
if (next_req_kind == WRITE_FRAME_REQ)
ios2 = (struct IOSana2Req*)RemHead((struct List *)&ut_wbuf_list);
Permit();
struct BufDesc *bd;
if (next_req_kind == READ_FRAME_REQ)
{
bd = (struct BufDesc *)RemHead(&et_rbuf_free_list);
bd->bd_Length = RAW_MTU;
AddTail(&et_rbuf_pending_list, (struct Node *)&bd->bd_Node);
}
else // WRITE_FRAME_REQ
{
bd = (struct BufDesc *)RemHead(&et_wbuf_free_list);
copy_to_bd_and_reply(bd, ios2);
AddTail(&et_wbuf_pending_list, (struct Node *)bd);
}
a314_write_buf.sm_Address = bd->bd_BufferAddress;
a314_write_buf.sm_Length = bd->bd_Length;
a314_write_buf.sm_Kind = next_req_kind;
send_a314_cmd(&write_ior, A314_WRITE, (void *)&a314_write_buf, sizeof(a314_write_buf));
pending_a314_write = TRUE;
last_req_kind = next_req_kind;
}
void device_process_run()
{
ULONG sana2_signal = AllocSignal(-1);
sana2_sigmask = 1UL << sana2_signal;
ULONG shutdown_signal = AllocSignal(-1);
shutdown_sigmask = 1UL << shutdown_signal;
a314_mp.mp_SigBit = AllocSignal(-1);
a314_mp.mp_SigTask = FindTask(NULL);
do_a314_cmd(&reset_ior, A314_CONNECT, (char *)service_name, strlen(service_name));
device_start_error = reset_ior.a314_Request.io_Error == A314_CONNECT_OK ? 0 : -1;
Signal((struct Task *)init_task, SIGF_SINGLE);
if (device_start_error)
return;
ULONG a314_sigmask = 1UL << a314_mp.mp_SigBit;
send_a314_cmd(&read_ior, A314_READ, (void *)&a314_read_buf, sizeof(a314_read_buf));
pending_a314_read = TRUE;
BOOL shutting_down = FALSE;
while (TRUE)
{
complete_read_reqs();
maybe_write_req();
if (shutting_down && !pending_a314_read && !pending_a314_write && !pending_a314_reset)
break;
ULONG sigs = Wait(a314_sigmask | sana2_sigmask | shutdown_sigmask);
if ((sigs & shutdown_sigmask) && !shutting_down)
{
send_a314_cmd(&reset_ior, A314_RESET, NULL, 0);
pending_a314_reset = TRUE;
shutting_down = TRUE;
}
if (sigs & a314_sigmask)
{
struct A314_IORequest *ior;
while ((ior = (struct A314_IORequest *)GetMsg(&a314_mp)))
handle_a314_reply(ior);
}
}
Signal((struct Task *)init_task, SIGF_SINGLE);
}
static struct TagItem *FindTagItem(Tag tagVal, struct TagItem *tagList)
{
struct TagItem *ti = tagList;
while (ti && ti->ti_Tag != tagVal)
{
switch (ti->ti_Tag)
{
case TAG_DONE:
return NULL;
case TAG_MORE:
ti = (struct TagItem *)ti->ti_Data;
break;
case TAG_SKIP:
ti += ti->ti_Data + 1;
break;
case TAG_IGNORE:
default:
ti++;
break;
}
}
return ti;
}
static ULONG GetTagData(Tag tagVal, ULONG defaultData, struct TagItem *tagList)
{
struct TagItem *ti = FindTagItem(tagVal, tagList);
return ti ? ti->ti_Data : defaultData;
}
static void open(__reg("a6") struct Library *dev, __reg("a1") struct IOSana2Req *ios2, __reg("d0") ULONG unitnum, __reg("d1") ULONG flags)
{
ios2->ios2_Req.io_Error = IOERR_OPENFAIL;
ios2->ios2_Req.io_Message.mn_Node.ln_Type = NT_REPLYMSG;
if (unitnum != 0 || dev->lib_OpenCnt)
return;
dev->lib_OpenCnt++;
copyfrom = (buf_copy_func_t)GetTagData(S2_CopyFromBuff, 0, (struct TagItem *)ios2->ios2_BufferManagement);
copyto = (buf_copy_func_t)GetTagData(S2_CopyToBuff, 0, (struct TagItem *)ios2->ios2_BufferManagement);
ios2->ios2_BufferManagement = (void *)0xdeadbeefUL;
memset(&a314_mp, 0, sizeof(a314_mp));
a314_mp.mp_Node.ln_Pri = 0;
a314_mp.mp_Node.ln_Type = NT_MSGPORT;
a314_mp.mp_Node.ln_Name = (char *)device_name;
a314_mp.mp_Flags = PA_SIGNAL;
NewList(&a314_mp.mp_MsgList);
memset(&write_ior, 0, sizeof(write_ior));
write_ior.a314_Request.io_Message.mn_ReplyPort = &a314_mp;
write_ior.a314_Request.io_Message.mn_Length = sizeof(write_ior);
write_ior.a314_Request.io_Message.mn_Node.ln_Type = NT_REPLYMSG;
A314Base = NULL;
if (OpenDevice((char *)a314_device_name, 0, (struct IORequest *)&write_ior, 0))
goto error;
A314Base = &(write_ior.a314_Request.io_Device->dd_Library);
memcpy(&read_ior, &write_ior, sizeof(read_ior));
memcpy(&reset_ior, &write_ior, sizeof(reset_ior));
memset(&global_stats, 0, sizeof(global_stats));
is_online = FALSE;
struct DateStamp ds;
DateStamp(&ds);
a314_socket = (ds.ds_Minute * 60 * TICKS_PER_SECOND) + ds.ds_Tick;
last_req_kind = WRITE_FRAME_REQ;
NewList((struct List *)&ut_rbuf_list);
NewList((struct List *)&ut_wbuf_list);
NewList(&et_rbuf_free_list);
NewList(&et_rbuf_pending_list);
NewList(&et_rbuf_has_data_list);
NewList(&et_wbuf_free_list);
NewList(&et_wbuf_pending_list);
for (int i = 0; i < ET_BUF_CNT; i++)
memset(&et_bufs[i], 0, sizeof(struct BufDesc));
for (int i = 0; i < ET_BUF_CNT; i++)
{
struct BufDesc *bd = &et_bufs[i];
bd->bd_Buffer = AllocMem(RAW_MTU, 0);
if (!bd->bd_Buffer)
goto error;
bd->bd_BufferAddress = AllocMemA314(RAW_MTU);
if (bd->bd_BufferAddress == INVALID_A314_ADDRESS)
goto error;
if (i < ET_RBUF_CNT)
AddTail(&et_rbuf_free_list, (struct Node*)&bd->bd_Node);
else
AddTail(&et_wbuf_free_list, (struct Node*)&bd->bd_Node);
}
init_task = FindTask(NULL);
struct MsgPort *device_mp = CreateProc((char *)device_name, TASK_PRIO, ((ULONG)device_process_seglist) >> 2, 2048);
if (!device_mp)
goto error;
device_process = (struct Process *)((char *)device_mp - sizeof(struct Task));
Wait(SIGF_SINGLE);
if (device_start_error)
goto error;
ios2->ios2_Req.io_Error = 0;
return;
error:
for (int i = ET_BUF_CNT - 1; i >= 0; i--)
{
if (et_bufs[i].bd_BufferAddress)
FreeMemA314(et_bufs[i].bd_BufferAddress, RAW_MTU);
if (et_bufs[i].bd_Buffer)
FreeMem(et_bufs[i].bd_Buffer, RAW_MTU);
}
if (A314Base)
{
CloseDevice((struct IORequest *)&write_ior);
A314Base = NULL;
}
dev->lib_OpenCnt--;
}
static BPTR close(__reg("a6") struct Library *dev, __reg("a1") struct IOSana2Req *ios2)
{
init_task = FindTask(NULL);
Signal(&device_process->pr_Task, shutdown_sigmask);
Wait(SIGF_SINGLE);
for (int i = ET_BUF_CNT - 1; i >= 0; i--)
{
FreeMemA314(et_bufs[i].bd_BufferAddress, RAW_MTU);
FreeMem(et_bufs[i].bd_Buffer, RAW_MTU);
}
CloseDevice((struct IORequest *)&write_ior);
A314Base = NULL;
ios2->ios2_Req.io_Device = NULL;
ios2->ios2_Req.io_Unit = NULL;
dev->lib_OpenCnt--;
if (dev->lib_OpenCnt == 0 && (dev->lib_Flags & LIBF_DELEXP))
return expunge(dev);
return 0;
}
static void device_query(struct IOSana2Req *req)
{
struct Sana2DeviceQuery *query;
query = req->ios2_StatData;
query->DevQueryFormat = 0;
query->DeviceLevel = 0;
if (query->SizeAvailable >= 18)
query->AddrFieldSize = MACADDR_SIZE * 8;
if (query->SizeAvailable >= 22)
query->MTU = ETH_MTU;
if (query->SizeAvailable >= 26)
query->BPS = NIC_BPS;
if (query->SizeAvailable >= 30)
query->HardwareType = S2WireType_Ethernet;
query->SizeSupplied = query->SizeAvailable < 30 ? query->SizeAvailable : 30;
}
static void set_last_start()
{
struct IORequest req;
memset(&req, 0, sizeof(req));
req.io_Message.mn_Length = sizeof(req);
if (OpenDevice(TIMERNAME, UNIT_MICROHZ, &req, 0) == 0)
{
struct Device *TimerBase = req.io_Device;
GetSysTime(&global_stats.LastStart);
CloseDevice(&req);
}
}
static void begin_io(__reg("a6") struct Library *dev, __reg("a1") struct IOSana2Req *ios2)
{
ios2->ios2_Req.io_Error = S2ERR_NO_ERROR;
ios2->ios2_WireError = S2WERR_GENERIC_ERROR;
switch (ios2->ios2_Req.io_Command)
{
case CMD_READ:
if (!ios2->ios2_BufferManagement)
{
ios2->ios2_Req.io_Error = S2ERR_BAD_ARGUMENT;
ios2->ios2_WireError = S2WERR_BUFF_ERROR;
break;
}
Forbid();
AddTail((struct List *)&ut_rbuf_list, &ios2->ios2_Req.io_Message.mn_Node);
Permit();
ios2->ios2_Req.io_Flags &= ~SANA2IOF_QUICK;
ios2 = NULL;
Signal(&device_process->pr_Task, sana2_sigmask);
break;
case S2_BROADCAST:
memset(ios2->ios2_DstAddr, 0xff, MACADDR_SIZE);
/* Fall through */
case CMD_WRITE:
if (((ios2->ios2_Req.io_Flags & SANA2IOF_RAW) != 0 && ios2->ios2_DataLength > RAW_MTU) ||
((ios2->ios2_Req.io_Flags & SANA2IOF_RAW) == 0 && ios2->ios2_DataLength > ETH_MTU))
{
ios2->ios2_Req.io_Error = S2ERR_MTU_EXCEEDED;
break;
}
if (!ios2->ios2_BufferManagement)
{
ios2->ios2_Req.io_Error = S2ERR_BAD_ARGUMENT;
ios2->ios2_WireError = S2WERR_BUFF_ERROR;
break;
}
Forbid();
AddTail((struct List *)&ut_wbuf_list, &ios2->ios2_Req.io_Message.mn_Node);
Permit();
ios2->ios2_Req.io_Flags &= ~SANA2IOF_QUICK;
ios2 = NULL;
Signal(&device_process->pr_Task, sana2_sigmask);
break;
case S2_CONFIGINTERFACE:
/* Fall through */
case S2_ONLINE:
set_last_start();
is_online = TRUE;
break;
case S2_OFFLINE:
is_online = FALSE;
break;
case S2_GETSTATIONADDRESS:
memcpy(ios2->ios2_SrcAddr, macaddr, sizeof(macaddr));
memcpy(ios2->ios2_DstAddr, macaddr, sizeof(macaddr));
break;
case S2_DEVICEQUERY:
device_query(ios2);
break;
case S2_ONEVENT:
case S2_TRACKTYPE:
case S2_UNTRACKTYPE:
case S2_GETTYPESTATS:
case S2_READORPHAN:
case S2_GETSPECIALSTATS:
break;
case S2_GETGLOBALSTATS:
if (ios2->ios2_StatData)
memcpy(ios2->ios2_StatData, &global_stats, sizeof(struct Sana2DeviceStats));
break;
default:
ios2->ios2_Req.io_Error = IOERR_NOCMD;
ios2->ios2_WireError = S2WERR_GENERIC_ERROR;
break;
}
if (ios2)
{
if (ios2->ios2_Req.io_Flags & SANA2IOF_QUICK)
ios2->ios2_Req.io_Message.mn_Node.ln_Type = NT_MESSAGE;
else
ReplyMsg(&ios2->ios2_Req.io_Message);
}
}
static void remove_from_list(struct List *list, struct Node *node)
{
for (struct Node *n = list->lh_Head; n->ln_Succ; n = n->ln_Succ)
{
if (n == node)
{
Remove(n);
return;
}
}
}
static ULONG abort_io(__reg("a6") struct Library *dev, __reg("a1") struct IOSana2Req *ios2)
{
Forbid();
remove_from_list((struct List *)&ut_rbuf_list, &ios2->ios2_Req.io_Message.mn_Node);
remove_from_list((struct List *)&ut_wbuf_list, &ios2->ios2_Req.io_Message.mn_Node);
Permit();
ios2->ios2_Req.io_Error = IOERR_ABORTED;
ios2->ios2_WireError = 0;
ReplyMsg(&ios2->ios2_Req.io_Message);
return 0;
}
static ULONG device_vectors[] =
{
(ULONG)open,
(ULONG)close,
(ULONG)expunge,
0,
(ULONG)begin_io,
(ULONG)abort_io,
-1,
};
ULONG auto_init_tables[] =
{
sizeof(struct Library),
(ULONG)device_vectors,
0,
(ULONG)init_device,
};