vio.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 (!strcmp(vdev->type, "network"))
  90. str = "vnet";
  91. else if (!strcmp(vdev->type, "block"))
  92. str = "vdisk";
  93. return sprintf(buf, "%s\n", str);
  94. }
  95. static ssize_t type_show(struct device *dev,
  96. struct device_attribute *attr, char *buf)
  97. {
  98. struct vio_dev *vdev = to_vio_dev(dev);
  99. return sprintf(buf, "%s\n", vdev->type);
  100. }
  101. static struct device_attribute vio_dev_attrs[] = {
  102. __ATTR_RO(devspec),
  103. __ATTR_RO(type),
  104. __ATTR_NULL
  105. };
  106. static struct bus_type vio_bus_type = {
  107. .name = "vio",
  108. .dev_attrs = vio_dev_attrs,
  109. .match = vio_bus_match,
  110. .probe = vio_device_probe,
  111. .remove = vio_device_remove,
  112. };
  113. int vio_register_driver(struct vio_driver *viodrv)
  114. {
  115. viodrv->driver.bus = &vio_bus_type;
  116. return driver_register(&viodrv->driver);
  117. }
  118. EXPORT_SYMBOL(vio_register_driver);
  119. void vio_unregister_driver(struct vio_driver *viodrv)
  120. {
  121. driver_unregister(&viodrv->driver);
  122. }
  123. EXPORT_SYMBOL(vio_unregister_driver);
  124. static void __devinit vio_dev_release(struct device *dev)
  125. {
  126. kfree(to_vio_dev(dev));
  127. }
  128. static ssize_t
  129. show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
  130. char *buf)
  131. {
  132. struct vio_dev *vdev;
  133. struct device_node *dp;
  134. vdev = to_vio_dev(dev);
  135. dp = vdev->dp;
  136. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  137. }
  138. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
  139. show_pciobppath_attr, NULL);
  140. struct device_node *cdev_node;
  141. static struct vio_dev *root_vdev;
  142. static u64 cdev_cfg_handle;
  143. static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp,
  144. struct vio_dev *vdev)
  145. {
  146. u64 a;
  147. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  148. const u64 *chan_id;
  149. const u64 *irq;
  150. u64 target;
  151. target = mdesc_arc_target(hp, a);
  152. irq = mdesc_get_property(hp, target, "tx-ino", NULL);
  153. if (irq)
  154. vdev->tx_irq = sun4v_build_virq(cdev_cfg_handle, *irq);
  155. irq = mdesc_get_property(hp, target, "rx-ino", NULL);
  156. if (irq)
  157. vdev->rx_irq = sun4v_build_virq(cdev_cfg_handle, *irq);
  158. chan_id = mdesc_get_property(hp, target, "id", NULL);
  159. if (chan_id)
  160. vdev->channel_id = *chan_id;
  161. }
  162. }
  163. static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
  164. struct device *parent)
  165. {
  166. const char *type, *compat;
  167. struct device_node *dp;
  168. struct vio_dev *vdev;
  169. int err, clen;
  170. type = mdesc_get_property(hp, mp, "device-type", NULL);
  171. if (!type) {
  172. type = mdesc_get_property(hp, mp, "name", NULL);
  173. if (!type)
  174. type = mdesc_node_name(hp, mp);
  175. }
  176. compat = mdesc_get_property(hp, mp, "device-type", &clen);
  177. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  178. if (!vdev) {
  179. printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
  180. return NULL;
  181. }
  182. vdev->mp = mp;
  183. vdev->type = type;
  184. vdev->compat = compat;
  185. vdev->compat_len = clen;
  186. vdev->channel_id = ~0UL;
  187. vdev->tx_irq = ~0;
  188. vdev->rx_irq = ~0;
  189. vio_fill_channel_info(hp, mp, vdev);
  190. snprintf(vdev->dev.bus_id, BUS_ID_SIZE, "%lx", mp);
  191. vdev->dev.parent = parent;
  192. vdev->dev.bus = &vio_bus_type;
  193. vdev->dev.release = vio_dev_release;
  194. if (parent == NULL) {
  195. dp = cdev_node;
  196. } else if (to_vio_dev(parent) == root_vdev) {
  197. dp = of_get_next_child(cdev_node, NULL);
  198. while (dp) {
  199. if (!strcmp(dp->type, type))
  200. break;
  201. dp = of_get_next_child(cdev_node, dp);
  202. }
  203. } else {
  204. dp = to_vio_dev(parent)->dp;
  205. }
  206. vdev->dp = dp;
  207. err = device_register(&vdev->dev);
  208. if (err) {
  209. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  210. vdev->dev.bus_id, err);
  211. kfree(vdev);
  212. return NULL;
  213. }
  214. if (vdev->dp)
  215. err = sysfs_create_file(&vdev->dev.kobj,
  216. &dev_attr_obppath.attr);
  217. return vdev;
  218. }
  219. static void walk_tree(struct mdesc_handle *hp, u64 n, struct vio_dev *parent)
  220. {
  221. u64 a;
  222. mdesc_for_each_arc(a, hp, n, MDESC_ARC_TYPE_FWD) {
  223. struct vio_dev *vdev;
  224. u64 target;
  225. target = mdesc_arc_target(hp, a);
  226. vdev = vio_create_one(hp, target, &parent->dev);
  227. if (vdev)
  228. walk_tree(hp, target, vdev);
  229. }
  230. }
  231. static void create_devices(struct mdesc_handle *hp, u64 root)
  232. {
  233. u64 mp;
  234. root_vdev = vio_create_one(hp, root, NULL);
  235. if (!root_vdev) {
  236. printk(KERN_ERR "VIO: Coult not create root device.\n");
  237. return;
  238. }
  239. walk_tree(hp, root, root_vdev);
  240. /* Domain services is odd as it doesn't sit underneath the
  241. * channel-devices node, so we plug it in manually.
  242. */
  243. mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "domain-services");
  244. if (mp != MDESC_NODE_NULL) {
  245. struct vio_dev *parent = vio_create_one(hp, mp,
  246. &root_vdev->dev);
  247. if (parent)
  248. walk_tree(hp, mp, parent);
  249. }
  250. }
  251. const char *channel_devices_node = "channel-devices";
  252. const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  253. const char *cfg_handle_prop = "cfg-handle";
  254. static int __init vio_init(void)
  255. {
  256. struct mdesc_handle *hp;
  257. const char *compat;
  258. const u64 *cfg_handle;
  259. int err, len;
  260. u64 root;
  261. hp = mdesc_grab();
  262. if (!hp)
  263. return 0;
  264. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  265. if (root == MDESC_NODE_NULL) {
  266. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  267. mdesc_release(hp);
  268. return 0;
  269. }
  270. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  271. err = -ENODEV;
  272. if (!cdev_node) {
  273. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  274. goto out_release;
  275. }
  276. compat = mdesc_get_property(hp, root, "compatible", &len);
  277. if (!compat) {
  278. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  279. "property\n");
  280. goto out_release;
  281. }
  282. if (!find_in_proplist(compat, channel_devices_compat, len)) {
  283. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  284. "compat entry.\n", channel_devices_compat);
  285. goto out_release;
  286. }
  287. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  288. if (!cfg_handle) {
  289. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  290. cfg_handle_prop);
  291. goto out_release;
  292. }
  293. cdev_cfg_handle = *cfg_handle;
  294. err = bus_register(&vio_bus_type);
  295. if (err) {
  296. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  297. err);
  298. return err;
  299. }
  300. create_devices(hp, root);
  301. mdesc_release(hp);
  302. return 0;
  303. out_release:
  304. mdesc_release(hp);
  305. return err;
  306. }
  307. postcore_initcall(vio_init);