scm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Recognize and maintain s390 storage class memory.
  4. *
  5. * Copyright IBM Corp. 2012
  6. * Author(s): Sebastian Ott <[email protected]>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <asm/eadm.h>
  15. #include "chsc.h"
  16. static struct device *scm_root;
  17. #define to_scm_dev(n) container_of(n, struct scm_device, dev)
  18. #define to_scm_drv(d) container_of(d, struct scm_driver, drv)
  19. static int scmdev_probe(struct device *dev)
  20. {
  21. struct scm_device *scmdev = to_scm_dev(dev);
  22. struct scm_driver *scmdrv = to_scm_drv(dev->driver);
  23. return scmdrv->probe ? scmdrv->probe(scmdev) : -ENODEV;
  24. }
  25. static void scmdev_remove(struct device *dev)
  26. {
  27. struct scm_device *scmdev = to_scm_dev(dev);
  28. struct scm_driver *scmdrv = to_scm_drv(dev->driver);
  29. if (scmdrv->remove)
  30. scmdrv->remove(scmdev);
  31. }
  32. static int scmdev_uevent(struct device *dev, struct kobj_uevent_env *env)
  33. {
  34. return add_uevent_var(env, "MODALIAS=scm:scmdev");
  35. }
  36. static struct bus_type scm_bus_type = {
  37. .name = "scm",
  38. .probe = scmdev_probe,
  39. .remove = scmdev_remove,
  40. .uevent = scmdev_uevent,
  41. };
  42. /**
  43. * scm_driver_register() - register a scm driver
  44. * @scmdrv: driver to be registered
  45. */
  46. int scm_driver_register(struct scm_driver *scmdrv)
  47. {
  48. struct device_driver *drv = &scmdrv->drv;
  49. drv->bus = &scm_bus_type;
  50. return driver_register(drv);
  51. }
  52. EXPORT_SYMBOL_GPL(scm_driver_register);
  53. /**
  54. * scm_driver_unregister() - deregister a scm driver
  55. * @scmdrv: driver to be deregistered
  56. */
  57. void scm_driver_unregister(struct scm_driver *scmdrv)
  58. {
  59. driver_unregister(&scmdrv->drv);
  60. }
  61. EXPORT_SYMBOL_GPL(scm_driver_unregister);
  62. void scm_irq_handler(struct aob *aob, blk_status_t error)
  63. {
  64. struct aob_rq_header *aobrq = (void *) aob->request.data;
  65. struct scm_device *scmdev = aobrq->scmdev;
  66. struct scm_driver *scmdrv = to_scm_drv(scmdev->dev.driver);
  67. scmdrv->handler(scmdev, aobrq->data, error);
  68. }
  69. EXPORT_SYMBOL_GPL(scm_irq_handler);
  70. #define scm_attr(name) \
  71. static ssize_t show_##name(struct device *dev, \
  72. struct device_attribute *attr, char *buf) \
  73. { \
  74. struct scm_device *scmdev = to_scm_dev(dev); \
  75. int ret; \
  76. \
  77. device_lock(dev); \
  78. ret = sprintf(buf, "%u\n", scmdev->attrs.name); \
  79. device_unlock(dev); \
  80. \
  81. return ret; \
  82. } \
  83. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  84. scm_attr(persistence);
  85. scm_attr(oper_state);
  86. scm_attr(data_state);
  87. scm_attr(rank);
  88. scm_attr(release);
  89. scm_attr(res_id);
  90. static struct attribute *scmdev_attrs[] = {
  91. &dev_attr_persistence.attr,
  92. &dev_attr_oper_state.attr,
  93. &dev_attr_data_state.attr,
  94. &dev_attr_rank.attr,
  95. &dev_attr_release.attr,
  96. &dev_attr_res_id.attr,
  97. NULL,
  98. };
  99. static struct attribute_group scmdev_attr_group = {
  100. .attrs = scmdev_attrs,
  101. };
  102. static const struct attribute_group *scmdev_attr_groups[] = {
  103. &scmdev_attr_group,
  104. NULL,
  105. };
  106. static void scmdev_release(struct device *dev)
  107. {
  108. struct scm_device *scmdev = to_scm_dev(dev);
  109. kfree(scmdev);
  110. }
  111. static void scmdev_setup(struct scm_device *scmdev, struct sale *sale,
  112. unsigned int size, unsigned int max_blk_count)
  113. {
  114. dev_set_name(&scmdev->dev, "%016llx", (unsigned long long) sale->sa);
  115. scmdev->nr_max_block = max_blk_count;
  116. scmdev->address = sale->sa;
  117. scmdev->size = 1UL << size;
  118. scmdev->attrs.rank = sale->rank;
  119. scmdev->attrs.persistence = sale->p;
  120. scmdev->attrs.oper_state = sale->op_state;
  121. scmdev->attrs.data_state = sale->data_state;
  122. scmdev->attrs.rank = sale->rank;
  123. scmdev->attrs.release = sale->r;
  124. scmdev->attrs.res_id = sale->rid;
  125. scmdev->dev.parent = scm_root;
  126. scmdev->dev.bus = &scm_bus_type;
  127. scmdev->dev.release = scmdev_release;
  128. scmdev->dev.groups = scmdev_attr_groups;
  129. }
  130. /*
  131. * Check for state-changes, notify the driver and userspace.
  132. */
  133. static void scmdev_update(struct scm_device *scmdev, struct sale *sale)
  134. {
  135. struct scm_driver *scmdrv;
  136. bool changed;
  137. device_lock(&scmdev->dev);
  138. changed = scmdev->attrs.rank != sale->rank ||
  139. scmdev->attrs.oper_state != sale->op_state;
  140. scmdev->attrs.rank = sale->rank;
  141. scmdev->attrs.oper_state = sale->op_state;
  142. if (!scmdev->dev.driver)
  143. goto out;
  144. scmdrv = to_scm_drv(scmdev->dev.driver);
  145. if (changed && scmdrv->notify)
  146. scmdrv->notify(scmdev, SCM_CHANGE);
  147. out:
  148. device_unlock(&scmdev->dev);
  149. if (changed)
  150. kobject_uevent(&scmdev->dev.kobj, KOBJ_CHANGE);
  151. }
  152. static int check_address(struct device *dev, const void *data)
  153. {
  154. struct scm_device *scmdev = to_scm_dev(dev);
  155. const struct sale *sale = data;
  156. return scmdev->address == sale->sa;
  157. }
  158. static struct scm_device *scmdev_find(struct sale *sale)
  159. {
  160. struct device *dev;
  161. dev = bus_find_device(&scm_bus_type, NULL, sale, check_address);
  162. return dev ? to_scm_dev(dev) : NULL;
  163. }
  164. static int scm_add(struct chsc_scm_info *scm_info, size_t num)
  165. {
  166. struct sale *sale, *scmal = scm_info->scmal;
  167. struct scm_device *scmdev;
  168. int ret;
  169. for (sale = scmal; sale < scmal + num; sale++) {
  170. scmdev = scmdev_find(sale);
  171. if (scmdev) {
  172. scmdev_update(scmdev, sale);
  173. /* Release reference from scm_find(). */
  174. put_device(&scmdev->dev);
  175. continue;
  176. }
  177. scmdev = kzalloc(sizeof(*scmdev), GFP_KERNEL);
  178. if (!scmdev)
  179. return -ENODEV;
  180. scmdev_setup(scmdev, sale, scm_info->is, scm_info->mbc);
  181. ret = device_register(&scmdev->dev);
  182. if (ret) {
  183. /* Release reference from device_initialize(). */
  184. put_device(&scmdev->dev);
  185. return ret;
  186. }
  187. }
  188. return 0;
  189. }
  190. int scm_update_information(void)
  191. {
  192. struct chsc_scm_info *scm_info;
  193. u64 token = 0;
  194. size_t num;
  195. int ret;
  196. scm_info = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
  197. if (!scm_info)
  198. return -ENOMEM;
  199. do {
  200. ret = chsc_scm_info(scm_info, token);
  201. if (ret)
  202. break;
  203. num = (scm_info->response.length -
  204. (offsetof(struct chsc_scm_info, scmal) -
  205. offsetof(struct chsc_scm_info, response))
  206. ) / sizeof(struct sale);
  207. ret = scm_add(scm_info, num);
  208. if (ret)
  209. break;
  210. token = scm_info->restok;
  211. } while (token);
  212. free_page((unsigned long)scm_info);
  213. return ret;
  214. }
  215. static int scm_dev_avail(struct device *dev, void *unused)
  216. {
  217. struct scm_driver *scmdrv = to_scm_drv(dev->driver);
  218. struct scm_device *scmdev = to_scm_dev(dev);
  219. if (dev->driver && scmdrv->notify)
  220. scmdrv->notify(scmdev, SCM_AVAIL);
  221. return 0;
  222. }
  223. int scm_process_availability_information(void)
  224. {
  225. return bus_for_each_dev(&scm_bus_type, NULL, NULL, scm_dev_avail);
  226. }
  227. static int __init scm_init(void)
  228. {
  229. int ret;
  230. ret = bus_register(&scm_bus_type);
  231. if (ret)
  232. return ret;
  233. scm_root = root_device_register("scm");
  234. if (IS_ERR(scm_root)) {
  235. bus_unregister(&scm_bus_type);
  236. return PTR_ERR(scm_root);
  237. }
  238. scm_update_information();
  239. return 0;
  240. }
  241. subsys_initcall_sync(scm_init);