vio.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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_add(struct mdesc_handle *hp, u64 node)
  142. {
  143. const char *name = mdesc_get_property(hp, node, "name", NULL);
  144. const u64 *id = mdesc_get_property(hp, node, "id", NULL);
  145. printk(KERN_ERR "VIO: Device add (%s) ID[%lx]\n",
  146. name, *id);
  147. }
  148. static void vio_remove(struct mdesc_handle *hp, u64 node)
  149. {
  150. const char *name = mdesc_get_property(hp, node, "name", NULL);
  151. const u64 *id = mdesc_get_property(hp, node, "id", NULL);
  152. printk(KERN_ERR "VIO: Device remove (%s) ID[%lx]\n",
  153. name, *id);
  154. }
  155. static struct mdesc_notifier_client vio_device_notifier = {
  156. .add = vio_add,
  157. .remove = vio_remove,
  158. .node_name = "virtual-device-port",
  159. };
  160. static struct mdesc_notifier_client vio_ds_notifier = {
  161. .add = vio_add,
  162. .remove = vio_remove,
  163. .node_name = "domain-services-port",
  164. };
  165. static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp,
  166. struct vio_dev *vdev)
  167. {
  168. u64 a;
  169. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  170. const u64 *chan_id;
  171. const u64 *irq;
  172. u64 target;
  173. target = mdesc_arc_target(hp, a);
  174. irq = mdesc_get_property(hp, target, "tx-ino", NULL);
  175. if (irq)
  176. vdev->tx_irq = sun4v_build_virq(cdev_cfg_handle, *irq);
  177. irq = mdesc_get_property(hp, target, "rx-ino", NULL);
  178. if (irq)
  179. vdev->rx_irq = sun4v_build_virq(cdev_cfg_handle, *irq);
  180. chan_id = mdesc_get_property(hp, target, "id", NULL);
  181. if (chan_id)
  182. vdev->channel_id = *chan_id;
  183. }
  184. }
  185. static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
  186. struct device *parent)
  187. {
  188. const char *type, *compat;
  189. struct device_node *dp;
  190. struct vio_dev *vdev;
  191. int err, tlen, clen;
  192. type = mdesc_get_property(hp, mp, "device-type", &tlen);
  193. if (!type) {
  194. type = mdesc_get_property(hp, mp, "name", &tlen);
  195. if (!type) {
  196. type = mdesc_node_name(hp, mp);
  197. tlen = strlen(type) + 1;
  198. }
  199. }
  200. if (tlen > VIO_MAX_TYPE_LEN) {
  201. printk(KERN_ERR "VIO: Type string [%s] is too long.\n",
  202. type);
  203. return NULL;
  204. }
  205. compat = mdesc_get_property(hp, mp, "device-type", &clen);
  206. if (!compat) {
  207. clen = 0;
  208. } else if (clen > VIO_MAX_COMPAT_LEN) {
  209. printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n",
  210. clen, type);
  211. return NULL;
  212. }
  213. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  214. if (!vdev) {
  215. printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
  216. return NULL;
  217. }
  218. vdev->mp = mp;
  219. memcpy(vdev->type, type, tlen);
  220. if (compat)
  221. memcpy(vdev->compat, compat, clen);
  222. else
  223. memset(vdev->compat, 0, sizeof(vdev->compat));
  224. vdev->compat_len = clen;
  225. vdev->channel_id = ~0UL;
  226. vdev->tx_irq = ~0;
  227. vdev->rx_irq = ~0;
  228. vio_fill_channel_info(hp, mp, vdev);
  229. snprintf(vdev->dev.bus_id, BUS_ID_SIZE, "%lx", mp);
  230. vdev->dev.parent = parent;
  231. vdev->dev.bus = &vio_bus_type;
  232. vdev->dev.release = vio_dev_release;
  233. if (parent == NULL) {
  234. dp = cdev_node;
  235. } else if (to_vio_dev(parent) == root_vdev) {
  236. dp = of_get_next_child(cdev_node, NULL);
  237. while (dp) {
  238. if (!strcmp(dp->type, type))
  239. break;
  240. dp = of_get_next_child(cdev_node, dp);
  241. }
  242. } else {
  243. dp = to_vio_dev(parent)->dp;
  244. }
  245. vdev->dp = dp;
  246. err = device_register(&vdev->dev);
  247. if (err) {
  248. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  249. vdev->dev.bus_id, err);
  250. kfree(vdev);
  251. return NULL;
  252. }
  253. if (vdev->dp)
  254. err = sysfs_create_file(&vdev->dev.kobj,
  255. &dev_attr_obppath.attr);
  256. return vdev;
  257. }
  258. static void walk_tree(struct mdesc_handle *hp, u64 n, struct vio_dev *parent)
  259. {
  260. u64 a;
  261. mdesc_for_each_arc(a, hp, n, MDESC_ARC_TYPE_FWD) {
  262. struct vio_dev *vdev;
  263. u64 target;
  264. target = mdesc_arc_target(hp, a);
  265. vdev = vio_create_one(hp, target, &parent->dev);
  266. if (vdev)
  267. walk_tree(hp, target, vdev);
  268. }
  269. }
  270. static void create_devices(struct mdesc_handle *hp, u64 root)
  271. {
  272. u64 mp;
  273. root_vdev = vio_create_one(hp, root, NULL);
  274. if (!root_vdev) {
  275. printk(KERN_ERR "VIO: Coult not create root device.\n");
  276. return;
  277. }
  278. walk_tree(hp, root, root_vdev);
  279. /* Domain services is odd as it doesn't sit underneath the
  280. * channel-devices node, so we plug it in manually.
  281. */
  282. mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "domain-services");
  283. if (mp != MDESC_NODE_NULL) {
  284. struct vio_dev *parent = vio_create_one(hp, mp,
  285. &root_vdev->dev);
  286. if (parent)
  287. walk_tree(hp, mp, parent);
  288. }
  289. }
  290. const char *channel_devices_node = "channel-devices";
  291. const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  292. const char *cfg_handle_prop = "cfg-handle";
  293. static int __init vio_init(void)
  294. {
  295. struct mdesc_handle *hp;
  296. const char *compat;
  297. const u64 *cfg_handle;
  298. int err, len;
  299. u64 root;
  300. err = bus_register(&vio_bus_type);
  301. if (err) {
  302. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  303. err);
  304. return err;
  305. }
  306. hp = mdesc_grab();
  307. if (!hp)
  308. return 0;
  309. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  310. if (root == MDESC_NODE_NULL) {
  311. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  312. mdesc_release(hp);
  313. return 0;
  314. }
  315. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  316. err = -ENODEV;
  317. if (!cdev_node) {
  318. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  319. goto out_release;
  320. }
  321. compat = mdesc_get_property(hp, root, "compatible", &len);
  322. if (!compat) {
  323. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  324. "property\n");
  325. goto out_release;
  326. }
  327. if (!find_in_proplist(compat, channel_devices_compat, len)) {
  328. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  329. "compat entry.\n", channel_devices_compat);
  330. goto out_release;
  331. }
  332. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  333. if (!cfg_handle) {
  334. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  335. cfg_handle_prop);
  336. goto out_release;
  337. }
  338. cdev_cfg_handle = *cfg_handle;
  339. mdesc_register_notifier(&vio_device_notifier);
  340. mdesc_register_notifier(&vio_ds_notifier);
  341. create_devices(hp, root);
  342. mdesc_release(hp);
  343. return 0;
  344. out_release:
  345. mdesc_release(hp);
  346. return err;
  347. }
  348. postcore_initcall(vio_init);