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.
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/sysfs.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
,
24 struct cxl
*adapter
= to_cxl_adapter(device
);
26 return scnprintf(buf
, PAGE_SIZE
, "%i.%i\n", adapter
->caia_major
,
30 static ssize_t
psl_revision_show(struct device
*device
,
31 struct device_attribute
*attr
,
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
,
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
,
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 ssize_t
reset_adapter_store(struct device
*device
,
60 struct device_attribute
*attr
,
61 const char *buf
, size_t count
)
63 struct cxl
*adapter
= to_cxl_adapter(device
);
67 rc
= sscanf(buf
, "%i", &val
);
68 if ((rc
!= 1) || (val
!= 1))
71 if ((rc
= cxl_reset(adapter
)))
76 static ssize_t
load_image_on_perst_show(struct device
*device
,
77 struct device_attribute
*attr
,
80 struct cxl
*adapter
= to_cxl_adapter(device
);
82 if (!adapter
->perst_loads_image
)
83 return scnprintf(buf
, PAGE_SIZE
, "none\n");
85 if (adapter
->perst_select_user
)
86 return scnprintf(buf
, PAGE_SIZE
, "user\n");
87 return scnprintf(buf
, PAGE_SIZE
, "factory\n");
90 static ssize_t
load_image_on_perst_store(struct device
*device
,
91 struct device_attribute
*attr
,
92 const char *buf
, size_t count
)
94 struct cxl
*adapter
= to_cxl_adapter(device
);
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;
108 if ((rc
= cxl_update_image_control(adapter
)))
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
),
124 /********* AFU master specific attributes **********************************/
126 static ssize_t
mmio_size_show_master(struct device
*device
,
127 struct device_attribute
*attr
,
130 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
132 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->adapter
->ps_size
);
135 static ssize_t
pp_mmio_off_show(struct device
*device
,
136 struct device_attribute
*attr
,
139 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
141 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->pp_offset
);
144 static ssize_t
pp_mmio_len_show(struct device
*device
,
145 struct device_attribute
*attr
,
148 struct cxl_afu
*afu
= to_afu_chardev_m(device
);
150 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->pp_size
);
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
),
160 /********* AFU attributes **************************************************/
162 static ssize_t
mmio_size_show(struct device
*device
,
163 struct device_attribute
*attr
,
166 struct cxl_afu
*afu
= to_cxl_afu(device
);
169 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->pp_size
);
170 return scnprintf(buf
, PAGE_SIZE
, "%llu\n", afu
->adapter
->ps_size
);
173 static ssize_t
reset_store_afu(struct device
*device
,
174 struct device_attribute
*attr
,
175 const char *buf
, size_t count
)
177 struct cxl_afu
*afu
= to_cxl_afu(device
);
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
)) {
187 if ((rc
= cxl_afu_reset(afu
)))
192 mutex_unlock(&afu
->contexts_lock
);
196 static ssize_t
irqs_min_show(struct device
*device
,
197 struct device_attribute
*attr
,
200 struct cxl_afu
*afu
= to_cxl_afu(device
);
202 return scnprintf(buf
, PAGE_SIZE
, "%i\n", afu
->pp_irqs
);
205 static ssize_t
irqs_max_show(struct device
*device
,
206 struct device_attribute
*attr
,
209 struct cxl_afu
*afu
= to_cxl_afu(device
);
211 return scnprintf(buf
, PAGE_SIZE
, "%i\n", afu
->irqs_max
);
214 static ssize_t
irqs_max_store(struct device
*device
,
215 struct device_attribute
*attr
,
216 const char *buf
, size_t count
)
218 struct cxl_afu
*afu
= to_cxl_afu(device
);
222 ret
= sscanf(buf
, "%i", &irqs_max
);
226 if (irqs_max
< afu
->pp_irqs
)
229 if (irqs_max
> afu
->adapter
->user_irqs
)
232 afu
->irqs_max
= irqs_max
;
236 static ssize_t
modes_supported_show(struct device
*device
,
237 struct device_attribute
*attr
, char *buf
)
239 struct cxl_afu
*afu
= to_cxl_afu(device
);
240 char *p
= buf
, *end
= buf
+ PAGE_SIZE
;
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");
249 static ssize_t
prefault_mode_show(struct device
*device
,
250 struct device_attribute
*attr
,
253 struct cxl_afu
*afu
= to_cxl_afu(device
);
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");
261 return scnprintf(buf
, PAGE_SIZE
, "none\n");
265 static ssize_t
prefault_mode_store(struct device
*device
,
266 struct device_attribute
*attr
,
267 const char *buf
, size_t count
)
269 struct cxl_afu
*afu
= to_cxl_afu(device
);
270 enum prefault_modes mode
= -1;
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
;
282 afu
->prefault_mode
= mode
;
286 static ssize_t
mode_show(struct device
*device
,
287 struct device_attribute
*attr
,
290 struct cxl_afu
*afu
= to_cxl_afu(device
);
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");
299 static ssize_t
mode_store(struct device
*device
, struct device_attribute
*attr
,
300 const char *buf
, size_t count
)
302 struct cxl_afu
*afu
= to_cxl_afu(device
);
303 int old_mode
, mode
= -1;
306 /* can't change this if we have a user */
307 mutex_lock(&afu
->contexts_lock
);
308 if (!idr_is_empty(&afu
->contexts_idr
))
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))
324 * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
325 * other contexts coming in before we are ready:
327 old_mode
= afu
->current_mode
;
328 afu
->current_mode
= 0;
331 mutex_unlock(&afu
->contexts_lock
);
333 if ((rc
= _cxl_afu_deactivate_mode(afu
, old_mode
)))
335 if ((rc
= cxl_afu_activate_mode(afu
, mode
)))
340 mutex_unlock(&afu
->contexts_lock
);
344 static ssize_t
api_version_show(struct device
*device
,
345 struct device_attribute
*attr
,
348 return scnprintf(buf
, PAGE_SIZE
, "%i\n", CXL_API_VERSION
);
351 static ssize_t
api_version_compatible_show(struct device
*device
,
352 struct device_attribute
*attr
,
355 return scnprintf(buf
, PAGE_SIZE
, "%i\n", CXL_API_VERSION_COMPATIBLE
);
358 static struct device_attribute afu_attrs
[] = {
359 __ATTR_RO(mmio_size
),
362 __ATTR_RO(modes_supported
),
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
),
372 int cxl_sysfs_adapter_add(struct cxl
*adapter
)
376 for (i
= 0; i
< ARRAY_SIZE(adapter_attrs
); i
++) {
377 if ((rc
= device_create_file(&adapter
->dev
, &adapter_attrs
[i
])))
382 for (i
--; i
>= 0; i
--)
383 device_remove_file(&adapter
->dev
, &adapter_attrs
[i
]);
386 void cxl_sysfs_adapter_remove(struct cxl
*adapter
)
390 for (i
= 0; i
< ARRAY_SIZE(adapter_attrs
); i
++)
391 device_remove_file(&adapter
->dev
, &adapter_attrs
[i
]);
394 int cxl_sysfs_afu_add(struct cxl_afu
*afu
)
398 for (i
= 0; i
< ARRAY_SIZE(afu_attrs
); i
++) {
399 if ((rc
= device_create_file(&afu
->dev
, &afu_attrs
[i
])))
406 for (i
--; i
>= 0; i
--)
407 device_remove_file(&afu
->dev
, &afu_attrs
[i
]);
411 void cxl_sysfs_afu_remove(struct cxl_afu
*afu
)
415 for (i
= 0; i
< ARRAY_SIZE(afu_attrs
); i
++)
416 device_remove_file(&afu
->dev
, &afu_attrs
[i
]);
419 int cxl_sysfs_afu_m_add(struct cxl_afu
*afu
)
423 for (i
= 0; i
< ARRAY_SIZE(afu_master_attrs
); i
++) {
424 if ((rc
= device_create_file(afu
->chardev_m
, &afu_master_attrs
[i
])))
431 for (i
--; i
>= 0; i
--)
432 device_remove_file(afu
->chardev_m
, &afu_master_attrs
[i
]);
436 void cxl_sysfs_afu_m_remove(struct cxl_afu
*afu
)
440 for (i
= 0; i
< ARRAY_SIZE(afu_master_attrs
); i
++)
441 device_remove_file(afu
->chardev_m
, &afu_master_attrs
[i
]);