-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmt5744.c
2593 lines (2367 loc) · 75.2 KB
/
mt5744.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
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2021, Kevin Jordan, Tom Hunter
**
** Name: mt5744.c
**
** Description:
** Perform emulation of CDC 5744 Automated Cartridge Subsystem (ACS).
** The ACS was a StorageTek 4400 robotic cartridge tape system with
** two communication paths. One path, the control path, was a UDP/IP
** path managed by the NOS ATF subsystem. Under the direction of
** the NOS MAGNET system, ATF sent commands to mount and dismount
** cartridges across the control path. Application data was streamed
** across the second path, the data path. The data path used a
** Cyber channel to connect the mainframe to the StorageTek device
** via a CCC (Cyber Channel Coupler) connected to a FIPS controller.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
#define DEBUG 0
/*
** -------------
** Include Files
** -------------
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32)
#include <winsock.h>
#else
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#include <string.h>
#include "const.h"
#include "types.h"
#include "proto.h"
/*
** -----------------
** Private Constants
** -----------------
*/
/*
** ACS function codes as defined in 1MT (NOS 2.8.7):
**
** F0001 EQU 0001 RELEASE UNIT
** F0002 EQU 0002 CONTINUE
** F0010 EQU 0010 REWIND
** F0110 EQU 0110 REWIND/UNLOAD
** F0012 EQU 0012 GENERAL STATUS
** F0013 EQU 0013 FORESPACE BLOCK
** F0016 EQU 0016 LOCATE BLOCK
** F0113 EQU 0113 BACKSPACE BLOCK
** F0112 EQU 0112 DETAILED STATUS
** F0212 EQU 0212 READ BLOCK ID
** F0312 EQU 0312 READ BUFFERED LOG
** F0020 EQU 0020 CONNECT
** F0220 EQU 0220 CONNECT AND SELECT DATA COMPRESSION
** F0040 EQU 0040 READ FORWARD
** F0140 EQU 0140 READ REVERSE
** F0050 EQU 0050 WRITE
** F0250 EQU 0250 SHORT WRITE
** F0051 EQU 0051 WRITE TAPE MARK
** F0414 EQU 0414 AUTOLOAD
*/
#define Fc5744Release 00001
#define Fc5744Continue 00002
#define Fc5744Rewind 00010
#define Fc5744RewindUnload 00110
#define Fc5744GeneralStatus 00012
#define Fc5744SpaceFwd 00013
#define Fc5744LocateBlock 00016
#define Fc5744SpaceBkw 00113
#define Fc5744DetailedStatus 00112
#define Fc5744ReadBlockId 00212
#define Fc5744ReadBufferedLog 00312
#define Fc5744Connect 00020
#define Fc5744ConnectAndSelectCompression 00220
#define Fc5744ReadFwd 00040
#define Fc5744ReadBkw 00140
#define Fc5744Write 00050
#define Fc5744WriteShort 00250
#define Fc5744WriteTapeMark 00051
#define Fc5744Autoload 00414
/*
** General status
*/
#define St5744Alert 04000
#define St5744CommandRetry 02000
#define St5744NoUnit 01000
#define St5744BlockNotFound 00400
#define St5744WriteEnabled 00200
#define St5744RetryInProgress 00100
#define St5744CharacterFill 00040
#define St5744TapeMark 00020
#define St5744EOT 00010
#define St5744BOT 00004
#define St5744Busy 00002
#define St5744Ready 00001
/*
** ACS error codes as defined in 1MT (NOS 2.8.7). These
** values are returned in General Status Word 2. General
** Status Word 2 is read when General Status Word 1 indicates
** an alert has occurred (bit 11) and command retry not
** indicated (bit 10).
**
** CE001 EQU 1 TRANSPORT NOT ON-LINE
** CE007 EQU 7 DENSITY MARK/BLOCK ID READ FAILURE
** CE012 EQU 12 WRITE ERROR AT LOAD POINT
** CE032 EQU 32 DRIVE BUSY
** CE033 EQU 33 CONTROL UNIT BUSY
** CE051 EQU 51 NO TAPE UNIT CONNECTED
*/
#define EcTranportNotOnline 00001
#define EcBlockIdError 00007
#define EcWriteErrorAtLoadPoint 00012
#define EcDriveBusy 00032
#define EcControlUnitBusy 00033
#define EcNoTapeUnitConnected 00051
#define BlockIdLength 8
#define BufferedLogLength 32
#define DetailedStatusLength 26
#define GeneralStatusLength 2
#define LocateBlockLength 3
/*
** Misc constants.
*/
#define ConnectionRetryInterval 60
#define MaxPpBuf 40000
#define MaxByteBuf 60000
#define VolumeNameSize 6
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
#if DEBUG
#define HexColumn(x) (3 * (x) + 4)
#define AsciiColumn(x) (HexColumn(16) + 2 + (x))
#define LogLineLength (AsciiColumn(16))
#endif
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
/*
** ACS unit state.
*/
typedef enum
{
StAcsDisconnected = 0,
StAcsConnecting,
StAcsRegistering,
StAcsReady
} AcsState;
/*
** Tape Server I/O buffer
*/
typedef struct tapeBuffer
{
u32 in;
u32 out;
u8 data[MaxByteBuf + 16];
} TapeBuffer;
/*
** ACS controller.
*/
typedef struct ctrlParam
{
bool isWriting;
bool isOddFrameCount;
u8 ioDelay;
u8 channelNo;
u8 eqNo;
PpWord generalStatus[GeneralStatusLength];
PpWord detailedStatus[DetailedStatusLength];
#if DEBUG
bool isJustActivated;
#endif
} CtrlParam;
/*
** ACS tape unit.
*/
typedef struct tapeParam
{
CtrlParam *controller;
struct tapeParam *nextTape;
AcsState state;
void (*callback)(struct tapeParam *tp);
time_t nextConnectionAttempt;
char *driveName;
char *serverName;
u8 channelNo;
u8 eqNo;
u8 unitNo;
char volumeName[VolumeNameSize + 1];
struct sockaddr_in serverAddr;
#if defined(_WIN32)
SOCKET fd;
#else
int fd;
#endif
TapeBuffer inputBuffer;
TapeBuffer outputBuffer;
bool isAlert;
bool isBlockNotFound;
bool isBOT;
bool isBusy;
bool isCharacterFill;
bool isEOT;
bool isReady;
bool isTapeMark;
bool isWriteEnabled;
PpWord errorCode;
u32 recordLength;
PpWord ioBuffer[MaxPpBuf];
PpWord *bp;
} TapeParam;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static void mt5744Activate(void);
static void mt5744CalculateBufferedLog(TapeParam *tp);
static void mt5744CalculateDetailedStatus(TapeParam *tp);
static void mt5744CalculateGeneralStatus(TapeParam *tp);
static void mt5744CheckTapeServer(void);
static void mt5744CloseTapeServerConnection(TapeParam *tp);
static void mt5744ConnectCallback(TapeParam *tp);
static void mt5744DismountRequestCallback(TapeParam *tp);
static FcStatus mt5744Func(PpWord funcCode);
static void mt5744FuncBackspace(void);
static void mt5744FuncForespace(void);
static void mt5744FuncReadBkw(void);
static void mt5744FuncReadFwd(void);
static void mt5744Disconnect(void);
static void mt5744FlushWrite(void);
static void mt5744Io(void);
static void mt5744InitiateConnection(TapeParam *tp);
static void mt5744IssueTapeServerRequest(TapeParam *tp, char *request, void (*callback)(struct tapeParam *tp));
void mt5744LoadTape(TapeParam *tp, bool writeEnable);
static void mt5744LocateBlockRequestCallback(TapeParam *tp);
static int mt5744PackBytes(TapeParam *tp, u8 *rp, int recLen);
static char *mt5744ParseTapeServerResponse(TapeParam *tp, int *status);
static void mt5744ReadBlockIdRequestCallback(TapeParam *tp);
static void mt5744ReadRequestCallback(TapeParam *tp);
static void mt5744ReceiveTapeServerResponse(TapeParam *tp);
static void mt5744RegisterUnit(TapeParam *tp);
static void mt5744RegisterUnitRequestCallback(TapeParam *tp);
static void mt5744ResetInputBuffer(TapeParam *tp, u8 *eor);
static void mt5744ResetStatus(TapeParam *tp);
static void mt5744ResetUnit(TapeParam *tp);
static void mt5744RewindRequestCallback(TapeParam *tp);
static void mt5744RewindUnloadRequestCallback(TapeParam *tp);
static void mt5744SendTapeServerRequest(TapeParam *tp);
static void mt5744SpaceRequestCallback(TapeParam *tp);
void mt5744UnloadTape(TapeParam *tp);
static void mt5744WriteRequestCallback(TapeParam *tp);
static void mt5744WriteMarkRequestCallback(TapeParam *tp);
#if DEBUG
static char *mt5744Func2String(PpWord funcCode);
static void mt5744LogBytes(u8 *bytes, int len);
static void mt5744LogFlush(void);
#endif
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
static TapeParam *firstTape = NULL;
static TapeParam *lastTape = NULL;
#if DEBUG
static FILE *mt5744Log = NULL;
static char mt5744LogBuf[LogLineLength + 1];
static int mt5744LogBytesCol = 0;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise 5744 tape drives.
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo number of the unit to initialise
** channelNo channel number the device is attached to
** deviceName optional TCP address of StorageTek 4400 emulator
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void mt5744Init(u8 eqNo, u8 unitNo, u8 channelNo, char *deviceName)
{
CtrlParam *cp;
DevSlot *dp;
struct hostent *hp;
u16 serverPort;
char *sp;
TapeParam *tp;
long value;
if (deviceName == NULL)
{
logDtError(LogErrorLocation, "StorageTek 4400 simulator connection information required for MT5744 on channel %o equipment %o unit %o\n",
channelNo, eqNo, unitNo);
exit(1);
}
#if DEBUG
if (mt5744Log == NULL)
{
mt5744Log = fopen("mt5744log.txt", "wt");
mt5744LogFlush();
}
#endif
/*
** Attach device to channel.
*/
dp = channelAttach(channelNo, eqNo, DtMt5744);
/*
** Setup channel functions.
*/
dp->activate = mt5744Activate;
dp->disconnect = mt5744Disconnect;
dp->func = mt5744Func;
dp->io = mt5744Io;
dp->selectedUnit = -1;
/*
** Setup controller context.
*/
if (dp->controllerContext == NULL)
{
dp->controllerContext = calloc(1, sizeof(CtrlParam));
cp = dp->controllerContext;
cp->channelNo = channelNo;
cp->eqNo = eqNo;
}
/*
** No file associations on this type of unit
*/
dp->fcb[unitNo] = NULL;
/*
** Setup tape unit parameter block.
*/
tp = calloc(1, sizeof(TapeParam));
if (tp == NULL)
{
logDtError(LogErrorLocation, "Failed to allocate MT5744 context block\n");
exit(1);
}
mt5744ResetUnit(tp);
dp->context[unitNo] = tp;
tp->controller = dp->controllerContext;
tp->state = StAcsDisconnected;
tp->nextConnectionAttempt = 0;
tp->fd = 0;
/*
** Set up server connection
*/
tp->driveName = NULL;
tp->serverName = NULL;
serverPort = 0;
sp = strchr(deviceName, '/');
if (sp != NULL)
{
*sp++ = '\0';
tp->driveName = (char *)malloc(strlen(sp) + 1);
strcpy(tp->driveName, sp);
sp = strchr(deviceName, ':');
if (sp != NULL)
{
*sp++ = '\0';
tp->serverName = (char *)malloc(strlen(deviceName) + 1);
strcpy(tp->serverName, deviceName);
value = strtol(sp, NULL, 10);
if ((value > 0) && (value < 0x10000))
{
serverPort = (u16)value;
}
}
}
if ((tp->driveName == NULL) || (tp->serverName == NULL) || (serverPort == 0))
{
fprintf(stderr,
"(mt5744 ) Invalid StorageTek 4400 simulator connection specification for MT5744 on channel %o equipment %o unit %o\n",
channelNo, eqNo, unitNo);
exit(1);
}
hp = gethostbyname(tp->serverName);
if (hp == NULL)
{
logDtError(LogErrorLocation, "Failed to lookup address of StorageTek 4400 simulator host %s\n", tp->serverName);
exit(1);
}
tp->serverAddr.sin_family = AF_INET;
memcpy(&tp->serverAddr.sin_addr.s_addr, (struct in_addr *)hp->h_addr_list[0], sizeof(struct in_addr));
tp->serverAddr.sin_port = htons(serverPort);
/*
** Link into list of tape units.
*/
if (lastTape == NULL)
{
firstTape = tp;
}
else
{
lastTape->nextTape = tp;
}
lastTape = tp;
/*
** Setup show_tape values.
*/
tp->channelNo = channelNo;
tp->eqNo = eqNo;
tp->unitNo = unitNo;
/*
** Print a friendly message.
*/
printf("(mt5744 ) initialised on channel %o equipment %o unit %o, drive %s on tape server %s:%d\n",
channelNo, eqNo, unitNo, tp->driveName, tp->serverName, serverPort);
}
/*--------------------------------------------------------------------------
** Purpose: Show tape status (operator interface).
**
** Parameters: Name Description.
**
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void mt5744ShowTapeStatus()
{
TapeParam *tp = firstTape;
char outBuf[400];
while (tp)
{
sprintf(outBuf, " > %-8s C%02o E%02o U%02o", "5744", tp->channelNo, tp->eqNo, tp->unitNo);
opDisplay(outBuf);
switch (tp->state)
{
case StAcsDisconnected:
opDisplay(" (disconnected)\n");
break;
case StAcsConnecting:
opDisplay(" (connecting)\n");
break;
case StAcsRegistering:
opDisplay(" (registering)\n");
break;
case StAcsReady:
if (tp->volumeName[0])
{
sprintf(outBuf, " %s %s\n", tp->isWriteEnabled ? "w" : "r", tp->volumeName);
opDisplay(outBuf);
}
else
{
opDisplay(" (idle)\n");
}
break;
default:
opDisplay(" (unknown state)\n");
break;
}
tp = tp->nextTape;
}
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mt5744Activate(void)
{
#if DEBUG
CtrlParam *cp = activeDevice->controllerContext;
fprintf(mt5744Log, "\n%010u PP:%02o CH:%02o P:%04o Activate",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id,
activePpu->regP);
cp->isJustActivated = TRUE;
#endif
activeChannel->delayStatus = 5;
}
/*--------------------------------------------------------------------------
** Purpose: Set buffered log information.
**
** Parameters: Name Description.
** tp pointer to tape parameters
**
** Returns: Nothing
**
**------------------------------------------------------------------------*/
static void mt5744CalculateBufferedLog(TapeParam *tp)
{
int i;
for (i = 0; i < BufferedLogLength; i++)
{
tp->ioBuffer[i] = 0;
}
}
/*--------------------------------------------------------------------------
** Purpose: Set detailed device status based on current tape parameters.
**
** Parameters: Name Description.
** tp pointer to tape parameters
**
** Returns: Nothing
**
**------------------------------------------------------------------------*/
static void mt5744CalculateDetailedStatus(TapeParam *tp)
{
CtrlParam *cp;
int i;
cp = activeDevice->controllerContext;
for (i = 0; i < DetailedStatusLength; i++)
{
cp->detailedStatus[i] = 0;
}
if (tp != NULL)
{
cp->detailedStatus[0] = tp->errorCode;
if (tp->isReady == FALSE)
{
cp->detailedStatus[0] |= 00020;
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Set general device status based on current tape parameters.
**
** Parameters: Name Description.
** tp pointer to tape parameters
**
** Returns: Nothing
**
**------------------------------------------------------------------------*/
static void mt5744CalculateGeneralStatus(TapeParam *tp)
{
CtrlParam *cp;
cp = activeDevice->controllerContext;
cp->generalStatus[0] = 0;
cp->generalStatus[1] = 0;
if (tp != NULL)
{
if ((tp->state == StAcsReady) && (tp->fd > 0) && tp->isReady)
{
cp->generalStatus[0] = St5744Ready;
}
if (tp->isBOT)
{
cp->generalStatus[0] |= St5744BOT;
}
if (tp->isBusy)
{
cp->generalStatus[0] |= St5744Busy;
}
if (tp->isCharacterFill)
{
cp->generalStatus[0] |= St5744CharacterFill;
}
if (tp->isTapeMark)
{
cp->generalStatus[0] |= St5744TapeMark;
}
if (tp->isWriteEnabled)
{
cp->generalStatus[0] |= St5744WriteEnabled;
}
if (tp->isEOT)
{
cp->generalStatus[0] |= St5744EOT;
}
if (tp->isBlockNotFound)
{
cp->generalStatus[0] |= St5744BlockNotFound;
}
if (tp->isAlert)
{
cp->generalStatus[0] |= St5744Alert;
}
cp->generalStatus[1] = tp->errorCode;
}
else
{
cp->generalStatus[0] = St5744Ready;
}
}
/*--------------------------------------------------------------------------
** Purpose: Process tape server I/O and state transitions.
**
** Parameters: Name Description.
** None.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mt5744CheckTapeServer(void)
{
#if defined(_WIN32)
SOCKET maxFd;
#else
int maxFd;
#endif
fd_set readFds;
int readySockets;
struct timeval timeout;
TapeParam *tp;
fd_set writeFds;
/*
** First, process any tape server connections in progress
*/
FD_ZERO(&writeFds);
tp = firstTape;
maxFd = 0;
while (tp)
{
if (tp->state == StAcsDisconnected)
{
mt5744InitiateConnection(tp);
}
else if ((tp->fd > 0) && (tp->state == StAcsConnecting))
{
FD_SET(tp->fd, &writeFds);
if (tp->fd > maxFd)
{
maxFd = tp->fd;
}
}
tp = tp->nextTape;
}
if (maxFd > 0)
{
timeout.tv_sec = 0;
timeout.tv_usec = 0;
readySockets = select((int)(maxFd + 1), NULL, &writeFds, NULL, &timeout);
if (readySockets > 0)
{
tp = firstTape;
while (tp)
{
if ((tp->fd > 0) && (tp->state == StAcsConnecting) && FD_ISSET(tp->fd, &writeFds))
{
mt5744ConnectCallback(tp);
}
tp = tp->nextTape;
}
}
}
/*
** Second, process normal I/O for connected tape units
*/
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
tp = firstTape;
maxFd = 0;
while (tp)
{
if ((tp->fd > 0) && (tp->state > StAcsConnecting))
{
FD_SET(tp->fd, &readFds);
if (tp->outputBuffer.out < tp->outputBuffer.in)
{
FD_SET(tp->fd, &writeFds);
}
if (tp->fd > maxFd)
{
maxFd = tp->fd;
}
}
tp = tp->nextTape;
}
if (maxFd < 1)
{
return;
}
timeout.tv_sec = 0;
timeout.tv_usec = 0;
readySockets = select((int)(maxFd + 1), &readFds, &writeFds, NULL, &timeout);
if (readySockets < 1)
{
return;
}
tp = firstTape;
while (tp)
{
if ((tp->fd > 0) && (tp->state > StAcsConnecting))
{
if (FD_ISSET(tp->fd, &readFds))
{
mt5744ReceiveTapeServerResponse(tp);
}
if ((tp->fd > 0) && FD_ISSET(tp->fd, &writeFds))
{
mt5744SendTapeServerRequest(tp);
}
}
tp = tp->nextTape;
}
}
/*--------------------------------------------------------------------------
** Purpose: Close connection to StorageTek simulator
**
** Parameters: Name Description.
** tp pointer to tape unit parameters
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mt5744CloseTapeServerConnection(TapeParam *tp)
{
#if DEBUG
fprintf(mt5744Log, "\n%010u Close connection on socket %d to %s:%u for CH:%02o u:%d", traceSequenceNo,
tp->fd, tp->serverName, ntohs(tp->serverAddr.sin_port), tp->channelNo, tp->unitNo);
#endif
netCloseConnection(tp->fd);
tp->fd = 0;
tp->isReady = FALSE;
tp->isBusy = FALSE;
tp->errorCode = EcTranportNotOnline;
tp->state = StAcsDisconnected;
tp->nextConnectionAttempt = getSeconds() + (time_t)ConnectionRetryInterval;
}
/*--------------------------------------------------------------------------
** Purpose: Handle a TCP connection event
**
** Parameters: Name Description.
** tp pointer to tape unit parameters
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mt5744ConnectCallback(TapeParam *tp)
{
int rc;
rc = netGetErrorStatus(tp->fd);
if (rc != 0) // connection failed
{
#if DEBUG
fprintf(mt5744Log, "\n%010u Failed to connect on socket %d to %s:%u for CH:%02o u:%d", traceSequenceNo,
tp->fd, tp->serverName, ntohs(tp->serverAddr.sin_port), tp->channelNo, tp->unitNo);
#endif
mt5744CloseTapeServerConnection(tp);
}
else
{
#if DEBUG
fprintf(mt5744Log, "\n%010u Connected on socket %d to %s:%u for CH:%02o u:%d", traceSequenceNo,
tp->fd, tp->serverName, ntohs(tp->serverAddr.sin_port), tp->channelNo, tp->unitNo);
#endif
mt5744RegisterUnit(tp);
}
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mt5744Disconnect(void)
{
#if DEBUG
fprintf(mt5744Log, "\n%010u PP:%02o CH:%02o P:%04o Disconnect",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id,
activePpu->regP);
#endif
/*
** Abort pending device disconnects - the PP is doing the disconnect.
*/
activeChannel->delayDisconnect = 0;
activeChannel->discAfterInput = FALSE;
}
/*--------------------------------------------------------------------------
** Purpose: Process a response from the StorageTek simulator to a
** DISMOUNT request.
**
** Parameters: Name Description.
** tp pointer to tape unit parameters
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mt5744DismountRequestCallback(TapeParam *tp)
{
char *eor;
int status;
eor = mt5744ParseTapeServerResponse(tp, &status);
if (eor == NULL)
{
return;
}
tp->isBusy = FALSE;
if (status == 200)
{
tp->isReady = FALSE;
}
else
{
logDtError(LogErrorLocation, "Unexpected status %d received from StorageTek simulator for DISMOUNT request\n", status);
mt5744CloseTapeServerConnection(tp);
}
mt5744ResetInputBuffer(tp, (u8 *)eor);
}
/*--------------------------------------------------------------------------
** Purpose: Flush accumulated write data.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mt5744FlushWrite(void)
{
char buffer[10];
CtrlParam *cp = activeDevice->controllerContext;
u8 *dataStart;
u32 i;
PpWord *ip;
int len;
u32 recLen0;
u32 recLen2;
u8 *rp;
TapeParam *tp;
i8 unitNo;
unitNo = activeDevice->selectedUnit;
tp = (TapeParam *)activeDevice->context[unitNo];
if ((unitNo == -1) || !tp->isReady)
{
return;
}
tp->bp = tp->ioBuffer;
recLen0 = 0;
recLen2 = tp->recordLength;
ip = tp->ioBuffer;
memcpy(&tp->outputBuffer.data[0], "WRITE \n", 16);
rp = dataStart = &tp->outputBuffer.data[16];
for (i = 0; i < recLen2; i += 2)
{
*rp++ = ((ip[0] >> 4) & 0xff);
*rp++ = ((ip[0] << 4) & 0xf0) | ((ip[1] >> 8) & 0x0f);
*rp++ = ((ip[1] >> 0) & 0xff);
ip += 2;
}
recLen0 = (u32)(rp - dataStart);
if ((recLen2 & 1) != 0)
{
recLen0 -= 2;
}
else if (cp->isOddFrameCount)
{
recLen0 -= 1;
}
len = sprintf(buffer, "%d", recLen0);
memcpy(&tp->outputBuffer.data[6], buffer, len);
tp->outputBuffer.out = 0;
tp->outputBuffer.in = recLen0 + 16;
tp->callback = mt5744WriteRequestCallback;
tp->isBusy = TRUE;
cp->isWriting = FALSE;
cp->isOddFrameCount = FALSE;
#if DEBUG
fprintf(mt5744Log, "\n%010u PP:%02o CH:%02o P:%04o Write %d PP words",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id,
activePpu->regP,
tp->recordLength);
#endif
mt5744SendTapeServerRequest(tp);
}
/*--------------------------------------------------------------------------
** Purpose: Execute function code on 5744 tape drives.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus mt5744Func(PpWord funcCode)
{
CtrlParam *cp;
TapeParam *tp;