vio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. vdev->rx_ino = *irq;
  150. }
  151. chan_id = mdesc_get_property(hp, target, "id", NULL);
  152. if (chan_id)
  153. vdev->channel_id = *chan_id;
  154. }
  155. }
  156. int vio_set_intr(unsigned long dev_ino, int state)
  157. {
  158. int err;
  159. err = sun4v_vintr_set_valid(cdev_cfg_handle, dev_ino, state);
  160. return err;
  161. }
  162. EXPORT_SYMBOL(vio_set_intr);
  163. static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
  164. struct device *parent)
  165. {
  166. const char *type, *compat, *bus_id_name;
  167. struct device_node *dp;
  168. struct vio_dev *vdev;
  169. int err, tlen, clen;
  170. const u64 *id, *cfg_handle;
  171. u64 a;
  172. type = mdesc_get_property(hp, mp, "device-type", &tlen);
  173. if (!type) {
  174. type = mdesc_get_property(hp, mp, "name", &tlen);
  175. if (!type) {
  176. type = mdesc_node_name(hp, mp);
  177. tlen = strlen(type) + 1;
  178. }
  179. }
  180. if (tlen > VIO_MAX_TYPE_LEN) {
  181. printk(KERN_ERR "VIO: Type string [%s] is too long.\n",
  182. type);
  183. return NULL;
  184. }
  185. id = mdesc_get_property(hp, mp, "id", NULL);
  186. cfg_handle = NULL;
  187. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_BACK) {
  188. u64 target;
  189. target = mdesc_arc_target(hp, a);
  190. cfg_handle = mdesc_get_property(hp, target,
  191. "cfg-handle", NULL);
  192. if (cfg_handle)
  193. break;
  194. }
  195. bus_id_name = type;
  196. if (!strcmp(type, "domain-services-port"))
  197. bus_id_name = "ds";
  198. /*
  199. * 20 char is the old driver-core name size limit, which is no more.
  200. * This check can probably be removed after review and possible
  201. * adaption of the vio users name length handling.
  202. */
  203. if (strlen(bus_id_name) >= 20 - 4) {
  204. printk(KERN_ERR "VIO: bus_id_name [%s] is too long.\n",
  205. bus_id_name);
  206. return NULL;
  207. }
  208. compat = mdesc_get_property(hp, mp, "device-type", &clen);
  209. if (!compat) {
  210. clen = 0;
  211. } else if (clen > VIO_MAX_COMPAT_LEN) {
  212. printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n",
  213. clen, type);
  214. return NULL;
  215. }
  216. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  217. if (!vdev) {
  218. printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
  219. return NULL;
  220. }
  221. vdev->mp = mp;
  222. memcpy(vdev->type, type, tlen);
  223. if (compat)
  224. memcpy(vdev->compat, compat, clen);
  225. else
  226. memset(vdev->compat, 0, sizeof(vdev->compat));
  227. vdev->compat_len = clen;
  228. vdev->channel_id = ~0UL;
  229. vdev->tx_irq = ~0;
  230. vdev->rx_irq = ~0;
  231. vio_fill_channel_info(hp, mp, vdev);
  232. if (!id) {
  233. dev_set_name(&vdev->dev, "%s", bus_id_name);
  234. vdev->dev_no = ~(u64)0;
  235. vdev->id = ~(u64)0;
  236. } else if (!cfg_handle) {
  237. dev_set_name(&vdev->dev, "%s-%llu", bus_id_name, *id);
  238. vdev->dev_no = *id;
  239. vdev->id = ~(u64)0;
  240. } else {
  241. dev_set_name(&vdev->dev, "%s-%llu-%llu", bus_id_name,
  242. *cfg_handle, *id);
  243. vdev->dev_no = *cfg_handle;
  244. vdev->id = *id;
  245. }
  246. vdev->dev.parent = parent;
  247. vdev->dev.bus = &vio_bus_type;
  248. vdev->dev.release = vio_dev_release;
  249. if (parent == NULL) {
  250. dp = cdev_node;
  251. } else if (to_vio_dev(parent) == root_vdev) {
  252. dp = of_get_next_child(cdev_node, NULL);
  253. while (dp) {
  254. if (!strcmp(dp->type, type))
  255. break;
  256. dp = of_get_next_child(cdev_node, dp);
  257. }
  258. } else {
  259. dp = to_vio_dev(parent)->dp;
  260. }
  261. vdev->dp = dp;
  262. printk(KERN_INFO "VIO: Adding device %s\n", dev_name(&vdev->dev));
  263. err = device_register(&vdev->dev);
  264. if (err) {
  265. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  266. dev_name(&vdev->dev), err);
  267. kfree(vdev);
  268. return NULL;
  269. }
  270. if (vdev->dp)
  271. err = sysfs_create_file(&vdev->dev.kobj,
  272. &dev_attr_obppath.attr);
  273. return vdev;
  274. }
  275. static void vio_add(struct mdesc_handle *hp, u64 node)
  276. {
  277. (void) vio_create_one(hp, node, &root_vdev->dev);
  278. }
  279. struct vio_md_node_query {
  280. const char *type;
  281. u64 dev_no;
  282. u64 id;
  283. };
  284. static int vio_md_node_match(struct device *dev, void *arg)
  285. {
  286. struct vio_md_node_query *query = (struct vio_md_node_query *) arg;
  287. struct vio_dev *vdev = to_vio_dev(dev);
  288. if (vdev->dev_no != query->dev_no)
  289. return 0;
  290. if (vdev->id != query->id)
  291. return 0;
  292. if (strcmp(vdev->type, query->type))
  293. return 0;
  294. return 1;
  295. }
  296. static void vio_remove(struct mdesc_handle *hp, u64 node)
  297. {
  298. const char *type;
  299. const u64 *id, *cfg_handle;
  300. u64 a;
  301. struct vio_md_node_query query;
  302. struct device *dev;
  303. type = mdesc_get_property(hp, node, "device-type", NULL);
  304. if (!type) {
  305. type = mdesc_get_property(hp, node, "name", NULL);
  306. if (!type)
  307. type = mdesc_node_name(hp, node);
  308. }
  309. query.type = type;
  310. id = mdesc_get_property(hp, node, "id", NULL);
  311. cfg_handle = NULL;
  312. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  313. u64 target;
  314. target = mdesc_arc_target(hp, a);
  315. cfg_handle = mdesc_get_property(hp, target,
  316. "cfg-handle", NULL);
  317. if (cfg_handle)
  318. break;
  319. }
  320. if (!id) {
  321. query.dev_no = ~(u64)0;
  322. query.id = ~(u64)0;
  323. } else if (!cfg_handle) {
  324. query.dev_no = *id;
  325. query.id = ~(u64)0;
  326. } else {
  327. query.dev_no = *cfg_handle;
  328. query.id = *id;
  329. }
  330. dev = device_find_child(&root_vdev->dev, &query,
  331. vio_md_node_match);
  332. if (dev) {
  333. printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev));
  334. device_unregister(dev);
  335. put_device(dev);
  336. } else {
  337. if (!id)
  338. printk(KERN_ERR "VIO: Removed unknown %s node.\n",
  339. type);
  340. else if (!cfg_handle)
  341. printk(KERN_ERR "VIO: Removed unknown %s node %llu.\n",
  342. type, *id);
  343. else
  344. printk(KERN_ERR "VIO: Removed unknown %s node %llu-%llu.\n",
  345. type, *cfg_handle, *id);
  346. }
  347. }
  348. static struct mdesc_notifier_client vio_device_notifier = {
  349. .add = vio_add,
  350. .remove = vio_remove,
  351. .node_name = "virtual-device-port",
  352. };
  353. /* We are only interested in domain service ports under the
  354. * "domain-services" node. On control nodes there is another port
  355. * under "openboot" that we should not mess with as aparently that is
  356. * reserved exclusively for OBP use.
  357. */
  358. static void vio_add_ds(struct mdesc_handle *hp, u64 node)
  359. {
  360. int found;
  361. u64 a;
  362. found = 0;
  363. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  364. u64 target = mdesc_arc_target(hp, a);
  365. const char *name = mdesc_node_name(hp, target);
  366. if (!strcmp(name, "domain-services")) {
  367. found = 1;
  368. break;
  369. }
  370. }
  371. if (found)
  372. (void) vio_create_one(hp, node, &root_vdev->dev);
  373. }
  374. static struct mdesc_notifier_client vio_ds_notifier = {
  375. .add = vio_add_ds,
  376. .remove = vio_remove,
  377. .node_name = "domain-services-port",
  378. };
  379. static const char *channel_devices_node = "channel-devices";
  380. static const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  381. static const char *cfg_handle_prop = "cfg-handle";
  382. static int __init vio_init(void)
  383. {
  384. struct mdesc_handle *hp;
  385. const char *compat;
  386. const u64 *cfg_handle;
  387. int err, len;
  388. u64 root;
  389. err = bus_register(&vio_bus_type);
  390. if (err) {
  391. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  392. err);
  393. return err;
  394. }
  395. hp = mdesc_grab();
  396. if (!hp)
  397. return 0;
  398. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  399. if (root == MDESC_NODE_NULL) {
  400. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  401. mdesc_release(hp);
  402. return 0;
  403. }
  404. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  405. err = -ENODEV;
  406. if (!cdev_node) {
  407. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  408. goto out_release;
  409. }
  410. compat = mdesc_get_property(hp, root, "compatible", &len);
  411. if (!compat) {
  412. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  413. "property\n");
  414. goto out_release;
  415. }
  416. if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
  417. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  418. "compat entry.\n", channel_devices_compat);
  419. goto out_release;
  420. }
  421. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  422. if (!cfg_handle) {
  423. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  424. cfg_handle_prop);
  425. goto out_release;
  426. }
  427. cdev_cfg_handle = *cfg_handle;
  428. root_vdev = vio_create_one(hp, root, NULL);
  429. err = -ENODEV;
  430. if (!root_vdev) {
  431. printk(KERN_ERR "VIO: Could not create root device.\n");
  432. goto out_release;
  433. }
  434. mdesc_register_notifier(&vio_device_notifier);
  435. mdesc_register_notifier(&vio_ds_notifier);
  436. mdesc_release(hp);
  437. return err;
  438. out_release:
  439. mdesc_release(hp);
  440. return err;
  441. }
  442. postcore_initcall(vio_init);