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