sysfs.c 15 KB

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