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,
24 #include <exec/types.h>
25 #include <exec/resident.h>
27 #include <exec/ports.h>
28 #include <exec/errors.h>
30 #include <devices/sana2.h>
31 #include <devices/sana2specialstats.h>
32 #include <devices/newstyle.h>
34 #include <utility/utility.h>
35 #include <utility/tagitem.h>
36 #include <utility/hooks.h>
38 #include <proto/exec.h>
39 #include <proto/dos.h>
40 #include <proto/battclock.h>
46 #include LC_LIBDEFS_FILE
48 #define KNOWN_EVENTS \
49 (S2EVENT_ERROR | S2EVENT_TX | S2EVENT_RX | S2EVENT_ONLINE \
50 | S2EVENT_OFFLINE | S2EVENT_BUFF | S2EVENT_HARDWARE | S2EVENT_SOFTWARE)
52 static BOOL
CmdInvalid(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
53 static BOOL
CmdRead(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
54 static BOOL
CmdWrite(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
55 static BOOL
CmdFlush(LIBBASETYPEPTR LIBBASE
, struct IORequest
*request
);
56 static BOOL
CmdS2DeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
57 static BOOL
CmdGetStationAddress(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
58 static BOOL
CmdConfigInterface(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
59 static BOOL
CmdBroadcast(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
60 static BOOL
CmdTrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
61 static BOOL
CmdUntrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
62 static BOOL
CmdGetTypeStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
63 static BOOL
CmdGetGlobalStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
64 static BOOL
CmdDeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOStdReq
*request
);
65 static BOOL
CmdOnEvent(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
66 static BOOL
CmdReadOrphan(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
67 static BOOL
CmdOnline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
68 static BOOL
CmdOffline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
69 static BOOL
CmdAddMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
70 static BOOL
CmdDelMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
72 static const UWORD supported_commands
[] =
80 S2_ADDMULTICASTADDRESS
,
81 S2_DELMULTICASTADDRESS
,
87 // S2_GETSPECIALSTATS,
94 S2_ADDMULTICASTADDRESSES
,
95 S2_DELMULTICASTADDRESSES
,
99 void handle_request(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
103 switch(request
->ios2_Req
.io_Command
)
106 complete
= CmdRead(LIBBASE
, request
);
111 complete
= CmdWrite(LIBBASE
, request
);
115 complete
= CmdFlush(LIBBASE
, (struct IORequest
*)request
);
119 complete
= CmdS2DeviceQuery(LIBBASE
, request
);
122 case S2_GETSTATIONADDRESS
:
123 complete
= CmdGetStationAddress(LIBBASE
, request
);
126 case S2_CONFIGINTERFACE
:
127 complete
= CmdConfigInterface(LIBBASE
, request
);
131 complete
= CmdBroadcast(LIBBASE
, request
);
135 complete
= CmdTrackType(LIBBASE
, request
);
139 complete
= CmdUntrackType(LIBBASE
, request
);
142 case S2_GETTYPESTATS
:
143 complete
= CmdGetTypeStats(LIBBASE
, request
);
146 case S2_GETGLOBALSTATS
:
147 complete
= CmdGetGlobalStats(LIBBASE
, request
);
151 complete
= CmdOnEvent(LIBBASE
, request
);
155 complete
= CmdReadOrphan(LIBBASE
, request
);
159 complete
= CmdOnline(LIBBASE
, request
);
163 complete
= CmdOffline(LIBBASE
, request
);
166 case S2_ADDMULTICASTADDRESS
:
167 case S2_ADDMULTICASTADDRESSES
:
168 complete
= CmdAddMulticastAddresses(LIBBASE
, request
);
171 case S2_DELMULTICASTADDRESS
:
172 case S2_DELMULTICASTADDRESSES
:
173 complete
= CmdDelMulticastAddresses(LIBBASE
, request
);
176 case NSCMD_DEVICEQUERY
:
177 complete
= CmdDeviceQuery(LIBBASE
, (struct IOStdReq
*)request
);
181 complete
= CmdInvalid(LIBBASE
, request
);
184 if(complete
&& (request
->ios2_Req
.io_Flags
& IOF_QUICK
) == 0)
185 ReplyMsg((APTR
)request
);
187 ReleaseSemaphore(&((struct RTL8168Unit
*)request
->ios2_Req
.io_Unit
)->rtl8168u_unit_lock
);
190 static BOOL
CmdInvalid(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
192 request
->ios2_Req
.io_Error
= IOERR_NOCMD
;
193 request
->ios2_WireError
= S2WERR_GENERIC_ERROR
;
198 static BOOL
CmdRead(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
200 struct RTL8168Unit
*unit
;
201 struct Opener
*opener
;
202 BOOL complete
= FALSE
;
204 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
206 RTLD(bug("[%s] S2CmdRead()\n", unit
->rtl8168u_name
))
208 if((unit
->rtl8168u_flags
& IFF_UP
) != 0)
210 opener
= request
->ios2_BufferManagement
;
211 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
212 PutMsg(&opener
->read_port
, (struct Message
*)request
);
216 request
->ios2_Req
.io_Error
= S2ERR_OUTOFSERVICE
;
217 request
->ios2_WireError
= S2WERR_UNIT_OFFLINE
;
226 static BOOL
CmdWrite(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
228 struct RTL8168Unit
*unit
;
230 ULONG wire_error
= S2WERR_GENERIC_ERROR
;
231 BOOL complete
= FALSE
;
233 /* Check request is valid */
235 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
237 RTLD(bug("[%s] S2CmdWrite()\n", unit
->rtl8168u_name
))
239 if((unit
->rtl8168u_flags
& IFF_UP
) == 0)
241 error
= S2ERR_OUTOFSERVICE
;
242 wire_error
= S2WERR_UNIT_OFFLINE
;
244 else if((request
->ios2_Req
.io_Command
== S2_MULTICAST
) &&
245 ((request
->ios2_DstAddr
[0] & 0x1) == 0))
247 error
= S2ERR_BAD_ADDRESS
;
248 wire_error
= S2WERR_BAD_MULTICAST
;
251 /* Queue request for sending */
254 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
255 PutMsg(unit
->rtl8168u_request_ports
[WRITE_QUEUE
], (APTR
)request
);
259 request
->ios2_Req
.io_Error
= error
;
260 request
->ios2_WireError
= wire_error
;
269 static BOOL
CmdFlush(LIBBASETYPEPTR LIBBASE
, struct IORequest
*request
)
271 FlushUnit(LIBBASE
, (APTR
)request
->io_Unit
, EVENT_QUEUE
, IOERR_ABORTED
);
275 static BOOL
CmdS2DeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
277 struct RTL8168Unit
*unit
= (APTR
)request
->ios2_Req
.io_Unit
;
278 // struct rtl8168_priv *np = unit->rtl8168u_priv;
279 struct Sana2DeviceQuery
*info
;
280 ULONG size_available
, size
;
282 RTLD(bug("[%s] S2CmdDeviceQuery()\n", unit
->rtl8168u_name
))
284 /* Copy device info */
286 info
= request
->ios2_StatData
;
287 size
= size_available
= info
->SizeAvailable
;
288 if(size
> sizeof(struct Sana2DeviceQuery
))
289 size
= sizeof(struct Sana2DeviceQuery
);
291 CopyMem(&unit
->rtl8168u_Sana2Info
, info
, size
);
293 info
->BPS
= unit
->rtl8168u_rtl_LinkSpeed
;
295 info
->HardwareType
= S2WireType_Ethernet
;
296 info
->SizeAvailable
= size_available
;
297 info
->SizeSupplied
= size
;
304 static BOOL
CmdGetStationAddress(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
306 struct RTL8168Unit
*unit
;
310 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
312 RTLD(bug("[%s] S2CmdGetStationAddress()\n", unit
->rtl8168u_name
))
314 CopyMem(unit
->rtl8168u_dev_addr
, request
->ios2_SrcAddr
, ETH_ADDRESSSIZE
);
315 CopyMem(unit
->rtl8168u_org_addr
, request
->ios2_DstAddr
, ETH_ADDRESSSIZE
);
322 static BOOL
CmdConfigInterface(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
324 struct RTL8168Unit
*unit
;
326 /* Configure adapter */
328 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
330 RTLD(bug("[%s] S2CmdConfigInterface()\n", unit
->rtl8168u_name
))
332 if((unit
->rtl8168u_flags
& IFF_CONFIGURED
) == 0)
334 CopyMem(request
->ios2_SrcAddr
, unit
->rtl8168u_dev_addr
, ETH_ADDRESSSIZE
);
335 unit
->set_mac_address(unit
);
336 unit
->rtl8168u_flags
|= IFF_CONFIGURED
;
340 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
341 request
->ios2_WireError
= S2WERR_IS_CONFIGURED
;
349 static BOOL
CmdBroadcast(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
351 /* Fill in the broadcast address as destination */
353 memset(request
->ios2_DstAddr
, 0xff, 6);
355 /* Queue the write as normal */
357 return CmdWrite(LIBBASE
, request
);
360 static BOOL
CmdTrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
362 struct RTL8168Unit
*unit
;
363 struct Opener
*opener
;
364 ULONG packet_type
, wire_error
=0;
365 struct TypeTracker
*tracker
;
366 struct TypeStats
*initial_stats
;
369 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
371 RTLD(bug("[%s] S2CmdTrackType()\n", unit
->rtl8168u_name
))
373 packet_type
= request
->ios2_PacketType
;
375 /* Get global tracker */
377 tracker
= (struct TypeTracker
*)
378 FindTypeStats(LIBBASE
, unit
, &unit
->rtl8168u_type_trackers
, packet_type
);
381 tracker
->user_count
++;
385 AllocMem(sizeof(struct TypeTracker
), MEMF_PUBLIC
|MEMF_CLEAR
);
388 tracker
->packet_type
= packet_type
;
389 tracker
->user_count
= 1;
392 AddTail((APTR
)&unit
->rtl8168u_type_trackers
, (APTR
)tracker
);
397 /* Store initial figures for this opener */
399 opener
= request
->ios2_BufferManagement
;
400 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
402 if(initial_stats
!= NULL
)
404 error
= S2ERR_BAD_STATE
;
405 wire_error
= S2WERR_ALREADY_TRACKED
;
410 initial_stats
= AllocMem(sizeof(struct TypeStats
), MEMF_PUBLIC
);
411 if(initial_stats
== NULL
)
413 error
= S2ERR_NO_RESOURCES
;
414 wire_error
= S2WERR_GENERIC_ERROR
;
420 CopyMem(tracker
, initial_stats
, sizeof(struct TypeStats
));
421 AddTail((APTR
)&opener
->initial_stats
, (APTR
)initial_stats
);
426 request
->ios2_Req
.io_Error
= error
;
427 request
->ios2_WireError
= wire_error
;
431 static BOOL
CmdUntrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
433 struct RTL8168Unit
*unit
;
434 struct Opener
*opener
;
436 struct TypeTracker
*tracker
;
437 struct TypeStats
*initial_stats
;
439 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
441 RTLD(bug("[%s] S2CmdUntrackType()\n", unit
->rtl8168u_name
))
443 packet_type
= request
->ios2_PacketType
;
445 /* Get global tracker and initial figures */
447 tracker
= (struct TypeTracker
*)
448 FindTypeStats(LIBBASE
, unit
, &unit
->rtl8168u_type_trackers
, packet_type
);
449 opener
= request
->ios2_BufferManagement
;
450 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
452 /* Decrement tracker usage and free unused structures */
454 if(initial_stats
!= NULL
)
456 if((--tracker
->user_count
) == 0)
459 Remove((APTR
)tracker
);
461 FreeMem(tracker
, sizeof(struct TypeTracker
));
464 Remove((APTR
)initial_stats
);
465 FreeMem(initial_stats
, sizeof(struct TypeStats
));
469 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
470 request
->ios2_WireError
= S2WERR_NOT_TRACKED
;
478 static BOOL
CmdGetTypeStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
480 struct RTL8168Unit
*unit
;
481 struct Opener
*opener
;
483 struct TypeStats
*initial_stats
, *tracker
;
484 struct Sana2PacketTypeStats
*stats
;
486 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
488 RTLD(bug("[%s] S2CmdGetTypeStats()\n", unit
->rtl8168u_name
))
490 packet_type
= request
->ios2_PacketType
;
492 /* Get global tracker and initial figures */
494 tracker
= FindTypeStats(LIBBASE
, unit
, &unit
->rtl8168u_type_trackers
, packet_type
);
495 opener
= request
->ios2_BufferManagement
;
496 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
498 /* Copy and adjust figures */
499 if(initial_stats
!= NULL
)
501 stats
= request
->ios2_StatData
;
502 CopyMem(&tracker
->stats
, stats
, sizeof(struct Sana2PacketTypeStats
));
503 stats
->PacketsSent
-= initial_stats
->stats
.PacketsSent
;
504 stats
->PacketsReceived
-= initial_stats
->stats
.PacketsReceived
;
505 stats
->BytesSent
-= initial_stats
->stats
.BytesSent
;
506 stats
->BytesReceived
-= initial_stats
->stats
.BytesReceived
;
507 stats
->PacketsDropped
-= initial_stats
->stats
.PacketsDropped
;
511 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
512 request
->ios2_WireError
= S2WERR_NOT_TRACKED
;
520 static BOOL
CmdGetGlobalStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
522 struct RTL8168Unit
*unit
;
524 /* Update and copy stats */
526 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
528 RTLD(bug("[%s] S2CmdGetGlobalStats()\n", unit
->rtl8168u_name
))
530 CopyMem(&unit
->rtl8168u_stats
, request
->ios2_StatData
,
531 sizeof(struct Sana2DeviceStats
));
538 static BOOL
CmdDeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOStdReq
*request
)
540 struct NSDeviceQueryResult
*info
;
542 /* Set structure size twice */
544 info
= request
->io_Data
;
545 request
->io_Actual
= info
->SizeAvailable
=
546 offsetof(struct NSDeviceQueryResult
, SupportedCommands
) + sizeof(APTR
);
548 /* Report device details */
550 info
->DeviceType
= NSDEVTYPE_SANA2
;
551 info
->DeviceSubType
= 0;
553 info
->SupportedCommands
= (APTR
)supported_commands
;
560 static BOOL
CmdOnEvent(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
562 struct RTL8168Unit
*unit
;
563 ULONG events
, wanted_events
;
564 BOOL complete
= FALSE
;
566 /* Check if we understand the event types */
568 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
570 RTLD(bug("[%s] S2CmdOnEvent()\n", unit
->rtl8168u_name
))
572 wanted_events
= request
->ios2_WireError
;
573 if((wanted_events
& ~KNOWN_EVENTS
) != 0)
575 request
->ios2_Req
.io_Error
= S2ERR_NOT_SUPPORTED
;
576 events
= S2WERR_BAD_EVENT
;
580 if((unit
->rtl8168u_flags
& IFF_UP
) != 0)
581 events
= S2EVENT_ONLINE
;
583 events
= S2EVENT_OFFLINE
;
585 events
&= wanted_events
;
588 /* Reply request if a wanted event has already occurred */
592 request
->ios2_WireError
= events
;
597 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
598 PutMsg(unit
->rtl8168u_request_ports
[EVENT_QUEUE
], (APTR
)request
);
606 static BOOL
CmdReadOrphan(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
608 struct RTL8168Unit
*unit
;
611 BOOL complete
= FALSE
;
613 /* Check request is valid */
615 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
616 RTLD(bug("[%s] S2CmdReadOrphan()\n", unit
->rtl8168u_name
))
618 if((unit
->rtl8168u_flags
& IFF_UP
) == 0)
620 error
= S2ERR_OUTOFSERVICE
;
621 wire_error
= S2WERR_UNIT_OFFLINE
;
628 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
629 PutMsg(unit
->rtl8168u_request_ports
[ADOPT_QUEUE
], (struct Message
*)request
);
633 request
->ios2_Req
.io_Error
= error
;
634 request
->ios2_WireError
= wire_error
;
643 static BOOL
CmdOnline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
645 struct RTL8168Unit
*unit
= (struct RTL8168Unit
*)request
->ios2_Req
.io_Unit
;
647 ULONG wire_error
= 0;
650 RTLD(bug("[%s] S2CmdOnline()\n", unit
->rtl8168u_name
))
652 /* Check request is valid */
653 if((unit
->rtl8168u_flags
& IFF_CONFIGURED
) == 0)
655 error
= S2ERR_BAD_STATE
;
656 wire_error
= S2WERR_NOT_CONFIGURED
;
659 /* Clear global and special stats and put adapter back online */
661 if((error
== 0) && ((unit
->rtl8168u_flags
& IFF_UP
) == 0))
663 unit
->rtl8168u_stats
.PacketsReceived
= 0;
664 unit
->rtl8168u_stats
.PacketsSent
= 0;
665 unit
->rtl8168u_stats
.BadData
= 0;
666 unit
->rtl8168u_stats
.Overruns
= 0;
667 unit
->rtl8168u_stats
.UnknownTypesReceived
= 0;
668 unit
->rtl8168u_stats
.Reconfigurations
= 0;
670 for(i
= 0; i
< STAT_COUNT
; i
++)
671 unit
->rtl8168u_special_stats
[i
] = 0;
673 if (unit
->start(unit
))
675 error
= S2ERR_OUTOFSERVICE
;
676 wire_error
= S2WERR_GENERIC_ERROR
;
682 request
->ios2_Req
.io_Error
= error
;
683 request
->ios2_WireError
= wire_error
;
687 static BOOL
CmdOffline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
689 struct RTL8168Unit
*unit
;
691 /* Put adapter offline */
693 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
695 RTLD(bug("[%s] S2CmdOffline()\n", unit
->rtl8168u_name
))
697 if((unit
->rtl8168u_flags
& IFF_UP
) != 0)
704 static BOOL
CmdAddMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
706 struct RTL8168Unit
*unit
;
707 UBYTE
*lower_bound
, *upper_bound
;
709 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
711 RTLD(bug("[%s] S2CmdAddMulticastAddresses()\n", unit
->rtl8168u_name
))
713 lower_bound
= request
->ios2_SrcAddr
;
714 if(request
->ios2_Req
.io_Command
== S2_ADDMULTICASTADDRESS
)
715 upper_bound
= lower_bound
;
717 upper_bound
= request
->ios2_DstAddr
;
719 if(!AddMulticastRange(LIBBASE
, unit
, lower_bound
, upper_bound
))
721 request
->ios2_Req
.io_Error
= S2ERR_NO_RESOURCES
;
722 request
->ios2_WireError
= S2WERR_GENERIC_ERROR
;
730 static BOOL
CmdDelMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
732 struct RTL8168Unit
*unit
;
733 UBYTE
*lower_bound
, *upper_bound
;
735 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
737 RTLD(bug("[%s] S2CmdDelMulticastAddresses()\n", unit
->rtl8168u_name
))
739 lower_bound
= request
->ios2_SrcAddr
;
740 if(request
->ios2_Req
.io_Command
== S2_DELMULTICASTADDRESS
)
741 upper_bound
= lower_bound
;
743 upper_bound
= request
->ios2_DstAddr
;
745 if(!RemMulticastRange(LIBBASE
, unit
, lower_bound
, upper_bound
))
747 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
748 request
->ios2_WireError
= S2WERR_BAD_MULTICAST
;