vio.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 __devinit 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. 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) >= KOBJ_NAME_LEN - 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. snprintf(vdev->dev.bus_id, BUS_ID_SIZE, "%s",
  214. bus_id_name);
  215. vdev->dev_no = ~(u64)0;
  216. } else if (!cfg_handle) {
  217. snprintf(vdev->dev.bus_id, BUS_ID_SIZE, "%s-%lu",
  218. bus_id_name, *id);
  219. vdev->dev_no = *id;
  220. } else {
  221. snprintf(vdev->dev.bus_id, BUS_ID_SIZE, "%s-%lu-%lu",
  222. bus_id_name, *cfg_handle, *id);
  223. vdev->dev_no = *cfg_handle;
  224. }
  225. vdev->dev.parent = parent;
  226. vdev->dev.bus = &vio_bus_type;
  227. vdev->dev.release = vio_dev_release;
  228. if (parent == NULL) {
  229. dp = cdev_node;
  230. } else if (to_vio_dev(parent) == root_vdev) {
  231. dp = of_get_next_child(cdev_node, NULL);
  232. while (dp) {
  233. if (!strcmp(dp->type, type))
  234. break;
  235. dp = of_get_next_child(cdev_node, dp);
  236. }
  237. } else {
  238. dp = to_vio_dev(parent)->dp;
  239. }
  240. vdev->dp = dp;
  241. printk(KERN_INFO "VIO: Adding device %s\n", vdev->dev.bus_id);
  242. err = device_register(&vdev->dev);
  243. if (err) {
  244. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  245. vdev->dev.bus_id, err);
  246. kfree(vdev);
  247. return NULL;
  248. }
  249. if (vdev->dp)
  250. err = sysfs_create_file(&vdev->dev.kobj,
  251. &dev_attr_obppath.attr);
  252. return vdev;
  253. }
  254. static void vio_add(struct mdesc_handle *hp, u64 node)
  255. {
  256. (void) vio_create_one(hp, node, &root_vdev->dev);
  257. }
  258. static int vio_md_node_match(struct device *dev, void *arg)
  259. {
  260. struct vio_dev *vdev = to_vio_dev(dev);
  261. if (vdev->mp == (u64) arg)
  262. return 1;
  263. return 0;
  264. }
  265. static void vio_remove(struct mdesc_handle *hp, u64 node)
  266. {
  267. struct device *dev;
  268. dev = device_find_child(&root_vdev->dev, (void *) node,
  269. vio_md_node_match);
  270. if (dev) {
  271. printk(KERN_INFO "VIO: Removing device %s\n", dev->bus_id);
  272. device_unregister(dev);
  273. }
  274. }
  275. static struct mdesc_notifier_client vio_device_notifier = {
  276. .add = vio_add,
  277. .remove = vio_remove,
  278. .node_name = "virtual-device-port",
  279. };
  280. /* We are only interested in domain service ports under the
  281. * "domain-services" node. On control nodes there is another port
  282. * under "openboot" that we should not mess with as aparently that is
  283. * reserved exclusively for OBP use.
  284. */
  285. static void vio_add_ds(struct mdesc_handle *hp, u64 node)
  286. {
  287. int found;
  288. u64 a;
  289. found = 0;
  290. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  291. u64 target = mdesc_arc_target(hp, a);
  292. const char *name = mdesc_node_name(hp, target);
  293. if (!strcmp(name, "domain-services")) {
  294. found = 1;
  295. break;
  296. }
  297. }
  298. if (found)
  299. (void) vio_create_one(hp, node, &root_vdev->dev);
  300. }
  301. static struct mdesc_notifier_client vio_ds_notifier = {
  302. .add = vio_add_ds,
  303. .remove = vio_remove,
  304. .node_name = "domain-services-port",
  305. };
  306. const char *channel_devices_node = "channel-devices";
  307. const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  308. const char *cfg_handle_prop = "cfg-handle";
  309. static int __init vio_init(void)
  310. {
  311. struct mdesc_handle *hp;
  312. const char *compat;
  313. const u64 *cfg_handle;
  314. int err, len;
  315. u64 root;
  316. err = bus_register(&vio_bus_type);
  317. if (err) {
  318. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  319. err);
  320. return err;
  321. }
  322. hp = mdesc_grab();
  323. if (!hp)
  324. return 0;
  325. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  326. if (root == MDESC_NODE_NULL) {
  327. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  328. mdesc_release(hp);
  329. return 0;
  330. }
  331. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  332. err = -ENODEV;
  333. if (!cdev_node) {
  334. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  335. goto out_release;
  336. }
  337. compat = mdesc_get_property(hp, root, "compatible", &len);
  338. if (!compat) {
  339. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  340. "property\n");
  341. goto out_release;
  342. }
  343. if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
  344. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  345. "compat entry.\n", channel_devices_compat);
  346. goto out_release;
  347. }
  348. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  349. if (!cfg_handle) {
  350. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  351. cfg_handle_prop);
  352. goto out_release;
  353. }
  354. cdev_cfg_handle = *cfg_handle;
  355. root_vdev = vio_create_one(hp, root, NULL);
  356. err = -ENODEV;
  357. if (!root_vdev) {
  358. printk(KERN_ERR "VIO: Coult not create root device.\n");
  359. goto out_release;
  360. }
  361. mdesc_register_notifier(&vio_device_notifier);
  362. mdesc_register_notifier(&vio_ds_notifier);
  363. mdesc_release(hp);
  364. return err;
  365. out_release:
  366. mdesc_release(hp);
  367. return err;
  368. }
  369. postcore_initcall(vio_init);