sysfs.c 17 KB

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