pci-hotplug.c 8.3 KB

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