2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful, but
8 WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 #include <aros/debug.h>
21 #include <exec/types.h>
22 #include <exec/resident.h>
24 #include <exec/ports.h>
25 #include <exec/errors.h>
27 #include <devices/sana2.h>
28 #include <devices/sana2specialstats.h>
29 #include <devices/newstyle.h>
31 #include <utility/utility.h>
32 #include <utility/tagitem.h>
33 #include <utility/hooks.h>
35 #include <proto/exec.h>
36 #include <proto/dos.h>
37 #include <proto/battclock.h>
42 #include LC_LIBDEFS_FILE
44 #define KNOWN_EVENTS \
45 (S2EVENT_ERROR | S2EVENT_TX | S2EVENT_RX | S2EVENT_ONLINE \
46 | S2EVENT_OFFLINE | S2EVENT_BUFF | S2EVENT_HARDWARE | S2EVENT_SOFTWARE)
48 static BOOL
CmdInvalid(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
49 static BOOL
CmdRead(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
50 static BOOL
CmdWrite(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
51 static BOOL
CmdFlush(LIBBASETYPEPTR LIBBASE
, struct IORequest
*request
);
52 static BOOL
CmdS2DeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
53 static BOOL
CmdGetStationAddress(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
54 static BOOL
CmdConfigInterface(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
55 static BOOL
CmdBroadcast(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
56 static BOOL
CmdTrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
57 static BOOL
CmdUntrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
58 static BOOL
CmdGetTypeStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
59 static BOOL
CmdGetGlobalStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
60 static BOOL
CmdDeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOStdReq
*request
);
61 static BOOL
CmdOnEvent(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
62 static BOOL
CmdReadOrphan(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
63 static BOOL
CmdOnline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
64 static BOOL
CmdOffline(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
65 static BOOL
CmdAddMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
66 static BOOL
CmdDelMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
);
68 static const UWORD supported_commands
[] =
76 S2_ADDMULTICASTADDRESS
,
77 S2_DELMULTICASTADDRESS
,
83 // S2_GETSPECIALSTATS,
90 S2_ADDMULTICASTADDRESSES
,
91 S2_DELMULTICASTADDRESSES
,
95 void handle_request(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
99 switch(request
->ios2_Req
.io_Command
)
102 complete
= CmdRead(LIBBASE
, request
);
107 complete
= CmdWrite(LIBBASE
, request
);
111 complete
= CmdFlush(LIBBASE
, (struct IORequest
*)request
);
115 complete
= CmdS2DeviceQuery(LIBBASE
, request
);
118 case S2_GETSTATIONADDRESS
:
119 complete
= CmdGetStationAddress(LIBBASE
, request
);
122 case S2_CONFIGINTERFACE
:
123 complete
= CmdConfigInterface(LIBBASE
, request
);
127 complete
= CmdBroadcast(LIBBASE
, request
);
131 complete
= CmdTrackType(LIBBASE
, request
);
135 complete
= CmdUntrackType(LIBBASE
, request
);
138 case S2_GETTYPESTATS
:
139 complete
= CmdGetTypeStats(LIBBASE
, request
);
142 case S2_GETGLOBALSTATS
:
143 complete
= CmdGetGlobalStats(LIBBASE
, request
);
147 complete
= CmdOnEvent(LIBBASE
, request
);
151 complete
= CmdReadOrphan(LIBBASE
, request
);
155 complete
= CmdOnline(LIBBASE
, request
);
159 complete
= CmdOffline(LIBBASE
, request
);
162 case S2_ADDMULTICASTADDRESS
:
163 case S2_ADDMULTICASTADDRESSES
:
164 complete
= CmdAddMulticastAddresses(LIBBASE
, request
);
167 case S2_DELMULTICASTADDRESS
:
168 case S2_DELMULTICASTADDRESSES
:
169 complete
= CmdDelMulticastAddresses(LIBBASE
, request
);
172 case NSCMD_DEVICEQUERY
:
173 complete
= CmdDeviceQuery(LIBBASE
, (struct IOStdReq
*)request
);
177 complete
= CmdInvalid(LIBBASE
, request
);
180 if(complete
&& (request
->ios2_Req
.io_Flags
& IOF_QUICK
) == 0)
181 ReplyMsg((APTR
)request
);
183 ReleaseSemaphore(&((struct EMACUnit
*)request
->ios2_Req
.io_Unit
)->eu_Lock
);
186 static BOOL
CmdInvalid(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
188 request
->ios2_Req
.io_Error
= IOERR_NOCMD
;
189 request
->ios2_WireError
= S2WERR_GENERIC_ERROR
;
194 static BOOL
CmdRead(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
196 struct EMACUnit
*unit
;
197 struct Opener
*opener
;
198 BOOL complete
= FALSE
;
200 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
202 D(bug("[EMAC%d] S2CmdRead()\n", unit
->eu_UnitNum
));
204 if((unit
->eu_Flags
& IFF_UP
) != 0)
206 opener
= request
->ios2_BufferManagement
;
207 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
208 PutMsg(&opener
->read_port
, (struct Message
*)request
);
212 request
->ios2_Req
.io_Error
= S2ERR_OUTOFSERVICE
;
213 request
->ios2_WireError
= S2WERR_UNIT_OFFLINE
;
222 static BOOL
CmdWrite(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
224 struct EMACUnit
*unit
;
226 ULONG wire_error
= S2WERR_GENERIC_ERROR
;
227 BOOL complete
= FALSE
;
229 /* Check request is valid */
231 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
233 D(bug("[EMAC%d] S2CmdWrite()\n", unit
->eu_UnitNum
));
235 if((unit
->eu_Flags
& IFF_UP
) == 0)
237 error
= S2ERR_OUTOFSERVICE
;
238 wire_error
= S2WERR_UNIT_OFFLINE
;
240 else if((request
->ios2_Req
.io_Command
== S2_MULTICAST
) &&
241 ((request
->ios2_DstAddr
[0] & 0x1) == 0))
243 error
= S2ERR_BAD_ADDRESS
;
244 wire_error
= S2WERR_BAD_MULTICAST
;
247 /* Queue request for sending */
250 request
->ios2_Req
.io_Flags
&= ~IOF_QUICK
;
251 PutMsg(unit
->eu_RequestPorts
[WRITE_QUEUE
], (APTR
)request
);
255 request
->ios2_Req
.io_Error
= error
;
256 request
->ios2_WireError
= wire_error
;
265 static BOOL
CmdFlush(LIBBASETYPEPTR LIBBASE
, struct IORequest
*request
)
267 // TODO: Implement CmdFlush!!!!!!!!!
268 // FlushUnit(LIBBASE, (APTR)request->io_Unit, EVENT_QUEUE, IOERR_ABORTED);
272 static BOOL
CmdS2DeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
274 struct EMACUnit
*unit
= (APTR
)request
->ios2_Req
.io_Unit
;
275 //struct fe_priv *np = unit->pcnu_fe_priv;
276 struct Sana2DeviceQuery
*info
;
277 ULONG size_available
, size
;
279 D(bug("[EMAC%d] S2CmdDeviceQuery()\n", unit
->eu_UnitNum
));
281 /* Copy device info */
283 info
= request
->ios2_StatData
;
284 size
= size_available
= info
->SizeAvailable
;
285 if(size
> sizeof(struct Sana2DeviceQuery
))
286 size
= sizeof(struct Sana2DeviceQuery
);
288 CopyMem(&LIBBASE
->emb_Sana2Info
, info
, size
);
290 info
->BPS
= 100000000;
292 info
->HardwareType
= S2WireType_Ethernet
;
293 info
->SizeAvailable
= size_available
;
294 info
->SizeSupplied
= size
;
301 static BOOL
CmdGetStationAddress(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
303 struct EMACUnit
*unit
;
307 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
309 D(bug("[EMAC%d] S2CmdGetStationAddress()\n", unit
->eu_UnitNum
));
311 CopyMem(unit
->eu_DevAddr
, request
->ios2_SrcAddr
, ETH_ADDRESSSIZE
);
312 CopyMem(unit
->eu_OrgAddr
, request
->ios2_DstAddr
, ETH_ADDRESSSIZE
);
319 static BOOL
CmdConfigInterface(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
321 struct EMACUnit
*unit
;
323 /* Configure adapter */
325 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
327 D(bug("[EMAC%d] S2CmdConfigInterface()\n", unit
->eu_UnitNum
));
329 if((unit
->eu_Flags
& IFF_CONFIGURED
) == 0)
331 CopyMem(request
->ios2_SrcAddr
, unit
->eu_DevAddr
, ETH_ADDRESSSIZE
);
332 unit
->set_mac_address(unit
);
333 unit
->eu_Flags
|= IFF_CONFIGURED
;
337 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
338 request
->ios2_WireError
= S2WERR_IS_CONFIGURED
;
346 static BOOL
CmdBroadcast(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
350 /* Fill in the broadcast address as destination */
352 for(i
= 0; i
< ETH_ADDRESSSIZE
; i
++)
353 request
->ios2_DstAddr
[i
] = 0xff;
355 /* Queue the write as normal */
357 return CmdWrite(LIBBASE
, request
);
360 static BOOL
CmdTrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
362 struct EMACUnit
*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 D(bug("[EMAC%d] S2CmdTrackType(%d)\n", unit
->eu_UnitNum
, request
->ios2_PacketType
));
373 packet_type
= request
->ios2_PacketType
;
375 /* Get global tracker */
376 tracker
= (struct TypeTracker
*)
377 FindTypeStats(LIBBASE
, unit
, &unit
->eu_TypeTrackers
, 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
->eu_TypeTrackers
, (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
);
400 if(initial_stats
!= NULL
)
402 error
= S2ERR_BAD_STATE
;
403 wire_error
= S2WERR_ALREADY_TRACKED
;
408 initial_stats
= AllocMem(sizeof(struct TypeStats
), MEMF_PUBLIC
);
409 if(initial_stats
== NULL
)
411 error
= S2ERR_NO_RESOURCES
;
412 wire_error
= S2WERR_GENERIC_ERROR
;
418 CopyMem(tracker
, initial_stats
, sizeof(struct TypeStats
));
419 AddTail((APTR
)&opener
->initial_stats
, (APTR
)initial_stats
);
424 request
->ios2_Req
.io_Error
= error
;
425 request
->ios2_WireError
= wire_error
;
429 static BOOL
CmdUntrackType(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
431 struct EMACUnit
*unit
;
432 struct Opener
*opener
;
434 struct TypeTracker
*tracker
;
435 struct TypeStats
*initial_stats
;
437 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
439 D(bug("[EMAC%d] S2CmdUntrackType()\n", unit
->eu_UnitNum
));
441 packet_type
= request
->ios2_PacketType
;
443 /* Get global tracker and initial figures */
445 tracker
= (struct TypeTracker
*)
446 FindTypeStats(LIBBASE
, unit
, &unit
->eu_TypeTrackers
, packet_type
);
447 opener
= request
->ios2_BufferManagement
;
448 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
450 /* Decrement tracker usage and free unused structures */
452 if(initial_stats
!= NULL
)
454 if((--tracker
->user_count
) == 0)
457 Remove((APTR
)tracker
);
459 FreeMem(tracker
, sizeof(struct TypeTracker
));
462 Remove((APTR
)initial_stats
);
463 FreeMem(initial_stats
, sizeof(struct TypeStats
));
467 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
468 request
->ios2_WireError
= S2WERR_NOT_TRACKED
;
476 static BOOL
CmdGetTypeStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
478 struct EMACUnit
*unit
;
479 struct Opener
*opener
;
481 struct TypeStats
*initial_stats
, *tracker
;
482 struct Sana2PacketTypeStats
*stats
;
484 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
486 D(bug("[EMAC%d] S2CmdGetTypeStats()\n", unit
->eu_UnitNum
));
488 packet_type
= request
->ios2_PacketType
;
490 /* Get global tracker and initial figures */
492 tracker
= FindTypeStats(LIBBASE
, unit
, &unit
->eu_TypeTrackers
, packet_type
);
493 opener
= request
->ios2_BufferManagement
;
494 initial_stats
= FindTypeStats(LIBBASE
, unit
, &opener
->initial_stats
, packet_type
);
496 /* Copy and adjust figures */
497 if(initial_stats
!= NULL
)
499 stats
= request
->ios2_StatData
;
500 CopyMem(&tracker
->stats
, stats
, sizeof(struct Sana2PacketTypeStats
));
501 stats
->PacketsSent
-= initial_stats
->stats
.PacketsSent
;
502 stats
->PacketsReceived
-= initial_stats
->stats
.PacketsReceived
;
503 stats
->BytesSent
-= initial_stats
->stats
.BytesSent
;
504 stats
->BytesReceived
-= initial_stats
->stats
.BytesReceived
;
505 stats
->PacketsDropped
-= initial_stats
->stats
.PacketsDropped
;
509 request
->ios2_Req
.io_Error
= S2ERR_BAD_STATE
;
510 request
->ios2_WireError
= S2WERR_NOT_TRACKED
;
518 static BOOL
CmdGetGlobalStats(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
520 struct EMACUnit
*unit
;
522 /* Update and copy stats */
524 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
526 D(bug("[EMAC%d] S2CmdGetGlobalStats()\n", unit
->eu_UnitNum
));
528 CopyMem(&unit
->eu_Stats
, request
->ios2_StatData
,
529 sizeof(struct Sana2DeviceStats
));
536 static BOOL
CmdDeviceQuery(LIBBASETYPEPTR LIBBASE
, struct IOStdReq
*request
)
538 struct NSDeviceQueryResult
*info
;
540 /* Set structure size twice */
542 info
= request
->io_Data
;
543 request
->io_Actual
= info
->SizeAvailable
=
544 offsetof(struct NSDeviceQueryResult
, SupportedCommands
) + sizeof(APTR
);
546 /* Report device details */
548 info
->DeviceType
= NSDEVTYPE_SANA2
;
549 info
->DeviceSubType
= 0;
551 info
->SupportedCommands
= (APTR
)supported_commands
;
559 static BOOL
CmdOnEvent(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
561 struct EMACUnit
*unit
;
562 ULONG events
, wanted_events
;
563 BOOL complete
= FALSE
;
565 /* Check if we understand the event types */
567 unit
= (struct EMACUnit
*)request
->ios2_Req
.io_Unit
;
569 D(bug("[EMAC%d] S2CmdOnEvent()\n", unit
->eu_UnitNum
));
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
->eu_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
->eu_RequestPorts
[EVENT_QUEUE
], (APTR
)request
);
605 static BOOL
CmdReadOrphan(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
607 struct EMACUnit
*unit
;
610 BOOL complete
= FALSE
;
612 /* Check request is valid */
614 unit
= (struct EMACUnit
*)request
->ios2_Req
.io_Unit
;
616 D(bug("[EMAC%d] S2CmdReadOrphan()\n", unit
->eu_UnitNum
));
618 if((unit
->eu_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
->eu_RequestPorts
[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 EMACUnit
*unit
= (struct EMACUnit
*)request
->ios2_Req
.io_Unit
;
647 ULONG wire_error
= 0;
650 D(bug("[EMAC%d] S2CmdOnline()\n", unit
->eu_UnitNum
));
652 /* Check request is valid */
653 if((unit
->eu_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
->eu_Flags
& IFF_UP
) == 0))
663 unit
->eu_Stats
.PacketsReceived
= 0;
664 unit
->eu_Stats
.PacketsSent
= 0;
665 unit
->eu_Stats
.BadData
= 0;
666 unit
->eu_Stats
.Overruns
= 0;
667 unit
->eu_Stats
.UnknownTypesReceived
= 0;
668 unit
->eu_Stats
.Reconfigurations
= 0;
670 for(i
= 0; i
< STAT_COUNT
; i
++)
671 unit
->eu_SpecialStats
[i
] = 0;
673 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 EMACUnit
*unit
;
690 /* Put adapter offline */
692 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
694 D(bug("[EMAC%d] S2CmdOffline()\n", unit
->eu_UnitNum
));
696 if((unit
->eu_Flags
& IFF_UP
) != 0)
703 static BOOL
CmdAddMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
705 struct EMACUnit
*unit
;
706 UBYTE
*lower_bound
, *upper_bound
;
708 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
710 D(bug("[EMAC%d] S2CmdAddMulticastAddresses()\n", unit
->eu_UnitNum
));
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
;
730 static BOOL
CmdDelMulticastAddresses(LIBBASETYPEPTR LIBBASE
, struct IOSana2Req
*request
)
732 struct EMACUnit
*unit
;
733 UBYTE
*lower_bound
, *upper_bound
;
735 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
737 D(bug("[EMAC%d] S2CmdDelMulticastAddresses()\n", unit
->eu_UnitNum
));
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
;