sclp_ocf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SCLP OCF communication parameters sysfs interface
  4. *
  5. * Copyright IBM Corp. 2011
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "sclp_ocf"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/stat.h>
  13. #include <linux/device.h>
  14. #include <linux/string.h>
  15. #include <linux/ctype.h>
  16. #include <linux/kmod.h>
  17. #include <linux/timer.h>
  18. #include <linux/err.h>
  19. #include <asm/ebcdic.h>
  20. #include <asm/sclp.h>
  21. #include "sclp.h"
  22. #define OCF_LENGTH_HMC_NETWORK 8UL
  23. #define OCF_LENGTH_CPC_NAME 8UL
  24. static char hmc_network[OCF_LENGTH_HMC_NETWORK + 1];
  25. static char cpc_name[OCF_LENGTH_CPC_NAME]; /* in EBCDIC */
  26. static DEFINE_SPINLOCK(sclp_ocf_lock);
  27. static struct work_struct sclp_ocf_change_work;
  28. static struct kset *ocf_kset;
  29. static void sclp_ocf_change_notify(struct work_struct *work)
  30. {
  31. kobject_uevent(&ocf_kset->kobj, KOBJ_CHANGE);
  32. }
  33. /* Handler for OCF event. Look for the CPC image name. */
  34. static void sclp_ocf_handler(struct evbuf_header *evbuf)
  35. {
  36. struct gds_vector *v;
  37. struct gds_subvector *sv, *netid, *cpc;
  38. size_t size;
  39. /* Find the 0x9f00 block. */
  40. v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
  41. 0x9f00);
  42. if (!v)
  43. return;
  44. /* Find the 0x9f22 block inside the 0x9f00 block. */
  45. v = sclp_find_gds_vector(v + 1, (void *) v + v->length, 0x9f22);
  46. if (!v)
  47. return;
  48. /* Find the 0x81 block inside the 0x9f22 block. */
  49. sv = sclp_find_gds_subvector(v + 1, (void *) v + v->length, 0x81);
  50. if (!sv)
  51. return;
  52. /* Find the 0x01 block inside the 0x81 block. */
  53. netid = sclp_find_gds_subvector(sv + 1, (void *) sv + sv->length, 1);
  54. /* Find the 0x02 block inside the 0x81 block. */
  55. cpc = sclp_find_gds_subvector(sv + 1, (void *) sv + sv->length, 2);
  56. /* Copy network name and cpc name. */
  57. spin_lock(&sclp_ocf_lock);
  58. if (netid) {
  59. size = min(OCF_LENGTH_HMC_NETWORK, (size_t) netid->length);
  60. memcpy(hmc_network, netid + 1, size);
  61. EBCASC(hmc_network, size);
  62. hmc_network[size] = 0;
  63. }
  64. if (cpc) {
  65. size = min(OCF_LENGTH_CPC_NAME, (size_t) cpc->length);
  66. memset(cpc_name, 0, OCF_LENGTH_CPC_NAME);
  67. memcpy(cpc_name, cpc + 1, size);
  68. }
  69. spin_unlock(&sclp_ocf_lock);
  70. schedule_work(&sclp_ocf_change_work);
  71. }
  72. static struct sclp_register sclp_ocf_event = {
  73. .receive_mask = EVTYP_OCF_MASK,
  74. .receiver_fn = sclp_ocf_handler,
  75. };
  76. void sclp_ocf_cpc_name_copy(char *dst)
  77. {
  78. spin_lock_irq(&sclp_ocf_lock);
  79. memcpy(dst, cpc_name, OCF_LENGTH_CPC_NAME);
  80. spin_unlock_irq(&sclp_ocf_lock);
  81. }
  82. EXPORT_SYMBOL(sclp_ocf_cpc_name_copy);
  83. static ssize_t cpc_name_show(struct kobject *kobj,
  84. struct kobj_attribute *attr, char *page)
  85. {
  86. char name[OCF_LENGTH_CPC_NAME + 1];
  87. sclp_ocf_cpc_name_copy(name);
  88. name[OCF_LENGTH_CPC_NAME] = 0;
  89. EBCASC(name, OCF_LENGTH_CPC_NAME);
  90. return snprintf(page, PAGE_SIZE, "%s\n", name);
  91. }
  92. static struct kobj_attribute cpc_name_attr =
  93. __ATTR(cpc_name, 0444, cpc_name_show, NULL);
  94. static ssize_t hmc_network_show(struct kobject *kobj,
  95. struct kobj_attribute *attr, char *page)
  96. {
  97. int rc;
  98. spin_lock_irq(&sclp_ocf_lock);
  99. rc = snprintf(page, PAGE_SIZE, "%s\n", hmc_network);
  100. spin_unlock_irq(&sclp_ocf_lock);
  101. return rc;
  102. }
  103. static struct kobj_attribute hmc_network_attr =
  104. __ATTR(hmc_network, 0444, hmc_network_show, NULL);
  105. static struct attribute *ocf_attrs[] = {
  106. &cpc_name_attr.attr,
  107. &hmc_network_attr.attr,
  108. NULL,
  109. };
  110. static const struct attribute_group ocf_attr_group = {
  111. .attrs = ocf_attrs,
  112. };
  113. static int __init ocf_init(void)
  114. {
  115. int rc;
  116. INIT_WORK(&sclp_ocf_change_work, sclp_ocf_change_notify);
  117. ocf_kset = kset_create_and_add("ocf", NULL, firmware_kobj);
  118. if (!ocf_kset)
  119. return -ENOMEM;
  120. rc = sysfs_create_group(&ocf_kset->kobj, &ocf_attr_group);
  121. if (rc) {
  122. kset_unregister(ocf_kset);
  123. return rc;
  124. }
  125. return sclp_register(&sclp_ocf_event);
  126. }
  127. device_initcall(ocf_init);