-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathndasuser.c
More file actions
537 lines (494 loc) · 14.6 KB
/
ndasuser.c
File metadata and controls
537 lines (494 loc) · 14.6 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
#include "linux_ver.h"
#include "inc/sal/libc.h"
#include "inc/sal/debug.h"
//#include "inc/lpx/lpxutil.h"
#include "inc/netdisk/netdisk.h"
#include "inc/ndasuser/ndasuser.h"
#include "inc/ndasuser/persist.h"
#include "inc/netdisk/serial.h"
#include "inc/netdisk/netdiskid.h"
#include "inc/xplatcfg.h"
#include "inc/xlib/dpc.h"
#include "ndiod.h"
#include "registrar.h"
#include "ndiod.h"
#include "ndpnp.h"
#include "nhix.h"
#include "registrar.h"
#include "udev.h"
#ifdef DEBUG
#define debug_ndasuser(l,x...) do { \
if ( l <= DEBUG_LEVEL_LDASUSER ) \
sal_debug_print("NU|%d|%s|",l,__FUNCTION__);\
sal_debug_println(x);\
} while(0)
#else
#define debug_ndasuser(l,fmt...)
#endif
#define XLIB_DPC_COUNT 150
#define DEFAULT_XBUF_NUM 64
#define DEFAULT_MAX_SLOT 16
// FIXME - like this really matters... gregkh
#define NDAS_VER_MAJOR 1
#define NDAS_VER_MINOR 1
#define NDAS_VER_BUILD 1
NDASUSER_API ndas_error_t ndas_get_version(int* major, int* minor, int* build)
{
/* Temporary: hardcode version number. Version scheme will be changed */
*major = NDAS_VER_MAJOR;
*minor = NDAS_VER_MINOR;
*build = NDAS_VER_BUILD;
return NDAS_OK;
}
NDASUSER_API ndas_error_t
ndas_init(
int io_unit, // KB
int sock_buffer_size, // KB
int reserved,
int max_slot)
{
ndas_error_t ret;
if ( io_unit <= 0 ) io_unit = DEFAULT_XBUF_NUM;
if ( sock_buffer_size <= 0 ) sock_buffer_size = DEFAULT_XBUF_NUM;
if ( max_slot <= 0) max_slot = DEFAULT_MAX_SLOT;
// sal_error_print("\nndas: Initializing NDAS driver version %d.%d.%d\n",
// NDAS_VER_MAJOR, NDAS_VER_MINOR, NDAS_VER_BUILD);
ret = dpc_start();
#ifdef XPLAT_BPC
ret = bpc_start();
#endif
ret = lpx_init(sock_buffer_size);
if ( !NDAS_SUCCESS(ret) ) {
goto out1;
}
// sal_error_print("ndas: Setting max request size to %dkbytes\n", io_unit);
#ifndef NDAS_NO_LANSCSI
ret = ndas_block_core_init();
if ( !NDAS_SUCCESS(ret) ) {
goto out1;
}
udev_set_max_xfer_unit(io_unit*1024);
ret = registrar_init(max_slot);
#endif
return NDAS_OK;
out1:
dpc_stop();
return ret;
}
NDASUSER_API ndas_error_t
ndas_cleanup(void)
{
debug_ndasuser(1, "ing");
#ifndef NDAS_NO_LANSCSI
ndas_block_core_cleanup();
registrar_cleanup();
#endif
lpx_cleanup();
#ifdef XPLAT_BPC
bpc_stop();
#endif
dpc_stop();
debug_ndasuser(1, "ed");
return NDAS_OK;
}
/**
* internal static functions
*/
NDASUSER_API ndas_error_t
ndas_register_network_interface(const char* devname)
{
return lpx_register_dev(devname);
}
NDASUSER_API ndas_error_t
ndas_unregister_network_interface(const char* devname)
{
return lpx_unregister_dev(devname);
}
static int v_dpc_func(void* a, void* b) {
((NDAS_CALL void(*)(void*)) a) (b);
return 0;
}
NDASUSER_API ndas_error_t
ndas_queue_task(sal_tick delay, NDAS_CALL void (*func)(void*), void* arg)
{
int ret;
dpc_id id;
id = dpc_create(DPC_PRIO_NORMAL, v_dpc_func, func, arg, NULL, 0);
if (id) {
ret = dpc_queue(id, delay);
if(ret < 0) {
dpc_destroy(id);
}
}
return id == DPC_INVALID_ID ? NDAS_ERROR_OUT_OF_MEMORY : NDAS_OK;
}
#ifndef NDAS_NO_LANSCSI
static int started;
NDASUSER_API ndas_error_t
ndas_start()
{
ndas_error_t err;
if ( started ) { /* no consideration for race condition */
return NDAS_ERROR_ALREADY_STARTED;
}
err = ndpnp_init();
if ( !NDAS_SUCCESS(err) )
return err;
err = nhix_init();
if ( !NDAS_SUCCESS(err) )
{
ndpnp_cleanup();
return err;
}
started = 1;
return NDAS_OK;
}
NDASUSER_API ndas_error_t
ndas_restart()
{
ndas_error_t err;
if ( !started ) { /* no consideration for race condition */
debug_ndasuser(1, "ndas not stared yet");
return NDAS_ERROR_ALREADY_STOPPED;
}
err = ndpnp_reinit();
if ( !NDAS_SUCCESS(err) ) {
nhix_cleanup();
started = 0;
debug_ndasuser(1, "Failed to ndpnp_reinit");
return err;
}
err = nhix_reinit();
if ( !NDAS_SUCCESS(err) )
{
ndpnp_cleanup();
started = 0;
debug_ndasuser(1, "Failed to nhix_reinit");
return err;
}
started = 1;
return NDAS_OK;
}
NDASUSER_API ndas_error_t
ndas_stop()
{
if ( !started ) { /* no consideration for race condition */
debug_ndasuser(1, "Already stopped");
return NDAS_ERROR_ALREADY_STOPPED;
}
nhix_cleanup();
ndpnp_cleanup();
#ifndef NDAS_NO_LANSCSI
registrar_stop();
#endif
started = 0;
return NDAS_OK;
}
#ifdef NDAS_PROBE
NDASUSER_API
ndas_error_t
ndas_probed_size() {
return ndev_probe_size();
}
NDASUSER_API
ndas_error_t
ndas_probed_list(struct ndas_probed* data, int devicen)
{
return ndev_probe_serial(data, devicen);
}
#else
NDASUSER_API
ndas_error_t
ndas_probed_size() {
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
NDASUSER_API
ndas_error_t
ndas_probed_list(struct ndas_probed* data, int devicen)
{
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
#endif
NDASUSER_API ndas_error_t
ndas_get_registration_data(char** data, int* size)
{
#ifdef XPLAT_RESTORE
return ndev_get_registration_data(data,size);
#else
return NDAS_ERROR_UNSUPPORTED_FEATURE;
#endif
}
NDASUSER_API ndas_error_t
ndas_set_registration_data(char *data, int size)
{
#ifdef XPLAT_RESTORE
return ndev_set_registration_data(data, size);
#else
return NDAS_ERROR_UNSUPPORTED_FEATURE;
#endif
}
NDASUSER_API
ndas_error_t
ndas_registered_size()
{
#ifndef NDAS_NO_LANSCSI
return ndev_registered_size();
#else
return 0;
#endif
}
NDASUSER_API
ndas_error_t ndas_registered_list(struct ndas_registered *data, int size)
{
#ifndef NDAS_NO_LANSCSI
return ndev_registered_list(data, size);
#else
return 0;
#endif
}
NDASUSER_API
ndas_error_t ndas_query_ndas_id_by_serial(const char* serial, char* id, char* key)
{
ndas_id_info nid;
ndas_error_t ret;
if (!serial || !id || !key) {
return NDAS_ERROR_INVALID_PARAMETER;
}
ret = get_networkid_from_serial(nid.ndas_network_id, serial);
if (ret !=NDAS_OK) {
return NDAS_ERROR_INVALID_PARAMETER;
}
sal_memcpy(nid.key1, NDIDV1Key1, 8);
sal_memcpy(nid.key2, NDIDV1Key2, 8);
sal_memcpy(nid.reserved, NDIDV1Rsv, 2);
/* To do: For seagate HW this should be detected from device */
#ifdef NDAS_CRYPTO
nid.vid = NDIDV1VID_NETCAM;
#else
nid.vid = NDIDV1VID_DEFAULT;
#endif
nid.random = NDIDV1Rnd;
if ( EncryptNdasID(&nid) )
{
#ifdef XPLAT_SERIAL2ID
sal_memcpy(id, nid.ndas_id[0], NDAS_KEY_LENGTH);
sal_memcpy(id+5, nid.ndas_id[1], NDAS_KEY_LENGTH);
sal_memcpy(id+10, nid.ndas_id[2], NDAS_KEY_LENGTH);
sal_memcpy(id+15, nid.ndas_id[3], NDAS_KEY_LENGTH);
id[NDAS_ID_LENGTH] = 0;
sal_memcpy(key, nid.ndas_key, NDAS_KEY_LENGTH);
key[NDAS_KEY_LENGTH] = 0;
#else
sal_memcpy(id, nid.ndas_id[0], NDAS_KEY_LENGTH);
sal_memcpy(id+5, nid.ndas_id[1], NDAS_KEY_LENGTH);
sal_memcpy(id+10, nid.ndas_id[2], NDAS_KEY_LENGTH);
/* Hide last 5 digit and key */
sal_memset(id+15, '*', NDAS_ID_LENGTH);
id[NDAS_ID_LENGTH] = 0;
sal_memset(key, '*', NDAS_KEY_LENGTH);
key[NDAS_KEY_LENGTH] = 0;
#endif
return NDAS_OK;
} else {
return NDAS_ERROR_INVALID_PARAMETER;
}
}
#else
NDASUSER_API ndas_error_t
ndas_start(void)
{
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
NDASUSER_API ndas_error_t
ndas_restart()
{
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
NDASUSER_API ndas_error_t
ndas_stop()
{
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
#ifdef NDAS_PROBE
NDASUSER_API
ndas_error_t
ndas_probed_size() {
return ndev_probe_size();
}
NDASUSER_API
ndas_error_t
ndas_probed_list(struct ndas_probed* data, int devicen)
{
return ndev_probe_serial(data, devicen);
}
#else
NDASUSER_API
ndas_error_t
ndas_probed_size() {
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
NDASUSER_API
ndas_error_t
ndas_probed_list(struct ndas_probed* data, int devicen)
{
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
#endif
NDASUSER_API ndas_error_t
ndas_get_registration_data(char** data, int* size)
{
#ifdef XPLAT_RESTORE
return ndev_get_registration_data(data,size);
#else
return NDAS_ERROR_UNSUPPORTED_FEATURE;
#endif
}
NDASUSER_API ndas_error_t
ndas_set_registration_data(char *data, int size)
{
#ifdef XPLAT_RESTORE
return ndev_set_registration_data(data, size);
#else
return NDAS_ERROR_UNSUPPORTED_FEATURE;
#endif
}
NDASUSER_API
ndas_error_t
ndas_registered_size()
{
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
NDASUSER_API
ndas_error_t ndas_registered_list(struct ndas_registered *data, int size)
{
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
NDASUSER_API
ndas_error_t ndas_query_ndas_id_by_serial(const char* serial, char* id, char* key)
{
return NDAS_ERROR_UNSUPPORTED_FEATURE;
}
#endif /* NDAS_NO_LANSCSI*/
NDASUSER_API ndas_error_t
ndas_get_string_error(ndas_error_t code, char* dest, int len)
{
switch (code) {
case NDAS_OK:
sal_strncpy(dest,"NDAS_OK",len); break;
case NDAS_ERROR:
sal_strncpy(dest,"NDAS_ERROR",len); break;
case NDAS_ERROR_DRIVER_NOT_LOADED:
sal_strncpy(dest,"DRIVER_NOT_LOADED",len); break;
case NDAS_ERROR_DRIVER_ALREADY_LOADED:
sal_strncpy(dest,"DRIVER_ALREADY_LOADED",len); break;
case NDAS_ERROR_LIBARARY_NOT_INITIALIZED:
sal_strncpy(dest,"LIBARARY_NOT_INITIALIZED",len); break;
case NDAS_ERROR_INVALID_NDAS_ID:
sal_strncpy(dest,"INVALID_NDAS_ID",len); break;
case NDAS_ERROR_INVALID_NDAS_KEY:
sal_strncpy(dest,"INVALID_NDAS_KEY",len); break;
case NDAS_ERROR_NOT_IMPLEMENTED:
sal_strncpy(dest,"NOT_IMPLEMENTED",len); break;
case NDAS_ERROR_INVALID_PARAMETER:
sal_strncpy(dest,"INVALID_PARAMETER",len); break;
case NDAS_ERROR_INVALID_SLOT_NUMBER:
sal_strncpy(dest,"INVALID_SLOT_NUMBER",len); break;
case NDAS_ERROR_INVALID_NAME:
sal_strncpy(dest,"invalid name",len); break;
case NDAS_ERROR_NO_DEVICE:
sal_strncpy(dest,"NO_DEVICE",len); break;
case NDAS_ERROR_ALREADY_REGISTERED_DEVICE:
sal_strncpy(dest,"ALREADY_REGISTERED_DEVICE",len); break;
case NDAS_ERROR_ALREADY_ENABLED_DEVICE:
sal_strncpy(dest,"ALREADY_ENABLED_DEVICE",len); break;
case NDAS_ERROR_ALREADY_REGISTERED_NAME:
sal_strncpy(dest,"ALREADY_REGISTERED_NAME",len); break;
case NDAS_ERROR_ALREADY_DISABLED_DEVICE:
sal_strncpy(dest,"ALREADY_DISABLED_DEVICE",len); break;
case NDAS_ERROR_ALREADY_STARTED:
sal_strncpy(dest,"ALREADY_STARTED",len); break;
case NDAS_ERROR_ALREADY_STOPPED:
sal_strncpy(dest,"ALREADY_STOPPED",len); break;
case NDAS_ERROR_NOT_ONLINE :
sal_strncpy(dest,"the NDAS device is not online",len); break;
case NDAS_ERROR_NOT_CONNECTED:
sal_strncpy(dest,"the NDAS device is not connected",len); break;
case NDAS_ERROR_INVALID_HANDLE:
sal_strncpy(dest,"INVALID_HANDLE",len); break;
case NDAS_ERROR_NO_WRITE_ACCESS_RIGHT:
sal_strncpy(dest,"no access right to write the data",len); break;
case NDAS_ERROR_WRITE_BUSY:
sal_strncpy(dest,"WRITE_BUSY",len); break;
case NDAS_ERROR_UNSUPPORTED_HARDWARE_VERSION:
sal_strncpy(dest,"UNSUPPORTED_HARDWARE_VERSION",len); break;
case NDAS_ERROR_UNSUPPORTED_SOFTWARE_VERSION:
sal_strncpy(dest,"UNSUPPORTED_SOFTWARE_VERSION",len); break;
case NDAS_ERROR_UNSUPPORTED_DISK_MODE:
sal_strncpy(dest,"UNSUPPORTED_DISK_MODE",len); break;
case NDAS_ERROR_UNSUPPORTED_FEATURE:
sal_strncpy(dest,"UNSUPPORTED_FEATURE",len); break;
case NDAS_ERROR_BUFFER_OVERFLOW:
sal_strncpy(dest,"BUFFER_OVERFLOW",len); break;
case NDAS_ERROR_NO_NETWORK_INTERFACE:
sal_strncpy(dest,"NO_NETWORK_INTERFACE",len); break;
case NDAS_ERROR_INVALID_OPERATION:
sal_strncpy(dest,"INVALID_OPERATION",len); break;
case NDAS_ERROR_NETWORK_DOWN:
sal_strncpy(dest,"NETWORK_DOWN",len); break;
case NDAS_ERROR_MEDIA_CHANGED:
sal_strncpy(dest,"MEDIA_CHANGED",len); break;
case NDAS_ERROR_TIME_OUT:
sal_strncpy(dest,"Timed out",len); break;
case NDAS_ERROR_READONLY:
sal_strncpy(dest,"read-only",len); break;
case NDAS_ERROR_OUT_OF_MEMORY:
sal_strncpy(dest,"out of memory",len); break;
case NDAS_ERROR_EXIST:
sal_strncpy(dest,"EXIST",len); break;
case NDAS_ERROR_SHUTDOWN:
sal_strncpy(dest,"SHUTDOWN",len); break;
case NDAS_ERROR_PROTO_REGISTRATION_FAIL:
sal_strncpy(dest,"PROTO_REGISTRATION_FAIL",len); break;
case NDAS_ERROR_SHUTDOWN_IN_PROGRESS:
sal_strncpy(dest,"Shutdown is in progress", len); break;
case NDAS_ERROR_ADDRESS_NOT_AVAIABLE:
sal_strncpy(dest,"ADDRESS_NOT_AVAIABLE",len); break;
case NDAS_ERROR_NOT_BOUND:
sal_strncpy(dest,"NOT_BOUND",len); break;
case NDAS_ERROR_NETWORK_FAIL:
sal_strncpy(dest,"NETWORK_FAIL",len); break;
case NDAS_ERROR_HDD_DMA2_NOT_SUPPORTED:
sal_strncpy(dest,"Hard Disk Device does not support DMA 2 mode",len); break;
case NDAS_ERROR_IDE_REMOTE_INITIATOR_NOT_EXIST:
sal_strncpy(dest,"Remote Initiator not exists",len); break;
case NDAS_ERROR_IDE_REMOTE_INITIATOR_BAD_COMMAND:
sal_strncpy(dest,"Remote Initiator bad command",len); break;
case NDAS_ERROR_IDE_REMOTE_COMMAND_FAILED:
sal_strncpy(dest,"Remote Initiator command failed",len); break;
case NDAS_ERROR_IDE_REMOTE_AUTH_FAILED:
sal_strncpy(dest,"Remote Authorization failed",len); break;
case NDAS_ERROR_IDE_TARGET_NOT_EXIST:
sal_strncpy(dest,"Target not exists",len); break;
case NDAS_ERROR_HARDWARE_DEFECT:
sal_strncpy(dest,"Hardware defect",len); break;
case NDAS_ERROR_BAD_SECTOR:
sal_strncpy(dest,"Bad sector",len); break;
case NDAS_ERROR_IDE_TARGET_BROKEN_DATA:
sal_strncpy(dest,"Target broken data",len); break;
case NDAS_ERROR_IDE_VENDOR_SPECIFIC:
sal_strncpy(dest,"IDE vendor specific error",len); break;
case NDAS_ERROR_INTERNAL:
sal_strncpy(dest,"The error is caused by the internal framework bug",len); break;
case NDAS_ERROR_MAX_USER_ERR_NUM:
sal_strncpy(dest,"MAX_USER_ERR_NUM",len); break;
case NDAS_ERROR_INVALID_RANGE_REQUEST:
sal_strncpy(dest,"Invalid range of request",len); break;
case NDAS_ERROR_INVALID_METADATA:
sal_strncpy(dest,"Invalid metadata", len); break;
case NDAS_ERROR_CONNECT_FAILED:
sal_strncpy(dest,"Failed to connect", len); break;
default:
sal_snprintf(dest,len,"UNKNOWN CODE(%d)",code); break;
}
return NDAS_OK;
}