blob: 9e38cc4808f75c965b8284776bf138095f9a1889 [file] [log] [blame]
aliguori6f338c32009-02-11 15:21:54 +00001/*
2 * QEMU device hotplug helpers
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
Paolo Bonzini1559e0d2013-02-04 17:20:47 +010025#include "hw/hw.h"
26#include "hw/boards.h"
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +020027#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010028#include "sysemu/blockdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/config-file.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010031#include "monitor/monitor.h"
aliguori6f338c32009-02-11 15:21:54 +000032
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +020033DriveInfo *add_init_drive(const char *optstr)
aliguori6f338c32009-02-11 15:21:54 +000034{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +020035 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +020036 QemuOpts *opts;
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +020037 MachineClass *mc;
aliguori6f338c32009-02-11 15:21:54 +000038
Markus Armbruster2292dda2011-01-28 11:21:41 +010039 opts = drive_def(optstr);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +020040 if (!opts)
Gerd Hoffmann751c6a12009-07-22 16:42:57 +020041 return NULL;
aliguori6f338c32009-02-11 15:21:54 +000042
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +020043 mc = MACHINE_GET_CLASS(current_machine);
Markus Armbruster60e19e02014-06-06 14:50:58 +020044 dinfo = drive_new(opts, mc->block_default_type);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +020045 if (!dinfo) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +020046 qemu_opts_del(opts);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +020047 return NULL;
aliguori6f338c32009-02-11 15:21:54 +000048 }
49
Gerd Hoffmann751c6a12009-07-22 16:42:57 +020050 return dinfo;
aliguori6f338c32009-02-11 15:21:54 +000051}
Alexander Grafdd97aa82010-08-23 23:43:10 +020052
Alexander Grafdd97aa82010-08-23 23:43:10 +020053void drive_hot_add(Monitor *mon, const QDict *qdict)
54{
Alexander Grafdd97aa82010-08-23 23:43:10 +020055 DriveInfo *dinfo = NULL;
56 const char *opts = qdict_get_str(qdict, "opts");
57
58 dinfo = add_init_drive(opts);
59 if (!dinfo) {
60 goto err;
61 }
62 if (dinfo->devaddr) {
63 monitor_printf(mon, "Parameter addr not supported\n");
64 goto err;
65 }
Alexander Grafdd97aa82010-08-23 23:43:10 +020066
Markus Armbruster4dbd84e2012-11-22 15:16:36 +010067 switch (dinfo->type) {
Alexander Grafdd97aa82010-08-23 23:43:10 +020068 case IF_NONE:
69 monitor_printf(mon, "OK\n");
70 break;
71 default:
Markus Armbruster4dbd84e2012-11-22 15:16:36 +010072 if (pci_drive_hot_add(mon, qdict, dinfo)) {
Alexander Grafdd97aa82010-08-23 23:43:10 +020073 goto err;
74 }
75 }
76 return;
77
78err:
79 if (dinfo) {
Markus Armbrusterb9fe8a72014-10-07 13:59:09 +020080 blk_unref(blk_by_legacy_dinfo(dinfo));
Alexander Grafdd97aa82010-08-23 23:43:10 +020081 }
Alexander Grafdd97aa82010-08-23 23:43:10 +020082}