Commit 5607c38820366954c38dd702e979499486057481

Authored by Markus Armbruster
Committed by Anthony Liguori
1 parent 07b7d053

Support addr=... in option argument of -net nic

Make net_client_init() accept addr=, put the value into struct
NICinfo.  Use it in pci_nic_init(), and remove arguments bus and
devfn.

Don't support addr= in third argument of monitor command pci_add,
because that clashes with its first argument.  Admittedly unelegant.

Machines "malta" and "r2d" have a default NIC with a well-known PCI
address.  Deal with that the same way as the NIC model: make
pci_nic_init() take an optional default to be used when the user
doesn't specify one.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
hw/mips_malta.c
... ... @@ -474,19 +474,19 @@ static void audio_init (PCIBus *pci_bus)
474 474 #endif
475 475  
476 476 /* Network support */
477   -static void network_init (PCIBus *pci_bus)
  477 +static void network_init(void)
478 478 {
479 479 int i;
480 480  
481 481 for(i = 0; i < nb_nics; i++) {
482 482 NICInfo *nd = &nd_table[i];
483   - int devfn = -1;
  483 + const char *default_devaddr = NULL;
484 484  
485 485 if (i == 0 && (!nd->model || strcmp(nd->model, "pcnet") == 0))
486 486 /* The malta board has a PCNet card using PCI SLOT 11 */
487   - devfn = 88;
  487 + default_devaddr = "0b";
488 488  
489   - pci_nic_init(pci_bus, nd, devfn, "pcnet");
  489 + pci_nic_init(nd, "pcnet", default_devaddr);
490 490 }
491 491 }
492 492  
... ... @@ -943,7 +943,7 @@ void mips_malta_init (ram_addr_t ram_size,
943 943 #endif
944 944  
945 945 /* Network card */
946   - network_init(pci_bus);
  946 + network_init();
947 947  
948 948 /* Optional PCI video card */
949 949 if (cirrus_vga_enabled) {
... ...
... ... @@ -1078,7 +1078,7 @@ static void pc_init1(ram_addr_t ram_size,
1078 1078 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
1079 1079 pc_init_ne2k_isa(nd, i8259);
1080 1080 else
1081   - pci_nic_init(pci_bus, nd, -1, "ne2k_pci");
  1081 + pci_nic_init(nd, "ne2k_pci", NULL);
1082 1082 }
1083 1083  
1084 1084 piix4_acpi_system_hot_add_init();
... ...
hw/pci-hotplug.c
... ... @@ -33,15 +33,18 @@
33 33 #include "virtio-blk.h"
34 34  
35 35 #if defined(TARGET_I386) || defined(TARGET_X86_64)
36   -static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, PCIBus *pci_bus,
37   - const char *opts)
  36 +static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, const char *opts)
38 37 {
39 38 int ret;
40 39  
41 40 ret = net_client_init(mon, "nic", opts);
42 41 if (ret < 0)
43 42 return NULL;
44   - return pci_nic_init(pci_bus, &nd_table[ret], -1, "rtl8139");
  43 + if (nd_table[ret].devaddr) {
  44 + monitor_printf(mon, "Parameter addr not supported\n");
  45 + return NULL;
  46 + }
  47 + return pci_nic_init(&nd_table[ret], "rtl8139", NULL);
45 48 }
46 49  
47 50 void drive_hot_add(Monitor *mon, const char *pci_addr, const char *opts)
... ... @@ -149,7 +152,7 @@ void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
149 152 }
150 153  
151 154 if (strcmp(type, "nic") == 0)
152   - dev = qemu_pci_hot_add_nic(mon, pci_bus, opts);
  155 + dev = qemu_pci_hot_add_nic(mon, opts);
153 156 else if (strcmp(type, "storage") == 0)
154 157 dev = qemu_pci_hot_add_storage(mon, pci_bus, opts);
155 158 else
... ...
hw/pci.c
... ... @@ -254,6 +254,24 @@ int pci_assign_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
254 254 return pci_parse_devaddr(devaddr, domp, busp, slotp);
255 255 }
256 256  
  257 +static PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
  258 +{
  259 + int dom, bus;
  260 + unsigned slot;
  261 +
  262 + if (!devaddr) {
  263 + *devfnp = -1;
  264 + return pci_find_bus(0);
  265 + }
  266 +
  267 + if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
  268 + return NULL;
  269 + }
  270 +
  271 + *devfnp = slot << 3;
  272 + return pci_find_bus(bus);
  273 +}
  274 +
257 275 /* -1 for devfn means auto assign */
258 276 static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
259 277 const char *name, int devfn,
... ... @@ -802,6 +820,24 @@ void pci_info(Monitor *mon)
802 820 pci_for_each_device(0, pci_info_device);
803 821 }
804 822  
  823 +static PCIDevice *pci_create(const char *name, const char *devaddr)
  824 +{
  825 + PCIBus *bus;
  826 + int devfn;
  827 + DeviceState *dev;
  828 +
  829 + bus = pci_get_bus_devfn(&devfn, devaddr);
  830 + if (!bus) {
  831 + fprintf(stderr, "Invalid PCI device address %s for device %s\n",
  832 + devaddr, name);
  833 + exit(1);
  834 + }
  835 +
  836 + dev = qdev_create(&bus->qbus, name);
  837 + qdev_set_prop_int(dev, "devfn", devfn);
  838 + return (PCIDevice *)dev;
  839 +}
  840 +
805 841 static const char * const pci_nic_models[] = {
806 842 "ne2k_pci",
807 843 "i82551",
... ... @@ -827,9 +863,11 @@ static const char * const pci_nic_names[] = {
827 863 };
828 864  
829 865 /* Initialize a PCI NIC. */
830   -PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
831   - const char *default_model)
  866 +PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
  867 + const char *default_devaddr)
832 868 {
  869 + const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
  870 + PCIDevice *pci_dev;
833 871 DeviceState *dev;
834 872 int i;
835 873  
... ... @@ -837,12 +875,12 @@ PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
837 875  
838 876 for (i = 0; pci_nic_models[i]; i++) {
839 877 if (strcmp(nd->model, pci_nic_models[i]) == 0) {
840   - dev = qdev_create(&bus->qbus, pci_nic_names[i]);
841   - qdev_set_prop_int(dev, "devfn", devfn);
  878 + pci_dev = pci_create(pci_nic_names[i], devaddr);
  879 + dev = &pci_dev->qdev;
842 880 qdev_set_netdev(dev, nd);
843 881 qdev_init(dev);
844 882 nd->private = dev;
845   - return (PCIDevice *)dev;
  883 + return pci_dev;
846 884 }
847 885 }
848 886  
... ...
hw/pci.h
... ... @@ -183,8 +183,8 @@ PCIBus *pci_register_bus(DeviceState *parent, const char *name,
183 183 pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
184 184 qemu_irq *pic, int devfn_min, int nirq);
185 185  
186   -PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
187   - const char *default_model);
  186 +PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
  187 + const char *default_devaddr);
188 188 void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len);
189 189 uint32_t pci_data_read(void *opaque, uint32_t addr, int len);
190 190 int pci_bus_num(PCIBus *s);
... ...
hw/ppc440_bamboo.c
... ... @@ -125,7 +125,7 @@ static void bamboo_init(ram_addr_t ram_size,
125 125 for (i = 0; i < nb_nics; i++) {
126 126 /* There are no PCI NICs on the Bamboo board, but there are
127 127 * PCI slots, so we can pick whatever default model we want. */
128   - pci_nic_init(pcibus, &nd_table[i], -1, "e1000");
  128 + pci_nic_init(&nd_table[i], "e1000", NULL);
129 129 }
130 130 }
131 131  
... ...
hw/ppc_newworld.c
... ... @@ -304,7 +304,7 @@ static void ppc_core99_init (ram_addr_t ram_size,
304 304 serial_hds[0], serial_hds[1], ESCC_CLOCK, 4);
305 305  
306 306 for(i = 0; i < nb_nics; i++)
307   - pci_nic_init(pci_bus, &nd_table[i], -1, "ne2k_pci");
  307 + pci_nic_init(&nd_table[i], "ne2k_pci", NULL);
308 308  
309 309 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
310 310 fprintf(stderr, "qemu: too many IDE bus\n");
... ...
hw/ppc_oldworld.c
... ... @@ -315,7 +315,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
315 315 serial_hds[1], ESCC_CLOCK, 4);
316 316  
317 317 for(i = 0; i < nb_nics; i++)
318   - pci_nic_init(pci_bus, &nd_table[i], -1, "ne2k_pci");
  318 + pci_nic_init(&nd_table[i], "ne2k_pci", NULL);
319 319  
320 320  
321 321 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
... ...
hw/ppc_prep.c
... ... @@ -680,7 +680,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
680 680 if (strcmp(nd_table[i].model, "ne2k_isa") == 0) {
681 681 isa_ne2000_init(ne2000_io[i], i8259[ne2000_irq[i]], &nd_table[i]);
682 682 } else {
683   - pci_nic_init(pci_bus, &nd_table[i], -1, "ne2k_pci");
  683 + pci_nic_init(&nd_table[i], "ne2k_pci", NULL);
684 684 }
685 685 }
686 686  
... ...
hw/ppce500_mpc8544ds.c
... ... @@ -225,7 +225,7 @@ static void mpc8544ds_init(ram_addr_t ram_size,
225 225  
226 226 /* Register network interfaces. */
227 227 for (i = 0; i < nb_nics; i++) {
228   - pci_nic_init(pci_bus, &nd_table[i], -1, "virtio");
  228 + pci_nic_init(&nd_table[i], "virtio", NULL);
229 229 }
230 230 }
231 231  
... ...
hw/r2d.c
... ... @@ -231,7 +231,7 @@ static void r2d_init(ram_addr_t ram_size,
231 231  
232 232 /* NIC: rtl8139 on-board, and 2 slots. */
233 233 for (i = 0; i < nb_nics; i++)
234   - pci_nic_init(pci, &nd_table[i], (i==0)? 2<<3: -1, "rtl8139");
  234 + pci_nic_init(&nd_table[i], "rtl8139", i==0 ? "2" : NULL);
235 235  
236 236 /* Todo: register on board registers */
237 237 if (kernel_filename) {
... ...
hw/realview.c
... ... @@ -125,7 +125,7 @@ static void realview_init(ram_addr_t ram_size,
125 125 smc91c111_init(nd, 0x4e000000, pic[28]);
126 126 done_smc = 1;
127 127 } else {
128   - pci_nic_init(pci_bus, nd, -1, "rtl8139");
  128 + pci_nic_init(nd, "rtl8139", NULL);
129 129 }
130 130 }
131 131  
... ...
hw/versatilepb.c
... ... @@ -212,7 +212,7 @@ static void versatile_init(ram_addr_t ram_size,
212 212 smc91c111_init(nd, 0x10010000, sic[25]);
213 213 done_smc = 1;
214 214 } else {
215   - pci_nic_init(pci_bus, nd, -1, "rtl8139");
  215 + pci_nic_init(nd, "rtl8139", NULL);
216 216 }
217 217 }
218 218 if (usb_enabled) {
... ...
... ... @@ -2103,7 +2103,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
2103 2103 }
2104 2104 if (!strcmp(device, "nic")) {
2105 2105 static const char * const nic_params[] = {
2106   - "vlan", "name", "macaddr", "model", NULL
  2106 + "vlan", "name", "macaddr", "model", "addr", NULL
2107 2107 };
2108 2108 NICInfo *nd;
2109 2109 uint8_t *macaddr;
... ... @@ -2138,6 +2138,9 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
2138 2138 if (get_param_value(buf, sizeof(buf), "model", p)) {
2139 2139 nd->model = strdup(buf);
2140 2140 }
  2141 + if (get_param_value(buf, sizeof(buf), "addr", p)) {
  2142 + nd->devaddr = strdup(buf);
  2143 + }
2141 2144 nd->vlan = vlan;
2142 2145 nd->name = name;
2143 2146 nd->used = 1;
... ...
... ... @@ -88,6 +88,7 @@ struct NICInfo {
88 88 uint8_t macaddr[6];
89 89 const char *model;
90 90 const char *name;
  91 + const char *devaddr;
91 92 VLANState *vlan;
92 93 void *private;
93 94 int used;
... ...
qemu-options.hx
... ... @@ -733,7 +733,7 @@ STEXI
733 733 ETEXI
734 734  
735 735 DEF("net", HAS_ARG, QEMU_OPTION_net,
736   - "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
  736 + "-net nic[,vlan=n][,macaddr=mac][,model=type][,name=str][,addr=str]\n"
737 737 " create a new Network Interface Card and connect it to VLAN 'n'\n"
738 738 #ifdef CONFIG_SLIRP
739 739 "-net user[,vlan=n][,name=str][,hostname=host]\n"
... ... @@ -767,10 +767,11 @@ DEF(&quot;net&quot;, HAS_ARG, QEMU_OPTION_net,
767 767 "-net none use it alone to have zero network devices; if no -net option\n"
768 768 " is provided, the default is '-net nic -net user'\n")
769 769 STEXI
770   -@item -net nic[,vlan=@var{n}][,macaddr=@var{addr}][,model=@var{type}][,name=@var{name}]
  770 +@item -net nic[,vlan=@var{n}][,macaddr=@var{mac}][,model=@var{type}][,name=@var{name}][,addr=@var{addr}]
771 771 Create a new Network Interface Card and connect it to VLAN @var{n} (@var{n}
772 772 = 0 is the default). The NIC is an ne2k_pci by default on the PC
773   -target. Optionally, the MAC address can be changed to @var{addr}
  773 +target. Optionally, the MAC address can be changed to @var{mac}, the
  774 +device address set to @var{addr} (PCI cards only),
774 775 and a @var{name} can be assigned for use in monitor commands. If no
775 776 @option{-net} option is specified, a single NIC is created.
776 777 Qemu can emulate several different models of network card.
... ...