forked from pynvme/pynvme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
1725 lines (1449 loc) · 45.9 KB
/
driver.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
/*-
* BSD LICENSE
*
* Copyright (c) Crane Che <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/sysinfo.h>
#include "spdk/stdinc.h"
#include "spdk/nvme.h"
#include "spdk/env.h"
#include "spdk/crc32.h"
#include "spdk/rpc.h"
#include "spdk_internal/log.h"
#include "spdk/lib/nvme/nvme_internal.h"
#include "driver.h"
#define US_PER_S (1000ULL*1000ULL)
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#ifndef BIT
#define BIT(a) (1UL << (a))
#endif /* BIT */
// the global configuration of the driver
#define DCFG_VERIFY_READ (BIT(0))
//// shared data
///////////////////////////////
#define DRIVER_IO_TOKEN_NAME "driver_io_token"
#define DRIVER_CRC32_TABLE_NAME "driver_crc32_table"
#define DRIVER_GLOBAL_CONFIG_NAME "driver_global_config"
// TODO: support multiple namespace
static uint64_t g_driver_table_size = 0;
static uint64_t* g_driver_io_token_ptr = NULL;
static uint32_t* g_driver_csum_table_ptr = NULL;
static uint64_t* g_driver_global_config_ptr = NULL;
static int memzone_reserve_shared_memory(uint64_t table_size)
{
if (spdk_process_is_primary())
{
assert(g_driver_io_token_ptr == NULL);
assert(g_driver_csum_table_ptr == NULL);
// get the shared memory for token
SPDK_INFOLOG(SPDK_LOG_NVME, "create token table, size: %ld\n", table_size);
g_driver_table_size = table_size;
g_driver_csum_table_ptr = spdk_memzone_reserve(DRIVER_CRC32_TABLE_NAME,
table_size,
0, SPDK_MEMZONE_NO_IOVA_CONTIG);
g_driver_io_token_ptr = spdk_memzone_reserve(DRIVER_IO_TOKEN_NAME,
sizeof(uint64_t),
0, 0);
}
else
{
// find the shared memory for token
g_driver_table_size = table_size;
g_driver_io_token_ptr = spdk_memzone_lookup(DRIVER_IO_TOKEN_NAME);
g_driver_csum_table_ptr = spdk_memzone_lookup(DRIVER_CRC32_TABLE_NAME);
}
if (g_driver_csum_table_ptr == NULL)
{
SPDK_NOTICELOG("memory is not large enough to keep CRC32 table.\n");
SPDK_NOTICELOG("Data verification is disabled!\n");
}
if (g_driver_io_token_ptr == NULL)
{
SPDK_ERRLOG("fail to find memzone space\n");
return -1;
}
return 0;
}
void crc32_clear(uint64_t lba, uint64_t lba_count, int sanitize, int uncorr)
{
int c = uncorr ? 0xff : 0;
size_t len = lba_count*sizeof(uint32_t);
if (sanitize == true)
{
assert(lba == 0);
assert(g_driver_table_size != 0); //Namspace instance not exist, you may need to add nvme0n1 in the fixture list
SPDK_DEBUGLOG(SPDK_LOG_NVME, "clear the whole table\n");
len = g_driver_table_size;
}
if (g_driver_csum_table_ptr != NULL)
{
SPDK_DEBUGLOG(SPDK_LOG_NVME, "clear checksum table, lba 0x%lx, c %d, len %ld\n",
lba, c, len);
memset(&g_driver_csum_table_ptr[lba], c, len);
}
}
static void crc32_fini(void)
{
if (spdk_process_is_primary())
{
spdk_memzone_free(DRIVER_IO_TOKEN_NAME);
spdk_memzone_free(DRIVER_CRC32_TABLE_NAME);
}
g_driver_io_token_ptr = NULL;
g_driver_csum_table_ptr = NULL;
}
////module: buffer
///////////////////////////////
void* buffer_init(size_t bytes, uint64_t *phys_addr)
{
void* buf = spdk_dma_zmalloc(bytes, 0x1000, phys_addr);
SPDK_DEBUGLOG(SPDK_LOG_NVME, "buffer: alloc ptr at %p, size %ld\n",
buf, bytes);
assert(buf != NULL);
return buf;
}
static inline uint32_t buffer_calc_csum(uint64_t* ptr, int len)
{
uint32_t crc = spdk_crc32c_update(ptr, len, 0);
//reserve 0: nomapping
//reserve 0xffffffff: uncorrectable
if (crc == 0) crc = 1;
if (crc == 0xffffffff) crc = 0xfffffffe;
return crc;
}
static void buffer_fill_data(void* buf,
uint64_t lba,
uint32_t lba_count,
uint32_t lba_size)
{
// token is keeping increasing, so every write has different data
uint64_t token = __atomic_fetch_add(g_driver_io_token_ptr,
lba_count,
__ATOMIC_SEQ_CST);
SPDK_DEBUGLOG(SPDK_LOG_NVME, "token: %ld\n", token);
SPDK_DEBUGLOG(SPDK_LOG_NVME, "lba count: %d\n", lba_count);
for (uint32_t i=0; i<lba_count; i++, lba++)
{
uint64_t* ptr = (uint64_t*)(buf+i*lba_size);
//first and last 64bit-words are filled with special data
ptr[0] = lba;
ptr[lba_size/sizeof(uint64_t)-1] = token+i;
//keep crc in memory if allocated
// suppose device modify data correctly. If the command fail, we cannot
// tell what part of data is updated, while what not. Even when atomic
// write is supported, we still cannot tell that.
if (g_driver_csum_table_ptr != NULL)
{
g_driver_csum_table_ptr[lba] = buffer_calc_csum(ptr, lba_size);
}
}
}
static int buffer_verify_data(const void* buf,
const unsigned long lba_first,
const uint32_t lba_count,
const uint32_t lba_size)
{
unsigned long lba = lba_first;
for (uint32_t i=0; i<lba_count; i++, lba++)
{
unsigned long* ptr = (unsigned long*)(buf+i*lba_size);
uint32_t computed_crc = buffer_calc_csum(ptr, lba_size);
uint32_t expected_crc = computed_crc;
// if crc table is not available, just use computed crc as
//expected crc, to bypass verification
if (g_driver_csum_table_ptr != NULL)
{
expected_crc = g_driver_csum_table_ptr[lba];
}
if (expected_crc == 0)
{
//no mapping, nothing to verify
continue;
}
if (expected_crc == 0xffffffff)
{
SPDK_WARNLOG("lba uncorrectable: lba 0x%lx\n", lba);
return -1;
}
if (lba != ptr[0])
{
SPDK_WARNLOG("lba mismatch: lba 0x%lx, but got: 0x%lx\n", lba, ptr[0]);
return -2;
}
if (computed_crc != expected_crc)
{
SPDK_WARNLOG("crc mismatch: lba 0x%lx, expected crc 0x%x, but got: 0x%x\n",
lba, expected_crc, computed_crc);
return -3;
}
}
return 0;
}
void buffer_fini(void* buf)
{
SPDK_DEBUGLOG(SPDK_LOG_NVME, "buffer: free ptr at %p\n", buf);
assert(buf != NULL);
spdk_dma_free(buf);
}
////cmd log
///////////////////////////////
// log_table contains latest cmd and cpl and their timestamps
// queue_table traces cmd log tables by queue pairs
// CMD_LOG_DEPTH should be larger than Q depth to keep all outstanding commands.
#define CMD_LOG_DEPTH (2048-1) // reserved one slot space for tail value
#define CMD_LOG_MAX_Q (16)
struct cmd_log_entry_t {
// cmd and cpl
struct timeval time_cmd;
struct spdk_nvme_cmd cmd;
struct timeval time_cpl;
struct spdk_nvme_cpl cpl;
// for data verification after read
void* buf;
uint64_t lba;
uint16_t lba_count;
uint32_t lba_size;
// callback to user functions
spdk_nvme_cmd_cb cb_fn;
void* cb_arg;
uint64_t dummy[5];
};
static_assert(sizeof(struct cmd_log_entry_t) == 192, "cacheline aligned");
struct cmd_log_table_t {
struct cmd_log_entry_t table[CMD_LOG_DEPTH];
uint32_t tail_index;
uint32_t dummy[47];
};
static_assert(sizeof(struct cmd_log_table_t) == sizeof(struct cmd_log_entry_t)*(CMD_LOG_DEPTH+1), "cacheline aligned");
#define DRIVER_CMDLOG_TABLE_NAME "driver_cmdlog_table"
static struct cmd_log_table_t* cmd_log_queue_table;
static unsigned int timeval_to_us(struct timeval* t)
{
return t->tv_sec*US_PER_S + t->tv_usec;
}
static void cmd_log_qpair_init(uint16_t qid)
{
assert(qid < CMD_LOG_MAX_Q);
// set tail to invalid value, means the qpair is empty
cmd_log_queue_table[qid].tail_index = 0;
}
static void cmd_log_qpair_clear(uint16_t qid)
{
assert(qid < CMD_LOG_MAX_Q);
// set tail to invalid value, means the qpair is empty
cmd_log_queue_table[qid].tail_index = CMD_LOG_DEPTH;
}
static int cmd_log_init(void)
{
if (spdk_process_is_primary())
{
cmd_log_queue_table = spdk_memzone_reserve(DRIVER_CMDLOG_TABLE_NAME,
sizeof(struct cmd_log_table_t)*CMD_LOG_MAX_Q,
0, SPDK_MEMZONE_NO_IOVA_CONTIG);
// clear all qpair's cmd log
for (int i=0; i<CMD_LOG_MAX_Q; i++)
{
cmd_log_qpair_clear(i);
}
// also init config word with cmdlog
g_driver_global_config_ptr = spdk_memzone_reserve(DRIVER_GLOBAL_CONFIG_NAME,
sizeof(uint64_t),
0, 0);
*g_driver_global_config_ptr = 0;
}
else
{
cmd_log_queue_table = spdk_memzone_lookup(DRIVER_CMDLOG_TABLE_NAME);
g_driver_global_config_ptr = spdk_memzone_lookup(DRIVER_GLOBAL_CONFIG_NAME);
}
if (cmd_log_queue_table == NULL)
{
fprintf(stderr, "Cannot allocate or find the cmdlog memory!\n");
return -1;
}
return 0;
}
static void cmd_log_finish(void)
{
spdk_memzone_free(DRIVER_CMDLOG_TABLE_NAME);
spdk_memzone_free(DRIVER_GLOBAL_CONFIG_NAME);
}
static struct cmd_log_entry_t*
cmd_log_add_cmd(uint16_t qid,
void* buf,
uint64_t lba,
uint16_t lba_count,
uint32_t lba_size,
const struct spdk_nvme_cmd* cmd,
spdk_nvme_cmd_cb cb_fn,
void *cb_arg)
{
struct cmd_log_table_t* log_table = &cmd_log_queue_table[qid];
struct cmd_log_entry_t* log_entry = &log_table->table[log_table->tail_index];
assert(qid < CMD_LOG_MAX_Q);
assert(log_table != NULL);
log_entry->buf = buf;
log_entry->lba = lba;
log_entry->lba_count = lba_count;
log_entry->lba_size = lba_size;
log_entry->cb_fn = cb_fn;
log_entry->cb_arg = cb_arg;
log_entry->time_cpl = (struct timeval){0};
memcpy(&log_entry->cmd, cmd, sizeof(struct spdk_nvme_cmd));
gettimeofday(&log_entry->time_cmd, NULL);
return log_entry;
}
static void cmd_log_commit_cmd(uint16_t qid)
{
struct cmd_log_table_t* log_table = &cmd_log_queue_table[qid];
uint32_t tail_index = log_table->tail_index;
assert(tail_index < CMD_LOG_DEPTH);
// add tail to commit the new cmd only when it is sent successfully
tail_index += 1;
if (tail_index == CMD_LOG_DEPTH)
{
tail_index = 0;
}
log_table->tail_index = tail_index;
}
static void cmd_log_add_cpl_cb(void* cb_ctx, const struct spdk_nvme_cpl* cpl)
{
struct timeval diff;
struct cmd_log_entry_t* log_entry = (struct cmd_log_entry_t*)cb_ctx;
assert(cpl != NULL);
assert(log_entry != NULL);
//reuse dword2 of cpl as latency value
gettimeofday(&log_entry->time_cpl, NULL);
memcpy(&log_entry->cpl, cpl, sizeof(struct spdk_nvme_cpl));
timersub(&log_entry->time_cpl, &log_entry->time_cmd, &diff);
(&log_entry->cpl.cdw0)[2] = timeval_to_us(&diff);
//SPDK_DEBUGLOG(SPDK_LOG_NVME, "cmd completed, cid %d\n", log_entry->cpl.cid);
//verify read data
if (log_entry->cmd.opc == 2 && log_entry->buf != NULL)
{
if ((*g_driver_global_config_ptr & DCFG_VERIFY_READ) != 0)
{
int ret = 0;
assert (log_entry->lba_count != 0);
assert (log_entry->lba_size != 0);
assert (log_entry->lba_size == 512);
ret = buffer_verify_data(log_entry->buf,
log_entry->lba,
log_entry->lba_count,
log_entry->lba_size);
if (ret != 0)
{
//Unrecovered Read Error: The read data could not be recovered from the media.
log_entry->cpl.status.sct = 0x02;
log_entry->cpl.status.sc = 0x81;
}
}
}
//callback to cython layer
if (log_entry->cb_fn)
{
log_entry->cb_fn(log_entry->cb_arg, &log_entry->cpl);
}
}
//// probe callbacks
///////////////////////////////
struct cb_ctx {
struct spdk_nvme_transport_id* trid;
struct spdk_nvme_ctrlr* ctrlr;
};
static bool probe_cb(void *cb_ctx,
const struct spdk_nvme_transport_id *trid,
struct spdk_nvme_ctrlr_opts *opts)
{
if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE)
{
struct spdk_nvme_transport_id* target = ((struct cb_ctx*)cb_ctx)->trid;
if (0 != spdk_nvme_transport_id_compare(target, trid))
{
SPDK_ERRLOG("Wrong address %s\n", trid->traddr);
return false;
}
opts->use_cmb_sqs = false;
SPDK_INFOLOG(SPDK_LOG_NVME, "Attaching to NVMe Controller at %s\n",
trid->traddr);
}
else
{
SPDK_INFOLOG(SPDK_LOG_NVME, "Attaching to NVMe over Fabrics controller at %s:%s: %s\n",
trid->traddr, trid->trsvcid, trid->subnqn);
}
/* Set io_queue_size to UINT16_MAX, NVMe driver
* will then reduce this to MQES to maximize
* the io_queue_size as much as possible.
*/
opts->io_queue_size = UINT16_MAX;
/* Set the header and data_digest */
opts->header_digest = false;
opts->data_digest = false;
return true;
}
static void attach_cb(void *cb_ctx,
const struct spdk_nvme_transport_id *trid,
struct spdk_nvme_ctrlr *ctrlr,
const struct spdk_nvme_ctrlr_opts *opts)
{
const struct spdk_nvme_ctrlr_data *cdata = spdk_nvme_ctrlr_get_data(ctrlr);
SPDK_INFOLOG(SPDK_LOG_NVME,
"attached device %s: %s, %d namespaces, pid %d\n",
trid->traddr, cdata->mn,
spdk_nvme_ctrlr_get_num_ns(ctrlr),
getpid());
((struct cb_ctx*)cb_ctx)->ctrlr = ctrlr;
}
////module: pcie ctrlr
///////////////////////////////
struct spdk_pci_device* pcie_init(struct spdk_nvme_ctrlr* ctrlr)
{
return spdk_nvme_ctrlr_get_pci_device(ctrlr);
}
int pcie_cfg_read8(struct spdk_pci_device* pci,
unsigned char* value,
unsigned int offset)
{
return spdk_pci_device_cfg_read8(pci, value, offset);
}
int pcie_cfg_write8(struct spdk_pci_device* pci,
unsigned char value,
unsigned int offset)
{
return spdk_pci_device_cfg_write8(pci, value, offset);
}
////module: nvme ctrlr
///////////////////////////////
struct spdk_nvme_ctrlr* nvme_probe(char* traddr)
{
struct spdk_nvme_transport_id trid;
struct cb_ctx cb_ctx;
int rc;
SPDK_DEBUGLOG(SPDK_LOG_NVME, "looking for NVMe @%s\n", traddr);
// device address
memset(&trid, 0, sizeof(trid));
if (strchr(traddr, ':') == NULL)
{
// tcp/ip address: fixed port to 4420
trid.trtype = SPDK_NVME_TRANSPORT_TCP;
trid.adrfam = SPDK_NVMF_ADRFAM_IPV4;
strncpy(trid.traddr, traddr, strlen(traddr)+1);
strncpy(trid.trsvcid, "4420", 4+1);
snprintf(trid.subnqn, sizeof(trid.subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
}
else
{
// pcie address: contains ':' characters
trid.trtype = SPDK_NVME_TRANSPORT_PCIE;
strncpy(trid.traddr, traddr, strlen(traddr)+1);
}
cb_ctx.trid = &trid;
cb_ctx.ctrlr = NULL;
rc = spdk_nvme_probe(&trid, &cb_ctx, probe_cb, attach_cb, NULL);
if (rc != 0 || cb_ctx.ctrlr == NULL)
{
SPDK_ERRLOG("not found device: %s, rc %d, cb_ctx.ctrlr %p\n",
trid.traddr, rc, cb_ctx.ctrlr);
return NULL;
}
return cb_ctx.ctrlr;
}
struct spdk_nvme_ctrlr* nvme_init(char * traddr)
{
struct spdk_nvme_ctrlr* ctrlr;
//enum the device
ctrlr = nvme_probe(traddr);
if (ctrlr == NULL)
{
return NULL;
}
SPDK_DEBUGLOG(SPDK_LOG_NVME, "found device: %s\n", ctrlr->trid.traddr);
return ctrlr;
}
int nvme_fini(struct spdk_nvme_ctrlr* ctrlr)
{
SPDK_DEBUGLOG(SPDK_LOG_NVME, "free ctrlr: %s\n", ctrlr->trid.traddr);
if (ctrlr == NULL)
{
return 0;
}
// io qpairs should all be deleted before closing master controller
if (true == spdk_process_is_primary() &&
false == TAILQ_EMPTY(&ctrlr->active_io_qpairs))
{
return -1;
}
SPDK_DEBUGLOG(SPDK_LOG_NVME, "close device: %s\n", ctrlr->trid.traddr);
return spdk_nvme_detach(ctrlr);
}
int nvme_set_reg32(struct spdk_nvme_ctrlr* ctrlr,
unsigned int offset,
unsigned int value)
{
return nvme_pcie_ctrlr_set_reg_4(ctrlr, offset, value);
}
int nvme_get_reg32(struct spdk_nvme_ctrlr* ctrlr,
unsigned int offset,
unsigned int* value)
{
return nvme_pcie_ctrlr_get_reg_4(ctrlr, offset, value);
}
int nvme_wait_completion_admin(struct spdk_nvme_ctrlr* ctrlr)
{
return spdk_nvme_ctrlr_process_admin_completions(ctrlr);
}
static void nvme_deallocate_ranges(struct spdk_nvme_dsm_range *ranges,
unsigned int count)
{
for (unsigned int i=0; i<count; i++)
{
SPDK_DEBUGLOG(SPDK_LOG_NVME, "deallocate lba 0x%lx, count %d\n",
ranges[i].starting_lba,
ranges[i].length);
crc32_clear(ranges[i].starting_lba, ranges[i].length, 0, 0);
}
}
int nvme_send_cmd_raw(struct spdk_nvme_ctrlr* ctrlr,
struct spdk_nvme_qpair *qpair,
unsigned int opcode,
unsigned int nsid,
void* buf, size_t len,
unsigned int cdw10,
unsigned int cdw11,
unsigned int cdw12,
unsigned int cdw13,
unsigned int cdw14,
unsigned int cdw15,
spdk_nvme_cmd_cb cb_fn,
void* cb_arg)
{
int rc = 0;
uint16_t qid;
struct spdk_nvme_cmd cmd;
struct cmd_log_entry_t* log_entry;
assert(ctrlr != NULL);
//setup cmd structure
memset(&cmd, 0, sizeof(struct spdk_nvme_cmd));
cmd.opc = opcode;
cmd.nsid = nsid;
cmd.cdw10 = cdw10;
cmd.cdw11 = cdw11;
cmd.cdw12 = cdw12;
cmd.cdw13 = cdw13;
cmd.cdw14 = cdw14;
cmd.cdw15 = cdw15;
qid = qpair ? qpair->id : 0;
log_entry = cmd_log_add_cmd(qid, NULL, 0, 0, 0, &cmd, cb_fn, cb_arg);
if (qpair)
{
// update host-side table for the trimed data
// other write-like operation updates crc32 table in driver wraper
if (opcode == 9)
{
nvme_deallocate_ranges(buf, cdw10+1);
}
//send io cmd in qpair
rc = spdk_nvme_ctrlr_cmd_io_raw(ctrlr, qpair, &cmd, buf, len,
cmd_log_add_cpl_cb, log_entry);
}
else
{
//not qpair, admin cmd
rc = spdk_nvme_ctrlr_cmd_admin_raw(ctrlr, &cmd, buf, len,
cmd_log_add_cpl_cb, log_entry);
}
if (rc == 0)
{
// no error, cmd is sent, so commit it to cmdlog
cmd_log_commit_cmd(qid);
}
return rc;
}
void nvme_register_aer_cb(struct spdk_nvme_ctrlr* ctrlr,
spdk_nvme_aer_cb aer_cb,
void* aer_cb_arg)
{
spdk_nvme_ctrlr_register_aer_callback(ctrlr, aer_cb, aer_cb_arg);
}
void nvme_register_timeout_cb(struct spdk_nvme_ctrlr* ctrlr,
spdk_nvme_timeout_cb timeout_cb,
unsigned int timeout)
{
spdk_nvme_ctrlr_register_timeout_callback(
ctrlr, (uint64_t)timeout*US_PER_S, timeout_cb, NULL);
}
int nvme_cpl_is_error(const struct spdk_nvme_cpl* cpl)
{
return spdk_nvme_cpl_is_error(cpl);
}
////module: qpair
///////////////////////////////
struct spdk_nvme_qpair *qpair_create(struct spdk_nvme_ctrlr* ctrlr,
int prio, int depth)
{
struct spdk_nvme_qpair* qpair;
struct spdk_nvme_io_qpair_opts opts;
//user options
opts.qprio = prio;
opts.io_queue_size = depth;
opts.io_queue_requests = depth*2;
qpair = spdk_nvme_ctrlr_alloc_io_qpair(ctrlr, &opts, sizeof(opts));
if (qpair == NULL)
{
SPDK_ERRLOG("alloc io qpair fail\n");
return NULL;
}
// limited qpair count
if (qpair->id >= CMD_LOG_MAX_Q)
{
SPDK_ERRLOG("not support so many queue pairs\n");
spdk_nvme_ctrlr_free_io_qpair(qpair);
return NULL;
}
cmd_log_qpair_init(qpair->id);
return qpair;
}
int qpair_wait_completion(struct spdk_nvme_qpair *qpair, uint32_t max_completions)
{
return spdk_nvme_qpair_process_completions(qpair, max_completions);
}
int qpair_get_id(struct spdk_nvme_qpair* q)
{
// q NULL is admin queue
return q ? q->id : 0;
}
int qpair_free(struct spdk_nvme_qpair* q)
{
if (q == NULL)
{
return 0;
}
SPDK_DEBUGLOG(SPDK_LOG_NVME, "free qpair: %d\n", q->id);
cmd_log_qpair_clear(q->id);
return spdk_nvme_ctrlr_free_io_qpair(q);
}
////module: namespace
///////////////////////////////
struct spdk_nvme_ns* ns_init(struct spdk_nvme_ctrlr* ctrlr, uint32_t nsid)
{
struct spdk_nvme_ns* ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid);
uint64_t nsze = spdk_nvme_ns_get_num_sectors(ns);
assert(ns != NULL);
if (0 != memzone_reserve_shared_memory(sizeof(uint32_t)*nsze))
{
return NULL;
}
return ns;
}
int ns_cmd_read_write(int is_read,
struct spdk_nvme_ns* ns,
struct spdk_nvme_qpair* qpair,
void* buf,
size_t len,
uint64_t lba,
uint16_t lba_count,
uint32_t io_flags,
spdk_nvme_cmd_cb cb_fn,
void* cb_arg)
{
int rc = 0;
struct spdk_nvme_cmd cmd;
struct cmd_log_entry_t* log_entry;
uint32_t lba_size = spdk_nvme_ns_get_sector_size(ns);
assert(ns != NULL);
assert(qpair != NULL);
//only support 1 namespace now
assert(ns->id == 1);
//validate data buffer
assert(buf != NULL);
assert(lba_size == 512);
assert(len >= lba_count*lba_size);
assert((io_flags&0xffff) == 0);
//setup cmd structure
memset(&cmd, 0, sizeof(struct spdk_nvme_cmd));
cmd.opc = is_read ? 2 : 1;
cmd.nsid = ns->id;
cmd.cdw10 = lba;
cmd.cdw11 = lba>>32;
cmd.cdw12 = io_flags | (lba_count-1);
cmd.cdw13 = 0;
cmd.cdw14 = 0;
cmd.cdw15 = 0;
//fill write buffer with lba, token, and checksum
if (is_read != true)
{
//for write buffer
buffer_fill_data(buf, lba, lba_count, lba_size);
}
//get entry in cmd log
log_entry = cmd_log_add_cmd(qpair->id, buf, lba, lba_count, lba_size,
&cmd, cb_fn, cb_arg);
//send io cmd in qpair
rc = spdk_nvme_ctrlr_cmd_io_raw(ns->ctrlr, qpair, &cmd, buf, len,
cmd_log_add_cpl_cb, log_entry);
if (rc == 0)
{
// cmd was sent, commit it to cmdlog
cmd_log_commit_cmd(qpair->id);
}
return rc;
}
uint32_t ns_get_sector_size(struct spdk_nvme_ns* ns)
{
return spdk_nvme_ns_get_sector_size(ns);
}
uint64_t ns_get_num_sectors(struct spdk_nvme_ns* ns)
{
return spdk_nvme_ns_get_num_sectors(ns);
}
int ns_fini(struct spdk_nvme_ns* ns)
{
crc32_fini();
return 0;
}
////module: ioworker
///////////////////////////////
// used for callback
struct ioworker_io_ctx {
void* data_buf;
size_t data_buf_len;
bool is_read;
struct timeval time_sent;
struct ioworker_global_ctx* gctx;
};
struct ioworker_global_ctx {
struct ioworker_args* args;
struct ioworker_rets* rets;
struct spdk_nvme_ns* ns;
struct spdk_nvme_qpair *qpair;
struct timeval due_time;
struct timeval io_due_time;
struct timeval io_delay_time;
struct timeval time_next_sec;
uint64_t io_count_till_last_sec;
uint64_t sequential_lba;
uint64_t io_count_sent;
uint64_t io_count_cplt;
uint32_t last_sec;
bool flag_finish;
};
#define ALIGN_UP(n, a) (((n)%(a))?((n)+(a)-((n)%(a))):((n)))
#define ALIGN_DOWN(n, a) ((n)-((n)%(a)))
static int ioworker_send_one(struct spdk_nvme_ns* ns,
struct spdk_nvme_qpair *qpair,
struct ioworker_io_ctx* ctx,
struct ioworker_global_ctx* gctx);
static inline void timeradd_second(struct timeval* now,
unsigned int seconds,
struct timeval* due)
{
struct timeval duration;
duration.tv_sec = seconds;
duration.tv_usec = 0;
timeradd(now, &duration, due);
}
static bool ioworker_send_one_is_finish(struct ioworker_args* args,
struct ioworker_global_ctx* c)
{
struct timeval now;
// limit by io count, and/or time, which happens first
if (c->io_count_sent == args->io_count)
{
SPDK_DEBUGLOG(SPDK_LOG_NVME, "ioworker finish, sent %ld io\n", c->io_count_sent);
return true;
}
assert(c->io_count_sent < args->io_count);
gettimeofday(&now, NULL);
if (true == timercmp(&now, &c->due_time, >))
{
SPDK_DEBUGLOG(SPDK_LOG_NVME, "ioworker finish, due time %ld us\n", c->due_time.tv_usec);
return true;
}
return false;
}
static void ioworker_one_io_throttle(struct ioworker_global_ctx* gctx,
struct timeval* now)
{
SPDK_DEBUGLOG(SPDK_LOG_NVME, "this io due at %ld.%06ld\n",
gctx->io_due_time.tv_sec, gctx->io_due_time.tv_usec);
if (true == timercmp(&gctx->io_due_time, now, >))
{
//delay usec to meet the IOPS prequisit
struct timeval diff;
timersub(&gctx->io_due_time, now, &diff);
usleep(timeval_to_us(&diff));
}
timeradd(&gctx->io_due_time, &gctx->io_delay_time, &gctx->io_due_time);
}
static uint32_t ioworker_get_duration(struct timeval* start,
struct ioworker_global_ctx* gctx)
{
struct timeval now;
struct timeval diff;
uint32_t msec;
gettimeofday(&now, NULL);
timersub(&now, start, &diff);
msec = diff.tv_sec * 1000UL;
return msec + (diff.tv_usec+500)/1000;
}