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