vio.c 9.1 KB

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