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