xref: /openbmc/qemu/hw/xen/xen_devconfig.c (revision dc5bd18f)
1 #include "qemu/osdep.h"
2 #include "hw/xen/xen_backend.h"
3 #include "qemu/option.h"
4 #include "sysemu/block-backend.h"
5 #include "sysemu/blockdev.h"
6 
7 /* ------------------------------------------------------------- */
8 
9 static int xen_config_dev_dirs(const char *ftype, const char *btype, int vdev,
10 			       char *fe, char *be, int len)
11 {
12     char *dom;
13 
14     dom = xs_get_domain_path(xenstore, xen_domid);
15     snprintf(fe, len, "%s/device/%s/%d", dom, ftype, vdev);
16     free(dom);
17 
18     dom = xs_get_domain_path(xenstore, 0);
19     snprintf(be, len, "%s/backend/%s/%d/%d", dom, btype, xen_domid, vdev);
20     free(dom);
21 
22     xenstore_mkdir(fe, XS_PERM_READ | XS_PERM_WRITE);
23     xenstore_mkdir(be, XS_PERM_READ);
24     return 0;
25 }
26 
27 static int xen_config_dev_all(char *fe, char *be)
28 {
29     /* frontend */
30     if (xen_protocol)
31         xenstore_write_str(fe, "protocol", xen_protocol);
32 
33     xenstore_write_int(fe, "state",           XenbusStateInitialising);
34     xenstore_write_int(fe, "backend-id",      0);
35     xenstore_write_str(fe, "backend",         be);
36 
37     /* backend */
38     xenstore_write_str(be, "domain",          qemu_name ? qemu_name : "no-name");
39     xenstore_write_int(be, "online",          1);
40     xenstore_write_int(be, "state",           XenbusStateInitialising);
41     xenstore_write_int(be, "frontend-id",     xen_domid);
42     xenstore_write_str(be, "frontend",        fe);
43 
44     return 0;
45 }
46 
47 /* ------------------------------------------------------------- */
48 
49 int xen_config_dev_blk(DriveInfo *disk)
50 {
51     char fe[256], be[256], device_name[32];
52     int vdev = 202 * 256 + 16 * disk->unit;
53     int cdrom = disk->media_cd;
54     const char *devtype = cdrom ? "cdrom" : "disk";
55     const char *mode    = cdrom ? "r"     : "w";
56     const char *filename = qemu_opt_get(disk->opts, "file");
57 
58     snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit);
59     xen_pv_printf(NULL, 1, "config disk %d [%s]: %s\n",
60                   disk->unit, device_name, filename);
61     xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
62 
63     /* frontend */
64     xenstore_write_int(fe, "virtual-device",  vdev);
65     xenstore_write_str(fe, "device-type",     devtype);
66 
67     /* backend */
68     xenstore_write_str(be, "dev",             device_name);
69     xenstore_write_str(be, "type",            "file");
70     xenstore_write_str(be, "params",          filename);
71     xenstore_write_str(be, "mode",            mode);
72 
73     /* common stuff */
74     return xen_config_dev_all(fe, be);
75 }
76 
77 int xen_config_dev_nic(NICInfo *nic)
78 {
79     char fe[256], be[256];
80     char mac[20];
81     int vlan_id = -1;
82 
83     net_hub_id_for_client(nic->netdev, &vlan_id);
84     snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
85              nic->macaddr.a[0], nic->macaddr.a[1], nic->macaddr.a[2],
86              nic->macaddr.a[3], nic->macaddr.a[4], nic->macaddr.a[5]);
87     xen_pv_printf(NULL, 1, "config nic %d: mac=\"%s\"\n", vlan_id, mac);
88     xen_config_dev_dirs("vif", "qnic", vlan_id, fe, be, sizeof(fe));
89 
90     /* frontend */
91     xenstore_write_int(fe, "handle",     vlan_id);
92     xenstore_write_str(fe, "mac",        mac);
93 
94     /* backend */
95     xenstore_write_int(be, "handle",     vlan_id);
96     xenstore_write_str(be, "mac",        mac);
97 
98     /* common stuff */
99     return xen_config_dev_all(fe, be);
100 }
101 
102 int xen_config_dev_vfb(int vdev, const char *type)
103 {
104     char fe[256], be[256];
105 
106     xen_config_dev_dirs("vfb", "vfb", vdev, fe, be, sizeof(fe));
107 
108     /* backend */
109     xenstore_write_str(be, "type",  type);
110 
111     /* common stuff */
112     return xen_config_dev_all(fe, be);
113 }
114 
115 int xen_config_dev_vkbd(int vdev)
116 {
117     char fe[256], be[256];
118 
119     xen_config_dev_dirs("vkbd", "vkbd", vdev, fe, be, sizeof(fe));
120     return xen_config_dev_all(fe, be);
121 }
122 
123 int xen_config_dev_console(int vdev)
124 {
125     char fe[256], be[256];
126 
127     xen_config_dev_dirs("console", "console", vdev, fe, be, sizeof(fe));
128     return xen_config_dev_all(fe, be);
129 }
130