qemu/hw/ppc/spapr_drc.c
<<
>>
Prefs
   1/*
   2 * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
   3 *
   4 * Copyright IBM Corp. 2014
   5 *
   6 * Authors:
   7 *  Michael Roth      <mdroth@linux.vnet.ibm.com>
   8 *
   9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10 * See the COPYING file in the top-level directory.
  11 */
  12
  13#include "qemu/osdep.h"
  14#include "qapi/error.h"
  15#include "qapi/qmp/qnull.h"
  16#include "qemu/cutils.h"
  17#include "hw/ppc/spapr_drc.h"
  18#include "qom/object.h"
  19#include "migration/vmstate.h"
  20#include "qapi/visitor.h"
  21#include "qemu/error-report.h"
  22#include "hw/ppc/spapr.h" /* for RTAS return codes */
  23#include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
  24#include "hw/ppc/spapr_nvdimm.h"
  25#include "sysemu/device_tree.h"
  26#include "sysemu/reset.h"
  27#include "trace.h"
  28
  29#define DRC_CONTAINER_PATH "/dr-connector"
  30#define DRC_INDEX_TYPE_SHIFT 28
  31#define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
  32
  33SpaprDrcType spapr_drc_type(SpaprDrc *drc)
  34{
  35    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  36
  37    return 1 << drck->typeshift;
  38}
  39
  40uint32_t spapr_drc_index(SpaprDrc *drc)
  41{
  42    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  43
  44    /* no set format for a drc index: it only needs to be globally
  45     * unique. this is how we encode the DRC type on bare-metal
  46     * however, so might as well do that here
  47     */
  48    return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
  49        | (drc->id & DRC_INDEX_ID_MASK);
  50}
  51
  52static void spapr_drc_release(SpaprDrc *drc)
  53{
  54    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  55
  56    drck->release(drc->dev);
  57
  58    drc->unplug_requested = false;
  59    g_free(drc->fdt);
  60    drc->fdt = NULL;
  61    drc->fdt_start_offset = 0;
  62    object_property_del(OBJECT(drc), "device");
  63    drc->dev = NULL;
  64}
  65
  66static uint32_t drc_isolate_physical(SpaprDrc *drc)
  67{
  68    switch (drc->state) {
  69    case SPAPR_DRC_STATE_PHYSICAL_POWERON:
  70        return RTAS_OUT_SUCCESS; /* Nothing to do */
  71    case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
  72        break; /* see below */
  73    case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
  74        return RTAS_OUT_PARAM_ERROR; /* not allowed */
  75    default:
  76        g_assert_not_reached();
  77    }
  78
  79    drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
  80
  81    if (drc->unplug_requested) {
  82        uint32_t drc_index = spapr_drc_index(drc);
  83        trace_spapr_drc_set_isolation_state_finalizing(drc_index);
  84        spapr_drc_release(drc);
  85    }
  86
  87    return RTAS_OUT_SUCCESS;
  88}
  89
  90static uint32_t drc_unisolate_physical(SpaprDrc *drc)
  91{
  92    switch (drc->state) {
  93    case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
  94    case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
  95        return RTAS_OUT_SUCCESS; /* Nothing to do */
  96    case SPAPR_DRC_STATE_PHYSICAL_POWERON:
  97        break; /* see below */
  98    default:
  99        g_assert_not_reached();
 100    }
 101
 102    /* cannot unisolate a non-existent resource, and, or resources
 103     * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
 104     * 13.5.3.5)
 105     */
 106    if (!drc->dev) {
 107        return RTAS_OUT_NO_SUCH_INDICATOR;
 108    }
 109
 110    drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE;
 111    drc->ccs_offset = drc->fdt_start_offset;
 112    drc->ccs_depth = 0;
 113
 114    return RTAS_OUT_SUCCESS;
 115}
 116
 117static uint32_t drc_isolate_logical(SpaprDrc *drc)
 118{
 119    switch (drc->state) {
 120    case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
 121    case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
 122        return RTAS_OUT_SUCCESS; /* Nothing to do */
 123    case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
 124        break; /* see below */
 125    case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
 126        return RTAS_OUT_PARAM_ERROR; /* not allowed */
 127    default:
 128        g_assert_not_reached();
 129    }
 130
 131    /*
 132     * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
 133     * belong to a DIMM device that is marked for removal.
 134     *
 135     * Currently the guest userspace tool drmgr that drives the memory
 136     * hotplug/unplug will just try to remove a set of 'removable' LMBs
 137     * in response to a hot unplug request that is based on drc-count.
 138     * If the LMB being removed doesn't belong to a DIMM device that is
 139     * actually being unplugged, fail the isolation request here.
 140     */
 141    if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
 142        && !drc->unplug_requested) {
 143        return RTAS_OUT_HW_ERROR;
 144    }
 145
 146    drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
 147
 148    return RTAS_OUT_SUCCESS;
 149}
 150
 151static uint32_t drc_unisolate_logical(SpaprDrc *drc)
 152{
 153    SpaprMachineState *spapr = NULL;
 154
 155    switch (drc->state) {
 156    case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
 157    case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
 158        /*
 159         * Unisolating a logical DRC that was marked for unplug
 160         * means that the kernel is refusing the removal.
 161         */
 162        if (drc->unplug_requested && drc->dev) {
 163            if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
 164                spapr = SPAPR_MACHINE(qdev_get_machine());
 165
 166                spapr_memory_unplug_rollback(spapr, drc->dev);
 167            }
 168
 169            drc->unplug_requested = false;
 170            error_report("Device hotunplug rejected by the guest "
 171                         "for device %s", drc->dev->id);
 172
 173            /*
 174             * TODO: send a QAPI DEVICE_UNPLUG_ERROR event when
 175             * it is implemented.
 176             */
 177        }
 178
 179        return RTAS_OUT_SUCCESS; /* Nothing to do */
 180    case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
 181        break; /* see below */
 182    case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
 183        return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
 184    default:
 185        g_assert_not_reached();
 186    }
 187
 188    /* Move to AVAILABLE state should have ensured device was present */
 189    g_assert(drc->dev);
 190
 191    drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
 192    drc->ccs_offset = drc->fdt_start_offset;
 193    drc->ccs_depth = 0;
 194
 195    return RTAS_OUT_SUCCESS;
 196}
 197
 198static uint32_t drc_set_usable(SpaprDrc *drc)
 199{
 200    switch (drc->state) {
 201    case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
 202    case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
 203    case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
 204        return RTAS_OUT_SUCCESS; /* Nothing to do */
 205    case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
 206        break; /* see below */
 207    default:
 208        g_assert_not_reached();
 209    }
 210
 211    /* if there's no resource/device associated with the DRC, there's
 212     * no way for us to put it in an allocation state consistent with
 213     * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
 214     * result in an RTAS return code of -3 / "no such indicator"
 215     */
 216    if (!drc->dev) {
 217        return RTAS_OUT_NO_SUCH_INDICATOR;
 218    }
 219    if (drc->unplug_requested) {
 220        /* Don't allow the guest to move a device away from UNUSABLE
 221         * state when we want to unplug it */
 222        return RTAS_OUT_NO_SUCH_INDICATOR;
 223    }
 224
 225    drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
 226
 227    return RTAS_OUT_SUCCESS;
 228}
 229
 230static uint32_t drc_set_unusable(SpaprDrc *drc)
 231{
 232    switch (drc->state) {
 233    case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
 234        return RTAS_OUT_SUCCESS; /* Nothing to do */
 235    case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
 236        break; /* see below */
 237    case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
 238    case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
 239        return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
 240    default:
 241        g_assert_not_reached();
 242    }
 243
 244    drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
 245    if (drc->unplug_requested) {
 246        uint32_t drc_index = spapr_drc_index(drc);
 247        trace_spapr_drc_set_allocation_state_finalizing(drc_index);
 248        spapr_drc_release(drc);
 249    }
 250
 251    return RTAS_OUT_SUCCESS;
 252}
 253
 254static char *spapr_drc_name(SpaprDrc *drc)
 255{
 256    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 257
 258    /* human-readable name for a DRC to encode into the DT
 259     * description. this is mainly only used within a guest in place
 260     * of the unique DRC index.
 261     *
 262     * in the case of VIO/PCI devices, it corresponds to a "location
 263     * code" that maps a logical device/function (DRC index) to a
 264     * physical (or virtual in the case of VIO) location in the system
 265     * by chaining together the "location label" for each
 266     * encapsulating component.
 267     *
 268     * since this is more to do with diagnosing physical hardware
 269     * issues than guest compatibility, we choose location codes/DRC
 270     * names that adhere to the documented format, but avoid encoding
 271     * the entire topology information into the label/code, instead
 272     * just using the location codes based on the labels for the
 273     * endpoints (VIO/PCI adaptor connectors), which is basically just
 274     * "C" followed by an integer ID.
 275     *
 276     * DRC names as documented by PAPR+ v2.7, 13.5.2.4
 277     * location codes as documented by PAPR+ v2.7, 12.3.1.5
 278     */
 279    return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
 280}
 281
 282/*
 283 * dr-entity-sense sensor value
 284 * returned via get-sensor-state RTAS calls
 285 * as expected by state diagram in PAPR+ 2.7, 13.4
 286 * based on the current allocation/indicator/power states
 287 * for the DR connector.
 288 */
 289static SpaprDREntitySense physical_entity_sense(SpaprDrc *drc)
 290{
 291    /* this assumes all PCI devices are assigned to a 'live insertion'
 292     * power domain, where QEMU manages power state automatically as
 293     * opposed to the guest. present, non-PCI resources are unaffected
 294     * by power state.
 295     */
 296    return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
 297        : SPAPR_DR_ENTITY_SENSE_EMPTY;
 298}
 299
 300static SpaprDREntitySense logical_entity_sense(SpaprDrc *drc)
 301{
 302    switch (drc->state) {
 303    case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
 304        return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
 305    case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
 306    case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
 307    case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
 308        g_assert(drc->dev);
 309        return SPAPR_DR_ENTITY_SENSE_PRESENT;
 310    default:
 311        g_assert_not_reached();
 312    }
 313}
 314
 315static void prop_get_index(Object *obj, Visitor *v, const char *name,
 316                           void *opaque, Error **errp)
 317{
 318    SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
 319    uint32_t value = spapr_drc_index(drc);
 320    visit_type_uint32(v, name, &value, errp);
 321}
 322
 323static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
 324                         void *opaque, Error **errp)
 325{
 326    SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
 327    QNull *null = NULL;
 328    int fdt_offset_next, fdt_offset, fdt_depth;
 329    void *fdt;
 330
 331    if (!drc->fdt) {
 332        visit_type_null(v, NULL, &null, errp);
 333        qobject_unref(null);
 334        return;
 335    }
 336
 337    fdt = drc->fdt;
 338    fdt_offset = drc->fdt_start_offset;
 339    fdt_depth = 0;
 340
 341    do {
 342        const char *name = NULL;
 343        const struct fdt_property *prop = NULL;
 344        int prop_len = 0, name_len = 0;
 345        uint32_t tag;
 346        bool ok;
 347
 348        tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
 349        switch (tag) {
 350        case FDT_BEGIN_NODE:
 351            fdt_depth++;
 352            name = fdt_get_name(fdt, fdt_offset, &name_len);
 353            if (!visit_start_struct(v, name, NULL, 0, errp)) {
 354                return;
 355            }
 356            break;
 357        case FDT_END_NODE:
 358            /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
 359            g_assert(fdt_depth > 0);
 360            ok = visit_check_struct(v, errp);
 361            visit_end_struct(v, NULL);
 362            if (!ok) {
 363                return;
 364            }
 365            fdt_depth--;
 366            break;
 367        case FDT_PROP: {
 368            int i;
 369            prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
 370            name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
 371            if (!visit_start_list(v, name, NULL, 0, errp)) {
 372                return;
 373            }
 374            for (i = 0; i < prop_len; i++) {
 375                if (!visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i],
 376                                      errp)) {
 377                    return;
 378                }
 379            }
 380            ok = visit_check_list(v, errp);
 381            visit_end_list(v, NULL);
 382            if (!ok) {
 383                return;
 384            }
 385            break;
 386        }
 387        default:
 388            error_report("device FDT in unexpected state: %d", tag);
 389            abort();
 390        }
 391        fdt_offset = fdt_offset_next;
 392    } while (fdt_depth != 0);
 393}
 394
 395void spapr_drc_attach(SpaprDrc *drc, DeviceState *d)
 396{
 397    trace_spapr_drc_attach(spapr_drc_index(drc));
 398
 399    g_assert(!drc->dev);
 400    g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
 401             || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
 402
 403    drc->dev = d;
 404
 405    object_property_add_link(OBJECT(drc), "device",
 406                             object_get_typename(OBJECT(drc->dev)),
 407                             (Object **)(&drc->dev),
 408                             NULL, 0);
 409}
 410
 411void spapr_drc_unplug_request(SpaprDrc *drc)
 412{
 413    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 414
 415    trace_spapr_drc_unplug_request(spapr_drc_index(drc));
 416
 417    g_assert(drc->dev);
 418
 419    drc->unplug_requested = true;
 420
 421    if (drc->state != drck->empty_state) {
 422        trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
 423        return;
 424    }
 425
 426    spapr_drc_release(drc);
 427}
 428
 429bool spapr_drc_reset(SpaprDrc *drc)
 430{
 431    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 432    bool unplug_completed = false;
 433
 434    trace_spapr_drc_reset(spapr_drc_index(drc));
 435
 436    /* immediately upon reset we can safely assume DRCs whose devices
 437     * are pending removal can be safely removed.
 438     */
 439    if (drc->unplug_requested) {
 440        spapr_drc_release(drc);
 441        unplug_completed = true;
 442    }
 443
 444    if (drc->dev) {
 445        /* A device present at reset is ready to go, same as coldplugged */
 446        drc->state = drck->ready_state;
 447        /*
 448         * Ensure that we are able to send the FDT fragment again
 449         * via configure-connector call if the guest requests.
 450         */
 451        drc->ccs_offset = drc->fdt_start_offset;
 452        drc->ccs_depth = 0;
 453    } else {
 454        drc->state = drck->empty_state;
 455        drc->ccs_offset = -1;
 456        drc->ccs_depth = -1;
 457    }
 458
 459    return unplug_completed;
 460}
 461
 462static bool spapr_drc_unplug_requested_needed(void *opaque)
 463{
 464    return spapr_drc_unplug_requested(opaque);
 465}
 466
 467static const VMStateDescription vmstate_spapr_drc_unplug_requested = {
 468    .name = "spapr_drc/unplug_requested",
 469    .version_id = 1,
 470    .minimum_version_id = 1,
 471    .needed = spapr_drc_unplug_requested_needed,
 472    .fields  = (VMStateField []) {
 473        VMSTATE_BOOL(unplug_requested, SpaprDrc),
 474        VMSTATE_END_OF_LIST()
 475    }
 476};
 477
 478static bool spapr_drc_needed(void *opaque)
 479{
 480    SpaprDrc *drc = opaque;
 481    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 482
 483    /*
 484     * If no dev is plugged in there is no need to migrate the DRC state
 485     * nor to reset the DRC at CAS.
 486     */
 487    if (!drc->dev) {
 488        return false;
 489    }
 490
 491    /*
 492     * We need to reset the DRC at CAS or to migrate the DRC state if it's
 493     * not equal to the expected long-term state, which is the same as the
 494     * coldplugged initial state, or if an unplug request is pending.
 495     */
 496    return drc->state != drck->ready_state ||
 497        spapr_drc_unplug_requested(drc);
 498}
 499
 500static const VMStateDescription vmstate_spapr_drc = {
 501    .name = "spapr_drc",
 502    .version_id = 1,
 503    .minimum_version_id = 1,
 504    .needed = spapr_drc_needed,
 505    .fields  = (VMStateField []) {
 506        VMSTATE_UINT32(state, SpaprDrc),
 507        VMSTATE_END_OF_LIST()
 508    },
 509    .subsections = (const VMStateDescription * []) {
 510        &vmstate_spapr_drc_unplug_requested,
 511        NULL
 512    }
 513};
 514
 515static void drc_realize(DeviceState *d, Error **errp)
 516{
 517    SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
 518    Object *root_container;
 519    gchar *link_name;
 520    const char *child_name;
 521
 522    trace_spapr_drc_realize(spapr_drc_index(drc));
 523    /* NOTE: we do this as part of realize/unrealize due to the fact
 524     * that the guest will communicate with the DRC via RTAS calls
 525     * referencing the global DRC index. By unlinking the DRC
 526     * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
 527     * inaccessible by the guest, since lookups rely on this path
 528     * existing in the composition tree
 529     */
 530    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
 531    link_name = g_strdup_printf("%x", spapr_drc_index(drc));
 532    child_name = object_get_canonical_path_component(OBJECT(drc));
 533    trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
 534    object_property_add_alias(root_container, link_name,
 535                              drc->owner, child_name);
 536    g_free(link_name);
 537    vmstate_register(VMSTATE_IF(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
 538                     drc);
 539    trace_spapr_drc_realize_complete(spapr_drc_index(drc));
 540}
 541
 542static void drc_unrealize(DeviceState *d)
 543{
 544    SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
 545    Object *root_container;
 546    gchar *name;
 547
 548    trace_spapr_drc_unrealize(spapr_drc_index(drc));
 549    vmstate_unregister(VMSTATE_IF(drc), &vmstate_spapr_drc, drc);
 550    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
 551    name = g_strdup_printf("%x", spapr_drc_index(drc));
 552    object_property_del(root_container, name);
 553    g_free(name);
 554}
 555
 556SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
 557                                         uint32_t id)
 558{
 559    SpaprDrc *drc = SPAPR_DR_CONNECTOR(object_new(type));
 560    char *prop_name;
 561
 562    drc->id = id;
 563    drc->owner = owner;
 564    prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
 565                                spapr_drc_index(drc));
 566    object_property_add_child(owner, prop_name, OBJECT(drc));
 567    object_unref(OBJECT(drc));
 568    qdev_realize(DEVICE(drc), NULL, NULL);
 569    g_free(prop_name);
 570
 571    return drc;
 572}
 573
 574static void spapr_dr_connector_instance_init(Object *obj)
 575{
 576    SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
 577    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 578
 579    object_property_add_uint32_ptr(obj, "id", &drc->id, OBJ_PROP_FLAG_READ);
 580    object_property_add(obj, "index", "uint32", prop_get_index,
 581                        NULL, NULL, NULL);
 582    object_property_add(obj, "fdt", "struct", prop_get_fdt,
 583                        NULL, NULL, NULL);
 584    drc->state = drck->empty_state;
 585}
 586
 587static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
 588{
 589    DeviceClass *dk = DEVICE_CLASS(k);
 590
 591    dk->realize = drc_realize;
 592    dk->unrealize = drc_unrealize;
 593    /*
 594     * Reason: DR connector needs to be wired to either the machine or to a
 595     * PHB in spapr_dr_connector_new().
 596     */
 597    dk->user_creatable = false;
 598}
 599
 600static bool drc_physical_needed(void *opaque)
 601{
 602    SpaprDrcPhysical *drcp = (SpaprDrcPhysical *)opaque;
 603    SpaprDrc *drc = SPAPR_DR_CONNECTOR(drcp);
 604
 605    if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
 606        || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
 607        return false;
 608    }
 609    return true;
 610}
 611
 612static const VMStateDescription vmstate_spapr_drc_physical = {
 613    .name = "spapr_drc/physical",
 614    .version_id = 1,
 615    .minimum_version_id = 1,
 616    .needed = drc_physical_needed,
 617    .fields  = (VMStateField []) {
 618        VMSTATE_UINT32(dr_indicator, SpaprDrcPhysical),
 619        VMSTATE_END_OF_LIST()
 620    }
 621};
 622
 623static void drc_physical_reset(void *opaque)
 624{
 625    SpaprDrc *drc = SPAPR_DR_CONNECTOR(opaque);
 626    SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
 627
 628    if (drc->dev) {
 629        drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
 630    } else {
 631        drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
 632    }
 633}
 634
 635static void realize_physical(DeviceState *d, Error **errp)
 636{
 637    SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
 638    Error *local_err = NULL;
 639
 640    drc_realize(d, &local_err);
 641    if (local_err) {
 642        error_propagate(errp, local_err);
 643        return;
 644    }
 645
 646    vmstate_register(VMSTATE_IF(drcp),
 647                     spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
 648                     &vmstate_spapr_drc_physical, drcp);
 649    qemu_register_reset(drc_physical_reset, drcp);
 650}
 651
 652static void unrealize_physical(DeviceState *d)
 653{
 654    SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
 655
 656    drc_unrealize(d);
 657    vmstate_unregister(VMSTATE_IF(drcp), &vmstate_spapr_drc_physical, drcp);
 658    qemu_unregister_reset(drc_physical_reset, drcp);
 659}
 660
 661static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
 662{
 663    DeviceClass *dk = DEVICE_CLASS(k);
 664    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
 665
 666    dk->realize = realize_physical;
 667    dk->unrealize = unrealize_physical;
 668    drck->dr_entity_sense = physical_entity_sense;
 669    drck->isolate = drc_isolate_physical;
 670    drck->unisolate = drc_unisolate_physical;
 671    drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
 672    drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
 673}
 674
 675static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
 676{
 677    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
 678
 679    drck->dr_entity_sense = logical_entity_sense;
 680    drck->isolate = drc_isolate_logical;
 681    drck->unisolate = drc_unisolate_logical;
 682    drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
 683    drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
 684}
 685
 686static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
 687{
 688    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
 689
 690    drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
 691    drck->typename = "CPU";
 692    drck->drc_name_prefix = "CPU ";
 693    drck->release = spapr_core_release;
 694    drck->dt_populate = spapr_core_dt_populate;
 695}
 696
 697static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
 698{
 699    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
 700
 701    drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
 702    drck->typename = "28";
 703    drck->drc_name_prefix = "C";
 704    drck->release = spapr_phb_remove_pci_device_cb;
 705    drck->dt_populate = spapr_pci_dt_populate;
 706}
 707
 708static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
 709{
 710    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
 711
 712    drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
 713    drck->typename = "MEM";
 714    drck->drc_name_prefix = "LMB ";
 715    drck->release = spapr_lmb_release;
 716    drck->dt_populate = spapr_lmb_dt_populate;
 717}
 718
 719static void spapr_drc_phb_class_init(ObjectClass *k, void *data)
 720{
 721    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
 722
 723    drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB;
 724    drck->typename = "PHB";
 725    drck->drc_name_prefix = "PHB ";
 726    drck->release = spapr_phb_release;
 727    drck->dt_populate = spapr_phb_dt_populate;
 728}
 729
 730static void spapr_drc_pmem_class_init(ObjectClass *k, void *data)
 731{
 732    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
 733
 734    drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM;
 735    drck->typename = "PMEM";
 736    drck->drc_name_prefix = "PMEM ";
 737    drck->release = NULL;
 738    drck->dt_populate = spapr_pmem_dt_populate;
 739}
 740
 741static const TypeInfo spapr_dr_connector_info = {
 742    .name          = TYPE_SPAPR_DR_CONNECTOR,
 743    .parent        = TYPE_DEVICE,
 744    .instance_size = sizeof(SpaprDrc),
 745    .instance_init = spapr_dr_connector_instance_init,
 746    .class_size    = sizeof(SpaprDrcClass),
 747    .class_init    = spapr_dr_connector_class_init,
 748    .abstract      = true,
 749};
 750
 751static const TypeInfo spapr_drc_physical_info = {
 752    .name          = TYPE_SPAPR_DRC_PHYSICAL,
 753    .parent        = TYPE_SPAPR_DR_CONNECTOR,
 754    .instance_size = sizeof(SpaprDrcPhysical),
 755    .class_init    = spapr_drc_physical_class_init,
 756    .abstract      = true,
 757};
 758
 759static const TypeInfo spapr_drc_logical_info = {
 760    .name          = TYPE_SPAPR_DRC_LOGICAL,
 761    .parent        = TYPE_SPAPR_DR_CONNECTOR,
 762    .class_init    = spapr_drc_logical_class_init,
 763    .abstract      = true,
 764};
 765
 766static const TypeInfo spapr_drc_cpu_info = {
 767    .name          = TYPE_SPAPR_DRC_CPU,
 768    .parent        = TYPE_SPAPR_DRC_LOGICAL,
 769    .class_init    = spapr_drc_cpu_class_init,
 770};
 771
 772static const TypeInfo spapr_drc_pci_info = {
 773    .name          = TYPE_SPAPR_DRC_PCI,
 774    .parent        = TYPE_SPAPR_DRC_PHYSICAL,
 775    .class_init    = spapr_drc_pci_class_init,
 776};
 777
 778static const TypeInfo spapr_drc_lmb_info = {
 779    .name          = TYPE_SPAPR_DRC_LMB,
 780    .parent        = TYPE_SPAPR_DRC_LOGICAL,
 781    .class_init    = spapr_drc_lmb_class_init,
 782};
 783
 784static const TypeInfo spapr_drc_phb_info = {
 785    .name          = TYPE_SPAPR_DRC_PHB,
 786    .parent        = TYPE_SPAPR_DRC_LOGICAL,
 787    .instance_size = sizeof(SpaprDrc),
 788    .class_init    = spapr_drc_phb_class_init,
 789};
 790
 791static const TypeInfo spapr_drc_pmem_info = {
 792    .name          = TYPE_SPAPR_DRC_PMEM,
 793    .parent        = TYPE_SPAPR_DRC_LOGICAL,
 794    .class_init    = spapr_drc_pmem_class_init,
 795};
 796
 797/* helper functions for external users */
 798
 799SpaprDrc *spapr_drc_by_index(uint32_t index)
 800{
 801    Object *obj;
 802    gchar *name;
 803
 804    name = g_strdup_printf("%s/%x", DRC_CONTAINER_PATH, index);
 805    obj = object_resolve_path(name, NULL);
 806    g_free(name);
 807
 808    return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
 809}
 810
 811SpaprDrc *spapr_drc_by_id(const char *type, uint32_t id)
 812{
 813    SpaprDrcClass *drck
 814        = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
 815
 816    return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
 817                              | (id & DRC_INDEX_ID_MASK));
 818}
 819
 820/**
 821 * spapr_dt_drc
 822 *
 823 * @fdt: libfdt device tree
 824 * @path: path in the DT to generate properties
 825 * @owner: parent Object/DeviceState for which to generate DRC
 826 *         descriptions for
 827 * @drc_type_mask: mask of SpaprDrcType values corresponding
 828 *   to the types of DRCs to generate entries for
 829 *
 830 * generate OF properties to describe DRC topology/indices to guests
 831 *
 832 * as documented in PAPR+ v2.1, 13.5.2
 833 */
 834int spapr_dt_drc(void *fdt, int offset, Object *owner, uint32_t drc_type_mask)
 835{
 836    Object *root_container;
 837    ObjectProperty *prop;
 838    ObjectPropertyIterator iter;
 839    uint32_t drc_count = 0;
 840    GArray *drc_indexes, *drc_power_domains;
 841    GString *drc_names, *drc_types;
 842    int ret;
 843
 844    /*
 845     * This should really be only called once per node since it overwrites
 846     * the OF properties if they already exist.
 847     */
 848    g_assert(!fdt_get_property(fdt, offset, "ibm,drc-indexes", NULL));
 849
 850    /* the first entry of each properties is a 32-bit integer encoding
 851     * the number of elements in the array. we won't know this until
 852     * we complete the iteration through all the matching DRCs, but
 853     * reserve the space now and set the offsets accordingly so we
 854     * can fill them in later.
 855     */
 856    drc_indexes = g_array_new(false, true, sizeof(uint32_t));
 857    drc_indexes = g_array_set_size(drc_indexes, 1);
 858    drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
 859    drc_power_domains = g_array_set_size(drc_power_domains, 1);
 860    drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
 861    drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
 862
 863    /* aliases for all DRConnector objects will be rooted in QOM
 864     * composition tree at DRC_CONTAINER_PATH
 865     */
 866    root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
 867
 868    object_property_iter_init(&iter, root_container);
 869    while ((prop = object_property_iter_next(&iter))) {
 870        Object *obj;
 871        SpaprDrc *drc;
 872        SpaprDrcClass *drck;
 873        char *drc_name = NULL;
 874        uint32_t drc_index, drc_power_domain;
 875
 876        if (!strstart(prop->type, "link<", NULL)) {
 877            continue;
 878        }
 879
 880        obj = object_property_get_link(root_container, prop->name,
 881                                       &error_abort);
 882        drc = SPAPR_DR_CONNECTOR(obj);
 883        drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
 884
 885        if (owner && (drc->owner != owner)) {
 886            continue;
 887        }
 888
 889        if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
 890            continue;
 891        }
 892
 893        drc_count++;
 894
 895        /* ibm,drc-indexes */
 896        drc_index = cpu_to_be32(spapr_drc_index(drc));
 897        g_array_append_val(drc_indexes, drc_index);
 898
 899        /* ibm,drc-power-domains */
 900        drc_power_domain = cpu_to_be32(-1);
 901        g_array_append_val(drc_power_domains, drc_power_domain);
 902
 903        /* ibm,drc-names */
 904        drc_name = spapr_drc_name(drc);
 905        drc_names = g_string_append(drc_names, drc_name);
 906        drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
 907        g_free(drc_name);
 908
 909        /* ibm,drc-types */
 910        drc_types = g_string_append(drc_types, drck->typename);
 911        drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
 912    }
 913
 914    /* now write the drc count into the space we reserved at the
 915     * beginning of the arrays previously
 916     */
 917    *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
 918    *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
 919    *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
 920    *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
 921
 922    ret = fdt_setprop(fdt, offset, "ibm,drc-indexes",
 923                      drc_indexes->data,
 924                      drc_indexes->len * sizeof(uint32_t));
 925    if (ret) {
 926        error_report("Couldn't create ibm,drc-indexes property");
 927        goto out;
 928    }
 929
 930    ret = fdt_setprop(fdt, offset, "ibm,drc-power-domains",
 931                      drc_power_domains->data,
 932                      drc_power_domains->len * sizeof(uint32_t));
 933    if (ret) {
 934        error_report("Couldn't finalize ibm,drc-power-domains property");
 935        goto out;
 936    }
 937
 938    ret = fdt_setprop(fdt, offset, "ibm,drc-names",
 939                      drc_names->str, drc_names->len);
 940    if (ret) {
 941        error_report("Couldn't finalize ibm,drc-names property");
 942        goto out;
 943    }
 944
 945    ret = fdt_setprop(fdt, offset, "ibm,drc-types",
 946                      drc_types->str, drc_types->len);
 947    if (ret) {
 948        error_report("Couldn't finalize ibm,drc-types property");
 949        goto out;
 950    }
 951
 952out:
 953    g_array_free(drc_indexes, true);
 954    g_array_free(drc_power_domains, true);
 955    g_string_free(drc_names, true);
 956    g_string_free(drc_types, true);
 957
 958    return ret;
 959}
 960
 961void spapr_drc_reset_all(SpaprMachineState *spapr)
 962{
 963    Object *drc_container;
 964    ObjectProperty *prop;
 965    ObjectPropertyIterator iter;
 966
 967    drc_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
 968restart:
 969    object_property_iter_init(&iter, drc_container);
 970    while ((prop = object_property_iter_next(&iter))) {
 971        SpaprDrc *drc;
 972
 973        if (!strstart(prop->type, "link<", NULL)) {
 974            continue;
 975        }
 976        drc = SPAPR_DR_CONNECTOR(object_property_get_link(drc_container,
 977                                                          prop->name,
 978                                                          &error_abort));
 979
 980        /*
 981         * This will complete any pending plug/unplug requests.
 982         * In case of a unplugged PHB or PCI bridge, this will
 983         * cause some DRCs to be destroyed and thus potentially
 984         * invalidate the iterator.
 985         */
 986        if (spapr_drc_reset(drc)) {
 987            goto restart;
 988        }
 989    }
 990}
 991
 992/*
 993 * RTAS calls
 994 */
 995
 996static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
 997{
 998    SpaprDrc *drc = spapr_drc_by_index(idx);
 999    SpaprDrcClass *drck;
1000
1001    if (!drc) {
1002        return RTAS_OUT_NO_SUCH_INDICATOR;
1003    }
1004
1005    trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
1006
1007    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1008
1009    switch (state) {
1010    case SPAPR_DR_ISOLATION_STATE_ISOLATED:
1011        return drck->isolate(drc);
1012
1013    case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
1014        return drck->unisolate(drc);
1015
1016    default:
1017        return RTAS_OUT_PARAM_ERROR;
1018    }
1019}
1020
1021static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
1022{
1023    SpaprDrc *drc = spapr_drc_by_index(idx);
1024
1025    if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
1026        return RTAS_OUT_NO_SUCH_INDICATOR;
1027    }
1028
1029    trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
1030
1031    switch (state) {
1032    case SPAPR_DR_ALLOCATION_STATE_USABLE:
1033        return drc_set_usable(drc);
1034
1035    case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
1036        return drc_set_unusable(drc);
1037
1038    default:
1039        return RTAS_OUT_PARAM_ERROR;
1040    }
1041}
1042
1043static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
1044{
1045    SpaprDrc *drc = spapr_drc_by_index(idx);
1046
1047    if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
1048        return RTAS_OUT_NO_SUCH_INDICATOR;
1049    }
1050    if ((state != SPAPR_DR_INDICATOR_INACTIVE)
1051        && (state != SPAPR_DR_INDICATOR_ACTIVE)
1052        && (state != SPAPR_DR_INDICATOR_IDENTIFY)
1053        && (state != SPAPR_DR_INDICATOR_ACTION)) {
1054        return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
1055    }
1056
1057    trace_spapr_drc_set_dr_indicator(idx, state);
1058    SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
1059    return RTAS_OUT_SUCCESS;
1060}
1061
1062static void rtas_set_indicator(PowerPCCPU *cpu, SpaprMachineState *spapr,
1063                               uint32_t token,
1064                               uint32_t nargs, target_ulong args,
1065                               uint32_t nret, target_ulong rets)
1066{
1067    uint32_t type, idx, state;
1068    uint32_t ret = RTAS_OUT_SUCCESS;
1069
1070    if (nargs != 3 || nret != 1) {
1071        ret = RTAS_OUT_PARAM_ERROR;
1072        goto out;
1073    }
1074
1075    type = rtas_ld(args, 0);
1076    idx = rtas_ld(args, 1);
1077    state = rtas_ld(args, 2);
1078
1079    switch (type) {
1080    case RTAS_SENSOR_TYPE_ISOLATION_STATE:
1081        ret = rtas_set_isolation_state(idx, state);
1082        break;
1083    case RTAS_SENSOR_TYPE_DR:
1084        ret = rtas_set_dr_indicator(idx, state);
1085        break;
1086    case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1087        ret = rtas_set_allocation_state(idx, state);
1088        break;
1089    default:
1090        ret = RTAS_OUT_NOT_SUPPORTED;
1091    }
1092
1093out:
1094    rtas_st(rets, 0, ret);
1095}
1096
1097static void rtas_get_sensor_state(PowerPCCPU *cpu, SpaprMachineState *spapr,
1098                                  uint32_t token, uint32_t nargs,
1099                                  target_ulong args, uint32_t nret,
1100                                  target_ulong rets)
1101{
1102    uint32_t sensor_type;
1103    uint32_t sensor_index;
1104    uint32_t sensor_state = 0;
1105    SpaprDrc *drc;
1106    SpaprDrcClass *drck;
1107    uint32_t ret = RTAS_OUT_SUCCESS;
1108
1109    if (nargs != 2 || nret != 2) {
1110        ret = RTAS_OUT_PARAM_ERROR;
1111        goto out;
1112    }
1113
1114    sensor_type = rtas_ld(args, 0);
1115    sensor_index = rtas_ld(args, 1);
1116
1117    if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1118        /* currently only DR-related sensors are implemented */
1119        trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1120                                                        sensor_type);
1121        ret = RTAS_OUT_NOT_SUPPORTED;
1122        goto out;
1123    }
1124
1125    drc = spapr_drc_by_index(sensor_index);
1126    if (!drc) {
1127        trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1128        ret = RTAS_OUT_PARAM_ERROR;
1129        goto out;
1130    }
1131    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1132    sensor_state = drck->dr_entity_sense(drc);
1133
1134out:
1135    rtas_st(rets, 0, ret);
1136    rtas_st(rets, 1, sensor_state);
1137}
1138
1139/* configure-connector work area offsets, int32_t units for field
1140 * indexes, bytes for field offset/len values.
1141 *
1142 * as documented by PAPR+ v2.7, 13.5.3.5
1143 */
1144#define CC_IDX_NODE_NAME_OFFSET 2
1145#define CC_IDX_PROP_NAME_OFFSET 2
1146#define CC_IDX_PROP_LEN 3
1147#define CC_IDX_PROP_DATA_OFFSET 4
1148#define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1149#define CC_WA_LEN 4096
1150
1151static void configure_connector_st(target_ulong addr, target_ulong offset,
1152                                   const void *buf, size_t len)
1153{
1154    cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1155                              buf, MIN(len, CC_WA_LEN - offset));
1156}
1157
1158static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1159                                         SpaprMachineState *spapr,
1160                                         uint32_t token, uint32_t nargs,
1161                                         target_ulong args, uint32_t nret,
1162                                         target_ulong rets)
1163{
1164    uint64_t wa_addr;
1165    uint64_t wa_offset;
1166    uint32_t drc_index;
1167    SpaprDrc *drc;
1168    SpaprDrcClass *drck;
1169    SpaprDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1170    int rc;
1171
1172    if (nargs != 2 || nret != 1) {
1173        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1174        return;
1175    }
1176
1177    wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1178
1179    drc_index = rtas_ld(wa_addr, 0);
1180    drc = spapr_drc_by_index(drc_index);
1181    if (!drc) {
1182        trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1183        rc = RTAS_OUT_PARAM_ERROR;
1184        goto out;
1185    }
1186
1187    if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
1188        && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
1189        && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
1190        && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
1191        /*
1192         * Need to unisolate the device before configuring
1193         * or it should already be in configured state to
1194         * allow configure-connector be called repeatedly.
1195         */
1196        rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1197        goto out;
1198    }
1199
1200    drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1201
1202    /*
1203     * This indicates that the kernel is reconfiguring a LMB due to
1204     * a failed hotunplug. Rollback the DIMM unplug process.
1205     */
1206    if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB &&
1207        drc->unplug_requested) {
1208        spapr_memory_unplug_rollback(spapr, drc->dev);
1209    }
1210
1211    if (!drc->fdt) {
1212        void *fdt;
1213        int fdt_size;
1214
1215        fdt = create_device_tree(&fdt_size);
1216
1217        if (drck->dt_populate(drc, spapr, fdt, &drc->fdt_start_offset,
1218                              NULL)) {
1219            g_free(fdt);
1220            rc = SPAPR_DR_CC_RESPONSE_ERROR;
1221            goto out;
1222        }
1223
1224        drc->fdt = fdt;
1225        drc->ccs_offset = drc->fdt_start_offset;
1226        drc->ccs_depth = 0;
1227    }
1228
1229    do {
1230        uint32_t tag;
1231        const char *name;
1232        const struct fdt_property *prop;
1233        int fdt_offset_next, prop_len;
1234
1235        tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
1236
1237        switch (tag) {
1238        case FDT_BEGIN_NODE:
1239            drc->ccs_depth++;
1240            name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
1241
1242            /* provide the name of the next OF node */
1243            wa_offset = CC_VAL_DATA_OFFSET;
1244            rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1245            configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1246            resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1247            break;
1248        case FDT_END_NODE:
1249            drc->ccs_depth--;
1250            if (drc->ccs_depth == 0) {
1251                uint32_t drc_index = spapr_drc_index(drc);
1252
1253                /* done sending the device tree, move to configured state */
1254                trace_spapr_drc_set_configured(drc_index);
1255                drc->state = drck->ready_state;
1256                /*
1257                 * Ensure that we are able to send the FDT fragment
1258                 * again via configure-connector call if the guest requests.
1259                 */
1260                drc->ccs_offset = drc->fdt_start_offset;
1261                drc->ccs_depth = 0;
1262                fdt_offset_next = drc->fdt_start_offset;
1263                resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1264            } else {
1265                resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1266            }
1267            break;
1268        case FDT_PROP:
1269            prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
1270                                              &prop_len);
1271            name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
1272
1273            /* provide the name of the next OF property */
1274            wa_offset = CC_VAL_DATA_OFFSET;
1275            rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1276            configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1277
1278            /* provide the length and value of the OF property. data gets
1279             * placed immediately after NULL terminator of the OF property's
1280             * name string
1281             */
1282            wa_offset += strlen(name) + 1,
1283            rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1284            rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1285            configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1286            resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1287            break;
1288        case FDT_END:
1289            resp = SPAPR_DR_CC_RESPONSE_ERROR;
1290        default:
1291            /* keep seeking for an actionable tag */
1292            break;
1293        }
1294        if (drc->ccs_offset >= 0) {
1295            drc->ccs_offset = fdt_offset_next;
1296        }
1297    } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1298
1299    rc = resp;
1300out:
1301    rtas_st(rets, 0, rc);
1302}
1303
1304static void spapr_drc_register_types(void)
1305{
1306    type_register_static(&spapr_dr_connector_info);
1307    type_register_static(&spapr_drc_physical_info);
1308    type_register_static(&spapr_drc_logical_info);
1309    type_register_static(&spapr_drc_cpu_info);
1310    type_register_static(&spapr_drc_pci_info);
1311    type_register_static(&spapr_drc_lmb_info);
1312    type_register_static(&spapr_drc_phb_info);
1313    type_register_static(&spapr_drc_pmem_info);
1314
1315    spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1316                        rtas_set_indicator);
1317    spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1318                        rtas_get_sensor_state);
1319    spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1320                        rtas_ibm_configure_connector);
1321}
1322type_init(spapr_drc_register_types)
1323