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. #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,
  86. false, -1);
  87. if (!scsidev) {
  88. return -1;
  89. }
  90. dinfo->unit = scsidev->id;
  91. if (printinfo)
  92. monitor_printf(mon, "OK bus %d, unit %d\n",
  93. scsibus->busnr, scsidev->id);
  94. return 0;
  95. }
  96. void drive_hot_add(Monitor *mon, const QDict *qdict)
  97. {
  98. int dom, pci_bus;
  99. unsigned slot;
  100. int type;
  101. PCIDevice *dev;
  102. DriveInfo *dinfo = NULL;
  103. const char *pci_addr = qdict_get_str(qdict, "pci_addr");
  104. const char *opts = qdict_get_str(qdict, "opts");
  105. dinfo = add_init_drive(opts);
  106. if (!dinfo)
  107. goto err;
  108. if (dinfo->devaddr) {
  109. monitor_printf(mon, "Parameter addr not supported\n");
  110. goto err;
  111. }
  112. type = dinfo->type;
  113. switch (type) {
  114. case IF_SCSI:
  115. if (pci_read_devaddr(mon, pci_addr, &dom, &pci_bus, &slot)) {
  116. goto err;
  117. }
  118. dev = pci_find_device(pci_find_root_bus(dom), pci_bus,
  119. PCI_DEVFN(slot, 0));
  120. if (!dev) {
  121. monitor_printf(mon, "no pci device with address %s\n", pci_addr);
  122. goto err;
  123. }
  124. if (scsi_hot_add(mon, &dev->qdev, dinfo, 1) != 0) {
  125. goto err;
  126. }
  127. break;
  128. case IF_NONE:
  129. monitor_printf(mon, "OK\n");
  130. break;
  131. default:
  132. monitor_printf(mon, "Can't hot-add drive to type %d\n", type);
  133. goto err;
  134. }
  135. return;
  136. err:
  137. if (dinfo)
  138. drive_put_ref(dinfo);
  139. return;
  140. }
  141. static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon,
  142. const char *devaddr,
  143. const char *opts)
  144. {
  145. PCIDevice *dev;
  146. DriveInfo *dinfo = NULL;
  147. int type = -1;
  148. char buf[128];
  149. PCIBus *bus;
  150. int devfn;
  151. if (get_param_value(buf, sizeof(buf), "if", opts)) {
  152. if (!strcmp(buf, "scsi"))
  153. type = IF_SCSI;
  154. else if (!strcmp(buf, "virtio")) {
  155. type = IF_VIRTIO;
  156. } else {
  157. monitor_printf(mon, "type %s not a hotpluggable PCI device.\n", buf);
  158. return NULL;
  159. }
  160. } else {
  161. monitor_printf(mon, "no if= specified\n");
  162. return NULL;
  163. }
  164. if (get_param_value(buf, sizeof(buf), "file", opts)) {
  165. dinfo = add_init_drive(opts);
  166. if (!dinfo)
  167. return NULL;
  168. if (dinfo->devaddr) {
  169. monitor_printf(mon, "Parameter addr not supported\n");
  170. return NULL;
  171. }
  172. } else {
  173. dinfo = NULL;
  174. }
  175. bus = pci_get_bus_devfn(&devfn, devaddr);
  176. if (!bus) {
  177. monitor_printf(mon, "Invalid PCI device address %s\n", devaddr);
  178. return NULL;
  179. }
  180. if (!((BusState*)bus)->allow_hotplug) {
  181. monitor_printf(mon, "PCI bus doesn't support hotplug\n");
  182. return NULL;
  183. }
  184. switch (type) {
  185. case IF_SCSI:
  186. dev = pci_create(bus, devfn, "lsi53c895a");
  187. if (qdev_init(&dev->qdev) < 0)
  188. dev = NULL;
  189. if (dev && dinfo) {
  190. if (scsi_hot_add(mon, &dev->qdev, dinfo, 0) != 0) {
  191. qdev_unplug(&dev->qdev);
  192. dev = NULL;
  193. }
  194. }
  195. break;
  196. case IF_VIRTIO:
  197. if (!dinfo) {
  198. monitor_printf(mon, "virtio requires a backing file/device.\n");
  199. return NULL;
  200. }
  201. dev = pci_create(bus, devfn, "virtio-blk-pci");
  202. if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
  203. qdev_free(&dev->qdev);
  204. dev = NULL;
  205. break;
  206. }
  207. if (qdev_init(&dev->qdev) < 0)
  208. dev = NULL;
  209. break;
  210. default:
  211. dev = NULL;
  212. }
  213. return dev;
  214. }
  215. void pci_device_hot_add(Monitor *mon, const QDict *qdict)
  216. {
  217. PCIDevice *dev = NULL;
  218. const char *pci_addr = qdict_get_str(qdict, "pci_addr");
  219. const char *type = qdict_get_str(qdict, "type");
  220. const char *opts = qdict_get_try_str(qdict, "opts");
  221. /* strip legacy tag */
  222. if (!strncmp(pci_addr, "pci_addr=", 9)) {
  223. pci_addr += 9;
  224. }
  225. if (!opts) {
  226. opts = "";
  227. }
  228. if (!strcmp(pci_addr, "auto"))
  229. pci_addr = NULL;
  230. if (strcmp(type, "nic") == 0) {
  231. dev = qemu_pci_hot_add_nic(mon, pci_addr, opts);
  232. } else if (strcmp(type, "storage") == 0) {
  233. dev = qemu_pci_hot_add_storage(mon, pci_addr, opts);
  234. } else {
  235. monitor_printf(mon, "invalid type: %s\n", type);
  236. }
  237. if (dev) {
  238. monitor_printf(mon, "OK domain %d, bus %d, slot %d, function %d\n",
  239. pci_find_domain(dev->bus),
  240. pci_bus_num(dev->bus), PCI_SLOT(dev->devfn),
  241. PCI_FUNC(dev->devfn));
  242. } else
  243. monitor_printf(mon, "failed to add %s\n", opts);
  244. }
  245. #endif
  246. static int pci_device_hot_remove(Monitor *mon, const char *pci_addr)
  247. {
  248. PCIDevice *d;
  249. int dom, bus;
  250. unsigned slot;
  251. if (pci_read_devaddr(mon, pci_addr, &dom, &bus, &slot)) {
  252. return -1;
  253. }
  254. d = pci_find_device(pci_find_root_bus(dom), bus, PCI_DEVFN(slot, 0));
  255. if (!d) {
  256. monitor_printf(mon, "slot %d empty\n", slot);
  257. return -1;
  258. }
  259. return qdev_unplug(&d->qdev);
  260. }
  261. void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)
  262. {
  263. pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));
  264. }