qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code
@ 2023-09-13 16:09 Thomas Huth
  2023-09-13 16:09 ` [PATCH v2 1/2] hw/mips/jazz: Move the NIC init code into a separate function Thomas Huth
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Thomas Huth @ 2023-09-13 16:09 UTC (permalink / raw)
  To: qemu-devel, Philippe Mathieu-Daudé
  Cc: Hervé Poussineau, Aleksandar Rikalo, Jiaxun Yang

The NIC init code of the jazz machines is rather cumbersome, with
a for-loop around it that is always left after the first iteration.
This patch series reworks this a little bit to make the code more
readable and shorter.

v2:
- Dropped the first patch from v1 since it has been solved
  differently by the patch "trivial: Simplify the spots that
  use TARGET_BIG_ENDIAN as a numeric value" recently
- Rebased the code on top of that RARGET_BIG_ENDIAN change

Thomas Huth (2):
  hw/mips/jazz: Move the NIC init code into a separate function
  hw/mips/jazz: Simplify the NIC setup code

 hw/mips/jazz.c | 81 ++++++++++++++++++++++++--------------------------
 1 file changed, 39 insertions(+), 42 deletions(-)

-- 
2.41.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] hw/mips/jazz: Move the NIC init code into a separate function
  2023-09-13 16:09 [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code Thomas Huth
@ 2023-09-13 16:09 ` Thomas Huth
  2023-09-13 16:09 ` [PATCH v2 2/2] hw/mips/jazz: Simplify the NIC setup code Thomas Huth
  2023-09-14  5:51 ` [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code Philippe Mathieu-Daudé
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Huth @ 2023-09-13 16:09 UTC (permalink / raw)
  To: qemu-devel, Philippe Mathieu-Daudé
  Cc: Hervé Poussineau, Aleksandar Rikalo, Jiaxun Yang

The mips_jazz_init() function is already quite big, so moving
away some code here can help to make it more understandable.
Additionally, by moving this code into a separate function, the
next patch (that will refactor the for-loop around the NIC init
code) will be much shorter and easier to understand.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 hw/mips/jazz.c | 62 ++++++++++++++++++++++++++++----------------------
 1 file changed, 35 insertions(+), 27 deletions(-)

diff --git a/hw/mips/jazz.c b/hw/mips/jazz.c
index 0081dcf921..829c9248e5 100644
--- a/hw/mips/jazz.c
+++ b/hw/mips/jazz.c
@@ -114,6 +114,40 @@ static const MemoryRegionOps dma_dummy_ops = {
     .endianness = DEVICE_NATIVE_ENDIAN,
 };
 
+static void mips_jazz_init_net(NICInfo *nd, IOMMUMemoryRegion *rc4030_dma_mr,
+                               DeviceState *rc4030, MemoryRegion *dp8393x_prom)
+{
+    DeviceState *dev;
+    SysBusDevice *sysbus;
+    int checksum, i;
+    uint8_t *prom;
+
+    qemu_check_nic_model(nd, "dp83932");
+
+    dev = qdev_new("dp8393x");
+    qdev_set_nic_properties(dev, nd);
+    qdev_prop_set_uint8(dev, "it_shift", 2);
+    qdev_prop_set_bit(dev, "big_endian", TARGET_BIG_ENDIAN);
+    object_property_set_link(OBJECT(dev), "dma_mr",
+                             OBJECT(rc4030_dma_mr), &error_abort);
+    sysbus = SYS_BUS_DEVICE(dev);
+    sysbus_realize_and_unref(sysbus, &error_fatal);
+    sysbus_mmio_map(sysbus, 0, 0x80001000);
+    sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(rc4030, 4));
+
+    /* Add MAC address with valid checksum to PROM */
+    prom = memory_region_get_ram_ptr(dp8393x_prom);
+    checksum = 0;
+    for (i = 0; i < 6; i++) {
+        prom[i] = nd->macaddr.a[i];
+        checksum += prom[i];
+        if (checksum > 0xff) {
+            checksum = (checksum + 1) & 0xff;
+        }
+    }
+    prom[7] = 0xff - checksum;
+}
+
 #define MAGNUM_BIOS_SIZE_MAX 0x7e000
 #define MAGNUM_BIOS_SIZE                                                       \
         (BIOS_SIZE < MAGNUM_BIOS_SIZE_MAX ? BIOS_SIZE : MAGNUM_BIOS_SIZE_MAX)
@@ -287,33 +321,7 @@ static void mips_jazz_init(MachineState *machine,
             nd->model = g_strdup("dp83932");
         }
         if (strcmp(nd->model, "dp83932") == 0) {
-            int checksum, i;
-            uint8_t *prom;
-
-            qemu_check_nic_model(nd, "dp83932");
-
-            dev = qdev_new("dp8393x");
-            qdev_set_nic_properties(dev, nd);
-            qdev_prop_set_uint8(dev, "it_shift", 2);
-            qdev_prop_set_bit(dev, "big_endian", TARGET_BIG_ENDIAN);
-            object_property_set_link(OBJECT(dev), "dma_mr",
-                                     OBJECT(rc4030_dma_mr), &error_abort);
-            sysbus = SYS_BUS_DEVICE(dev);
-            sysbus_realize_and_unref(sysbus, &error_fatal);
-            sysbus_mmio_map(sysbus, 0, 0x80001000);
-            sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(rc4030, 4));
-
-            /* Add MAC address with valid checksum to PROM */
-            prom = memory_region_get_ram_ptr(dp8393x_prom);
-            checksum = 0;
-            for (i = 0; i < 6; i++) {
-                prom[i] = nd->macaddr.a[i];
-                checksum += prom[i];
-                if (checksum > 0xff) {
-                    checksum = (checksum + 1) & 0xff;
-                }
-            }
-            prom[7] = 0xff - checksum;
+            mips_jazz_init_net(nd, rc4030_dma_mr, rc4030, dp8393x_prom);
             break;
         } else if (is_help_option(nd->model)) {
             error_report("Supported NICs: dp83932");
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] hw/mips/jazz: Simplify the NIC setup code
  2023-09-13 16:09 [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code Thomas Huth
  2023-09-13 16:09 ` [PATCH v2 1/2] hw/mips/jazz: Move the NIC init code into a separate function Thomas Huth
@ 2023-09-13 16:09 ` Thomas Huth
  2023-09-14  5:51 ` [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code Philippe Mathieu-Daudé
  2 siblings, 0 replies; 4+ messages in thread
From: Thomas Huth @ 2023-09-13 16:09 UTC (permalink / raw)
  To: qemu-devel, Philippe Mathieu-Daudé
  Cc: Hervé Poussineau, Aleksandar Rikalo, Jiaxun Yang

The for-loop does not make much sense here - it is always left after
the first iteration, so we can also check for nb_nics == 1 instead
which is way easier to understand.

Also, the checks for nd->model are superfluous since the code in
mips_jazz_init_net() calls qemu_check_nic_model() that already
takes care of this (i.e. initializing nd->model if it has not been
set yet, and checking whether it is the "help" option or the
supported NIC model).

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 hw/mips/jazz.c | 21 +++++----------------
 1 file changed, 5 insertions(+), 16 deletions(-)

diff --git a/hw/mips/jazz.c b/hw/mips/jazz.c
index 829c9248e5..c32d2b0b0a 100644
--- a/hw/mips/jazz.c
+++ b/hw/mips/jazz.c
@@ -172,7 +172,6 @@ static void mips_jazz_init(MachineState *machine,
     MemoryRegion *rtc = g_new(MemoryRegion, 1);
     MemoryRegion *dma_dummy = g_new(MemoryRegion, 1);
     MemoryRegion *dp8393x_prom = g_new(MemoryRegion, 1);
-    NICInfo *nd;
     DeviceState *dev, *rc4030;
     MMIOKBDState *i8042;
     SysBusDevice *sysbus;
@@ -315,21 +314,11 @@ static void mips_jazz_init(MachineState *machine,
     }
 
     /* Network controller */
-    for (n = 0; n < nb_nics; n++) {
-        nd = &nd_table[n];
-        if (!nd->model) {
-            nd->model = g_strdup("dp83932");
-        }
-        if (strcmp(nd->model, "dp83932") == 0) {
-            mips_jazz_init_net(nd, rc4030_dma_mr, rc4030, dp8393x_prom);
-            break;
-        } else if (is_help_option(nd->model)) {
-            error_report("Supported NICs: dp83932");
-            exit(1);
-        } else {
-            error_report("Unsupported NIC: %s", nd->model);
-            exit(1);
-        }
+    if (nb_nics == 1) {
+        mips_jazz_init_net(&nd_table[0], rc4030_dma_mr, rc4030, dp8393x_prom);
+    } else if (nb_nics > 1) {
+        error_report("This machine only supports one NIC");
+        exit(1);
     }
 
     /* SCSI adapter */
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code
  2023-09-13 16:09 [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code Thomas Huth
  2023-09-13 16:09 ` [PATCH v2 1/2] hw/mips/jazz: Move the NIC init code into a separate function Thomas Huth
  2023-09-13 16:09 ` [PATCH v2 2/2] hw/mips/jazz: Simplify the NIC setup code Thomas Huth
@ 2023-09-14  5:51 ` Philippe Mathieu-Daudé
  2 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-09-14  5:51 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel
  Cc: Hervé Poussineau, Aleksandar Rikalo, Jiaxun Yang

On 13/9/23 18:09, Thomas Huth wrote:

> Thomas Huth (2):
>    hw/mips/jazz: Move the NIC init code into a separate function
>    hw/mips/jazz: Simplify the NIC setup code

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-14  5:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-13 16:09 [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code Thomas Huth
2023-09-13 16:09 ` [PATCH v2 1/2] hw/mips/jazz: Move the NIC init code into a separate function Thomas Huth
2023-09-13 16:09 ` [PATCH v2 2/2] hw/mips/jazz: Simplify the NIC setup code Thomas Huth
2023-09-14  5:51 ` [PATCH v2 0/2] hw/mips/jazz: Rework the NIC init code Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).