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>
13 #include <linux/pci_regs.h>
17 #define to_afu_chardev_m(d) dev_get_drvdata(d)
19 /********* Adapter attributes **********************************************/
21 static ssize_t caia_version_show(struct device *device,
22 struct device_attribute *attr,
25 struct cxl *adapter = to_cxl_adapter(device);
27 return scnprintf(buf, PAGE_SIZE, "%i.%i\n", adapter->caia_major,
31 static ssize_t psl_revision_show(struct device *device,
32 struct device_attribute *attr,
35 struct cxl *adapter = to_cxl_adapter(device);
37 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_rev);
40 static ssize_t base_image_show(struct device *device,
41 struct device_attribute *attr,
44 struct cxl *adapter = to_cxl_adapter(device);
46 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->base_image);
49 static ssize_t image_loaded_show(struct device *device,
50 struct device_attribute *attr,
53 struct cxl *adapter = to_cxl_adapter(device);
55 if (adapter->user_image_loaded)
56 return scnprintf(buf, PAGE_SIZE, "user\n");
57 return scnprintf(buf, PAGE_SIZE, "factory\n");
60 static ssize_t reset_adapter_store(struct device *device,
61 struct device_attribute *attr,
62 const char *buf, size_t count)
64 struct cxl *adapter = to_cxl_adapter(device);
68 rc = sscanf(buf, "%i", &val);
69 if ((rc != 1) || (val != 1))
72 if ((rc = cxl_reset(adapter)))
77 static ssize_t load_image_on_perst_show(struct device *device,
78 struct device_attribute *attr,
81 struct cxl *adapter = to_cxl_adapter(device);
83 if (!adapter->perst_loads_image)
84 return scnprintf(buf, PAGE_SIZE, "none\n");
86 if (adapter->perst_select_user)
87 return scnprintf(buf, PAGE_SIZE, "user\n");
88 return scnprintf(buf, PAGE_SIZE, "factory\n");
91 static ssize_t load_image_on_perst_store(struct device *device,
92 struct device_attribute *attr,
93 const char *buf, size_t count)
95 struct cxl *adapter = to_cxl_adapter(device);
98 if (!strncmp(buf, "none", 4))
99 adapter->perst_loads_image = false;
100 else if (!strncmp(buf, "user", 4)) {
101 adapter->perst_select_user = true;
102 adapter->perst_loads_image = true;
103 } else if (!strncmp(buf, "factory", 7)) {
104 adapter->perst_select_user = false;
105 adapter->perst_loads_image = true;
109 if ((rc = cxl_update_image_control(adapter)))
115 static ssize_t perst_reloads_same_image_show(struct device *device,
116 struct device_attribute *attr,
119 struct cxl *adapter = to_cxl_adapter(device);
121 return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->perst_same_image);
124 static ssize_t perst_reloads_same_image_store(struct device *device,
125 struct device_attribute *attr,
126 const char *buf, size_t count)
128 struct cxl *adapter = to_cxl_adapter(device);
132 rc = sscanf(buf, "%i", &val);
133 if ((rc != 1) || !(val == 1 || val == 0))
136 adapter->perst_same_image = (val == 1 ? true : false);
140 static struct device_attribute adapter_attrs[] = {
141 __ATTR_RO(caia_version),
142 __ATTR_RO(psl_revision),
143 __ATTR_RO(base_image),
144 __ATTR_RO(image_loaded),
145 __ATTR_RW(load_image_on_perst),
146 __ATTR_RW(perst_reloads_same_image),
147 __ATTR(reset, S_IWUSR, NULL, reset_adapter_store),
151 /********* AFU master specific attributes **********************************/
153 static ssize_t mmio_size_show_master(struct device *device,
154 struct device_attribute *attr,
157 struct cxl_afu *afu = to_afu_chardev_m(device);
159 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
162 static ssize_t pp_mmio_off_show(struct device *device,
163 struct device_attribute *attr,
166 struct cxl_afu *afu = to_afu_chardev_m(device);
168 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_offset);
171 static ssize_t pp_mmio_len_show(struct device *device,
172 struct device_attribute *attr,
175 struct cxl_afu *afu = to_afu_chardev_m(device);
177 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
180 static struct device_attribute afu_master_attrs[] = {
181 __ATTR(mmio_size, S_IRUGO, mmio_size_show_master, NULL),
182 __ATTR_RO(pp_mmio_off),
183 __ATTR_RO(pp_mmio_len),
187 /********* AFU attributes **************************************************/
189 static ssize_t mmio_size_show(struct device *device,
190 struct device_attribute *attr,
193 struct cxl_afu *afu = to_cxl_afu(device);
196 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
197 return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
200 static ssize_t reset_store_afu(struct device *device,
201 struct device_attribute *attr,
202 const char *buf, size_t count)
204 struct cxl_afu *afu = to_cxl_afu(device);
207 /* Not safe to reset if it is currently in use */
208 mutex_lock(&afu->contexts_lock);
209 if (!idr_is_empty(&afu->contexts_idr)) {
214 if ((rc = __cxl_afu_reset(afu)))
219 mutex_unlock(&afu->contexts_lock);
223 static ssize_t irqs_min_show(struct device *device,
224 struct device_attribute *attr,
227 struct cxl_afu *afu = to_cxl_afu(device);
229 return scnprintf(buf, PAGE_SIZE, "%i\n", afu->pp_irqs);
232 static ssize_t irqs_max_show(struct device *device,
233 struct device_attribute *attr,
236 struct cxl_afu *afu = to_cxl_afu(device);
238 return scnprintf(buf, PAGE_SIZE, "%i\n", afu->irqs_max);
241 static ssize_t irqs_max_store(struct device *device,
242 struct device_attribute *attr,
243 const char *buf, size_t count)
245 struct cxl_afu *afu = to_cxl_afu(device);
249 ret = sscanf(buf, "%i", &irqs_max);
253 if (irqs_max < afu->pp_irqs)
256 if (irqs_max > afu->adapter->user_irqs)
259 afu->irqs_max = irqs_max;
263 static ssize_t modes_supported_show(struct device *device,
264 struct device_attribute *attr, char *buf)
266 struct cxl_afu *afu = to_cxl_afu(device);
267 char *p = buf, *end = buf + PAGE_SIZE;
269 if (afu->modes_supported & CXL_MODE_DEDICATED)
270 p += scnprintf(p, end - p, "dedicated_process\n");
271 if (afu->modes_supported & CXL_MODE_DIRECTED)
272 p += scnprintf(p, end - p, "afu_directed\n");
276 static ssize_t prefault_mode_show(struct device *device,
277 struct device_attribute *attr,
280 struct cxl_afu *afu = to_cxl_afu(device);
282 switch (afu->prefault_mode) {
283 case CXL_PREFAULT_WED:
284 return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
285 case CXL_PREFAULT_ALL:
286 return scnprintf(buf, PAGE_SIZE, "all\n");
288 return scnprintf(buf, PAGE_SIZE, "none\n");
292 static ssize_t prefault_mode_store(struct device *device,
293 struct device_attribute *attr,
294 const char *buf, size_t count)
296 struct cxl_afu *afu = to_cxl_afu(device);
297 enum prefault_modes mode = -1;
299 if (!strncmp(buf, "work_element_descriptor", 23))
300 mode = CXL_PREFAULT_WED;
301 if (!strncmp(buf, "all", 3))
302 mode = CXL_PREFAULT_ALL;
303 if (!strncmp(buf, "none", 4))
304 mode = CXL_PREFAULT_NONE;
309 afu->prefault_mode = mode;
313 static ssize_t mode_show(struct device *device,
314 struct device_attribute *attr,
317 struct cxl_afu *afu = to_cxl_afu(device);
319 if (afu->current_mode == CXL_MODE_DEDICATED)
320 return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
321 if (afu->current_mode == CXL_MODE_DIRECTED)
322 return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
323 return scnprintf(buf, PAGE_SIZE, "none\n");
326 static ssize_t mode_store(struct device *device, struct device_attribute *attr,
327 const char *buf, size_t count)
329 struct cxl_afu *afu = to_cxl_afu(device);
330 int old_mode, mode = -1;
333 /* can't change this if we have a user */
334 mutex_lock(&afu->contexts_lock);
335 if (!idr_is_empty(&afu->contexts_idr))
338 if (!strncmp(buf, "dedicated_process", 17))
339 mode = CXL_MODE_DEDICATED;
340 if (!strncmp(buf, "afu_directed", 12))
341 mode = CXL_MODE_DIRECTED;
342 if (!strncmp(buf, "none", 4))
351 * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
352 * other contexts coming in before we are ready:
354 old_mode = afu->current_mode;
355 afu->current_mode = 0;
358 mutex_unlock(&afu->contexts_lock);
360 if ((rc = _cxl_afu_deactivate_mode(afu, old_mode)))
362 if ((rc = cxl_afu_activate_mode(afu, mode)))
367 mutex_unlock(&afu->contexts_lock);
371 static ssize_t api_version_show(struct device *device,
372 struct device_attribute *attr,
375 return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
378 static ssize_t api_version_compatible_show(struct device *device,
379 struct device_attribute *attr,
382 return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
385 static ssize_t afu_eb_read(struct file *filp, struct kobject *kobj,
386 struct bin_attribute *bin_attr, char *buf,
387 loff_t off, size_t count)
389 struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj));
391 return cxl_afu_read_err_buffer(afu, buf, off, count);
394 static struct device_attribute afu_attrs[] = {
395 __ATTR_RO(mmio_size),
398 __ATTR_RO(modes_supported),
400 __ATTR_RW(prefault_mode),
401 __ATTR_RO(api_version),
402 __ATTR_RO(api_version_compatible),
403 __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
406 int cxl_sysfs_adapter_add(struct cxl *adapter)
410 for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
411 if ((rc = device_create_file(&adapter->dev, &adapter_attrs[i])))
416 for (i--; i >= 0; i--)
417 device_remove_file(&adapter->dev, &adapter_attrs[i]);
420 void cxl_sysfs_adapter_remove(struct cxl *adapter)
424 for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++)
425 device_remove_file(&adapter->dev, &adapter_attrs[i]);
428 struct afu_config_record {
430 struct bin_attribute config_attr;
431 struct list_head list;
438 #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
440 static ssize_t vendor_show(struct kobject *kobj,
441 struct kobj_attribute *attr, char *buf)
443 struct afu_config_record *cr = to_cr(kobj);
445 return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
448 static ssize_t device_show(struct kobject *kobj,
449 struct kobj_attribute *attr, char *buf)
451 struct afu_config_record *cr = to_cr(kobj);
453 return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
456 static ssize_t class_show(struct kobject *kobj,
457 struct kobj_attribute *attr, char *buf)
459 struct afu_config_record *cr = to_cr(kobj);
461 return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
464 static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
465 struct bin_attribute *bin_attr, char *buf,
466 loff_t off, size_t count)
468 struct afu_config_record *cr = to_cr(kobj);
469 struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj->parent));
473 for (i = 0; i < count;) {
474 val = cxl_afu_cr_read64(afu, cr->cr, off & ~0x7);
475 for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
476 buf[i] = (val >> (j * 8)) & 0xff;
482 static struct kobj_attribute vendor_attribute =
484 static struct kobj_attribute device_attribute =
486 static struct kobj_attribute class_attribute =
489 static struct attribute *afu_cr_attrs[] = {
490 &vendor_attribute.attr,
491 &device_attribute.attr,
492 &class_attribute.attr,
496 static void release_afu_config_record(struct kobject *kobj)
498 struct afu_config_record *cr = to_cr(kobj);
503 static struct kobj_type afu_config_record_type = {
504 .sysfs_ops = &kobj_sysfs_ops,
505 .release = release_afu_config_record,
506 .default_attrs = afu_cr_attrs,
509 static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
511 struct afu_config_record *cr;
514 cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
516 return ERR_PTR(-ENOMEM);
519 cr->device = cxl_afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID);
520 cr->vendor = cxl_afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID);
521 cr->class = cxl_afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION) >> 8;
524 * Export raw AFU PCIe like config record. For now this is read only by
525 * root - we can expand that later to be readable by non-root and maybe
526 * even writable provided we have a good use-case. Once we suport
527 * exposing AFUs through a virtual PHB they will get that for free from
528 * Linux' PCI infrastructure, but until then it's not clear that we
529 * need it for anything since the main use case is just identifying
530 * AFUs, which can be done via the vendor, device and class attributes.
532 sysfs_bin_attr_init(&cr->config_attr);
533 cr->config_attr.attr.name = "config";
534 cr->config_attr.attr.mode = S_IRUSR;
535 cr->config_attr.size = afu->crs_len;
536 cr->config_attr.read = afu_read_config;
538 rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
539 &afu->dev.kobj, "cr%i", cr->cr);
543 rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
547 rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
553 sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
555 kobject_put(&cr->kobj);
562 void cxl_sysfs_afu_remove(struct cxl_afu *afu)
564 struct afu_config_record *cr, *tmp;
567 /* remove the err buffer bin attribute */
569 device_remove_bin_file(&afu->dev, &afu->attr_eb);
571 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
572 device_remove_file(&afu->dev, &afu_attrs[i]);
574 list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
575 sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
576 kobject_put(&cr->kobj);
580 int cxl_sysfs_afu_add(struct cxl_afu *afu)
582 struct afu_config_record *cr;
585 INIT_LIST_HEAD(&afu->crs);
587 for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
588 if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
592 /* conditionally create the add the binary file for error info buffer */
594 sysfs_attr_init(&afu->attr_eb.attr);
596 afu->attr_eb.attr.name = "afu_err_buff";
597 afu->attr_eb.attr.mode = S_IRUGO;
598 afu->attr_eb.size = afu->eb_len;
599 afu->attr_eb.read = afu_eb_read;
601 rc = device_create_bin_file(&afu->dev, &afu->attr_eb);
604 "Unable to create eb attr for the afu. Err(%d)\n",
610 for (i = 0; i < afu->crs_num; i++) {
611 cr = cxl_sysfs_afu_new_cr(afu, i);
616 list_add(&cr->list, &afu->crs);
622 cxl_sysfs_afu_remove(afu);
625 /* reset the eb_len as we havent created the bin attr */
628 for (i--; i >= 0; i--)
629 device_remove_file(&afu->dev, &afu_attrs[i]);
633 int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
637 for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
638 if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
645 for (i--; i >= 0; i--)
646 device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
650 void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
654 for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++)
655 device_remove_file(afu->chardev_m, &afu_master_attrs[i]);