vio.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 const struct vio_device_id *vio_match_device(
  17. const struct vio_device_id *matches,
  18. const struct vio_dev *dev)
  19. {
  20. const char *type, *compat;
  21. int len;
  22. type = dev->type;
  23. compat = dev->compat;
  24. len = dev->compat_len;
  25. while (matches->type[0] || matches->compat[0]) {
  26. int match = 1;
  27. if (matches->type[0])
  28. match &= !strcmp(matches->type, type);
  29. if (matches->compat[0]) {
  30. match &= len &&
  31. of_find_in_proplist(compat, matches->compat, len);
  32. }
  33. if (match)
  34. return matches;
  35. matches++;
  36. }
  37. return NULL;
  38. }
  39. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  40. {
  41. struct vio_dev *vio_dev = to_vio_dev(dev);
  42. struct vio_driver *vio_drv = to_vio_driver(drv);
  43. const struct vio_device_id *matches = vio_drv->id_table;
  44. if (!matches)
  45. return 0;
  46. return vio_match_device(matches, vio_dev) != NULL;
  47. }
  48. static int vio_device_probe(struct device *dev)
  49. {
  50. struct vio_dev *vdev = to_vio_dev(dev);
  51. struct vio_driver *drv = to_vio_driver(dev->driver);
  52. const struct vio_device_id *id;
  53. int error = -ENODEV;
  54. if (drv->probe) {
  55. id = vio_match_device(drv->id_table, vdev);
  56. if (id)
  57. error = drv->probe(vdev, id);
  58. }
  59. return error;
  60. }
  61. static int vio_device_remove(struct device *dev)
  62. {
  63. struct vio_dev *vdev = to_vio_dev(dev);
  64. struct vio_driver *drv = to_vio_driver(dev->driver);
  65. if (drv->remove)
  66. return drv->remove(vdev);
  67. return 1;
  68. }
  69. static ssize_t devspec_show(struct device *dev,
  70. struct device_attribute *attr, char *buf)
  71. {
  72. struct vio_dev *vdev = to_vio_dev(dev);
  73. const char *str = "none";
  74. if (!strcmp(vdev->type, "vnet-port"))
  75. str = "vnet";
  76. else if (!strcmp(vdev->type, "vdc-port"))
  77. str = "vdisk";
  78. return sprintf(buf, "%s\n", str);
  79. }
  80. static ssize_t type_show(struct device *dev,
  81. struct device_attribute *attr, char *buf)
  82. {
  83. struct vio_dev *vdev = to_vio_dev(dev);
  84. return sprintf(buf, "%s\n", vdev->type);
  85. }
  86. static struct device_attribute vio_dev_attrs[] = {
  87. __ATTR_RO(devspec),
  88. __ATTR_RO(type),
  89. __ATTR_NULL
  90. };
  91. static struct bus_type vio_bus_type = {
  92. .name = "vio",
  93. .dev_attrs = vio_dev_attrs,
  94. .match = vio_bus_match,
  95. .probe = vio_device_probe,
  96. .remove = vio_device_remove,
  97. };
  98. int vio_register_driver(struct vio_driver *viodrv)
  99. {
  100. viodrv->driver.bus = &vio_bus_type;
  101. return driver_register(&viodrv->driver);
  102. }
  103. EXPORT_SYMBOL(vio_register_driver);
  104. void vio_unregister_driver(struct vio_driver *viodrv)
  105. {
  106. driver_unregister(&viodrv->driver);
  107. }
  108. EXPORT_SYMBOL(vio_unregister_driver);
  109. static void vio_dev_release(struct device *dev)
  110. {
  111. kfree(to_vio_dev(dev));
  112. }
  113. static ssize_t
  114. show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
  115. char *buf)
  116. {
  117. struct vio_dev *vdev;
  118. struct device_node *dp;
  119. vdev = to_vio_dev(dev);
  120. dp = vdev->dp;
  121. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  122. }
  123. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
  124. show_pciobppath_attr, NULL);
  125. static struct device_node *cdev_node;
  126. static struct vio_dev *root_vdev;
  127. static u64 cdev_cfg_handle;
  128. static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp,
  129. struct vio_dev *vdev)
  130. {
  131. u64 a;
  132. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  133. const u64 *chan_id;
  134. const u64 *irq;
  135. u64 target;
  136. target = mdesc_arc_target(hp, a);
  137. irq = mdesc_get_property(hp, target, "tx-ino", NULL);
  138. if (irq)
  139. vdev->tx_irq = sun4v_build_virq(cdev_cfg_handle, *irq);
  140. irq = mdesc_get_property(hp, target, "rx-ino", NULL);
  141. if (irq)
  142. vdev->rx_irq = sun4v_build_virq(cdev_cfg_handle, *irq);
  143. chan_id = mdesc_get_property(hp, target, "id", NULL);
  144. if (chan_id)
  145. vdev->channel_id = *chan_id;
  146. }
  147. }
  148. static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
  149. struct device *parent)
  150. {
  151. const char *type, *compat, *bus_id_name;
  152. struct device_node *dp;
  153. struct vio_dev *vdev;
  154. int err, tlen, clen;
  155. const u64 *id, *cfg_handle;
  156. u64 a;
  157. type = mdesc_get_property(hp, mp, "device-type", &tlen);
  158. if (!type) {
  159. type = mdesc_get_property(hp, mp, "name", &tlen);
  160. if (!type) {
  161. type = mdesc_node_name(hp, mp);
  162. tlen = strlen(type) + 1;
  163. }
  164. }
  165. if (tlen > VIO_MAX_TYPE_LEN) {
  166. printk(KERN_ERR "VIO: Type string [%s] is too long.\n",
  167. type);
  168. return NULL;
  169. }
  170. id = mdesc_get_property(hp, mp, "id", NULL);
  171. cfg_handle = NULL;
  172. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  173. u64 target;
  174. target = mdesc_arc_target(hp, a);
  175. cfg_handle = mdesc_get_property(hp, target,
  176. "cfg-handle", NULL);
  177. if (cfg_handle)
  178. break;
  179. }
  180. bus_id_name = type;
  181. if (!strcmp(type, "domain-services-port"))
  182. bus_id_name = "ds";
  183. if (strlen(bus_id_name) >= BUS_ID_SIZE - 4) {
  184. printk(KERN_ERR "VIO: bus_id_name [%s] is too long.\n",
  185. bus_id_name);
  186. return NULL;
  187. }
  188. compat = mdesc_get_property(hp, mp, "device-type", &clen);
  189. if (!compat) {
  190. clen = 0;
  191. } else if (clen > VIO_MAX_COMPAT_LEN) {
  192. printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n",
  193. clen, type);
  194. return NULL;
  195. }
  196. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  197. if (!vdev) {
  198. printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
  199. return NULL;
  200. }
  201. vdev->mp = mp;
  202. memcpy(vdev->type, type, tlen);
  203. if (compat)
  204. memcpy(vdev->compat, compat, clen);
  205. else
  206. memset(vdev->compat, 0, sizeof(vdev->compat));
  207. vdev->compat_len = clen;
  208. vdev->channel_id = ~0UL;
  209. vdev->tx_irq = ~0;
  210. vdev->rx_irq = ~0;
  211. vio_fill_channel_info(hp, mp, vdev);
  212. if (!id) {
  213. dev_set_name(&vdev->dev, "%s", bus_id_name);
  214. vdev->dev_no = ~(u64)0;
  215. } else if (!cfg_handle) {
  216. dev_set_name(&vdev->dev, "%s-%llu", bus_id_name, *id);
  217. vdev->dev_no = *id;
  218. } else {
  219. dev_set_name(&vdev->dev, "%s-%llu-%llu", bus_id_name,
  220. *cfg_handle, *id);
  221. vdev->dev_no = *cfg_handle;
  222. }
  223. vdev->dev.parent = parent;
  224. vdev->dev.bus = &vio_bus_type;
  225. vdev->dev.release = vio_dev_release;
  226. if (parent == NULL) {
  227. dp = cdev_node;
  228. } else if (to_vio_dev(parent) == root_vdev) {
  229. dp = of_get_next_child(cdev_node, NULL);
  230. while (dp) {
  231. if (!strcmp(dp->type, type))
  232. break;
  233. dp = of_get_next_child(cdev_node, dp);
  234. }
  235. } else {
  236. dp = to_vio_dev(parent)->dp;
  237. }
  238. vdev->dp = dp;
  239. printk(KERN_INFO "VIO: Adding device %s\n", dev_name(&vdev->dev));
  240. err = device_register(&vdev->dev);
  241. if (err) {
  242. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  243. dev_name(&vdev->dev), err);
  244. kfree(vdev);
  245. return NULL;
  246. }
  247. if (vdev->dp)
  248. err = sysfs_create_file(&vdev->dev.kobj,
  249. &dev_attr_obppath.attr);
  250. return vdev;
  251. }
  252. static void vio_add(struct mdesc_handle *hp, u64 node)
  253. {
  254. (void) vio_create_one(hp, node, &root_vdev->dev);
  255. }
  256. static int vio_md_node_match(struct device *dev, void *arg)
  257. {
  258. struct vio_dev *vdev = to_vio_dev(dev);
  259. if (vdev->mp == (u64) arg)
  260. return 1;
  261. return 0;
  262. }
  263. static void vio_remove(struct mdesc_handle *hp, u64 node)
  264. {
  265. struct device *dev;
  266. dev = device_find_child(&root_vdev->dev, (void *) node,
  267. vio_md_node_match);
  268. if (dev) {
  269. printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev));
  270. device_unregister(dev);
  271. }
  272. }
  273. static struct mdesc_notifier_client vio_device_notifier = {
  274. .add = vio_add,
  275. .remove = vio_remove,
  276. .node_name = "virtual-device-port",
  277. };
  278. /* We are only interested in domain service ports under the
  279. * "domain-services" node. On control nodes there is another port
  280. * under "openboot" that we should not mess with as aparently that is
  281. * reserved exclusively for OBP use.
  282. */
  283. static void vio_add_ds(struct mdesc_handle *hp, u64 node)
  284. {
  285. int found;
  286. u64 a;
  287. found = 0;
  288. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  289. u64 target = mdesc_arc_target(hp, a);
  290. const char *name = mdesc_node_name(hp, target);
  291. if (!strcmp(name, "domain-services")) {
  292. found = 1;
  293. break;
  294. }
  295. }
  296. if (found)
  297. (void) vio_create_one(hp, node, &root_vdev->dev);
  298. }
  299. static struct mdesc_notifier_client vio_ds_notifier = {
  300. .add = vio_add_ds,
  301. .remove = vio_remove,
  302. .node_name = "domain-services-port",
  303. };
  304. static const char *channel_devices_node = "channel-devices";
  305. static const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  306. static const char *cfg_handle_prop = "cfg-handle";
  307. static int __init vio_init(void)
  308. {
  309. struct mdesc_handle *hp;
  310. const char *compat;
  311. const u64 *cfg_handle;
  312. int err, len;
  313. u64 root;
  314. err = bus_register(&vio_bus_type);
  315. if (err) {
  316. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  317. err);
  318. return err;
  319. }
  320. hp = mdesc_grab();
  321. if (!hp)
  322. return 0;
  323. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  324. if (root == MDESC_NODE_NULL) {
  325. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  326. mdesc_release(hp);
  327. return 0;
  328. }
  329. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  330. err = -ENODEV;
  331. if (!cdev_node) {
  332. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  333. goto out_release;
  334. }
  335. compat = mdesc_get_property(hp, root, "compatible", &len);
  336. if (!compat) {
  337. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  338. "property\n");
  339. goto out_release;
  340. }
  341. if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
  342. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  343. "compat entry.\n", channel_devices_compat);
  344. goto out_release;
  345. }
  346. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  347. if (!cfg_handle) {
  348. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  349. cfg_handle_prop);
  350. goto out_release;
  351. }
  352. cdev_cfg_handle = *cfg_handle;
  353. root_vdev = vio_create_one(hp, root, NULL);
  354. err = -ENODEV;
  355. if (!root_vdev) {
  356. printk(KERN_ERR "VIO: Coult not create root device.\n");
  357. goto out_release;
  358. }
  359. mdesc_register_notifier(&vio_device_notifier);
  360. mdesc_register_notifier(&vio_ds_notifier);
  361. mdesc_release(hp);
  362. return err;
  363. out_release:
  364. mdesc_release(hp);
  365. return err;
  366. }
  367. postcore_initcall(vio_init);