Support addr=... in option argument of -drive if=virtio
[qemu] / hw / pci-hotplug.c
1 /*
2  * QEMU PCI hotplug support
3  *
4  * Copyright (c) 2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include "hw.h"
26 #include "boards.h"
27 #include "pci.h"
28 #include "net.h"
29 #include "sysemu.h"
30 #include "pc.h"
31 #include "monitor.h"
32 #include "block_int.h"
33 #include "virtio-blk.h"
34
35 #if defined(TARGET_I386) || defined(TARGET_X86_64)
36 static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
37                                        const char *devaddr, const char *opts)
38 {
39     int ret;
40
41     ret = net_client_init(mon, "nic", opts);
42     if (ret < 0)
43         return NULL;
44     if (nd_table[ret].devaddr) {
45         monitor_printf(mon, "Parameter addr not supported\n");
46         return NULL;
47     }
48     return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
49 }
50
51 void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
52 {
53     int dom, pci_bus;
54     unsigned slot;
55     int drive_idx, type, bus;
56     int success = 0;
57     PCIDevice *dev;
58
59     if (pci_read_devaddr(pci_addr, &dom, &pci_bus, &slot)) {
60         monitor_printf(mon, "Invalid pci address\n");
61         return;
62     }
63
64     dev = pci_find_device(pci_bus, slot, 0);
65     if (!dev) {
66         monitor_printf(mon, "no pci device with address %s\n", pci_addr);
67         return;
68     }
69
70     drive_idx = add_init_drive(opts);
71     if (drive_idx < 0)
72         return;
73     if (drives_table[drive_idx].devaddr) {
74         monitor_printf(mon, "Parameter addr not supported\n");
75         return;
76     }
77     type = drives_table[drive_idx].type;
78     bus = drive_get_max_bus (type);
79
80     switch (type) {
81     case IF_SCSI:
82         success = 1;
83         lsi_scsi_attach(&dev->qdev, drives_table[drive_idx].bdrv,
84                         drives_table[drive_idx].unit);
85         break;
86     default:
87         monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
88     }
89
90     if (success)
91         monitor_printf(mon, "OK bus %d, unit %d\n",
92                        drives_table[drive_idx].bus,
93                        drives_table[drive_idx].unit);
94     return;
95 }
96
97 static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
98                                            const char *devaddr,
99                                            const char *opts)
100 {
101     PCIDevice *dev;
102     int type = -1, drive_idx = -1;
103     char buf[128];
104
105     if (get_param_value(buf, sizeof(buf), "if", opts)) {
106         if (!strcmp(buf, "scsi"))
107             type = IF_SCSI;
108         else if (!strcmp(buf, "virtio")) {
109             type = IF_VIRTIO;
110         } else {
111             monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
112             return NULL;
113         }
114     } else {
115         monitor_printf(mon, "no if= specified\n");
116         return NULL;
117     }
118
119     if (get_param_value(buf, sizeof(buf), "file", opts)) {
120         drive_idx = add_init_drive(opts);
121         if (drive_idx < 0)
122             return NULL;
123         if (drives_table[drive_idx].devaddr) {
124             monitor_printf(mon, "Parameter addr not supported\n");
125             return NULL;
126         }
127     } else if (type == IF_VIRTIO) {
128         monitor_printf(mon, "virtio requires a backing file/device.\n");
129         return NULL;
130     }
131
132     switch (type) {
133     case IF_SCSI:
134         dev = pci_create("lsi53c895a", devaddr);
135         break;
136     case IF_VIRTIO:
137         dev = pci_create("virtio-blk-pci", devaddr);
138         break;
139     default:
140         dev = NULL;
141     }
142     if (dev)
143         qdev_init(&dev->qdev);
144     return dev;
145 }
146
147 void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
148                         const char *opts)
149 {
150     PCIDevice *dev = NULL;
151     const char *devaddr = NULL;
152     char buf[32];
153
154     if (!get_param_value(buf, sizeof(buf), "pci_addr", pci_addr)) {
155         monitor_printf(mon, "Invalid pci address\n");
156         return;
157     }
158
159     if (strcmp(buf, "auto"))
160         devaddr = buf;
161
162     if (strcmp(type, "nic") == 0)
163         dev = qemu_pci_hot_add_nic(mon, devaddr, opts);
164     else if (strcmp(type, "storage") == 0)
165         dev = qemu_pci_hot_add_storage(mon, devaddr, opts);
166     else
167         monitor_printf(mon, "invalid type: %s\n", type);
168
169     if (dev) {
170         qemu_system_device_hot_add(pci_bus_num(dev->bus),
171                                    PCI_SLOT(dev->devfn), 1);
172         monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
173                        0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
174                        PCI_FUNC(dev->devfn));
175     } else
176         monitor_printf(mon, "failed to add %s\n", opts);
177 }
178 #endif
179
180 void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
181 {
182     PCIDevice *d;
183     int dom, bus;
184     unsigned slot;
185
186     if (pci_read_devaddr(pci_addr, &dom, &bus, &slot)) {
187         monitor_printf(mon, "Invalid pci address\n");
188         return;
189     }
190
191     d = pci_find_device(bus, slot, 0);
192     if (!d) {
193         monitor_printf(mon, "slot %d empty\n", slot);
194         return;
195     }
196
197     qemu_system_device_hot_add(bus, slot, 0);
198 }
199
200 static int pci_match_fn(void *dev_private, void *arg)
201 {
202     PCIDevice *dev = dev_private;
203     PCIDevice *match = arg;
204
205     return (dev == match);
206 }
207
208 /*
209  * OS has executed _EJ0 method, we now can remove the device
210  */
211 void pci_device_hot_remove_success(int pcibus, int slot)
212 {
213     PCIDevice *d = pci_find_device(pcibus, slot, 0);
214     int class_code;
215
216     if (!d) {
217         monitor_printf(cur_mon, "invalid slot %d\n", slot);
218         return;
219     }
220
221     class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
222
223     switch(class_code) {
224     case PCI_BASE_CLASS_STORAGE:
225         destroy_bdrvs(pci_match_fn, d);
226         break;
227     case PCI_BASE_CLASS_NETWORK:
228         destroy_nic(pci_match_fn, d);
229         break;
230     }
231
232     pci_unregister_device(d);
233 }
234