1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 CERN (www.cern.ch)
4 * Author: Federico Vaga <federico.vaga@cern.ch>
7 #include <linux/module.h>
8 #include <linux/device.h>
9 #include <linux/init.h>
11 #include <linux/debugfs.h>
12 #include <linux/seq_file.h>
13 #include <asm/byteorder.h>
15 #include <linux/fmc.h>
16 #include <linux/sdb.h>
17 #include <linux/fmc-sdb.h>
19 #define FMC_DBG_SDB_DUMP "dump_sdb"
21 static char *__strip_trailing_space(char *buf
, char *str
, int len
)
25 memcpy(buf
, str
, len
);
27 while (i
>= 0 && buf
[i
] == ' ')
32 #define __sdb_string(buf, field) ({ \
33 BUILD_BUG_ON(sizeof(buf) < sizeof(field)); \
34 __strip_trailing_space(buf, (void *)(field), sizeof(field)); \
38 * We do not check seq_printf() errors because we want to see things in any case
40 static void fmc_sdb_dump_recursive(struct fmc_device
*fmc
, struct seq_file
*s
,
41 const struct sdb_array
*arr
)
43 unsigned long base
= arr
->baseaddr
;
44 int i
, j
, n
= arr
->len
, level
= arr
->level
;
47 for (i
= 0; i
< n
; i
++) {
49 struct sdb_product
*p
;
50 struct sdb_component
*c
;
53 c
= &r
->dev
.sdb_component
;
56 for (j
= 0; j
< level
; j
++)
58 switch (r
->empty
.record_type
) {
59 case sdb_type_interconnect
:
60 seq_printf(s
, "%08llx:%08x %.19s\n",
61 __be64_to_cpu(p
->vendor_id
),
62 __be32_to_cpu(p
->device_id
),
66 seq_printf(s
, "%08llx:%08x %.19s (%08llx-%08llx)\n",
67 __be64_to_cpu(p
->vendor_id
),
68 __be32_to_cpu(p
->device_id
),
70 __be64_to_cpu(c
->addr_first
) + base
,
71 __be64_to_cpu(c
->addr_last
) + base
);
74 seq_printf(s
, "%08llx:%08x %.19s (bridge: %08llx)\n",
75 __be64_to_cpu(p
->vendor_id
),
76 __be32_to_cpu(p
->device_id
),
78 __be64_to_cpu(c
->addr_first
) + base
);
79 if (IS_ERR(arr
->subtree
[i
])) {
80 seq_printf(s
, "SDB: (bridge error %li)\n",
81 PTR_ERR(arr
->subtree
[i
]));
84 fmc_sdb_dump_recursive(fmc
, s
, arr
->subtree
[i
]);
86 case sdb_type_integration
:
87 seq_printf(s
, "integration\n");
89 case sdb_type_repo_url
:
90 seq_printf(s
, "Synthesis repository: %s\n",
91 __sdb_string(tmp
, r
->repo_url
.repo_url
));
93 case sdb_type_synthesis
:
94 seq_printf(s
, "Bitstream '%s' ",
95 __sdb_string(tmp
, r
->synthesis
.syn_name
));
96 seq_printf(s
, "synthesized %08x by %s ",
97 __be32_to_cpu(r
->synthesis
.date
),
98 __sdb_string(tmp
, r
->synthesis
.user_name
));
99 seq_printf(s
, "(%s version %x), ",
100 __sdb_string(tmp
, r
->synthesis
.tool_name
),
101 __be32_to_cpu(r
->synthesis
.tool_version
));
102 seq_printf(s
, "commit %pm\n",
103 r
->synthesis
.commit_id
);
106 seq_printf(s
, "empty\n");
109 seq_printf(s
, "UNKNOWN TYPE 0x%02x\n",
110 r
->empty
.record_type
);
116 static int fmc_sdb_dump(struct seq_file
*s
, void *offset
)
118 struct fmc_device
*fmc
= s
->private;
121 seq_printf(s
, "no SDB information\n");
125 seq_printf(s
, "FMC: %s (%s), slot %i, device %s\n", dev_name(fmc
->hwdev
),
126 fmc
->carrier_name
, fmc
->slot_id
, dev_name(&fmc
->dev
));
127 /* Dump SDB information */
128 fmc_sdb_dump_recursive(fmc
, s
, fmc
->sdb
);
134 static int fmc_sdb_dump_open(struct inode
*inode
, struct file
*file
)
136 struct fmc_device
*fmc
= inode
->i_private
;
138 return single_open(file
, fmc_sdb_dump
, fmc
);
142 const struct file_operations fmc_dbgfs_sdb_dump
= {
143 .owner
= THIS_MODULE
,
144 .open
= fmc_sdb_dump_open
,
147 .release
= single_release
,
150 int fmc_debug_init(struct fmc_device
*fmc
)
152 fmc
->dbg_dir
= debugfs_create_dir(dev_name(&fmc
->dev
), NULL
);
153 if (IS_ERR_OR_NULL(fmc
->dbg_dir
)) {
154 pr_err("FMC: Cannot create debugfs\n");
155 return PTR_ERR(fmc
->dbg_dir
);
158 fmc
->dbg_sdb_dump
= debugfs_create_file(FMC_DBG_SDB_DUMP
, 0444,
160 &fmc_dbgfs_sdb_dump
);
161 if (IS_ERR_OR_NULL(fmc
->dbg_sdb_dump
))
162 pr_err("FMC: Cannot create debugfs file %s\n",
168 void fmc_debug_exit(struct fmc_device
*fmc
)
171 debugfs_remove_recursive(fmc
->dbg_dir
);