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