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 SiS900Unit
*)request
->ios2_Req
.io_Unit
)->sis900u_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 SiS900Unit
*unit
;
201 struct Opener
*opener
;
202 BOOL complete
= FALSE
;
204 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
206 D(bug("[%s]: S2CmdRead()\n", unit
->sis900u_name
));
208 if((unit
->sis900u_ifflags
& 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 SiS900Unit
*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 D(bug("[%s]: S2CmdWrite()\n", unit
->sis900u_name
));
239 if((unit
->sis900u_ifflags
& 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
->sis900u_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 SiS900Unit
*unit
= (APTR
)request
->ios2_Req
.io_Unit
;
278 // struct fe_priv *np = unit->sis900u_fe_priv;
279 struct Sana2DeviceQuery
*info
;
280 ULONG size_available
, size
, status
;
283 D(bug("[%s]: S2CmdDeviceQuery()\n", unit
->sis900u_name
));
285 /* STSOUT register is Latched on Transition, read operation updates it */
287 status
= mdio_read(unit
, unit
->cur_phy
, MII_STSOUT
);
289 D(bug("[%s]: S2CmdDeviceQuery: PHY status %x\n", unit
->sis900u_name
, status
));
291 /* Copy device info */
292 info
= request
->ios2_StatData
;
293 size
= size_available
= info
->SizeAvailable
;
294 if(size
> sizeof(struct Sana2DeviceQuery
))
295 size
= sizeof(struct Sana2DeviceQuery
);
297 CopyMem(&unit
->sis900u_Sana2Info
, info
, size
);
299 if (status
& MII_STSOUT_SPD
)
300 info
->BPS
= 100000000;
302 info
->BPS
= 10000000;
304 info
->MTU
= unit
->sis900u_mtu
;
305 info
->HardwareType
= S2WireType_Ethernet
;
306 info
->SizeAvailable
= size_available
;
307 info
->SizeSupplied
= size
;
314 static BOOL
CmdGetStationAddress(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
316 struct SiS900Unit
*unit
;
320 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
322 D(bug("[%s]: S2CmdGetStationAddress()\n", unit
->sis900u_name
));
324 CopyMem(unit
->sis900u_dev_addr
, request
->ios2_SrcAddr
, ETH_ADDRESSSIZE
);
325 CopyMem(unit
->sis900u_org_addr
, request
->ios2_DstAddr
, ETH_ADDRESSSIZE
);
332 static BOOL
CmdConfigInterface(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
334 struct SiS900Unit
*unit
;
336 /* Configure adapter */
338 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
340 D(bug("[%s]: S2CmdConfigInterface()\n", unit
->sis900u_name
));
342 if((unit
->sis900u_ifflags
& IFF_CONFIGURED
) == 0)
344 CopyMem(request
->ios2_SrcAddr
, unit
->sis900u_dev_addr
, ETH_ADDRESSSIZE
);
345 sis900func_set_mac(unit
);
346 unit
->sis900u_ifflags
|= IFF_CONFIGURED
;
350 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
351 request
->ios2_WireError
= S2WERR_IS_CONFIGURED
;
359 static BOOL
CmdBroadcast(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
361 /* Fill in the broadcast address as destination */
363 memset(request
->ios2_DstAddr
, 0xff, 6);
365 /* Queue the write as normal */
367 return CmdWrite(LIBBASE
, request
);
370 static BOOL
CmdTrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
372 struct SiS900Unit
*unit
;
373 struct Opener
*opener
;
374 ULONG packet_type
, wire_error
=0;
375 struct TypeTracker
*tracker
;
376 struct TypeStats
*initial_stats
;
379 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
381 D(bug("[%s]: S2CmdTrackType()\n", unit
->sis900u_name
));
383 packet_type
= request
->ios2_PacketType
;
385 /* Get global tracker */
387 tracker
= (struct TypeTracker
*)
388 FindTypeStats(LIBBASE
, unit
, &unit
->sis900u_type_trackers
, packet_type
);
391 tracker
->user_count
++;
395 AllocMem(sizeof(struct TypeTracker
), MEMF_PUBLIC
|MEMF_CLEAR
);
398 tracker
->packet_type
= packet_type
;
399 tracker
->user_count
= 1;
402 AddTail((APTR
)&unit
->sis900u_type_trackers
, (APTR
)tracker
);
407 /* Store initial figures for this opener */
409 opener
= request
->ios2_BufferManagement
;
410 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
412 if(initial_stats
!= NULL
)
414 error
= S2ERR_BAD_STATE
;
415 wire_error
= S2WERR_ALREADY_TRACKED
;
420 initial_stats
= AllocMem(sizeof(struct TypeStats
), MEMF_PUBLIC
);
421 if(initial_stats
== NULL
)
423 error
= S2ERR_NO_RESOURCES
;
424 wire_error
= S2WERR_GENERIC_ERROR
;
430 CopyMem(tracker
, initial_stats
, sizeof(struct TypeStats
));
431 AddTail((APTR
)&opener
->initial_stats
, (APTR
)initial_stats
);
436 request
->ios2_Req
.io_Error
= error
;
437 request
->ios2_WireError
= wire_error
;
441 static BOOL
CmdUntrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
443 struct SiS900Unit
*unit
;
444 struct Opener
*opener
;
446 struct TypeTracker
*tracker
;
447 struct TypeStats
*initial_stats
;
449 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
451 D(bug("[%s]: S2CmdUntrackType()\n", unit
->sis900u_name
));
453 packet_type
= request
->ios2_PacketType
;
455 /* Get global tracker and initial figures */
457 tracker
= (struct TypeTracker
*)
458 FindTypeStats(LIBBASE
, unit
, &unit
->sis900u_type_trackers
, packet_type
);
459 opener
= request
->ios2_BufferManagement
;
460 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
462 /* Decrement tracker usage and free unused structures */
464 if(initial_stats
!= NULL
)
466 if((--tracker
->user_count
) == 0)
469 Remove((APTR
)tracker
);
471 FreeMem(tracker
, sizeof(struct TypeTracker
));
474 Remove((APTR
)initial_stats
);
475 FreeMem(initial_stats
, sizeof(struct TypeStats
));
479 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
480 request
->ios2_WireError
= S2WERR_NOT_TRACKED
;
488 static BOOL
CmdGetTypeStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
490 struct SiS900Unit
*unit
;
491 struct Opener
*opener
;
493 struct TypeStats
*initial_stats
, *tracker
;
494 struct Sana2PacketTypeStats
*stats
;
496 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
498 D(bug("[%s]: S2CmdGetTypeStats()\n", unit
->sis900u_name
));
500 packet_type
= request
->ios2_PacketType
;
502 /* Get global tracker and initial figures */
504 tracker
= FindTypeStats(LIBBASE
, unit
, &unit
->sis900u_type_trackers
, packet_type
);
505 opener
= request
->ios2_BufferManagement
;
506 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
508 /* Copy and adjust figures */
509 if(initial_stats
!= NULL
)
511 stats
= request
->ios2_StatData
;
512 CopyMem(&tracker
->stats
, stats
, sizeof(struct Sana2PacketTypeStats
));
513 stats
->PacketsSent
-= initial_stats
->stats
.PacketsSent
;
514 stats
->PacketsReceived
-= initial_stats
->stats
.PacketsReceived
;
515 stats
->BytesSent
-= initial_stats
->stats
.BytesSent
;
516 stats
->BytesReceived
-= initial_stats
->stats
.BytesReceived
;
517 stats
->PacketsDropped
-= initial_stats
->stats
.PacketsDropped
;
521 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
522 request
->ios2_WireError
= S2WERR_NOT_TRACKED
;
530 static BOOL
CmdGetGlobalStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
532 struct SiS900Unit
*unit
;
534 /* Update and copy stats */
536 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
538 D(bug("[%s]: S2CmdGetGlobalStats()\n", unit
->sis900u_name
));
540 CopyMem(&unit
->sis900u_stats
, request
->ios2_StatData
,
541 sizeof(struct Sana2DeviceStats
));
548 static BOOL
CmdDeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOStdReq
*request
)
550 struct NSDeviceQueryResult
*info
;
552 /* Set structure size twice */
554 info
= request
->io_Data
;
555 request
->io_Actual
= info
->SizeAvailable
=
556 offsetof(struct NSDeviceQueryResult
, SupportedCommands
) + sizeof(APTR
);
558 /* Report device details */
560 info
->DeviceType
= NSDEVTYPE_SANA2
;
561 info
->DeviceSubType
= 0;
563 info
->SupportedCommands
= (APTR
)supported_commands
;
570 static BOOL
CmdOnEvent(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
572 struct SiS900Unit
*unit
;
573 ULONG events
, wanted_events
;
574 BOOL complete
= FALSE
;
576 /* Check if we understand the event types */
578 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
580 D(bug("[%s]: S2CmdOnEvent()\n", unit
->sis900u_name
));
582 wanted_events
= request
->ios2_WireError
;
583 if((wanted_events
& ~KNOWN_EVENTS
) != 0)
585 request
->ios2_Req
.io_Error
= S2ERR_NOT_SUPPORTED
;
586 events
= S2WERR_BAD_EVENT
;
590 if((unit
->sis900u_ifflags
& IFF_UP
) != 0)
591 events
= S2EVENT_ONLINE
;
593 events
= S2EVENT_OFFLINE
;
595 events
&= wanted_events
;
598 /* Reply request if a wanted event has already occurred */
602 request
->ios2_WireError
= events
;
607 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
608 PutMsg(unit
->sis900u_request_ports
[EVENT_QUEUE
], (APTR
)request
);
616 static BOOL
CmdReadOrphan(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
618 struct SiS900Unit
*unit
;
621 BOOL complete
= FALSE
;
623 /* Check request is valid */
625 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
626 D(bug("[%s]: S2CmdReadOrphan()\n", unit
->sis900u_name
));
628 if((unit
->sis900u_ifflags
& IFF_UP
) == 0)
630 error
= S2ERR_OUTOFSERVICE
;
631 wire_error
= S2WERR_UNIT_OFFLINE
;
638 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
639 PutMsg(unit
->sis900u_request_ports
[ADOPT_QUEUE
], (struct Message
*)request
);
643 request
->ios2_Req
.io_Error
= error
;
644 request
->ios2_WireError
= wire_error
;
653 static BOOL
CmdOnline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
655 struct SiS900Unit
*unit
= (struct SiS900Unit
*)request
->ios2_Req
.io_Unit
;
657 ULONG wire_error
= 0;
660 D(bug("[%s]: S2CmdOnline()\n", unit
->sis900u_name
));
662 /* Check request is valid */
663 if((unit
->sis900u_ifflags
& IFF_CONFIGURED
) == 0)
665 error
= S2ERR_BAD_STATE
;
666 wire_error
= S2WERR_NOT_CONFIGURED
;
669 /* Clear global and special stats and put adapter back online */
671 if((error
== 0) && ((unit
->sis900u_ifflags
& IFF_UP
) == 0))
673 unit
->sis900u_stats
.PacketsReceived
= 0;
674 unit
->sis900u_stats
.PacketsSent
= 0;
675 unit
->sis900u_stats
.BadData
= 0;
676 unit
->sis900u_stats
.Overruns
= 0;
677 unit
->sis900u_stats
.UnknownTypesReceived
= 0;
678 unit
->sis900u_stats
.Reconfigurations
= 0;
680 for(i
= 0; i
< SANA2_SPECIAL_STAT_COUNT
; i
++)
681 unit
->sis900u_special_stats
[i
] = 0;
683 if (sis900func_open(unit
))
685 error
= S2ERR_OUTOFSERVICE
;
686 wire_error
= S2WERR_GENERIC_ERROR
;
692 request
->ios2_Req
.io_Error
= error
;
693 request
->ios2_WireError
= wire_error
;
697 static BOOL
CmdOffline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
699 struct SiS900Unit
*unit
;
701 /* Put adapter offline */
703 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
705 D(bug("[%s]: S2CmdOffline()\n", unit
->sis900u_name
));
707 if((unit
->sis900u_ifflags
& IFF_UP
) != 0)
708 sis900func_close(unit
);
714 static BOOL
CmdAddMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
716 struct SiS900Unit
*unit
;
717 UBYTE
*lower_bound
, *upper_bound
;
719 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
721 D(bug("[%s]: S2CmdAddMulticastAddresses()\n", unit
->sis900u_name
));
723 lower_bound
= request
->ios2_SrcAddr
;
724 if(request
->ios2_Req
.io_Command
== S2_ADDMULTICASTADDRESS
)
725 upper_bound
= lower_bound
;
727 upper_bound
= request
->ios2_DstAddr
;
729 if(!AddMulticastRange(LIBBASE
, unit
, lower_bound
, upper_bound
))
731 request
->ios2_Req
.io_Error
= S2ERR_NO_RESOURCES
;
732 request
->ios2_WireError
= S2WERR_GENERIC_ERROR
;
740 static BOOL
CmdDelMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
742 struct SiS900Unit
*unit
;
743 UBYTE
*lower_bound
, *upper_bound
;
745 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
747 D(bug("[%s]: S2CmdDelMulticastAddresses()\n", unit
->sis900u_name
));
749 lower_bound
= request
->ios2_SrcAddr
;
750 if(request
->ios2_Req
.io_Command
== S2_DELMULTICASTADDRESS
)
751 upper_bound
= lower_bound
;
753 upper_bound
= request
->ios2_DstAddr
;
755 if(!RemMulticastRange(LIBBASE
, unit
, lower_bound
, upper_bound
))
757 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
758 request
->ios2_WireError
= S2WERR_BAD_MULTICAST
;