pci-hotplug.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "hw.h"
  25. #include "boards.h"
  26. #include "pci.h"
  27. #include "net.h"
  28. #include "pc.h"
  29. #include "monitor.h"
  30. #include "scsi.h"
  31. #include "virtio-blk.h"
  32. #include "qemu-config.h"
  33. #include "blockdev.h"
  34. #include "error.h"
  35. #if defined(TARGET_I386)
  36. static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
  37. const char *devaddr,
  38. const char *opts_str)
  39. {
  40. Error *local_err = NULL;
  41. QemuOpts *opts;
  42. PCIBus *bus;
  43. int ret, devfn;
  44. bus = pci_get_bus_devfn(&devfn, devaddr);
  45. if (!bus) {
  46. monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
  47. return NULL;
  48. }
  49. if (!((BusState*)bus)->allow_hotplug) {
  50. monitor_printf(mon, "PCI bus doesn't support hotplug\n");
  51. return NULL;
  52. }
  53. opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
  54. if (!opts) {
  55. return NULL;
  56. }
  57. qemu_opt_set(opts, "type", "nic");
  58. ret = net_client_init(opts, 0, &local_err);
  59. if (error_is_set(&local_err)) {
  60. qerror_report_err(local_err);
  61. error_free(local_err);
  62. return NULL;
  63. }
  64. if (nd_table[ret].devaddr) {
  65. monitor_printf(mon, "Parameter addr not supported\n");
  66. return NULL;
  67. }
  68. return pci_nic_init(&nd_table[ret], "rtl8139", devaddr);
  69. }
  70. static int scsi_hot_add(Monitor *mon, DeviceState *adapter,
  71. DriveInfo *dinfo, int printinfo)
  72. {
  73. SCSIBus *scsibus;
  74. SCSIDevice *scsidev;
  75. scsibus = (SCSIBus *)
  76. object_dynamic_cast(OBJECT(QLIST_FIRST(&adapter->child_bus)),
  77. TYPE_SCSI_BUS);
  78. if (!scsibus) {
  79. error_report("Device is not a SCSI adapter");
  80. return -1;
  81. }
  82. /*
  83. * drive_init() tries to find a default for dinfo->unit. Doesn't
  84. * work at all for hotplug though as we assign the device to a
  85. * specific bus instead of the first bus with spare scsi ids.
  86. *
  87. * Ditch the calculated value and reload from option string (if
  88. * specified).
  89. */
  90. dinfo->unit = qemu_opt_get_number(dinfo->opts, "unit", -1);
  91. dinfo->bus = scsibus->busnr;
  92. scsidev = scsi_bus_legacy_add_drive(scsibus, dinfo->bdrv, dinfo->unit,
  93. false, -1);
  94. if (!scsidev) {
  95. return -1;
  96. }
  97. dinfo->unit = scsidev->id;
  98. if (printinfo)
  99. monitor_printf(mon, "OK bus %d, unit %d\n",
  100. scsibus->busnr, scsidev->id);
  101. return 0;
  102. }
  103. int pci_drive_hot_add(Monitor *mon, const QDict *qdict,
  104. DriveInfo *dinfo, int type)
  105. {
  106. int dom, pci_bus;
  107. unsigned slot;
  108. PCIDevice *dev;
  109. const char *pci_addr = qdict_get_str(qdict, "pci_addr");
  110. switch (type) {
  111. case IF_SCSI:
  112. if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
  113. goto err;
  114. }
  115. dev = pci_find_device(pci_find_root_bus(dom), pci_bus,
  116. PCI_DEVFN(slot, 0));
  117. if (!dev) {
  118. monitor_printf(mon, "no pci device with address %s\n", pci_addr);
  119. goto err;
  120. }
  121. if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
  122. goto err;
  123. }
  124. break;
  125. default:
  126. monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
  127. goto err;
  128. }
  129. return 0;
  130. err:
  131. return -1;
  132. }
  133. static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
  134. const char *devaddr,
  135. const char *opts)
  136. {
  137. PCIDevice *dev;
  138. DriveInfo *dinfo = NULL;
  139. int type = -1;
  140. char buf[128];
  141. PCIBus *bus;
  142. int devfn;
  143. if (get_param_value(buf, sizeof(buf), "if", opts)) {
  144. if (!strcmp(buf, "scsi"))
  145. type = IF_SCSI;
  146. else if (!strcmp(buf, "virtio")) {
  147. type = IF_VIRTIO;
  148. } else {
  149. monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
  150. return NULL;
  151. }
  152. } else {
  153. monitor_printf(mon, "no if= specified\n");
  154. return NULL;
  155. }
  156. if (get_param_value(buf, sizeof(buf), "file", opts)) {
  157. dinfo = add_init_drive(opts);
  158. if (!dinfo)
  159. return NULL;
  160. if (dinfo->devaddr) {
  161. monitor_printf(mon, "Parameter addr not supported\n");
  162. return NULL;
  163. }
  164. } else {
  165. dinfo = NULL;
  166. }
  167. bus = pci_get_bus_devfn(&devfn, devaddr);
  168. if (!bus) {
  169. monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
  170. return NULL;
  171. }
  172. if (!((BusState*)bus)->allow_hotplug) {
  173. monitor_printf(mon, "PCI bus doesn't support hotplug\n");
  174. return NULL;
  175. }
  176. switch (type) {
  177. case IF_SCSI:
  178. dev = pci_create(bus, devfn, "lsi53c895a");
  179. if (qdev_init(&dev->qdev) < 0)
  180. dev = NULL;
  181. if (dev && dinfo) {
  182. if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
  183. qdev_unplug(&dev->qdev, NULL);
  184. dev = NULL;
  185. }
  186. }
  187. break;
  188. case IF_VIRTIO:
  189. if (!dinfo) {
  190. monitor_printf(mon, "virtio requires a backing file/device.\n");
  191. return NULL;
  192. }
  193. dev = pci_create(bus, devfn, "virtio-blk-pci");
  194. if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
  195. qdev_free(&dev->qdev);
  196. dev = NULL;
  197. break;
  198. }
  199. if (qdev_init(&dev->qdev) < 0)
  200. dev = NULL;
  201. break;
  202. default:
  203. dev = NULL;
  204. }
  205. return dev;
  206. }
  207. void pci_device_hot_add(Monitor *mon, const QDict *qdict)
  208. {
  209. PCIDevice *dev = NULL;
  210. const char *pci_addr = qdict_get_str(qdict, "pci_addr");
  211. const char *type = qdict_get_str(qdict, "type");
  212. const char *opts = qdict_get_try_str(qdict, "opts");
  213. /* strip legacy tag */
  214. if (!strncmp(pci_addr, "pci_addr=", 9)) {
  215. pci_addr += 9;
  216. }
  217. if (!opts) {
  218. opts = "";
  219. }
  220. if (!strcmp(pci_addr, "auto"))
  221. pci_addr = NULL;
  222. if (strcmp(type, "nic") == 0) {
  223. dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
  224. } else if (strcmp(type, "storage") == 0) {
  225. dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
  226. } else {
  227. monitor_printf(mon, "invalid type: %s\n", type);
  228. }
  229. if (dev) {
  230. monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
  231. pci_find_domain(dev->bus),
  232. pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
  233. PCI_FUNC(dev->devfn));
  234. } else
  235. monitor_printf(mon, "failed to add %s\n", opts);
  236. }
  237. #endif
  238. static int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
  239. {
  240. PCIDevice *d;
  241. int dom, bus;
  242. unsigned slot;
  243. Error *local_err = NULL;
  244. if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
  245. return -1;
  246. }
  247. d = pci_find_device(pci_find_root_bus(dom), bus, PCI_DEVFN(slot, 0));
  248. if (!d) {
  249. monitor_printf(mon, "slot %d empty\n", slot);
  250. return -1;
  251. }
  252. qdev_unplug(&d->qdev, &local_err);
  253. if (error_is_set(&local_err)) {
  254. monitor_printf(mon, "%s\n", error_get_pretty(local_err));
  255. error_free(local_err);
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
  261. {
  262. pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
  263. }