sclp_ocf.c 3.8 KB

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