-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnddev.c
More file actions
1343 lines (1188 loc) · 38.5 KB
/
nddev.c
File metadata and controls
1343 lines (1188 loc) · 38.5 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
/**********************************************************************
* Copyright ( C ) 2012 IOCELL Networks
* All rights reserved.
**********************************************************************/
#include "linux_ver.h"
#include "inc/xplatcfg.h"
#include "inc/sal/sal.h"
#include "inc/sal/libc.h"
#include "inc/sal/sync.h"
#include "inc/sal/thread.h"
#include "inc/sal/debug.h"
#include "inc/sal/mem.h"
#include "inc/sal/types.h"
#include "inc/xlib/dpc.h"
#include "inc/xlib/xhash.h"
#include "inc/xlib/xbuf.h"
#include "inc/xlib/gtypes.h" /* g_ntohl g_htonl g_ntohs g_htons */
#include "inc/ndasuser/info.h"
#include "inc/ndasuser/ndasuser.h"
#include "inc/ndasuser/write.h"
#include "inc/lpx/lpx.h"
#include "inc/lpx/lpxutil.h"
#include "inc/netdisk/serial.h"
#include "inc/netdisk/netdisk.h"
#include "inc/netdisk/netdiskid.h"
#include "inc/netdisk/list.h"
#include "inc/netdisk/ndasdib.h"
#include "inc/netdisk/ndev.h"
#include "ndiod.h"
#include "ndpnp.h"
#include "registrar.h"
#include "inc/netdisk/conn.h"
#include "inc/netdisk/sdev.h"
#include "udev.h"
#include "ndpnp.h"
#define NDAS_PROBING_TIMEOUT 15*1000
#define NDAS_PNP_DEFAULT_TIMEOUT 10*1000
#define NDAS_REGISTER_DEFAULT_TIMEOUT 30*1000
#ifndef NDAS_NO_LANSCSI
#ifdef XPLAT_RAID
#include "raid/raid.h"
#ifndef XPLAT_ASYNC_IO
#error Asynchronous I/O should be enabled to support NDAS BIND
#endif
#endif
#define STAT_NDIOD_INTERVAL (10 * SAL_TICKS_PER_SEC)
#define TH(i) ((i) == 1?"st":(i) == 2?"nd":(i) == 3?"rd":"th")
#ifdef DEBUG
#define debug_nddev(l, x...) do {\
if(l <= DEBUG_LEVEL_NDDEV) { \
sal_debug_print("ND|%d|%s|",l,__FUNCTION__); \
sal_debug_println(x); \
} \
} while(0)
#else
#define debug_nddev(l, x...) do {} while(0)
#endif
void * Urb_pool = NULL;
ndas_error_t
ndas_block_core_init(void)
{
Urb_pool = sal_create_mem_pool("ndas_urb", sizeof(struct udev_request_block));
if(Urb_pool == NULL)
return NDAS_ERROR_OUT_OF_MEMORY;
return NDAS_OK;
}
ndas_error_t
ndas_block_core_cleanup(void)
{
if(Urb_pool)
sal_destroy_mem_pool(Urb_pool);
return NDAS_OK;
}
static ndas_error_t ndas_discover_device(const char* name);
/* return TRUE for valid key
* Parameters
* ndas_idserial: the NDAS id to read the NDAS device(aka read key)
* ndas_key: the NDAS key to write the NDAS device (aka write key)
* ndas_device_id: [out] the NDAS device id (currently mac address)
* writable: [out] whether the device can be written.
*/
LOCAL ndas_error_t
get_network_id(const char* ndas_id, const char* ndas_key, xuchar* network_id, int* writable)
{
ndas_id_info info;
debug_nddev(3, "ing: %s %s", ndas_id, ndas_key);
//
// inputs for decryption
//
// NDAS ID / NDAS Key(Write key)
sal_memcpy(info.ndas_id[0], &ndas_id[0], 5);
sal_memcpy(info.ndas_id[1], &ndas_id[5], 5);
sal_memcpy(info.ndas_id[2], &ndas_id[10], 5);
sal_memcpy(info.ndas_id[3], &ndas_id[15], 5);
if (ndas_key) {
sal_memcpy(info.ndas_key, ndas_key, 5);
} else {
sal_memset(info.ndas_key, 0, 5);
}
//
// two keys for decryption
//
sal_memcpy(info.key1, NDIDV1Key1, 8) ;
sal_memcpy(info.key2, NDIDV1Key2, 8) ;
if(DecryptNdasID(&info) == TRUE) {
#ifdef NDAS_CRYPTO
if (info.vid == NDIDV1VID_NETCAM &&
info.reserved[0] == NDIDV1Rsv[0] &&
info.reserved[1] == NDIDV1Rsv[1] &&
info.random == NDIDV1Rnd)
{
dbg_calln(9, "valid id&key(NetCam");
/* Key is correct. */
if ( network_id )
memcpy(network_id, info.ndas_network_id, 6);
if ( writable )
*writable = info.bIsReadWrite ;
return NDAS_OK;
}
#else
if (info.vid == NDIDV1VID_DEFAULT &&
info.reserved[0] == NDIDV1Rsv[0] &&
info.reserved[1] == NDIDV1Rsv[1] &&
info.random == NDIDV1Rnd)
{
debug_nddev(9, "valid id&key");
/* Key is correct. */
if ( network_id )
sal_memcpy(network_id, info.ndas_network_id, 6);
if ( writable )
*writable = info.bIsReadWrite ;
return NDAS_OK;
} else if (info.vid == NDIDV1VID_SEAGATE &&
info.reserved[0] == NDIDV1Rsv[0] &&
info.reserved[1] == NDIDV1Rsv[1] &&
info.random == NDIDV1Rnd)
{
debug_nddev(9, "valid id&key(seagate");
/* Key is correct. */
if ( network_id )
sal_memcpy(network_id, info.ndas_network_id, 6);
if ( writable )
*writable = info.bIsReadWrite ;
return NDAS_OK;
}
#endif
}
return NDAS_ERROR_INVALID_NDAS_ID;
}
#ifdef XPLAT_RAID
LOCAL void raid_set_slot_number(logunit_t *raid, int *slots)
{
int i;
for (i = 0; i < SDEV_RAID_INFO(raid)->disks; i++)
{
SDEV_RAID_INFO(raid)->slots[i] = slots[i];
}
}
LOCAL int raid_update(logunit_t *raid, void *arg)
{
int try_lock = (int)arg;
int i;
xbool has_key = TRUE;
debug_nddev(4, "raid=%p try_lock=%d", raid, try_lock);
if ( try_lock )
SDEV_REENTER(raid, raid_update, (void *)raid, (void *)FALSE);
/* Update informations */
debug_nddev(4, "disks=%d", SDEV_RAID_INFO(raid)->disks);
for (i = 0; i < SDEV_RAID_INFO(raid)->disks; i++)
{
logunit_t *single = raid->disks[i];
if ( !single && SDEV_RAID_INFO(raid)->slots[i] >= NDAS_FIRST_SLOT_NR )
{
debug_nddev(1, "Discovered slot=%d index=%d", SDEV_RAID_INFO(raid)->slots[i], i);
single = sdev_lookup_byslot(SDEV_RAID_INFO(raid)->slots[i]);
raid->disks[i] = single;
}
debug_nddev(4, "disks[%d]=%p parent_slot=%d", i,single, raid->info.slot);
if ( !single )
continue;
debug_nddev(4, "disks[%d]-info2=%p parent_slot=%d", i,
SDEV_UNIT_INFO(single), raid->info.slot);
SDEV_UNIT_INFO(single)->raid_slot = raid->info.slot;
has_key &= single->has_key;
}
raid->has_key = has_key;
return 0;
}
#if 0
LOCAL ndas_error_t raid_detect(logunit_t *single, xbool pnp)
{
unsigned int i;
int slots[8];
ndas_error_t err;
logunit_t *raid = NULL;
ndas_metadata_t *nmd;
NDAS_DIB_V2 *dib;
udev_t* udev = SDEV2UDEV(single);
xbool raid_is_locked = FALSE;
debug_nddev(3, "single=%p", single);
nmd = nmd_create();
if ( !nmd ) return NDAS_ERROR_OUT_OF_MEMORY;
err = udev_query_unit(udev, nmd);
if ( !NDAS_SUCCESS(err) ) {
debug_nddev(1, "ed out=%d", err);
if ( err == NDAS_ERROR_TIME_OUT )
err = NDAS_ERROR_NO_DEVICE;
goto free_nmd;
}
dib = nmd->dib;
sal_assert(dib);
debug_nddev(5, "mode=%d", single->info.mode);
if ( single->info.mode == NDAS_DISK_MODE_SINGLE ) {
err = NDAS_OK;
goto free_nmd;
}
sal_assert(sal_memcmp(dib->u.s.Signature, NDAS_DIB_V2_SIGNATURE,8) == 0);
debug_nddev(5, "nDiskCount=%d", dib->u.s.nDiskCount);
for ( i = 0; i < dib->u.s.nDiskCount; i++)
{
ndev_t* ndev = ndev_lookup_bynetworkid(dib->UnitDisks[i].MACAddr);
if ( !ndev || ndev->info.nr_unit <= dib->UnitDisks[i].UnitNumber)
{
debug_nddev(1, "not found %s", DBG_DIB_UNITDISK(dib->UnitDisks + i));
slots[i] = NDAS_INVALID_SLOT;
continue;
}
slots[i] = ndev->unit[dib->UnitDisks[i].UnitNumber]->info.slot;
single = sdev_lookup_byslot(slots[i]);
if ( !single ) {
debug_nddev(2,"child slot(%d) not found", slots[i]);
continue;
}
sal_assert(slots[i] == single->info.slot);
if ( single->info.mode != NDAS_DISK_MODE_RAID0
&& single->info.mode != NDAS_DISK_MODE_RAID1
&& single->info.mode != NDAS_DISK_MODE_RAID4
&& single->info.mode != NDAS_DISK_MODE_MIRROR
&& single->info.mode != NDAS_DISK_MODE_AGGREGATION )
{
debug_nddev(2,"slot mode is not raid but %d", single->info.mode);
err = NDAS_ERROR_INVALID_METADATA;
goto free_nmd;
}
if ( single->info.type != NDAS_UNIT_TYPE_HARD_DISK )
{
debug_nddev(2,"slot type is not HDD but %d", single->info.type);
err = NDAS_ERROR_INTERNAL;
goto free_nmd;
}
debug_nddev(4, "slot[%d]=%d{%s}", i, slots[i],
DBG_DIB_UNITDISK(dib->UnitDisks + i));
if ( SDEV_UNIT_INFO(single)->raid_slot <= NDAS_INVALID_SLOT) {
debug_nddev(2,"parent slot for %d not exists", slots[i]);
continue;
}
debug_nddev(2,"parent slot (%d) exist", SDEV_UNIT_INFO(single)->raid_slot);
raid = sdev_lookup_byslot(SDEV_UNIT_INFO(single)->raid_slot);
}
if ( !raid )
{
sdev_create_param_t param;
param.mode = single->info.mode;
param.u.raid.disk_count = dib->u.s.nDiskCount;
sal_memcpy(param.u.raid.slots, slots, sizeof(slots));
raid = sdev_create(¶m);
if ( !raid ) {
err = NDAS_ERROR_OUT_OF_MEMORY;
goto free_nmd;
}
if ( !pnp )
sdev_unlock(raid);
else
raid_is_locked = TRUE;
} else
raid_set_slot_number(raid, slots);
if ( raid_is_locked )
raid_update((void *)raid, (void *)FALSE);
else
if ( pnp )
raid_update((void *)raid, (void *)TRUE);
else
raid_update((void *)raid, (void *)FALSE);
debug_nddev(4, "mode=%d", single->info.mode);
debug_nddev(4, "nr_disks=%d", dib->u.s.nDiskCount);
err = NDAS_OK;
free_nmd:
nmd_destroy(nmd);
return err;
}
#endif
#endif
#ifdef XPLAT_RAID
int ndev_free_raid_(logunit_t *raid, logunit_t *sdev)
{
int i, count = 0;
debug_nddev(1,"raid slot=%d sdev slot=%d", raid->info.slot, sdev->info.slot);
SDEV_REENTER(raid, ndev_free_raid_, (void *)raid, (void *)sdev);
for ( i = 0; i < SDEV_RAID_INFO(raid)->disks; i++) {
if ( raid->disks[i] == sdev ) {
debug_nddev(1, "removed slot=%d from raid slot %d",sdev->info.slot, raid->info.slot);
raid->disks[i] = NULL;
SDEV_RAID_INFO(raid)->slots[i] = NDAS_INVALID_SLOT;
}
if ( raid->disks[i] != NULL )
count++;
}
if ( count == 0 ) {
debug_nddev(1, "All slot is remove from raid %d, cleaning", raid->info.slot);
sdev_do_cleanup(raid);
} else {
sdev_unlock(sdev);
}
return 0;
}
void ndev_free_raid(logunit_t *sdev)
{
logunit_t *raid = sdev_lookup_byslot(SDEV_UNIT_INFO(sdev)->raid_slot);
if ( !raid ) {
debug_nddev(1, "No raid sdev is found for slot %d", SDEV_UNIT_INFO(sdev)->raid_slot);
return;
}
ndev_free_raid_(raid, sdev);
}
#endif
/*
* Destroy the unit disk's sdev object.
* Called if
* 1. ndev is unregistered (but still in the registrar for probe list)
* 2. ndev_cleanup
*/
void ndev_unit_cleanup(ndev_t *ndev)
{
int unit;
debug_nddev(3, "ndev=%p",ndev);
for (unit = 0; unit < ndev->info.nr_unit; unit++)
{
logunit_t * sdev = ndev->unit[unit];
if ( sdev == NULL ) {
debug_nddev(1, "sdev=NULL:fix me");
continue;
}
debug_nddev(1, "sdev slot=%d",ndev->unit[unit]->info.slot);
#ifdef XPLAT_RAID
if ( slot_is_the_raid(&sdev->info) ) {
ndev_free_raid(sdev);
}
#else
#endif
sdev_cleanup(sdev, NULL);
ndev->unit[unit] = NULL;
}
}
void ndev_cleanup(ndev_t *ndev)
{
ndev_unit_cleanup(ndev);
#if 0
debug_nddev(5, "ndev->break_lock=%p",ndev->break_lock);
if ( ndev->break_lock != SAL_INVALID_SEMAPHORE ) {
sal_semaphore_destroy(ndev->break_lock);
} else {
// There should be ndev->break_lock
sal_assert(0);
}
#endif
// xlib_hash_table_remove(v_slot.ndasid_2_device, ndev->info.ndas_idserial);
// m2d_remove(ndev->network_id, FALSE);
sal_free(ndev);
}
LOCAL int ndev_reenter_offline_notify(logunit_t *sdev, void * arg)
{
long slot = (long)arg;
SDEV_REENTER(sdev, ndev_reenter_offline_notify, sdev, (void*) slot);
if ( sdev->ops->changed )
sdev->ops->changed(sdev, (int)slot, 0);
else
sdev_unlock(sdev);
return 0;
}
LOCAL int ndev_reenter_online_notify(logunit_t *sdev, void * arg)
{
long slot = (long)arg;
SDEV_REENTER(sdev, ndev_reenter_online_notify, sdev, (void*) slot);
if ( sdev->ops->changed )
sdev->ops->changed(sdev, (int)slot, 1);
else
sdev_unlock(sdev);
return 0;
}
#ifdef XPLAT_PNP
LOCAL void ndev_pnp_offline(ndev_t* ndev)
{
int i;
logunit_t *raid_sdev = NULL;
for ( i = 0; i < ndev->info.nr_unit; i++)
{
logunit_t *sdev;
debug_nddev(1, "slot=%d", ndev->info.slot[i]);
sdev = sdev_lookup_byslot(ndev->info.slot[i]);
if ( !sdev ) continue;
debug_nddev(1, "mode=%d", sdev->info.mode);
if ( !slot_is_the_raid(&sdev->info) )
continue;
raid_sdev = sdev_lookup_byslot(SDEV_UNIT_INFO(sdev)->raid_slot);
if ( !raid_sdev) continue;
if ( raid_sdev->ops->changed ) {
ndev_reenter_offline_notify(raid_sdev, (void *)(long)ndev->info.slot[i]);
return;
}
// rddc_device_changed
if ( raid_sdev->ops->changed )
raid_sdev->ops->changed(raid_sdev, ndev->info.slot[i], 0);
}
}
LOCAL void ndev_pnp_changed(const char *serial, const char *name, NDAS_DEV_STATUS online)
{
ndas_error_t err;
ndev_t* ndev = ndev_lookup_byserial(serial);
debug_nddev(3, "serial=%s online=%d", serial, online);
if ( !ndev ) {
debug_nddev(1, "not registered");
return;
}
if ( name == NULL ) {
/* Currently auto-registration is not implemented */
return;
}
debug_nddev(1, "ndas: registered device \"%s\" is status %d",name, online);
if ( online == NDAS_DEV_STATUS_OFFLINE) {
debug_nddev(1, "ndas: device became offline");
ndev_pnp_offline(ndev);
return;
}
err = ndas_discover_device(name);
if (!NDAS_SUCCESS(err)) {
debug_nddev(1, "Failed to discover device %s", name);
return;
}
}
#endif
static ndas_error_t
ndev_register_device(ndev_t* ndev, xuint16 enable_bits)
{
ndas_error_t err;
debug_nddev(1, "ndev=%p network_id=%s",ndev, ndev->network_id);
ndev->enable_flags = enable_bits;
err = ndev_register(ndev);
if ( !NDAS_SUCCESS(err) ) {
debug_nddev(1, "clean up ndev");
ndev_cleanup(ndev);
return err;
}
#ifdef XPLAT_PNP
pnp_subscribe(ndev->info.ndas_serial, ndev_pnp_changed);
#endif
/*
To do:
This is temp fix for start-up enabling. Fix to handle start-up enabling in user-side.
*/
if (enable_bits) {
//
// Make status change handler is called and device is discovered.
//
ndev_notify_status_changed(ndev, NDAS_DEV_STATUS_UNKNOWN);
}
return NDAS_OK;
}
ndas_error_t register_internal(const char* name, const char *idserial, const char *ndas_key, xuint32 enable_bits, xbool use_serial)
{
xuchar network_id[SAL_ETHER_ADDR_LEN];
ndas_error_t err;
int writable = 0;
ndev_t* ndev;
if ( !name || !name[0] || sal_strnlen(name, NDAS_MAX_NAME_LENGTH) > NDAS_MAX_NAME_LENGTH )
return NDAS_ERROR_INVALID_NAME;
if ( !idserial )
return NDAS_ERROR_INVALID_NDAS_ID;
if ( ndev_lookup_byname(name) ) {
return NDAS_ERROR_ALREADY_REGISTERED_NAME;
}
if (use_serial) {
err = get_networkid_from_serial(network_id, (const char*)idserial);
/* Do not allow writing when registerd with serial */
writable = FALSE;
} else {
err = get_network_id(idserial, ndas_key, network_id, &writable);
}
if ( !NDAS_SUCCESS(err) ) {
return err;
}
err = ndev_lookup(network_id, &ndev);
if ( !NDAS_SUCCESS(err) ) {
return err;
}
sal_assert(ndev);
if ( ndev->info.name[0] ) {
return NDAS_ERROR_ALREADY_REGISTERED_DEVICE;
}
sal_strncpy(ndev->info.name, name, NDAS_MAX_NAME_LENGTH);
if ( writable ) {
sal_strncpy(ndev->info.ndas_key, ndas_key, NDAS_KEY_LENGTH);
} else {
sal_memset(ndev->info.ndas_key,0,NDAS_KEY_LENGTH);
}
err = ndev_register_device(ndev, enable_bits);
return err;
}
NDASUSER_API ndas_error_t
ndas_register_device(const char* name, const char *id, const char *ndas_key)
{
return register_internal(name, id, ndas_key, 0, FALSE);
}
NDASUSER_API
ndas_error_t
ndas_register_device_by_serial(const char* name, const char *serial)
{
return register_internal(name, serial, NULL, 0, TRUE);
}
NDASUSER_API ndas_error_t
ndas_unregister_device(const char *name)
{
int i = 0;
ndev_t* ndev = ndev_lookup_byname(name);
if ( !ndev )
return NDAS_ERROR_INVALID_NAME;
for (i = 0; i < ndev->info.nr_unit; i++)
{
if ( ndev->unit[i] && ndev->unit[i]->info.enabled ) {
return NDAS_ERROR_ALREADY_ENABLED_DEVICE;
}
}
#ifdef XPLAT_PNP
pnp_unsubscribe(ndev->info.ndas_serial, TRUE);
#endif
ndev_unregister(ndev);
ndev_unit_cleanup(ndev);
return NDAS_OK;
}
struct enable_arg {
int *wait;
ndas_error_t err;
};
LOCAL NDAS_CALL void
enable_internal_done(int slot, ndas_error_t err, void *arg)
{
struct enable_arg *earg = (struct enable_arg *)arg;
logunit_t * lunit;
debug_nddev(1,"slot=%d err=%d", slot, err);
earg->err = err;
(*earg->wait) = 0;
lunit = sdev_lookup_byslot(slot);
debug_nddev(1,"lunit=%d", lunit->info.slot);
// sdev_unlock(lunit);
}
/*
* Hold the thread until the flag is reset
*/
int int_wait(int *flag, sal_msec interval, int count)
{
int c = 0;
while((*flag) == 1) { // use polling to reduce semaphore usage count
sal_msleep(interval);
if ( c++ > count ) {
return -1;
}
}
return 0;
}
/*
* Enable the slot with given parameters.
*
* Called by ndas_enable_slot funtions (user thread)
*/
LOCAL
ndas_error_t
enable_internal(int slot,
xbool exclusive_writable,
xbool writeshare)
{
int wait = 0;
struct enable_arg earg = {&wait,NDAS_OK};
logunit_t * sdev = (logunit_t *) sdev_lookup_byslot(slot);
debug_nddev(3, "sdev=%p", sdev);
if ( sdev == NULL ) {
debug_nddev(1, "invalid slot");
return NDAS_ERROR_INVALID_SLOT_NUMBER;
}
if ( slot_is_the_raid(&sdev->info) ) {
debug_nddev(1, "Disk is raid member!!!");
return NDAS_ERROR_UNSUPPORTED_DISK_MODE;
}
/* If device is locked, it is being enabled/disabled */
if ( !sdev_lock(sdev)) {
/* Device may be in initializing or not yet discovered. */
debug_nddev(1, "sdev is locked!!!");
return NDAS_ERROR_TRY_AGAIN;
}
debug_nddev(1, "sdev=%p slot=%d", sdev, slot);
if ( sdev->info.enabled ) {
debug_nddev(1, "already enabled");
sdev_unlock(sdev);
return NDAS_ERROR_ALREADY_ENABLED_DEVICE;
}
if ((exclusive_writable || writeshare) && sdev->has_key==FALSE) {
debug_nddev(1, "No NDAS key:writemode=%d writeshare=%d has_key=%d"
, exclusive_writable, writeshare, sdev->has_key);
sdev_unlock(sdev);
return NDAS_ERROR_NO_WRITE_ACCESS_RIGHT;
}
/* Don't fire disable listener as the slot has not yet been enabled */
sdev->notify_enable_result_on_disabled = FALSE;
if ( !sdev->enabled_func ) {
debug_nddev(1, "No enabled_func set. Using enable_internal_done instead");
sdev->enabled_func = enable_internal_done;
sdev->arg = &earg;
wait = 1;
}
/* uop_enable, jbod_op_enable, rddc_op_enable */
sdev->ops->enable(sdev, ENABLE_FLAG(exclusive_writable, writeshare, FALSE , FALSE));
/* sdev will be unlocked by enable notifying function or in error cases */
if ( int_wait(&wait, 1000, 20) == -1 ) {
sdev_unlock(sdev);
return NDAS_ERROR_TIME_OUT;
}
return earg.err;
}
/*
Write-share version of ndas_enable_slot. In this mode, writing can be executed in read-only connection.
And writing is executed only when NDAS device lock is held.(not implemented)
*/
NDASUSER_API ndas_error_t ndas_enable_writeshare(int slot)
{
return enable_internal(slot, FALSE, TRUE);
}
NDASUSER_API ndas_error_t ndas_enable_slot(int slot)
{
return enable_internal(slot, FALSE, FALSE);
}
NDASUSER_API ndas_error_t ndas_enable_exclusive_writable(int slot)
{
return enable_internal(slot, TRUE, FALSE);
}
/**
* Description
* Request to disable the NDAS unit device enabled by ndas_enable_slot function.
* The result will be signalled by the disconnected handler
*
* Parameters
* slot: NDAS device slot number, allocated when ndas_register_device is called
*/
NDASUSER_API ndas_error_t ndas_disable_slot(int slot)
{
logunit_t * sdev = sdev_lookup_byslot(slot);
if ( sdev == NULL ) {
debug_nddev(1, "Invalid slot number %d\n", slot);
return NDAS_ERROR_INVALID_SLOT_NUMBER;
}
debug_nddev(3, "slot=%d",slot);
if ( slot_is_the_raid(&sdev->info) ) {
return NDAS_ERROR_UNSUPPORTED_DISK_MODE;
}
if ( !sdev->info.enabled ) {
debug_nddev(1, "Trying to disable disabled slot");
return NDAS_ERROR_ALREADY_DISABLED_DEVICE; // todo:
}
sdev_disable(sdev, 0);
return NDAS_OK;
}
NDASUSER_API ndas_error_t
ndas_query_unit(int slot, ndas_unit_info_t* info)
{
logunit_t *sdev = sdev_lookup_byslot(slot);
debug_nddev(3, "slot=%d sdev=%p", slot, sdev);
if ( sdev == NULL ) {
debug_nddev(1, "invalid slot=%d", slot);
return NDAS_ERROR_INVALID_SLOT_NUMBER;
}
sal_memcpy(info, SDEV_UNIT_INFO(sdev), sizeof(ndas_unit_info_t));
return NDAS_OK;
}
/*
* Obtain the information about the ndas device registered.
* Parameters
* slot: NDAS device slot number for the NDAS device in the NDAS device registrar
* info: [out] information about the ndas device identified by physical_slot
*/
NDASUSER_API ndas_error_t
ndas_query_slot(int slot, ndas_slot_info_t* info)
{
// ndas_error_t err;
logunit_t *sdev = sdev_lookup_byslot(slot);
debug_nddev(3, "slot=%d sdev=%p", slot, sdev);
if ( sdev == NULL ) {
debug_nddev(1, "invalid slot=%d", slot);
return NDAS_ERROR_INVALID_SLOT_NUMBER;
}
/*
To do: Actual informatin retrieval from device should be done another function.
For timebeing, device/slot/unit information is already retreived in ndas_discover_device.
*/
#if 0
/* uop_query, r0op_query, r1op_query, r5op_query*/
err = sdev->ops->query(sdev, sdev->nmd);
if ( !NDAS_SUCCESS(err) )
return err;
#endif
sal_assert(info!=&sdev->info);
sal_memcpy(info, &sdev->info, sizeof(ndas_slot_info_t));
if (sdev->info.mode == NDAS_DISK_MODE_SINGLE) {
/* Adjust user accessible area */
/* To do: seperate internal ndas_slot_info and user ndas_slot_info */
info->sectors = sdev->info.sectors - NDAS_BLOCK_SIZE_XAREA;
info->capacity = info->sectors * sdev->info.sector_size;
}
debug_nddev(3, "ed");
return NDAS_OK;
}
NDASUSER_API ndas_error_t
ndas_read(int slot, ndas_io_request_ptr req)
{
return ndas_io(slot, ND_CMD_READ, req, TRUE);
}
NDASUSER_API ndas_error_t
ndas_write(int slot, ndas_io_request_ptr ioreq)
{
return ndas_io(slot, ND_CMD_WRITE, ioreq, TRUE);
}
NDASUSER_API ndas_error_t
ndas_flush(int slot, ndas_io_request_ptr req)
{
return ndas_io(slot, ND_CMD_FLUSH, req, TRUE);
}
NDASUSER_API ndas_error_t
ndas_packetcmd(int slot, ndas_io_request_ptr req)
{
return ndas_io_pc(slot, req, TRUE);
}
NDASUSER_API
ndas_error_t
ndas_set_slot_handlers(int slot,
ndas_io_done enabled_func,
ndas_io_done disabled_func,
ndas_permission_request_handler write_func,
void* arg)
{
logunit_t * sdev = sdev_lookup_byslot(slot);
if ( sdev == NULL ) {
return NDAS_ERROR_INVALID_SLOT_NUMBER;
}
sdev->enabled_func = enabled_func;
sdev->disabled_func = disabled_func;
#ifdef XPLAT_NDASHIX
sdev->surrender_request_handler = write_func;
#endif
sdev->arg = arg;
if ( slot_is_the_raid(&sdev->info) )
return NDAS_OK;
debug_nddev(1,"mode=0x%x", sdev->info.mode);
#if 0
if ( !slot_is_the_raid(&sdev->info) ) {
enabled_bits = ( SDEV2UDEV(sdev)->ndev->enable_flags >> (sdev->info.unit*2)) & REGDATA_BIT_MASK;
} else {
// TODO: think about enabling the raid on boot time.
}
debug_nddev(1,"enabled_bits=0x%x", enabled_bits);
if ( enabled_func ) {
switch ( enabled_bits ) {
case REGDATA_BIT_SHARE_ENABLED:
ndas_enable_writeshare(slot);
break;
case REGDATA_BIT_WRITE_ENABLED:
ndas_enable_exclusive_writable(slot);
break;
case REGDATA_BIT_READ_ENABLED:
ndas_enable_slot(slot);
break;
}
}
#endif
return NDAS_OK;
}
NDASUSER_API
ndas_error_t
ndas_detect_device(const char* name)
{
ndev_t* ndev;
ndev = ndev_lookup_byname(name);
if (ndev) {
ndev_notify_status_changed(ndev, NDAS_DEV_STATUS_UNKNOWN);
return NDAS_OK;
} else {
return NDAS_ERROR_INVALID_NAME;
}
}
/*
Get device information such as number of unit, DIB
*/
static
ndas_error_t
ndas_discover_device(const char* name)
{
ndas_error_t err = NDAS_OK;
int i = 0;
ndev_t* ndev;
uconn_t conn[2] = {{0}}; // We need seperate connection to get each inform from each disk.
udev_t * udev;
logunit_t* ldev;
ndas_slot_info_t* sinfo;
const lsp_ata_handshake_data_t* hsdata;
sdev_create_param_t sparam;
lsp_text_target_list_t targetList;
debug_nddev(1, "Discovering %s..", name);
ndev = ndev_lookup_byname(name);
if ( ndev == NULL )
{
debug_nddev(1, "NDAS_ERROR_INVALID_NAME name=%s", name);
return NDAS_ERROR_INVALID_NAME;
}
debug_nddev(5, "ndev=%p",ndev);
/*
Connect to device and
update device info and slot info
*/
err = conn_init(&conn[0], ndev->network_id, 0, NDAS_CONN_MODE_READ_ONLY, NULL, NULL);
if ( !NDAS_SUCCESS(err)) {
/* This does not happen */
return err;
}
err = conn_init(&conn[1], ndev->network_id, 1, NDAS_CONN_MODE_READ_ONLY, NULL, NULL);
if ( !NDAS_SUCCESS(err)) {
conn_clean(&conn[0]);
/* This does not happen */
return err;
}
err = conn_connect(&conn[0], NDAS_PROBING_TIMEOUT);
if ( !NDAS_SUCCESS(err)) {
debug_nddev(1, "Connect failed");
/* Connect failed */
ndev->info.version = NDAS_VERSION_UNKNOWN;
ndev->info.nr_unit = 0;
/*
* Determine proper status
*/
#ifdef XPLAT_PNP
if (sal_tick_sub(sal_get_tick() , ndev->last_tick + PNP_ALIVE_TIMEOUT) <= 0) {
//
// PNP packet is received recently.
//
if (NDAS_ERROR_CONNECT_FAILED == err) {
ndev->info.status = NDAS_DEV_STATUS_CONNECT_FAILURE;
} else {
ndev->info.status = NDAS_DEV_STATUS_LOGIN_FAILURE;
}
} else {
ndev->info.status = NDAS_DEV_STATUS_OFFLINE;
}
#else
if (NDAS_ERROR_CONNECT_FAILED == err) {
ndev->info.status = NDAS_DEV_STATUS_OFFLINE;
} else {
ndev->info.status = NDAS_DEV_STATUS_LOGIN_FAILURE;
}
#endif
conn_clean(&conn[0]);
conn_clean(&conn[1]);
return NDAS_ERROR;
}
/* Connection is successful */
#ifdef XPLAT_PNP
ndev->last_tick = sal_get_tick();
#endif
ndev->info.version = conn[0].hwdata->hardware_version;
ndev->info.status = NDAS_DEV_STATUS_HEALTHY;
err = conn_text_target_list(&conn[0], &targetList, sizeof(lsp_text_target_list_t));
if ( !NDAS_SUCCESS(err)) {
debug_nddev(1, "Text target list failed");
ndev->info.status = NDAS_DEV_STATUS_TARGET_LIST_FAILURE;
conn_disconnect(&conn[0]);
conn_clean(&conn[0]);
conn_clean(&conn[1]);