vio.c 10 KB

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