vio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. vdev->id = ~(u64)0;
  250. } else if (!cfg_handle) {
  251. dev_set_name(&vdev->dev, "%s-%llu", bus_id_name, *id);
  252. vdev->dev_no = *id;
  253. vdev->id = ~(u64)0;
  254. } else {
  255. dev_set_name(&vdev->dev, "%s-%llu-%llu", bus_id_name,
  256. *cfg_handle, *id);
  257. vdev->dev_no = *cfg_handle;
  258. vdev->id = *id;
  259. }
  260. vdev->dev.parent = parent;
  261. vdev->dev.bus = &vio_bus_type;
  262. vdev->dev.release = vio_dev_release;
  263. if (parent == NULL) {
  264. dp = cdev_node;
  265. } else if (to_vio_dev(parent) == root_vdev) {
  266. dp = of_get_next_child(cdev_node, NULL);
  267. while (dp) {
  268. if (!strcmp(dp->type, type))
  269. break;
  270. dp = of_get_next_child(cdev_node, dp);
  271. }
  272. } else {
  273. dp = to_vio_dev(parent)->dp;
  274. }
  275. vdev->dp = dp;
  276. printk(KERN_INFO "VIO: Adding device %s\n", dev_name(&vdev->dev));
  277. err = device_register(&vdev->dev);
  278. if (err) {
  279. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  280. dev_name(&vdev->dev), err);
  281. kfree(vdev);
  282. return NULL;
  283. }
  284. if (vdev->dp)
  285. err = sysfs_create_file(&vdev->dev.kobj,
  286. &dev_attr_obppath.attr);
  287. return vdev;
  288. }
  289. static void vio_add(struct mdesc_handle *hp, u64 node)
  290. {
  291. (void) vio_create_one(hp, node, &root_vdev->dev);
  292. }
  293. struct vio_md_node_query {
  294. const char *type;
  295. u64 dev_no;
  296. u64 id;
  297. };
  298. static int vio_md_node_match(struct device *dev, void *arg)
  299. {
  300. struct vio_md_node_query *query = (struct vio_md_node_query *) arg;
  301. struct vio_dev *vdev = to_vio_dev(dev);
  302. if (vdev->dev_no != query->dev_no)
  303. return 0;
  304. if (vdev->id != query->id)
  305. return 0;
  306. if (strcmp(vdev->type, query->type))
  307. return 0;
  308. return 1;
  309. }
  310. static void vio_remove(struct mdesc_handle *hp, u64 node)
  311. {
  312. const char *type;
  313. const u64 *id, *cfg_handle;
  314. u64 a;
  315. struct vio_md_node_query query;
  316. struct device *dev;
  317. type = mdesc_get_property(hp, node, "device-type", NULL);
  318. if (!type) {
  319. type = mdesc_get_property(hp, node, "name", NULL);
  320. if (!type)
  321. type = mdesc_node_name(hp, node);
  322. }
  323. query.type = type;
  324. id = mdesc_get_property(hp, node, "id", NULL);
  325. cfg_handle = NULL;
  326. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  327. u64 target;
  328. target = mdesc_arc_target(hp, a);
  329. cfg_handle = mdesc_get_property(hp, target,
  330. "cfg-handle", NULL);
  331. if (cfg_handle)
  332. break;
  333. }
  334. if (!id) {
  335. query.dev_no = ~(u64)0;
  336. query.id = ~(u64)0;
  337. } else if (!cfg_handle) {
  338. query.dev_no = *id;
  339. query.id = ~(u64)0;
  340. } else {
  341. query.dev_no = *cfg_handle;
  342. query.id = *id;
  343. }
  344. dev = device_find_child(&root_vdev->dev, &query,
  345. vio_md_node_match);
  346. if (dev) {
  347. printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev));
  348. device_unregister(dev);
  349. put_device(dev);
  350. } else {
  351. if (!id)
  352. printk(KERN_ERR "VIO: Removed unknown %s node.\n",
  353. type);
  354. else if (!cfg_handle)
  355. printk(KERN_ERR "VIO: Removed unknown %s node %llu.\n",
  356. type, *id);
  357. else
  358. printk(KERN_ERR "VIO: Removed unknown %s node %llu-%llu.\n",
  359. type, *cfg_handle, *id);
  360. }
  361. }
  362. static struct mdesc_notifier_client vio_device_notifier = {
  363. .add = vio_add,
  364. .remove = vio_remove,
  365. .node_name = "virtual-device-port",
  366. };
  367. /* We are only interested in domain service ports under the
  368. * "domain-services" node. On control nodes there is another port
  369. * under "openboot" that we should not mess with as aparently that is
  370. * reserved exclusively for OBP use.
  371. */
  372. static void vio_add_ds(struct mdesc_handle *hp, u64 node)
  373. {
  374. int found;
  375. u64 a;
  376. found = 0;
  377. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  378. u64 target = mdesc_arc_target(hp, a);
  379. const char *name = mdesc_node_name(hp, target);
  380. if (!strcmp(name, "domain-services")) {
  381. found = 1;
  382. break;
  383. }
  384. }
  385. if (found)
  386. (void) vio_create_one(hp, node, &root_vdev->dev);
  387. }
  388. static struct mdesc_notifier_client vio_ds_notifier = {
  389. .add = vio_add_ds,
  390. .remove = vio_remove,
  391. .node_name = "domain-services-port",
  392. };
  393. static const char *channel_devices_node = "channel-devices";
  394. static const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  395. static const char *cfg_handle_prop = "cfg-handle";
  396. static int __init vio_init(void)
  397. {
  398. struct mdesc_handle *hp;
  399. const char *compat;
  400. const u64 *cfg_handle;
  401. int err, len;
  402. u64 root;
  403. err = bus_register(&vio_bus_type);
  404. if (err) {
  405. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  406. err);
  407. return err;
  408. }
  409. hp = mdesc_grab();
  410. if (!hp)
  411. return 0;
  412. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  413. if (root == MDESC_NODE_NULL) {
  414. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  415. mdesc_release(hp);
  416. return 0;
  417. }
  418. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  419. err = -ENODEV;
  420. if (!cdev_node) {
  421. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  422. goto out_release;
  423. }
  424. compat = mdesc_get_property(hp, root, "compatible", &len);
  425. if (!compat) {
  426. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  427. "property\n");
  428. goto out_release;
  429. }
  430. if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
  431. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  432. "compat entry.\n", channel_devices_compat);
  433. goto out_release;
  434. }
  435. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  436. if (!cfg_handle) {
  437. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  438. cfg_handle_prop);
  439. goto out_release;
  440. }
  441. cdev_cfg_handle = *cfg_handle;
  442. root_vdev = vio_create_one(hp, root, NULL);
  443. err = -ENODEV;
  444. if (!root_vdev) {
  445. printk(KERN_ERR "VIO: Could not create root device.\n");
  446. goto out_release;
  447. }
  448. mdesc_register_notifier(&vio_device_notifier);
  449. mdesc_register_notifier(&vio_ds_notifier);
  450. mdesc_release(hp);
  451. return err;
  452. out_release:
  453. mdesc_release(hp);
  454. return err;
  455. }
  456. postcore_initcall(vio_init);