btrfs: replace remaining do_div calls with div_u64 variants
[linux/fpc-iii.git] / drivers / misc / cxl / sysfs.c
blob461bdbd5d48317fa941ac002aa1f014761336f9a
1 /*
2 * Copyright 2014 IBM Corp.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/sysfs.h>
14 #include "cxl.h"
16 #define to_afu_chardev_m(d) dev_get_drvdata(d)
18 /********* Adapter attributes **********************************************/
20 static ssize_t caia_version_show(struct device *device,
21 struct device_attribute *attr,
22 char *buf)
24 struct cxl *adapter = to_cxl_adapter(device);
26 return scnprintf(buf, PAGE_SIZE, "%i.%i\n", adapter->caia_major,
27 adapter->caia_minor);
30 static ssize_t psl_revision_show(struct device *device,
31 struct device_attribute *attr,
32 char *buf)
34 struct cxl *adapter = to_cxl_adapter(device);
36 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_rev);
39 static ssize_t base_image_show(struct device *device,
40 struct device_attribute *attr,
41 char *buf)
43 struct cxl *adapter = to_cxl_adapter(device);
45 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->base_image);
48 static ssize_t image_loaded_show(struct device *device,
49 struct device_attribute *attr,
50 char *buf)
52 struct cxl *adapter = to_cxl_adapter(device);
54 if (adapter->user_image_loaded)
55 return scnprintf(buf, PAGE_SIZE, "user\n");
56 return scnprintf(buf, PAGE_SIZE, "factory\n");
59 static struct device_attribute adapter_attrs[] = {
60 __ATTR_RO(caia_version),
61 __ATTR_RO(psl_revision),
62 __ATTR_RO(base_image),
63 __ATTR_RO(image_loaded),
67 /********* AFU master specific attributes **********************************/
69 static ssize_t mmio_size_show_master(struct device *device,
70 struct device_attribute *attr,
71 char *buf)
73 struct cxl_afu *afu = to_afu_chardev_m(device);
75 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
78 static ssize_t pp_mmio_off_show(struct device *device,
79 struct device_attribute *attr,
80 char *buf)
82 struct cxl_afu *afu = to_afu_chardev_m(device);
84 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_offset);
87 static ssize_t pp_mmio_len_show(struct device *device,
88 struct device_attribute *attr,
89 char *buf)
91 struct cxl_afu *afu = to_afu_chardev_m(device);
93 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
96 static struct device_attribute afu_master_attrs[] = {
97 __ATTR(mmio_size, S_IRUGO, mmio_size_show_master, NULL),
98 __ATTR_RO(pp_mmio_off),
99 __ATTR_RO(pp_mmio_len),
103 /********* AFU attributes **************************************************/
105 static ssize_t mmio_size_show(struct device *device,
106 struct device_attribute *attr,
107 char *buf)
109 struct cxl_afu *afu = to_cxl_afu(device);
111 if (afu->pp_size)
112 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
113 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
116 static ssize_t reset_store_afu(struct device *device,
117 struct device_attribute *attr,
118 const char *buf, size_t count)
120 struct cxl_afu *afu = to_cxl_afu(device);
121 int rc;
123 /* Not safe to reset if it is currently in use */
124 mutex_lock(&afu->contexts_lock);
125 if (!idr_is_empty(&afu->contexts_idr)) {
126 rc = -EBUSY;
127 goto err;
130 if ((rc = cxl_afu_reset(afu)))
131 goto err;
133 rc = count;
134 err:
135 mutex_unlock(&afu->contexts_lock);
136 return rc;
139 static ssize_t irqs_min_show(struct device *device,
140 struct device_attribute *attr,
141 char *buf)
143 struct cxl_afu *afu = to_cxl_afu(device);
145 return scnprintf(buf, PAGE_SIZE, "%i\n", afu->pp_irqs);
148 static ssize_t irqs_max_show(struct device *device,
149 struct device_attribute *attr,
150 char *buf)
152 struct cxl_afu *afu = to_cxl_afu(device);
154 return scnprintf(buf, PAGE_SIZE, "%i\n", afu->irqs_max);
157 static ssize_t irqs_max_store(struct device *device,
158 struct device_attribute *attr,
159 const char *buf, size_t count)
161 struct cxl_afu *afu = to_cxl_afu(device);
162 ssize_t ret;
163 int irqs_max;
165 ret = sscanf(buf, "%i", &irqs_max);
166 if (ret != 1)
167 return -EINVAL;
169 if (irqs_max < afu->pp_irqs)
170 return -EINVAL;
172 if (irqs_max > afu->adapter->user_irqs)
173 return -EINVAL;
175 afu->irqs_max = irqs_max;
176 return count;
179 static ssize_t modes_supported_show(struct device *device,
180 struct device_attribute *attr, char *buf)
182 struct cxl_afu *afu = to_cxl_afu(device);
183 char *p = buf, *end = buf + PAGE_SIZE;
185 if (afu->modes_supported & CXL_MODE_DEDICATED)
186 p += scnprintf(p, end - p, "dedicated_process\n");
187 if (afu->modes_supported & CXL_MODE_DIRECTED)
188 p += scnprintf(p, end - p, "afu_directed\n");
189 return (p - buf);
192 static ssize_t prefault_mode_show(struct device *device,
193 struct device_attribute *attr,
194 char *buf)
196 struct cxl_afu *afu = to_cxl_afu(device);
198 switch (afu->prefault_mode) {
199 case CXL_PREFAULT_WED:
200 return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
201 case CXL_PREFAULT_ALL:
202 return scnprintf(buf, PAGE_SIZE, "all\n");
203 default:
204 return scnprintf(buf, PAGE_SIZE, "none\n");
208 static ssize_t prefault_mode_store(struct device *device,
209 struct device_attribute *attr,
210 const char *buf, size_t count)
212 struct cxl_afu *afu = to_cxl_afu(device);
213 enum prefault_modes mode = -1;
215 if (!strncmp(buf, "work_element_descriptor", 23))
216 mode = CXL_PREFAULT_WED;
217 if (!strncmp(buf, "all", 3))
218 mode = CXL_PREFAULT_ALL;
219 if (!strncmp(buf, "none", 4))
220 mode = CXL_PREFAULT_NONE;
222 if (mode == -1)
223 return -EINVAL;
225 afu->prefault_mode = mode;
226 return count;
229 static ssize_t mode_show(struct device *device,
230 struct device_attribute *attr,
231 char *buf)
233 struct cxl_afu *afu = to_cxl_afu(device);
235 if (afu->current_mode == CXL_MODE_DEDICATED)
236 return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
237 if (afu->current_mode == CXL_MODE_DIRECTED)
238 return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
239 return scnprintf(buf, PAGE_SIZE, "none\n");
242 static ssize_t mode_store(struct device *device, struct device_attribute *attr,
243 const char *buf, size_t count)
245 struct cxl_afu *afu = to_cxl_afu(device);
246 int old_mode, mode = -1;
247 int rc = -EBUSY;
249 /* can't change this if we have a user */
250 mutex_lock(&afu->contexts_lock);
251 if (!idr_is_empty(&afu->contexts_idr))
252 goto err;
254 if (!strncmp(buf, "dedicated_process", 17))
255 mode = CXL_MODE_DEDICATED;
256 if (!strncmp(buf, "afu_directed", 12))
257 mode = CXL_MODE_DIRECTED;
258 if (!strncmp(buf, "none", 4))
259 mode = 0;
261 if (mode == -1) {
262 rc = -EINVAL;
263 goto err;
267 * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
268 * other contexts coming in before we are ready:
270 old_mode = afu->current_mode;
271 afu->current_mode = 0;
272 afu->num_procs = 0;
274 mutex_unlock(&afu->contexts_lock);
276 if ((rc = _cxl_afu_deactivate_mode(afu, old_mode)))
277 return rc;
278 if ((rc = cxl_afu_activate_mode(afu, mode)))
279 return rc;
281 return count;
282 err:
283 mutex_unlock(&afu->contexts_lock);
284 return rc;
287 static ssize_t api_version_show(struct device *device,
288 struct device_attribute *attr,
289 char *buf)
291 return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
294 static ssize_t api_version_compatible_show(struct device *device,
295 struct device_attribute *attr,
296 char *buf)
298 return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
301 static struct device_attribute afu_attrs[] = {
302 __ATTR_RO(mmio_size),
303 __ATTR_RO(irqs_min),
304 __ATTR_RW(irqs_max),
305 __ATTR_RO(modes_supported),
306 __ATTR_RW(mode),
307 __ATTR_RW(prefault_mode),
308 __ATTR_RO(api_version),
309 __ATTR_RO(api_version_compatible),
310 __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
315 int cxl_sysfs_adapter_add(struct cxl *adapter)
317 int i, rc;
319 for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
320 if ((rc = device_create_file(&adapter->dev, &adapter_attrs[i])))
321 goto err;
323 return 0;
324 err:
325 for (i--; i >= 0; i--)
326 device_remove_file(&adapter->dev, &adapter_attrs[i]);
327 return rc;
329 void cxl_sysfs_adapter_remove(struct cxl *adapter)
331 int i;
333 for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++)
334 device_remove_file(&adapter->dev, &adapter_attrs[i]);
337 int cxl_sysfs_afu_add(struct cxl_afu *afu)
339 int i, rc;
341 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
342 if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
343 goto err;
346 return 0;
348 err:
349 for (i--; i >= 0; i--)
350 device_remove_file(&afu->dev, &afu_attrs[i]);
351 return rc;
354 void cxl_sysfs_afu_remove(struct cxl_afu *afu)
356 int i;
358 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
359 device_remove_file(&afu->dev, &afu_attrs[i]);
362 int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
364 int i, rc;
366 for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
367 if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
368 goto err;
371 return 0;
373 err:
374 for (i--; i >= 0; i--)
375 device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
376 return rc;
379 void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
381 int i;
383 for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++)
384 device_remove_file(afu->chardev_m, &afu_master_attrs[i]);