vio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. /*
  184. * 20 char is the old driver-core name size limit, which is no more.
  185. * This check can probably be removed after review and possible
  186. * adaption of the vio users name length handling.
  187. */
  188. if (strlen(bus_id_name) >= 20 - 4) {
  189. printk(KERN_ERR "VIO: bus_id_name [%s] is too long.\n",
  190. bus_id_name);
  191. return NULL;
  192. }
  193. compat = mdesc_get_property(hp, mp, "device-type", &clen);
  194. if (!compat) {
  195. clen = 0;
  196. } else if (clen > VIO_MAX_COMPAT_LEN) {
  197. printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n",
  198. clen, type);
  199. return NULL;
  200. }
  201. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  202. if (!vdev) {
  203. printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
  204. return NULL;
  205. }
  206. vdev->mp = mp;
  207. memcpy(vdev->type, type, tlen);
  208. if (compat)
  209. memcpy(vdev->compat, compat, clen);
  210. else
  211. memset(vdev->compat, 0, sizeof(vdev->compat));
  212. vdev->compat_len = clen;
  213. vdev->channel_id = ~0UL;
  214. vdev->tx_irq = ~0;
  215. vdev->rx_irq = ~0;
  216. vio_fill_channel_info(hp, mp, vdev);
  217. if (!id) {
  218. dev_set_name(&vdev->dev, "%s", bus_id_name);
  219. vdev->dev_no = ~(u64)0;
  220. } else if (!cfg_handle) {
  221. dev_set_name(&vdev->dev, "%s-%llu", bus_id_name, *id);
  222. vdev->dev_no = *id;
  223. } else {
  224. dev_set_name(&vdev->dev, "%s-%llu-%llu", bus_id_name,
  225. *cfg_handle, *id);
  226. vdev->dev_no = *cfg_handle;
  227. }
  228. vdev->dev.parent = parent;
  229. vdev->dev.bus = &vio_bus_type;
  230. vdev->dev.release = vio_dev_release;
  231. if (parent == NULL) {
  232. dp = cdev_node;
  233. } else if (to_vio_dev(parent) == root_vdev) {
  234. dp = of_get_next_child(cdev_node, NULL);
  235. while (dp) {
  236. if (!strcmp(dp->type, type))
  237. break;
  238. dp = of_get_next_child(cdev_node, dp);
  239. }
  240. } else {
  241. dp = to_vio_dev(parent)->dp;
  242. }
  243. vdev->dp = dp;
  244. printk(KERN_INFO "VIO: Adding device %s\n", dev_name(&vdev->dev));
  245. err = device_register(&vdev->dev);
  246. if (err) {
  247. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  248. dev_name(&vdev->dev), err);
  249. kfree(vdev);
  250. return NULL;
  251. }
  252. if (vdev->dp)
  253. err = sysfs_create_file(&vdev->dev.kobj,
  254. &dev_attr_obppath.attr);
  255. return vdev;
  256. }
  257. static void vio_add(struct mdesc_handle *hp, u64 node)
  258. {
  259. (void) vio_create_one(hp, node, &root_vdev->dev);
  260. }
  261. static int vio_md_node_match(struct device *dev, void *arg)
  262. {
  263. struct vio_dev *vdev = to_vio_dev(dev);
  264. if (vdev->mp == (u64) arg)
  265. return 1;
  266. return 0;
  267. }
  268. static void vio_remove(struct mdesc_handle *hp, u64 node)
  269. {
  270. struct device *dev;
  271. dev = device_find_child(&root_vdev->dev, (void *) node,
  272. vio_md_node_match);
  273. if (dev) {
  274. printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev));
  275. device_unregister(dev);
  276. }
  277. }
  278. static struct mdesc_notifier_client vio_device_notifier = {
  279. .add = vio_add,
  280. .remove = vio_remove,
  281. .node_name = "virtual-device-port",
  282. };
  283. /* We are only interested in domain service ports under the
  284. * "domain-services" node. On control nodes there is another port
  285. * under "openboot" that we should not mess with as aparently that is
  286. * reserved exclusively for OBP use.
  287. */
  288. static void vio_add_ds(struct mdesc_handle *hp, u64 node)
  289. {
  290. int found;
  291. u64 a;
  292. found = 0;
  293. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  294. u64 target = mdesc_arc_target(hp, a);
  295. const char *name = mdesc_node_name(hp, target);
  296. if (!strcmp(name, "domain-services")) {
  297. found = 1;
  298. break;
  299. }
  300. }
  301. if (found)
  302. (void) vio_create_one(hp, node, &root_vdev->dev);
  303. }
  304. static struct mdesc_notifier_client vio_ds_notifier = {
  305. .add = vio_add_ds,
  306. .remove = vio_remove,
  307. .node_name = "domain-services-port",
  308. };
  309. static const char *channel_devices_node = "channel-devices";
  310. static const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  311. static const char *cfg_handle_prop = "cfg-handle";
  312. static int __init vio_init(void)
  313. {
  314. struct mdesc_handle *hp;
  315. const char *compat;
  316. const u64 *cfg_handle;
  317. int err, len;
  318. u64 root;
  319. err = bus_register(&vio_bus_type);
  320. if (err) {
  321. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  322. err);
  323. return err;
  324. }
  325. hp = mdesc_grab();
  326. if (!hp)
  327. return 0;
  328. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  329. if (root == MDESC_NODE_NULL) {
  330. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  331. mdesc_release(hp);
  332. return 0;
  333. }
  334. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  335. err = -ENODEV;
  336. if (!cdev_node) {
  337. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  338. goto out_release;
  339. }
  340. compat = mdesc_get_property(hp, root, "compatible", &len);
  341. if (!compat) {
  342. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  343. "property\n");
  344. goto out_release;
  345. }
  346. if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
  347. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  348. "compat entry.\n", channel_devices_compat);
  349. goto out_release;
  350. }
  351. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  352. if (!cfg_handle) {
  353. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  354. cfg_handle_prop);
  355. goto out_release;
  356. }
  357. cdev_cfg_handle = *cfg_handle;
  358. root_vdev = vio_create_one(hp, root, NULL);
  359. err = -ENODEV;
  360. if (!root_vdev) {
  361. printk(KERN_ERR "VIO: Coult not create root device.\n");
  362. goto out_release;
  363. }
  364. mdesc_register_notifier(&vio_device_notifier);
  365. mdesc_register_notifier(&vio_ds_notifier);
  366. mdesc_release(hp);
  367. return err;
  368. out_release:
  369. mdesc_release(hp);
  370. return err;
  371. }
  372. postcore_initcall(vio_init);