sysfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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, "work_element_descriptor", 23))
  270. mode = CXL_PREFAULT_WED;
  271. if (!strncmp(buf, "all", 3))
  272. mode = CXL_PREFAULT_ALL;
  273. if (!strncmp(buf, "none", 4))
  274. mode = CXL_PREFAULT_NONE;
  275. if (mode == -1)
  276. return -EINVAL;
  277. afu->prefault_mode = mode;
  278. return count;
  279. }
  280. static ssize_t mode_show(struct device *device,
  281. struct device_attribute *attr,
  282. char *buf)
  283. {
  284. struct cxl_afu *afu = to_cxl_afu(device);
  285. if (afu->current_mode == CXL_MODE_DEDICATED)
  286. return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
  287. if (afu->current_mode == CXL_MODE_DIRECTED)
  288. return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
  289. return scnprintf(buf, PAGE_SIZE, "none\n");
  290. }
  291. static ssize_t mode_store(struct device *device, struct device_attribute *attr,
  292. const char *buf, size_t count)
  293. {
  294. struct cxl_afu *afu = to_cxl_afu(device);
  295. int old_mode, mode = -1;
  296. int rc = -EBUSY;
  297. /* can't change this if we have a user */
  298. mutex_lock(&afu->contexts_lock);
  299. if (!idr_is_empty(&afu->contexts_idr))
  300. goto err;
  301. if (!strncmp(buf, "dedicated_process", 17))
  302. mode = CXL_MODE_DEDICATED;
  303. if (!strncmp(buf, "afu_directed", 12))
  304. mode = CXL_MODE_DIRECTED;
  305. if (!strncmp(buf, "none", 4))
  306. mode = 0;
  307. if (mode == -1) {
  308. rc = -EINVAL;
  309. goto err;
  310. }
  311. /*
  312. * afu_deactivate_mode needs to be done outside the lock, prevent
  313. * other contexts coming in before we are ready:
  314. */
  315. old_mode = afu->current_mode;
  316. afu->current_mode = 0;
  317. afu->num_procs = 0;
  318. mutex_unlock(&afu->contexts_lock);
  319. if ((rc = cxl_ops->afu_deactivate_mode(afu, old_mode)))
  320. return rc;
  321. if ((rc = cxl_ops->afu_activate_mode(afu, mode)))
  322. return rc;
  323. return count;
  324. err:
  325. mutex_unlock(&afu->contexts_lock);
  326. return rc;
  327. }
  328. static ssize_t api_version_show(struct device *device,
  329. struct device_attribute *attr,
  330. char *buf)
  331. {
  332. return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
  333. }
  334. static ssize_t api_version_compatible_show(struct device *device,
  335. struct device_attribute *attr,
  336. char *buf)
  337. {
  338. return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
  339. }
  340. static ssize_t afu_eb_read(struct file *filp, struct kobject *kobj,
  341. struct bin_attribute *bin_attr, char *buf,
  342. loff_t off, size_t count)
  343. {
  344. struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj));
  345. return cxl_ops->afu_read_err_buffer(afu, buf, off, count);
  346. }
  347. static struct device_attribute afu_attrs[] = {
  348. __ATTR_RO(mmio_size),
  349. __ATTR_RO(irqs_min),
  350. __ATTR_RW(irqs_max),
  351. __ATTR_RO(modes_supported),
  352. __ATTR_RW(mode),
  353. __ATTR_RW(prefault_mode),
  354. __ATTR_RO(api_version),
  355. __ATTR_RO(api_version_compatible),
  356. __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
  357. };
  358. int cxl_sysfs_adapter_add(struct cxl *adapter)
  359. {
  360. struct device_attribute *dev_attr;
  361. int i, rc;
  362. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
  363. dev_attr = &adapter_attrs[i];
  364. if (cxl_ops->support_attributes(dev_attr->attr.name,
  365. CXL_ADAPTER_ATTRS)) {
  366. if ((rc = device_create_file(&adapter->dev, dev_attr)))
  367. goto err;
  368. }
  369. }
  370. return 0;
  371. err:
  372. for (i--; i >= 0; i--) {
  373. dev_attr = &adapter_attrs[i];
  374. if (cxl_ops->support_attributes(dev_attr->attr.name,
  375. CXL_ADAPTER_ATTRS))
  376. device_remove_file(&adapter->dev, dev_attr);
  377. }
  378. return rc;
  379. }
  380. void cxl_sysfs_adapter_remove(struct cxl *adapter)
  381. {
  382. struct device_attribute *dev_attr;
  383. int i;
  384. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
  385. dev_attr = &adapter_attrs[i];
  386. if (cxl_ops->support_attributes(dev_attr->attr.name,
  387. CXL_ADAPTER_ATTRS))
  388. device_remove_file(&adapter->dev, dev_attr);
  389. }
  390. }
  391. struct afu_config_record {
  392. struct kobject kobj;
  393. struct bin_attribute config_attr;
  394. struct list_head list;
  395. int cr;
  396. u16 device;
  397. u16 vendor;
  398. u32 class;
  399. };
  400. #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
  401. static ssize_t vendor_show(struct kobject *kobj,
  402. struct kobj_attribute *attr, char *buf)
  403. {
  404. struct afu_config_record *cr = to_cr(kobj);
  405. return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
  406. }
  407. static ssize_t device_show(struct kobject *kobj,
  408. struct kobj_attribute *attr, char *buf)
  409. {
  410. struct afu_config_record *cr = to_cr(kobj);
  411. return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
  412. }
  413. static ssize_t class_show(struct kobject *kobj,
  414. struct kobj_attribute *attr, char *buf)
  415. {
  416. struct afu_config_record *cr = to_cr(kobj);
  417. return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
  418. }
  419. static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
  420. struct bin_attribute *bin_attr, char *buf,
  421. loff_t off, size_t count)
  422. {
  423. struct afu_config_record *cr = to_cr(kobj);
  424. struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj->parent));
  425. u64 i, j, val, rc;
  426. for (i = 0; i < count;) {
  427. rc = cxl_ops->afu_cr_read64(afu, cr->cr, off & ~0x7, &val);
  428. if (rc)
  429. val = ~0ULL;
  430. for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
  431. buf[i] = (val >> (j * 8)) & 0xff;
  432. }
  433. return count;
  434. }
  435. static struct kobj_attribute vendor_attribute =
  436. __ATTR_RO(vendor);
  437. static struct kobj_attribute device_attribute =
  438. __ATTR_RO(device);
  439. static struct kobj_attribute class_attribute =
  440. __ATTR_RO(class);
  441. static struct attribute *afu_cr_attrs[] = {
  442. &vendor_attribute.attr,
  443. &device_attribute.attr,
  444. &class_attribute.attr,
  445. NULL,
  446. };
  447. static void release_afu_config_record(struct kobject *kobj)
  448. {
  449. struct afu_config_record *cr = to_cr(kobj);
  450. kfree(cr);
  451. }
  452. static struct kobj_type afu_config_record_type = {
  453. .sysfs_ops = &kobj_sysfs_ops,
  454. .release = release_afu_config_record,
  455. .default_attrs = afu_cr_attrs,
  456. };
  457. static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
  458. {
  459. struct afu_config_record *cr;
  460. int rc;
  461. cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
  462. if (!cr)
  463. return ERR_PTR(-ENOMEM);
  464. cr->cr = cr_idx;
  465. rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID, &cr->device);
  466. if (rc)
  467. goto err;
  468. rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID, &cr->vendor);
  469. if (rc)
  470. goto err;
  471. rc = cxl_ops->afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION, &cr->class);
  472. if (rc)
  473. goto err;
  474. cr->class >>= 8;
  475. /*
  476. * Export raw AFU PCIe like config record. For now this is read only by
  477. * root - we can expand that later to be readable by non-root and maybe
  478. * even writable provided we have a good use-case. Once we support
  479. * exposing AFUs through a virtual PHB they will get that for free from
  480. * Linux' PCI infrastructure, but until then it's not clear that we
  481. * need it for anything since the main use case is just identifying
  482. * AFUs, which can be done via the vendor, device and class attributes.
  483. */
  484. sysfs_bin_attr_init(&cr->config_attr);
  485. cr->config_attr.attr.name = "config";
  486. cr->config_attr.attr.mode = S_IRUSR;
  487. cr->config_attr.size = afu->crs_len;
  488. cr->config_attr.read = afu_read_config;
  489. rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
  490. &afu->dev.kobj, "cr%i", cr->cr);
  491. if (rc)
  492. goto err;
  493. rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
  494. if (rc)
  495. goto err1;
  496. rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
  497. if (rc)
  498. goto err2;
  499. return cr;
  500. err2:
  501. sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
  502. err1:
  503. kobject_put(&cr->kobj);
  504. return ERR_PTR(rc);
  505. err:
  506. kfree(cr);
  507. return ERR_PTR(rc);
  508. }
  509. void cxl_sysfs_afu_remove(struct cxl_afu *afu)
  510. {
  511. struct device_attribute *dev_attr;
  512. struct afu_config_record *cr, *tmp;
  513. int i;
  514. /* remove the err buffer bin attribute */
  515. if (afu->eb_len)
  516. device_remove_bin_file(&afu->dev, &afu->attr_eb);
  517. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  518. dev_attr = &afu_attrs[i];
  519. if (cxl_ops->support_attributes(dev_attr->attr.name,
  520. CXL_AFU_ATTRS))
  521. device_remove_file(&afu->dev, &afu_attrs[i]);
  522. }
  523. list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
  524. sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
  525. kobject_put(&cr->kobj);
  526. }
  527. }
  528. int cxl_sysfs_afu_add(struct cxl_afu *afu)
  529. {
  530. struct device_attribute *dev_attr;
  531. struct afu_config_record *cr;
  532. int i, rc;
  533. INIT_LIST_HEAD(&afu->crs);
  534. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  535. dev_attr = &afu_attrs[i];
  536. if (cxl_ops->support_attributes(dev_attr->attr.name,
  537. CXL_AFU_ATTRS)) {
  538. if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
  539. goto err;
  540. }
  541. }
  542. /* conditionally create the add the binary file for error info buffer */
  543. if (afu->eb_len) {
  544. sysfs_attr_init(&afu->attr_eb.attr);
  545. afu->attr_eb.attr.name = "afu_err_buff";
  546. afu->attr_eb.attr.mode = S_IRUGO;
  547. afu->attr_eb.size = afu->eb_len;
  548. afu->attr_eb.read = afu_eb_read;
  549. rc = device_create_bin_file(&afu->dev, &afu->attr_eb);
  550. if (rc) {
  551. dev_err(&afu->dev,
  552. "Unable to create eb attr for the afu. Err(%d)\n",
  553. rc);
  554. goto err;
  555. }
  556. }
  557. for (i = 0; i < afu->crs_num; i++) {
  558. cr = cxl_sysfs_afu_new_cr(afu, i);
  559. if (IS_ERR(cr)) {
  560. rc = PTR_ERR(cr);
  561. goto err1;
  562. }
  563. list_add(&cr->list, &afu->crs);
  564. }
  565. return 0;
  566. err1:
  567. cxl_sysfs_afu_remove(afu);
  568. return rc;
  569. err:
  570. /* reset the eb_len as we havent created the bin attr */
  571. afu->eb_len = 0;
  572. for (i--; i >= 0; i--) {
  573. dev_attr = &afu_attrs[i];
  574. if (cxl_ops->support_attributes(dev_attr->attr.name,
  575. CXL_AFU_ATTRS))
  576. device_remove_file(&afu->dev, &afu_attrs[i]);
  577. }
  578. return rc;
  579. }
  580. int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
  581. {
  582. struct device_attribute *dev_attr;
  583. int i, rc;
  584. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
  585. dev_attr = &afu_master_attrs[i];
  586. if (cxl_ops->support_attributes(dev_attr->attr.name,
  587. CXL_AFU_MASTER_ATTRS)) {
  588. if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
  589. goto err;
  590. }
  591. }
  592. return 0;
  593. err:
  594. for (i--; i >= 0; i--) {
  595. dev_attr = &afu_master_attrs[i];
  596. if (cxl_ops->support_attributes(dev_attr->attr.name,
  597. CXL_AFU_MASTER_ATTRS))
  598. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  599. }
  600. return rc;
  601. }
  602. void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
  603. {
  604. struct device_attribute *dev_attr;
  605. int i;
  606. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
  607. dev_attr = &afu_master_attrs[i];
  608. if (cxl_ops->support_attributes(dev_attr->attr.name,
  609. CXL_AFU_MASTER_ATTRS))
  610. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  611. }
  612. }