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