sysfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. #include <linux/kernel.h>
  10. #include <linux/device.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/pci_regs.h>
  13. #include "cxl.h"
  14. #define to_afu_chardev_m(d) dev_get_drvdata(d)
  15. /********* Adapter attributes **********************************************/
  16. static ssize_t caia_version_show(struct device *device,
  17. struct device_attribute *attr,
  18. char *buf)
  19. {
  20. struct cxl *adapter = to_cxl_adapter(device);
  21. return scnprintf(buf, PAGE_SIZE, "%i.%i\n", adapter->caia_major,
  22. adapter->caia_minor);
  23. }
  24. static ssize_t psl_revision_show(struct device *device,
  25. struct device_attribute *attr,
  26. char *buf)
  27. {
  28. struct cxl *adapter = to_cxl_adapter(device);
  29. return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_rev);
  30. }
  31. static ssize_t base_image_show(struct device *device,
  32. struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct cxl *adapter = to_cxl_adapter(device);
  36. return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->base_image);
  37. }
  38. static ssize_t image_loaded_show(struct device *device,
  39. struct device_attribute *attr,
  40. char *buf)
  41. {
  42. struct cxl *adapter = to_cxl_adapter(device);
  43. if (adapter->user_image_loaded)
  44. return scnprintf(buf, PAGE_SIZE, "user\n");
  45. return scnprintf(buf, PAGE_SIZE, "factory\n");
  46. }
  47. static ssize_t psl_timebase_synced_show(struct device *device,
  48. struct device_attribute *attr,
  49. char *buf)
  50. {
  51. struct cxl *adapter = to_cxl_adapter(device);
  52. return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_timebase_synced);
  53. }
  54. static ssize_t reset_adapter_store(struct device *device,
  55. struct device_attribute *attr,
  56. const char *buf, size_t count)
  57. {
  58. struct cxl *adapter = to_cxl_adapter(device);
  59. int rc;
  60. int val;
  61. rc = sscanf(buf, "%i", &val);
  62. if ((rc != 1) || (val != 1))
  63. return -EINVAL;
  64. if ((rc = cxl_ops->adapter_reset(adapter)))
  65. return rc;
  66. return count;
  67. }
  68. static ssize_t load_image_on_perst_show(struct device *device,
  69. struct device_attribute *attr,
  70. char *buf)
  71. {
  72. struct cxl *adapter = to_cxl_adapter(device);
  73. if (!adapter->perst_loads_image)
  74. return scnprintf(buf, PAGE_SIZE, "none\n");
  75. if (adapter->perst_select_user)
  76. return scnprintf(buf, PAGE_SIZE, "user\n");
  77. return scnprintf(buf, PAGE_SIZE, "factory\n");
  78. }
  79. static ssize_t load_image_on_perst_store(struct device *device,
  80. struct device_attribute *attr,
  81. const char *buf, size_t count)
  82. {
  83. struct cxl *adapter = to_cxl_adapter(device);
  84. int rc;
  85. if (!strncmp(buf, "none", 4))
  86. adapter->perst_loads_image = false;
  87. else if (!strncmp(buf, "user", 4)) {
  88. adapter->perst_select_user = true;
  89. adapter->perst_loads_image = true;
  90. } else if (!strncmp(buf, "factory", 7)) {
  91. adapter->perst_select_user = false;
  92. adapter->perst_loads_image = true;
  93. } else
  94. return -EINVAL;
  95. if ((rc = cxl_update_image_control(adapter)))
  96. return rc;
  97. return count;
  98. }
  99. static ssize_t perst_reloads_same_image_show(struct device *device,
  100. struct device_attribute *attr,
  101. char *buf)
  102. {
  103. struct cxl *adapter = to_cxl_adapter(device);
  104. return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->perst_same_image);
  105. }
  106. static ssize_t perst_reloads_same_image_store(struct device *device,
  107. struct device_attribute *attr,
  108. const char *buf, size_t count)
  109. {
  110. struct cxl *adapter = to_cxl_adapter(device);
  111. int rc;
  112. int val;
  113. rc = sscanf(buf, "%i", &val);
  114. if ((rc != 1) || !(val == 1 || val == 0))
  115. return -EINVAL;
  116. adapter->perst_same_image = (val == 1 ? true : false);
  117. return count;
  118. }
  119. static struct device_attribute adapter_attrs[] = {
  120. __ATTR_RO(caia_version),
  121. __ATTR_RO(psl_revision),
  122. __ATTR_RO(base_image),
  123. __ATTR_RO(image_loaded),
  124. __ATTR_RO(psl_timebase_synced),
  125. __ATTR_RW(load_image_on_perst),
  126. __ATTR_RW(perst_reloads_same_image),
  127. __ATTR(reset, S_IWUSR, NULL, reset_adapter_store),
  128. };
  129. /********* AFU master specific attributes **********************************/
  130. static ssize_t mmio_size_show_master(struct device *device,
  131. struct device_attribute *attr,
  132. char *buf)
  133. {
  134. struct cxl_afu *afu = to_afu_chardev_m(device);
  135. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
  136. }
  137. static ssize_t pp_mmio_off_show(struct device *device,
  138. struct device_attribute *attr,
  139. char *buf)
  140. {
  141. struct cxl_afu *afu = to_afu_chardev_m(device);
  142. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->native->pp_offset);
  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. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
  150. }
  151. static struct device_attribute afu_master_attrs[] = {
  152. __ATTR(mmio_size, S_IRUGO, mmio_size_show_master, NULL),
  153. __ATTR_RO(pp_mmio_off),
  154. __ATTR_RO(pp_mmio_len),
  155. };
  156. /********* AFU attributes **************************************************/
  157. static ssize_t mmio_size_show(struct device *device,
  158. struct device_attribute *attr,
  159. char *buf)
  160. {
  161. struct cxl_afu *afu = to_cxl_afu(device);
  162. if (afu->pp_size)
  163. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
  164. return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
  165. }
  166. static ssize_t reset_store_afu(struct device *device,
  167. struct device_attribute *attr,
  168. const char *buf, size_t count)
  169. {
  170. struct cxl_afu *afu = to_cxl_afu(device);
  171. int rc;
  172. /* Not safe to reset if it is currently in use */
  173. mutex_lock(&afu->contexts_lock);
  174. if (!idr_is_empty(&afu->contexts_idr)) {
  175. rc = -EBUSY;
  176. goto err;
  177. }
  178. if ((rc = cxl_ops->afu_reset(afu)))
  179. goto err;
  180. rc = count;
  181. err:
  182. mutex_unlock(&afu->contexts_lock);
  183. return rc;
  184. }
  185. static ssize_t irqs_min_show(struct device *device,
  186. struct device_attribute *attr,
  187. char *buf)
  188. {
  189. struct cxl_afu *afu = to_cxl_afu(device);
  190. return scnprintf(buf, PAGE_SIZE, "%i\n", afu->pp_irqs);
  191. }
  192. static ssize_t irqs_max_show(struct device *device,
  193. struct device_attribute *attr,
  194. char *buf)
  195. {
  196. struct cxl_afu *afu = to_cxl_afu(device);
  197. return scnprintf(buf, PAGE_SIZE, "%i\n", afu->irqs_max);
  198. }
  199. static ssize_t irqs_max_store(struct device *device,
  200. struct device_attribute *attr,
  201. const char *buf, size_t count)
  202. {
  203. struct cxl_afu *afu = to_cxl_afu(device);
  204. ssize_t ret;
  205. int irqs_max;
  206. ret = sscanf(buf, "%i", &irqs_max);
  207. if (ret != 1)
  208. return -EINVAL;
  209. if (irqs_max < afu->pp_irqs)
  210. return -EINVAL;
  211. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  212. if (irqs_max > afu->adapter->user_irqs)
  213. return -EINVAL;
  214. } else {
  215. /* pHyp sets a per-AFU limit */
  216. if (irqs_max > afu->guest->max_ints)
  217. return -EINVAL;
  218. }
  219. afu->irqs_max = irqs_max;
  220. return count;
  221. }
  222. static ssize_t modes_supported_show(struct device *device,
  223. struct device_attribute *attr, char *buf)
  224. {
  225. struct cxl_afu *afu = to_cxl_afu(device);
  226. char *p = buf, *end = buf + PAGE_SIZE;
  227. if (afu->modes_supported & CXL_MODE_DEDICATED)
  228. p += scnprintf(p, end - p, "dedicated_process\n");
  229. if (afu->modes_supported & CXL_MODE_DIRECTED)
  230. p += scnprintf(p, end - p, "afu_directed\n");
  231. return (p - buf);
  232. }
  233. static ssize_t prefault_mode_show(struct device *device,
  234. struct device_attribute *attr,
  235. char *buf)
  236. {
  237. struct cxl_afu *afu = to_cxl_afu(device);
  238. switch (afu->prefault_mode) {
  239. case CXL_PREFAULT_WED:
  240. return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
  241. case CXL_PREFAULT_ALL:
  242. return scnprintf(buf, PAGE_SIZE, "all\n");
  243. default:
  244. return scnprintf(buf, PAGE_SIZE, "none\n");
  245. }
  246. }
  247. static ssize_t prefault_mode_store(struct device *device,
  248. struct device_attribute *attr,
  249. const char *buf, size_t count)
  250. {
  251. struct cxl_afu *afu = to_cxl_afu(device);
  252. enum prefault_modes mode = -1;
  253. if (!strncmp(buf, "work_element_descriptor", 23))
  254. mode = CXL_PREFAULT_WED;
  255. if (!strncmp(buf, "all", 3))
  256. mode = CXL_PREFAULT_ALL;
  257. if (!strncmp(buf, "none", 4))
  258. mode = CXL_PREFAULT_NONE;
  259. if (mode == -1)
  260. return -EINVAL;
  261. afu->prefault_mode = mode;
  262. return count;
  263. }
  264. static ssize_t mode_show(struct device *device,
  265. struct device_attribute *attr,
  266. char *buf)
  267. {
  268. struct cxl_afu *afu = to_cxl_afu(device);
  269. if (afu->current_mode == CXL_MODE_DEDICATED)
  270. return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
  271. if (afu->current_mode == CXL_MODE_DIRECTED)
  272. return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
  273. return scnprintf(buf, PAGE_SIZE, "none\n");
  274. }
  275. static ssize_t mode_store(struct device *device, struct device_attribute *attr,
  276. const char *buf, size_t count)
  277. {
  278. struct cxl_afu *afu = to_cxl_afu(device);
  279. int old_mode, mode = -1;
  280. int rc = -EBUSY;
  281. /* can't change this if we have a user */
  282. mutex_lock(&afu->contexts_lock);
  283. if (!idr_is_empty(&afu->contexts_idr))
  284. goto err;
  285. if (!strncmp(buf, "dedicated_process", 17))
  286. mode = CXL_MODE_DEDICATED;
  287. if (!strncmp(buf, "afu_directed", 12))
  288. mode = CXL_MODE_DIRECTED;
  289. if (!strncmp(buf, "none", 4))
  290. mode = 0;
  291. if (mode == -1) {
  292. rc = -EINVAL;
  293. goto err;
  294. }
  295. /*
  296. * afu_deactivate_mode needs to be done outside the lock, prevent
  297. * other contexts coming in before we are ready:
  298. */
  299. old_mode = afu->current_mode;
  300. afu->current_mode = 0;
  301. afu->num_procs = 0;
  302. mutex_unlock(&afu->contexts_lock);
  303. if ((rc = cxl_ops->afu_deactivate_mode(afu, old_mode)))
  304. return rc;
  305. if ((rc = cxl_ops->afu_activate_mode(afu, mode)))
  306. return rc;
  307. return count;
  308. err:
  309. mutex_unlock(&afu->contexts_lock);
  310. return rc;
  311. }
  312. static ssize_t api_version_show(struct device *device,
  313. struct device_attribute *attr,
  314. char *buf)
  315. {
  316. return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
  317. }
  318. static ssize_t api_version_compatible_show(struct device *device,
  319. struct device_attribute *attr,
  320. char *buf)
  321. {
  322. return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
  323. }
  324. static ssize_t afu_eb_read(struct file *filp, struct kobject *kobj,
  325. struct bin_attribute *bin_attr, char *buf,
  326. loff_t off, size_t count)
  327. {
  328. struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj));
  329. return cxl_ops->afu_read_err_buffer(afu, buf, off, count);
  330. }
  331. static struct device_attribute afu_attrs[] = {
  332. __ATTR_RO(mmio_size),
  333. __ATTR_RO(irqs_min),
  334. __ATTR_RW(irqs_max),
  335. __ATTR_RO(modes_supported),
  336. __ATTR_RW(mode),
  337. __ATTR_RW(prefault_mode),
  338. __ATTR_RO(api_version),
  339. __ATTR_RO(api_version_compatible),
  340. __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
  341. };
  342. int cxl_sysfs_adapter_add(struct cxl *adapter)
  343. {
  344. struct device_attribute *dev_attr;
  345. int i, rc;
  346. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
  347. dev_attr = &adapter_attrs[i];
  348. if (cxl_ops->support_attributes(dev_attr->attr.name,
  349. CXL_ADAPTER_ATTRS)) {
  350. if ((rc = device_create_file(&adapter->dev, dev_attr)))
  351. goto err;
  352. }
  353. }
  354. return 0;
  355. err:
  356. for (i--; i >= 0; i--) {
  357. dev_attr = &adapter_attrs[i];
  358. if (cxl_ops->support_attributes(dev_attr->attr.name,
  359. CXL_ADAPTER_ATTRS))
  360. device_remove_file(&adapter->dev, dev_attr);
  361. }
  362. return rc;
  363. }
  364. void cxl_sysfs_adapter_remove(struct cxl *adapter)
  365. {
  366. struct device_attribute *dev_attr;
  367. int i;
  368. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
  369. dev_attr = &adapter_attrs[i];
  370. if (cxl_ops->support_attributes(dev_attr->attr.name,
  371. CXL_ADAPTER_ATTRS))
  372. device_remove_file(&adapter->dev, dev_attr);
  373. }
  374. }
  375. struct afu_config_record {
  376. struct kobject kobj;
  377. struct bin_attribute config_attr;
  378. struct list_head list;
  379. int cr;
  380. u16 device;
  381. u16 vendor;
  382. u32 class;
  383. };
  384. #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
  385. static ssize_t vendor_show(struct kobject *kobj,
  386. struct kobj_attribute *attr, char *buf)
  387. {
  388. struct afu_config_record *cr = to_cr(kobj);
  389. return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
  390. }
  391. static ssize_t device_show(struct kobject *kobj,
  392. struct kobj_attribute *attr, char *buf)
  393. {
  394. struct afu_config_record *cr = to_cr(kobj);
  395. return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
  396. }
  397. static ssize_t class_show(struct kobject *kobj,
  398. struct kobj_attribute *attr, char *buf)
  399. {
  400. struct afu_config_record *cr = to_cr(kobj);
  401. return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
  402. }
  403. static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
  404. struct bin_attribute *bin_attr, char *buf,
  405. loff_t off, size_t count)
  406. {
  407. struct afu_config_record *cr = to_cr(kobj);
  408. struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj->parent));
  409. u64 i, j, val, rc;
  410. for (i = 0; i < count;) {
  411. rc = cxl_ops->afu_cr_read64(afu, cr->cr, off & ~0x7, &val);
  412. if (rc)
  413. val = ~0ULL;
  414. for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
  415. buf[i] = (val >> (j * 8)) & 0xff;
  416. }
  417. return count;
  418. }
  419. static struct kobj_attribute vendor_attribute =
  420. __ATTR_RO(vendor);
  421. static struct kobj_attribute device_attribute =
  422. __ATTR_RO(device);
  423. static struct kobj_attribute class_attribute =
  424. __ATTR_RO(class);
  425. static struct attribute *afu_cr_attrs[] = {
  426. &vendor_attribute.attr,
  427. &device_attribute.attr,
  428. &class_attribute.attr,
  429. NULL,
  430. };
  431. static void release_afu_config_record(struct kobject *kobj)
  432. {
  433. struct afu_config_record *cr = to_cr(kobj);
  434. kfree(cr);
  435. }
  436. static struct kobj_type afu_config_record_type = {
  437. .sysfs_ops = &kobj_sysfs_ops,
  438. .release = release_afu_config_record,
  439. .default_attrs = afu_cr_attrs,
  440. };
  441. static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
  442. {
  443. struct afu_config_record *cr;
  444. int rc;
  445. cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
  446. if (!cr)
  447. return ERR_PTR(-ENOMEM);
  448. cr->cr = cr_idx;
  449. rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID, &cr->device);
  450. if (rc)
  451. goto err;
  452. rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID, &cr->vendor);
  453. if (rc)
  454. goto err;
  455. rc = cxl_ops->afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION, &cr->class);
  456. if (rc)
  457. goto err;
  458. cr->class >>= 8;
  459. /*
  460. * Export raw AFU PCIe like config record. For now this is read only by
  461. * root - we can expand that later to be readable by non-root and maybe
  462. * even writable provided we have a good use-case. Once we support
  463. * exposing AFUs through a virtual PHB they will get that for free from
  464. * Linux' PCI infrastructure, but until then it's not clear that we
  465. * need it for anything since the main use case is just identifying
  466. * AFUs, which can be done via the vendor, device and class attributes.
  467. */
  468. sysfs_bin_attr_init(&cr->config_attr);
  469. cr->config_attr.attr.name = "config";
  470. cr->config_attr.attr.mode = S_IRUSR;
  471. cr->config_attr.size = afu->crs_len;
  472. cr->config_attr.read = afu_read_config;
  473. rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
  474. &afu->dev.kobj, "cr%i", cr->cr);
  475. if (rc)
  476. goto err;
  477. rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
  478. if (rc)
  479. goto err1;
  480. rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
  481. if (rc)
  482. goto err2;
  483. return cr;
  484. err2:
  485. sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
  486. err1:
  487. kobject_put(&cr->kobj);
  488. return ERR_PTR(rc);
  489. err:
  490. kfree(cr);
  491. return ERR_PTR(rc);
  492. }
  493. void cxl_sysfs_afu_remove(struct cxl_afu *afu)
  494. {
  495. struct device_attribute *dev_attr;
  496. struct afu_config_record *cr, *tmp;
  497. int i;
  498. /* remove the err buffer bin attribute */
  499. if (afu->eb_len)
  500. device_remove_bin_file(&afu->dev, &afu->attr_eb);
  501. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  502. dev_attr = &afu_attrs[i];
  503. if (cxl_ops->support_attributes(dev_attr->attr.name,
  504. CXL_AFU_ATTRS))
  505. device_remove_file(&afu->dev, &afu_attrs[i]);
  506. }
  507. list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
  508. sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
  509. kobject_put(&cr->kobj);
  510. }
  511. }
  512. int cxl_sysfs_afu_add(struct cxl_afu *afu)
  513. {
  514. struct device_attribute *dev_attr;
  515. struct afu_config_record *cr;
  516. int i, rc;
  517. INIT_LIST_HEAD(&afu->crs);
  518. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  519. dev_attr = &afu_attrs[i];
  520. if (cxl_ops->support_attributes(dev_attr->attr.name,
  521. CXL_AFU_ATTRS)) {
  522. if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
  523. goto err;
  524. }
  525. }
  526. /* conditionally create the add the binary file for error info buffer */
  527. if (afu->eb_len) {
  528. sysfs_attr_init(&afu->attr_eb.attr);
  529. afu->attr_eb.attr.name = "afu_err_buff";
  530. afu->attr_eb.attr.mode = S_IRUGO;
  531. afu->attr_eb.size = afu->eb_len;
  532. afu->attr_eb.read = afu_eb_read;
  533. rc = device_create_bin_file(&afu->dev, &afu->attr_eb);
  534. if (rc) {
  535. dev_err(&afu->dev,
  536. "Unable to create eb attr for the afu. Err(%d)\n",
  537. rc);
  538. goto err;
  539. }
  540. }
  541. for (i = 0; i < afu->crs_num; i++) {
  542. cr = cxl_sysfs_afu_new_cr(afu, i);
  543. if (IS_ERR(cr)) {
  544. rc = PTR_ERR(cr);
  545. goto err1;
  546. }
  547. list_add(&cr->list, &afu->crs);
  548. }
  549. return 0;
  550. err1:
  551. cxl_sysfs_afu_remove(afu);
  552. return rc;
  553. err:
  554. /* reset the eb_len as we havent created the bin attr */
  555. afu->eb_len = 0;
  556. for (i--; i >= 0; i--) {
  557. dev_attr = &afu_attrs[i];
  558. if (cxl_ops->support_attributes(dev_attr->attr.name,
  559. CXL_AFU_ATTRS))
  560. device_remove_file(&afu->dev, &afu_attrs[i]);
  561. }
  562. return rc;
  563. }
  564. int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
  565. {
  566. struct device_attribute *dev_attr;
  567. int i, rc;
  568. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
  569. dev_attr = &afu_master_attrs[i];
  570. if (cxl_ops->support_attributes(dev_attr->attr.name,
  571. CXL_AFU_MASTER_ATTRS)) {
  572. if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
  573. goto err;
  574. }
  575. }
  576. return 0;
  577. err:
  578. for (i--; i >= 0; i--) {
  579. dev_attr = &afu_master_attrs[i];
  580. if (cxl_ops->support_attributes(dev_attr->attr.name,
  581. CXL_AFU_MASTER_ATTRS))
  582. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  583. }
  584. return rc;
  585. }
  586. void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
  587. {
  588. struct device_attribute *dev_attr;
  589. int i;
  590. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
  591. dev_attr = &afu_master_attrs[i];
  592. if (cxl_ops->support_attributes(dev_attr->attr.name,
  593. CXL_AFU_MASTER_ATTRS))
  594. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  595. }
  596. }