vio.c 11 KB

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