adf1f6d849131eefaf6f7520651dd780800cd326
[deliverable/linux.git] / drivers / misc / cxl / sysfs.c
1 /*
2 * Copyright 2014 IBM Corp.
3 *
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 */
9
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/sysfs.h>
13
14 #include "cxl.h"
15
16 #define to_afu_chardev_m(d) dev_get_drvdata(d)
17
18 /********* Adapter attributes **********************************************/
19
20 static ssize_t caia_version_show(struct device *device,
21 struct device_attribute *attr,
22 char *buf)
23 {
24 struct cxl *adapter = to_cxl_adapter(device);
25
26 return scnprintf(buf, PAGE_SIZE, "%i.%i\n", adapter->caia_major,
27 adapter->caia_minor);
28 }
29
30 static ssize_t psl_revision_show(struct device *device,
31 struct device_attribute *attr,
32 char *buf)
33 {
34 struct cxl *adapter = to_cxl_adapter(device);
35
36 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_rev);
37 }
38
39 static ssize_t base_image_show(struct device *device,
40 struct device_attribute *attr,
41 char *buf)
42 {
43 struct cxl *adapter = to_cxl_adapter(device);
44
45 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->base_image);
46 }
47
48 static ssize_t image_loaded_show(struct device *device,
49 struct device_attribute *attr,
50 char *buf)
51 {
52 struct cxl *adapter = to_cxl_adapter(device);
53
54 if (adapter->user_image_loaded)
55 return scnprintf(buf, PAGE_SIZE, "user\n");
56 return scnprintf(buf, PAGE_SIZE, "factory\n");
57 }
58
59 static ssize_t reset_adapter_store(struct device *device,
60 struct device_attribute *attr,
61 const char *buf, size_t count)
62 {
63 struct cxl *adapter = to_cxl_adapter(device);
64 int rc;
65 int val;
66
67 rc = sscanf(buf, "%i", &val);
68 if ((rc != 1) || (val != 1))
69 return -EINVAL;
70
71 if ((rc = cxl_reset(adapter)))
72 return rc;
73 return count;
74 }
75
76 static ssize_t load_image_on_perst_show(struct device *device,
77 struct device_attribute *attr,
78 char *buf)
79 {
80 struct cxl *adapter = to_cxl_adapter(device);
81
82 if (!adapter->perst_loads_image)
83 return scnprintf(buf, PAGE_SIZE, "none\n");
84
85 if (adapter->perst_select_user)
86 return scnprintf(buf, PAGE_SIZE, "user\n");
87 return scnprintf(buf, PAGE_SIZE, "factory\n");
88 }
89
90 static ssize_t load_image_on_perst_store(struct device *device,
91 struct device_attribute *attr,
92 const char *buf, size_t count)
93 {
94 struct cxl *adapter = to_cxl_adapter(device);
95 int rc;
96
97 if (!strncmp(buf, "none", 4))
98 adapter->perst_loads_image = false;
99 else if (!strncmp(buf, "user", 4)) {
100 adapter->perst_select_user = true;
101 adapter->perst_loads_image = true;
102 } else if (!strncmp(buf, "factory", 7)) {
103 adapter->perst_select_user = false;
104 adapter->perst_loads_image = true;
105 } else
106 return -EINVAL;
107
108 if ((rc = cxl_update_image_control(adapter)))
109 return rc;
110
111 return count;
112 }
113
114 static struct device_attribute adapter_attrs[] = {
115 __ATTR_RO(caia_version),
116 __ATTR_RO(psl_revision),
117 __ATTR_RO(base_image),
118 __ATTR_RO(image_loaded),
119 __ATTR_RW(load_image_on_perst),
120 __ATTR(reset, S_IWUSR, NULL, reset_adapter_store),
121 };
122
123
124 /********* AFU master specific attributes **********************************/
125
126 static ssize_t mmio_size_show_master(struct device *device,
127 struct device_attribute *attr,
128 char *buf)
129 {
130 struct cxl_afu *afu = to_afu_chardev_m(device);
131
132 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
133 }
134
135 static ssize_t pp_mmio_off_show(struct device *device,
136 struct device_attribute *attr,
137 char *buf)
138 {
139 struct cxl_afu *afu = to_afu_chardev_m(device);
140
141 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_offset);
142 }
143
144 static ssize_t pp_mmio_len_show(struct device *device,
145 struct device_attribute *attr,
146 char *buf)
147 {
148 struct cxl_afu *afu = to_afu_chardev_m(device);
149
150 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
151 }
152
153 static struct device_attribute afu_master_attrs[] = {
154 __ATTR(mmio_size, S_IRUGO, mmio_size_show_master, NULL),
155 __ATTR_RO(pp_mmio_off),
156 __ATTR_RO(pp_mmio_len),
157 };
158
159
160 /********* AFU attributes **************************************************/
161
162 static ssize_t mmio_size_show(struct device *device,
163 struct device_attribute *attr,
164 char *buf)
165 {
166 struct cxl_afu *afu = to_cxl_afu(device);
167
168 if (afu->pp_size)
169 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
170 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
171 }
172
173 static ssize_t reset_store_afu(struct device *device,
174 struct device_attribute *attr,
175 const char *buf, size_t count)
176 {
177 struct cxl_afu *afu = to_cxl_afu(device);
178 int rc;
179
180 /* Not safe to reset if it is currently in use */
181 mutex_lock(&afu->contexts_lock);
182 if (!idr_is_empty(&afu->contexts_idr)) {
183 rc = -EBUSY;
184 goto err;
185 }
186
187 if ((rc = cxl_afu_reset(afu)))
188 goto err;
189
190 rc = count;
191 err:
192 mutex_unlock(&afu->contexts_lock);
193 return rc;
194 }
195
196 static ssize_t irqs_min_show(struct device *device,
197 struct device_attribute *attr,
198 char *buf)
199 {
200 struct cxl_afu *afu = to_cxl_afu(device);
201
202 return scnprintf(buf, PAGE_SIZE, "%i\n", afu->pp_irqs);
203 }
204
205 static ssize_t irqs_max_show(struct device *device,
206 struct device_attribute *attr,
207 char *buf)
208 {
209 struct cxl_afu *afu = to_cxl_afu(device);
210
211 return scnprintf(buf, PAGE_SIZE, "%i\n", afu->irqs_max);
212 }
213
214 static ssize_t irqs_max_store(struct device *device,
215 struct device_attribute *attr,
216 const char *buf, size_t count)
217 {
218 struct cxl_afu *afu = to_cxl_afu(device);
219 ssize_t ret;
220 int irqs_max;
221
222 ret = sscanf(buf, "%i", &irqs_max);
223 if (ret != 1)
224 return -EINVAL;
225
226 if (irqs_max < afu->pp_irqs)
227 return -EINVAL;
228
229 if (irqs_max > afu->adapter->user_irqs)
230 return -EINVAL;
231
232 afu->irqs_max = irqs_max;
233 return count;
234 }
235
236 static ssize_t modes_supported_show(struct device *device,
237 struct device_attribute *attr, char *buf)
238 {
239 struct cxl_afu *afu = to_cxl_afu(device);
240 char *p = buf, *end = buf + PAGE_SIZE;
241
242 if (afu->modes_supported & CXL_MODE_DEDICATED)
243 p += scnprintf(p, end - p, "dedicated_process\n");
244 if (afu->modes_supported & CXL_MODE_DIRECTED)
245 p += scnprintf(p, end - p, "afu_directed\n");
246 return (p - buf);
247 }
248
249 static ssize_t prefault_mode_show(struct device *device,
250 struct device_attribute *attr,
251 char *buf)
252 {
253 struct cxl_afu *afu = to_cxl_afu(device);
254
255 switch (afu->prefault_mode) {
256 case CXL_PREFAULT_WED:
257 return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
258 case CXL_PREFAULT_ALL:
259 return scnprintf(buf, PAGE_SIZE, "all\n");
260 default:
261 return scnprintf(buf, PAGE_SIZE, "none\n");
262 }
263 }
264
265 static ssize_t prefault_mode_store(struct device *device,
266 struct device_attribute *attr,
267 const char *buf, size_t count)
268 {
269 struct cxl_afu *afu = to_cxl_afu(device);
270 enum prefault_modes mode = -1;
271
272 if (!strncmp(buf, "work_element_descriptor", 23))
273 mode = CXL_PREFAULT_WED;
274 if (!strncmp(buf, "all", 3))
275 mode = CXL_PREFAULT_ALL;
276 if (!strncmp(buf, "none", 4))
277 mode = CXL_PREFAULT_NONE;
278
279 if (mode == -1)
280 return -EINVAL;
281
282 afu->prefault_mode = mode;
283 return count;
284 }
285
286 static ssize_t mode_show(struct device *device,
287 struct device_attribute *attr,
288 char *buf)
289 {
290 struct cxl_afu *afu = to_cxl_afu(device);
291
292 if (afu->current_mode == CXL_MODE_DEDICATED)
293 return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
294 if (afu->current_mode == CXL_MODE_DIRECTED)
295 return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
296 return scnprintf(buf, PAGE_SIZE, "none\n");
297 }
298
299 static ssize_t mode_store(struct device *device, struct device_attribute *attr,
300 const char *buf, size_t count)
301 {
302 struct cxl_afu *afu = to_cxl_afu(device);
303 int old_mode, mode = -1;
304 int rc = -EBUSY;
305
306 /* can't change this if we have a user */
307 mutex_lock(&afu->contexts_lock);
308 if (!idr_is_empty(&afu->contexts_idr))
309 goto err;
310
311 if (!strncmp(buf, "dedicated_process", 17))
312 mode = CXL_MODE_DEDICATED;
313 if (!strncmp(buf, "afu_directed", 12))
314 mode = CXL_MODE_DIRECTED;
315 if (!strncmp(buf, "none", 4))
316 mode = 0;
317
318 if (mode == -1) {
319 rc = -EINVAL;
320 goto err;
321 }
322
323 /*
324 * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
325 * other contexts coming in before we are ready:
326 */
327 old_mode = afu->current_mode;
328 afu->current_mode = 0;
329 afu->num_procs = 0;
330
331 mutex_unlock(&afu->contexts_lock);
332
333 if ((rc = _cxl_afu_deactivate_mode(afu, old_mode)))
334 return rc;
335 if ((rc = cxl_afu_activate_mode(afu, mode)))
336 return rc;
337
338 return count;
339 err:
340 mutex_unlock(&afu->contexts_lock);
341 return rc;
342 }
343
344 static ssize_t api_version_show(struct device *device,
345 struct device_attribute *attr,
346 char *buf)
347 {
348 return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
349 }
350
351 static ssize_t api_version_compatible_show(struct device *device,
352 struct device_attribute *attr,
353 char *buf)
354 {
355 return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
356 }
357
358 static struct device_attribute afu_attrs[] = {
359 __ATTR_RO(mmio_size),
360 __ATTR_RO(irqs_min),
361 __ATTR_RW(irqs_max),
362 __ATTR_RO(modes_supported),
363 __ATTR_RW(mode),
364 __ATTR_RW(prefault_mode),
365 __ATTR_RO(api_version),
366 __ATTR_RO(api_version_compatible),
367 __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
368 };
369
370
371
372 int cxl_sysfs_adapter_add(struct cxl *adapter)
373 {
374 int i, rc;
375
376 for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
377 if ((rc = device_create_file(&adapter->dev, &adapter_attrs[i])))
378 goto err;
379 }
380 return 0;
381 err:
382 for (i--; i >= 0; i--)
383 device_remove_file(&adapter->dev, &adapter_attrs[i]);
384 return rc;
385 }
386 void cxl_sysfs_adapter_remove(struct cxl *adapter)
387 {
388 int i;
389
390 for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++)
391 device_remove_file(&adapter->dev, &adapter_attrs[i]);
392 }
393
394 int cxl_sysfs_afu_add(struct cxl_afu *afu)
395 {
396 int i, rc;
397
398 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
399 if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
400 goto err;
401 }
402
403 return 0;
404
405 err:
406 for (i--; i >= 0; i--)
407 device_remove_file(&afu->dev, &afu_attrs[i]);
408 return rc;
409 }
410
411 void cxl_sysfs_afu_remove(struct cxl_afu *afu)
412 {
413 int i;
414
415 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
416 device_remove_file(&afu->dev, &afu_attrs[i]);
417 }
418
419 int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
420 {
421 int i, rc;
422
423 for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
424 if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
425 goto err;
426 }
427
428 return 0;
429
430 err:
431 for (i--; i >= 0; i--)
432 device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
433 return rc;
434 }
435
436 void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
437 {
438 int i;
439
440 for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++)
441 device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
442 }
This page took 0.041619 seconds and 5 git commands to generate.