]> git.proxmox.com Git - qemu.git/blame - hw/pci-hotplug.c
Support addr=... in option argument of -net nic
[qemu.git] / hw / pci-hotplug.c
CommitLineData
6f338c34
AL
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"
376253ec 31#include "monitor.h"
6f338c34
AL
32#include "block_int.h"
33#include "virtio-blk.h"
34
35#if defined(TARGET_I386) || defined(TARGET_X86_64)
5607c388 36static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, const char *opts)
6f338c34
AL
37{
38 int ret;
39
10ae5a7a 40 ret = net_client_init(mon, "nic", opts);
eefb4091 41 if (ret < 0)
6f338c34 42 return NULL;
5607c388
MA
43 if (nd_table[ret].devaddr) {
44 monitor_printf(mon, "Parameter addr not supported\n");
45 return NULL;
46 }
47 return pci_nic_init(&nd_table[ret], "rtl8139", NULL);
6f338c34
AL
48}
49
376253ec 50void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
6f338c34
AL
51{
52 int dom, pci_bus;
53 unsigned slot;
54 int drive_idx, type, bus;
55 int success = 0;
56 PCIDevice *dev;
57
58 if (pci_read_devaddr(pci_addr, &dom, &pci_bus, &slot)) {
376253ec 59 monitor_printf(mon, "Invalid pci address\n");
6f338c34
AL
60 return;
61 }
62
63 dev = pci_find_device(pci_bus, slot, 0);
64 if (!dev) {
376253ec 65 monitor_printf(mon, "no pci device with address %s\n", pci_addr);
6f338c34
AL
66 return;
67 }
68
69 drive_idx = add_init_drive(opts);
70 if (drive_idx < 0)
71 return;
72 type = drives_table[drive_idx].type;
73 bus = drive_get_max_bus (type);
74
75 switch (type) {
76 case IF_SCSI:
77 success = 1;
9be5dafe
PB
78 lsi_scsi_attach(&dev->qdev, drives_table[drive_idx].bdrv,
79 drives_table[drive_idx].unit);
6f338c34
AL
80 break;
81 default:
376253ec 82 monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
6f338c34
AL
83 }
84
85 if (success)
376253ec
AL
86 monitor_printf(mon, "OK bus %d, unit %d\n",
87 drives_table[drive_idx].bus,
88 drives_table[drive_idx].unit);
6f338c34
AL
89 return;
90}
91
376253ec
AL
92static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, PCIBus *pci_bus,
93 const char *opts)
6f338c34
AL
94{
95 void *opaque = NULL;
96 int type = -1, drive_idx = -1;
97 char buf[128];
98
99 if (get_param_value(buf, sizeof(buf), "if", opts)) {
100 if (!strcmp(buf, "scsi"))
101 type = IF_SCSI;
102 else if (!strcmp(buf, "virtio")) {
103 type = IF_VIRTIO;
8707ecca
AL
104 } else {
105 monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
106 goto out;
6f338c34
AL
107 }
108 } else {
376253ec 109 monitor_printf(mon, "no if= specified\n");
8707ecca 110 goto out;
6f338c34
AL
111 }
112
113 if (get_param_value(buf, sizeof(buf), "file", opts)) {
114 drive_idx = add_init_drive(opts);
115 if (drive_idx < 0)
8707ecca 116 goto out;
6f338c34 117 } else if (type == IF_VIRTIO) {
376253ec 118 monitor_printf(mon, "virtio requires a backing file/device.\n");
8707ecca 119 goto out;
6f338c34
AL
120 }
121
122 switch (type) {
123 case IF_SCSI:
9be5dafe 124 opaque = pci_create_simple(pci_bus, -1, "lsi53c895a");
6f338c34
AL
125 break;
126 case IF_VIRTIO:
53c25cea 127 opaque = pci_create_simple(pci_bus, -1, "virtio-blk-pci");
6f338c34 128 break;
6f338c34
AL
129 }
130
8707ecca 131out:
6f338c34
AL
132 return opaque;
133}
134
376253ec
AL
135void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
136 const char *opts)
6f338c34
AL
137{
138 PCIDevice *dev = NULL;
139 PCIBus *pci_bus;
140 int dom, bus;
141 unsigned slot;
142
143 if (pci_assign_devaddr(pci_addr, &dom, &bus, &slot)) {
376253ec 144 monitor_printf(mon, "Invalid pci address\n");
6f338c34
AL
145 return;
146 }
147
148 pci_bus = pci_find_bus(bus);
149 if (!pci_bus) {
376253ec 150 monitor_printf(mon, "Can't find pci_bus %d\n", bus);
6f338c34
AL
151 return;
152 }
153
154 if (strcmp(type, "nic") == 0)
5607c388 155 dev = qemu_pci_hot_add_nic(mon, opts);
6f338c34 156 else if (strcmp(type, "storage") == 0)
376253ec 157 dev = qemu_pci_hot_add_storage(mon, pci_bus, opts);
6f338c34 158 else
376253ec 159 monitor_printf(mon, "invalid type: %s\n", type);
6f338c34
AL
160
161 if (dev) {
162 qemu_system_device_hot_add(bus, PCI_SLOT(dev->devfn), 1);
376253ec
AL
163 monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
164 0, pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
165 PCI_FUNC(dev->devfn));
6f338c34 166 } else
376253ec 167 monitor_printf(mon, "failed to add %s\n", opts);
6f338c34
AL
168}
169#endif
170
376253ec 171void pci_device_hot_remove(Monitor *mon, const char *pci_addr)
6f338c34
AL
172{
173 PCIDevice *d;
174 int dom, bus;
175 unsigned slot;
176
177 if (pci_read_devaddr(pci_addr, &dom, &bus, &slot)) {
376253ec 178 monitor_printf(mon, "Invalid pci address\n");
6f338c34
AL
179 return;
180 }
181
182 d = pci_find_device(bus, slot, 0);
183 if (!d) {
376253ec 184 monitor_printf(mon, "slot %d empty\n", slot);
6f338c34
AL
185 return;
186 }
187
188 qemu_system_device_hot_add(bus, slot, 0);
189}
190
191static int pci_match_fn(void *dev_private, void *arg)
192{
193 PCIDevice *dev = dev_private;
194 PCIDevice *match = arg;
195
196 return (dev == match);
197}
198
199/*
200 * OS has executed _EJ0 method, we now can remove the device
201 */
202void pci_device_hot_remove_success(int pcibus, int slot)
203{
204 PCIDevice *d = pci_find_device(pcibus, slot, 0);
205 int class_code;
206
207 if (!d) {
376253ec 208 monitor_printf(cur_mon, "invalid slot %d\n", slot);
6f338c34
AL
209 return;
210 }
211
212 class_code = d->config_read(d, PCI_CLASS_DEVICE+1, 1);
213
214 switch(class_code) {
215 case PCI_BASE_CLASS_STORAGE:
216 destroy_bdrvs(pci_match_fn, d);
217 break;
218 case PCI_BASE_CLASS_NETWORK:
219 destroy_nic(pci_match_fn, d);
220 break;
221 }
222
223 pci_unregister_device(d);
224}
225