vio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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, struct module *owner,
  101. const char *mod_name)
  102. {
  103. viodrv->driver.bus = &vio_bus_type;
  104. viodrv->driver.name = viodrv->name;
  105. viodrv->driver.owner = owner;
  106. viodrv->driver.mod_name = mod_name;
  107. return driver_register(&viodrv->driver);
  108. }
  109. EXPORT_SYMBOL(__vio_register_driver);
  110. void vio_unregister_driver(struct vio_driver *viodrv)
  111. {
  112. driver_unregister(&viodrv->driver);
  113. }
  114. EXPORT_SYMBOL(vio_unregister_driver);
  115. static void vio_dev_release(struct device *dev)
  116. {
  117. kfree(to_vio_dev(dev));
  118. }
  119. static ssize_t
  120. show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
  121. char *buf)
  122. {
  123. struct vio_dev *vdev;
  124. struct device_node *dp;
  125. vdev = to_vio_dev(dev);
  126. dp = vdev->dp;
  127. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  128. }
  129. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
  130. show_pciobppath_attr, NULL);
  131. static struct device_node *cdev_node;
  132. static struct vio_dev *root_vdev;
  133. static u64 cdev_cfg_handle;
  134. static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp,
  135. struct vio_dev *vdev)
  136. {
  137. u64 a;
  138. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  139. const u64 *chan_id;
  140. const u64 *irq;
  141. u64 target;
  142. target = mdesc_arc_target(hp, a);
  143. irq = mdesc_get_property(hp, target, "tx-ino", NULL);
  144. if (irq)
  145. vdev->tx_irq = sun4v_build_virq(cdev_cfg_handle, *irq);
  146. irq = mdesc_get_property(hp, target, "rx-ino", NULL);
  147. if (irq)
  148. vdev->rx_irq = sun4v_build_virq(cdev_cfg_handle, *irq);
  149. chan_id = mdesc_get_property(hp, target, "id", NULL);
  150. if (chan_id)
  151. vdev->channel_id = *chan_id;
  152. }
  153. }
  154. static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
  155. struct device *parent)
  156. {
  157. const char *type, *compat, *bus_id_name;
  158. struct device_node *dp;
  159. struct vio_dev *vdev;
  160. int err, tlen, clen;
  161. const u64 *id, *cfg_handle;
  162. u64 a;
  163. type = mdesc_get_property(hp, mp, "device-type", &tlen);
  164. if (!type) {
  165. type = mdesc_get_property(hp, mp, "name", &tlen);
  166. if (!type) {
  167. type = mdesc_node_name(hp, mp);
  168. tlen = strlen(type) + 1;
  169. }
  170. }
  171. if (tlen > VIO_MAX_TYPE_LEN) {
  172. printk(KERN_ERR "VIO: Type string [%s] is too long.\n",
  173. type);
  174. return NULL;
  175. }
  176. id = mdesc_get_property(hp, mp, "id", NULL);
  177. cfg_handle = NULL;
  178. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  179. u64 target;
  180. target = mdesc_arc_target(hp, a);
  181. cfg_handle = mdesc_get_property(hp, target,
  182. "cfg-handle", NULL);
  183. if (cfg_handle)
  184. break;
  185. }
  186. bus_id_name = type;
  187. if (!strcmp(type, "domain-services-port"))
  188. bus_id_name = "ds";
  189. /*
  190. * 20 char is the old driver-core name size limit, which is no more.
  191. * This check can probably be removed after review and possible
  192. * adaption of the vio users name length handling.
  193. */
  194. if (strlen(bus_id_name) >= 20 - 4) {
  195. printk(KERN_ERR "VIO: bus_id_name [%s] is too long.\n",
  196. bus_id_name);
  197. return NULL;
  198. }
  199. compat = mdesc_get_property(hp, mp, "device-type", &clen);
  200. if (!compat) {
  201. clen = 0;
  202. } else if (clen > VIO_MAX_COMPAT_LEN) {
  203. printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n",
  204. clen, type);
  205. return NULL;
  206. }
  207. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  208. if (!vdev) {
  209. printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
  210. return NULL;
  211. }
  212. vdev->mp = mp;
  213. memcpy(vdev->type, type, tlen);
  214. if (compat)
  215. memcpy(vdev->compat, compat, clen);
  216. else
  217. memset(vdev->compat, 0, sizeof(vdev->compat));
  218. vdev->compat_len = clen;
  219. vdev->channel_id = ~0UL;
  220. vdev->tx_irq = ~0;
  221. vdev->rx_irq = ~0;
  222. vio_fill_channel_info(hp, mp, vdev);
  223. if (!id) {
  224. dev_set_name(&vdev->dev, "%s", bus_id_name);
  225. vdev->dev_no = ~(u64)0;
  226. } else if (!cfg_handle) {
  227. dev_set_name(&vdev->dev, "%s-%llu", bus_id_name, *id);
  228. vdev->dev_no = *id;
  229. } else {
  230. dev_set_name(&vdev->dev, "%s-%llu-%llu", bus_id_name,
  231. *cfg_handle, *id);
  232. vdev->dev_no = *cfg_handle;
  233. }
  234. vdev->dev.parent = parent;
  235. vdev->dev.bus = &vio_bus_type;
  236. vdev->dev.release = vio_dev_release;
  237. if (parent == NULL) {
  238. dp = cdev_node;
  239. } else if (to_vio_dev(parent) == root_vdev) {
  240. dp = of_get_next_child(cdev_node, NULL);
  241. while (dp) {
  242. if (!strcmp(dp->type, type))
  243. break;
  244. dp = of_get_next_child(cdev_node, dp);
  245. }
  246. } else {
  247. dp = to_vio_dev(parent)->dp;
  248. }
  249. vdev->dp = dp;
  250. printk(KERN_INFO "VIO: Adding device %s\n", dev_name(&vdev->dev));
  251. err = device_register(&vdev->dev);
  252. if (err) {
  253. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  254. dev_name(&vdev->dev), err);
  255. kfree(vdev);
  256. return NULL;
  257. }
  258. if (vdev->dp)
  259. err = sysfs_create_file(&vdev->dev.kobj,
  260. &dev_attr_obppath.attr);
  261. return vdev;
  262. }
  263. static void vio_add(struct mdesc_handle *hp, u64 node)
  264. {
  265. (void) vio_create_one(hp, node, &root_vdev->dev);
  266. }
  267. static int vio_md_node_match(struct device *dev, void *arg)
  268. {
  269. struct vio_dev *vdev = to_vio_dev(dev);
  270. if (vdev->mp == (u64) arg)
  271. return 1;
  272. return 0;
  273. }
  274. static void vio_remove(struct mdesc_handle *hp, u64 node)
  275. {
  276. struct device *dev;
  277. dev = device_find_child(&root_vdev->dev, (void *) node,
  278. vio_md_node_match);
  279. if (dev) {
  280. printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev));
  281. device_unregister(dev);
  282. }
  283. }
  284. static struct mdesc_notifier_client vio_device_notifier = {
  285. .add = vio_add,
  286. .remove = vio_remove,
  287. .node_name = "virtual-device-port",
  288. };
  289. /* We are only interested in domain service ports under the
  290. * "domain-services" node. On control nodes there is another port
  291. * under "openboot" that we should not mess with as aparently that is
  292. * reserved exclusively for OBP use.
  293. */
  294. static void vio_add_ds(struct mdesc_handle *hp, u64 node)
  295. {
  296. int found;
  297. u64 a;
  298. found = 0;
  299. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  300. u64 target = mdesc_arc_target(hp, a);
  301. const char *name = mdesc_node_name(hp, target);
  302. if (!strcmp(name, "domain-services")) {
  303. found = 1;
  304. break;
  305. }
  306. }
  307. if (found)
  308. (void) vio_create_one(hp, node, &root_vdev->dev);
  309. }
  310. static struct mdesc_notifier_client vio_ds_notifier = {
  311. .add = vio_add_ds,
  312. .remove = vio_remove,
  313. .node_name = "domain-services-port",
  314. };
  315. static const char *channel_devices_node = "channel-devices";
  316. static const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  317. static const char *cfg_handle_prop = "cfg-handle";
  318. static int __init vio_init(void)
  319. {
  320. struct mdesc_handle *hp;
  321. const char *compat;
  322. const u64 *cfg_handle;
  323. int err, len;
  324. u64 root;
  325. err = bus_register(&vio_bus_type);
  326. if (err) {
  327. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  328. err);
  329. return err;
  330. }
  331. hp = mdesc_grab();
  332. if (!hp)
  333. return 0;
  334. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  335. if (root == MDESC_NODE_NULL) {
  336. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  337. mdesc_release(hp);
  338. return 0;
  339. }
  340. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  341. err = -ENODEV;
  342. if (!cdev_node) {
  343. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  344. goto out_release;
  345. }
  346. compat = mdesc_get_property(hp, root, "compatible", &len);
  347. if (!compat) {
  348. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  349. "property\n");
  350. goto out_release;
  351. }
  352. if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
  353. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  354. "compat entry.\n", channel_devices_compat);
  355. goto out_release;
  356. }
  357. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  358. if (!cfg_handle) {
  359. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  360. cfg_handle_prop);
  361. goto out_release;
  362. }
  363. cdev_cfg_handle = *cfg_handle;
  364. root_vdev = vio_create_one(hp, root, NULL);
  365. err = -ENODEV;
  366. if (!root_vdev) {
  367. printk(KERN_ERR "VIO: Coult not create root device.\n");
  368. goto out_release;
  369. }
  370. mdesc_register_notifier(&vio_device_notifier);
  371. mdesc_register_notifier(&vio_ds_notifier);
  372. mdesc_release(hp);
  373. return err;
  374. out_release:
  375. mdesc_release(hp);
  376. return err;
  377. }
  378. postcore_initcall(vio_init);