-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlockmgmt.c
More file actions
1720 lines (1446 loc) · 48.1 KB
/
lockmgmt.c
File metadata and controls
1720 lines (1446 loc) · 48.1 KB
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
/*
Buffer lock managememt.
Branched from http://liah/repos/ndas/fremont/trunk/src/drivers/ndasklib/lockmgmt.c
*/
#ifndef NDAS_NO_LANSCSI
#include "linux_ver.h"
#include "inc/xplatcfg.h"
#include "inc/sal/types.h"
#include "inc/ndasuser/ndasuser.h"
#include "inc/sal/debug.h"
#include "lockmgmt.h"
#include "inc/lspx/lsp.h"
#include "inc/netdisk/conn.h"
#include "udev.h"
#define DEBUG_LEVEL_LOCK 1
#undef INLINE
#define INLINE inline
#ifdef DEBUG
#define debug_lock(l, x...) do {\
if(l <= DEBUG_LEVEL_LOCK) { \
sal_debug_print("UN|%d|%s|",l,__FUNCTION__); \
sal_debug_println(x); \
} \
} while(0)
#else
#define debug_lock(l, x...) do {} while(0)
#endif
//
// Lock ID map
// Maps LUR device lock ID to ndas device lock index depending on the target ID.
//
#define MAX_TARGET_ID 2
#define NDAS_NR_GPLOCK (1 + 3) // NDAS chip 1.1, 2.0 including null lock, buffer lock, and write previlege lock.
#define NDAS_NR_ADV_GPLOCK (1 + 8) // NDAS chip 2.5 including null lock.
// General purpose lock supported by NDAS chip 1.1, 2.0
xuint32 DevLockIdMap[MAX_TARGET_ID][NDAS_NR_GPLOCK] = {
{-1, 2, 3, 0}, // Target 0 uses lock 0 for buffer lock
{-1, 2, 3, 1} // Target 0 uses lock 1 for buffer lock
};
// Advanced general purpose lock supported by NDAS chip 2.5
xuint32 DevLockIdMap_Adv[MAX_TARGET_ID][NDAS_NR_ADV_GPLOCK] = {
{-1, 0, 1, 2, 3, 4, 5, 6, 7},
{-1, 0, 1, 2, 3, 4, 5, 6, 7}
};
xuint32
LockIdToTargetLockIdx(xuint32 HwVesion, xuint32 TargetId, xuint32 LockId) {
xuint32 lockIdx;
//
// Map the lock ID.
//
sal_assert(TargetId < MAX_TARGET_ID);
if(HwVesion == NDAS_VERSION_1_1 ||
HwVesion == NDAS_VERSION_2_0
) {
lockIdx = DevLockIdMap[TargetId][LockId];
} else {
lockIdx = DevLockIdMap_Adv[TargetId][LockId];
}
return lockIdx;
}
//////////////////////////////////////////////////////////////////////////
//
// Lock operations
//
//
// Acquire a general purpose lock.
// NOTE: With RetryWhenFailure is FALSE, be aware that the lock-cleanup
// workaround will not be performed in this function.
//
// return values
// NDAS_ERROR_ACQUIRE_LOCK_FAILED : Lock is owned by another session.
// This is not communication error.
//
// Opxxxx is calling conn_xxx functions and it is not multi-thread safe.Should be called in ndiod thread or single thread.
//
ndas_error_t
OpAcquireDevLock(
IN uconn_t* conn,
IN xuint32 LockIndex,
OUT xuchar* LockData,
IN sal_msec TimeOut,
IN xbool RetryWhenFailure
){
ndas_error_t status = NDAS_ERROR_ACQUIRE_LOCK_FAILED;
xuint32 lockContention;
sal_msec startTime;
sal_msec maximumWaitTime;
lockContention = 0;
startTime = sal_time_msec();
if(TimeOut) {
maximumWaitTime = TimeOut;
} else {
maximumWaitTime = 2 * SAL_MSEC_PER_SEC;
}
while(sal_tick_sub(sal_time_msec(), sal_tick_add(startTime, maximumWaitTime))<=0) {
status = conn_lock_operation(conn, NDAS_LOCK_TAKE, LockIndex, LockData, NULL);
// status = udev_lock_operation(udev, NDAS_LOCK_TAKE, LockIndex, LockData, NULL);
if(status == NDAS_ERROR_ACQUIRE_LOCK_FAILED) {
debug_lock(4, "LockIndex%u: Lock contention #%u!!!", LockIndex, lockContention);
} else if(NDAS_SUCCESS(status)) {
break;
} else {
break;
}
lockContention ++;
//
// Clean up the lock on the NDAS device.
//
if(lockContention != 0 && (lockContention % 10000 == 0)) {
conn_lock_operation(conn, NDAS_LOCK_BREAK, LockIndex, NULL, NULL);
// udev_lock_operation(udev, NDAS_LOCK_BREAK, LockIndex, NULL, NULL);
}
if(RetryWhenFailure == FALSE) {
break;
}
}
if(status == NDAS_ERROR_ACQUIRE_LOCK_FAILED) {
debug_lock(1, "Lock denied. idx=%u lock contention=%u", LockIndex, lockContention);
} else if(!NDAS_SUCCESS(status)) {
debug_lock(1, "Failed to acquire lock idx=%u lock contention=%u", LockIndex, lockContention);
} else {
debug_lock(4, "Acquired lock idx=%u lock contention=%u", LockIndex, lockContention);
}
return status;
}
ndas_error_t
OpReleaseDevLock(
IN uconn_t* conn,
IN xuint32 LockIndex,
IN xuchar* LockData,
IN sal_msec TimeOut
){
ndas_error_t status;
status = conn_lock_operation(conn, NDAS_LOCK_GIVE, LockIndex, LockData, NULL);
// status = udev_lock_operation(udev, NDAS_LOCK_GIVE, LockIndex, LockData, NULL);
if(!NDAS_SUCCESS(status)) {
debug_lock(4, "Failed to release lock idx=%d", LockIndex);
} else {
debug_lock(4, "Release lock idx=%d", LockIndex);
}
return status;
}
#if 0 /* not used */
static
ndas_error_t
OpQueryDevLockOwner(
IN uconn_t* conn,
IN xuint32 LockIndex,
IN xuchar* LockOwner,
IN sal_msec TimeOut
){
ndas_error_t status;
status = conn_lock_operation(conn, NDAS_LOCK_GET_OWNER, LockIndex, LockOwner, NULL);
/* status = udev_lock_operation(
udev,
NDAS_LOCK_GET_OWNER,
LockIndex,
LockOwner,
NULL);*/
if(!NDAS_SUCCESS(status)) {
debug_lock(1,"Failed to query lock owner idx=%d", LockIndex);
return status;
}
return NDAS_OK;
}
#endif
static
ndas_error_t
OpGetDevLockData(
IN uconn_t* conn,
IN xuint32 LockIndex,
IN xuchar* LockData,
IN sal_msec TimeOut
){
ndas_error_t status;
status = conn_lock_operation(conn, NDAS_LOCK_GET_COUNT, LockIndex, LockData, NULL);
/* status = udev_lock_operation(
udev,
NDAS_LOCK_GET_COUNT,
LockIndex,
LockData,
NULL);*/
if(!NDAS_SUCCESS(status)) {
debug_lock(1, "Failed to get lock data idx=%d", LockIndex);
return status;
}
return NDAS_OK;
}
//////////////////////////////////////////////////////////////////////////
//
// Lock cache
//
static
INLINE
xbool
LockCacheIsAcquired (
IN NDAS_DEVLOCK_INFO* LockInfo,
IN xuint32 LockId
) {
PNDAS_DEVLOCK_STATUS lockStatus = &LockInfo->DevLockStatus[LockId];
return lockStatus->Acquired;
}
static
INLINE
void
LockCacheSetDevLockAcquisition(
IN NDAS_DEVLOCK_INFO* LockInfo,
IN xuint32 LockId,
IN xbool AddressRangeValid,
IN xuint64 StartingAddress,
IN xuint64 EndingAddress
) {
PNDAS_DEVLOCK_STATUS lockStatus = &LockInfo->DevLockStatus[LockId];
sal_assert(lockStatus->Acquired == FALSE);
sal_assert(LockInfo->AcquiredLockCount < NDAS_MAX_DEVICE_LOCK_COUNT);
sal_assert(StartingAddress <= EndingAddress);
LockInfo->AcquiredLockCount ++;
lockStatus->Acquired = TRUE;
if(lockStatus->Lost) {
debug_lock(1, "%u: Cleared lost state.", LockId);
LockInfo->LostLockCount --;
lockStatus->Lost = FALSE;
}
if(AddressRangeValid) {
lockStatus->AddressRangeValid = 1;
lockStatus->StartingAddress = StartingAddress;
lockStatus->EndingAddress = EndingAddress;
} else {
lockStatus->AddressRangeValid = 0;
}
}
static
INLINE
void
LockCacheSetDevLockRelease(
IN NDAS_DEVLOCK_INFO* LockInfo,
IN xuint32 LockId
){
PNDAS_DEVLOCK_STATUS lockStatus = &LockInfo->DevLockStatus[LockId];
sal_assert(lockStatus->Acquired == TRUE);
sal_assert(LockInfo->AcquiredLockCount > 0);
LockInfo->AcquiredLockCount --;
lockStatus->Acquired = FALSE;
lockStatus->AddressRangeValid = 0;
}
static
INLINE
void
LockCacheInvalidateAddressRange(
IN NDAS_DEVLOCK_INFO* LockInfo,
IN xuint32 LockId
){
PNDAS_DEVLOCK_STATUS lockStatus = &LockInfo->DevLockStatus[LockId];
lockStatus->AddressRangeValid = 0;
}
static
INLINE
xbool
LockCacheIsLost(
IN NDAS_DEVLOCK_INFO* LockInfo,
IN xuint32 LockId
){
PNDAS_DEVLOCK_STATUS lockStatus = &LockInfo->DevLockStatus[LockId];
return lockStatus->Lost;
}
static
INLINE
void
LockCacheSetDevLockLoss(
IN NDAS_DEVLOCK_INFO* LockInfo,
IN xuint32 LockId
){
PNDAS_DEVLOCK_STATUS lockStatus = &LockInfo->DevLockStatus[LockId];
sal_assert(lockStatus->Acquired == TRUE);
sal_assert(lockStatus->Lost == FALSE);
sal_assert(LockInfo->LostLockCount < NDAS_MAX_DEVICE_LOCK_COUNT);
LockCacheSetDevLockRelease(LockInfo, LockId);
debug_lock(1, "Lost lock #%d", LockId);
LockInfo->LostLockCount ++;
lockStatus->Lost = TRUE;
}
static
INLINE
void
LockCacheClearDevLockLoss(
IN NDAS_DEVLOCK_INFO* LockInfo,
IN xuint32 LockId
){
PNDAS_DEVLOCK_STATUS lockStatus = &LockInfo->DevLockStatus[LockId];
sal_assert(lockStatus->Lost == TRUE);
sal_assert(LockInfo->LostLockCount > 0);
debug_lock(1, "Confirmed lost lock #%d", LockId);
LockInfo->LostLockCount --;
lockStatus->Lost = FALSE;
}
//
// Mark all lock lost during reconnection.
//
void
LockCacheAllLocksLost(
IN NDAS_DEVLOCK_INFO* LockInfo
){
xuint32 lockId;
for(lockId = 0; lockId < NDAS_MAX_DEVICE_LOCK_COUNT; lockId ++) {
if(LockInfo->DevLockStatus[lockId].Acquired) {
LockCacheSetDevLockLoss(LockInfo, lockId);
#ifdef DEBUG
if(lockId == NDAS_DEVLOCK_ID_BUFFLOCK) {
debug_lock( 1, "Lost Buf lock #%d", lockId);
} else {
debug_lock(1, "Lost lock #%d", lockId);
}
#endif
}
}
#ifdef DEBUG
if(LockInfo->LostLockCount == 0) {
debug_lock(1, "No Lost lock");
}
#endif
}
//
// Check to see if there are lock acquired except for the buffer lock.
//
// Called when link has changed. This case is not implemented in xplat.
//
xbool
LockCacheAcquiredLocksExistsExceptForBufferLock(
IN NDAS_DEVLOCK_INFO* LockInfo
){
xuint32 lockId;
for(lockId = 0; lockId < NDAS_MAX_DEVICE_LOCK_COUNT; lockId ++) {
if(LockInfo->DevLockStatus[lockId].Acquired) {
if(lockId != NDAS_DEVLOCK_ID_BUFFLOCK) {
debug_lock(1, "Lost lock #%d", lockId);
return TRUE;
}
}
}
return FALSE;
}
//
// Check if the IO range requires a lock that is lost.
//
xbool
LockCacheCheckLostLockIORange(
IN NDAS_DEVLOCK_INFO* LockInfo,
IN xuint64 StartingAddress,
IN xuint64 EndingAddress
){
xuint32 lostLockCnt;
xuint32 lockId;
PNDAS_DEVLOCK_STATUS devLockStatus;
sal_assert(StartingAddress <= EndingAddress);
lostLockCnt = LockInfo->LostLockCount;
lockId = 0;
while(lostLockCnt &&
lockId < NDAS_MAX_DEVICE_LOCK_COUNT) {
devLockStatus = &LockInfo->DevLockStatus[lockId];
if(devLockStatus->Lost) {
//
// Check the intersection.
//
if(devLockStatus->AddressRangeValid) {
if(StartingAddress < devLockStatus->StartingAddress) {
if(EndingAddress >= devLockStatus->StartingAddress) {
debug_lock(1, "The address range is in lost lock's address range(1).");
return TRUE;
}
} else if(StartingAddress <= devLockStatus->EndingAddress) {
debug_lock(1, "The address range is in lost lock's address range(2).");
return TRUE;
}
}
lostLockCnt --;
}
lockId++;
}
sal_assert(lostLockCnt == 0);
return FALSE;
}
#if 0 /* Not used */
//////////////////////////////////////////////////////////////////////////
//
// Device lock control request dispatcher
//
ndas_error_t
LurnIdeDiskDeviceLockControl(
IN PLURELATION_NODE Lurn,
IN PLURNEXT_IDE_DEVICE IdeDisk,
IN PCCB Ccb
){
ndas_error_t status;
PLURN_DEVLOCK_CONTROL devLockControl;
xuint32 lockIdx;
xuint32 lockDataLength;
UNREFERENCED_PARAMETER(Lurn);
if(Ccb->DataBufferLength < sizeof(LURN_DEVLOCK_CONTROL)) {
Ccb->CcbStatus = CCB_STATUS_DATA_OVERRUN;
return NDAS_OK;
}
status = NDAS_OK;
//
// Verify the lock operation
//
devLockControl = (PLURN_DEVLOCK_CONTROL)Ccb->DataBuffer;
if(devLockControl->LockId == LURNDEVLOCK_ID_NONE) {
sal_assert( FALSE );
// Nothing to do.
Ccb->CcbStatus = CCB_STATUS_SUCCESS;
return NDAS_OK;
}
switch(IdeDisk->udev.HwVersion) {
case NDAS_VERSION_1_1:
case NDAS_VERSION_2_0:
#ifdef DEBUG
if (devLockControl->LockId == NDAS_DEVLOCK_ID_BUFFLOCK) {
sal_assert( LsCcbIsFlagOn(Ccb, CCB_FLAG_CALLED_INTERNEL) );
}
#endif
if(devLockControl->LockId >= NDAS_NR_GPLOCK) {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
return NDAS_OK;
}
if(devLockControl->AdvancedLock) {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
return NDAS_OK;
}
lockDataLength = 4;
break;
#if 0
case LANSCSIIDE_VERSION_2_5:
if(devLockControl->LockId >= NDAS_NR_ADV_GPLOCK) {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
return NDAS_OK;
}
if(!devLockControl->AdvancedLock) {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
return NDAS_OK;
}
lockDataLength = 64;
break;
#endif
case LANSCSIIDE_VERSION_1_0:
default:
sal_assert( FALSE );
Ccb->CcbStatus = CCB_STATUS_INVALID_COMMAND;
return NDAS_OK;
}
// Check to see if the lock acquisition is required.
if( devLockControl->RequireLockAcquisition
) {
if(IdeDisk->udev.lock_info.DevLockStatus[devLockControl->LockId].Acquired == FALSE) {
Ccb->CcbStatus = CCB_STATUS_LOST_LOCK;
return NDAS_OK;
}
}
//
// Map the lock ID.
//
sal_assert(IdeDisk->udev.conn.unit < MAX_TARGET_ID);
if(IdeDisk->udev.HwVersion == NDAS_VERSION_1_1 ||
IdeDisk->udev.HwVersion == NDAS_VERSION_2_0
) {
lockIdx = DevLockIdMap[IdeDisk->udev.conn.unit][devLockControl->LockId];
} else {
lockIdx = DevLockIdMap_Adv[IdeDisk->udev.conn.unit][devLockControl->LockId];
}
//
// Execute the lock operation
//
switch(devLockControl->LockOpCode) {
case LURNLOCK_OPCODE_ACQUIRE: {
sal_msec timeOut;
if(devLockControl->AddressRangeValid) {
if(devLockControl->StartingAddress > devLockControl->EndingAddress) {
Ccb->CcbStatus = CCB_STATUS_INVALID_COMMAND;
return NDAS_OK;
}
}
if(LockCacheIsLost(&IdeDisk->udev, devLockControl->LockId)) {
//
// The lock lost. Clear lost status by releasing it.
//
// Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
Ccb->CcbStatus = CCB_STATUS_LOST_LOCK;
return NDAS_OK;
}
if (devLockControl->LockId == NDAS_DEVLOCK_ID_BUFFLOCK) {
sal_assert( LockCacheIsAcquired(&IdeDisk->udev,devLockControl->LockId) == FALSE );
}
//
// Release the buffer lock to prevent deadlock or race condition
// between the buffer and the others.
//
if(IdeDisk->BuffLockCtl.BufferLockConrol == TRUE) {
if (devLockControl->LockId != NDAS_DEVLOCK_ID_BUFFLOCK) {
status = NdasReleaseBufferLock(
&IdeDisk->BuffLockCtl,
IdeDisk->LanScsiSession,
&IdeDisk->udev,
NULL,
NULL,
TRUE,
0);
if(!NDAS_SUCCESS(status)) {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
return status;
}
}
}
timeOut = devLockControl->ContentionTimeOut;
status = OpAcquireDevLock(
IdeDisk->LanScsiSession,
lockIdx,
devLockControl->LockData,
&timeOut,
TRUE);
if(NDAS_SUCCESS(status)) {
Ccb->CcbStatus = CCB_STATUS_SUCCESS;
sal_assert( LockCacheIsAcquired(&IdeDisk->udev,devLockControl->LockId) == FALSE );
//
// The buffer lock acquired by outside of the IDE LURN.
// Hand in the buffer lock control to the outside of the IDE LURN.
// Turn off the collision control
//
if(devLockControl->LockId == NDAS_DEVLOCK_ID_BUFFLOCK && IdeDisk->BuffLockCtl.BufferLockConrol) {
debug_lock(1, "Bufflock control off.");
IdeDisk->BuffLockCtl.BufferLockConrol = FALSE;
}
//
// Acquire success
// Update the lock status
// Update the lock status only if previously we released the lock.
//
if(LockCacheIsAcquired(&IdeDisk->udev, devLockControl->LockId) == FALSE) {
LockCacheSetDevLockAcquisition(
&IdeDisk->udev,
devLockControl->LockId,
devLockControl->AddressRangeValid,
devLockControl->StartingAddress,
devLockControl->EndingAddress
);
}
} else if(status == NDAS_ERROR_ACQUIRE_LOCK_FAILED) {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
status = NDAS_OK;
} else {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
}
return status;
}
case LURNLOCK_OPCODE_RELEASE: {
if(LockCacheIsAcquired(&IdeDisk->udev, devLockControl->LockId) == FALSE) {
if(LockCacheIsLost(&IdeDisk->udev, devLockControl->LockId)) {
//
// Clear lost status
//
LockCacheClearDevLockLoss(&IdeDisk->udev, devLockControl->LockId);
Ccb->CcbStatus = CCB_STATUS_SUCCESS;
return NDAS_OK;
}
//
// Already released.
//
if (devLockControl->LockId == NDAS_DEVLOCK_ID_BUFFLOCK) {
sal_assert( FALSE );
}
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
return NDAS_OK;
}
sal_assert( LockCacheIsAcquired(&IdeDisk->udev,devLockControl->LockId) == TRUE );
//
// Release success
// Update the lock status only if previously we acquired the lock
// whether the release request succeeds or not.
//
if(LockCacheIsAcquired(&IdeDisk->udev, devLockControl->LockId)) {
LockCacheSetDevLockRelease(&IdeDisk->udev, devLockControl->LockId);
}
status = OpReleaseDevLock(
IdeDisk->LanScsiSession,
lockIdx,
devLockControl->LockData,
NULL);
if(NDAS_SUCCESS(status)) {
Ccb->CcbStatus = CCB_STATUS_SUCCESS;
} else {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
}
return status;
}
case LURNLOCK_OPCODE_QUERY_OWNER:
status = OpQueryDevLockOwner(
IdeDisk->LanScsiSession,
lockIdx,
devLockControl->LockData,
NULL);
if(NDAS_SUCCESS(status)) {
Ccb->CcbStatus = CCB_STATUS_SUCCESS;
} else {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
}
return status;
case LURNLOCK_OPCODE_GETDATA: // Not yet implemented
status = OpGetDevLockData(
IdeDisk->LanScsiSession,
lockIdx,
devLockControl->LockData,
NULL);
if(NDAS_SUCCESS(status)) {
Ccb->CcbStatus = CCB_STATUS_SUCCESS;
} else {
Ccb->CcbStatus = CCB_STATUS_COMMAND_FAILED;
}
return status;
case LURNLOCK_OPCODE_SETDATA: // Not yet implemented
case LURNLOCK_OPCODE_BREAK: // Not yet implemented
default: ;
Ccb->CcbStatus = CCB_STATUS_INVALID_COMMAND;
return NDAS_OK;
}
return status;
}
#endif
//////////////////////////////////////////////////////////////////////////
//
// Buffer lock intention
//
//
// Increase the buffer lock acquisition request count.
// This is expensive operation due to four NDAS requests of lock and target data.
// Avoid to call this function during the buffer lock acquisition.
//
static
ndas_error_t
QueueBufferLockRequest(
IN PBUFFLOCK_CONTROL BuffLockCtl,
IN uconn_t* conn,
IN NDAS_DEVLOCK_INFO* LockInfo,
IN sal_msec TimeOut
){
ndas_error_t status, release_status;
TEXT_TARGET_DATA targetData;
xuint32 lockIdx;
xuint32 devRequestCount;
//
// Map the lock ID.
//
lockIdx = LockIdToTargetLockIdx(
conn->hwdata->hardware_version,
conn->unit,
NDAS_DEVLOCK_ID_EXTLOCK);
//
// Acquire the extension lock.
//
if(LockCacheIsAcquired(LockInfo, NDAS_DEVLOCK_ID_EXTLOCK) == FALSE) {
status = OpAcquireDevLock(
conn,
lockIdx,
NULL,
TimeOut,
TRUE);
if(NDAS_ERROR_ACQUIRE_LOCK_FAILED != status && !NDAS_SUCCESS(status)) {
debug_lock(1, "OpAcquireDevLock() failed. STATUS=%08x", status);
return status;
}
LockCacheSetDevLockAcquisition(LockInfo, NDAS_DEVLOCK_ID_EXTLOCK, FALSE, 0, 0);
}
//
// Read the target data
//
status = conn_text_target_data(conn, FALSE, &targetData);
if(!NDAS_SUCCESS(status)) {
debug_lock(1, "conn_text_target_data() failed. STATUS=%08x", status);
goto exit;
}
//
// Set intention bit.
// Do not touch other bit fields.
//
devRequestCount = (xuint32)(targetData & TARGETDATA_REQUEST_COUNT_MASK);
// Update the intention count before increase.
BuffLockCtl->MyRequestCount ++;
devRequestCount ++;
devRequestCount &= TARGETDATA_REQUEST_COUNT_MASK;
targetData = (targetData & (~TARGETDATA_REQUEST_COUNT_MASK)) | devRequestCount;
//
// Write the target data
//
status = conn_text_target_data(conn, TRUE, &targetData);
if(!NDAS_SUCCESS(status)) {
debug_lock(1, "LspTextTartgetData() failed. STATUS=%08x", status);
goto exit;
}
debug_lock(1, "Request count=%u", devRequestCount);
exit:
//
// Release the extension lock.
// Do not override the status value.
//
release_status = OpReleaseDevLock(
conn,
lockIdx,
NULL,
TimeOut);
LockCacheSetDevLockRelease(LockInfo, NDAS_DEVLOCK_ID_EXTLOCK);
if(!NDAS_SUCCESS(release_status)) {
debug_lock(1, "OpReleaseDevLock() failed. STATUS=%08x", release_status);
}
return status;
}
//
// Retrieve the number of pending requests.
//
static
INLINE
void
UpdateRequestCount(
IN PBUFFLOCK_CONTROL BuffLockCtl,
IN xuint32 RequestCount,
OUT xulong* PendingRequests
){
xuint32 pendingRequests;
if(RequestCount >= BuffLockCtl->RequestCountWhenReleased)
pendingRequests = RequestCount - BuffLockCtl->RequestCountWhenReleased;
else
pendingRequests = RequestCount +
((TARGETDATA_REQUEST_COUNT_MASK+1) - BuffLockCtl->RequestCountWhenReleased);
#ifdef DEBUG
if(BuffLockCtl->RequestCountWhenReleased != RequestCount) {
debug_lock(4, "Request count = %d", pendingRequests);
}
#endif
//
// Set return value of the buffer lock request pending count.
//
if(PendingRequests)
*PendingRequests = pendingRequests;
}
#if 0 /* not used */
static
ndas_error_t
GetBufferLockPendingRequests(
IN PBUFFLOCK_CONTROL BuffLockCtl,
IN uconn_t* conn,
OUT xulong* PendingRequests,
IN sal_msec TimeOut
){
ndas_error_t status;
TEXT_TARGET_DATA targetData;
xuint32 requestCount;
status = conn_text_target_data(conn, FALSE, &targetData);
if(!NDAS_SUCCESS(status)) {
debug_lock(1, "LspTextTartgetData() failed. %d", status);
return status;
}
debug_lock(1, "TargetData:%lx", ((xuint64)targetData));
//
// Match the signature.
// If not match, it might be interference by anonymous application.
//
requestCount = (xuint32)(targetData & TARGETDATA_REQUEST_COUNT_MASK);
UpdateRequestCount(BuffLockCtl, requestCount, PendingRequests);
return status;
}
#endif
//
// Retrieve the numbers of pending requests, RW hosts, and RO hosts.
//
static
ndas_error_t
GetBufferLockPendingRequestsWithHostInfo(
IN PBUFFLOCK_CONTROL BuffLockCtl,
IN uconn_t* conn,
OUT xulong* RequestCounts,
OUT xulong* PendingRequests,
OUT xulong* RWHostCount,
OUT xulong* ROHostCount,
IN sal_msec TimeOut
){
ndas_error_t status;
lsp_text_target_list_t targetList;
lsp_text_target_list_element_t* targetEntry;
xuint32 idx_targetentry;
xbool found;
xuint32 requestCount;
status = conn_text_target_list(conn, &targetList, sizeof(lsp_text_target_list_t));
if(!NDAS_SUCCESS(status)) {
debug_lock(1, "conn_text_target_list() failed. %d", status);
return status;
}
sal_assert(targetList.number_of_elements <= 2);
found = FALSE;
for(idx_targetentry = 0;
idx_targetentry < MAX_TARGET_ID && idx_targetentry < targetList.number_of_elements;
idx_targetentry ++) {
targetEntry = &targetList.elements[idx_targetentry];
if(targetEntry->target_id == conn->unit) {
xuint64 targetdata;
// Network endian to host endian
targetdata = lsp_ntohll(*((xuint64*)&targetEntry->target_data));
requestCount = ((xuint32)targetdata & TARGETDATA_REQUEST_COUNT_MASK);
if(RequestCounts)
*RequestCounts = requestCount;
debug_lock(2, "TargetData:0x%llu intentionCount:%u In-mem:%u",
targetdata,
requestCount,
BuffLockCtl->RequestCountWhenReleased);
UpdateRequestCount(BuffLockCtl, requestCount, PendingRequests);
if(RWHostCount)
*RWHostCount = targetEntry->rw_hosts;
if(ROHostCount)
*ROHostCount = targetEntry->ro_hosts;
//
// Workaround for NDAS chip 2.0 original
// It does not return correct ReadOnly host count
//
if( conn->hwdata->hardware_version == LSP_HARDWARE_VERSION_2_0 &&
conn->hwdata->hardware_revision== LSP_HARDWARE_V20_REV_0) {
if(RWHostCount)
*RWHostCount = 1;
if(ROHostCount)
*ROHostCount = NDAS_MAX_CONNECTION_COUNT - 1;
}
found = TRUE;
}
}
if(found == FALSE) {
return NDAS_ERROR_NO_DEVICE;
}
return status;
}