162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/* Copyright(c) 2023 Advanced Micro Devices, Inc */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <linux/pci.h>
562306a36Sopenharmony_ci#include <linux/vdpa.h>
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/pds/pds_common.h>
862306a36Sopenharmony_ci#include <linux/pds/pds_core_if.h>
962306a36Sopenharmony_ci#include <linux/pds/pds_adminq.h>
1062306a36Sopenharmony_ci#include <linux/pds/pds_auxbus.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include "aux_drv.h"
1362306a36Sopenharmony_ci#include "vdpa_dev.h"
1462306a36Sopenharmony_ci#include "debugfs.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistatic struct dentry *dbfs_dir;
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_civoid pds_vdpa_debugfs_create(void)
1962306a36Sopenharmony_ci{
2062306a36Sopenharmony_ci	dbfs_dir = debugfs_create_dir(PDS_VDPA_DRV_NAME, NULL);
2162306a36Sopenharmony_ci}
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_civoid pds_vdpa_debugfs_destroy(void)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	debugfs_remove_recursive(dbfs_dir);
2662306a36Sopenharmony_ci	dbfs_dir = NULL;
2762306a36Sopenharmony_ci}
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define PRINT_SBIT_NAME(__seq, __f, __name)                     \
3062306a36Sopenharmony_ci	do {                                                    \
3162306a36Sopenharmony_ci		if ((__f) & (__name))                               \
3262306a36Sopenharmony_ci			seq_printf(__seq, " %s", &#__name[16]); \
3362306a36Sopenharmony_ci	} while (0)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistatic void print_status_bits(struct seq_file *seq, u8 status)
3662306a36Sopenharmony_ci{
3762306a36Sopenharmony_ci	seq_puts(seq, "status:");
3862306a36Sopenharmony_ci	PRINT_SBIT_NAME(seq, status, VIRTIO_CONFIG_S_ACKNOWLEDGE);
3962306a36Sopenharmony_ci	PRINT_SBIT_NAME(seq, status, VIRTIO_CONFIG_S_DRIVER);
4062306a36Sopenharmony_ci	PRINT_SBIT_NAME(seq, status, VIRTIO_CONFIG_S_DRIVER_OK);
4162306a36Sopenharmony_ci	PRINT_SBIT_NAME(seq, status, VIRTIO_CONFIG_S_FEATURES_OK);
4262306a36Sopenharmony_ci	PRINT_SBIT_NAME(seq, status, VIRTIO_CONFIG_S_NEEDS_RESET);
4362306a36Sopenharmony_ci	PRINT_SBIT_NAME(seq, status, VIRTIO_CONFIG_S_FAILED);
4462306a36Sopenharmony_ci	seq_puts(seq, "\n");
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistatic void print_feature_bits_all(struct seq_file *seq, u64 features)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	int i;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	seq_puts(seq, "features:");
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	for (i = 0; i < (sizeof(u64) * 8); i++) {
5462306a36Sopenharmony_ci		u64 mask = BIT_ULL(i);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci		switch (features & mask) {
5762306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_CSUM):
5862306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_CSUM");
5962306a36Sopenharmony_ci			break;
6062306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_GUEST_CSUM):
6162306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_GUEST_CSUM");
6262306a36Sopenharmony_ci			break;
6362306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS):
6462306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_CTRL_GUEST_OFFLOADS");
6562306a36Sopenharmony_ci			break;
6662306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_MTU):
6762306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_MTU");
6862306a36Sopenharmony_ci			break;
6962306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_MAC):
7062306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_MAC");
7162306a36Sopenharmony_ci			break;
7262306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_GUEST_TSO4):
7362306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_GUEST_TSO4");
7462306a36Sopenharmony_ci			break;
7562306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_GUEST_TSO6):
7662306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_GUEST_TSO6");
7762306a36Sopenharmony_ci			break;
7862306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_GUEST_ECN):
7962306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_GUEST_ECN");
8062306a36Sopenharmony_ci			break;
8162306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_GUEST_UFO):
8262306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_GUEST_UFO");
8362306a36Sopenharmony_ci			break;
8462306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_HOST_TSO4):
8562306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_HOST_TSO4");
8662306a36Sopenharmony_ci			break;
8762306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_HOST_TSO6):
8862306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_HOST_TSO6");
8962306a36Sopenharmony_ci			break;
9062306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_HOST_ECN):
9162306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_HOST_ECN");
9262306a36Sopenharmony_ci			break;
9362306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_HOST_UFO):
9462306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_HOST_UFO");
9562306a36Sopenharmony_ci			break;
9662306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_MRG_RXBUF):
9762306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_MRG_RXBUF");
9862306a36Sopenharmony_ci			break;
9962306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_STATUS):
10062306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_STATUS");
10162306a36Sopenharmony_ci			break;
10262306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_CTRL_VQ):
10362306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_CTRL_VQ");
10462306a36Sopenharmony_ci			break;
10562306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_CTRL_RX):
10662306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_CTRL_RX");
10762306a36Sopenharmony_ci			break;
10862306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_CTRL_VLAN):
10962306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_CTRL_VLAN");
11062306a36Sopenharmony_ci			break;
11162306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_CTRL_RX_EXTRA):
11262306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_CTRL_RX_EXTRA");
11362306a36Sopenharmony_ci			break;
11462306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_GUEST_ANNOUNCE):
11562306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_GUEST_ANNOUNCE");
11662306a36Sopenharmony_ci			break;
11762306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_MQ):
11862306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_MQ");
11962306a36Sopenharmony_ci			break;
12062306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR):
12162306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_CTRL_MAC_ADDR");
12262306a36Sopenharmony_ci			break;
12362306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_HASH_REPORT):
12462306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_HASH_REPORT");
12562306a36Sopenharmony_ci			break;
12662306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_RSS):
12762306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_RSS");
12862306a36Sopenharmony_ci			break;
12962306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_RSC_EXT):
13062306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_RSC_EXT");
13162306a36Sopenharmony_ci			break;
13262306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_STANDBY):
13362306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_STANDBY");
13462306a36Sopenharmony_ci			break;
13562306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX):
13662306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_NET_F_SPEED_DUPLEX");
13762306a36Sopenharmony_ci			break;
13862306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_F_NOTIFY_ON_EMPTY):
13962306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_F_NOTIFY_ON_EMPTY");
14062306a36Sopenharmony_ci			break;
14162306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_F_ANY_LAYOUT):
14262306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_F_ANY_LAYOUT");
14362306a36Sopenharmony_ci			break;
14462306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_F_VERSION_1):
14562306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_F_VERSION_1");
14662306a36Sopenharmony_ci			break;
14762306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_F_ACCESS_PLATFORM):
14862306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_F_ACCESS_PLATFORM");
14962306a36Sopenharmony_ci			break;
15062306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_F_RING_PACKED):
15162306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_F_RING_PACKED");
15262306a36Sopenharmony_ci			break;
15362306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_F_ORDER_PLATFORM):
15462306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_F_ORDER_PLATFORM");
15562306a36Sopenharmony_ci			break;
15662306a36Sopenharmony_ci		case BIT_ULL(VIRTIO_F_SR_IOV):
15762306a36Sopenharmony_ci			seq_puts(seq, " VIRTIO_F_SR_IOV");
15862306a36Sopenharmony_ci			break;
15962306a36Sopenharmony_ci		case 0:
16062306a36Sopenharmony_ci			break;
16162306a36Sopenharmony_ci		default:
16262306a36Sopenharmony_ci			seq_printf(seq, " bit_%d", i);
16362306a36Sopenharmony_ci			break;
16462306a36Sopenharmony_ci		}
16562306a36Sopenharmony_ci	}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	seq_puts(seq, "\n");
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_civoid pds_vdpa_debugfs_add_pcidev(struct pds_vdpa_aux *vdpa_aux)
17162306a36Sopenharmony_ci{
17262306a36Sopenharmony_ci	vdpa_aux->dentry = debugfs_create_dir(pci_name(vdpa_aux->padev->vf_pdev), dbfs_dir);
17362306a36Sopenharmony_ci}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistatic int identity_show(struct seq_file *seq, void *v)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	struct pds_vdpa_aux *vdpa_aux = seq->private;
17862306a36Sopenharmony_ci	struct vdpa_mgmt_dev *mgmt;
17962306a36Sopenharmony_ci	u64 hw_features;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	seq_printf(seq, "aux_dev:            %s\n",
18262306a36Sopenharmony_ci		   dev_name(&vdpa_aux->padev->aux_dev.dev));
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	mgmt = &vdpa_aux->vdpa_mdev;
18562306a36Sopenharmony_ci	seq_printf(seq, "max_vqs:            %d\n", mgmt->max_supported_vqs);
18662306a36Sopenharmony_ci	seq_printf(seq, "config_attr_mask:   %#llx\n", mgmt->config_attr_mask);
18762306a36Sopenharmony_ci	hw_features = le64_to_cpu(vdpa_aux->ident.hw_features);
18862306a36Sopenharmony_ci	seq_printf(seq, "hw_features:        %#llx\n", hw_features);
18962306a36Sopenharmony_ci	print_feature_bits_all(seq, hw_features);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	return 0;
19262306a36Sopenharmony_ci}
19362306a36Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(identity);
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_civoid pds_vdpa_debugfs_add_ident(struct pds_vdpa_aux *vdpa_aux)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	debugfs_create_file("identity", 0400, vdpa_aux->dentry,
19862306a36Sopenharmony_ci			    vdpa_aux, &identity_fops);
19962306a36Sopenharmony_ci}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistatic int config_show(struct seq_file *seq, void *v)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	struct pds_vdpa_device *pdsv = seq->private;
20462306a36Sopenharmony_ci	struct virtio_net_config vc;
20562306a36Sopenharmony_ci	u8 status;
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	memcpy_fromio(&vc, pdsv->vdpa_aux->vd_mdev.device,
20862306a36Sopenharmony_ci		      sizeof(struct virtio_net_config));
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	seq_printf(seq, "mac:                  %pM\n", vc.mac);
21162306a36Sopenharmony_ci	seq_printf(seq, "max_virtqueue_pairs:  %d\n",
21262306a36Sopenharmony_ci		   __virtio16_to_cpu(true, vc.max_virtqueue_pairs));
21362306a36Sopenharmony_ci	seq_printf(seq, "mtu:                  %d\n", __virtio16_to_cpu(true, vc.mtu));
21462306a36Sopenharmony_ci	seq_printf(seq, "speed:                %d\n", le32_to_cpu(vc.speed));
21562306a36Sopenharmony_ci	seq_printf(seq, "duplex:               %d\n", vc.duplex);
21662306a36Sopenharmony_ci	seq_printf(seq, "rss_max_key_size:     %d\n", vc.rss_max_key_size);
21762306a36Sopenharmony_ci	seq_printf(seq, "rss_max_indirection_table_length: %d\n",
21862306a36Sopenharmony_ci		   le16_to_cpu(vc.rss_max_indirection_table_length));
21962306a36Sopenharmony_ci	seq_printf(seq, "supported_hash_types: %#x\n",
22062306a36Sopenharmony_ci		   le32_to_cpu(vc.supported_hash_types));
22162306a36Sopenharmony_ci	seq_printf(seq, "vn_status:            %#x\n",
22262306a36Sopenharmony_ci		   __virtio16_to_cpu(true, vc.status));
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	status = vp_modern_get_status(&pdsv->vdpa_aux->vd_mdev);
22562306a36Sopenharmony_ci	seq_printf(seq, "dev_status:           %#x\n", status);
22662306a36Sopenharmony_ci	print_status_bits(seq, status);
22762306a36Sopenharmony_ci	seq_printf(seq, "negotiated_features:  %#llx\n", pdsv->negotiated_features);
22862306a36Sopenharmony_ci	print_feature_bits_all(seq, pdsv->negotiated_features);
22962306a36Sopenharmony_ci	seq_printf(seq, "vdpa_index:           %d\n", pdsv->vdpa_index);
23062306a36Sopenharmony_ci	seq_printf(seq, "num_vqs:              %d\n", pdsv->num_vqs);
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	return 0;
23362306a36Sopenharmony_ci}
23462306a36Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(config);
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistatic int vq_show(struct seq_file *seq, void *v)
23762306a36Sopenharmony_ci{
23862306a36Sopenharmony_ci	struct pds_vdpa_vq_info *vq = seq->private;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	seq_printf(seq, "ready:      %d\n", vq->ready);
24162306a36Sopenharmony_ci	seq_printf(seq, "desc_addr:  %#llx\n", vq->desc_addr);
24262306a36Sopenharmony_ci	seq_printf(seq, "avail_addr: %#llx\n", vq->avail_addr);
24362306a36Sopenharmony_ci	seq_printf(seq, "used_addr:  %#llx\n", vq->used_addr);
24462306a36Sopenharmony_ci	seq_printf(seq, "q_len:      %d\n", vq->q_len);
24562306a36Sopenharmony_ci	seq_printf(seq, "qid:        %d\n", vq->qid);
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	seq_printf(seq, "doorbell:   %#llx\n", vq->doorbell);
24862306a36Sopenharmony_ci	seq_printf(seq, "avail_idx:  %d\n", vq->avail_idx);
24962306a36Sopenharmony_ci	seq_printf(seq, "used_idx:   %d\n", vq->used_idx);
25062306a36Sopenharmony_ci	seq_printf(seq, "irq:        %d\n", vq->irq);
25162306a36Sopenharmony_ci	seq_printf(seq, "irq-name:   %s\n", vq->irq_name);
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	return 0;
25462306a36Sopenharmony_ci}
25562306a36Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(vq);
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_civoid pds_vdpa_debugfs_add_vdpadev(struct pds_vdpa_aux *vdpa_aux)
25862306a36Sopenharmony_ci{
25962306a36Sopenharmony_ci	int i;
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	debugfs_create_file("config", 0400, vdpa_aux->dentry, vdpa_aux->pdsv, &config_fops);
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	for (i = 0; i < vdpa_aux->pdsv->num_vqs; i++) {
26462306a36Sopenharmony_ci		char name[16];
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci		snprintf(name, sizeof(name), "vq%02d", i);
26762306a36Sopenharmony_ci		debugfs_create_file(name, 0400, vdpa_aux->dentry,
26862306a36Sopenharmony_ci				    &vdpa_aux->pdsv->vqs[i], &vq_fops);
26962306a36Sopenharmony_ci	}
27062306a36Sopenharmony_ci}
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_civoid pds_vdpa_debugfs_del_vdpadev(struct pds_vdpa_aux *vdpa_aux)
27362306a36Sopenharmony_ci{
27462306a36Sopenharmony_ci	debugfs_remove_recursive(vdpa_aux->dentry);
27562306a36Sopenharmony_ci	vdpa_aux->dentry = NULL;
27662306a36Sopenharmony_ci}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_civoid pds_vdpa_debugfs_reset_vdpadev(struct pds_vdpa_aux *vdpa_aux)
27962306a36Sopenharmony_ci{
28062306a36Sopenharmony_ci	/* we don't keep track of the entries, so remove it all
28162306a36Sopenharmony_ci	 * then rebuild the basics
28262306a36Sopenharmony_ci	 */
28362306a36Sopenharmony_ci	pds_vdpa_debugfs_del_vdpadev(vdpa_aux);
28462306a36Sopenharmony_ci	pds_vdpa_debugfs_add_pcidev(vdpa_aux);
28562306a36Sopenharmony_ci	pds_vdpa_debugfs_add_ident(vdpa_aux);
28662306a36Sopenharmony_ci}
287