-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfpga-cfg.c
2110 lines (1800 loc) · 50.1 KB
/
fpga-cfg.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
/*
* FPGA configuration driver for FPP/SPI/CvP/PR.
*
* Copyright (C) 2017 DENX Software Engineering
* Anatolij Gustschin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that 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.
*/
#if 1
#define DEBUG
#endif
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/firmware.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/fpga/fpga-mgr.h>
#include <linux/uaccess.h>
#include <linux/fsnotify.h>
#include <linux/idr.h>
#if LINUX_VERSION_CODE <= KERNEL_VERSION(4, 10, 0)
#include <linux/sched.h>
#else
#include <linux/sched/clock.h>
#endif
#define FPGA_DRV_STRING "fpga_cfg"
#define FPP_RING_MGR_NAME "ftdi-fpp-fpga-mgr"
#if LINUX_VERSION_CODE <= KERNEL_VERSION(4, 11, 0)
#define SPI_RING_MGR_NAME "Altera Cyclone PS SPI FPGA Manager"
#define SPI_XLNX_MGR_NAME "Xilinx Slave Serial FPGA Manager"
#else
#define SPI_RING_MGR_NAME "altera-ps-spi"
#define SPI_XLNX_MGR_NAME "xlnx-slave-spi"
#endif
#define FPGA_CFG_HISTORY_ENTRIES_MIN 500
#define FPGA_CFG_HISTORY_ENTRIES_MAX 10000
#define FPGA_CFG_HISTORY_ENTRIES_DFLT 5000
static unsigned int fpgacfg_hist_len = FPGA_CFG_HISTORY_ENTRIES_DFLT;
module_param(fpgacfg_hist_len, uint, 0);
MODULE_PARM_DESC(fpgacfg_hist_len,
"Max number of entries for FPGA config operations history");
static DEFINE_MUTEX(mgr_list_lock);
static struct list_head mgr_devs = LIST_HEAD_INIT(mgr_devs);
static struct list_head pci_dev_wait_list = LIST_HEAD_INIT(pci_dev_wait_list);
static struct dentry *dbgfs_root;
static struct class *fpga_mgr_class;
static DEFINE_IDA(fpga_cfg_ida);
struct fpga_cfg_device {
struct list_head list;
struct fpga_manager *mgr;
struct platform_device *pdev;
};
enum fpga_cfg_mgr_type {
NOP_MGR,
FPP_RING_MGR,
SPI_RING_MGR,
CVP_MGR,
PR_MGR,
FPP_META,
SPI_META,
CVP_META,
PR_META,
SPI_MGR,
CFG_BUS_NR,
CFG_USB_ID,
CFG_TYPE,
CFG_BS_LSB,
FPGA_DRV,
FPGA_DRV_ARGS,
};
struct fpga_cfg_mgr {
const char *mgr_name;
enum fpga_cfg_mgr_type mgr_type;
};
struct fpga_cfg_mgr fpga_cfg_mgr_tbl[] = {
{ FPP_RING_MGR_NAME, FPP_RING_MGR },
{ SPI_RING_MGR_NAME, SPI_RING_MGR },
{ SPI_XLNX_MGR_NAME, SPI_MGR },
{ },
};
struct cfg_desc {
struct fpga_manager *mgr;
struct device *mgr_dev;
u64 cfg_ts_nsec;
char firmware[NAME_MAX];
char metadata[NAME_MAX];
char firmware_abs[PATH_MAX + NAME_MAX];
char metadata_abs[PATH_MAX + NAME_MAX];
char log_tmp[PATH_MAX + NAME_MAX + 64];
};
struct fpga_cfg_log_entry {
struct list_head list;
size_t len;
char entry[];
};
struct fpga_cfg_fpga_inst;
struct fpga_cfg_attribute {
struct attribute attr;
ssize_t (*show)(struct fpga_cfg_fpga_inst *,
struct attribute *, char *);
ssize_t (*store)(struct fpga_cfg_fpga_inst *, struct attribute *,
const char *, size_t);
};
struct fpga_cfg_fpga_inst {
struct fpga_cfg *cfg;
size_t idx;
struct kobject kobj_fpga_dir;
struct work_struct pci_rm_work;
wait_queue_head_t wq_bind;
wait_queue_head_t wq_unbind;
struct list_head link;
struct pci_dev *pci_dev;
const char *driver_to_bind;
bool drv_bound;
int bus;
int dev;
int func;
int debug;
char bdf[16];
char type[16];
char usb_dev_id[16];
char fpga_drv[48];
char fpga_drv_args[2048];
int bs_lsb_first;
enum fpga_cfg_mgr_type mgr_type;
struct cfg_desc fpp;
struct cfg_desc spi;
struct cfg_desc cvp;
struct cfg_desc pr;
struct attribute_group pr_attr_grp;
struct attribute *pr_attrs[3];
struct fpga_cfg_attribute pr_image_attr;
struct fpga_cfg_attribute pr_meta_attr;
size_t pr_seq_num;
size_t cfg_seq_num;
char pr_image_attr_name[16];
char pr_meta_attr_name[16];
enum fpga_cfg_mgr_type cfg_op1;
enum fpga_cfg_mgr_type cfg_op2;
bool cfg_done;
bool history_header;
struct mutex history_lock;
struct list_head history_list;
size_t history_max_entries;
size_t history_entries;
unsigned hist_count;
unsigned hist_count_new;
wait_queue_head_t hist_queue;
struct dentry *dbgfs_history;
};
struct fpga_cfg {
struct platform_device *pdev;
struct class *mgr_class;
struct dentry *dbgfs_devdir;
char dir_buf[16];
struct fpga_cfg_fpga_inst fpga;
};
struct fpga_cfg_platform_data {
struct fpga_manager *mgr;
enum fpga_cfg_mgr_type mgr_type;
};
struct modprobe_data {
char *module_name;
char *module_args;
bool remove;
};
static void fpga_cfg_modprobe_cleanup(struct subprocess_info *info)
{
struct modprobe_data *data = info->data;
if (data) {
if (!data->remove)
kfree(data->module_args);
kfree(data->module_name);
}
kfree(info->argv);
}
#define MAX_MOD_ARGS 32
/* 6 = 5 (for modprobe command and its args) + 1 (for NULL termination) */
#define MAX_ARGV (6 + MAX_MOD_ARGS)
char modprobe_path[] = "/sbin/modprobe";
static int fpga_cfg_modprobe(char *module_name, int wait,
bool remove, char *module_args)
{
struct subprocess_info *info;
struct modprobe_data *data;
char **argv;
static char *envp[] = {
"HOME=/",
"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
"TERM=linux",
NULL
};
argv = kzalloc(sizeof(char *[MAX_ARGV]), GFP_KERNEL);
if (!argv)
goto out;
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (!data)
goto free_argv;
data->module_name = kstrdup(module_name, GFP_KERNEL);
if (!data->module_name)
goto free_data;
data->remove = remove;
argv[0] = modprobe_path;
argv[1] = "-q";
if (remove) {
argv[2] = "-r";
argv[3] = "--";
argv[4] = data->module_name;
argv[5] = NULL;
} else {
argv[2] = "--";
argv[3] = data->module_name;
argv[4] = NULL;
/*
* No module parameter passing needed any more here. The
* parameters are now passed via platform_data to the
* device instead (device specific)
*/
}
info = call_usermodehelper_setup(modprobe_path, argv, envp,
GFP_KERNEL, NULL,
fpga_cfg_modprobe_cleanup, data);
if (!info) {
if (!remove)
kfree(data->module_args);
goto free_module_name;
}
return call_usermodehelper_exec(info, wait | UMH_KILLABLE);
free_module_name:
kfree(data->module_name);
free_data:
kfree(data);
free_argv:
kfree(argv);
out:
return -ENOMEM;
}
static int fpga_cfg_add_new_mgr(struct platform_device *pdev,
struct fpga_manager *mgr)
{
struct fpga_cfg_device *cfg;
/* first, check if this mgr is already listed */
mutex_lock(&mgr_list_lock);
list_for_each_entry(cfg, &mgr_devs, list) {
if (cfg->mgr == mgr) {
mutex_unlock(&mgr_list_lock);
return 0;
}
}
mutex_unlock(&mgr_list_lock);
cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
if (!cfg) {
pr_err("failed to alloc memory\n");
return -ENOMEM;
}
cfg->mgr = mgr;
cfg->pdev = pdev;
mutex_lock(&mgr_list_lock);
list_add_tail(&cfg->list, &mgr_devs);
mutex_unlock(&mgr_list_lock);
return 0;
}
static int fpga_cfg_create_inst(struct fpga_manager *mgr)
{
struct fpga_cfg_platform_data pdata;
struct platform_device *pdev;
enum fpga_cfg_mgr_type mgr_type;
const char *name;
int i, id;
bool found;
size_t len;
mgr_type = NOP_MGR;
found = false;
if (!strncmp("Altera CvP", mgr->name, 10))
return 0; /* No ring mgr, no config instance */
for (i = 0; fpga_cfg_mgr_tbl[i].mgr_name; i++) {
name = fpga_cfg_mgr_tbl[i].mgr_name;
len = strlen(name);
pr_debug("LOOKING for [ %s ] in '%s'\n", name, mgr->name);
if (!strncmp(name, mgr->name, len)) {
mgr_type = fpga_cfg_mgr_tbl[i].mgr_type;
found = true;
break;
}
}
if (!found) {
pr_debug("NO RING MGR found\n");
/*
* No ring fpga manager or no single fpga manager found,
* so won't create the configuration interface device
*/
return 0;
}
id = ida_simple_get(&fpga_cfg_ida, 0, 0, GFP_KERNEL);
if (id < 0) {
return id;
}
pdata.mgr = mgr;
pdata.mgr_type = mgr_type;
pdev = platform_device_register_data(NULL, "fpga-cfg", id,
&pdata, sizeof(pdata));
if (IS_ERR(pdev)) {
pr_err("Can't create FPGA config device: %ld\n", PTR_ERR(pdev));
ida_simple_remove(&fpga_cfg_ida, id);
return PTR_ERR(pdev);
}
fpga_cfg_add_new_mgr(pdev, mgr);
return 0;
}
static void fpga_cfg_remove_inst(struct fpga_manager *mgr)
{
struct fpga_cfg_device *cfg, *tmp_cfg;
pr_debug("%s: ##### remove mgr %s\n", __func__, mgr->name);
mutex_lock(&mgr_list_lock);
list_for_each_entry_safe(cfg, tmp_cfg, &mgr_devs, list) {
if (cfg->mgr == mgr) {
list_del(&cfg->list);
if (cfg->mgr)
pr_debug("remove: '%s'\n", cfg->mgr->name);
platform_device_unregister(cfg->pdev);
kfree(cfg);
}
}
mutex_unlock(&mgr_list_lock);
}
static int fpga_cfg_mgr_ncb(struct notifier_block *nb, unsigned long val,
void *priv)
{
struct fpga_manager *mgr = priv;
int result = NOTIFY_OK;
if (!mgr)
return NOTIFY_BAD;
if (!fpga_mgr_class)
fpga_mgr_class = mgr->dev.class;
switch (val) {
case FPGA_MGR_ADD:
fpga_cfg_create_inst(mgr);
break;
case FPGA_MGR_REMOVE:
fpga_cfg_remove_inst(mgr);
break;
default:
WARN_ON(1);
result = NOTIFY_BAD;
}
return result;
}
static struct notifier_block fpga_mgr_notifier = {
.notifier_call = fpga_cfg_mgr_ncb,
};
static struct pci_dev *fpga_cfg_find_cvp_dev(struct fpga_cfg_fpga_inst *inst)
{
struct pci_dev *pdev;
struct device *dev;
unsigned int devfn;
dev = &inst->cfg->pdev->dev;
devfn = PCI_DEVFN(inst->dev, inst->func);
if (inst->debug)
dev_dbg(dev, "find bus %02x, devfn %d\n", inst->bus, devfn);
pdev = pci_get_domain_bus_and_slot(0, inst->bus, devfn);
if (!pdev) {
if (inst->debug)
dev_dbg(dev, "Can't find CvP/PR PCIe device '%s'\n",
inst->bdf);
return NULL;
}
if (inst->debug)
dev_dbg(dev, "found CvP device '%s'\n", dev_name(&pdev->dev));
return pdev;
}
static char fpga_cfg_mgr_name_buf[128];
static char fpga_cfg_mgr_name_addr_buf[16];
static int fpga_cfg_detach(struct device *dev, void *data)
{
struct fpga_manager *mgr = to_fpga_manager(dev);
fpga_cfg_remove_inst(mgr);
return 0;
}
static int fpga_cfg_detach_mgrs(struct class *class)
{
return class_for_each_device(class, NULL, NULL, fpga_cfg_detach);
}
static int fpga_cfg_history_header(struct fpga_cfg_fpga_inst *inst)
{
struct fpga_cfg_log_entry *log;
int len = 0, ret = 0;
char *buf;
buf = kmalloc(128, GFP_KERNEL);
if (!buf) {
ret = -ENOMEM;
goto err;
}
len += snprintf(buf, 128, "=== Config Log for %s device @ %s ===\n",
inst->type, inst->bdf);
log = kmalloc(sizeof(*log) + len + 1, GFP_KERNEL);
if (log) {
strncpy(log->entry, buf, len + 1);
log->len = len;
mutex_lock(&inst->history_lock);
list_add_tail(&log->list, &inst->history_list);
inst->hist_count_new += len;
inst->history_header = true;
mutex_unlock(&inst->history_lock);
} else {
kfree(buf);
ret = -ENOMEM;
goto err;
}
kfree(buf);
return ret;
err:
dev_warn(&inst->cfg->pdev->dev, "No memory for log entry.\n");
return ret;
}
static void fpga_cfg_free_log(struct fpga_cfg_fpga_inst *inst)
{
struct fpga_cfg_log_entry *log, *tmp;
mutex_lock(&inst->history_lock);
list_for_each_entry_safe(log, tmp, &inst->history_list, list) {
list_del(&log->list);
kfree(log);
}
inst->history_header = false;
inst->hist_count = 0;
inst->hist_count_new = 0;
mutex_unlock(&inst->history_lock);
}
static void fpga_cfg_update_hist_attr(struct fpga_cfg_fpga_inst *inst)
{
struct iattr newattrs = {};
newattrs.ia_valid = ATTR_SIZE | ATTR_FORCE;
newattrs.ia_size = inst->hist_count_new;
inode_lock(d_inode(inst->dbgfs_history));
notify_change(inst->dbgfs_history, &newattrs, NULL);
inode_unlock(d_inode(inst->dbgfs_history));
}
static ssize_t fpga_cfg_history_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
struct fpga_cfg_fpga_inst *inst = file->private_data;
struct fpga_cfg_log_entry *log, *tmp;
size_t read_cnt, read_max;
loff_t line_offs, offs = 0;
int ret;
if (!count)
return 0;
if (!inst->hist_count_new)
return 0;
if (*ppos >= inst->hist_count_new)
return 0;
read_cnt = 0;
read_max = count;
mutex_lock(&inst->history_lock);
list_for_each_entry_safe(log, tmp, &inst->history_list, list) {
if (*ppos >= (offs + log->len)) {
offs += log->len;
continue;
}
line_offs = *ppos - offs;
ret = simple_read_from_buffer(buf + read_cnt, read_max,
&line_offs, log->entry, log->len);
if (ret < 0) {
mutex_unlock(&inst->history_lock);
if (read_cnt)
return read_cnt;
return ret;
}
read_max -= ret;
read_cnt += ret;
*ppos += ret;
inst->hist_count += ret;
if (read_cnt == count)
break;
offs += log->len;
}
mutex_unlock(&inst->history_lock);
return read_cnt;
}
static int fpga_cfg_history_open(struct inode *inode, struct file *file)
{
struct fpga_cfg_fpga_inst *inst;
if (inode->i_private) {
file->private_data = inode->i_private;
inst = inode->i_private;
inst->hist_count = 0;
return 0;
}
return -ENODEV;
}
static ssize_t fpga_cfg_history_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
struct fpga_cfg_fpga_inst *inst = file->private_data;
int ret, val;
ret = sscanf(buf, "%d", &val);
if (ret != 1)
return -EINVAL;
if (val == 0) {
fpga_cfg_free_log(inst);
fpga_cfg_history_header(inst);
fpga_cfg_update_hist_attr(inst);
}
return count;
}
static const struct file_operations dbgfs_history_ops = {
.open = fpga_cfg_history_open,
.read = fpga_cfg_history_read,
.write = fpga_cfg_history_write,
.llseek = default_llseek,
};
static ssize_t fpga_cfg_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct fpga_cfg_attribute *fpga_cfg_attr;
struct fpga_cfg_fpga_inst *inst;
inst = container_of(kobj, struct fpga_cfg_fpga_inst, kobj_fpga_dir);
fpga_cfg_attr = container_of(attr, struct fpga_cfg_attribute, attr);
if (!fpga_cfg_attr->show)
return -ENOENT;
return fpga_cfg_attr->show(inst, attr, buf);
}
static ssize_t fpga_cfg_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t size)
{
struct fpga_cfg_attribute *fpga_cfg_attr;
struct fpga_cfg_fpga_inst *inst;
inst = container_of(kobj, struct fpga_cfg_fpga_inst, kobj_fpga_dir);
fpga_cfg_attr = container_of(attr, struct fpga_cfg_attribute, attr);
if (!fpga_cfg_attr->store)
return -ENOENT;
return fpga_cfg_attr->store(inst, attr, buf, size);
}
static const struct sysfs_ops fpga_cfg_sysfs_ops = {
.show = fpga_cfg_attr_show,
.store = fpga_cfg_attr_store,
};
#if 0
static ssize_t show_history(struct fpga_cfg_fpga_inst *inst,
struct attribute *attr, char *buf)
{
struct fpga_cfg_log_entry *log, *tmp;
int len = 0;
list_for_each_entry_safe(log, tmp, &inst->history_list, list) {
/*len += sprintf(buf + len, "%s", log->entry);*/
pr_debug("%d: %s", len++, log->entry);
}
return len;
}
#endif
static ssize_t show_debug(struct fpga_cfg_fpga_inst *inst,
struct attribute *attr, char *buf)
{
return snprintf(buf, 3, "%d\n", inst->debug);
}
static ssize_t store_debug(struct fpga_cfg_fpga_inst *inst,
struct attribute *attr, const char *buf, size_t size)
{
sscanf(buf, "%d\n", &inst->debug);
return size;
}
static ssize_t show_load(struct fpga_cfg_fpga_inst *inst,
struct attribute *attr, char *buf)
{
return snprintf(buf, 3, "%d\n", inst->cfg_done);
}
static ssize_t show_ready(struct fpga_cfg_fpga_inst *inst,
struct attribute *attr, char *buf)
{
return snprintf(buf, 3, "%d\n", inst->cfg_done);
}
static ssize_t show_status(struct fpga_cfg_fpga_inst *inst,
struct attribute *attr, char *buf)
{
return snprintf(buf, 3, "%d\n", inst->cfg_done);
}
/*
* Helper functions and structures for parsing the config description
*/
#define KEY_SZ 64
#define VAL_SZ (PATH_MAX + NAME_MAX + 3)
static DEFINE_MUTEX(parser_lock);
static char key_buf[KEY_SZ];
static char val_buf[VAL_SZ];
struct key_type_tbl {
enum fpga_cfg_mgr_type type;
const char *key;
bool done;
};
struct key_type_tbl fpga_cfg_key_tbl[] = {
{ CFG_USB_ID, "fpp-usb-dev-id" },
{ CFG_BUS_NR, "fpga-pcie-bus-nr" },
{ CFG_TYPE, "fpga-type" },
{ CFG_BS_LSB, "spi-lsb-first" },
{ FPP_RING_MGR, "fpp-image" },
{ SPI_RING_MGR, "spi-image" },
{ CVP_MGR, "cvp-image" },
{ PR_MGR, "part-reconf-image" },
{ FPP_META, "fpp-image-meta" },
{ SPI_META, "spi-image-meta" },
{ CVP_META, "cvp-image-meta" },
{ PR_META, "part-reconf-image-meta" },
{ FPGA_DRV, "mfd-driver" },
{ FPGA_DRV_ARGS, "mfd-driver-param" },
};
static void reset_key_tbl_search(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(fpga_cfg_key_tbl); i++)
fpga_cfg_key_tbl[i].done = 0;
}
static int parse_line(const char *line, char *key, char *val)
{
char fmt_key_val_pfx[] = "%23s = \"%";
char line_fmt_str[24] = "";
int ret;
/* Allow reading of key_values with spaces included */
snprintf(line_fmt_str, sizeof(line_fmt_str), "%s%d[^\n]",
fmt_key_val_pfx, VAL_SZ);
ret = sscanf(line, line_fmt_str, key, val);
/*pr_debug("fmt: '%s' , ret %d\n", line_fmt_str, ret);*/
if (ret > 0)
return ret;
pr_err("Invalid line format.\n");
return -EINVAL;
}
static int chk_and_terminate_val(char *val)
{
char *p;
/* check for trailing '";' and terminate the value string */
p = strrchr(val, '"');
if (!p)
return 0;
if (p[1] != ';')
return 0;
p[0] = 0;
return 1;
}
static int assign_values(struct fpga_cfg_fpga_inst *inst,
char *key, char *val)
{
struct device *dev = &inst->cfg->pdev->dev;
char *dst, *dst_sub;
int i, ret;
size_t len;
for (i = 0; i < ARRAY_SIZE(fpga_cfg_key_tbl); i++) {
/* do not search for already processed key */
if (fpga_cfg_key_tbl[i].done)
continue;
if (strcmp(fpga_cfg_key_tbl[i].key, key))
continue;
fpga_cfg_key_tbl[i].done = 1;
if (!chk_and_terminate_val(val)) {
dev_err(dev, "Invalid line end: '%s'\n", val);
return -EINVAL;
}
dst_sub = NULL;
switch (fpga_cfg_key_tbl[i].type) {
case FPP_RING_MGR:
dst_sub = inst->fpp.firmware;
dst = inst->fpp.firmware_abs;
len = sizeof(inst->fpp.firmware_abs);
inst->cfg_op1 = FPP_RING_MGR;
break;
case SPI_RING_MGR:
dst_sub = inst->spi.firmware;
dst = inst->spi.firmware_abs;
len = sizeof(inst->spi.firmware_abs);
inst->cfg_op1 = inst->mgr_type;
break;
case CVP_MGR:
dst_sub = inst->cvp.firmware;
dst = inst->cvp.firmware_abs;
len = sizeof(inst->cvp.firmware_abs);
inst->cfg_op2 = CVP_MGR;
break;
case PR_MGR:
dst_sub = inst->pr.firmware;
dst = inst->pr.firmware_abs;
len = sizeof(inst->pr.firmware_abs);
inst->cfg_op1 = PR_MGR;
break;
case FPP_META:
dst = inst->fpp.metadata_abs;
len = sizeof(inst->fpp.metadata_abs);
break;
case SPI_META:
dst = inst->spi.metadata_abs;
len = sizeof(inst->spi.metadata_abs);
break;
case CVP_META:
dst = inst->cvp.metadata_abs;
len = sizeof(inst->cvp.metadata_abs);
break;
case PR_META:
dst = inst->pr.metadata_abs;
len = sizeof(inst->pr.metadata_abs);
break;
case CFG_BUS_NR:
if (sscanf(val, "%x:%x.%x",
&inst->bus, &inst->dev, &inst->func) != 3) {
dev_err(dev, "Invalid bus-nr: '%s'\n", val);
return -EINVAL;
}
sscanf(val, "%s", inst->bdf);
if (inst->debug)
dev_dbg(dev, "BDF '%s'\n", inst->bdf);
return 0;
case CFG_USB_ID:
if (strncmp(val, inst->usb_dev_id,
sizeof(inst->usb_dev_id))) {
dev_warn(dev, "FPP usb id '%s', expected '%s'\n",
val, inst->usb_dev_id);
}
if (inst->debug)
dev_dbg(dev, "Using FPP dev '%s'\n", val);
return 0;
case CFG_TYPE:
if (sscanf(val, "%s", inst->type) != 1) {
dev_err(dev, "Invalid type '%s'\n", val);
return -EINVAL;
}
if (inst->debug)
dev_dbg(dev, "TYPE '%s'\n", inst->type);
return 0;
case CFG_BS_LSB:
if (sscanf(val, "%d", &inst->bs_lsb_first) != 1) {
dev_err(dev, "Invalid bitorder flag '%s'\n", val);
return -EINVAL;
}
if (inst->debug) {
dev_dbg(dev, "Bitstream LSB first flag '%d'\n",
inst->bs_lsb_first);
}
return 0;
case FPGA_DRV:
if (sscanf(val, "%s", inst->fpga_drv) != 1) {
dev_err(dev, "Invalid 'mfd-driver': '%s'\n", val);
return -EINVAL;
}
if (inst->debug)
dev_dbg(dev, "Using mfd-driver: '%s'\n", inst->fpga_drv);
return 0;
case FPGA_DRV_ARGS:
strncpy(inst->fpga_drv_args, val,
sizeof(inst->fpga_drv_args));
if (inst->debug)
dev_dbg(dev, "Using mfd-driver-param: '%s'\n",
inst->fpga_drv_args);
return 0;
default:
return 0;
}
strncpy(dst, val, len);
if (inst->debug)
dev_dbg(dev, "abs. name '%s'\n", dst);
if (dst_sub) {
ret = sscanf(val, "/lib/firmware/%s", dst_sub);
if (ret == 1) {
if (inst->debug)
dev_dbg(dev, "base name '%s'\n",
dst_sub);
return 0;
}
dev_err(dev,
"/lib/firmware/ prefix expected, found: %s\n",
val);
return -EINVAL;
}
}
return 0;
}
static int fpga_cfg_desc_parse(struct fpga_cfg_fpga_inst *inst,
const char *buf, size_t size)
{
struct fpga_cfg *cfg = inst->cfg;
struct device *dev = &cfg->pdev->dev;
const char *p, *line, *end;
int ret;
/* process data buffer until trailing "\n}\n" */
p = buf;
end = buf + size - 3;
mutex_lock(&parser_lock);
reset_key_tbl_search();
while (p < end) {
if (*p == '\n') {
line = p + 1;
ret = parse_line(line, key_buf, val_buf);
if (ret <= 1) {
dev_err(dev, "parse error: '%s'\n", line);
mutex_unlock(&parser_lock);
return -EINVAL;
}
ret = assign_values(inst, key_buf, val_buf);
if (ret < 0) {
mutex_unlock(&parser_lock);
return ret;
}
}
p++;
}
mutex_unlock(&parser_lock);
return 0;
}
static int fpga_cfg_op_log(struct fpga_cfg_fpga_inst *inst,
struct cfg_desc *desc)
{
struct fpga_cfg_log_entry *log, *hdr, *first;
unsigned long rem_nsec;
int len, ret = 0;
desc->cfg_ts_nsec = local_clock();
rem_nsec = do_div(desc->cfg_ts_nsec, 1000000000);
len = snprintf(desc->log_tmp, sizeof(desc->log_tmp),
"[%5lu.%06lu] load %zi: %s\tmeta: %s\n",
(unsigned long)desc->cfg_ts_nsec,
rem_nsec / 1000, inst->cfg_seq_num,
desc->firmware_abs, desc->metadata_abs);
log = kmalloc(sizeof(*log) + len + 1, GFP_KERNEL);
if (log) {
strncpy(log->entry, desc->log_tmp, len + 1);
log->len = len;
mutex_lock(&inst->history_lock);
list_add_tail(&log->list, &inst->history_list);
inst->history_entries++;
mutex_unlock(&inst->history_lock);
} else {
dev_warn(&inst->cfg->pdev->dev, "No memory for log entry.\n");
return -ENOMEM;
}
if (inst->history_entries > inst->history_max_entries) {
mutex_lock(&inst->history_lock);
hdr = list_first_entry(&inst->history_list,
struct fpga_cfg_log_entry,
list);
if (hdr) {
first = list_next_entry(hdr, list);
inst->hist_count_new -= first->len;
inst->history_entries--;
pr_debug("hist. len %zi, delete entry '%s'\n",
inst->history_entries, first->entry);
list_del(&first->list);
kfree(first);
}
mutex_unlock(&inst->history_lock);
}