vio.c 13 KB

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