vio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* vio.c: Virtual I/O channel devices probing infrastructure.
  2. *
  3. * Copyright (c) 2003-2005 IBM Corp.
  4. * Dave Engebretsen engebret@us.ibm.com
  5. * Santiago Leon santil@us.ibm.com
  6. * Hollis Blanchard <hollisb@us.ibm.com>
  7. * Stephen Rothwell
  8. *
  9. * Adapted to sparc64 by David S. Miller davem@davemloft.net
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/irq.h>
  13. #include <linux/init.h>
  14. #include <asm/mdesc.h>
  15. #include <asm/vio.h>
  16. static inline int find_in_proplist(const char *list, const char *match,
  17. int len)
  18. {
  19. while (len > 0) {
  20. int l;
  21. if (!strcmp(list, match))
  22. return 1;
  23. l = strlen(list) + 1;
  24. list += l;
  25. len -= l;
  26. }
  27. return 0;
  28. }
  29. static const struct vio_device_id *vio_match_device(
  30. const struct vio_device_id *matches,
  31. const struct vio_dev *dev)
  32. {
  33. const char *type, *compat;
  34. int len;
  35. type = dev->type;
  36. compat = dev->compat;
  37. len = dev->compat_len;
  38. while (matches->type[0] || matches->compat[0]) {
  39. int match = 1;
  40. if (matches->type[0]) {
  41. match &= type
  42. && !strcmp(matches->type, type);
  43. }
  44. if (matches->compat[0]) {
  45. match &= compat &&
  46. find_in_proplist(compat, matches->compat, len);
  47. }
  48. if (match)
  49. return matches;
  50. matches++;
  51. }
  52. return NULL;
  53. }
  54. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  55. {
  56. struct vio_dev *vio_dev = to_vio_dev(dev);
  57. struct vio_driver *vio_drv = to_vio_driver(drv);
  58. const struct vio_device_id *matches = vio_drv->id_table;
  59. if (!matches)
  60. return 0;
  61. return vio_match_device(matches, vio_dev) != NULL;
  62. }
  63. static int vio_device_probe(struct device *dev)
  64. {
  65. struct vio_dev *vdev = to_vio_dev(dev);
  66. struct vio_driver *drv = to_vio_driver(dev->driver);
  67. const struct vio_device_id *id;
  68. int error = -ENODEV;
  69. if (drv->probe) {
  70. id = vio_match_device(drv->id_table, vdev);
  71. if (id)
  72. error = drv->probe(vdev, id);
  73. }
  74. return error;
  75. }
  76. static int vio_device_remove(struct device *dev)
  77. {
  78. struct vio_dev *vdev = to_vio_dev(dev);
  79. struct vio_driver *drv = to_vio_driver(dev->driver);
  80. if (drv->remove)
  81. return drv->remove(vdev);
  82. return 1;
  83. }
  84. static ssize_t devspec_show(struct device *dev,
  85. struct device_attribute *attr, char *buf)
  86. {
  87. struct vio_dev *vdev = to_vio_dev(dev);
  88. const char *str = "none";
  89. if (vdev->type) {
  90. if (!strcmp(vdev->type, "network"))
  91. str = "vnet";
  92. else if (!strcmp(vdev->type, "block"))
  93. str = "vdisk";
  94. }
  95. return sprintf(buf, "%s\n", str);
  96. }
  97. static struct device_attribute vio_dev_attrs[] = {
  98. __ATTR_RO(devspec),
  99. __ATTR_NULL
  100. };
  101. static struct bus_type vio_bus_type = {
  102. .name = "vio",
  103. .dev_attrs = vio_dev_attrs,
  104. .match = vio_bus_match,
  105. .probe = vio_device_probe,
  106. .remove = vio_device_remove,
  107. };
  108. int vio_register_driver(struct vio_driver *viodrv)
  109. {
  110. viodrv->driver.bus = &vio_bus_type;
  111. return driver_register(&viodrv->driver);
  112. }
  113. EXPORT_SYMBOL(vio_register_driver);
  114. void vio_unregister_driver(struct vio_driver *viodrv)
  115. {
  116. driver_unregister(&viodrv->driver);
  117. }
  118. EXPORT_SYMBOL(vio_unregister_driver);
  119. struct mdesc_node *vio_find_endpoint(struct vio_dev *vdev)
  120. {
  121. struct mdesc_node *endp, *mp = vdev->mp;
  122. int i;
  123. endp = NULL;
  124. for (i = 0; i < mp->num_arcs; i++) {
  125. struct mdesc_node *t;
  126. if (strcmp(mp->arcs[i].name, "fwd"))
  127. continue;
  128. t = mp->arcs[i].arc;
  129. if (strcmp(t->name, "channel-endpoint"))
  130. continue;
  131. endp = t;
  132. break;
  133. }
  134. return endp;
  135. }
  136. EXPORT_SYMBOL(vio_find_endpoint);
  137. static void __devinit vio_dev_release(struct device *dev)
  138. {
  139. kfree(to_vio_dev(dev));
  140. }
  141. static ssize_t
  142. show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
  143. char *buf)
  144. {
  145. struct vio_dev *vdev;
  146. struct device_node *dp;
  147. vdev = to_vio_dev(dev);
  148. dp = vdev->dp;
  149. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  150. }
  151. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
  152. show_pciobppath_attr, NULL);
  153. struct device_node *cdev_node;
  154. static struct vio_dev *root_vdev;
  155. static u64 cdev_cfg_handle;
  156. static struct vio_dev *vio_create_one(struct mdesc_node *mp,
  157. struct device *parent)
  158. {
  159. const char *type, *compat;
  160. struct device_node *dp;
  161. struct vio_dev *vdev;
  162. const u64 *irq;
  163. int err, clen;
  164. type = md_get_property(mp, "device-type", NULL);
  165. if (!type)
  166. type = md_get_property(mp, "name", NULL);
  167. compat = md_get_property(mp, "device-type", &clen);
  168. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  169. if (!vdev) {
  170. printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
  171. return NULL;
  172. }
  173. vdev->mp = mp;
  174. vdev->type = type;
  175. vdev->compat = compat;
  176. vdev->compat_len = clen;
  177. irq = md_get_property(mp, "tx-ino", NULL);
  178. if (irq)
  179. mp->irqs[0] = sun4v_build_virq(cdev_cfg_handle, *irq);
  180. irq = md_get_property(mp, "rx-ino", NULL);
  181. if (irq)
  182. mp->irqs[1] = sun4v_build_virq(cdev_cfg_handle, *irq);
  183. snprintf(vdev->dev.bus_id, BUS_ID_SIZE, "%lx", mp->node);
  184. vdev->dev.parent = parent;
  185. vdev->dev.bus = &vio_bus_type;
  186. vdev->dev.release = vio_dev_release;
  187. if (parent == NULL) {
  188. dp = cdev_node;
  189. } else if (to_vio_dev(parent) == root_vdev) {
  190. dp = of_get_next_child(cdev_node, NULL);
  191. while (dp) {
  192. if (!strcmp(dp->type, type))
  193. break;
  194. dp = of_get_next_child(cdev_node, dp);
  195. }
  196. } else {
  197. dp = to_vio_dev(parent)->dp;
  198. }
  199. vdev->dp = dp;
  200. err = device_register(&vdev->dev);
  201. if (err) {
  202. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  203. vdev->dev.bus_id, err);
  204. kfree(vdev);
  205. return NULL;
  206. }
  207. if (vdev->dp)
  208. err = sysfs_create_file(&vdev->dev.kobj,
  209. &dev_attr_obppath.attr);
  210. return vdev;
  211. }
  212. static void walk_tree(struct mdesc_node *n, struct vio_dev *parent)
  213. {
  214. int i;
  215. for (i = 0; i < n->num_arcs; i++) {
  216. struct mdesc_node *mp;
  217. struct vio_dev *vdev;
  218. if (strcmp(n->arcs[i].name, "fwd"))
  219. continue;
  220. mp = n->arcs[i].arc;
  221. vdev = vio_create_one(mp, &parent->dev);
  222. if (vdev && mp->num_arcs)
  223. walk_tree(mp, vdev);
  224. }
  225. }
  226. static void create_devices(struct mdesc_node *root)
  227. {
  228. root_vdev = vio_create_one(root, NULL);
  229. if (!root_vdev) {
  230. printk(KERN_ERR "VIO: Coult not create root device.\n");
  231. return;
  232. }
  233. walk_tree(root, root_vdev);
  234. }
  235. const char *channel_devices_node = "channel-devices";
  236. const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  237. const char *cfg_handle_prop = "cfg-handle";
  238. static int __init vio_init(void)
  239. {
  240. struct mdesc_node *root;
  241. const char *compat;
  242. const u64 *cfg_handle;
  243. int err, len;
  244. root = md_find_node_by_name(NULL, channel_devices_node);
  245. if (!root) {
  246. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  247. return 0;
  248. }
  249. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  250. if (!cdev_node) {
  251. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  252. return -ENODEV;
  253. }
  254. compat = md_get_property(root, "compatible", &len);
  255. if (!compat) {
  256. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  257. "property\n");
  258. return -ENODEV;
  259. }
  260. if (!find_in_proplist(compat, channel_devices_compat, len)) {
  261. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  262. "compat entry.\n", channel_devices_compat);
  263. return -ENODEV;
  264. }
  265. cfg_handle = md_get_property(root, cfg_handle_prop, NULL);
  266. if (!cfg_handle) {
  267. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  268. cfg_handle_prop);
  269. return -ENODEV;
  270. }
  271. cdev_cfg_handle = *cfg_handle;
  272. err = bus_register(&vio_bus_type);
  273. if (err) {
  274. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  275. err);
  276. return err;
  277. }
  278. create_devices(root);
  279. return 0;
  280. }
  281. postcore_initcall(vio_init);