sysfs.c 18 KB

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