sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 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->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_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 (irqs_max > afu->adapter->user_irqs)
  204. return -EINVAL;
  205. afu->irqs_max = irqs_max;
  206. return count;
  207. }
  208. static ssize_t modes_supported_show(struct device *device,
  209. struct device_attribute *attr, char *buf)
  210. {
  211. struct cxl_afu *afu = to_cxl_afu(device);
  212. char *p = buf, *end = buf + PAGE_SIZE;
  213. if (afu->modes_supported & CXL_MODE_DEDICATED)
  214. p += scnprintf(p, end - p, "dedicated_process\n");
  215. if (afu->modes_supported & CXL_MODE_DIRECTED)
  216. p += scnprintf(p, end - p, "afu_directed\n");
  217. return (p - buf);
  218. }
  219. static ssize_t prefault_mode_show(struct device *device,
  220. struct device_attribute *attr,
  221. char *buf)
  222. {
  223. struct cxl_afu *afu = to_cxl_afu(device);
  224. switch (afu->prefault_mode) {
  225. case CXL_PREFAULT_WED:
  226. return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
  227. case CXL_PREFAULT_ALL:
  228. return scnprintf(buf, PAGE_SIZE, "all\n");
  229. default:
  230. return scnprintf(buf, PAGE_SIZE, "none\n");
  231. }
  232. }
  233. static ssize_t prefault_mode_store(struct device *device,
  234. struct device_attribute *attr,
  235. const char *buf, size_t count)
  236. {
  237. struct cxl_afu *afu = to_cxl_afu(device);
  238. enum prefault_modes mode = -1;
  239. if (!strncmp(buf, "work_element_descriptor", 23))
  240. mode = CXL_PREFAULT_WED;
  241. if (!strncmp(buf, "all", 3))
  242. mode = CXL_PREFAULT_ALL;
  243. if (!strncmp(buf, "none", 4))
  244. mode = CXL_PREFAULT_NONE;
  245. if (mode == -1)
  246. return -EINVAL;
  247. afu->prefault_mode = mode;
  248. return count;
  249. }
  250. static ssize_t mode_show(struct device *device,
  251. struct device_attribute *attr,
  252. char *buf)
  253. {
  254. struct cxl_afu *afu = to_cxl_afu(device);
  255. if (afu->current_mode == CXL_MODE_DEDICATED)
  256. return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
  257. if (afu->current_mode == CXL_MODE_DIRECTED)
  258. return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
  259. return scnprintf(buf, PAGE_SIZE, "none\n");
  260. }
  261. static ssize_t mode_store(struct device *device, struct device_attribute *attr,
  262. const char *buf, size_t count)
  263. {
  264. struct cxl_afu *afu = to_cxl_afu(device);
  265. int old_mode, mode = -1;
  266. int rc = -EBUSY;
  267. /* can't change this if we have a user */
  268. mutex_lock(&afu->contexts_lock);
  269. if (!idr_is_empty(&afu->contexts_idr))
  270. goto err;
  271. if (!strncmp(buf, "dedicated_process", 17))
  272. mode = CXL_MODE_DEDICATED;
  273. if (!strncmp(buf, "afu_directed", 12))
  274. mode = CXL_MODE_DIRECTED;
  275. if (!strncmp(buf, "none", 4))
  276. mode = 0;
  277. if (mode == -1) {
  278. rc = -EINVAL;
  279. goto err;
  280. }
  281. /*
  282. * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
  283. * other contexts coming in before we are ready:
  284. */
  285. old_mode = afu->current_mode;
  286. afu->current_mode = 0;
  287. afu->num_procs = 0;
  288. mutex_unlock(&afu->contexts_lock);
  289. if ((rc = _cxl_afu_deactivate_mode(afu, old_mode)))
  290. return rc;
  291. if ((rc = cxl_afu_activate_mode(afu, mode)))
  292. return rc;
  293. return count;
  294. err:
  295. mutex_unlock(&afu->contexts_lock);
  296. return rc;
  297. }
  298. static ssize_t api_version_show(struct device *device,
  299. struct device_attribute *attr,
  300. char *buf)
  301. {
  302. return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
  303. }
  304. static ssize_t api_version_compatible_show(struct device *device,
  305. struct device_attribute *attr,
  306. char *buf)
  307. {
  308. return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
  309. }
  310. static ssize_t afu_eb_read(struct file *filp, struct kobject *kobj,
  311. struct bin_attribute *bin_attr, char *buf,
  312. loff_t off, size_t count)
  313. {
  314. struct cxl_afu *afu = to_cxl_afu(container_of(kobj,
  315. struct device, kobj));
  316. return cxl_afu_read_err_buffer(afu, buf, off, count);
  317. }
  318. static struct device_attribute afu_attrs[] = {
  319. __ATTR_RO(mmio_size),
  320. __ATTR_RO(irqs_min),
  321. __ATTR_RW(irqs_max),
  322. __ATTR_RO(modes_supported),
  323. __ATTR_RW(mode),
  324. __ATTR_RW(prefault_mode),
  325. __ATTR_RO(api_version),
  326. __ATTR_RO(api_version_compatible),
  327. __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
  328. };
  329. int cxl_sysfs_adapter_add(struct cxl *adapter)
  330. {
  331. int i, rc;
  332. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
  333. if ((rc = device_create_file(&adapter->dev, &adapter_attrs[i])))
  334. goto err;
  335. }
  336. return 0;
  337. err:
  338. for (i--; i >= 0; i--)
  339. device_remove_file(&adapter->dev, &adapter_attrs[i]);
  340. return rc;
  341. }
  342. void cxl_sysfs_adapter_remove(struct cxl *adapter)
  343. {
  344. int i;
  345. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++)
  346. device_remove_file(&adapter->dev, &adapter_attrs[i]);
  347. }
  348. struct afu_config_record {
  349. struct kobject kobj;
  350. struct bin_attribute config_attr;
  351. struct list_head list;
  352. int cr;
  353. u16 device;
  354. u16 vendor;
  355. u32 class;
  356. };
  357. #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
  358. static ssize_t vendor_show(struct kobject *kobj,
  359. struct kobj_attribute *attr, char *buf)
  360. {
  361. struct afu_config_record *cr = to_cr(kobj);
  362. return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
  363. }
  364. static ssize_t device_show(struct kobject *kobj,
  365. struct kobj_attribute *attr, char *buf)
  366. {
  367. struct afu_config_record *cr = to_cr(kobj);
  368. return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
  369. }
  370. static ssize_t class_show(struct kobject *kobj,
  371. struct kobj_attribute *attr, char *buf)
  372. {
  373. struct afu_config_record *cr = to_cr(kobj);
  374. return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
  375. }
  376. static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
  377. struct bin_attribute *bin_attr, char *buf,
  378. loff_t off, size_t count)
  379. {
  380. struct afu_config_record *cr = to_cr(kobj);
  381. struct cxl_afu *afu = to_cxl_afu(container_of(kobj->parent, struct device, kobj));
  382. u64 i, j, val;
  383. for (i = 0; i < count;) {
  384. val = cxl_afu_cr_read64(afu, cr->cr, off & ~0x7);
  385. for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
  386. buf[i] = (val >> (j * 8)) & 0xff;
  387. }
  388. return count;
  389. }
  390. static struct kobj_attribute vendor_attribute =
  391. __ATTR_RO(vendor);
  392. static struct kobj_attribute device_attribute =
  393. __ATTR_RO(device);
  394. static struct kobj_attribute class_attribute =
  395. __ATTR_RO(class);
  396. static struct attribute *afu_cr_attrs[] = {
  397. &vendor_attribute.attr,
  398. &device_attribute.attr,
  399. &class_attribute.attr,
  400. NULL,
  401. };
  402. static void release_afu_config_record(struct kobject *kobj)
  403. {
  404. struct afu_config_record *cr = to_cr(kobj);
  405. kfree(cr);
  406. }
  407. static struct kobj_type afu_config_record_type = {
  408. .sysfs_ops = &kobj_sysfs_ops,
  409. .release = release_afu_config_record,
  410. .default_attrs = afu_cr_attrs,
  411. };
  412. static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
  413. {
  414. struct afu_config_record *cr;
  415. int rc;
  416. cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
  417. if (!cr)
  418. return ERR_PTR(-ENOMEM);
  419. cr->cr = cr_idx;
  420. cr->device = cxl_afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID);
  421. cr->vendor = cxl_afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID);
  422. cr->class = cxl_afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION) >> 8;
  423. /*
  424. * Export raw AFU PCIe like config record. For now this is read only by
  425. * root - we can expand that later to be readable by non-root and maybe
  426. * even writable provided we have a good use-case. Once we suport
  427. * exposing AFUs through a virtual PHB they will get that for free from
  428. * Linux' PCI infrastructure, but until then it's not clear that we
  429. * need it for anything since the main use case is just identifying
  430. * AFUs, which can be done via the vendor, device and class attributes.
  431. */
  432. sysfs_bin_attr_init(&cr->config_attr);
  433. cr->config_attr.attr.name = "config";
  434. cr->config_attr.attr.mode = S_IRUSR;
  435. cr->config_attr.size = afu->crs_len;
  436. cr->config_attr.read = afu_read_config;
  437. rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
  438. &afu->dev.kobj, "cr%i", cr->cr);
  439. if (rc)
  440. goto err;
  441. rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
  442. if (rc)
  443. goto err1;
  444. rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
  445. if (rc)
  446. goto err2;
  447. return cr;
  448. err2:
  449. sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
  450. err1:
  451. kobject_put(&cr->kobj);
  452. return ERR_PTR(rc);
  453. err:
  454. kfree(cr);
  455. return ERR_PTR(rc);
  456. }
  457. void cxl_sysfs_afu_remove(struct cxl_afu *afu)
  458. {
  459. struct afu_config_record *cr, *tmp;
  460. int i;
  461. /* remove the err buffer bin attribute */
  462. if (afu->eb_len)
  463. device_remove_bin_file(&afu->dev, &afu->attr_eb);
  464. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
  465. device_remove_file(&afu->dev, &afu_attrs[i]);
  466. list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
  467. sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
  468. kobject_put(&cr->kobj);
  469. }
  470. }
  471. int cxl_sysfs_afu_add(struct cxl_afu *afu)
  472. {
  473. struct afu_config_record *cr;
  474. int i, rc;
  475. INIT_LIST_HEAD(&afu->crs);
  476. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  477. if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
  478. goto err;
  479. }
  480. /* conditionally create the add the binary file for error info buffer */
  481. if (afu->eb_len) {
  482. sysfs_attr_init(&afu->attr_eb.attr);
  483. afu->attr_eb.attr.name = "afu_err_buff";
  484. afu->attr_eb.attr.mode = S_IRUGO;
  485. afu->attr_eb.size = afu->eb_len;
  486. afu->attr_eb.read = afu_eb_read;
  487. rc = device_create_bin_file(&afu->dev, &afu->attr_eb);
  488. if (rc) {
  489. dev_err(&afu->dev,
  490. "Unable to create eb attr for the afu. Err(%d)\n",
  491. rc);
  492. goto err;
  493. }
  494. }
  495. for (i = 0; i < afu->crs_num; i++) {
  496. cr = cxl_sysfs_afu_new_cr(afu, i);
  497. if (IS_ERR(cr)) {
  498. rc = PTR_ERR(cr);
  499. goto err1;
  500. }
  501. list_add(&cr->list, &afu->crs);
  502. }
  503. return 0;
  504. err1:
  505. cxl_sysfs_afu_remove(afu);
  506. return rc;
  507. err:
  508. /* reset the eb_len as we havent created the bin attr */
  509. afu->eb_len = 0;
  510. for (i--; i >= 0; i--)
  511. device_remove_file(&afu->dev, &afu_attrs[i]);
  512. return rc;
  513. }
  514. int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
  515. {
  516. int i, rc;
  517. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
  518. if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
  519. goto err;
  520. }
  521. return 0;
  522. err:
  523. for (i--; i >= 0; i--)
  524. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  525. return rc;
  526. }
  527. void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
  528. {
  529. int i;
  530. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++)
  531. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  532. }