vio.c 8.5 KB

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