sysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 struct device_attribute afu_attrs[] = {
  290. __ATTR_RO(mmio_size),
  291. __ATTR_RO(irqs_min),
  292. __ATTR_RW(irqs_max),
  293. __ATTR_RO(modes_supported),
  294. __ATTR_RW(mode),
  295. __ATTR_RW(prefault_mode),
  296. __ATTR_RO(api_version),
  297. __ATTR_RO(api_version_compatible),
  298. __ATTR(reset, S_IWUSR, NULL, reset_store_afu),
  299. };
  300. int cxl_sysfs_adapter_add(struct cxl *adapter)
  301. {
  302. int i, rc;
  303. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
  304. if ((rc = device_create_file(&adapter->dev, &adapter_attrs[i])))
  305. goto err;
  306. }
  307. return 0;
  308. err:
  309. for (i--; i >= 0; i--)
  310. device_remove_file(&adapter->dev, &adapter_attrs[i]);
  311. return rc;
  312. }
  313. void cxl_sysfs_adapter_remove(struct cxl *adapter)
  314. {
  315. int i;
  316. for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++)
  317. device_remove_file(&adapter->dev, &adapter_attrs[i]);
  318. }
  319. struct afu_config_record {
  320. struct kobject kobj;
  321. struct bin_attribute config_attr;
  322. struct list_head list;
  323. int cr;
  324. u16 device;
  325. u16 vendor;
  326. u32 class;
  327. };
  328. #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
  329. static ssize_t vendor_show(struct kobject *kobj,
  330. struct kobj_attribute *attr, char *buf)
  331. {
  332. struct afu_config_record *cr = to_cr(kobj);
  333. return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
  334. }
  335. static ssize_t device_show(struct kobject *kobj,
  336. struct kobj_attribute *attr, char *buf)
  337. {
  338. struct afu_config_record *cr = to_cr(kobj);
  339. return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
  340. }
  341. static ssize_t class_show(struct kobject *kobj,
  342. struct kobj_attribute *attr, char *buf)
  343. {
  344. struct afu_config_record *cr = to_cr(kobj);
  345. return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
  346. }
  347. static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
  348. struct bin_attribute *bin_attr, char *buf,
  349. loff_t off, size_t count)
  350. {
  351. struct afu_config_record *cr = to_cr(kobj);
  352. struct cxl_afu *afu = to_cxl_afu(container_of(kobj->parent, struct device, kobj));
  353. u64 i, j, val, size = afu->crs_len;
  354. if (off > size)
  355. return 0;
  356. if (off + count > size)
  357. count = size - off;
  358. for (i = 0; i < count;) {
  359. val = cxl_afu_cr_read64(afu, cr->cr, off & ~0x7);
  360. for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
  361. buf[i] = (val >> (j * 8)) & 0xff;
  362. }
  363. return count;
  364. }
  365. static struct kobj_attribute vendor_attribute =
  366. __ATTR_RO(vendor);
  367. static struct kobj_attribute device_attribute =
  368. __ATTR_RO(device);
  369. static struct kobj_attribute class_attribute =
  370. __ATTR_RO(class);
  371. static struct attribute *afu_cr_attrs[] = {
  372. &vendor_attribute.attr,
  373. &device_attribute.attr,
  374. &class_attribute.attr,
  375. NULL,
  376. };
  377. static void release_afu_config_record(struct kobject *kobj)
  378. {
  379. struct afu_config_record *cr = to_cr(kobj);
  380. kfree(cr);
  381. }
  382. static struct kobj_type afu_config_record_type = {
  383. .sysfs_ops = &kobj_sysfs_ops,
  384. .release = release_afu_config_record,
  385. .default_attrs = afu_cr_attrs,
  386. };
  387. static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
  388. {
  389. struct afu_config_record *cr;
  390. int rc;
  391. cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
  392. if (!cr)
  393. return ERR_PTR(-ENOMEM);
  394. cr->cr = cr_idx;
  395. cr->device = cxl_afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID);
  396. cr->vendor = cxl_afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID);
  397. cr->class = cxl_afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION) >> 8;
  398. /*
  399. * Export raw AFU PCIe like config record. For now this is read only by
  400. * root - we can expand that later to be readable by non-root and maybe
  401. * even writable provided we have a good use-case. Once we suport
  402. * exposing AFUs through a virtual PHB they will get that for free from
  403. * Linux' PCI infrastructure, but until then it's not clear that we
  404. * need it for anything since the main use case is just identifying
  405. * AFUs, which can be done via the vendor, device and class attributes.
  406. */
  407. sysfs_bin_attr_init(&cr->config_attr);
  408. cr->config_attr.attr.name = "config";
  409. cr->config_attr.attr.mode = S_IRUSR;
  410. cr->config_attr.size = afu->crs_len;
  411. cr->config_attr.read = afu_read_config;
  412. rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
  413. &afu->dev.kobj, "cr%i", cr->cr);
  414. if (rc)
  415. goto err;
  416. rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
  417. if (rc)
  418. goto err1;
  419. rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
  420. if (rc)
  421. goto err2;
  422. return cr;
  423. err2:
  424. sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
  425. err1:
  426. kobject_put(&cr->kobj);
  427. return ERR_PTR(rc);
  428. err:
  429. kfree(cr);
  430. return ERR_PTR(rc);
  431. }
  432. void cxl_sysfs_afu_remove(struct cxl_afu *afu)
  433. {
  434. struct afu_config_record *cr, *tmp;
  435. int i;
  436. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++)
  437. device_remove_file(&afu->dev, &afu_attrs[i]);
  438. list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
  439. sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
  440. kobject_put(&cr->kobj);
  441. }
  442. }
  443. int cxl_sysfs_afu_add(struct cxl_afu *afu)
  444. {
  445. struct afu_config_record *cr;
  446. int i, rc;
  447. INIT_LIST_HEAD(&afu->crs);
  448. for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
  449. if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
  450. goto err;
  451. }
  452. for (i = 0; i < afu->crs_num; i++) {
  453. cr = cxl_sysfs_afu_new_cr(afu, i);
  454. if (IS_ERR(cr)) {
  455. rc = PTR_ERR(cr);
  456. goto err1;
  457. }
  458. list_add(&cr->list, &afu->crs);
  459. }
  460. return 0;
  461. err1:
  462. cxl_sysfs_afu_remove(afu);
  463. return rc;
  464. err:
  465. for (i--; i >= 0; i--)
  466. device_remove_file(&afu->dev, &afu_attrs[i]);
  467. return rc;
  468. }
  469. int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
  470. {
  471. int i, rc;
  472. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
  473. if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
  474. goto err;
  475. }
  476. return 0;
  477. err:
  478. for (i--; i >= 0; i--)
  479. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  480. return rc;
  481. }
  482. void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
  483. {
  484. int i;
  485. for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++)
  486. device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
  487. }