6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 #include <exec/types.h>
23 #include <exec/resident.h>
25 #include <exec/ports.h>
26 #include <exec/errors.h>
28 #include <devices/sana2.h>
29 #include <devices/sana2specialstats.h>
30 #include <devices/newstyle.h>
32 #include <utility/utility.h>
33 #include <utility/tagitem.h>
34 #include <utility/hooks.h>
36 #include <proto/exec.h>
37 #include <proto/dos.h>
38 #include <proto/battclock.h>
42 #include "via-rhine.h"
44 #include LC_LIBDEFS_FILE
46 #define KNOWN_EVENTS \
47 (S2EVENT_ERROR | S2EVENT_TX | S2EVENT_RX | S2EVENT_ONLINE \
48 | S2EVENT_OFFLINE | S2EVENT_BUFF | S2EVENT_HARDWARE | S2EVENT_SOFTWARE)
50 static BOOL
CmdInvalid(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
51 static BOOL
CmdRead(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
52 static BOOL
CmdWrite(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
53 static BOOL
CmdFlush(LIBBASETYPEPTR LIBBASE
, struct IORequest
*request
);
54 static BOOL
CmdS2DeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
55 static BOOL
CmdGetStationAddress(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
56 static BOOL
CmdConfigInterface(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
57 static BOOL
CmdBroadcast(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
58 static BOOL
CmdTrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
59 static BOOL
CmdUntrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
60 static BOOL
CmdGetTypeStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
61 static BOOL
CmdGetGlobalStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
62 static BOOL
CmdDeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOStdReq
*request
);
63 static BOOL
CmdOnEvent(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
64 static BOOL
CmdReadOrphan(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
65 static BOOL
CmdOnline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
66 static BOOL
CmdOffline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
67 static BOOL
CmdAddMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
68 static BOOL
CmdDelMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
70 static const UWORD supported_commands
[] =
78 S2_ADDMULTICASTADDRESS
,
79 S2_DELMULTICASTADDRESS
,
85 // S2_GETSPECIALSTATS,
92 S2_ADDMULTICASTADDRESSES
,
93 S2_DELMULTICASTADDRESSES
,
97 void handle_request(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
101 switch(request
->ios2_Req
.io_Command
)
104 complete
= CmdRead(LIBBASE
, request
);
109 complete
= CmdWrite(LIBBASE
, request
);
113 complete
= CmdFlush(LIBBASE
, (struct IORequest
*)request
);
117 complete
= CmdS2DeviceQuery(LIBBASE
, request
);
120 case S2_GETSTATIONADDRESS
:
121 complete
= CmdGetStationAddress(LIBBASE
, request
);
124 case S2_CONFIGINTERFACE
:
125 complete
= CmdConfigInterface(LIBBASE
, request
);
129 complete
= CmdBroadcast(LIBBASE
, request
);
133 complete
= CmdTrackType(LIBBASE
, request
);
137 complete
= CmdUntrackType(LIBBASE
, request
);
140 case S2_GETTYPESTATS
:
141 complete
= CmdGetTypeStats(LIBBASE
, request
);
144 case S2_GETGLOBALSTATS
:
145 complete
= CmdGetGlobalStats(LIBBASE
, request
);
149 complete
= CmdOnEvent(LIBBASE
, request
);
153 complete
= CmdReadOrphan(LIBBASE
, request
);
157 complete
= CmdOnline(LIBBASE
, request
);
161 complete
= CmdOffline(LIBBASE
, request
);
164 case S2_ADDMULTICASTADDRESS
:
165 case S2_ADDMULTICASTADDRESSES
:
166 complete
= CmdAddMulticastAddresses(LIBBASE
, request
);
169 case S2_DELMULTICASTADDRESS
:
170 case S2_DELMULTICASTADDRESSES
:
171 complete
= CmdDelMulticastAddresses(LIBBASE
, request
);
174 case NSCMD_DEVICEQUERY
:
175 complete
= CmdDeviceQuery(LIBBASE
, (struct IOStdReq
*)request
);
179 complete
= CmdInvalid(LIBBASE
, request
);
182 if(complete
&& (request
->ios2_Req
.io_Flags
& IOF_QUICK
) == 0)
183 ReplyMsg((APTR
)request
);
185 ReleaseSemaphore(&((struct VIARHINEUnit
*)request
->ios2_Req
.io_Unit
)->rhineu_unit_lock
);
188 static BOOL
CmdInvalid(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
190 request
->ios2_Req
.io_Error
= IOERR_NOCMD
;
191 request
->ios2_WireError
= S2WERR_GENERIC_ERROR
;
196 static BOOL
CmdRead(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
198 struct VIARHINEUnit
*unit
;
199 struct Opener
*opener
;
200 BOOL complete
= FALSE
;
202 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
204 D(bug("%s: S2CmdRead()\n", unit
->rhineu_name
));
206 if((unit
->rhineu_flags
& IFF_UP
) != 0)
208 opener
= request
->ios2_BufferManagement
;
209 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
210 PutMsg(&opener
->read_port
, (struct Message
*)request
);
214 request
->ios2_Req
.io_Error
= S2ERR_OUTOFSERVICE
;
215 request
->ios2_WireError
= S2WERR_UNIT_OFFLINE
;
224 static BOOL
CmdWrite(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
226 struct VIARHINEUnit
*unit
;
228 ULONG wire_error
= S2WERR_GENERIC_ERROR
;
229 BOOL complete
= FALSE
;
231 /* Check request is valid */
233 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
235 D(bug("%s: S2CmdWrite()\n", unit
->rhineu_name
));
237 if((unit
->rhineu_flags
& IFF_UP
) == 0)
239 error
= S2ERR_OUTOFSERVICE
;
240 wire_error
= S2WERR_UNIT_OFFLINE
;
242 else if((request
->ios2_Req
.io_Command
== S2_MULTICAST
) &&
243 ((request
->ios2_DstAddr
[0] & 0x1) == 0))
245 error
= S2ERR_BAD_ADDRESS
;
246 wire_error
= S2WERR_BAD_MULTICAST
;
249 /* Queue request for sending */
252 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
253 PutMsg(unit
->rhineu_request_ports
[WRITE_QUEUE
], (APTR
)request
);
257 request
->ios2_Req
.io_Error
= error
;
258 request
->ios2_WireError
= wire_error
;
267 static BOOL
CmdFlush(LIBBASETYPEPTR LIBBASE
, struct IORequest
*request
)
269 FlushUnit(LIBBASE
, (APTR
)request
->io_Unit
, EVENT_QUEUE
, IOERR_ABORTED
);
273 static BOOL
CmdS2DeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
275 D(struct VIARHINEUnit
*unit
= (APTR
)request
->ios2_Req
.io_Unit
);
276 // struct fe_priv *np = unit->rhineu_fe_priv;
277 struct Sana2DeviceQuery
*info
;
278 ULONG size_available
, size
;
280 D(bug("%s: S2CmdDeviceQuery()\n", unit
->rhineu_name
));
282 /* Copy device info */
284 info
= request
->ios2_StatData
;
285 size
= size_available
= info
->SizeAvailable
;
286 if(size
> sizeof(struct Sana2DeviceQuery
))
287 size
= sizeof(struct Sana2DeviceQuery
);
289 CopyMem(&LIBBASE
->rhineb_Sana2Info
, info
, size
);
291 info
->BPS
= 100000000;
293 info
->HardwareType
= S2WireType_Ethernet
;
294 info
->SizeAvailable
= size_available
;
295 info
->SizeSupplied
= size
;
302 static BOOL
CmdGetStationAddress(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
304 struct VIARHINEUnit
*unit
;
308 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
310 D(bug("%s: S2CmdGetStationAddress()\n", unit
->rhineu_name
));
312 CopyMem(unit
->rhineu_dev_addr
, request
->ios2_SrcAddr
, ETH_ADDRESSSIZE
);
313 CopyMem(unit
->rhineu_org_addr
, request
->ios2_DstAddr
, ETH_ADDRESSSIZE
);
320 static BOOL
CmdConfigInterface(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
322 struct VIARHINEUnit
*unit
;
324 /* Configure adapter */
326 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
328 D(bug("%s: S2CmdConfigInterface()\n", unit
->rhineu_name
));
330 if((unit
->rhineu_flags
& IFF_CONFIGURED
) == 0)
332 CopyMem(request
->ios2_SrcAddr
, unit
->rhineu_dev_addr
, ETH_ADDRESSSIZE
);
333 unit
->set_mac_address(unit
);
334 unit
->rhineu_flags
|= IFF_CONFIGURED
;
338 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
339 request
->ios2_WireError
= S2WERR_IS_CONFIGURED
;
347 static BOOL
CmdBroadcast(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
349 /* Fill in the broadcast address as destination */
351 *((ULONG
*)request
->ios2_DstAddr
) = 0xffffffff;
352 *((UWORD
*)(request
->ios2_DstAddr
+ 4)) = 0xffff;
354 /* Queue the write as normal */
356 return CmdWrite(LIBBASE
, request
);
359 static BOOL
CmdTrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
361 struct VIARHINEUnit
*unit
;
362 struct Opener
*opener
;
363 ULONG packet_type
, wire_error
=0;
364 struct TypeTracker
*tracker
;
365 struct TypeStats
*initial_stats
;
368 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
370 D(bug("%s: S2CmdTrackType()\n", unit
->rhineu_name
));
372 packet_type
= request
->ios2_PacketType
;
374 /* Get global tracker */
376 tracker
= (struct TypeTracker
*)
377 FindTypeStats(LIBBASE
, unit
, &unit
->rhineu_type_trackers
, packet_type
);
380 tracker
->user_count
++;
384 AllocMem(sizeof(struct TypeTracker
), MEMF_PUBLIC
|MEMF_CLEAR
);
387 tracker
->packet_type
= packet_type
;
388 tracker
->user_count
= 1;
391 AddTail((APTR
)&unit
->rhineu_type_trackers
, (APTR
)tracker
);
396 /* Store initial figures for this opener */
398 opener
= request
->ios2_BufferManagement
;
399 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
401 if(initial_stats
!= NULL
)
403 error
= S2ERR_BAD_STATE
;
404 wire_error
= S2WERR_ALREADY_TRACKED
;
409 initial_stats
= AllocMem(sizeof(struct TypeStats
), MEMF_PUBLIC
);
410 if(initial_stats
== NULL
)
412 error
= S2ERR_NO_RESOURCES
;
413 wire_error
= S2WERR_GENERIC_ERROR
;
419 CopyMem(tracker
, initial_stats
, sizeof(struct TypeStats
));
420 AddTail((APTR
)&opener
->initial_stats
, (APTR
)initial_stats
);
425 request
->ios2_Req
.io_Error
= error
;
426 request
->ios2_WireError
= wire_error
;
430 static BOOL
CmdUntrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
432 struct VIARHINEUnit
*unit
;
433 struct Opener
*opener
;
435 struct TypeTracker
*tracker
;
436 struct TypeStats
*initial_stats
;
438 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
440 D(bug("%s: S2CmdUntrackType()\n", unit
->rhineu_name
));
442 packet_type
= request
->ios2_PacketType
;
444 /* Get global tracker and initial figures */
446 tracker
= (struct TypeTracker
*)
447 FindTypeStats(LIBBASE
, unit
, &unit
->rhineu_type_trackers
, packet_type
);
448 opener
= request
->ios2_BufferManagement
;
449 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
451 /* Decrement tracker usage and free unused structures */
453 if(initial_stats
!= NULL
)
455 if((--tracker
->user_count
) == 0)
458 Remove((APTR
)tracker
);
460 FreeMem(tracker
, sizeof(struct TypeTracker
));
463 Remove((APTR
)initial_stats
);
464 FreeMem(initial_stats
, sizeof(struct TypeStats
));
468 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
469 request
->ios2_WireError
= S2WERR_NOT_TRACKED
;
477 static BOOL
CmdGetTypeStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
479 struct VIARHINEUnit
*unit
;
480 struct Opener
*opener
;
482 struct TypeStats
*initial_stats
, *tracker
;
483 struct Sana2PacketTypeStats
*stats
;
485 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
487 D(bug("%s: S2CmdGetTypeStats()\n", unit
->rhineu_name
));
489 packet_type
= request
->ios2_PacketType
;
491 /* Get global tracker and initial figures */
493 tracker
= FindTypeStats(LIBBASE
, unit
, &unit
->rhineu_type_trackers
, packet_type
);
494 opener
= request
->ios2_BufferManagement
;
495 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
497 /* Copy and adjust figures */
498 if(initial_stats
!= NULL
)
500 stats
= request
->ios2_StatData
;
501 CopyMem(&tracker
->stats
, stats
, sizeof(struct Sana2PacketTypeStats
));
502 stats
->PacketsSent
-= initial_stats
->stats
.PacketsSent
;
503 stats
->PacketsReceived
-= initial_stats
->stats
.PacketsReceived
;
504 stats
->BytesSent
-= initial_stats
->stats
.BytesSent
;
505 stats
->BytesReceived
-= initial_stats
->stats
.BytesReceived
;
506 stats
->PacketsDropped
-= initial_stats
->stats
.PacketsDropped
;
510 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
511 request
->ios2_WireError
= S2WERR_NOT_TRACKED
;
519 static BOOL
CmdGetGlobalStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
521 struct VIARHINEUnit
*unit
;
523 /* Update and copy stats */
525 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
527 D(bug("%s: S2CmdGetGlobalStats()\n", unit
->rhineu_name
));
529 CopyMem(&unit
->rhineu_stats
, request
->ios2_StatData
,
530 sizeof(struct Sana2DeviceStats
));
537 static BOOL
CmdDeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOStdReq
*request
)
539 struct NSDeviceQueryResult
*info
;
541 /* Set structure size twice */
543 info
= request
->io_Data
;
544 request
->io_Actual
= info
->SizeAvailable
=
545 offsetof(struct NSDeviceQueryResult
, SupportedCommands
) + sizeof(APTR
);
547 /* Report device details */
549 info
->DeviceType
= NSDEVTYPE_SANA2
;
550 info
->DeviceSubType
= 0;
552 info
->SupportedCommands
= (APTR
)supported_commands
;
559 static BOOL
CmdOnEvent(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
561 struct VIARHINEUnit
*unit
;
562 ULONG events
, wanted_events
;
563 BOOL complete
= FALSE
;
565 /* Check if we understand the event types */
567 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
569 D(bug("%s: S2CmdOnEvent()\n", unit
->rhineu_name
));
571 wanted_events
= request
->ios2_WireError
;
572 if((wanted_events
& ~KNOWN_EVENTS
) != 0)
574 request
->ios2_Req
.io_Error
= S2ERR_NOT_SUPPORTED
;
575 events
= S2WERR_BAD_EVENT
;
579 if((unit
->rhineu_flags
& IFF_UP
) != 0)
580 events
= S2EVENT_ONLINE
;
582 events
= S2EVENT_OFFLINE
;
584 events
&= wanted_events
;
587 /* Reply request if a wanted event has already occurred */
591 request
->ios2_WireError
= events
;
596 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
597 PutMsg(unit
->rhineu_request_ports
[EVENT_QUEUE
], (APTR
)request
);
605 static BOOL
CmdReadOrphan(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
607 struct VIARHINEUnit
*unit
;
610 BOOL complete
= FALSE
;
612 /* Check request is valid */
614 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
615 D(bug("%s: S2CmdReadOrphan()\n", unit
->rhineu_name
));
617 if((unit
->rhineu_flags
& IFF_UP
) == 0)
619 error
= S2ERR_OUTOFSERVICE
;
620 wire_error
= S2WERR_UNIT_OFFLINE
;
627 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
628 PutMsg(unit
->rhineu_request_ports
[ADOPT_QUEUE
], (struct Message
*)request
);
632 request
->ios2_Req
.io_Error
= error
;
633 request
->ios2_WireError
= wire_error
;
642 static BOOL
CmdOnline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
644 struct VIARHINEUnit
*unit
= (struct VIARHINEUnit
*)request
->ios2_Req
.io_Unit
;
646 ULONG wire_error
= 0;
649 D(bug("%s: S2CmdOnline()\n", unit
->rhineu_name
));
651 /* Check request is valid */
652 if((unit
->rhineu_flags
& IFF_CONFIGURED
) == 0)
654 error
= S2ERR_BAD_STATE
;
655 wire_error
= S2WERR_NOT_CONFIGURED
;
658 /* Clear global and special stats and put adapter back online */
660 if((error
== 0) && ((unit
->rhineu_flags
& IFF_UP
) == 0))
662 unit
->rhineu_stats
.PacketsReceived
= 0;
663 unit
->rhineu_stats
.PacketsSent
= 0;
664 unit
->rhineu_stats
.BadData
= 0;
665 unit
->rhineu_stats
.Overruns
= 0;
666 unit
->rhineu_stats
.UnknownTypesReceived
= 0;
667 unit
->rhineu_stats
.Reconfigurations
= 0;
669 for(i
= 0; i
< STAT_COUNT
; i
++)
670 unit
->rhineu_special_stats
[i
] = 0;
672 if (unit
->start(unit
))
674 error
= S2ERR_OUTOFSERVICE
;
675 wire_error
= S2WERR_GENERIC_ERROR
;
681 request
->ios2_Req
.io_Error
= error
;
682 request
->ios2_WireError
= wire_error
;
686 static BOOL
CmdOffline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
688 struct VIARHINEUnit
*unit
;
690 /* Put adapter offline */
692 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
694 D(bug("%s: S2CmdOffline()\n", unit
->rhineu_name
));
696 if((unit
->rhineu_flags
& IFF_UP
) != 0)
703 static BOOL
CmdAddMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
705 struct VIARHINEUnit
*unit
;
706 UBYTE
*lower_bound
, *upper_bound
;
708 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
710 D(bug("%s: S2CmdAddMulticastAddresses()\n", unit
->rhineu_name
));
712 lower_bound
= request
->ios2_SrcAddr
;
713 if(request
->ios2_Req
.io_Command
== S2_ADDMULTICASTADDRESS
)
714 upper_bound
= lower_bound
;
716 upper_bound
= request
->ios2_DstAddr
;
718 if(!AddMulticastRange(LIBBASE
, unit
, lower_bound
, upper_bound
))
720 request
->ios2_Req
.io_Error
= S2ERR_NO_RESOURCES
;
721 request
->ios2_WireError
= S2WERR_GENERIC_ERROR
;
729 static BOOL
CmdDelMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
731 struct VIARHINEUnit
*unit
;
732 UBYTE
*lower_bound
, *upper_bound
;
734 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
736 D(bug("%s: S2CmdDelMulticastAddresses()\n", unit
->rhineu_name
));
738 lower_bound
= request
->ios2_SrcAddr
;
739 if(request
->ios2_Req
.io_Command
== S2_DELMULTICASTADDRESS
)
740 upper_bound
= lower_bound
;
742 upper_bound
= request
->ios2_DstAddr
;
744 if(!RemMulticastRange(LIBBASE
, unit
, lower_bound
, upper_bound
))
746 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
747 request
->ios2_WireError
= S2WERR_BAD_MULTICAST
;