]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_drc.c
spapr: Abolish DRC set_configured method
[mirror_qemu.git] / hw / ppc / spapr_drc.c
CommitLineData
bbf5c878
MR
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
0d75590d 13#include "qemu/osdep.h"
da34e65c 14#include "qapi/error.h"
4771d756 15#include "cpu.h"
f348b6d1 16#include "qemu/cutils.h"
bbf5c878
MR
17#include "hw/ppc/spapr_drc.h"
18#include "qom/object.h"
19#include "hw/qdev.h"
20#include "qapi/visitor.h"
21#include "qemu/error-report.h"
0cb688d2 22#include "hw/ppc/spapr.h" /* for RTAS return codes */
31834723 23#include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
24ac7755 24#include "trace.h"
bbf5c878
MR
25
26#define DRC_CONTAINER_PATH "/dr-connector"
27#define DRC_INDEX_TYPE_SHIFT 28
627c2ef7 28#define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
bbf5c878 29
b89b3d39
DG
30static sPAPRConfigureConnectorState *spapr_ccs_find(sPAPRMachineState *spapr,
31 uint32_t drc_index)
32{
33 sPAPRConfigureConnectorState *ccs = NULL;
34
35 QTAILQ_FOREACH(ccs, &spapr->ccs_list, next) {
36 if (ccs->drc_index == drc_index) {
37 break;
38 }
39 }
40
41 return ccs;
42}
43
44static void spapr_ccs_add(sPAPRMachineState *spapr,
45 sPAPRConfigureConnectorState *ccs)
46{
47 g_assert(!spapr_ccs_find(spapr, ccs->drc_index));
48 QTAILQ_INSERT_HEAD(&spapr->ccs_list, ccs, next);
49}
50
51static void spapr_ccs_remove(sPAPRMachineState *spapr,
52 sPAPRConfigureConnectorState *ccs)
53{
54 QTAILQ_REMOVE(&spapr->ccs_list, ccs, next);
55 g_free(ccs);
56}
57
bbf5c878
MR
58static sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type)
59{
60 uint32_t shift = 0;
61
62 /* make sure this isn't SPAPR_DR_CONNECTOR_TYPE_ANY, or some
63 * other wonky value.
64 */
65 g_assert(is_power_of_2(type));
66
67 while (type != (1 << shift)) {
68 shift++;
69 }
70 return shift;
71}
72
73static uint32_t get_index(sPAPRDRConnector *drc)
74{
75 /* no set format for a drc index: it only needs to be globally
76 * unique. this is how we encode the DRC type on bare-metal
77 * however, so might as well do that here
78 */
79 return (get_type_shift(drc->type) << DRC_INDEX_TYPE_SHIFT) |
80 (drc->id & DRC_INDEX_ID_MASK);
81}
82
0cb688d2
MR
83static uint32_t set_isolation_state(sPAPRDRConnector *drc,
84 sPAPRDRIsolationState state)
bbf5c878
MR
85{
86 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
87
24ac7755 88 trace_spapr_drc_set_isolation_state(get_index(drc), state);
bbf5c878 89
9d1852ce 90 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
b12227af 91 /* cannot unisolate a non-existent resource, and, or resources
9d1852ce
MR
92 * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5)
93 */
94 if (!drc->dev ||
95 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
96 return RTAS_OUT_NO_SUCH_INDICATOR;
97 }
98 }
99
cf632463
BR
100 /*
101 * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
102 * belong to a DIMM device that is marked for removal.
103 *
104 * Currently the guest userspace tool drmgr that drives the memory
105 * hotplug/unplug will just try to remove a set of 'removable' LMBs
106 * in response to a hot unplug request that is based on drc-count.
107 * If the LMB being removed doesn't belong to a DIMM device that is
108 * actually being unplugged, fail the isolation request here.
109 */
110 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_LMB) {
111 if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
112 !drc->awaiting_release) {
113 return RTAS_OUT_HW_ERROR;
114 }
115 }
116
bbf5c878
MR
117 drc->isolation_state = state;
118
119 if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
120 /* if we're awaiting release, but still in an unconfigured state,
121 * it's likely the guest is still in the process of configuring
122 * the device and is transitioning the devices to an ISOLATED
123 * state as a part of that process. so we only complete the
124 * removal when this transition happens for a device in a
125 * configured state, as suggested by the state diagram from
126 * PAPR+ 2.7, 13.4
127 */
128 if (drc->awaiting_release) {
129 if (drc->configured) {
24ac7755 130 trace_spapr_drc_set_isolation_state_finalizing(get_index(drc));
31834723 131 drck->detach(drc, DEVICE(drc->dev), NULL);
bbf5c878 132 } else {
24ac7755 133 trace_spapr_drc_set_isolation_state_deferring(get_index(drc));
bbf5c878
MR
134 }
135 }
136 drc->configured = false;
137 }
138
0cb688d2 139 return RTAS_OUT_SUCCESS;
bbf5c878
MR
140}
141
0cb688d2
MR
142static uint32_t set_indicator_state(sPAPRDRConnector *drc,
143 sPAPRDRIndicatorState state)
bbf5c878 144{
24ac7755 145 trace_spapr_drc_set_indicator_state(get_index(drc), state);
bbf5c878 146 drc->indicator_state = state;
0cb688d2 147 return RTAS_OUT_SUCCESS;
bbf5c878
MR
148}
149
0cb688d2
MR
150static uint32_t set_allocation_state(sPAPRDRConnector *drc,
151 sPAPRDRAllocationState state)
bbf5c878
MR
152{
153 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
154
24ac7755 155 trace_spapr_drc_set_allocation_state(get_index(drc), state);
bbf5c878 156
9d1852ce
MR
157 if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
158 /* if there's no resource/device associated with the DRC, there's
159 * no way for us to put it in an allocation state consistent with
160 * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
161 * result in an RTAS return code of -3 / "no such indicator"
162 */
163 if (!drc->dev) {
164 return RTAS_OUT_NO_SUCH_INDICATOR;
165 }
fe6824d1
LV
166 if (drc->awaiting_release && drc->awaiting_allocation) {
167 /* kernel is acknowledging a previous hotplug event
168 * while we are already removing it.
169 * it's safe to ignore awaiting_allocation here since we know the
170 * situation is predicated on the guest either already having done
171 * so (boot-time hotplug), or never being able to acquire in the
172 * first place (hotplug followed by immediate unplug).
173 */
174 drc->awaiting_allocation_skippable = true;
175 return RTAS_OUT_NO_SUCH_INDICATOR;
176 }
9d1852ce
MR
177 }
178
bbf5c878
MR
179 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
180 drc->allocation_state = state;
181 if (drc->awaiting_release &&
182 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
24ac7755 183 trace_spapr_drc_set_allocation_state_finalizing(get_index(drc));
31834723 184 drck->detach(drc, DEVICE(drc->dev), NULL);
aab99135
BR
185 } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) {
186 drc->awaiting_allocation = false;
bbf5c878
MR
187 }
188 }
0cb688d2 189 return RTAS_OUT_SUCCESS;
bbf5c878
MR
190}
191
192static uint32_t get_type(sPAPRDRConnector *drc)
193{
194 return drc->type;
195}
196
197static const char *get_name(sPAPRDRConnector *drc)
198{
199 return drc->name;
200}
201
f40eb921
MR
202/* has the guest been notified of device attachment? */
203static void set_signalled(sPAPRDRConnector *drc)
204{
205 drc->signalled = true;
206}
207
bbf5c878
MR
208/*
209 * dr-entity-sense sensor value
210 * returned via get-sensor-state RTAS calls
211 * as expected by state diagram in PAPR+ 2.7, 13.4
212 * based on the current allocation/indicator/power states
213 * for the DR connector.
214 */
0cb688d2 215static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state)
bbf5c878 216{
bbf5c878
MR
217 if (drc->dev) {
218 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
219 drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
220 /* for logical DR, we return a state of UNUSABLE
221 * iff the allocation state UNUSABLE.
222 * Otherwise, report the state as USABLE/PRESENT,
223 * as we would for PCI.
224 */
0cb688d2 225 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
bbf5c878
MR
226 } else {
227 /* this assumes all PCI devices are assigned to
228 * a 'live insertion' power domain, where QEMU
229 * manages power state automatically as opposed
230 * to the guest. present, non-PCI resources are
231 * unaffected by power state.
232 */
0cb688d2 233 *state = SPAPR_DR_ENTITY_SENSE_PRESENT;
bbf5c878
MR
234 }
235 } else {
236 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
237 /* PCI devices, and only PCI devices, use EMPTY
238 * in cases where we'd otherwise use UNUSABLE
239 */
0cb688d2 240 *state = SPAPR_DR_ENTITY_SENSE_EMPTY;
bbf5c878 241 } else {
0cb688d2 242 *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE;
bbf5c878
MR
243 }
244 }
245
24ac7755 246 trace_spapr_drc_entity_sense(get_index(drc), *state);
0cb688d2 247 return RTAS_OUT_SUCCESS;
bbf5c878
MR
248}
249
d7bce999
EB
250static void prop_get_index(Object *obj, Visitor *v, const char *name,
251 void *opaque, Error **errp)
bbf5c878
MR
252{
253 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
254 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
255 uint32_t value = (uint32_t)drck->get_index(drc);
51e72bc1 256 visit_type_uint32(v, name, &value, errp);
bbf5c878
MR
257}
258
d7bce999
EB
259static void prop_get_type(Object *obj, Visitor *v, const char *name,
260 void *opaque, Error **errp)
bbf5c878
MR
261{
262 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
263 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
264 uint32_t value = (uint32_t)drck->get_type(drc);
51e72bc1 265 visit_type_uint32(v, name, &value, errp);
bbf5c878
MR
266}
267
268static char *prop_get_name(Object *obj, Error **errp)
269{
270 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
271 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
272 return g_strdup(drck->get_name(drc));
273}
274
d7bce999
EB
275static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name,
276 void *opaque, Error **errp)
bbf5c878
MR
277{
278 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
279 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
0cb688d2
MR
280 uint32_t value;
281
282 drck->entity_sense(drc, &value);
51e72bc1 283 visit_type_uint32(v, name, &value, errp);
bbf5c878
MR
284}
285
d7bce999
EB
286static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
287 void *opaque, Error **errp)
bbf5c878
MR
288{
289 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
c75304a1 290 Error *err = NULL;
bbf5c878
MR
291 int fdt_offset_next, fdt_offset, fdt_depth;
292 void *fdt;
293
294 if (!drc->fdt) {
a543a554 295 visit_type_null(v, NULL, errp);
bbf5c878
MR
296 return;
297 }
298
299 fdt = drc->fdt;
300 fdt_offset = drc->fdt_start_offset;
301 fdt_depth = 0;
302
303 do {
304 const char *name = NULL;
305 const struct fdt_property *prop = NULL;
306 int prop_len = 0, name_len = 0;
307 uint32_t tag;
308
309 tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
310 switch (tag) {
311 case FDT_BEGIN_NODE:
312 fdt_depth++;
313 name = fdt_get_name(fdt, fdt_offset, &name_len);
337283df 314 visit_start_struct(v, name, NULL, 0, &err);
c75304a1
MA
315 if (err) {
316 error_propagate(errp, err);
317 return;
318 }
bbf5c878
MR
319 break;
320 case FDT_END_NODE:
321 /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
322 g_assert(fdt_depth > 0);
15c2f669 323 visit_check_struct(v, &err);
1158bb2a 324 visit_end_struct(v, NULL);
c75304a1
MA
325 if (err) {
326 error_propagate(errp, err);
327 return;
328 }
bbf5c878
MR
329 fdt_depth--;
330 break;
331 case FDT_PROP: {
332 int i;
333 prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
334 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
d9f62dde 335 visit_start_list(v, name, NULL, 0, &err);
c75304a1
MA
336 if (err) {
337 error_propagate(errp, err);
338 return;
339 }
bbf5c878 340 for (i = 0; i < prop_len; i++) {
51e72bc1 341 visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err);
c75304a1
MA
342 if (err) {
343 error_propagate(errp, err);
344 return;
345 }
346 }
a4a1c70d 347 visit_check_list(v, &err);
1158bb2a 348 visit_end_list(v, NULL);
a4a1c70d
MA
349 if (err) {
350 error_propagate(errp, err);
351 return;
352 }
bbf5c878
MR
353 break;
354 }
355 default:
356 error_setg(&error_abort, "device FDT in unexpected state: %d", tag);
357 }
358 fdt_offset = fdt_offset_next;
359 } while (fdt_depth != 0);
360}
361
362static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt,
363 int fdt_start_offset, bool coldplug, Error **errp)
364{
24ac7755 365 trace_spapr_drc_attach(get_index(drc));
bbf5c878
MR
366
367 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
368 error_setg(errp, "an attached device is still awaiting release");
369 return;
370 }
371 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
372 g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE);
373 }
374 g_assert(fdt || coldplug);
375
376 /* NOTE: setting initial isolation state to UNISOLATED means we can't
377 * detach unless guest has a userspace/kernel that moves this state
378 * back to ISOLATED in response to an unplug event, or this is done
379 * manually by the admin prior. if we force things while the guest
380 * may be accessing the device, we can easily crash the guest, so we
381 * we defer completion of removal in such cases to the reset() hook.
382 */
383 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
384 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED;
385 }
386 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE;
387
388 drc->dev = d;
389 drc->fdt = fdt;
390 drc->fdt_start_offset = fdt_start_offset;
785652dc 391 drc->configured = coldplug;
df18b2db
MR
392 /* 'logical' DR resources such as memory/cpus are in some cases treated
393 * as a pool of resources from which the guest is free to choose from
394 * based on only a count. for resources that can be assigned in this
395 * fashion, we must assume the resource is signalled immediately
396 * since a single hotplug request might make an arbitrary number of
397 * such attached resources available to the guest, as opposed to
398 * 'physical' DR resources such as PCI where each device/resource is
399 * signalled individually.
400 */
401 drc->signalled = (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI)
402 ? true : coldplug;
bbf5c878 403
aab99135
BR
404 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) {
405 drc->awaiting_allocation = true;
406 }
407
bbf5c878
MR
408 object_property_add_link(OBJECT(drc), "device",
409 object_get_typename(OBJECT(drc->dev)),
410 (Object **)(&drc->dev),
411 NULL, 0, NULL);
412}
413
31834723 414static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp)
bbf5c878 415{
24ac7755 416 trace_spapr_drc_detach(get_index(drc));
bbf5c878 417
f40eb921
MR
418 /* if we've signalled device presence to the guest, or if the guest
419 * has gone ahead and configured the device (via manually-executed
420 * device add via drmgr in guest, namely), we need to wait
421 * for the guest to quiesce the device before completing detach.
422 * Otherwise, we can assume the guest hasn't seen it and complete the
423 * detach immediately. Note that there is a small race window
424 * just before, or during, configuration, which is this context
425 * refers mainly to fetching the device tree via RTAS.
426 * During this window the device access will be arbitrated by
427 * associated DRC, which will simply fail the RTAS calls as invalid.
428 * This is recoverable within guest and current implementations of
429 * drmgr should be able to cope.
430 */
431 if (!drc->signalled && !drc->configured) {
432 /* if the guest hasn't seen the device we can't rely on it to
433 * set it back to an isolated state via RTAS, so do it here manually
434 */
435 drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED;
436 }
437
bbf5c878 438 if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) {
24ac7755 439 trace_spapr_drc_awaiting_isolated(get_index(drc));
bbf5c878
MR
440 drc->awaiting_release = true;
441 return;
442 }
443
444 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
445 drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) {
24ac7755 446 trace_spapr_drc_awaiting_unusable(get_index(drc));
bbf5c878
MR
447 drc->awaiting_release = true;
448 return;
449 }
450
aab99135 451 if (drc->awaiting_allocation) {
fe6824d1
LV
452 if (!drc->awaiting_allocation_skippable) {
453 drc->awaiting_release = true;
454 trace_spapr_drc_awaiting_allocation(get_index(drc));
455 return;
456 }
aab99135
BR
457 }
458
bbf5c878
MR
459 drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE;
460
31834723
DHB
461 /* Calling release callbacks based on drc->type. */
462 switch (drc->type) {
463 case SPAPR_DR_CONNECTOR_TYPE_CPU:
464 spapr_core_release(drc->dev);
465 break;
466 case SPAPR_DR_CONNECTOR_TYPE_PCI:
467 spapr_phb_remove_pci_device_cb(drc->dev);
468 break;
469 case SPAPR_DR_CONNECTOR_TYPE_LMB:
470 spapr_lmb_release(drc->dev);
471 break;
472 case SPAPR_DR_CONNECTOR_TYPE_PHB:
473 case SPAPR_DR_CONNECTOR_TYPE_VIO:
474 default:
475 g_assert(false);
bbf5c878
MR
476 }
477
478 drc->awaiting_release = false;
fe6824d1 479 drc->awaiting_allocation_skippable = false;
bbf5c878
MR
480 g_free(drc->fdt);
481 drc->fdt = NULL;
482 drc->fdt_start_offset = 0;
483 object_property_del(OBJECT(drc), "device", NULL);
484 drc->dev = NULL;
bbf5c878
MR
485}
486
487static bool release_pending(sPAPRDRConnector *drc)
488{
489 return drc->awaiting_release;
490}
491
492static void reset(DeviceState *d)
493{
494 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
495 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
f40eb921 496 sPAPRDREntitySense state;
bbf5c878 497
24ac7755 498 trace_spapr_drc_reset(drck->get_index(drc));
bbf5c878
MR
499 /* immediately upon reset we can safely assume DRCs whose devices
500 * are pending removal can be safely removed, and that they will
501 * subsequently be left in an ISOLATED state. move the DRC to this
502 * state in these cases (which will in turn complete any pending
503 * device removals)
504 */
505 if (drc->awaiting_release) {
506 drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED);
507 /* generally this should also finalize the removal, but if the device
508 * hasn't yet been configured we normally defer removal under the
509 * assumption that this transition is taking place as part of device
510 * configuration. so check if we're still waiting after this, and
511 * force removal if we are
512 */
513 if (drc->awaiting_release) {
31834723 514 drck->detach(drc, DEVICE(drc->dev), NULL);
bbf5c878
MR
515 }
516
517 /* non-PCI devices may be awaiting a transition to UNUSABLE */
518 if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI &&
519 drc->awaiting_release) {
520 drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE);
521 }
522 }
f40eb921
MR
523
524 drck->entity_sense(drc, &state);
525 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
526 drck->set_signalled(drc);
527 }
bbf5c878
MR
528}
529
a50919dd
DHB
530static bool spapr_drc_needed(void *opaque)
531{
532 sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque;
533 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
534 bool rc = false;
535 sPAPRDREntitySense value;
536 drck->entity_sense(drc, &value);
537
538 /* If no dev is plugged in there is no need to migrate the DRC state */
539 if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) {
540 return false;
541 }
542
543 /*
544 * If there is dev plugged in, we need to migrate the DRC state when
545 * it is different from cold-plugged state
546 */
547 switch (drc->type) {
548 case SPAPR_DR_CONNECTOR_TYPE_PCI:
549 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) &&
550 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) &&
551 drc->configured && drc->signalled && !drc->awaiting_release);
552 break;
553 case SPAPR_DR_CONNECTOR_TYPE_CPU:
554 case SPAPR_DR_CONNECTOR_TYPE_LMB:
555 rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) &&
556 (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) &&
557 drc->configured && drc->signalled && !drc->awaiting_release);
558 break;
559 case SPAPR_DR_CONNECTOR_TYPE_PHB:
560 case SPAPR_DR_CONNECTOR_TYPE_VIO:
561 default:
562 g_assert(false);
563 }
564 return rc;
565}
566
567static const VMStateDescription vmstate_spapr_drc = {
568 .name = "spapr_drc",
569 .version_id = 1,
570 .minimum_version_id = 1,
571 .needed = spapr_drc_needed,
572 .fields = (VMStateField []) {
573 VMSTATE_UINT32(isolation_state, sPAPRDRConnector),
574 VMSTATE_UINT32(allocation_state, sPAPRDRConnector),
575 VMSTATE_UINT32(indicator_state, sPAPRDRConnector),
576 VMSTATE_BOOL(configured, sPAPRDRConnector),
577 VMSTATE_BOOL(awaiting_release, sPAPRDRConnector),
578 VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector),
579 VMSTATE_BOOL(signalled, sPAPRDRConnector),
580 VMSTATE_END_OF_LIST()
581 }
582};
583
bbf5c878
MR
584static void realize(DeviceState *d, Error **errp)
585{
586 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
587 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
588 Object *root_container;
589 char link_name[256];
590 gchar *child_name;
591 Error *err = NULL;
592
24ac7755 593 trace_spapr_drc_realize(drck->get_index(drc));
bbf5c878
MR
594 /* NOTE: we do this as part of realize/unrealize due to the fact
595 * that the guest will communicate with the DRC via RTAS calls
596 * referencing the global DRC index. By unlinking the DRC
597 * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
598 * inaccessible by the guest, since lookups rely on this path
599 * existing in the composition tree
600 */
601 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
602 snprintf(link_name, sizeof(link_name), "%x", drck->get_index(drc));
603 child_name = object_get_canonical_path_component(OBJECT(drc));
24ac7755 604 trace_spapr_drc_realize_child(drck->get_index(drc), child_name);
bbf5c878
MR
605 object_property_add_alias(root_container, link_name,
606 drc->owner, child_name, &err);
607 if (err) {
4fffeb5e 608 error_report_err(err);
bbf5c878
MR
609 object_unref(OBJECT(drc));
610 }
586d2142 611 g_free(child_name);
a50919dd
DHB
612 vmstate_register(DEVICE(drc), drck->get_index(drc), &vmstate_spapr_drc,
613 drc);
24ac7755 614 trace_spapr_drc_realize_complete(drck->get_index(drc));
bbf5c878
MR
615}
616
617static void unrealize(DeviceState *d, Error **errp)
618{
619 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d);
620 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
621 Object *root_container;
622 char name[256];
623 Error *err = NULL;
624
24ac7755 625 trace_spapr_drc_unrealize(drck->get_index(drc));
bbf5c878
MR
626 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
627 snprintf(name, sizeof(name), "%x", drck->get_index(drc));
628 object_property_del(root_container, name, &err);
629 if (err) {
4fffeb5e 630 error_report_err(err);
bbf5c878
MR
631 object_unref(OBJECT(drc));
632 }
633}
634
635sPAPRDRConnector *spapr_dr_connector_new(Object *owner,
636 sPAPRDRConnectorType type,
637 uint32_t id)
638{
639 sPAPRDRConnector *drc =
640 SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR));
94649d42 641 char *prop_name;
bbf5c878
MR
642
643 g_assert(type);
644
645 drc->type = type;
646 drc->id = id;
647 drc->owner = owner;
94649d42
DG
648 prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", get_index(drc));
649 object_property_add_child(owner, prop_name, OBJECT(drc), NULL);
bbf5c878 650 object_property_set_bool(OBJECT(drc), true, "realized", NULL);
94649d42 651 g_free(prop_name);
bbf5c878
MR
652
653 /* human-readable name for a DRC to encode into the DT
654 * description. this is mainly only used within a guest in place
655 * of the unique DRC index.
656 *
657 * in the case of VIO/PCI devices, it corresponds to a
658 * "location code" that maps a logical device/function (DRC index)
659 * to a physical (or virtual in the case of VIO) location in the
660 * system by chaining together the "location label" for each
661 * encapsulating component.
662 *
663 * since this is more to do with diagnosing physical hardware
664 * issues than guest compatibility, we choose location codes/DRC
665 * names that adhere to the documented format, but avoid encoding
666 * the entire topology information into the label/code, instead
667 * just using the location codes based on the labels for the
668 * endpoints (VIO/PCI adaptor connectors), which is basically
669 * just "C" followed by an integer ID.
670 *
671 * DRC names as documented by PAPR+ v2.7, 13.5.2.4
672 * location codes as documented by PAPR+ v2.7, 12.3.1.5
673 */
674 switch (drc->type) {
675 case SPAPR_DR_CONNECTOR_TYPE_CPU:
676 drc->name = g_strdup_printf("CPU %d", id);
677 break;
678 case SPAPR_DR_CONNECTOR_TYPE_PHB:
679 drc->name = g_strdup_printf("PHB %d", id);
680 break;
681 case SPAPR_DR_CONNECTOR_TYPE_VIO:
682 case SPAPR_DR_CONNECTOR_TYPE_PCI:
683 drc->name = g_strdup_printf("C%d", id);
684 break;
685 case SPAPR_DR_CONNECTOR_TYPE_LMB:
686 drc->name = g_strdup_printf("LMB %d", id);
687 break;
688 default:
689 g_assert(false);
690 }
691
692 /* PCI slot always start in a USABLE state, and stay there */
693 if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) {
694 drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE;
695 }
696
697 return drc;
698}
699
700static void spapr_dr_connector_instance_init(Object *obj)
701{
702 sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj);
703
704 object_property_add_uint32_ptr(obj, "isolation-state",
705 &drc->isolation_state, NULL);
706 object_property_add_uint32_ptr(obj, "indicator-state",
707 &drc->indicator_state, NULL);
708 object_property_add_uint32_ptr(obj, "allocation-state",
709 &drc->allocation_state, NULL);
710 object_property_add_uint32_ptr(obj, "id", &drc->id, NULL);
711 object_property_add(obj, "index", "uint32", prop_get_index,
712 NULL, NULL, NULL, NULL);
713 object_property_add(obj, "connector_type", "uint32", prop_get_type,
714 NULL, NULL, NULL, NULL);
715 object_property_add_str(obj, "name", prop_get_name, NULL, NULL);
716 object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense,
717 NULL, NULL, NULL, NULL);
718 object_property_add(obj, "fdt", "struct", prop_get_fdt,
719 NULL, NULL, NULL, NULL);
720}
721
722static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
723{
724 DeviceClass *dk = DEVICE_CLASS(k);
725 sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
726
727 dk->reset = reset;
728 dk->realize = realize;
729 dk->unrealize = unrealize;
730 drck->set_isolation_state = set_isolation_state;
731 drck->set_indicator_state = set_indicator_state;
732 drck->set_allocation_state = set_allocation_state;
733 drck->get_index = get_index;
734 drck->get_type = get_type;
735 drck->get_name = get_name;
bbf5c878
MR
736 drck->entity_sense = entity_sense;
737 drck->attach = attach;
738 drck->detach = detach;
739 drck->release_pending = release_pending;
f40eb921 740 drck->set_signalled = set_signalled;
c401ae8c
MA
741 /*
742 * Reason: it crashes FIXME find and document the real reason
743 */
e90f2a8c 744 dk->user_creatable = false;
bbf5c878
MR
745}
746
747static const TypeInfo spapr_dr_connector_info = {
748 .name = TYPE_SPAPR_DR_CONNECTOR,
749 .parent = TYPE_DEVICE,
750 .instance_size = sizeof(sPAPRDRConnector),
751 .instance_init = spapr_dr_connector_instance_init,
752 .class_size = sizeof(sPAPRDRConnectorClass),
753 .class_init = spapr_dr_connector_class_init,
754};
755
bbf5c878
MR
756/* helper functions for external users */
757
758sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index)
759{
760 Object *obj;
761 char name[256];
762
763 snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index);
764 obj = object_resolve_path(name, NULL);
765
766 return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
767}
768
769sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type,
770 uint32_t id)
771{
772 return spapr_dr_connector_by_index(
773 (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) |
774 (id & DRC_INDEX_ID_MASK));
775}
e4b798bb
MR
776
777/* generate a string the describes the DRC to encode into the
778 * device tree.
779 *
780 * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1
781 */
782static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type)
783{
784 switch (type) {
785 case SPAPR_DR_CONNECTOR_TYPE_CPU:
786 return "CPU";
787 case SPAPR_DR_CONNECTOR_TYPE_PHB:
788 return "PHB";
789 case SPAPR_DR_CONNECTOR_TYPE_VIO:
790 return "SLOT";
791 case SPAPR_DR_CONNECTOR_TYPE_PCI:
792 return "28";
793 case SPAPR_DR_CONNECTOR_TYPE_LMB:
794 return "MEM";
795 default:
796 g_assert(false);
797 }
798
799 return NULL;
800}
801
802/**
803 * spapr_drc_populate_dt
804 *
805 * @fdt: libfdt device tree
806 * @path: path in the DT to generate properties
807 * @owner: parent Object/DeviceState for which to generate DRC
808 * descriptions for
809 * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding
810 * to the types of DRCs to generate entries for
811 *
812 * generate OF properties to describe DRC topology/indices to guests
813 *
814 * as documented in PAPR+ v2.1, 13.5.2
815 */
816int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner,
817 uint32_t drc_type_mask)
818{
819 Object *root_container;
820 ObjectProperty *prop;
7746abd8 821 ObjectPropertyIterator iter;
e4b798bb
MR
822 uint32_t drc_count = 0;
823 GArray *drc_indexes, *drc_power_domains;
824 GString *drc_names, *drc_types;
825 int ret;
826
827 /* the first entry of each properties is a 32-bit integer encoding
828 * the number of elements in the array. we won't know this until
829 * we complete the iteration through all the matching DRCs, but
830 * reserve the space now and set the offsets accordingly so we
831 * can fill them in later.
832 */
833 drc_indexes = g_array_new(false, true, sizeof(uint32_t));
834 drc_indexes = g_array_set_size(drc_indexes, 1);
835 drc_power_domains = g_array_new(false, true, sizeof(uint32_t));
836 drc_power_domains = g_array_set_size(drc_power_domains, 1);
837 drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
838 drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t));
839
840 /* aliases for all DRConnector objects will be rooted in QOM
841 * composition tree at DRC_CONTAINER_PATH
842 */
843 root_container = container_get(object_get_root(), DRC_CONTAINER_PATH);
844
7746abd8
DB
845 object_property_iter_init(&iter, root_container);
846 while ((prop = object_property_iter_next(&iter))) {
e4b798bb
MR
847 Object *obj;
848 sPAPRDRConnector *drc;
849 sPAPRDRConnectorClass *drck;
850 uint32_t drc_index, drc_power_domain;
851
852 if (!strstart(prop->type, "link<", NULL)) {
853 continue;
854 }
855
856 obj = object_property_get_link(root_container, prop->name, NULL);
857 drc = SPAPR_DR_CONNECTOR(obj);
858 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
859
860 if (owner && (drc->owner != owner)) {
861 continue;
862 }
863
864 if ((drc->type & drc_type_mask) == 0) {
865 continue;
866 }
867
868 drc_count++;
869
870 /* ibm,drc-indexes */
871 drc_index = cpu_to_be32(drck->get_index(drc));
872 g_array_append_val(drc_indexes, drc_index);
873
874 /* ibm,drc-power-domains */
875 drc_power_domain = cpu_to_be32(-1);
876 g_array_append_val(drc_power_domains, drc_power_domain);
877
878 /* ibm,drc-names */
879 drc_names = g_string_append(drc_names, drck->get_name(drc));
880 drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
881
882 /* ibm,drc-types */
883 drc_types = g_string_append(drc_types,
884 spapr_drc_get_type_str(drc->type));
885 drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
886 }
887
888 /* now write the drc count into the space we reserved at the
889 * beginning of the arrays previously
890 */
891 *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
892 *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
893 *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
894 *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
895
896 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes",
897 drc_indexes->data,
898 drc_indexes->len * sizeof(uint32_t));
899 if (ret) {
ce9863b7 900 error_report("Couldn't create ibm,drc-indexes property");
e4b798bb
MR
901 goto out;
902 }
903
904 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains",
905 drc_power_domains->data,
906 drc_power_domains->len * sizeof(uint32_t));
907 if (ret) {
ce9863b7 908 error_report("Couldn't finalize ibm,drc-power-domains property");
e4b798bb
MR
909 goto out;
910 }
911
912 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names",
913 drc_names->str, drc_names->len);
914 if (ret) {
ce9863b7 915 error_report("Couldn't finalize ibm,drc-names property");
e4b798bb
MR
916 goto out;
917 }
918
919 ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types",
920 drc_types->str, drc_types->len);
921 if (ret) {
ce9863b7 922 error_report("Couldn't finalize ibm,drc-types property");
e4b798bb
MR
923 goto out;
924 }
925
926out:
927 g_array_free(drc_indexes, true);
928 g_array_free(drc_power_domains, true);
929 g_string_free(drc_names, true);
930 g_string_free(drc_types, true);
931
932 return ret;
933}
b89b3d39
DG
934
935/*
936 * RTAS calls
937 */
938
939static bool sensor_type_is_dr(uint32_t sensor_type)
940{
941 switch (sensor_type) {
942 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
943 case RTAS_SENSOR_TYPE_DR:
944 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
945 return true;
946 }
947
948 return false;
949}
950
951static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
952 uint32_t token, uint32_t nargs,
953 target_ulong args, uint32_t nret,
954 target_ulong rets)
955{
956 uint32_t sensor_type;
957 uint32_t sensor_index;
958 uint32_t sensor_state;
959 uint32_t ret = RTAS_OUT_SUCCESS;
960 sPAPRDRConnector *drc;
961 sPAPRDRConnectorClass *drck;
962
963 if (nargs != 3 || nret != 1) {
964 ret = RTAS_OUT_PARAM_ERROR;
965 goto out;
966 }
967
968 sensor_type = rtas_ld(args, 0);
969 sensor_index = rtas_ld(args, 1);
970 sensor_state = rtas_ld(args, 2);
971
972 if (!sensor_type_is_dr(sensor_type)) {
973 goto out_unimplemented;
974 }
975
976 /* if this is a DR sensor we can assume sensor_index == drc_index */
977 drc = spapr_dr_connector_by_index(sensor_index);
978 if (!drc) {
979 trace_spapr_rtas_set_indicator_invalid(sensor_index);
980 ret = RTAS_OUT_PARAM_ERROR;
981 goto out;
982 }
983 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
984
985 switch (sensor_type) {
986 case RTAS_SENSOR_TYPE_ISOLATION_STATE:
987 /* if the guest is configuring a device attached to this
988 * DRC, we should reset the configuration state at this
989 * point since it may no longer be reliable (guest released
990 * device and needs to start over, or unplug occurred so
991 * the FDT is no longer valid)
992 */
993 if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
994 sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr,
995 sensor_index);
996 if (ccs) {
997 spapr_ccs_remove(spapr, ccs);
998 }
999 }
1000 ret = drck->set_isolation_state(drc, sensor_state);
1001 break;
1002 case RTAS_SENSOR_TYPE_DR:
1003 ret = drck->set_indicator_state(drc, sensor_state);
1004 break;
1005 case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
1006 ret = drck->set_allocation_state(drc, sensor_state);
1007 break;
1008 default:
1009 goto out_unimplemented;
1010 }
1011
1012out:
1013 rtas_st(rets, 0, ret);
1014 return;
1015
1016out_unimplemented:
1017 /* currently only DR-related sensors are implemented */
1018 trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
1019 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
1020}
1021
1022static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
1023 uint32_t token, uint32_t nargs,
1024 target_ulong args, uint32_t nret,
1025 target_ulong rets)
1026{
1027 uint32_t sensor_type;
1028 uint32_t sensor_index;
1029 uint32_t sensor_state = 0;
1030 sPAPRDRConnector *drc;
1031 sPAPRDRConnectorClass *drck;
1032 uint32_t ret = RTAS_OUT_SUCCESS;
1033
1034 if (nargs != 2 || nret != 2) {
1035 ret = RTAS_OUT_PARAM_ERROR;
1036 goto out;
1037 }
1038
1039 sensor_type = rtas_ld(args, 0);
1040 sensor_index = rtas_ld(args, 1);
1041
1042 if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
1043 /* currently only DR-related sensors are implemented */
1044 trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
1045 sensor_type);
1046 ret = RTAS_OUT_NOT_SUPPORTED;
1047 goto out;
1048 }
1049
1050 drc = spapr_dr_connector_by_index(sensor_index);
1051 if (!drc) {
1052 trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
1053 ret = RTAS_OUT_PARAM_ERROR;
1054 goto out;
1055 }
1056 drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
1057 ret = drck->entity_sense(drc, &sensor_state);
1058
1059out:
1060 rtas_st(rets, 0, ret);
1061 rtas_st(rets, 1, sensor_state);
1062}
1063
1064/* configure-connector work area offsets, int32_t units for field
1065 * indexes, bytes for field offset/len values.
1066 *
1067 * as documented by PAPR+ v2.7, 13.5.3.5
1068 */
1069#define CC_IDX_NODE_NAME_OFFSET 2
1070#define CC_IDX_PROP_NAME_OFFSET 2
1071#define CC_IDX_PROP_LEN 3
1072#define CC_IDX_PROP_DATA_OFFSET 4
1073#define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
1074#define CC_WA_LEN 4096
1075
1076static void configure_connector_st(target_ulong addr, target_ulong offset,
1077 const void *buf, size_t len)
1078{
1079 cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
1080 buf, MIN(len, CC_WA_LEN - offset));
1081}
1082
1083void spapr_ccs_reset_hook(void *opaque)
1084{
1085 sPAPRMachineState *spapr = opaque;
1086 sPAPRConfigureConnectorState *ccs, *ccs_tmp;
1087
1088 QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) {
1089 spapr_ccs_remove(spapr, ccs);
1090 }
1091}
1092
1093static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
1094 sPAPRMachineState *spapr,
1095 uint32_t token, uint32_t nargs,
1096 target_ulong args, uint32_t nret,
1097 target_ulong rets)
1098{
1099 uint64_t wa_addr;
1100 uint64_t wa_offset;
1101 uint32_t drc_index;
1102 sPAPRDRConnector *drc;
b89b3d39
DG
1103 sPAPRConfigureConnectorState *ccs;
1104 sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
1105 int rc;
b89b3d39
DG
1106
1107 if (nargs != 2 || nret != 1) {
1108 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
1109 return;
1110 }
1111
1112 wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
1113
1114 drc_index = rtas_ld(wa_addr, 0);
1115 drc = spapr_dr_connector_by_index(drc_index);
1116 if (!drc) {
1117 trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
1118 rc = RTAS_OUT_PARAM_ERROR;
1119 goto out;
1120 }
1121
88af6ea5 1122 if (!drc->fdt) {
b89b3d39
DG
1123 trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
1124 rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
1125 goto out;
1126 }
1127
1128 ccs = spapr_ccs_find(spapr, drc_index);
1129 if (!ccs) {
1130 ccs = g_new0(sPAPRConfigureConnectorState, 1);
88af6ea5 1131 ccs->fdt_offset = drc->fdt_start_offset;
b89b3d39
DG
1132 ccs->drc_index = drc_index;
1133 spapr_ccs_add(spapr, ccs);
1134 }
1135
1136 do {
1137 uint32_t tag;
1138 const char *name;
1139 const struct fdt_property *prop;
1140 int fdt_offset_next, prop_len;
1141
88af6ea5 1142 tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next);
b89b3d39
DG
1143
1144 switch (tag) {
1145 case FDT_BEGIN_NODE:
1146 ccs->fdt_depth++;
88af6ea5 1147 name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL);
b89b3d39
DG
1148
1149 /* provide the name of the next OF node */
1150 wa_offset = CC_VAL_DATA_OFFSET;
1151 rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
1152 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1153 resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
1154 break;
1155 case FDT_END_NODE:
1156 ccs->fdt_depth--;
1157 if (ccs->fdt_depth == 0) {
4f65ce00 1158 sPAPRDRIsolationState state = drc->isolation_state;
b89b3d39
DG
1159 /* done sending the device tree, don't need to track
1160 * the state anymore
1161 */
4f65ce00
DG
1162 trace_spapr_drc_set_configured(get_index(drc));
1163 if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) {
1164 drc->configured = true;
1165 } else {
1166 /* guest should be not configuring an isolated device */
1167 trace_spapr_drc_set_configured_skipping(get_index(drc));
1168 }
b89b3d39
DG
1169 spapr_ccs_remove(spapr, ccs);
1170 ccs = NULL;
1171 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
1172 } else {
1173 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
1174 }
1175 break;
1176 case FDT_PROP:
88af6ea5 1177 prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset,
b89b3d39 1178 &prop_len);
88af6ea5 1179 name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
b89b3d39
DG
1180
1181 /* provide the name of the next OF property */
1182 wa_offset = CC_VAL_DATA_OFFSET;
1183 rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
1184 configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
1185
1186 /* provide the length and value of the OF property. data gets
1187 * placed immediately after NULL terminator of the OF property's
1188 * name string
1189 */
1190 wa_offset += strlen(name) + 1,
1191 rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
1192 rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
1193 configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
1194 resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
1195 break;
1196 case FDT_END:
1197 resp = SPAPR_DR_CC_RESPONSE_ERROR;
1198 default:
1199 /* keep seeking for an actionable tag */
1200 break;
1201 }
1202 if (ccs) {
1203 ccs->fdt_offset = fdt_offset_next;
1204 }
1205 } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
1206
1207 rc = resp;
1208out:
1209 rtas_st(rets, 0, rc);
1210}
1211
1212static void spapr_drc_register_types(void)
1213{
1214 type_register_static(&spapr_dr_connector_info);
1215
1216 spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
1217 rtas_set_indicator);
1218 spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
1219 rtas_get_sensor_state);
1220 spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
1221 rtas_ibm_configure_connector);
1222}
1223type_init(spapr_drc_register_types)