forked from nvmecompliance/dnvme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnvme_ioctls.c
executable file
·1524 lines (1310 loc) · 51.8 KB
/
dnvme_ioctls.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
/*
* NVM Express Compliance Suite
* Copyright (c) 2011, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/unistd.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include "dnvme_ioctls.h"
#include "dnvme_interface.h"
#include "definitions.h"
#include "dnvme_reg.h"
#include "sysfuncproto.h"
#include "sysdnvme.h"
#include "dnvme_sts_chk.h"
#include "dnvme_queue.h"
#include "dnvme_cmds.h"
#include "dnvme_ds.h"
#include "dnvme_irq.h"
int device_status_chk(struct metrics_device_list *pmetrics_device, int *status)
{
int err;
u16 data;
int ker_status;
/*
* Read a word (16bit value) from the configuration register
* and pass it to user.
*/
err = pci_read_config_word(pmetrics_device->
metrics_device->private_dev.pdev, PCI_DEVICE_STATUS, &data);
if (err < 0) {
LOG_ERR("pci_read_config failed in driver error check");
return -EINVAL;
}
/*
* Get the Device Status from the PCI Header.
*/
ker_status = device_status_pci(data);
if (ker_status == SUCCESS) {
LOG_DBG("PCI Device Status SUCCESS (STS)");
} else {
LOG_ERR("PCI Device Status FAIL (STS)");
}
ker_status = (ker_status == SUCCESS) ? device_status_next
(pmetrics_device->metrics_device->private_dev.pdev) : FAIL;
if (ker_status == SUCCESS) {
LOG_DBG("NEXT Capability Status SUCCESS.");
} else {
LOG_ERR("NEXT Capability status FAIL");
}
ker_status = (ker_status == SUCCESS) ? nvme_controller_status
(pmetrics_device->metrics_device->private_dev.ctrlr_regs) : FAIL;
if (ker_status == SUCCESS) {
LOG_DBG("NVME Controller Status SUCCESS (CSTS)");
} else {
LOG_ERR("NVME Controller Status FAIL (CSTS)");
}
if (copy_to_user(status, &ker_status, sizeof(int))) {
LOG_ERR("Unable to copy to user space");
return -EFAULT;
}
return 0;
}
int driver_generic_read(struct rw_generic *nvme_data,
struct metrics_device_list *pmetrics_device)
{
u16 index;
int err = -EINVAL;
void *datap = NULL;
struct pci_dev *pdev;
struct nvme_device *nvme_dev;
struct rw_generic *user_data = NULL;
/* Allocating memory for user struct in kernel space */
user_data = kmalloc(sizeof(struct rw_generic), GFP_KERNEL);
if (user_data == NULL) {
LOG_ERR("Unable to alloc kernel memory to copy user data");
err = -ENOMEM;
goto fail_out;
}
if (copy_from_user(user_data, nvme_data, sizeof(struct rw_generic))) {
LOG_ERR("Unable to copy from user space");
err = -EFAULT;
goto fail_out;
}
/* Allocating memory for the read in kernel space */
datap = kmalloc(user_data->nBytes, GFP_KERNEL | __GFP_ZERO);
if (datap == NULL) {
LOG_ERR("Unable to allocate kernel memory");
err = -ENOMEM;
goto fail_out;
}
/* get the device from the list */
pdev = pmetrics_device->metrics_device->private_dev.pdev;
nvme_dev = pmetrics_device->metrics_device;
switch (user_data->type) {
case NVMEIO_PCI_HDR:
LOG_DBG("User App request to read the PCI Header Space");
LOG_DBG("Read request for bytes = 0x%x", user_data->nBytes);
LOG_DBG("off:acc= 0x%x:0x%2x", user_data->offset, user_data->acc_type);
for (index = 0; index < user_data->nBytes; ) {
LOG_DBG("Reading for index = 0x%x", index);
LOG_DBG("PCI Offset = 0x%x", user_data->offset + index);
if ((user_data->offset + index) > MAX_PCI_EXPRESS_CFG) {
LOG_ERR("Offset is > the PCIe extended cfg space");
err = -EINVAL;
goto fail_out;
}
/*
* Check the access width and access the PCI space as per
* the requested width.
*/
if ((user_data->acc_type == DWORD_LEN)
&& (user_data->nBytes % 4 == 0)) {
err = pci_read_config_dword(pdev,
(user_data->offset + index), (datap + index));
LOG_DBG("Reading PCI offset, data = 0x%x, 0x%x",
(user_data->offset + index), *(u32 *)(datap + index));
index += 4;
} else if ((user_data->acc_type == WORD_LEN)
&& (user_data->nBytes % 2 == 0)) {
err = pci_read_config_word(pdev,
(user_data->offset + index), (datap + index));
LOG_DBG("Reading PCI offset, data = 0x%x, 0x%x",
(user_data->offset + index), *(u16 *)(datap + index));
index += 2;
} else if (user_data->acc_type == BYTE_LEN) {
err = pci_read_config_byte(pdev,
(user_data->offset + index), (datap + index));
LOG_DBG("Reading PCI offset, data = 0x%x, 0x%x",
(user_data->offset + index), *(u8 *)(datap + index));
index++;
} else {
LOG_ERR("PCI space accessed by DWORD, WORD or BYTE");
err = -EINVAL;
goto fail_out;
}
if (err < 0) {
LOG_ERR("pci_read_config failed");
goto fail_out;
}
}
break;
case NVMEIO_BAR01:
if ((user_data->acc_type == DWORD_LEN) &&
(((user_data->nBytes % 4) != 0) ||
((user_data->offset % 4) != 0))) {
LOG_ERR("Offset must be DWORD align, nBytes must be DWORD align");
err = -EINVAL;
goto fail_out;
} else if ((user_data->acc_type == QUAD_LEN) &&
(((user_data->nBytes % 8) != 0) ||
((user_data->offset % 4) != 0))) {
LOG_ERR("Offset must be DWORD align, nBytes must be QWORD align");
err = -EINVAL;
goto fail_out;
} else if ((user_data->acc_type == WORD_LEN) &&
(((user_data->nBytes % 2) != 0) ||
((user_data->offset % 2) != 0))) {
LOG_ERR("Offset must be WORD align, nBytes must be WORD align");
err = -EINVAL;
goto fail_out;
}
err = read_nvme_reg_generic(nvme_dev->private_dev.bar0,
datap, user_data->nBytes, user_data->offset, user_data->acc_type);
if (err < 0) {
LOG_ERR("Read NVME Space failed");
goto fail_out;
}
break;
default:
LOG_DBG("Could not find switch case using default");
err = -EINVAL;
goto fail_out;
}
if (copy_to_user(user_data->buffer, datap, user_data->nBytes)) {
LOG_ERR("Unable to copy to user space");
err = -EFAULT;
goto fail_out;
}
/* Fall through upon success is meant to be */
fail_out:
if (datap != NULL) {
kfree(datap);
}
if (user_data != NULL) {
kfree(user_data);
}
return err;
}
int driver_generic_write(struct rw_generic *nvme_data,
struct metrics_device_list *pmetrics_device)
{
u16 index;
int err = -EINVAL;
void *datap = NULL;
struct pci_dev *pdev;
struct nvme_device *nvme_dev;
struct rw_generic *user_data = NULL;
/* Allocating memory for user struct in kernel space */
user_data = kmalloc(sizeof(struct rw_generic), GFP_KERNEL);
if (user_data == NULL) {
LOG_ERR("Unable to alloc kernel memory to copy user data");
err = -ENOMEM;
goto fail_out;
}
if (copy_from_user(user_data, nvme_data, sizeof(struct rw_generic))) {
LOG_ERR("Unable to copy from user space");
err = -EFAULT;
goto fail_out;
}
/* Allocating memory for the data in kernel space */
datap = kmalloc(user_data->nBytes, GFP_KERNEL | __GFP_ZERO);
if (!datap) {
LOG_ERR("Unable to allocate kernel memory");
return -ENOMEM;
}
pdev = pmetrics_device->metrics_device->private_dev.pdev;
nvme_dev = pmetrics_device->metrics_device;
/* Copying user space buffer to kernel memory */
if (copy_from_user(datap, user_data->buffer, user_data->nBytes)) {
LOG_ERR("Invalid copy from user space");
err = -EFAULT;
goto fail_out;
}
switch (user_data->type) {
case NVMEIO_PCI_HDR:
for (index = 0; index < user_data->nBytes; index++) {
/*
* write user data to pci config space at location
* indicated by (offset + index) as per access width.
*/
if ((user_data->acc_type == DWORD_LEN)
&& (user_data->nBytes % 4 == 0)) {
err = pci_write_config_dword(pdev,
(user_data->offset + index), *(u32 *)(datap + index));
LOG_DBG("Writing to PCI offset, data = 0x%x, 0x%x",
(user_data->offset + index), *(u32 *)(datap + index));
index += 4;
} else if ((user_data->acc_type == WORD_LEN)
&& (user_data->nBytes % 2 == 0)) {
err = pci_write_config_word(pdev,
(user_data->offset + index), *(u16 *)(datap + index));
LOG_DBG("Writing to PCI offset, data = 0x%x, 0x%x",
(user_data->offset + index), *(u16 *)(datap + index));
index += 2;
} else if (user_data->acc_type == BYTE_LEN) {
err = pci_write_config_byte(pdev,
(user_data->offset + index), *(u8 *)(datap + index));
LOG_DBG("Writing to PCI offset, data = 0x%x, 0x%x",
(user_data->offset + index), *(u8 *)(datap + index));
index++;
} else {
LOG_ERR("PCI space accessed by DWORD, WORD or BYTE");
err = -EINVAL;
goto fail_out;
}
if (err < 0) {
LOG_ERR("pci_write_config failed");
goto fail_out;
}
}
break;
case NVMEIO_BAR01:
if ((user_data->acc_type == DWORD_LEN) &&
(((user_data->nBytes % 4) != 0) ||
((user_data->offset % 4) != 0))) {
LOG_ERR("Offset must be DWORD align, nBytes must be DWORD align");
err = -EINVAL;
goto fail_out;
} else if ((user_data->acc_type == QUAD_LEN) &&
(((user_data->nBytes % 8) != 0) ||
((user_data->offset % 4) != 0))) {
LOG_ERR("Offset must be DWORD align, nBytes must be QWORD align");
err = -EINVAL;
goto fail_out;
} else if ((user_data->acc_type == WORD_LEN) &&
(((user_data->nBytes % 2) != 0) ||
((user_data->offset % 2) != 0))) {
LOG_ERR("Offset must be WORD align, nBytes must be WORD align");
err = -EINVAL;
goto fail_out;
}
err = write_nvme_reg_generic(nvme_dev->private_dev.bar0,
datap, user_data->nBytes, user_data->offset, user_data->acc_type);
if (err < 0) {
LOG_ERR("Write NVME Space failed");
goto fail_out;
}
break;
default:
LOG_DBG("Could not find switch case using default");
err = -EINVAL;
break;
}
/* Fall through upon success is meant to be */
fail_out:
if (datap != NULL) {
kfree(datap);
}
if (user_data != NULL) {
kfree(user_data);
}
return err;
}
int driver_create_asq(struct nvme_create_admn_q *create_admn_q,
struct metrics_device_list *pmetrics_device)
{
int err = -EINVAL;
u8 admn_id = 0; /* Always admin ID is 0 */
struct metrics_sq *pmetrics_sq_list = NULL; /* SQ linked list */
struct nvme_device *pnvme_dev = pmetrics_device->metrics_device;
if (readl(&pnvme_dev->private_dev.ctrlr_regs->cc) & NVME_CC_ENABLE) {
LOG_ERR("Device enable bit is set already");
goto fail_out; /* use invalid return code */
}
LOG_DBG("Searching for node in the sq_list_hd");
err = identify_unique(admn_id, METRICS_SQ, pmetrics_device);
if (err != SUCCESS) {
LOG_ERR("ASQ already exists");
goto fail_out; /* use invalid return code */
}
LOG_DBG("Alloc mem for a ASQ node");
pmetrics_sq_list = kmalloc(sizeof(struct metrics_sq), GFP_KERNEL);
if (pmetrics_sq_list == NULL) {
LOG_ERR("failed mem alloc in ASQ creation.");
err = -ENOMEM;
goto fail_out;
}
memset(pmetrics_sq_list, 0, sizeof(struct metrics_sq));
/* Set Admin Q Id. */
pmetrics_sq_list->public_sq.sq_id = admn_id;
pmetrics_sq_list->public_sq.elements = create_admn_q->elements;
/* Admin SQ is always associated with Admin CQ. */
pmetrics_sq_list->public_sq.cq_id = admn_id;
/* Adding Command Tracking list */
INIT_LIST_HEAD(&(pmetrics_sq_list->private_sq.cmd_track_list));
/* Call dma allocation, creation of contiguous memory for ASQ */
err = create_admn_sq(pnvme_dev, pmetrics_sq_list->public_sq.elements,
pmetrics_sq_list);
if (err != SUCCESS) {
LOG_ERR("Failed Admin Q creation!!");
goto fail_out;
}
LOG_DBG("Adding node for ASQ to the list.");
list_add_tail(&pmetrics_sq_list->sq_list_hd,
&pmetrics_device->metrics_sq_list);
return err;
fail_out:
if (pmetrics_sq_list != NULL) {
kfree(pmetrics_sq_list);
}
return err;
}
int driver_create_acq(struct nvme_create_admn_q *create_admn_q,
struct metrics_device_list *pmetrics_device)
{
int err = -EINVAL;
u8 admn_id = 0; /* Always Admin ID is zero. */
struct metrics_cq *pmetrics_cq_list = NULL; /* CQ linked list */
struct nvme_device *pnvme_dev = pmetrics_device->metrics_device;
if (readl(&pnvme_dev->private_dev.ctrlr_regs->cc) & NVME_CC_ENABLE) {
LOG_ERR("Device enable bit is set already");
goto fail_out; /* use invalid return code */
}
LOG_DBG("Searching for node in the cq_list_hd");
err = identify_unique(admn_id, METRICS_CQ, pmetrics_device);
if (err != SUCCESS) {
LOG_ERR("ACQ already exists");
goto fail_out; /* use invalid return code */
}
LOG_DBG("Alloc mem for a ACQ node");
pmetrics_cq_list = kmalloc(sizeof(struct metrics_cq), GFP_KERNEL);
if (pmetrics_cq_list == NULL) {
LOG_ERR("failed mem alloc in ACQ creation.");
err = -ENOMEM;
goto fail_out;
}
memset(pmetrics_cq_list, 0, sizeof(struct metrics_cq));
/* Set Admin CQ Id. */
pmetrics_cq_list->public_cq.q_id = admn_id;
pmetrics_cq_list->public_cq.elements = create_admn_q->elements;
pmetrics_cq_list->public_cq.irq_no = 0;
pmetrics_cq_list->public_cq.irq_enabled = 1;
/* Call dma allocation, creation of contiguous memory for ACQ */
err = create_admn_cq(pnvme_dev, pmetrics_cq_list->public_cq.elements,
pmetrics_cq_list);
if (err != SUCCESS) {
LOG_ERR("Admin CQ creation failed!!");
goto fail_out;
}
/* Set the pbit_new_entry value */
pmetrics_cq_list->public_cq.pbit_new_entry = 1;
LOG_DBG("Adding node for ACQ to the list.");
list_add_tail(&pmetrics_cq_list->cq_list_hd,
&pmetrics_device->metrics_cq_list);
return err;
fail_out:
if (pmetrics_cq_list != NULL) {
kfree(pmetrics_cq_list);
}
return err;
}
int driver_ioctl_init(struct pci_dev *pdev, void __iomem *bar0,
void __iomem *bar1, void __iomem *bar2,
struct metrics_device_list *pmetrics_device_list)
{
int err;
pmetrics_device_list->metrics_device =
kmalloc(sizeof(struct nvme_device), GFP_KERNEL);
if (pmetrics_device_list->metrics_device == NULL) {
LOG_ERR("Failed alloc of devel level metric storage area");
err = -ENOMEM;
goto fail_out;
}
/* Init linked lists for this device. */
INIT_LIST_HEAD(&(pmetrics_device_list->metrics_sq_list));
INIT_LIST_HEAD(&(pmetrics_device_list->metrics_cq_list));
INIT_LIST_HEAD(&(pmetrics_device_list->metrics_meta.meta_trk_list));
INIT_LIST_HEAD(&(pmetrics_device_list->irq_process.irq_track_list));
INIT_LIST_HEAD(&(pmetrics_device_list->irq_process.wrk_item_list));
mutex_init(&pmetrics_device_list->irq_process.irq_track_mtx);
pmetrics_device_list->metrics_device->private_dev.pdev = pdev;
pmetrics_device_list->metrics_device->private_dev.bar0 = bar0;
pmetrics_device_list->metrics_device->private_dev.bar1 = bar1;
pmetrics_device_list->metrics_device->private_dev.bar2 = bar2;
pmetrics_device_list->metrics_device->private_dev.ctrlr_regs = bar0;
pmetrics_device_list->metrics_device->private_dev.dmadev =
&pmetrics_device_list->metrics_device->private_dev.pdev->dev;
/* Used to create Coherent DMA mapping for PRP List */
pmetrics_device_list->metrics_meta.meta_dmapool_ptr = NULL;
pmetrics_device_list->metrics_device->private_dev.prp_page_pool =
dma_pool_create("prp page",
&pmetrics_device_list->metrics_device->private_dev.pdev->dev,
PAGE_SIZE, PAGE_SIZE, 0);
if (pmetrics_device_list->metrics_device->private_dev.prp_page_pool ==
NULL) {
LOG_ERR("Creating DMA Pool failed");
err = -ENOMEM;
goto fail_out;
}
/* Spinlock to protect from kernel preemption in ISR handler */
spin_lock_init(&pmetrics_device_list->irq_process.isr_spin_lock);
/* Initialize irq scheme to INT_NONE and perform cleanup of all lists */
err = init_irq_lists(pmetrics_device_list, INT_NONE);
if (err < 0) {
LOG_ERR("IRQ track initialization failed...");
goto fail_out;
}
return SUCCESS;
fail_out:
if (pmetrics_device_list->metrics_device != NULL) {
kfree(pmetrics_device_list->metrics_device);
}
if (pmetrics_device_list->metrics_device->private_dev.prp_page_pool !=
NULL) {
dma_pool_destroy(
pmetrics_device_list->metrics_device->private_dev.prp_page_pool);
}
return err;
}
/*
* Allocate a dma pool for the requested size. Initialize the DMA pool pointer
* with DWORD alignment and associate it with the active device.
*/
int metabuff_create(struct metrics_device_list *pmetrics_device_elem,
u32 alloc_size)
{
/* First Check if the meta pool already exists */
if (pmetrics_device_elem->metrics_meta.meta_dmapool_ptr != NULL) {
if (alloc_size == pmetrics_device_elem->metrics_meta.meta_buf_size) {
return SUCCESS;
}
LOG_ERR("Meta Pool already exists, of a different size");
return -EINVAL;
}
/* Create coherent DMA mapping for meta data buffer creation */
pmetrics_device_elem->metrics_meta.meta_dmapool_ptr = dma_pool_create
("meta_buff", &pmetrics_device_elem->metrics_device->
private_dev.pdev->dev, alloc_size, sizeof(u32), 0);
if (pmetrics_device_elem->metrics_meta.meta_dmapool_ptr == NULL) {
LOG_ERR("Creation of DMA Pool failed size = 0x%08X", alloc_size);
return -ENOMEM;
}
pmetrics_device_elem->metrics_meta.meta_buf_size = alloc_size;
return SUCCESS;
}
/*
* alloc a meta buffer node when user request and allocate a consistent
* dma memory from the meta dma pool. Add this node into the meta data
* linked list.
*/
int metabuff_alloc(struct metrics_device_list *pmetrics_device_elem,
u32 meta_id)
{
struct metrics_meta *pmeta_data = NULL;
int err = SUCCESS;
/* Check if parameters passed to this function are valid */
if (pmetrics_device_elem->metrics_meta.meta_dmapool_ptr == NULL) {
LOG_NRM("Call to Create the meta data pool first...");
LOG_ERR("Meta data pool is not created");
return -EINVAL;
}
pmeta_data = find_meta_node(pmetrics_device_elem, meta_id);
if (pmeta_data != NULL) {
LOG_ERR("Meta ID = %d already exists", pmeta_data->meta_id);
return -EINVAL;
}
/* Allocate memory to metrics_meta for each node */
pmeta_data = kmalloc(sizeof(struct metrics_meta), GFP_KERNEL);
if (pmeta_data == NULL) {
LOG_ERR("Allocation to contain meta data node failed");
err = -ENOMEM;
goto fail_out;
}
/* Allocate DMA memory for the meta data buffer */
pmeta_data->meta_id = meta_id;
pmeta_data->vir_kern_addr = dma_pool_alloc(pmetrics_device_elem->
metrics_meta.meta_dmapool_ptr, GFP_ATOMIC, &pmeta_data->meta_dma_addr);
if (pmeta_data->vir_kern_addr == NULL) {
LOG_ERR("Allocation for meta data buffer failed");
err = -ENOMEM;
goto fail_out;
}
/* Add the meta data node into the linked list */
list_add_tail(&pmeta_data->meta_list_hd, &pmetrics_device_elem->
metrics_meta.meta_trk_list);
return err;
fail_out:
if (pmeta_data != NULL) {
kfree(pmeta_data);
}
return err;
}
/*
* Delete the meta buffer node for given meta id from the linked list.
* First Free the dma pool allocated memory then delete the entry from the
* linked list and finally free the node memory from the kernel.
*/
int metabuff_del(struct metrics_device_list *pmetrics_device,
u32 meta_id)
{
struct metrics_meta *pmeta_data;
/* Check if invalid parameters are passed */
if (pmetrics_device->metrics_meta.meta_dmapool_ptr == NULL) {
LOG_ERR("Meta data pool is not created, nothing to delete");
return -EINVAL;
}
/* Check if meta node id exists */
pmeta_data = find_meta_node(pmetrics_device, meta_id);
if (pmeta_data == NULL) {
LOG_DBG("Meta ID does not exists, it is already deleted");
return SUCCESS;
}
/* Free the DMA memory if exists */
if (pmeta_data->vir_kern_addr != NULL) {
dma_pool_free(pmetrics_device->metrics_meta.meta_dmapool_ptr,
pmeta_data->vir_kern_addr, pmeta_data->meta_dma_addr);
}
/* Remove from the linked list and free the node */
list_del(&pmeta_data->meta_list_hd);
kfree(pmeta_data);
return SUCCESS;
}
/*
* deallocate_mb - This function will start freeing up the memory and
* nodes for the meta buffers allocated during the alloc and create meta.
*/
void deallocate_mb(struct metrics_device_list *pmetrics_device)
{
struct metrics_meta *pmeta_data = NULL;
struct metrics_meta *pmeta_data_next = NULL;
/* do not assume the node exists always */
if (pmetrics_device->metrics_meta.meta_dmapool_ptr == NULL) {
LOG_DBG("Meta node is not allocated..");
return;
}
/* Loop for each meta data node */
list_for_each_entry_safe(pmeta_data, pmeta_data_next,
&(pmetrics_device->metrics_meta.meta_trk_list), meta_list_hd) {
/* free the dma memory if exists */
if (pmeta_data->vir_kern_addr != NULL) {
dma_pool_free(pmetrics_device->metrics_meta.meta_dmapool_ptr,
pmeta_data->vir_kern_addr, pmeta_data->meta_dma_addr);
}
/* Remove from the linked list and free the node */
list_del(&pmeta_data->meta_list_hd);
kfree(pmeta_data);
}
/* check if it has dma pool created then destroy */
if (pmetrics_device->metrics_meta.meta_dmapool_ptr != NULL) {
dma_pool_destroy(pmetrics_device->metrics_meta.meta_dmapool_ptr);
pmetrics_device->metrics_meta.meta_dmapool_ptr = NULL;
}
pmetrics_device->metrics_meta.meta_buf_size = 0;
/* Prepare a clean list, empty, ready for next use */
INIT_LIST_HEAD(&pmetrics_device->metrics_meta.meta_trk_list);
}
int driver_toxic_dword(struct metrics_device_list *pmetrics_device,
struct backdoor_inject *err_inject)
{
int err = -EINVAL;
u32 *tgt_dword; /* DWORD which needs updating */
u32 entry_size = 64; /* Assumption is for ASQ */
struct metrics_sq *pmetrics_sq; /* Ptr to specific SQ of interest */
struct backdoor_inject *user_data = NULL;
#ifdef DEBUG
int i;
#endif
/* Allocating memory for user struct in kernel space */
user_data = kmalloc(sizeof(struct backdoor_inject), GFP_KERNEL);
if (user_data == NULL) {
LOG_ERR("Unable to alloc kernel memory to copy user data");
err = -ENOMEM;
goto fail_out;
}
if (copy_from_user(user_data, err_inject, sizeof(struct backdoor_inject))) {
LOG_ERR("Unable to copy from user space");
err = -EFAULT;
goto fail_out;
}
/* Get the required SQ for which command should be modified */
pmetrics_sq = find_sq(pmetrics_device, user_data->q_id);
if (pmetrics_sq == NULL) {
LOG_ERR("SQ ID = %d does not exist", user_data->q_id);
err = -EPERM;
goto fail_out;
}
/* If this SQ is an IOSQ, not ASQ, then lookup the element size */
if (pmetrics_sq->public_sq.sq_id != 0) {
entry_size = (pmetrics_sq->private_sq.size /
pmetrics_sq->public_sq.elements);
}
/* The cmd for which is being updated, better not have rung its doorbell */
if (pmetrics_sq->public_sq.tail_ptr_virt <
pmetrics_sq->public_sq.tail_ptr) {
// Handle wrapping state of the SQ
if ((user_data->cmd_ptr < pmetrics_sq->public_sq.tail_ptr) &&
(user_data->cmd_ptr >= pmetrics_sq->public_sq.tail_ptr_virt)) {
LOG_ERR("Already rung doorbell for cmd idx = %d",
user_data->cmd_ptr);
err = -EINVAL;
goto fail_out;
}
} else { // no wrap condition
if (user_data->cmd_ptr < pmetrics_sq->public_sq.tail_ptr) {
LOG_ERR("Already rung doorbell for cmd idx = %d",
user_data->cmd_ptr);
err = -EINVAL;
goto fail_out;
}
}
/* Validate requirement: [0 -> (CreateIOSQ.DW10.SIZE-1)] */
if (user_data->cmd_ptr >= pmetrics_sq->public_sq.elements) {
LOG_ERR("SQ ID %d contains only %d elements",
pmetrics_sq->public_sq.sq_id, pmetrics_sq->public_sq.elements);
err = -EPERM;
goto fail_out;
}
/* Validate requirement: [0 -> (CC.IOSQES-1)] */
if (user_data->dword >= entry_size) {
LOG_ERR("SQ ID %d elements are only of size %d bytes",
pmetrics_sq->public_sq.sq_id, entry_size);
err = -EPERM;
goto fail_out;
}
/* Inject the requested bit values into the appropriate place */
if (pmetrics_sq->private_sq.contig) {
#ifdef DEBUG
for (i = 0; i < entry_size; i += sizeof(u32)) {
LOG_DBG("B4 cmd DW%d = 0x%08X", (int)(i / sizeof(u32)),
*((u32 *)(pmetrics_sq->private_sq.vir_kern_addr +
(user_data->cmd_ptr * entry_size) + i)));
}
#endif
tgt_dword = (u32 *)(pmetrics_sq->private_sq.vir_kern_addr +
(user_data->cmd_ptr * entry_size) +
(user_data->dword * sizeof(u32)));
LOG_DBG("B4 tgt_DW%d = 0x%08X", user_data->dword, *tgt_dword);
*tgt_dword &= ~user_data->value_mask;
*tgt_dword |= (user_data->value & user_data->value_mask);
LOG_DBG("After tgt_DW%d = 0x%08X", user_data->dword, *tgt_dword);
#ifdef DEBUG
for (i = 0; i < entry_size; i += sizeof(u32)) {
LOG_DBG("After cmd DW%d = 0x%08X", (int)(i / sizeof(u32)),
*((u32 *)(pmetrics_sq->private_sq.vir_kern_addr +
(user_data->cmd_ptr * entry_size) + i)));
}
#endif
} else {
#ifdef DEBUG
for (i = 0; i < entry_size; i += sizeof(u32)) {
LOG_DBG("B4 cmd DW%d = 0x%08X", (int)(i / sizeof(u32)),
*((u32 *)(pmetrics_sq->private_sq.prp_persist.vir_kern_addr +
(user_data->cmd_ptr * entry_size) + i)));
}
#endif
tgt_dword = (u32 *)(pmetrics_sq->private_sq.prp_persist.vir_kern_addr +
(user_data->cmd_ptr * entry_size) +
(user_data->dword * sizeof(u32)));
LOG_DBG("B4 tgt_DW%d = 0x%08X", user_data->dword, *tgt_dword);
*tgt_dword &= ~user_data->value_mask;
*tgt_dword |= (user_data->value & user_data->value_mask);
LOG_DBG("After tgt_DW%d = 0x%08X", user_data->dword, *tgt_dword);
dma_sync_sg_for_device(pmetrics_device->metrics_device->
private_dev.dmadev, pmetrics_sq->private_sq.prp_persist.sg,
pmetrics_sq->private_sq.prp_persist.num_map_pgs,
pmetrics_sq->private_sq.prp_persist.data_dir);
#ifdef DEBUG
for (i = 0; i < entry_size; i += sizeof(u32)) {
LOG_DBG("After cmd DW%d = 0x%08X", (int)(i / sizeof(u32)),
*((u32 *)(pmetrics_sq->private_sq.prp_persist.vir_kern_addr +
(user_data->cmd_ptr * entry_size) + i)));
}
#endif
}
return SUCCESS;
fail_out:
if (user_data != NULL) {
kfree(user_data);
}
LOG_DBG("Injecting toxic cmd bits failed");
return err;
}
int driver_send_64b(struct metrics_device_list *pmetrics_device,
struct nvme_64b_send *cmd_request)
{
int err = -EINVAL;
u32 cmd_buf_size = 0;
/* Particular SQ from linked list of SQ's for device */
struct metrics_sq *pmetrics_sq;
/* SQ represented by the CMD.QID */
struct metrics_sq *p_cmd_sq;
/* Particular CQ (within CMD) from linked list of Q's for device */
struct metrics_cq *p_cmd_cq;
/* struct describing the meta buf */
struct metrics_meta *meta_buf;
/* Kernel space memory for passed in command */
void *nvme_cmd_ker = NULL;
/* Pointer to passed in command DW0-DW9 */
struct nvme_gen_cmd *nvme_gen_cmd;
/* Pointer to Gen IOSQ command */
struct nvme_create_sq *nvme_create_sq;
/* Pointer to Gen IOCQ command */
struct nvme_create_cq *nvme_create_cq;
/* Pointer to Delete IO Q command */
struct nvme_del_q *nvme_del_q;
/* void * pointer to check validity of Queues */
void *q_ptr = NULL;
struct nvme_prps prps; /* Pointer to PRP List */
struct nvme_64b_send *user_data = NULL;
/* Allocating memory for user struct in kernel space */
user_data = kmalloc(sizeof(struct nvme_64b_send), GFP_KERNEL);
if (user_data == NULL) {
LOG_ERR("Unable to alloc kernel memory to copy user data");
err = -ENOMEM;
goto free_out;
}
if (copy_from_user(user_data, cmd_request, sizeof(struct nvme_64b_send))) {
LOG_ERR("Unable to copy from user space");
err = -EFAULT;
goto free_out;
}
/* Initial invalid arguments checking */
if (NULL == user_data->cmd_buf_ptr) {
LOG_ERR("Command Buffer does not exist");
goto free_out;
} else if (
(user_data->data_buf_size != 0 && NULL == user_data->data_buf_ptr) ||
(user_data->data_buf_size == 0 && NULL != user_data->data_buf_ptr)) {
LOG_ERR("Data buffer size and data buffer inconsistent");
goto free_out;
}
/* Get the required SQ through which command should be sent */
pmetrics_sq = find_sq(pmetrics_device, user_data->q_id);
if (pmetrics_sq == NULL) {
LOG_ERR("SQ ID = %d does not exist", user_data->q_id);
err = -EPERM;
goto free_out;
}
/* Learn the command size */
cmd_buf_size =
(pmetrics_sq->private_sq.size / pmetrics_sq->public_sq.elements);
/* Check for SQ is full */
if ((((u32)pmetrics_sq->public_sq.tail_ptr_virt + 1UL) %
pmetrics_sq->public_sq.elements) ==
(u32)pmetrics_sq->public_sq.head_ptr) {
LOG_ERR("SQ is full");
err = -EPERM;
goto free_out;
}
/* Allocating memory for the command in kernel space */
nvme_cmd_ker = kmalloc(cmd_buf_size, GFP_ATOMIC | __GFP_ZERO);
if (nvme_cmd_ker == NULL) {
LOG_ERR("Unable to allocate kernel memory");
err = -ENOMEM;
goto free_out;
}
if (copy_from_user(nvme_cmd_ker, user_data->cmd_buf_ptr, cmd_buf_size)) {
LOG_ERR("Invalid copy from user space");
err = -EFAULT;
goto free_out;
}
nvme_gen_cmd = (struct nvme_gen_cmd *)nvme_cmd_ker;
memset(&prps, 0, sizeof(prps));
/* Copy and Increment the CMD ID, copy back to user space so can see ID */
user_data->unique_id = pmetrics_sq->private_sq.unique_cmd_id++;
nvme_gen_cmd->command_id = user_data->unique_id;
if (copy_to_user(cmd_request, user_data, sizeof(struct nvme_64b_send))) {
LOG_ERR("Unable to copy to user space");
err = -EFAULT;
goto fail_out;
}
/* Handling meta buffer */
if (user_data->bit_mask & MASK_MPTR) {
meta_buf = find_meta_node(pmetrics_device, user_data->meta_buf_id);
if (NULL == meta_buf) {
LOG_ERR("Meta Buff ID not found");
err = -EINVAL;
goto fail_out;
}
/* Add the required information to the command */
nvme_gen_cmd->metadata = cpu_to_le64(meta_buf->meta_dma_addr);
LOG_DBG("Metadata address: 0x%llx", nvme_gen_cmd->metadata);
}
/* Special handling for opcodes 0x00,0x01,0x04 and 0x05 of Admin cmd set */
if ((user_data->q_id == 0) && (nvme_gen_cmd->opcode == 0x01)) {
/* Create IOSQ command */
nvme_create_sq = (struct nvme_create_sq *) nvme_cmd_ker;
/* Get the required SQ from the global linked list from CMD.DW10.QID */
list_for_each_entry(p_cmd_sq, &pmetrics_device->metrics_sq_list,
sq_list_hd) {
if (nvme_create_sq->sqid == p_cmd_sq->public_sq.sq_id) {
q_ptr = (struct metrics_sq *)p_cmd_sq;
break;
}
}