vio.c 9.6 KB

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