vio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* vio.c: Virtual I/O channel devices probing infrastructure.
  3. *
  4. * Copyright (c) 2003-2005 IBM Corp.
  5. * Dave Engebretsen [email protected]
  6. * Santiago Leon [email protected]
  7. * Hollis Blanchard <[email protected]>
  8. * Stephen Rothwell
  9. *
  10. * Adapted to sparc64 by David S. Miller [email protected]
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/irq.h>
  15. #include <linux/export.h>
  16. #include <linux/init.h>
  17. #include <asm/mdesc.h>
  18. #include <asm/vio.h>
  19. static const struct vio_device_id *vio_match_device(
  20. const struct vio_device_id *matches,
  21. const struct vio_dev *dev)
  22. {
  23. const char *type, *compat;
  24. int len;
  25. type = dev->type;
  26. compat = dev->compat;
  27. len = dev->compat_len;
  28. while (matches->type[0] || matches->compat[0]) {
  29. int match = 1;
  30. if (matches->type[0])
  31. match &= !strcmp(matches->type, type);
  32. if (matches->compat[0]) {
  33. match &= len &&
  34. of_find_in_proplist(compat, matches->compat, len);
  35. }
  36. if (match)
  37. return matches;
  38. matches++;
  39. }
  40. return NULL;
  41. }
  42. static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
  43. {
  44. const struct vio_dev *vio_dev = to_vio_dev(dev);
  45. add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, vio_dev->compat);
  46. return 0;
  47. }
  48. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  49. {
  50. struct vio_dev *vio_dev = to_vio_dev(dev);
  51. struct vio_driver *vio_drv = to_vio_driver(drv);
  52. const struct vio_device_id *matches = vio_drv->id_table;
  53. if (!matches)
  54. return 0;
  55. return vio_match_device(matches, vio_dev) != NULL;
  56. }
  57. static int vio_device_probe(struct device *dev)
  58. {
  59. struct vio_dev *vdev = to_vio_dev(dev);
  60. struct vio_driver *drv = to_vio_driver(dev->driver);
  61. const struct vio_device_id *id;
  62. if (!drv->probe)
  63. return -ENODEV;
  64. id = vio_match_device(drv->id_table, vdev);
  65. if (!id)
  66. return -ENODEV;
  67. /* alloc irqs (unless the driver specified not to) */
  68. if (!drv->no_irq) {
  69. if (vdev->tx_irq == 0 && vdev->tx_ino != ~0UL)
  70. vdev->tx_irq = sun4v_build_virq(vdev->cdev_handle,
  71. vdev->tx_ino);
  72. if (vdev->rx_irq == 0 && vdev->rx_ino != ~0UL)
  73. vdev->rx_irq = sun4v_build_virq(vdev->cdev_handle,
  74. vdev->rx_ino);
  75. }
  76. return drv->probe(vdev, id);
  77. }
  78. static void vio_device_remove(struct device *dev)
  79. {
  80. struct vio_dev *vdev = to_vio_dev(dev);
  81. struct vio_driver *drv = to_vio_driver(dev->driver);
  82. if (drv->remove) {
  83. /*
  84. * Ideally, we would remove/deallocate tx/rx virqs
  85. * here - however, there are currently no support
  86. * routines to do so at the moment. TBD
  87. */
  88. drv->remove(vdev);
  89. }
  90. }
  91. static ssize_t devspec_show(struct device *dev,
  92. struct device_attribute *attr, char *buf)
  93. {
  94. struct vio_dev *vdev = to_vio_dev(dev);
  95. const char *str = "none";
  96. if (!strcmp(vdev->type, "vnet-port"))
  97. str = "vnet";
  98. else if (!strcmp(vdev->type, "vdc-port"))
  99. str = "vdisk";
  100. return sprintf(buf, "%s\n", str);
  101. }
  102. static DEVICE_ATTR_RO(devspec);
  103. static ssize_t type_show(struct device *dev,
  104. struct device_attribute *attr, char *buf)
  105. {
  106. struct vio_dev *vdev = to_vio_dev(dev);
  107. return sprintf(buf, "%s\n", vdev->type);
  108. }
  109. static DEVICE_ATTR_RO(type);
  110. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  111. char *buf)
  112. {
  113. const struct vio_dev *vdev = to_vio_dev(dev);
  114. return sprintf(buf, "vio:T%sS%s\n", vdev->type, vdev->compat);
  115. }
  116. static DEVICE_ATTR_RO(modalias);
  117. static struct attribute *vio_dev_attrs[] = {
  118. &dev_attr_devspec.attr,
  119. &dev_attr_type.attr,
  120. &dev_attr_modalias.attr,
  121. NULL,
  122. };
  123. ATTRIBUTE_GROUPS(vio_dev);
  124. static struct bus_type vio_bus_type = {
  125. .name = "vio",
  126. .dev_groups = vio_dev_groups,
  127. .uevent = vio_hotplug,
  128. .match = vio_bus_match,
  129. .probe = vio_device_probe,
  130. .remove = vio_device_remove,
  131. };
  132. int __vio_register_driver(struct vio_driver *viodrv, struct module *owner,
  133. const char *mod_name)
  134. {
  135. viodrv->driver.bus = &vio_bus_type;
  136. viodrv->driver.name = viodrv->name;
  137. viodrv->driver.owner = owner;
  138. viodrv->driver.mod_name = mod_name;
  139. return driver_register(&viodrv->driver);
  140. }
  141. EXPORT_SYMBOL(__vio_register_driver);
  142. void vio_unregister_driver(struct vio_driver *viodrv)
  143. {
  144. driver_unregister(&viodrv->driver);
  145. }
  146. EXPORT_SYMBOL(vio_unregister_driver);
  147. static void vio_dev_release(struct device *dev)
  148. {
  149. kfree(to_vio_dev(dev));
  150. }
  151. static ssize_t
  152. show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
  153. char *buf)
  154. {
  155. struct vio_dev *vdev;
  156. struct device_node *dp;
  157. vdev = to_vio_dev(dev);
  158. dp = vdev->dp;
  159. return scnprintf(buf, PAGE_SIZE, "%pOF\n", dp);
  160. }
  161. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
  162. show_pciobppath_attr, NULL);
  163. static struct device_node *cdev_node;
  164. static struct vio_dev *root_vdev;
  165. static u64 cdev_cfg_handle;
  166. static const u64 *vio_cfg_handle(struct mdesc_handle *hp, u64 node)
  167. {
  168. const u64 *cfg_handle = NULL;
  169. u64 a;
  170. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  171. u64 target;
  172. target = mdesc_arc_target(hp, a);
  173. cfg_handle = mdesc_get_property(hp, target,
  174. "cfg-handle", NULL);
  175. if (cfg_handle)
  176. break;
  177. }
  178. return cfg_handle;
  179. }
  180. /**
  181. * vio_vdev_node() - Find VDEV node in MD
  182. * @hp: Handle to the MD
  183. * @vdev: Pointer to VDEV
  184. *
  185. * Find the node in the current MD which matches the given vio_dev. This
  186. * must be done dynamically since the node value can change if the MD
  187. * is updated.
  188. *
  189. * NOTE: the MD must be locked, using mdesc_grab(), when calling this routine
  190. *
  191. * Return: The VDEV node in MDESC
  192. */
  193. u64 vio_vdev_node(struct mdesc_handle *hp, struct vio_dev *vdev)
  194. {
  195. u64 node;
  196. if (vdev == NULL)
  197. return MDESC_NODE_NULL;
  198. node = mdesc_get_node(hp, (const char *)vdev->node_name,
  199. &vdev->md_node_info);
  200. return node;
  201. }
  202. EXPORT_SYMBOL(vio_vdev_node);
  203. static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp,
  204. struct vio_dev *vdev)
  205. {
  206. u64 a;
  207. vdev->tx_ino = ~0UL;
  208. vdev->rx_ino = ~0UL;
  209. vdev->channel_id = ~0UL;
  210. mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
  211. const u64 *chan_id;
  212. const u64 *irq;
  213. u64 target;
  214. target = mdesc_arc_target(hp, a);
  215. irq = mdesc_get_property(hp, target, "tx-ino", NULL);
  216. if (irq)
  217. vdev->tx_ino = *irq;
  218. irq = mdesc_get_property(hp, target, "rx-ino", NULL);
  219. if (irq)
  220. vdev->rx_ino = *irq;
  221. chan_id = mdesc_get_property(hp, target, "id", NULL);
  222. if (chan_id)
  223. vdev->channel_id = *chan_id;
  224. }
  225. vdev->cdev_handle = cdev_cfg_handle;
  226. }
  227. int vio_set_intr(unsigned long dev_ino, int state)
  228. {
  229. int err;
  230. err = sun4v_vintr_set_valid(cdev_cfg_handle, dev_ino, state);
  231. return err;
  232. }
  233. EXPORT_SYMBOL(vio_set_intr);
  234. static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
  235. const char *node_name,
  236. struct device *parent)
  237. {
  238. const char *type, *compat;
  239. struct device_node *dp;
  240. struct vio_dev *vdev;
  241. int err, tlen, clen;
  242. const u64 *id, *cfg_handle;
  243. type = mdesc_get_property(hp, mp, "device-type", &tlen);
  244. if (!type) {
  245. type = mdesc_get_property(hp, mp, "name", &tlen);
  246. if (!type) {
  247. type = mdesc_node_name(hp, mp);
  248. tlen = strlen(type) + 1;
  249. }
  250. }
  251. if (tlen > VIO_MAX_TYPE_LEN || strlen(type) >= VIO_MAX_TYPE_LEN) {
  252. printk(KERN_ERR "VIO: Type string [%s] is too long.\n",
  253. type);
  254. return NULL;
  255. }
  256. id = mdesc_get_property(hp, mp, "id", NULL);
  257. cfg_handle = vio_cfg_handle(hp, mp);
  258. compat = mdesc_get_property(hp, mp, "device-type", &clen);
  259. if (!compat) {
  260. clen = 0;
  261. } else if (clen > VIO_MAX_COMPAT_LEN) {
  262. printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n",
  263. clen, type);
  264. return NULL;
  265. }
  266. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  267. if (!vdev) {
  268. printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
  269. return NULL;
  270. }
  271. vdev->mp = mp;
  272. memcpy(vdev->type, type, tlen);
  273. if (compat)
  274. memcpy(vdev->compat, compat, clen);
  275. else
  276. memset(vdev->compat, 0, sizeof(vdev->compat));
  277. vdev->compat_len = clen;
  278. vdev->port_id = ~0UL;
  279. vdev->tx_irq = 0;
  280. vdev->rx_irq = 0;
  281. vio_fill_channel_info(hp, mp, vdev);
  282. if (!id) {
  283. dev_set_name(&vdev->dev, "%s", type);
  284. vdev->dev_no = ~(u64)0;
  285. } else if (!cfg_handle) {
  286. dev_set_name(&vdev->dev, "%s-%llu", type, *id);
  287. vdev->dev_no = *id;
  288. } else {
  289. dev_set_name(&vdev->dev, "%s-%llu-%llu", type,
  290. *cfg_handle, *id);
  291. vdev->dev_no = *cfg_handle;
  292. vdev->port_id = *id;
  293. }
  294. vdev->dev.parent = parent;
  295. vdev->dev.bus = &vio_bus_type;
  296. vdev->dev.release = vio_dev_release;
  297. if (parent == NULL) {
  298. dp = cdev_node;
  299. } else if (to_vio_dev(parent) == root_vdev) {
  300. for_each_child_of_node(cdev_node, dp) {
  301. if (of_node_is_type(dp, type))
  302. break;
  303. }
  304. } else {
  305. dp = to_vio_dev(parent)->dp;
  306. }
  307. vdev->dp = dp;
  308. /*
  309. * node_name is NULL for the parent/channel-devices node and
  310. * the parent doesn't require the MD node info.
  311. */
  312. if (node_name != NULL) {
  313. (void) snprintf(vdev->node_name, VIO_MAX_NAME_LEN, "%s",
  314. node_name);
  315. err = mdesc_get_node_info(hp, mp, node_name,
  316. &vdev->md_node_info);
  317. if (err) {
  318. pr_err("VIO: Could not get MD node info %s, err=%d\n",
  319. dev_name(&vdev->dev), err);
  320. kfree(vdev);
  321. return NULL;
  322. }
  323. }
  324. pr_info("VIO: Adding device %s (tx_ino = %llx, rx_ino = %llx)\n",
  325. dev_name(&vdev->dev), vdev->tx_ino, vdev->rx_ino);
  326. err = device_register(&vdev->dev);
  327. if (err) {
  328. printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
  329. dev_name(&vdev->dev), err);
  330. put_device(&vdev->dev);
  331. return NULL;
  332. }
  333. if (vdev->dp)
  334. err = sysfs_create_file(&vdev->dev.kobj,
  335. &dev_attr_obppath.attr);
  336. return vdev;
  337. }
  338. static void vio_add(struct mdesc_handle *hp, u64 node,
  339. const char *node_name)
  340. {
  341. (void) vio_create_one(hp, node, node_name, &root_vdev->dev);
  342. }
  343. struct vio_remove_node_data {
  344. struct mdesc_handle *hp;
  345. u64 node;
  346. };
  347. static int vio_md_node_match(struct device *dev, void *arg)
  348. {
  349. struct vio_dev *vdev = to_vio_dev(dev);
  350. struct vio_remove_node_data *node_data;
  351. u64 node;
  352. node_data = (struct vio_remove_node_data *)arg;
  353. node = vio_vdev_node(node_data->hp, vdev);
  354. if (node == node_data->node)
  355. return 1;
  356. else
  357. return 0;
  358. }
  359. static void vio_remove(struct mdesc_handle *hp, u64 node, const char *node_name)
  360. {
  361. struct vio_remove_node_data node_data;
  362. struct device *dev;
  363. node_data.hp = hp;
  364. node_data.node = node;
  365. dev = device_find_child(&root_vdev->dev, (void *)&node_data,
  366. vio_md_node_match);
  367. if (dev) {
  368. printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev));
  369. device_unregister(dev);
  370. put_device(dev);
  371. } else {
  372. pr_err("VIO: %s node not found in MDESC\n", node_name);
  373. }
  374. }
  375. static struct mdesc_notifier_client vio_device_notifier = {
  376. .add = vio_add,
  377. .remove = vio_remove,
  378. .node_name = "virtual-device-port",
  379. };
  380. /* We are only interested in domain service ports under the
  381. * "domain-services" node. On control nodes there is another port
  382. * under "openboot" that we should not mess with as aparently that is
  383. * reserved exclusively for OBP use.
  384. */
  385. static void vio_add_ds(struct mdesc_handle *hp, u64 node,
  386. const char *node_name)
  387. {
  388. int found;
  389. u64 a;
  390. found = 0;
  391. mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
  392. u64 target = mdesc_arc_target(hp, a);
  393. const char *name = mdesc_node_name(hp, target);
  394. if (!strcmp(name, "domain-services")) {
  395. found = 1;
  396. break;
  397. }
  398. }
  399. if (found)
  400. (void) vio_create_one(hp, node, node_name, &root_vdev->dev);
  401. }
  402. static struct mdesc_notifier_client vio_ds_notifier = {
  403. .add = vio_add_ds,
  404. .remove = vio_remove,
  405. .node_name = "domain-services-port",
  406. };
  407. static const char *channel_devices_node = "channel-devices";
  408. static const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
  409. static const char *cfg_handle_prop = "cfg-handle";
  410. static int __init vio_init(void)
  411. {
  412. struct mdesc_handle *hp;
  413. const char *compat;
  414. const u64 *cfg_handle;
  415. int err, len;
  416. u64 root;
  417. err = bus_register(&vio_bus_type);
  418. if (err) {
  419. printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
  420. err);
  421. return err;
  422. }
  423. hp = mdesc_grab();
  424. if (!hp)
  425. return 0;
  426. root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
  427. if (root == MDESC_NODE_NULL) {
  428. printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
  429. mdesc_release(hp);
  430. return 0;
  431. }
  432. cdev_node = of_find_node_by_name(NULL, "channel-devices");
  433. err = -ENODEV;
  434. if (!cdev_node) {
  435. printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
  436. goto out_release;
  437. }
  438. compat = mdesc_get_property(hp, root, "compatible", &len);
  439. if (!compat) {
  440. printk(KERN_ERR "VIO: Channel devices lacks compatible "
  441. "property\n");
  442. goto out_release;
  443. }
  444. if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
  445. printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
  446. "compat entry.\n", channel_devices_compat);
  447. goto out_release;
  448. }
  449. cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
  450. if (!cfg_handle) {
  451. printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
  452. cfg_handle_prop);
  453. goto out_release;
  454. }
  455. cdev_cfg_handle = *cfg_handle;
  456. root_vdev = vio_create_one(hp, root, NULL, NULL);
  457. err = -ENODEV;
  458. if (!root_vdev) {
  459. printk(KERN_ERR "VIO: Could not create root device.\n");
  460. goto out_release;
  461. }
  462. mdesc_register_notifier(&vio_device_notifier);
  463. mdesc_register_notifier(&vio_ds_notifier);
  464. mdesc_release(hp);
  465. return err;
  466. out_release:
  467. mdesc_release(hp);
  468. return err;
  469. }
  470. postcore_initcall(vio_init);