forked from rbowler/spinhawk
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ctc_lcs.c
2710 lines (2184 loc) · 90 KB
/
ctc_lcs.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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CTC_LCS.C (c) Copyright James A. Pierson, 2002-2012 */
/* (c) Copyright "Fish" (David B. Trout), 2002-2011 */
/* Hercules LAN Channel Station Support */
#include "hstdinc.h"
/* jbs 10/27/2007 added _SOLARIS_ */
#if !defined(__SOLARIS__)
#include "hercules.h"
#include "ctcadpt.h"
#include "tuntap.h"
#include "hercifc.h"
#include "opcode.h"
#include "herc_getopt.h"
#if defined(OPTION_W32_CTCI)
#include "tt32api.h"
#endif
//-----------------------------------------------------------------------------
// Debugging...
//#define NO_LCS_OPTIMIZE // #undef for Release, #define while testing
#if !defined( DEBUG) && !defined( _DEBUG ) // only needed for Release builds
#ifdef NO_LCS_OPTIMIZE // for reliable breakpoints and instr stepping
#pragma optimize( "", off ) // disable optimizations for reliable breakpoints
#pragma warning( push ) // save current settings
#pragma warning( disable: 4748 ) // C4748: /GS can not ... because optimizations are disabled...
#endif // NO_LCS_OPTIMIZE
#endif // !defined( DEBUG) && !defined( _DEBUG )
#ifdef NO_LCS_OPTIMIZE
#undef ASSERT
#undef VERIFY
#ifdef _MSVC_
#define ASSERT(a) \
do \
{ \
if (!(a)) \
{ \
logmsg("HHCxx999W *** Assertion Failed! *** %s(%d); function: %s\n",__FILE__,__LINE__,__FUNCTION__); \
if (IsDebuggerPresent()) DebugBreak(); /* (break into debugger) */ \
} \
} \
while(0)
#else // ! _MSVC_
#define ASSERT(a) \
do \
{ \
if (!(a)) \
{ \
logmsg("HHCxx999W *** Assertion Failed! *** %s(%d)\n",__FILE__,__LINE__); \
} \
} \
while(0)
#endif // _MSVC_
#define VERIFY(a) ASSERT((a))
#endif // NO_LCS_OPTIMIZE
//-----------------------------------------------------------------------------
/* CCW Codes 0x03 & 0xC3 are immediate commands */
static BYTE CTC_Immed_Commands [256] =
{
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0, /* 00 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 10 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 20 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 30 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 60 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 80 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* A0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* B0 */
0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0, /* C0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* D0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* E0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* F0 */
};
// ====================================================================
// Declarations
// ====================================================================
static void LCS_Startup ( PLCSDEV pLCSDEV, PLCSCMDHDR pCmdFrame );
static void LCS_Shutdown ( PLCSDEV pLCSDEV, PLCSCMDHDR pCmdFrame );
static void LCS_StartLan ( PLCSDEV pLCSDEV, PLCSCMDHDR pCmdFrame );
static void LCS_StopLan ( PLCSDEV pLCSDEV, PLCSCMDHDR pCmdFrame );
static void LCS_QueryIPAssists( PLCSDEV pLCSDEV, PLCSCMDHDR pCmdFrame );
static void LCS_LanStats ( PLCSDEV pLCSDEV, PLCSCMDHDR pCmdFrame );
static void LCS_DefaultCmdProc( PLCSDEV pLCSDEV, PLCSCMDHDR pCmdFrame );
static void* LCS_PortThread( PLCSPORT pLCSPORT );
static int LCS_EnqueueEthFrame( PLCSDEV pLCSDEV, BYTE bPort,
BYTE* pData, size_t iSize );
static int LCS_EnqueueReplyFrame( PLCSDEV pLCSDEV, PLCSCMDHDR pReply,
size_t iSize );
static int BuildOAT( char* pszOATName, PLCSBLK pLCSBLK );
static char* ReadOAT( char* pszOATName, FILE* fp, char* pszBuff );
static int ParseArgs( DEVBLK* pDEVBLK, PLCSBLK pLCSBLK,
int argc, char** argv );
// ====================================================================
// Helper macros
// ====================================================================
#define INIT_REPLY_FRAME( reply, pCmdFrame ) \
\
memset( &(reply), 0, sizeof( reply )); \
memcpy( &(reply), (pCmdFrame), sizeof( LCSCMDHDR )); \
STORE_HW( (reply).bLCSCmdHdr.hwReturnCode, 0x0000 )
#define ENQUEUE_REPLY_FRAME( pLCSDEV, reply ) \
\
while \
(1 \
&& LCS_EnqueueReplyFrame( (pLCSDEV), (PLCSCMDHDR) &(reply), \
sizeof( reply )) != 0 \
&& (pLCSDEV)->pLCSBLK->Port[(pLCSDEV)->bPort].fd != -1 \
&& !(pLCSDEV)->pLCSBLK->Port[(pLCSDEV)->bPort].fCloseInProgress \
) \
{ \
TRACE("** ENQUEUE_REPLY_FRAME() failed...\n"); \
SLEEP( 1 ); \
}
// ====================================================================
// find_group_device
// ====================================================================
static DEVBLK * find_group_device(DEVGRP *group, U16 devnum)
{
int i;
for(i = 0; i < group->acount; i++)
if( group->memdev[i]->devnum == devnum )
return group->memdev[i];
return NULL;
}
// ====================================================================
// LCS_Init
// ====================================================================
int LCS_Init( DEVBLK* pDEVBLK, int argc, char *argv[] )
{
PLCSBLK pLCSBLK;
PLCSDEV pLCSDev;
int i;
struct in_addr addr; // Work area for addresses
pDEVBLK->devtype = 0x3088;
// Return when an existing group has been joined but is still incomplete
if(!group_device(pDEVBLK, 0) && pDEVBLK->group)
return 0;
// We need to create a group, and as such determine the number of devices
if(!pDEVBLK->group)
{
// Housekeeping
pLCSBLK = malloc( sizeof( LCSBLK ) );
if( !pLCSBLK )
{
logmsg( _("HHCLC001E %4.4X unable to allocate LCSBLK\n"),
pDEVBLK->devnum );
return -1;
}
memset( pLCSBLK, 0, sizeof( LCSBLK ) );
for( i = 0; i < LCS_MAX_PORTS; i++ )
{
memset( &pLCSBLK->Port[i], 0, sizeof ( LCSPORT ) );
pLCSBLK->Port[i].bPort = i;
pLCSBLK->Port[i].pLCSBLK = pLCSBLK;
// Initialize locking and event mechanisms
initialize_lock( &pLCSBLK->Port[i].Lock );
initialize_lock( &pLCSBLK->Port[i].EventLock );
initialize_condition( &pLCSBLK->Port[i].Event );
}
// Parse configuration file statement
if( ParseArgs( pDEVBLK, pLCSBLK, argc, (char**)argv ) != 0 )
{
free( pLCSBLK );
pLCSBLK = NULL;
return -1;
}
if( pLCSBLK->pszOATFilename )
{
// If an OAT file was specified, Parse it and build the
// OAT table.
if( BuildOAT( pLCSBLK->pszOATFilename, pLCSBLK ) != 0 )
{
free( pLCSBLK );
pLCSBLK = NULL;
return -1;
}
}
else
{
// Otherwise, build an OAT based on the address specified
// in the config file with an assumption of IP mode.
pLCSBLK->pDevices = malloc( sizeof( LCSDEV ) );
memset( pLCSBLK->pDevices, 0, sizeof( LCSDEV ) );
if( pLCSBLK->pszIPAddress )
inet_aton( pLCSBLK->pszIPAddress, &addr );
pLCSBLK->pDevices->sAddr = pDEVBLK->devnum;
pLCSBLK->pDevices->bMode = LCSDEV_MODE_IP;
pLCSBLK->pDevices->bPort = 0;
pLCSBLK->pDevices->bType = 0;
pLCSBLK->pDevices->lIPAddress = addr.s_addr; // (network byte order)
pLCSBLK->pDevices->pszIPAddress = pLCSBLK->pszIPAddress;
pLCSBLK->pDevices->pNext = NULL;
pLCSBLK->icDevices = 2;
}
// Now we must create the group
if(!group_device(pDEVBLK, pLCSBLK->icDevices))
{
pDEVBLK->group->grp_data = pLCSBLK;
return 0;
}
else
pDEVBLK->group->grp_data = pLCSBLK;
}
else
pLCSBLK = pDEVBLK->group->grp_data;
// When this code is reached the last devblk has been allocated...
//
// Now build the LCSDEV's.
// If an OAT is specified, the addresses that were specified in the
// hercules.cnf file must match those that are specified in the OAT.
for( pLCSDev = pLCSBLK->pDevices; pLCSDev; pLCSDev = pLCSDev->pNext )
{
pLCSDev->pDEVBLK[0] = find_group_device(pDEVBLK->group, pLCSDev->sAddr);
if( !pLCSDev->pDEVBLK[0] )
{
logmsg(D_("HHCLC040E %4.4X LCSDEV %4.4X not in configuration\n"),
pDEVBLK->group->memdev[0]->devnum, pLCSDev->sAddr );
return -1;
}
// Establish SENSE ID and Command Information Word data.
SetSIDInfo( pLCSDev->pDEVBLK[0], 0x3088, 0x60, 0x3088, 0x01 );
// SetCIWInfo( pLCSDev->pDEVBLK[0], 0, 0, 0x72, 0x0080 );
// SetCIWInfo( pLCSDev->pDEVBLK[0], 1, 1, 0x83, 0x0004 );
// SetCIWInfo( pLCSDev->pDEVBLK[0], 2, 2, 0x82, 0x0040 );
pLCSDev->pDEVBLK[0]->ctctype = CTC_LCS;
pLCSDev->pDEVBLK[0]->ctcxmode = 1;
pLCSDev->pDEVBLK[0]->dev_data = pLCSDev;
pLCSDev->pLCSBLK = pLCSBLK;
strcpy( pLCSDev->pDEVBLK[0]->filename, pLCSBLK->pszTUNDevice );
// If this is an IP Passthru address, we need a write address
if( pLCSDev->bMode == LCSDEV_MODE_IP )
{
pLCSDev->pDEVBLK[1] = find_group_device(pDEVBLK->group, pLCSDev->sAddr^1);
if( !pLCSDev->pDEVBLK[1] )
{
logmsg(D_("HHCLC040E %4.4X LCSDEV %4.4X not in configuration\n"),
pDEVBLK->group->memdev[0]->devnum, pLCSDev->sAddr^1 );
return -1;
}
// Establish SENSE ID and Command Information Word data.
SetSIDInfo( pLCSDev->pDEVBLK[1], 0x3088, 0x60, 0x3088, 0x01 );
// SetCIWInfo( pLCSDev->pDEVBLK[1], 0, 0, 0x72, 0x0080 );
// SetCIWInfo( pLCSDev->pDEVBLK[1], 1, 1, 0x83, 0x0004 );
// SetCIWInfo( pLCSDev->pDEVBLK[1], 2, 2, 0x82, 0x0040 );
pLCSDev->pDEVBLK[1]->ctctype = CTC_LCS;
pLCSDev->pDEVBLK[1]->ctcxmode = 1;
pLCSDev->pDEVBLK[1]->dev_data = pLCSDev;
strcpy( pLCSDev->pDEVBLK[1]->filename, pLCSBLK->pszTUNDevice );
}
// Indicate that the DEVBLK(s) have been create sucessfully
pLCSDev->fCreated = 1;
// Initialize locking and event mechanisms
initialize_lock( &pLCSDev->Lock );
initialize_lock( &pLCSDev->EventLock );
initialize_condition( &pLCSDev->Event );
// Create the TAP interface (if not already created by a
// previous pass. More than one interface can exist on a port.
if( !pLCSBLK->Port[pLCSDev->bPort].fCreated )
{
int rc;
rc = TUNTAP_CreateInterface( pLCSBLK->pszTUNDevice,
IFF_TAP | IFF_NO_PI,
&pLCSBLK->Port[pLCSDev->bPort].fd,
pLCSBLK->Port[pLCSDev->bPort].szNetDevName );
logmsg(_("HHCLC073I %4.4X: TAP device %s opened\n"),
pLCSDev->pDEVBLK[0]->devnum,
pLCSBLK->Port[pLCSDev->bPort].szNetDevName);
#if defined(OPTION_W32_CTCI)
// Set the specified driver/dll i/o buffer sizes..
{
struct tt32ctl tt32ctl;
memset( &tt32ctl, 0, sizeof(tt32ctl) );
strlcpy( tt32ctl.tt32ctl_name, pLCSBLK->Port[pLCSDev->bPort].szNetDevName, sizeof(tt32ctl.tt32ctl_name) );
tt32ctl.tt32ctl_devbuffsize = pLCSBLK->iKernBuff;
if( TUNTAP_IOCtl( pLCSBLK->Port[pLCSDev->bPort].fd, TT32SDEVBUFF, (char*)&tt32ctl ) != 0 )
{
logmsg( _("HHCLC074W TT32SDEVBUFF failed for device %s: %s.\n"),
pLCSBLK->Port[pLCSDev->bPort].szNetDevName, strerror( errno ) );
}
tt32ctl.tt32ctl_iobuffsize = pLCSBLK->iIOBuff;
if( TUNTAP_IOCtl( pLCSBLK->Port[pLCSDev->bPort].fd, TT32SIOBUFF, (char*)&tt32ctl ) != 0 )
{
logmsg( _("HHCLC075W TT32SIOBUFF failed for device %s: %s.\n"),
pLCSBLK->Port[pLCSDev->bPort].szNetDevName, strerror( errno ) );
}
}
#endif
// Indicate that the port is used.
pLCSBLK->Port[pLCSDev->bPort].fUsed = 1;
pLCSBLK->Port[pLCSDev->bPort].fCreated = 1;
create_thread( &pLCSBLK->Port[pLCSDev->bPort].tid,
JOINABLE, LCS_PortThread,
&pLCSBLK->Port[pLCSDev->bPort],
"LCS_PortThread" );
/* Identify the thread ID with the devices on which they are active */
pLCSDev->pDEVBLK[0]->tid = pLCSBLK->Port[pLCSDev->bPort].tid;
if (pLCSDev->pDEVBLK[1])
pLCSDev->pDEVBLK[1]->tid = pLCSBLK->Port[pLCSDev->bPort].tid;
}
// Add these devices to the ports device list.
pLCSBLK->Port[pLCSDev->bPort].icDevices++;
pLCSDev->pDEVBLK[0]->fd = pLCSBLK->Port[pLCSDev->bPort].fd;
if( pLCSDev->pDEVBLK[1] )
pLCSDev->pDEVBLK[1]->fd = pLCSBLK->Port[pLCSDev->bPort].fd;
}
return 0;
}
// ====================================================================
// LCS_ExecuteCCW
// ====================================================================
void LCS_ExecuteCCW( DEVBLK* pDEVBLK, BYTE bCode,
BYTE bFlags, BYTE bChained,
U16 sCount, BYTE bPrevCode,
int iCCWSeq, BYTE* pIOBuf,
BYTE* pMore, BYTE* pUnitStat,
U16* pResidual )
{
int iNum; // Number of bytes to move
BYTE bOpCode; // CCW opcode with modifier
// bits masked off
UNREFERENCED( bFlags );
UNREFERENCED( bChained );
UNREFERENCED( bPrevCode );
UNREFERENCED( iCCWSeq );
// Intervention required if the device file is not open
if( pDEVBLK->fd < 0 &&
!IS_CCW_SENSE( bCode ) &&
!IS_CCW_CONTROL( bCode ) )
{
pDEVBLK->sense[0] = SENSE_IR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Mask off the modifier bits in the CCW bOpCode
if( ( bCode & 0x07 ) == 0x07 )
bOpCode = 0x07;
else if( ( bCode & 0x03 ) == 0x02 )
bOpCode = 0x02;
else if( ( bCode & 0x0F ) == 0x0C )
bOpCode = 0x0C;
else if( ( bCode & 0x03 ) == 0x01 )
bOpCode = pDEVBLK->ctcxmode ? ( bCode & 0x83 ) : 0x01;
else if( ( bCode & 0x1F ) == 0x14 )
bOpCode = 0x14;
else if( ( bCode & 0x47 ) == 0x03 )
bOpCode = 0x03;
else if( ( bCode & 0xC7 ) == 0x43 )
bOpCode = 0x43;
#if 0
// Special case for LCS CIW's
else if( ( bCode == 72 || bCode == 82 || bCode == 83 ) )
bOpCode == bCode;
#endif
else
bOpCode = bCode;
// Process depending on CCW bOpCode
switch (bOpCode)
{
case 0x01: // 0MMMMM01 WRITE
//------------------------------------------------------------
// WRITE
//------------------------------------------------------------
// Return normal status if CCW count is zero
if( sCount == 0 )
{
*pUnitStat = CSW_CE | CSW_DE;
break;
}
LCS_Write( pDEVBLK, sCount, pIOBuf, pUnitStat, pResidual );
break;
case 0x81: // 1MMMMM01 WEOF
//------------------------------------------------------------
// WRITE EOF
//------------------------------------------------------------
// Return normal status
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x02: // MMMMMM10 READ
case 0x0C: // MMMM1100 RDBACK
// -----------------------------------------------------------
// READ & READ BACKWARDS
// -----------------------------------------------------------
// Read data and set unit status and residual byte count
LCS_Read( pDEVBLK, sCount, pIOBuf, pUnitStat, pResidual, pMore );
break;
case 0x07: // MMMMM111 CTL
// -----------------------------------------------------------
// CONTROL
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x03: // M0MMM011 NOP
// -----------------------------------------------------------
// CONTROL NO-OPERATON
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x43: // 00XXX011 SBM
// -----------------------------------------------------------
// SET BASIC MODE
// -----------------------------------------------------------
// Command reject if in basic mode
if( pDEVBLK->ctcxmode == 0 )
{
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
break;
}
// Reset extended mode and return normal status
pDEVBLK->ctcxmode = 0;
*pResidual = 0;
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xC3: // 11000011 SEM
// -----------------------------------------------------------
// SET EXTENDED MODE
// -----------------------------------------------------------
pDEVBLK->ctcxmode = 1;
*pResidual = 0;
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xE3: // 11100011
// -----------------------------------------------------------
// PREPARE (PREP)
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x14: // XXX10100 SCB
// -----------------------------------------------------------
// SENSE COMMAND BYTE
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x04: // 00000100 SENSE
// -----------------------------------------------------------
// SENSE
// -----------------------------------------------------------
// Command reject if in basic mode
if( pDEVBLK->ctcxmode == 0 )
{
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
break;
}
// Calculate residual byte count
iNum = ( sCount < pDEVBLK->numsense ) ?
sCount : pDEVBLK->numsense;
*pResidual = sCount - iNum;
if( sCount < pDEVBLK->numsense )
*pMore = 1;
// Copy device sense bytes to channel I/O buffer
memcpy( pIOBuf, pDEVBLK->sense, iNum );
// Clear the device sense bytes
memset( pDEVBLK->sense, 0, sizeof( pDEVBLK->sense ) );
// Return unit status
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xE4: // 11100100 SID
// -----------------------------------------------------------
// SENSE ID
// -----------------------------------------------------------
// Calculate residual byte count
iNum = ( sCount < pDEVBLK->numdevid ) ?
sCount : pDEVBLK->numdevid;
*pResidual = sCount - iNum;
if( sCount < pDEVBLK->numdevid )
*pMore = 1;
// Copy device identifier bytes to channel I/O buffer
memcpy( pIOBuf, pDEVBLK->devid, iNum );
// Return unit status
*pUnitStat = CSW_CE | CSW_DE;
break;
#if 0
case 0x72: // 0111010 RCD
// ------------------------------------------------------------
// READ CONFIGURATION DATA
// ------------------------------------------------------------
case 0x82: // 10000010 SID
// ------------------------------------------------------------
// SET INTERFACE IDENTIFER
// ------------------------------------------------------------
case 0x83: // 10000011 RID
// ------------------------------------------------------------
// READ NODE IDENTIFER
// ------------------------------------------------------------
LCS_SDC( pDEVBLK, bOpCode, sCount, pIOBuf,
pUnitStat, pResidual, pMore );
break;
#endif
default:
// ------------------------------------------------------------
// INVALID OPERATION
// ------------------------------------------------------------
// Set command reject sense byte, and unit check status
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
}
}
// ====================================================================
// LCS_Close
// ====================================================================
int LCS_Close( DEVBLK* pDEVBLK )
{
PLCSDEV pLCSDEV;
PLCSBLK pLCSBLK;
PLCSPORT pLCSPORT;
if (!(pLCSDEV = (PLCSDEV)pDEVBLK->dev_data))
return 0; // (was incomplete group)
pLCSBLK = pLCSDEV->pLCSBLK;
pLCSPORT = &pLCSBLK->Port[pLCSDEV->bPort];
pLCSPORT->icDevices--;
// Is this the last device on the port?
if( !pLCSPORT->icDevices )
{
// PROGRAMMING NOTE: there's currently no way to interrupt
// the "LCS_PortThread"s TUNTAP_Read of the adapter. Thus
// we must simply wait for LCS_PortThread to eventually
// notice that we're doing a close (via our setting of the
// fCloseInProgress flag). Its TUNTAP_Read will eventually
// timeout after a few seconds (currently 5, which is dif-
// ferent than the CTC_READ_TIMEOUT_SECS timeout value the
// CTCI_Read function uses) and will then do the close of
// the adapter for us (TUNTAP_Close) so we don't have to.
// All we need to do is ask it to exit (via our setting of
// the fCloseInProgress flag) and then wait for it to exit
// (which, as stated, could take up to a max of 5 seconds).
// All of this is simply because it's poor form to close a
// device from one thread while another thread is reading
// from it. Attempting to do so could trip a race condition
// wherein the internal i/o buffers used to process the
// read request could have been freed (by the close call)
// by the time the read request eventually gets serviced.
if( pLCSPORT->fd >= 0 )
{
TID tid = pLCSPORT->tid;
obtain_lock( &pLCSPORT->EventLock );
{
pLCSPORT->fStarted = 0;
pLCSPORT->fCloseInProgress = 1;
signal_condition( &pLCSPORT->Event );
}
release_lock( &pLCSPORT->EventLock );
signal_thread( tid, SIGUSR2 );
join_thread( tid, NULL );
detach_thread( tid );
}
if( pLCSDEV->pDEVBLK[0] && pLCSDEV->pDEVBLK[0]->fd >= 0 )
pLCSDEV->pDEVBLK[0]->fd = -1;
if( pLCSDEV->pDEVBLK[1] && pLCSDEV->pDEVBLK[1]->fd >= 0 )
pLCSDEV->pDEVBLK[1]->fd = -1;
}
// Housekeeping
if( pLCSDEV->pDEVBLK[0] == pDEVBLK )
pLCSDEV->pDEVBLK[0] = NULL;
if( pLCSDEV->pDEVBLK[1] == pDEVBLK )
pLCSDEV->pDEVBLK[1] = NULL;
if( !pLCSDEV->pDEVBLK[0] &&
!pLCSDEV->pDEVBLK[1] )
{
// Remove this LCS Device from the chain...
PLCSDEV pCurrLCSDev = NULL;
PLCSDEV* ppPrevLCSDev = &pLCSBLK->pDevices;
for( pCurrLCSDev = pLCSBLK->pDevices; pCurrLCSDev; pCurrLCSDev = pCurrLCSDev->pNext )
{
if( pCurrLCSDev == pLCSDEV )
{
*ppPrevLCSDev = pCurrLCSDev->pNext;
if( pCurrLCSDev->pszIPAddress )
{
free( pCurrLCSDev->pszIPAddress );
pCurrLCSDev->pszIPAddress = NULL;
}
free( pLCSDEV );
pLCSDEV = NULL;
break;
}
ppPrevLCSDev = &pCurrLCSDev->pNext;
}
}
if( !pLCSBLK->pDevices )
{
if( pLCSBLK->pszTUNDevice ) { free( pLCSBLK->pszTUNDevice ); pLCSBLK->pszTUNDevice = NULL; }
if( pLCSBLK->pszOATFilename ) { free( pLCSBLK->pszOATFilename ); pLCSBLK->pszOATFilename = NULL; }
// if( pLCSBLK->pszIPAddress ) { free( pLCSBLK->pszIPAddress ); pLCSBLK->pszIPAddress = NULL; }
if( pLCSBLK->pszMACAddress ) { free( pLCSBLK->pszMACAddress ); pLCSBLK->pszMACAddress = NULL; }
if( pLCSBLK->pszOATFilename )
{
if( pLCSBLK->pszIPAddress )
{
free( pLCSBLK->pszIPAddress );
pLCSBLK->pszIPAddress = NULL;
}
}
free( pLCSBLK );
pLCSBLK = NULL;
}
pDEVBLK->dev_data = NULL;
return 0;
}
// ====================================================================
// LCS_Query
// ====================================================================
void LCS_Query( DEVBLK* pDEVBLK, char** ppszClass,
int iBufLen, char* pBuffer )
{
char *sType[] = { "", " Pri", " Sec" };
LCSDEV* pLCSDEV;
BEGIN_DEVICE_CLASS_QUERY( "CTCA", pDEVBLK, ppszClass, iBufLen, pBuffer );
pLCSDEV = (LCSDEV*) pDEVBLK->dev_data;
if(!pLCSDEV)
{
strlcpy(pBuffer,"*Uninitialized",iBufLen);
return;
}
snprintf( pBuffer, iBufLen, "LCS Port %2.2X %s%s (%s)%s",
pLCSDEV->bPort,
pLCSDEV->bMode == LCSDEV_MODE_IP ? "IP" : "SNA",
sType[pLCSDEV->bType],
pLCSDEV->pLCSBLK->Port[pLCSDEV->bPort].szNetDevName,
pLCSDEV->pLCSBLK->fDebug ? " -d" : "" );
}
// ====================================================================
// LCS_Read
// ====================================================================
// The guest o/s is issuing a Read CCW for our LCS device. Return to
// it all available LCS Frames that we have buffered up in our buffer.
// --------------------------------------------------------------------
void LCS_Read( DEVBLK* pDEVBLK, U16 sCount,
BYTE* pIOBuf, BYTE* pUnitStat,
U16* pResidual, BYTE* pMore )
{
PLCSHDR pLCSHdr;
PLCSDEV pLCSDEV = (PLCSDEV)pDEVBLK->dev_data;
size_t iLength = 0;
int rc = 0;
// FIXME: we currently don't support data-chaining but
// probably should if real LCS devices do (I was unable
// to determine whether they do or not). -- Fish
for (;;)
{
// Wait for some LCS Frames to arrive in our buffer...
obtain_lock( &pLCSDEV->Lock );
if( !( pLCSDEV->fDataPending || pLCSDEV->fReplyPending ) )
{
struct timespec waittime;
struct timeval now;
release_lock( &pLCSDEV->Lock );
// Wait 5 seconds then check for channel conditions
gettimeofday( &now, NULL );
waittime.tv_sec = now.tv_sec + CTC_READ_TIMEOUT_SECS;
waittime.tv_nsec = now.tv_usec * 1000;
obtain_lock( &pLCSDEV->EventLock );
rc = timed_wait_condition( &pLCSDEV->Event,
&pLCSDEV->EventLock,
&waittime );
release_lock( &pLCSDEV->EventLock );
// If we didn't receive any, keep waiting...
if( rc == ETIMEDOUT || rc == EINTR )
{
// check for halt condition
if( pDEVBLK->scsw.flag2 & SCSW2_FC_HALT ||
pDEVBLK->scsw.flag2 & SCSW2_FC_CLEAR )
{
if( pDEVBLK->ccwtrace || pDEVBLK->ccwstep )
logmsg( _("HHCLC002I %4.4X: Halt or Clear Recognized\n"),
pDEVBLK->devnum );
*pUnitStat = CSW_CE | CSW_DE;
*pResidual = sCount;
return;
}
continue; // (keep waiting)
}
// We received some LCS Frames...
obtain_lock( &pLCSDEV->Lock );
}
// Point to the end of all buffered LCS Frames...
// (where the next Frame *would* go if there was one)
pLCSHdr = (PLCSHDR)( pLCSDEV->bFrameBuffer +
pLCSDEV->iFrameOffset );
// Mark the end of this batch of LCS Frames by setting
// the "offset to NEXT frame" LCS Header field to zero.
// (a zero "next Frame offset" is like an "EOF" flag)
STORE_HW( pLCSHdr->hwOffset, 0x0000 );
// Calculate how much data we're going to be giving them.
// Since 'iFrameOffset' points to the next available LCS
// Frame slot in our buffer, the total amount of LCS Frame
// data we have is exactly that amount. We give them two
// extra bytes however so that they can optionally chase
// the "hwOffset" field in each LCS Frame's LCS Header to
// eventually reach our zero hwOffset "EOF" flag).
iLength = pLCSDEV->iFrameOffset + sizeof(pLCSHdr->hwOffset);
// (calculate residual and set memcpy amount)
// FIXME: we currently don't support data-chaining but
// probably should if real LCS devices do (I was unable
// to determine whether they do or not). -- Fish
if( sCount < iLength )
{
*pMore = 1;
*pResidual = 0;
iLength = sCount;
// PROGRAMMING NOTE: As a result of the caller asking
// for less data than we actually have available, the
// remainder of their unread data they didn't ask for
// will end up being silently discarded. Refer to the
// other NOTEs and FIXME's sprinkled throughout this
// function...
}
else
{
*pMore = 0;
*pResidual -= iLength;
}
*pUnitStat = CSW_CE | CSW_DE;
memcpy( pIOBuf, pLCSDEV->bFrameBuffer, iLength );
// Trace the i/o if requested...
if( pDEVBLK->ccwtrace || pDEVBLK->ccwstep )
{
logmsg( _("HHCLC003I %4.4X: LCS Read:\n"),
pDEVBLK->devnum );
packet_trace( pIOBuf, iLength );
}
// Reset frame buffer to empty...
// PROGRAMMING NOTE: even though not all available data
// may have been read by the guest, we don't currently
// support data-chaining. Thus any unread data is always
// discarded by resetting both of the iFrameOffset and
// fDataPending fields to 0 so that the next read always
// grabs a new batch of LCS Frames starting at the very
// beginning of our frame buffer again. (I was unable
// to determine whether real LCS devices support data-
// chaining or not, but if they do we should fix this).
pLCSDEV->iFrameOffset = 0;
pLCSDEV->fReplyPending = 0;
pLCSDEV->fDataPending = 0;
release_lock( &pLCSDEV->Lock );
return;
}
}
// ====================================================================
// LCS_Write
// ====================================================================
void LCS_Write( DEVBLK* pDEVBLK, U16 sCount,
BYTE* pIOBuf, BYTE* pUnitStat,
U16* pResidual )
{
PLCSDEV pLCSDEV = (PLCSDEV)pDEVBLK->dev_data;
PLCSHDR pLCSHDR = NULL;
PLCSCMDHDR pCmdFrame = NULL;
PLCSETHFRM pLCSEthFrame = NULL;
PETHFRM pEthFrame = NULL;
U16 iOffset = 0;
U16 iPrevOffset = 0;
U16 iLength = 0;
U16 iEthLen = 0;
UNREFERENCED( sCount );
// Process each frame in the buffer...
while( 1 )
{
// Fix-up the LCS header pointer to the current frame
pLCSHDR = (PLCSHDR)( pIOBuf + iOffset );
// Save current offset so we can tell how big next frame is
iPrevOffset = iOffset;
// Get the next frame offset, exit loop if 0
FETCH_HW( iOffset, pLCSHDR->hwOffset );
if( iOffset == 0 ) // ("EOF")
break;
// Calculate size of this LCS Frame
iLength = iOffset - iPrevOffset;
switch( pLCSHDR->bType )
{
case LCS_FRMTYP_CMD: // LCS Command Frame
pCmdFrame = (PLCSCMDHDR)pLCSHDR;
// Trace received command frame...
if( pDEVBLK->ccwtrace || pDEVBLK->ccwstep )
{
logmsg( _("HHCLC051I %4.4X: Cmd Packet...\n"),
pDEVBLK->devnum );
packet_trace( (BYTE*)pCmdFrame, iLength );
}
// FIXME: what is this all about? I'm not saying it's wrong,
// only that we need to document via comments the purpose of
// this test. What's it doing? Why ignore "initiator 1"? etc.
// PLEASE EXPLAIN! -- Fish
if( pCmdFrame->bInitiator == 0x01 )
break;
switch( pCmdFrame->bCmdCode )
{