-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostif.c
1550 lines (1312 loc) · 37 KB
/
hostif.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
// SPDX-License-Identifier: GPL-2.0+
/*
*
* hostif.c: nCipher PCI HSM linux host interface
* Copyright 2019 nCipher Security Ltd
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/bitops.h>
#include <linux/version.h>
#include "solo.h"
#include "i21555.h"
#include "fsl.h"
/* limits & sizes */
#define NFP_READ_MAX (8 * 1024)
#define NFP_WRITE_MAX (8 * 1024)
#define NFP_READBUF_SIZE (NFP_READ_MAX + 8)
#define NFP_WRITEBUF_SIZE (NFP_WRITE_MAX + 8)
#define NFP_MAXDEV 16
/* other operating constants */
#define NFP_TIMEOUT_SEC 20
#define NFP_DMA_NBYTES_OFFSET (4)
#define NFP_DMA_ADDRESS_OFFSET (8)
#define NFP_DRVNAME "nCipher nFast PCI driver"
#define NFP_TIMEOUT ((NFP_TIMEOUT_SEC) * HZ)
/* Interpretation of the bits of struct nfp_dev.rd_outstanding */
#define WAIT_BIT 0 /* waiting for data */
#define CMPLT_BIT 1 /* completing a read (got data or timing out) */
/*
* Used to determine which installed module we're looking at
* from its minor device number
*/
#define INODE_FROM_FILE(file) ((file)->f_path.dentry->d_inode)
/*
* Major device type
* "nCipher nFast PCI crypto accelerator" in
* https://www.kernel.org/doc/html/v4.11/admin-guide/devices.html
*/
#define NFP_MAJOR 176
/* device list */
static struct nfp_dev *nfp_dev_list[NFP_MAXDEV];
static int nfp_num_devices;
static struct class *nfp_class;
/**
* Module structures.
*/
/**
* NSHIELD_SOLO module interface version parameter.
*
* This value can be overridden when the module is loaded, for example:
*
* insmod nshield_solo.ko nfp_ifvers=<n>
*
* where n = 0 allows any supported interface,
* n > 0 allows only interface versions <= n.
* See nfdev-common.h for a list of supported interface versions.
* Specific card models may not support all interface versions.
*/
static int nfp_ifvers;
MODULE_AUTHOR("nCipher");
MODULE_DESCRIPTION("nCipher PCI HSM driver");
module_param(nfp_ifvers, int, 0444);
MODULE_PARM_DESC(nfp_ifvers, "maximum interface version (1-2), or any (0)");
MODULE_LICENSE("GPL");
/**
* NSHIELD_SOLO character device file operations.
*/
/**
* nfp_poll - Polls an NSHIELD SOLO device.
* @filep: device file pointer
* @wait: poll table pointer
*
* The kernel calls this function when a user tries to poll an NSHIELD SOLO
* device. The function returns a bit mask which indicates if the device is
* immediately readable or writable. A readable device will set
* (POLLIN | POLLRDNORM). A writable device will set (POLLOUT | POLLWRNORM).
*
* RETURNS: mask indicating if readable and/or writable.
*/
static u32 nfp_poll(struct file *file, poll_table *wait)
{
struct nfp_dev *ndev;
u32 mask = 0;
int minor = MINOR(INODE_FROM_FILE(file)->i_rdev);
if (minor >= NFP_MAXDEV) {
pr_err("%s: minor out of range.", __func__);
return -ENODEV;
}
ndev = nfp_dev_list[minor];
if (!ndev) {
pr_err("%s: error: no device", __func__);
return -ENODEV;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
/* find ndev from minor */
poll_wait(file, &ndev->wr_queue, wait);
poll_wait(file, &ndev->rd_queue, wait);
if (test_bit(0, &ndev->wr_ready))
mask |= POLLOUT | POLLWRNORM; /* writeable */
if (test_bit(0, &ndev->rd_ready))
mask |= POLLIN | POLLRDNORM; /* readable */
dev_notice(&ndev->pcidev->dev, "%s: device is %swritable, %sreadable",
__func__,
mask & POLLOUT ? "" : "not ", mask & POLLIN ? "" : "not ");
return mask;
}
void nfp_write_complete(struct nfp_dev *ndev, int ok)
{
/* check for device */
if (!ndev) {
pr_err("%s: error: no device", __func__);
return;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
/*
* could be executed simultaneously by more than one thread -
* e.g. from the write isr and from the nfp_write/timeout
* we don't want that to happen.
*/
if (test_and_set_bit(CMPLT_BIT, &ndev->wr_outstanding))
return;
if (!test_bit(WAIT_BIT, &ndev->wr_outstanding)) {
/*
* we can only get here if the write has already
* been completed
*/
dev_err(&ndev->pcidev->dev,
"%s: no write outstanding to complete; ignoring completion",
__func__);
clear_bit(CMPLT_BIT, &ndev->wr_outstanding);
return;
}
/* complete write by waking waiting processes */
ndev->wr_ok = ok;
dev_notice(&ndev->pcidev->dev, "%s: write completed %sokay",
__func__, ok ? "" : "not ");
/* make sure that write is complete before we clear wr_outstanding */
smp_mb__before_atomic();
clear_bit(CMPLT_BIT, &ndev->wr_outstanding);
clear_bit(WAIT_BIT, &ndev->wr_outstanding);
/* wake up anyone waiting */
wake_up_all(&ndev->wr_queue);
}
#define CREATE_TRACE_POINTS
/**
* nfp_write - Writes to an NSHIELD SOLO device.
* @file: device file pointer
* @buf: pointer to a user space buffer
* @count: size of user space buffer
* @off: offset position (ignored)
*
* Data in the user space buffer is written to the NSHIELD SOLO device. Any
* previous data is overwritten. An error is returned if not all
* bytes are written from the user space buffer.
*
* RETURNS: actual number of bytes written or a negative value if an error
* occurred.
*/
static ssize_t nfp_write(struct file *file, char const __user *buf,
size_t count, loff_t *off)
{
struct nfp_dev *ndev;
u32 addr;
int nbytes;
int minor;
int ne;
/* find ndev from minor */
minor = MINOR(INODE_FROM_FILE(file)->i_rdev);
if (minor >= NFP_MAXDEV) {
pr_err("%s: minor out of range.", __func__);
return -ENODEV;
}
ndev = nfp_dev_list[minor];
if (!ndev) {
pr_err("%s: error: no device", __func__);
return -ENODEV;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
/* check max length requested */
if (count <= 0 || NFP_WRITE_MAX < count) {
dev_err(&ndev->pcidev->dev,
"%s: invalid requested write length %lu",
__func__, count);
return -EINVAL;
}
/* check if called before ready */
if (!test_and_clear_bit(0, &ndev->wr_ready)) {
dev_err(&ndev->pcidev->dev, "%s: write called when not ready.",
__func__);
return -ENXIO;
}
set_bit(WAIT_BIT, &ndev->wr_outstanding);
dev_notice(&ndev->pcidev->dev, "%s: writing %ld bytes",
__func__, count);
addr = 0;
if (ndev->ifvers >= NFDEV_IF_PCI_PULL) {
dev_notice(&ndev->pcidev->dev,
"%s: copying %lu bytes to dma buffer",
__func__, count);
addr = ndev->write_dma;
nbytes = cpu_to_le32(count);
*(u32 *)(ndev->write_buf + NFP_DMA_NBYTES_OFFSET) = nbytes;
if (0 !=
copy_from_user(ndev->write_buf + NFP_DMA_ADDRESS_OFFSET,
buf, count)) {
clear_bit(WAIT_BIT, &ndev->wr_outstanding);
set_bit(0, &ndev->wr_ready);
dev_err(&ndev->pcidev->dev,
"%s: copy from user space failed", __func__);
return -EIO;
}
}
ne = ndev->cmddev->write_block(addr, buf, count, ndev->cmdctx);
if (ne != 0) {
nfp_write_complete(ndev, 0);
if (ne != -EAGAIN)
dev_err(&ndev->pcidev->dev,
"%s: write_block failed", __func__);
}
while (test_bit(WAIT_BIT, &ndev->wr_outstanding)) {
if (!wait_event_timeout(ndev->wr_queue,
test_bit(WAIT_BIT,
&ndev->wr_outstanding) == 0,
NFP_TIMEOUT)) {
nfp_write_complete(ndev, 0);
set_bit(0, &ndev->wr_ready);
dev_err(&ndev->pcidev->dev,
"%s: module timed out", __func__);
return -ENXIO;
}
if (test_bit(WAIT_BIT, &ndev->wr_outstanding)) {
dev_err(&ndev->pcidev->dev,
"%s: handling spurious wake-up", __func__);
}
}
set_bit(0, &ndev->wr_ready);
dev_warn(&ndev->pcidev->dev, "%s: returning %ld.", __func__,
ndev->wr_ok ? count : -EIO);
if (!ndev->wr_ok) {
dev_err(&ndev->pcidev->dev,
"%s: device write failed", __func__);
return -EIO;
}
dev_warn(&ndev->pcidev->dev,
"%s: wrote %ld bytes (%s)", __func__, count,
ndev->ifvers >= NFDEV_IF_PCI_PULL ? "dma" : "std");
return count;
}
void nfp_read_complete(struct nfp_dev *ndev, int ok)
{
/* check for device */
if (!ndev) {
pr_err("%s: error: no device", __func__);
return;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
/*
* could be executed simultaneously by more than one thread -
* e.g. from the read isr and from the timeout
* we don't want that to happen.
*/
if (test_and_set_bit(CMPLT_BIT, &ndev->rd_outstanding))
return;
if (!test_bit(WAIT_BIT, &ndev->rd_outstanding)) {
/*
* we can only get here if the read has already been completed
* and no new ENSUREREADING request has been received since
*/
dev_err(&ndev->pcidev->dev,
"%s: no read outstanding to complete; ignoring completion",
__func__);
clear_bit(CMPLT_BIT, &ndev->rd_outstanding);
return;
}
/* in case the timer has not expired */
del_timer(&ndev->rd_timer);
ndev->rd_ok = ok;
set_bit(0, &ndev->rd_ready);
dev_notice(&ndev->pcidev->dev, "%s: read completed %sokay",
__func__, ok ? "" : "not ");
/* make sure that rd_ready is set before we clear rd_outstanding */
smp_mb__before_atomic();
clear_bit(CMPLT_BIT, &ndev->rd_outstanding);
clear_bit(WAIT_BIT, &ndev->rd_outstanding);
/* wake up anyone waiting */
wake_up_all(&ndev->rd_queue);
}
static void nfp_read_timeout(struct timer_list *t)
{
struct nfp_dev *ndev = from_timer(ndev, t, rd_timer);
/* check for device */
if (!ndev) {
pr_err("%s: error: no device", __func__);
return;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
nfp_read_complete(ndev, 0);
}
/**
* nfp_read - Reads from an NSHIELD SOLO device.
* @file: device file pointer
* @buf: pointer to a user space buffer
* @count: maximum size of user space buffer
* @off: offset position (ignored)
*
* Data is read from the NSHIELD SOLO device into the user space buffer.
* The read removes the data from the device. An error is returned is not
* all the bytes are read from the user space buffer.
*
* RETURNS: actual number of bytes read or a negative value if an error
* occurred.
*/
static ssize_t nfp_read(struct file *file, char __user *buf, size_t count,
loff_t *off)
{
struct nfp_dev *ndev;
int nbytes;
int minor;
int ne;
minor = MINOR(INODE_FROM_FILE(file)->i_rdev);
if (minor >= NFP_MAXDEV) {
pr_err("%s: minor out of range.", __func__);
return -ENODEV;
}
ndev = nfp_dev_list[minor];
if (!ndev) {
pr_err("%s: error: no device", __func__);
return -ENODEV;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
/* check user space buffer */
if (!access_ok(buf, count)) {
dev_err(&ndev->pcidev->dev,
"%s: user space verify failed.", __func__);
return -EFAULT;
}
/* check max length requested */
if (count <= 0 || NFP_READ_MAX < count) {
dev_err(&ndev->pcidev->dev,
"%s: invalid requested max read length %lu",
__func__, count);
return -EINVAL;
}
if (!test_and_clear_bit(0, &ndev->rd_ready)) {
dev_err(&ndev->pcidev->dev,
"%s: read called when not ready.", __func__);
return -EIO;
}
nbytes = 0;
/* check if read was ok */
if (!ndev->rd_ok) {
dev_err(&ndev->pcidev->dev, "%s: read failed", __func__);
return -EIO;
}
/* finish read */
if (ndev->ifvers >= NFDEV_IF_PCI_PUSH) {
nbytes = *(u32 *)(ndev->read_buf + NFP_DMA_NBYTES_OFFSET);
nbytes = le32_to_cpu(nbytes);
dev_notice(&ndev->pcidev->dev,
"%s: nbytes %d", __func__, nbytes);
if (nbytes < 0 || nbytes > count) {
dev_err(&ndev->pcidev->dev,
"%s: bad byte count (%d) from device",
__func__, nbytes);
return -EIO;
}
if (copy_to_user(buf, ndev->read_buf + NFP_DMA_ADDRESS_OFFSET,
nbytes) != 0) {
dev_err(&ndev->pcidev->dev,
"%s: copy to user space failed", __func__);
return -EIO;
}
} else {
nbytes = 0;
ne = ndev->cmddev->read_block(buf, count, ndev->cmdctx,
(void *)&nbytes);
if (ne != 0) {
dev_err(&ndev->pcidev->dev,
"%s: device read failed", __func__);
return ne;
}
}
if (nbytes > NFP_READ_MAX) {
dev_err(&ndev->pcidev->dev,
"%s: read reply overflow: %d > %d max",
__func__, nbytes, NFP_READ_MAX);
return -EIO;
}
dev_warn(&ndev->pcidev->dev, "%s: read %d bytes (%s)", __func__, nbytes,
ndev->ifvers >= NFDEV_IF_PCI_PUSH ? "dma" : "std");
return nbytes;
}
static int nfp_alloc_pci_push(struct nfp_dev *ndev)
{
/*
* allocate resources needed for PCI Push,
* if not already allocated.
* return True if successful
*/
if (!ndev->read_buf) {
ndev->read_buf =
kzalloc(NFP_READBUF_SIZE, GFP_KERNEL | GFP_DMA);
if (!ndev->read_buf)
return -ENOMEM;
ndev->read_dma =
dma_map_single(&ndev->pcidev->dev, ndev->read_buf,
NFP_READBUF_SIZE, DMA_BIDIRECTIONAL);
if (dma_mapping_error(&ndev->pcidev->dev, ndev->read_dma)) {
dev_err(&ndev->pcidev->dev,
"dma_mapping_error found after attempting dma_map_single");
kfree(ndev->read_buf);
ndev->read_buf = NULL;
ndev->read_dma = 0;
}
}
return ndev->read_buf ? 1 : 0;
}
static int nfp_alloc_pci_pull(struct nfp_dev *ndev)
{
/*
* allocate resources needed for PCI Pull,
* if not already allocated.
* return True if successful
*/
if (!ndev->write_buf) {
ndev->write_buf =
kzalloc(NFP_WRITEBUF_SIZE, GFP_KERNEL | GFP_DMA);
if (!ndev->write_buf)
return -ENOMEM;
ndev->write_dma = dma_map_single(&ndev->pcidev->dev,
ndev->write_buf,
NFP_WRITEBUF_SIZE,
DMA_BIDIRECTIONAL);
if (dma_mapping_error(&ndev->pcidev->dev, ndev->write_dma)) {
dev_err(&ndev->pcidev->dev,
"dma_mapping_error found after attempting dma_map_single");
kfree(ndev->write_buf);
ndev->write_buf = NULL;
ndev->write_dma = 0;
}
}
return ndev->write_buf ? 1 : 0;
}
static void nfp_free_pci_push(struct nfp_dev *ndev)
{
/* free resources allocated to PCI Push */
if (ndev->read_buf) {
dma_unmap_single(&ndev->pcidev->dev, ndev->read_dma,
NFP_READBUF_SIZE, DMA_BIDIRECTIONAL);
kfree(ndev->read_buf);
ndev->read_buf = NULL;
ndev->read_dma = 0;
}
}
static void nfp_free_pci_pull(struct nfp_dev *ndev)
{
/* free resources allocated to PCI Pull */
if (ndev->write_buf) {
dma_unmap_single(&ndev->pcidev->dev, ndev->write_dma,
NFP_WRITEBUF_SIZE, DMA_BIDIRECTIONAL);
kfree(ndev->write_buf);
ndev->write_buf = NULL;
ndev->write_dma = 0;
}
}
/**
* nfp_set_ifvers - Sets device interface version.
* @ndev: an NSHIELD SOLO device
* @ifvers: interface version
*
* RETURNS: interface version actually set.
*/
static int nfp_set_ifvers(struct nfp_dev *ndev, int ifvers)
{
int max_ifvers;
/* check for device */
if (!ndev) {
pr_err("%s: error: no device", __func__);
return 0; /* no interface version */
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
max_ifvers = ndev->cmddev->max_ifvers;
if (nfp_ifvers != 0 && max_ifvers > nfp_ifvers)
max_ifvers = nfp_ifvers;
/* on any error, ifvers remains unchanged */
if (ifvers < 0 || ifvers > max_ifvers) {
/* invalid nfp_ifvers: set to max as fallback */
dev_err(&ndev->pcidev->dev, "%s: %d out of allowable range [0:%d]",
__func__, ifvers, max_ifvers);
return ndev->ifvers;
}
if (ifvers == 0 &&
ndev->cmddev->deviceid == PCI_DEVICE_ID_FREESCALE_T1022) {
/*
* default ifvers: set to max for ngsolo not for legacy!
* The legacy card needs it set to 0 when in Maintenance
* mode and then the hardserver steps it up to ifvers 2
* when switching back to Operational mode. Solo XC starts
* at the max (3) whenever possible.
*/
ifvers = max_ifvers;
}
if (ifvers >= NFDEV_IF_PCI_PUSH) {
if (!nfp_alloc_pci_push(ndev)) {
dev_err(&ndev->pcidev->dev,
"%s: can't set ifvers %d as resources not available",
__func__, ifvers);
return ndev->ifvers;
}
} else {
nfp_free_pci_push(ndev);
}
if (ifvers >= NFDEV_IF_PCI_PULL) {
if (!nfp_alloc_pci_pull(ndev)) {
dev_err(&ndev->pcidev->dev,
"%s: can't set ifvers %d as resources not available",
__func__, ifvers);
return ndev->ifvers;
}
} else {
nfp_free_pci_pull(ndev);
}
ndev->ifvers = ifvers;
dev_warn(&ndev->pcidev->dev,
"%s: setting ifvers = %d", __func__, ifvers);
return ifvers;
}
/**
* nfp_ioctl - Performs an NSHIELD SOLO device IOCTL call.
* @inode: device inode pointer
* @file: device file pointer
* @cmd: command id
* @arg: command argument
*
* RETURNS: 0 if successful or other value if error.
*/
static int nfp_ioctl(struct inode *inode,
struct file *file,
u32 cmd,
u64 arg)
{
struct nfp_dev *ndev;
int minor;
/* find ndev from minor */
minor = MINOR(INODE_FROM_FILE(file)->i_rdev);
if (minor >= NFP_MAXDEV) {
pr_err("%s: minor out of range.", __func__);
return -ENODEV;
}
ndev = nfp_dev_list[minor];
/* check for device */
if (!ndev) {
pr_err("%s: error: no device", __func__);
return -ENODEV;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
switch (cmd) {
case NFDEV_IOCTL_ENQUIRY: {
struct nfdev_enquiry_str enq_data;
int err = -EIO;
dev_dbg(&ndev->pcidev->dev, "%s: enquiry", __func__);
enq_data.busno = ndev->busno;
enq_data.slotno = ndev->slotno;
if ((void *)arg) {
err = copy_to_user((void __user *)arg, &enq_data,
sizeof(enq_data)) ? -EFAULT : 0;
if (err) {
dev_err(&ndev->pcidev->dev,
"%s: copy to user space failed.",
__func__);
return err;
}
} else {
dev_err(&ndev->pcidev->dev,
"%s enquiry: arg pointer is NULL!", __func__);
return err;
}
} break;
case NFDEV_IOCTL_ENSUREREADING: {
dma_addr_t addr;
u32 len;
int err = -EIO;
int ne;
dev_dbg(&ndev->pcidev->dev, "%s: ensure reading", __func__);
/* get and check max length */
if ((void *)arg) {
err = copy_from_user((void *)&len, (void __user *)arg,
sizeof(u32)) ? -EFAULT : 0;
if (err) {
dev_err(&ndev->pcidev->dev,
"%s: ensure reading: copy from user space failed.",
__func__);
return err;
}
/* signal a read to the module */
dev_warn(&ndev->pcidev->dev,
"%s: signalling read request to module, len = %x.",
__func__, len);
if (len > NFP_READ_MAX) {
dev_err(&ndev->pcidev->dev, "%s: len > %x = %x.",
__func__, NFP_READ_MAX, len);
return -EINVAL;
}
} else {
dev_err(&ndev->pcidev->dev,
"%s ensure reading: arg pointer is NULL!",
__func__);
return err;
}
if (len <= 0 || NFP_READ_MAX < len) {
dev_err(&ndev->pcidev->dev,
"%s: ensure reading: invalid max length %d/%d",
__func__, len, NFP_READ_MAX);
err = -EINVAL;
return err;
}
/* check if okay to start read */
if (test_and_set_bit(WAIT_BIT, &ndev->rd_outstanding)) {
dev_err(&ndev->pcidev->dev,
"%s: ensure reading: another read is outstanding",
__func__);
return -EBUSY;
}
dev_warn(&ndev->pcidev->dev,
"%s: ndev->rd_outstanding=1", __func__);
/* start read ready timeout */
mod_timer(&ndev->rd_timer, jiffies + (NFP_TIMEOUT_SEC * HZ));
dev_warn(&ndev->pcidev->dev, "%s: read request", __func__);
/* start read */
addr = (ndev->ifvers < NFDEV_IF_PCI_PUSH) ? 0 : ndev->read_dma;
dev_notice(&ndev->pcidev->dev,
"%s: ensure reading: read request with ifvers=%d addr=%p",
__func__, ndev->ifvers, (void *)addr);
ne = ndev->cmddev->ensure_reading(addr, len, ndev->cmdctx);
if (ne != 0) {
dev_err(&ndev->pcidev->dev,
"%s: ensure reading: read request failed",
__func__);
del_timer_sync(&ndev->rd_timer);
/*
* make sure that del_timer_sync is done before
* we clear rd_outstanding
*/
smp_mb__before_atomic();
clear_bit(WAIT_BIT, &ndev->rd_outstanding);
return -EIO;
}
} break;
case NFDEV_IOCTL_PCI_IFVERS: {
int vers, err = -EIO;
dev_dbg(&ndev->pcidev->dev, "%s: set ifvers", __func__);
if ((void *)arg) {
err = copy_from_user(&vers, (void __user *)arg,
sizeof(vers)) ? -EFAULT : 0;
if (err) {
dev_err(&ndev->pcidev->dev,
"%s: set ifvers: copy from user space failed.",
__func__);
return err;
}
} else {
dev_err(&ndev->pcidev->dev,
"%s ifvers: arg pointer is NULL!",
__func__);
return err;
}
if (test_bit(WAIT_BIT, &ndev->rd_outstanding)) {
dev_err(&ndev->pcidev->dev,
"%s: set ifvers: unable to set interface version while read outstanding",
__func__);
return -EIO;
}
nfp_set_ifvers(ndev, vers);
} break;
case NFDEV_IOCTL_STATS: {
int err = -EIO;
dev_dbg(&ndev->pcidev->dev, "%s: stats", __func__);
if ((void *)arg) {
err = copy_to_user((void __user *)arg, &ndev->stats,
sizeof(struct nfdev_stats_str))
? -EFAULT : 0;
if (err) {
dev_err(&ndev->pcidev->dev,
"%s: stats: copy to user space failed.",
__func__);
return err;
}
} else {
dev_err(&ndev->pcidev->dev,
"%s stats: arg pointer is NULL!", __func__);
return err;
}
} break;
case NFDEV_IOCTL_CONTROL: {
int err = -EIO;
struct nfdev_control_str control;
dev_dbg(&ndev->pcidev->dev, "%s: control", __func__);
if ((void *)arg) {
err = copy_from_user(&control, (void __user *)arg,
sizeof(control)) ? -EFAULT : 0;
if (err) {
dev_err(&ndev->pcidev->dev,
"%s: control: copy from user space failed.",
__func__);
return err;
}
} else {
dev_err(&ndev->pcidev->dev,
"%s control: arg pointer is NULL!", __func__);
return err;
}
if (!ndev->cmddev->setcontrol) {
dev_warn(&ndev->pcidev->dev,
"%s: control: set control not supported for this device: ignored",
__func__);
return -EINVAL;
}
dev_notice(&ndev->pcidev->dev,
"%s: control: updating HSM control register to 0x%x.",
__func__,
control.control);
return ndev->cmddev->setcontrol(&control, ndev->cmdctx);
} break;
case NFDEV_IOCTL_STATUS: {
int err = -EIO;
struct nfdev_status_str status;
dev_dbg(&ndev->pcidev->dev, "%s: status", __func__);
if (!ndev->cmddev->getstatus) {
dev_warn(&ndev->pcidev->dev,
"%s: status not supported for this device: ignored",
__func__);
return -EINVAL;
}
err = ndev->cmddev->getstatus(&status,
ndev->cmdctx);
if (err)
return err;
if ((void *)arg) {
err = copy_to_user((void __user *)arg, &status,
sizeof(status)) ? -EFAULT : 0;
if (err) {
dev_err(&ndev->pcidev->dev,
"%s: status: copy from user space failed.",
__func__);
return err;
}
} else {
dev_err(&ndev->pcidev->dev,
"%s status: arg pointer is NULL!",
__func__);
return err;
}
dev_notice(&ndev->pcidev->dev,
"%s: read status: 0x%x, error: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
__func__,
status.status, status.error[0], status.error[1],
status.error[2], status.error[3], status.error[4],
status.error[5], status.error[6], status.error[7]);
} break;
default: {
dev_err(&ndev->pcidev->dev, "%s: unknown ioctl.", __func__);
return -EINVAL;
} break;
}
return 0;
}
/**
* nfp_unlocked_ioctl - Performs an NSHIELD SOLO device unlocked IOCTL call.
* @file: device file pointer
* @cmd: command id
* @arg: command argument
*
* RETURNS: 0 if successful or other value if error.
*/
static long nfp_unlocked_ioctl(struct file *file,
u32 cmd,
unsigned long arg)
{
long ret;
int minor;
struct nfp_dev *ndev;
minor = MINOR(INODE_FROM_FILE(file)->i_rdev);
if (minor >= NFP_MAXDEV) {
pr_err("%s: minor out of range.", __func__);
return -ENODEV;
}
ndev = nfp_dev_list[minor];
if (!ndev) {
pr_err("%s: error: no device", __func__);
return -ENODEV;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
mutex_lock(&ndev->ioctl_mutex);
ret = nfp_ioctl(NULL, file, cmd, arg);
mutex_unlock(&ndev->ioctl_mutex);
dev_dbg(&ndev->pcidev->dev, "%s: left", __func__);
return ret;
}
static irqreturn_t nfp_isr(int irq, void *context)
{
struct nfp_dev *ndev = (struct nfp_dev *)context;
int handled;
int ne;
/* check for device */
if (!ndev) {
pr_err("%s: error: no device", __func__);
return IRQ_NONE;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
ne = ndev->cmddev->isr(ndev->cmdctx, &handled);
if (ne != 0)
dev_err(&ndev->pcidev->dev, "%s: cmddev isr failed (%d)",
__func__, ne);
return IRQ_RETVAL(handled);
}
/**
* nfp_open - Opens an NSHIELD SOLO device.
* @inode: device inode pointer
* @file: device file pointer
*
* The kernel calls this function when a user tries to open an
* NSHIELD SOLO device. It is an error to attempt to open an
* already opened device.
*
* RETURNS: 0 if successful or other value if error.
*/
static int nfp_open(struct inode *inode, struct file *file)
{
struct nfp_dev *ndev;
int ne;
int minor = MINOR(INODE_FROM_FILE(file)->i_rdev);
/* find ndev */
if (minor >= NFP_MAXDEV) {
pr_err("%s: minor out of range.", __func__);
return -ENODEV;
}
ndev = nfp_dev_list[minor];
if (!ndev) {
pr_err("%s: cannot find dev %d.", __func__, minor);
return -ENODEV;
}
dev_dbg(&ndev->pcidev->dev, "%s: entered", __func__);
dev_notice(&ndev->pcidev->dev,
"%s: opening file at %p.", __func__, file);
/* check if already open */
if (atomic_read(&ndev->busy)) {
dev_err(&ndev->pcidev->dev, "%s: device %s busy", __func__,
pci_name(ndev->pcidev));
return -EBUSY;
}
atomic_set(&ndev->busy, 1);