xref: /linux/drivers/net/ipa/ipa_sysfs.c (revision cbdb1f163af2bb90d01be1f0263df1d8d5c9d9d3)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (C) 2021-2022 Linaro Ltd. */
4 
5 #include <linux/kernel.h>
6 #include <linux/types.h>
7 #include <linux/device.h>
8 #include <linux/sysfs.h>
9 
10 #include "ipa.h"
11 #include "ipa_version.h"
12 #include "ipa_sysfs.h"
13 
14 static const char *ipa_version_string(struct ipa *ipa)
15 {
16 	switch (ipa->version) {
17 	case IPA_VERSION_3_0:
18 		return "3.0";
19 	case IPA_VERSION_3_1:
20 		return "3.1";
21 	case IPA_VERSION_3_5:
22 		return "3.5";
23 	case IPA_VERSION_3_5_1:
24 		return "3.5.1";
25 	case IPA_VERSION_4_0:
26 		return "4.0";
27 	case IPA_VERSION_4_1:
28 		return "4.1";
29 	case IPA_VERSION_4_2:
30 		return "4.2";
31 	case IPA_VERSION_4_5:
32 		return "4.5";
33 	case IPA_VERSION_4_7:
34 		return "4.7";
35 	case IPA_VERSION_4_9:
36 		return "4.9";
37 	case IPA_VERSION_4_11:
38 		return "4.11";
39 	default:
40 		return "0.0";	/* Won't happen (checked at probe time) */
41 	}
42 }
43 
44 static ssize_t
45 version_show(struct device *dev, struct device_attribute *attr, char *buf)
46 {
47 	struct ipa *ipa = dev_get_drvdata(dev);
48 
49 	return sysfs_emit(buf, "%s\n", ipa_version_string(ipa));
50 }
51 
52 static DEVICE_ATTR_RO(version);
53 
54 static struct attribute *ipa_attrs[] = {
55 	&dev_attr_version.attr,
56 	NULL
57 };
58 
59 const struct attribute_group ipa_attribute_group = {
60 	.attrs		= ipa_attrs,
61 };
62 
63 static const char *ipa_offload_string(struct ipa *ipa)
64 {
65 	return ipa->version < IPA_VERSION_4_5 ? "MAPv4" : "MAPv5";
66 }
67 
68 static ssize_t rx_offload_show(struct device *dev,
69 			       struct device_attribute *attr, char *buf)
70 {
71 	struct ipa *ipa = dev_get_drvdata(dev);
72 
73 	return sysfs_emit(buf, "%s\n", ipa_offload_string(ipa));
74 }
75 
76 static DEVICE_ATTR_RO(rx_offload);
77 
78 static ssize_t tx_offload_show(struct device *dev,
79 			       struct device_attribute *attr, char *buf)
80 {
81 	struct ipa *ipa = dev_get_drvdata(dev);
82 
83 	return sysfs_emit(buf, "%s\n", ipa_offload_string(ipa));
84 }
85 
86 static DEVICE_ATTR_RO(tx_offload);
87 
88 static struct attribute *ipa_feature_attrs[] = {
89 	&dev_attr_rx_offload.attr,
90 	&dev_attr_tx_offload.attr,
91 	NULL
92 };
93 
94 const struct attribute_group ipa_feature_attribute_group = {
95 	.name		= "feature",
96 	.attrs		= ipa_feature_attrs,
97 };
98 
99 static umode_t ipa_endpoint_id_is_visible(struct kobject *kobj,
100 					  struct attribute *attr, int n)
101 {
102 	struct ipa *ipa = dev_get_drvdata(kobj_to_dev(kobj));
103 	struct device_attribute *dev_attr;
104 	struct dev_ext_attribute *ea;
105 	bool visible;
106 
107 	/* An endpoint id attribute is only visible if it's defined */
108 	dev_attr = container_of(attr, struct device_attribute, attr);
109 	ea = container_of(dev_attr, struct dev_ext_attribute, attr);
110 
111 	visible = !!ipa->name_map[(enum ipa_endpoint_name)(uintptr_t)ea->var];
112 
113 	return visible ? attr->mode : 0;
114 }
115 
116 static ssize_t endpoint_id_attr_show(struct device *dev,
117 				     struct device_attribute *attr, char *buf)
118 {
119 	struct ipa *ipa = dev_get_drvdata(dev);
120 	struct ipa_endpoint *endpoint;
121 	struct dev_ext_attribute *ea;
122 
123 	ea = container_of(attr, struct dev_ext_attribute, attr);
124 	endpoint = ipa->name_map[(enum ipa_endpoint_name)(uintptr_t)ea->var];
125 
126 	return sysfs_emit(buf, "%u\n", endpoint->endpoint_id);
127 }
128 
129 #define ENDPOINT_ID_ATTR(_n, _endpoint_name)				    \
130 	static struct dev_ext_attribute dev_attr_endpoint_id_ ## _n = {	    \
131 		.attr	= __ATTR(_n, 0444, endpoint_id_attr_show, NULL),    \
132 		.var	= (void *)(_endpoint_name),			    \
133 	}
134 
135 ENDPOINT_ID_ATTR(modem_rx, IPA_ENDPOINT_AP_MODEM_RX);
136 ENDPOINT_ID_ATTR(modem_tx, IPA_ENDPOINT_AP_MODEM_TX);
137 
138 static struct attribute *ipa_endpoint_id_attrs[] = {
139 	&dev_attr_endpoint_id_modem_rx.attr.attr,
140 	&dev_attr_endpoint_id_modem_tx.attr.attr,
141 	NULL
142 };
143 
144 const struct attribute_group ipa_endpoint_id_attribute_group = {
145 	.name		= "endpoint_id",
146 	.is_visible	= ipa_endpoint_id_is_visible,
147 	.attrs		= ipa_endpoint_id_attrs,
148 };
149 
150 /* Reuse endpoint ID attributes for the legacy modem endpoint IDs */
151 #define MODEM_ATTR(_n, _endpoint_name)					    \
152 	static struct dev_ext_attribute dev_attr_modem_ ## _n = {	    \
153 		.attr	= __ATTR(_n, 0444, endpoint_id_attr_show, NULL),    \
154 		.var	= (void *)(_endpoint_name),			    \
155 	}
156 
157 MODEM_ATTR(rx_endpoint_id, IPA_ENDPOINT_AP_MODEM_RX);
158 MODEM_ATTR(tx_endpoint_id, IPA_ENDPOINT_AP_MODEM_TX);
159 
160 static struct attribute *ipa_modem_attrs[] = {
161 	&dev_attr_modem_rx_endpoint_id.attr.attr,
162 	&dev_attr_modem_tx_endpoint_id.attr.attr,
163 	NULL,
164 };
165 
166 const struct attribute_group ipa_modem_attribute_group = {
167 	.name		= "modem",
168 	.attrs		= ipa_modem_attrs,
169 };
170