Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/cxl/cxl-mailbox-utils.c
Line
Count
Source
1
/*
2
 * CXL Utility library for mailbox interface
3
 *
4
 * Copyright(C) 2020 Intel Corporation.
5
 *
6
 * This work is licensed under the terms of the GNU GPL, version 2. See the
7
 * COPYING file in the top-level directory.
8
 */
9
10
#include "qemu/osdep.h"
11
#include <math.h>
12
13
#include "hw/pci/msi.h"
14
#include "hw/pci/msix.h"
15
#include "hw/cxl/cxl.h"
16
#include "hw/cxl/cxl_events.h"
17
#include "hw/cxl/cxl_mailbox.h"
18
#include "hw/cxl/cxl_port.h"
19
#include "hw/pci/pci.h"
20
#include "hw/pci-bridge/cxl_downstream_port.h"
21
#include "hw/pci-bridge/cxl_upstream_port.h"
22
#include "qemu/cutils.h"
23
#include "qemu/host-utils.h"
24
#include "qemu/log.h"
25
#include "qemu/units.h"
26
#include "qemu/uuid.h"
27
#include "system/hostmem.h"
28
#include "qemu/range.h"
29
#include "qapi/qapi-types-cxl.h"
30
31
0
#define CXL_CAPACITY_MULTIPLIER   (256 * MiB)
32
0
#define CXL_DC_EVENT_LOG_SIZE 8
33
0
#define CXL_NUM_TAGS_SUPPORTED 0
34
0
#define CXL_ALERTS_LIFE_USED_WARN_THRESH (1 << 0)
35
0
#define CXL_ALERTS_OVER_TEMP_WARN_THRESH (1 << 1)
36
0
#define CXL_ALERTS_UNDER_TEMP_WARN_THRESH (1 << 2)
37
0
#define CXL_ALERTS_COR_VMEM_ERR_WARN_THRESH (1 << 3)
38
0
#define CXL_ALERTS_COR_PMEM_ERR_WARN_THRESH (1 << 4)
39
40
/*
41
 * How to add a new command, example. The command set FOO, with cmd BAR.
42
 *  1. Add the command set and cmd to the enum.
43
 *     FOO    = 0x7f,
44
 *          #define BAR 0
45
 *  2. Implement the handler
46
 *    static CXLRetCode cmd_foo_bar(struct cxl_cmd *cmd,
47
 *                                  CXLDeviceState *cxl_dstate, uint16_t *len)
48
 *  3. Add the command to the cxl_cmd_set[][]
49
 *    [FOO][BAR] = { "FOO_BAR", cmd_foo_bar, x, y },
50
 *  4. Implement your handler
51
 *     define_mailbox_handler(FOO_BAR) { ... return CXL_MBOX_SUCCESS; }
52
 *
53
 *
54
 *  Writing the handler:
55
 *    The handler will provide the &struct cxl_cmd, the &CXLDeviceState, and the
56
 *    in/out length of the payload. The handler is responsible for consuming the
57
 *    payload from cmd->payload and operating upon it as necessary. It must then
58
 *    fill the output data into cmd->payload (overwriting what was there),
59
 *    setting the length, and returning a valid return code.
60
 *
61
 *  XXX: The handler need not worry about endianness. The payload is read out of
62
 *  a register interface that already deals with it.
63
 */
64
65
enum {
66
    INFOSTAT    = 0x00,
67
        #define IS_IDENTIFY   0x1
68
        #define BACKGROUND_OPERATION_STATUS    0x2
69
        #define GET_RESPONSE_MSG_LIMIT         0x3
70
        #define SET_RESPONSE_MSG_LIMIT         0x4
71
        #define BACKGROUND_OPERATION_ABORT     0x5
72
    EVENTS      = 0x01,
73
        #define GET_RECORDS   0x0
74
        #define CLEAR_RECORDS   0x1
75
        #define GET_INTERRUPT_POLICY   0x2
76
        #define SET_INTERRUPT_POLICY   0x3
77
    FIRMWARE_UPDATE = 0x02,
78
        #define GET_INFO      0x0
79
        #define TRANSFER      0x1
80
        #define ACTIVATE      0x2
81
    TIMESTAMP   = 0x03,
82
        #define GET           0x0
83
        #define SET           0x1
84
    LOGS        = 0x04,
85
        #define GET_SUPPORTED 0x0
86
        #define GET_LOG       0x1
87
    FEATURES    = 0x05,
88
        #define GET_SUPPORTED 0x0
89
        #define GET_FEATURE   0x1
90
        #define SET_FEATURE   0x2
91
    MAINTENANCE = 0x06,
92
        #define PERFORM 0x0
93
    IDENTIFY    = 0x40,
94
        #define MEMORY_DEVICE 0x0
95
    CCLS        = 0x41,
96
        #define GET_PARTITION_INFO     0x0
97
        #define GET_LSA       0x2
98
        #define SET_LSA       0x3
99
    HEALTH_INFO_ALERTS = 0x42,
100
        #define GET_ALERT_CONFIG 0x1
101
        #define SET_ALERT_CONFIG 0x2
102
    SANITIZE    = 0x44,
103
        #define OVERWRITE     0x0
104
        #define SECURE_ERASE  0x1
105
        #define MEDIA_OPERATIONS 0x2
106
    PERSISTENT_MEM = 0x45,
107
        #define GET_SECURITY_STATE     0x0
108
    MEDIA_AND_POISON = 0x43,
109
        #define GET_POISON_LIST        0x0
110
        #define INJECT_POISON          0x1
111
        #define CLEAR_POISON           0x2
112
        #define GET_SCAN_MEDIA_CAPABILITIES 0x3
113
        #define SCAN_MEDIA             0x4
114
        #define GET_SCAN_MEDIA_RESULTS 0x5
115
    DCD_CONFIG  = 0x48,
116
        #define GET_DC_CONFIG          0x0
117
        #define GET_DYN_CAP_EXT_LIST   0x1
118
        #define ADD_DYN_CAP_RSP        0x2
119
        #define RELEASE_DYN_CAP        0x3
120
    PHYSICAL_SWITCH = 0x51,
121
        #define IDENTIFY_SWITCH_DEVICE      0x0
122
        #define GET_PHYSICAL_PORT_STATE     0x1
123
        #define PHYSICAL_PORT_CONTROL       0x2
124
    TUNNEL = 0x53,
125
        #define MANAGEMENT_COMMAND     0x0
126
    FMAPI_DCD_MGMT = 0x56,
127
        #define GET_DCD_INFO    0x0
128
        #define GET_HOST_DC_REGION_CONFIG   0x1
129
        #define SET_DC_REGION_CONFIG        0x2
130
        #define GET_DC_REGION_EXTENT_LIST   0x3
131
        #define INITIATE_DC_ADD             0x4
132
        #define INITIATE_DC_RELEASE         0x5
133
};
134
135
/* CCI Message Format CXL r3.1 Figure 7-19 */
136
typedef struct CXLCCIMessage {
137
    uint8_t category;
138
0
#define CXL_CCI_CAT_REQ 0
139
0
#define CXL_CCI_CAT_RSP 1
140
    uint8_t tag;
141
    uint8_t resv1;
142
    uint8_t command;
143
    uint8_t command_set;
144
    uint8_t pl_length[3];
145
    uint16_t rc;
146
    uint16_t vendor_specific;
147
    uint8_t payload[];
148
} QEMU_PACKED CXLCCIMessage;
149
150
/* This command is only defined to an MLD FM Owned LD or an MHD */
151
static CXLRetCode cmd_tunnel_management_cmd(const struct cxl_cmd *cmd,
152
                                            uint8_t *payload_in,
153
                                            size_t len_in,
154
                                            uint8_t *payload_out,
155
                                            size_t *len_out,
156
                                            CXLCCI *cci)
157
0
{
158
0
    PCIDevice *tunnel_target;
159
0
    CXLCCI *target_cci;
160
0
    struct {
161
0
        uint8_t port_or_ld_id;
162
0
        uint8_t target_type;
163
0
        uint16_t size;
164
0
        CXLCCIMessage ccimessage;
165
0
    } QEMU_PACKED *in;
166
0
    struct {
167
0
        uint16_t resp_len;
168
0
        uint8_t resv[2];
169
0
        CXLCCIMessage ccimessage;
170
0
    } QEMU_PACKED *out;
171
0
    size_t pl_length, length_out;
172
0
    bool bg_started;
173
0
    int rc;
174
175
0
    if (cmd->in < sizeof(*in)) {
176
0
        return CXL_MBOX_INVALID_INPUT;
177
0
    }
178
0
    in = (void *)payload_in;
179
0
    out = (void *)payload_out;
180
181
0
    if (len_in < sizeof(*in)) {
182
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
183
0
    }
184
    /* Enough room for minimum sized message - no payload */
185
0
    if (in->size < sizeof(in->ccimessage)) {
186
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
187
0
    }
188
    /* Length of input payload should be in->size + a wrapping tunnel header */
189
0
    if (in->size != len_in - offsetof(typeof(*out), ccimessage)) {
190
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
191
0
    }
192
0
    if (in->ccimessage.category != CXL_CCI_CAT_REQ) {
193
0
        return CXL_MBOX_INVALID_INPUT;
194
0
    }
195
196
0
    if (in->target_type != 0) {
197
0
        qemu_log_mask(LOG_UNIMP,
198
0
                      "Tunneled Command sent to non existent FM-LD");
199
0
        return CXL_MBOX_INVALID_INPUT;
200
0
    }
201
202
    /*
203
     * Target of a tunnel unfortunately depends on type of CCI readint
204
     * the message.
205
     * If in a switch, then it's the port number.
206
     * If in an MLD it is the ld number.
207
     * If in an MHD target type indicate where we are going.
208
     */
209
0
    if (object_dynamic_cast(OBJECT(cci->d), TYPE_CXL_TYPE3)) {
210
0
        CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
211
0
        if (in->port_or_ld_id != 0) {
212
            /* Only pretending to have one for now! */
213
0
            return CXL_MBOX_INVALID_INPUT;
214
0
        }
215
0
        target_cci = &ct3d->ld0_cci;
216
0
    } else if (object_dynamic_cast(OBJECT(cci->d), TYPE_CXL_USP)) {
217
0
        CXLUpstreamPort *usp = CXL_USP(cci->d);
218
219
0
        tunnel_target = pcie_find_port_by_pn(&PCI_BRIDGE(usp)->sec_bus,
220
0
                                             in->port_or_ld_id);
221
0
        if (!tunnel_target) {
222
0
            return CXL_MBOX_INVALID_INPUT;
223
0
        }
224
0
        tunnel_target =
225
0
            pci_bridge_get_sec_bus(PCI_BRIDGE(tunnel_target))->devices[0];
226
0
        if (!tunnel_target) {
227
0
            return CXL_MBOX_INVALID_INPUT;
228
0
        }
229
0
        if (object_dynamic_cast(OBJECT(tunnel_target), TYPE_CXL_TYPE3)) {
230
0
            CXLType3Dev *ct3d = CXL_TYPE3(tunnel_target);
231
            /* Tunneled VDMs always land on FM Owned LD */
232
0
            target_cci = &ct3d->vdm_fm_owned_ld_mctp_cci;
233
0
        } else {
234
0
            return CXL_MBOX_INVALID_INPUT;
235
0
        }
236
0
    } else {
237
0
        return CXL_MBOX_INVALID_INPUT;
238
0
    }
239
240
0
    pl_length = in->ccimessage.pl_length[2] << 16 |
241
0
        in->ccimessage.pl_length[1] << 8 | in->ccimessage.pl_length[0];
242
0
    rc = cxl_process_cci_message(target_cci,
243
0
                                 in->ccimessage.command_set,
244
0
                                 in->ccimessage.command,
245
0
                                 pl_length, in->ccimessage.payload,
246
0
                                 &length_out, out->ccimessage.payload,
247
0
                                 &bg_started);
248
    /* Payload should be in place. Rest of CCI header and needs filling */
249
0
    out->resp_len = length_out + sizeof(CXLCCIMessage);
250
0
    st24_le_p(out->ccimessage.pl_length, length_out);
251
0
    out->ccimessage.rc = rc;
252
0
    out->ccimessage.category = CXL_CCI_CAT_RSP;
253
0
    out->ccimessage.command = in->ccimessage.command;
254
0
    out->ccimessage.command_set = in->ccimessage.command_set;
255
0
    out->ccimessage.tag = in->ccimessage.tag;
256
0
    *len_out = length_out + sizeof(*out);
257
258
0
    return CXL_MBOX_SUCCESS;
259
0
}
260
261
static CXLRetCode cmd_events_get_records(const struct cxl_cmd *cmd,
262
                                         uint8_t *payload_in, size_t len_in,
263
                                         uint8_t *payload_out, size_t *len_out,
264
                                         CXLCCI *cci)
265
0
{
266
0
    CXLDeviceState *cxlds = &CXL_TYPE3(cci->d)->cxl_dstate;
267
0
    CXLGetEventPayload *pl;
268
0
    uint8_t log_type;
269
0
    int max_recs;
270
271
0
    if (cmd->in < sizeof(log_type)) {
272
0
        return CXL_MBOX_INVALID_INPUT;
273
0
    }
274
275
0
    log_type = payload_in[0];
276
277
0
    pl = (CXLGetEventPayload *)payload_out;
278
279
0
    max_recs = (cxlds->payload_size - CXL_EVENT_PAYLOAD_HDR_SIZE) /
280
0
                CXL_EVENT_RECORD_SIZE;
281
0
    if (max_recs > 0xFFFF) {
282
0
        max_recs = 0xFFFF;
283
0
    }
284
285
0
    return cxl_event_get_records(cxlds, pl, log_type, max_recs, len_out);
286
0
}
287
288
static CXLRetCode cmd_events_clear_records(const struct cxl_cmd *cmd,
289
                                           uint8_t *payload_in,
290
                                           size_t len_in,
291
                                           uint8_t *payload_out,
292
                                           size_t *len_out,
293
                                           CXLCCI *cci)
294
0
{
295
0
    CXLDeviceState *cxlds = &CXL_TYPE3(cci->d)->cxl_dstate;
296
0
    CXLClearEventPayload *pl;
297
298
0
    pl = (CXLClearEventPayload *)payload_in;
299
300
0
    if (len_in < sizeof(*pl) ||
301
0
        len_in < sizeof(*pl) + sizeof(*pl->handle) * pl->nr_recs) {
302
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
303
0
    }
304
305
0
    *len_out = 0;
306
0
    return cxl_event_clear_records(cxlds, pl);
307
0
}
308
309
static CXLRetCode cmd_events_get_interrupt_policy(const struct cxl_cmd *cmd,
310
                                                  uint8_t *payload_in,
311
                                                  size_t len_in,
312
                                                  uint8_t *payload_out,
313
                                                  size_t *len_out,
314
                                                  CXLCCI *cci)
315
0
{
316
0
    CXLDeviceState *cxlds = &CXL_TYPE3(cci->d)->cxl_dstate;
317
0
    CXLEventInterruptPolicy *policy;
318
0
    CXLEventLog *log;
319
320
0
    policy = (CXLEventInterruptPolicy *)payload_out;
321
322
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_INFO];
323
0
    if (log->irq_enabled) {
324
0
        policy->info_settings = CXL_EVENT_INT_SETTING(log->irq_vec);
325
0
    }
326
327
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_WARN];
328
0
    if (log->irq_enabled) {
329
0
        policy->warn_settings = CXL_EVENT_INT_SETTING(log->irq_vec);
330
0
    }
331
332
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_FAIL];
333
0
    if (log->irq_enabled) {
334
0
        policy->failure_settings = CXL_EVENT_INT_SETTING(log->irq_vec);
335
0
    }
336
337
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_FATAL];
338
0
    if (log->irq_enabled) {
339
0
        policy->fatal_settings = CXL_EVENT_INT_SETTING(log->irq_vec);
340
0
    }
341
342
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_DYNAMIC_CAP];
343
0
    if (log->irq_enabled) {
344
        /* Dynamic Capacity borrows the same vector as info */
345
0
        policy->dyn_cap_settings = CXL_INT_MSI_MSIX;
346
0
    }
347
348
0
    *len_out = sizeof(*policy);
349
0
    return CXL_MBOX_SUCCESS;
350
0
}
351
352
static CXLRetCode cmd_events_set_interrupt_policy(const struct cxl_cmd *cmd,
353
                                                  uint8_t *payload_in,
354
                                                  size_t len_in,
355
                                                  uint8_t *payload_out,
356
                                                  size_t *len_out,
357
                                                  CXLCCI *cci)
358
0
{
359
0
    CXLDeviceState *cxlds = &CXL_TYPE3(cci->d)->cxl_dstate;
360
0
    CXLEventInterruptPolicy *policy;
361
0
    CXLEventLog *log;
362
363
0
    if (len_in < CXL_EVENT_INT_SETTING_MIN_LEN) {
364
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
365
0
    }
366
367
0
    policy = (CXLEventInterruptPolicy *)payload_in;
368
369
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_INFO];
370
0
    log->irq_enabled = (policy->info_settings & CXL_EVENT_INT_MODE_MASK) ==
371
0
                        CXL_INT_MSI_MSIX;
372
373
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_WARN];
374
0
    log->irq_enabled = (policy->warn_settings & CXL_EVENT_INT_MODE_MASK) ==
375
0
                        CXL_INT_MSI_MSIX;
376
377
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_FAIL];
378
0
    log->irq_enabled = (policy->failure_settings & CXL_EVENT_INT_MODE_MASK) ==
379
0
                        CXL_INT_MSI_MSIX;
380
381
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_FATAL];
382
0
    log->irq_enabled = (policy->fatal_settings & CXL_EVENT_INT_MODE_MASK) ==
383
0
                        CXL_INT_MSI_MSIX;
384
385
    /* DCD is optional */
386
0
    if (len_in < sizeof(*policy)) {
387
0
        return CXL_MBOX_SUCCESS;
388
0
    }
389
390
0
    log = &cxlds->event_logs[CXL_EVENT_TYPE_DYNAMIC_CAP];
391
0
    log->irq_enabled = (policy->dyn_cap_settings & CXL_EVENT_INT_MODE_MASK) ==
392
0
                        CXL_INT_MSI_MSIX;
393
394
0
    *len_out = 0;
395
0
    return CXL_MBOX_SUCCESS;
396
0
}
397
398
/* CXL r3.1 section 8.2.9.1.1: Identify (Opcode 0001h) */
399
static CXLRetCode cmd_infostat_identify(const struct cxl_cmd *cmd,
400
                                        uint8_t *payload_in,
401
                                        size_t len_in,
402
                                        uint8_t *payload_out,
403
                                        size_t *len_out,
404
                                        CXLCCI *cci)
405
0
{
406
0
    PCIDeviceClass *class = PCI_DEVICE_GET_CLASS(cci->d);
407
0
    struct {
408
0
        uint16_t pcie_vid;
409
0
        uint16_t pcie_did;
410
0
        uint16_t pcie_subsys_vid;
411
0
        uint16_t pcie_subsys_id;
412
0
        uint64_t sn;
413
0
        uint8_t max_message_size;
414
0
        uint8_t component_type;
415
0
    } QEMU_PACKED *is_identify;
416
0
    QEMU_BUILD_BUG_ON(sizeof(*is_identify) != 18);
417
418
0
    is_identify = (void *)payload_out;
419
0
    is_identify->pcie_vid = class->vendor_id;
420
0
    is_identify->pcie_did = class->device_id;
421
0
    if (object_dynamic_cast(OBJECT(cci->d), TYPE_CXL_USP)) {
422
0
        is_identify->sn = CXL_USP(cci->d)->sn;
423
        /* Subsystem info not defined for a USP */
424
0
        is_identify->pcie_subsys_vid = 0;
425
0
        is_identify->pcie_subsys_id = 0;
426
0
        is_identify->component_type = 0x0; /* Switch */
427
0
    } else if (object_dynamic_cast(OBJECT(cci->d), TYPE_CXL_TYPE3)) {
428
0
        PCIDevice *pci_dev = PCI_DEVICE(cci->d);
429
430
0
        is_identify->sn = CXL_TYPE3(cci->d)->sn;
431
        /*
432
         * We can't always use class->subsystem_vendor_id as
433
         * it is not set if the defaults are used.
434
         */
435
0
        is_identify->pcie_subsys_vid =
436
0
            pci_get_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID);
437
0
        is_identify->pcie_subsys_id =
438
0
            pci_get_word(pci_dev->config + PCI_SUBSYSTEM_ID);
439
0
        is_identify->component_type = 0x3; /* Type 3 */
440
0
    }
441
442
0
    is_identify->max_message_size = (uint8_t)log2(cci->payload_max);
443
0
    *len_out = sizeof(*is_identify);
444
0
    return CXL_MBOX_SUCCESS;
445
0
}
446
447
/* CXL r3.1 section 8.2.9.1.3: Get Response Message Limit (Opcode 0003h) */
448
static CXLRetCode cmd_get_response_msg_limit(const struct cxl_cmd *cmd,
449
                                             uint8_t *payload_in,
450
                                             size_t len_in,
451
                                             uint8_t *payload_out,
452
                                             size_t *len_out,
453
                                             CXLCCI *cci)
454
0
{
455
0
    struct {
456
0
        uint8_t rsp_limit;
457
0
    } QEMU_PACKED *get_rsp_msg_limit = (void *)payload_out;
458
0
    QEMU_BUILD_BUG_ON(sizeof(*get_rsp_msg_limit) != 1);
459
460
0
    get_rsp_msg_limit->rsp_limit = (uint8_t)log2(cci->payload_max);
461
462
0
    *len_out = sizeof(*get_rsp_msg_limit);
463
0
    return CXL_MBOX_SUCCESS;
464
0
}
465
466
/* CXL r3.1 section 8.2.9.1.4: Set Response Message Limit (Opcode 0004h) */
467
static CXLRetCode cmd_set_response_msg_limit(const struct cxl_cmd *cmd,
468
                                             uint8_t *payload_in,
469
                                             size_t len_in,
470
                                             uint8_t *payload_out,
471
                                             size_t *len_out,
472
                                             CXLCCI *cci)
473
0
{
474
0
    struct {
475
0
        uint8_t rsp_limit;
476
0
    } QEMU_PACKED *in = (void *)payload_in;
477
0
    QEMU_BUILD_BUG_ON(sizeof(*in) != 1);
478
0
    struct {
479
0
        uint8_t rsp_limit;
480
0
    } QEMU_PACKED *out = (void *)payload_out;
481
0
    QEMU_BUILD_BUG_ON(sizeof(*out) != 1);
482
483
0
    if (in->rsp_limit < 8 || in->rsp_limit > 10) {
484
0
        return CXL_MBOX_INVALID_INPUT;
485
0
    }
486
487
0
    cci->payload_max = 1 << in->rsp_limit;
488
0
    out->rsp_limit = in->rsp_limit;
489
490
0
    *len_out = sizeof(*out);
491
0
    return CXL_MBOX_SUCCESS;
492
0
}
493
494
static void cxl_set_dsp_active_bm(PCIBus *b, PCIDevice *d,
495
                                  void *private)
496
0
{
497
0
    uint8_t *bm = private;
498
0
    if (object_dynamic_cast(OBJECT(d), TYPE_CXL_DSP)) {
499
0
        uint8_t port = PCIE_PORT(d)->port;
500
0
        bm[port / 8] |= 1 << (port % 8);
501
0
    }
502
0
}
503
504
/* CXL r3.1 Section 7.6.7.1.1: Identify Switch Device (Opcode 5100h) */
505
static CXLRetCode cmd_identify_switch_device(const struct cxl_cmd *cmd,
506
                                             uint8_t *payload_in,
507
                                             size_t len_in,
508
                                             uint8_t *payload_out,
509
                                             size_t *len_out,
510
                                             CXLCCI *cci)
511
0
{
512
0
    PCIEPort *usp = PCIE_PORT(cci->d);
513
0
    PCIBus *bus = &PCI_BRIDGE(cci->d)->sec_bus;
514
0
    int num_phys_ports = pcie_count_ds_ports(bus);
515
516
0
    struct cxl_fmapi_ident_switch_dev_resp_pl {
517
0
        uint8_t ingress_port_id;
518
0
        uint8_t rsvd;
519
0
        uint8_t num_physical_ports;
520
0
        uint8_t num_vcss;
521
0
        uint8_t active_port_bitmask[0x20];
522
0
        uint8_t active_vcs_bitmask[0x20];
523
0
        uint16_t total_vppbs;
524
0
        uint16_t bound_vppbs;
525
0
        uint8_t num_hdm_decoders_per_usp;
526
0
    } QEMU_PACKED *out;
527
0
    QEMU_BUILD_BUG_ON(sizeof(*out) != 0x49);
528
529
0
    out = (struct cxl_fmapi_ident_switch_dev_resp_pl *)payload_out;
530
0
    *out = (struct cxl_fmapi_ident_switch_dev_resp_pl) {
531
0
        .num_physical_ports = num_phys_ports + 1, /* 1 USP */
532
0
        .num_vcss = 1, /* Not yet support multiple VCS - potentially tricky */
533
0
        .active_vcs_bitmask[0] = 0x1,
534
0
        .total_vppbs = num_phys_ports + 1,
535
0
        .bound_vppbs = num_phys_ports + 1,
536
0
        .num_hdm_decoders_per_usp = 4,
537
0
    };
538
539
    /* Depends on the CCI type */
540
0
    if (object_dynamic_cast(OBJECT(cci->intf), TYPE_PCIE_PORT)) {
541
0
        out->ingress_port_id = PCIE_PORT(cci->intf)->port;
542
0
    } else {
543
        /* MCTP? */
544
0
        out->ingress_port_id = 0;
545
0
    }
546
547
0
    pci_for_each_device_under_bus(bus, cxl_set_dsp_active_bm,
548
0
                                  out->active_port_bitmask);
549
0
    out->active_port_bitmask[usp->port / 8] |= (1 << usp->port % 8);
550
551
0
    *len_out = sizeof(*out);
552
553
0
    return CXL_MBOX_SUCCESS;
554
0
}
555
556
/* CXL r3.1 Section 7.6.7.1.2: Get Physical Port State (Opcode 5101h) */
557
static CXLRetCode cmd_get_physical_port_state(const struct cxl_cmd *cmd,
558
                                              uint8_t *payload_in,
559
                                              size_t len_in,
560
                                              uint8_t *payload_out,
561
                                              size_t *len_out,
562
                                              CXLCCI *cci)
563
0
{
564
    /* CXL r3.1 Table 7-17: Get Physical Port State Request Payload */
565
0
    struct cxl_fmapi_get_phys_port_state_req_pl {
566
0
        uint8_t num_ports;
567
0
        uint8_t ports[];
568
0
    } QEMU_PACKED *in;
569
570
    /*
571
     * CXL r3.2 Table 7-19: Get Physical Port State Port Information Block
572
     * Format
573
     */
574
0
    struct cxl_fmapi_port_state_info_block {
575
0
        uint8_t port_id;
576
0
        uint8_t config_state;
577
0
        uint8_t connected_device_mode;
578
0
        uint8_t rsv1;
579
0
        uint8_t connected_device_type;
580
0
        uint8_t supported_cxl_mode_bitmask;
581
0
        uint8_t max_link_width;
582
0
        uint8_t negotiated_link_width;
583
0
        uint8_t supported_link_speeds_vector;
584
0
        uint8_t max_link_speed;
585
0
        uint8_t current_link_speed;
586
0
        uint8_t ltssm_state;
587
0
        uint8_t first_lane_num;
588
0
        uint16_t link_state;
589
0
        uint8_t supported_ld_count;
590
0
    } QEMU_PACKED;
591
592
    /* CXL r3.1 Table 7-18: Get Physical Port State Response Payload */
593
0
    struct cxl_fmapi_get_phys_port_state_resp_pl {
594
0
        uint8_t num_ports;
595
0
        uint8_t rsv1[3];
596
0
        struct cxl_fmapi_port_state_info_block ports[];
597
0
    } QEMU_PACKED *out;
598
0
    PCIBus *bus = &PCI_BRIDGE(cci->d)->sec_bus;
599
0
    PCIEPort *usp = PCIE_PORT(cci->d);
600
0
    size_t pl_size;
601
0
    int i;
602
603
0
    in = (struct cxl_fmapi_get_phys_port_state_req_pl *)payload_in;
604
0
    out = (struct cxl_fmapi_get_phys_port_state_resp_pl *)payload_out;
605
606
0
    if (len_in < sizeof(*in)) {
607
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
608
0
    }
609
    /* Check if what was requested can fit */
610
0
    if (sizeof(*out) + sizeof(*out->ports) * in->num_ports > cci->payload_max) {
611
0
        return CXL_MBOX_INVALID_INPUT;
612
0
    }
613
614
    /* For success there should be a match for each requested */
615
0
    out->num_ports = in->num_ports;
616
617
0
    for (i = 0; i < in->num_ports; i++) {
618
0
        struct cxl_fmapi_port_state_info_block *port;
619
        /* First try to match on downstream port */
620
0
        PCIDevice *port_dev;
621
0
        CXLPhyPortPerst *perst;
622
0
        uint16_t lnkcap, lnkcap2, lnksta;
623
624
0
        port = &out->ports[i];
625
626
0
        port_dev = pcie_find_port_by_pn(bus, in->ports[i]);
627
0
        if (port_dev) { /* DSP */
628
0
            PCIDevice *ds_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(port_dev))
629
0
                ->devices[0];
630
0
            port->config_state = CXL_PORT_CONFIG_STATE_DSP;
631
0
            if (ds_dev) {
632
0
                if (object_dynamic_cast(OBJECT(ds_dev), TYPE_CXL_TYPE3)) {
633
0
                    uint16_t lnksta2;
634
635
0
                    if (!port_dev->exp.exp_cap) {
636
0
                        return CXL_MBOX_INTERNAL_ERROR;
637
0
                    }
638
639
0
                    lnksta2 = port_dev->config_read(port_dev,
640
0
                                  port_dev->exp.exp_cap + PCI_EXP_LNKSTA2,
641
0
                                  sizeof(lnksta2));
642
643
                    /* Assume MLD for now */
644
0
                    port->connected_device_type =
645
0
                        CXL_PORT_CONNECTED_DEV_TYPE_3_MLD;
646
0
                    if (lnksta2 & PCI_EXP_LNKSTA2_FLIT) {
647
0
                        port->connected_device_mode =
648
0
                            CXL_PORT_CONNECTED_DEV_MODE_256B;
649
0
                    } else {
650
0
                        port->connected_device_mode =
651
0
                            CXL_PORT_CONNECTED_DEV_MODE_68B_VH;
652
0
                    }
653
0
                } else {
654
0
                    port->connected_device_type =
655
0
                        CXL_PORT_CONNECTED_DEV_TYPE_PCIE;
656
0
                    port->connected_device_mode =
657
0
                        CXL_PORT_CONNECTED_DEV_MODE_NOT_CXL_OR_DISCONN;
658
659
0
                }
660
0
            } else {
661
0
                port->connected_device_type = CXL_PORT_CONNECTED_DEV_TYPE_NONE;
662
0
                port->connected_device_mode =
663
0
                    CXL_PORT_CONNECTED_DEV_MODE_NOT_CXL_OR_DISCONN;
664
0
            }
665
            /* DSP currently always support modes implemented in QEMU */
666
0
            port->supported_cxl_mode_bitmask = CXL_PORT_SUPPORTS_68B_VH |
667
0
                CXL_PORT_SUPPORTS_256B;
668
0
            port->supported_ld_count = 3;
669
0
            perst = cxl_dsp_get_perst(CXL_DSP(port_dev));
670
0
        } else if (usp->port == in->ports[i]) { /* USP */
671
0
            port_dev = PCI_DEVICE(usp);
672
0
            port->config_state = CXL_PORT_CONFIG_STATE_USP;
673
0
            port->connected_device_type = 0; /* Reserved for USP */
674
0
            port->connected_device_mode = 0; /* Reserved for USP */
675
0
            port->supported_cxl_mode_bitmask = CXL_PORT_SUPPORTS_68B_VH |
676
0
                (CXL_USP(usp)->flitmode ? CXL_PORT_SUPPORTS_256B : 0);
677
0
            perst = &CXL_USP(usp)->perst;
678
0
        } else {
679
0
            return CXL_MBOX_INVALID_INPUT;
680
0
        }
681
682
0
        port->port_id = in->ports[i];
683
        /* Information on status of this port in lnksta, lnkcap */
684
0
        if (!port_dev->exp.exp_cap) {
685
0
            return CXL_MBOX_INTERNAL_ERROR;
686
0
        }
687
0
        lnksta = port_dev->config_read(port_dev,
688
0
                                       port_dev->exp.exp_cap + PCI_EXP_LNKSTA,
689
0
                                       sizeof(lnksta));
690
0
        lnkcap = port_dev->config_read(port_dev,
691
0
                                       port_dev->exp.exp_cap + PCI_EXP_LNKCAP,
692
0
                                       sizeof(lnkcap));
693
0
        lnkcap2 = port_dev->config_read(port_dev,
694
0
                                        port_dev->exp.exp_cap + PCI_EXP_LNKCAP2,
695
0
                                        sizeof(lnkcap2));
696
697
0
        port->max_link_width = (lnkcap & PCI_EXP_LNKCAP_MLW) >> 4;
698
0
        port->negotiated_link_width = (lnksta & PCI_EXP_LNKSTA_NLW) >> 4;
699
        /* No definition for SLS field in linux/pci_regs.h */
700
0
        port->supported_link_speeds_vector = (lnkcap2 & 0xFE) >> 1;
701
0
        port->max_link_speed = lnkcap & PCI_EXP_LNKCAP_SLS;
702
0
        port->current_link_speed = lnksta & PCI_EXP_LNKSTA_CLS;
703
        /* TODO: Track down if we can get the rest of the info */
704
0
        port->ltssm_state = 0x7;
705
0
        port->first_lane_num = 0;
706
0
        port->link_state = perst ? CXL_PORT_LINK_STATE_FLAG_PERST_ASSERTED : 0;
707
0
    }
708
709
0
    pl_size = sizeof(*out) + sizeof(*out->ports) * in->num_ports;
710
0
    *len_out = pl_size;
711
712
0
    return CXL_MBOX_SUCCESS;
713
0
}
714
715
static void *bg_assertcb(void *opaque)
716
0
{
717
0
    CXLPhyPortPerst *perst = opaque;
718
719
    /* holding reset phase for 100ms */
720
0
    while (perst->asrt_time--) {
721
0
        usleep(1000);
722
0
    }
723
0
    perst->issued_assert_perst = true;
724
0
    return NULL;
725
0
}
726
727
static CXLRetCode cxl_deassert_perst(Object *obj, CXLPhyPortPerst *perst)
728
0
{
729
0
    if (!perst->issued_assert_perst) {
730
0
        return CXL_MBOX_INTERNAL_ERROR;
731
0
    }
732
733
0
    QEMU_LOCK_GUARD(&perst->lock);
734
0
    resettable_release_reset(obj, RESET_TYPE_COLD);
735
0
    perst->issued_assert_perst = false;
736
0
    perst->asrt_time = ASSERT_WAIT_TIME_MS;
737
738
0
    return CXL_MBOX_SUCCESS;
739
0
}
740
741
static CXLRetCode cxl_assert_perst(Object *obj, CXLPhyPortPerst *perst)
742
0
{
743
0
    if (cxl_perst_asserted(perst)) {
744
0
        return CXL_MBOX_INTERNAL_ERROR;
745
0
    }
746
747
0
    QEMU_LOCK_GUARD(&perst->lock);
748
0
    resettable_assert_reset(obj, RESET_TYPE_COLD);
749
0
    qemu_thread_create(&perst->asrt_thread, "assert_thread", bg_assertcb,
750
0
                       perst, QEMU_THREAD_DETACHED);
751
752
0
    return CXL_MBOX_SUCCESS;
753
0
}
754
755
static CXLDownstreamPort *cxl_find_dsp_on_bus(PCIBus *bus, uint8_t pn)
756
0
{
757
758
0
    PCIDevice *port_dev = pcie_find_port_by_pn(bus, pn);
759
760
0
    if (object_dynamic_cast(OBJECT(port_dev), TYPE_CXL_DSP)) {
761
0
        return CXL_DSP(port_dev);
762
0
    }
763
764
0
    return NULL;
765
0
}
766
767
/* CXL r3.2 Section 7.6.7.1.3: Get Physical Port Control (Opcode 5102h) */
768
static CXLRetCode cmd_physical_port_control(const struct cxl_cmd *cmd,
769
                                            uint8_t *payload_in,
770
                                            size_t len_in,
771
                                            uint8_t *payload_out,
772
                                            size_t *len_out,
773
                                            CXLCCI *cci)
774
0
{
775
0
   CXLUpstreamPort *pp = CXL_USP(cci->d);
776
0
   CXLPhyPortPerst *perst;
777
0
   PCIDevice *dev;
778
779
0
   struct cxl_fmapi_get_physical_port_control_req_pl {
780
0
        uint8_t ppb_id;
781
0
        uint8_t ports_op;
782
0
    } QEMU_PACKED *in = (void *)payload_in;
783
784
0
    if (len_in < sizeof(*in)) {
785
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
786
0
    }
787
788
0
    if (PCIE_PORT(pp)->port == in->ppb_id) {
789
0
        dev = PCI_DEVICE(pp);
790
0
        perst = &pp->perst;
791
0
    } else {
792
0
        CXLDownstreamPort *dsp =
793
0
            cxl_find_dsp_on_bus(&PCI_BRIDGE(pp)->sec_bus, in->ppb_id);
794
795
0
        if (!dsp) {
796
0
            return CXL_MBOX_INVALID_INPUT;
797
0
        }
798
0
        dev = PCI_DEVICE(dsp);
799
0
        perst = cxl_dsp_get_perst(dsp);
800
0
    }
801
802
0
    switch (in->ports_op) {
803
0
    case 0:
804
0
        return cxl_assert_perst(OBJECT(&dev->qdev), perst);
805
0
    case 1:
806
0
        return cxl_deassert_perst(OBJECT(&dev->qdev), perst);
807
0
    case 2: {
808
0
        if (!perst) {
809
0
            return CXL_MBOX_INVALID_INPUT;
810
0
        }
811
812
0
        if (perst->issued_assert_perst ||
813
0
            perst->asrt_time < ASSERT_WAIT_TIME_MS) {
814
0
            return CXL_MBOX_INTERNAL_ERROR;
815
0
        }
816
0
        device_cold_reset(&dev->qdev);
817
0
        return CXL_MBOX_SUCCESS;
818
0
    }
819
0
    default:
820
0
        return CXL_MBOX_INVALID_INPUT;
821
0
    }
822
0
}
823
824
/* CXL r3.1 Section 8.2.9.1.2: Background Operation Status (Opcode 0002h) */
825
static CXLRetCode cmd_infostat_bg_op_sts(const struct cxl_cmd *cmd,
826
                                         uint8_t *payload_in,
827
                                         size_t len_in,
828
                                         uint8_t *payload_out,
829
                                         size_t *len_out,
830
                                         CXLCCI *cci)
831
0
{
832
0
    struct {
833
0
        uint8_t status;
834
0
        uint8_t rsvd;
835
0
        uint16_t opcode;
836
0
        uint16_t returncode;
837
0
        uint16_t vendor_ext_status;
838
0
    } QEMU_PACKED *bg_op_status;
839
0
    QEMU_BUILD_BUG_ON(sizeof(*bg_op_status) != 8);
840
841
0
    bg_op_status = (void *)payload_out;
842
0
    bg_op_status->status = cci->bg.complete_pct << 1;
843
0
    if (cci->bg.runtime > 0) {
844
0
        bg_op_status->status |= 1U << 0;
845
0
    }
846
0
    bg_op_status->opcode = cci->bg.opcode;
847
0
    bg_op_status->returncode = cci->bg.ret_code;
848
0
    *len_out = sizeof(*bg_op_status);
849
850
0
    return CXL_MBOX_SUCCESS;
851
0
}
852
853
/*
854
 * CXL r3.1 Section 8.2.9.1.5:
855
 * Request Abort Background Operation (Opcode 0005h)
856
 */
857
static CXLRetCode cmd_infostat_bg_op_abort(const struct cxl_cmd *cmd,
858
                                           uint8_t *payload_in,
859
                                           size_t len_in,
860
                                           uint8_t *payload_out,
861
                                           size_t *len_out,
862
                                           CXLCCI *cci)
863
0
{
864
0
    int bg_set = cci->bg.opcode >> 8;
865
0
    int bg_cmd = cci->bg.opcode & 0xff;
866
0
    const struct cxl_cmd *bg_c = &cci->cxl_cmd_set[bg_set][bg_cmd];
867
868
0
    if (!(bg_c->effect & CXL_MBOX_BACKGROUND_OPERATION_ABORT)) {
869
0
        return CXL_MBOX_REQUEST_ABORT_NOTSUP;
870
0
    }
871
872
0
    qemu_mutex_lock(&cci->bg.lock);
873
0
    if (cci->bg.runtime) {
874
        /* operation is near complete, let it finish */
875
0
        if (cci->bg.complete_pct < 85) {
876
0
            timer_del(cci->bg.timer);
877
0
            cci->bg.ret_code = CXL_MBOX_ABORTED;
878
0
            cci->bg.starttime = 0;
879
0
            cci->bg.runtime = 0;
880
0
            cci->bg.aborted = true;
881
0
        }
882
0
    }
883
0
    qemu_mutex_unlock(&cci->bg.lock);
884
885
0
    return CXL_MBOX_SUCCESS;
886
0
}
887
888
0
#define CXL_FW_SLOTS 2
889
0
#define CXL_FW_SIZE  0x02000000 /* 32 mb */
890
891
/* CXL r3.1 Section 8.2.9.3.1: Get FW Info (Opcode 0200h) */
892
static CXLRetCode cmd_firmware_update_get_info(const struct cxl_cmd *cmd,
893
                                               uint8_t *payload_in,
894
                                               size_t len,
895
                                               uint8_t *payload_out,
896
                                               size_t *len_out,
897
                                               CXLCCI *cci)
898
0
{
899
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
900
0
    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
901
0
    struct {
902
0
        uint8_t slots_supported;
903
0
        uint8_t slot_info;
904
0
        uint8_t caps;
905
0
        uint8_t rsvd[0xd];
906
0
        char fw_rev1[0x10];
907
0
        char fw_rev2[0x10];
908
0
        char fw_rev3[0x10];
909
0
        char fw_rev4[0x10];
910
0
    } QEMU_PACKED *fw_info;
911
0
    QEMU_BUILD_BUG_ON(sizeof(*fw_info) != 0x50);
912
913
0
    if (!QEMU_IS_ALIGNED(cxl_dstate->vmem_size, CXL_CAPACITY_MULTIPLIER) ||
914
0
        !QEMU_IS_ALIGNED(cxl_dstate->pmem_size, CXL_CAPACITY_MULTIPLIER) ||
915
0
        !QEMU_IS_ALIGNED(ct3d->dc.total_capacity, CXL_CAPACITY_MULTIPLIER)) {
916
0
        return CXL_MBOX_INTERNAL_ERROR;
917
0
    }
918
919
0
    fw_info = (void *)payload_out;
920
921
0
    fw_info->slots_supported = CXL_FW_SLOTS;
922
0
    fw_info->slot_info = (cci->fw.active_slot & 0x7) |
923
0
            ((cci->fw.staged_slot & 0x7) << 3);
924
0
    fw_info->caps = BIT(0);  /* online update supported */
925
926
0
    if (cci->fw.slot[0]) {
927
0
        pstrcpy(fw_info->fw_rev1, sizeof(fw_info->fw_rev1), "BWFW VERSION 0");
928
0
    }
929
0
    if (cci->fw.slot[1]) {
930
0
        pstrcpy(fw_info->fw_rev2, sizeof(fw_info->fw_rev2), "BWFW VERSION 1");
931
0
    }
932
933
0
    *len_out = sizeof(*fw_info);
934
0
    return CXL_MBOX_SUCCESS;
935
0
}
936
937
/* CXL r3.1 section 8.2.9.3.2: Transfer FW (Opcode 0201h) */
938
0
#define CXL_FW_XFER_ALIGNMENT   128
939
940
0
#define CXL_FW_XFER_ACTION_FULL     0x0
941
0
#define CXL_FW_XFER_ACTION_INIT     0x1
942
0
#define CXL_FW_XFER_ACTION_CONTINUE 0x2
943
0
#define CXL_FW_XFER_ACTION_END      0x3
944
0
#define CXL_FW_XFER_ACTION_ABORT    0x4
945
946
static CXLRetCode cmd_firmware_update_transfer(const struct cxl_cmd *cmd,
947
                                               uint8_t *payload_in,
948
                                               size_t len,
949
                                               uint8_t *payload_out,
950
                                               size_t *len_out,
951
                                               CXLCCI *cci)
952
0
{
953
0
    struct {
954
0
        uint8_t action;
955
0
        uint8_t slot;
956
0
        uint8_t rsvd1[2];
957
0
        uint32_t offset;
958
0
        uint8_t rsvd2[0x78];
959
0
        uint8_t data[];
960
0
    } QEMU_PACKED *fw_transfer = (void *)payload_in;
961
0
    size_t offset, length;
962
963
0
    if (len < sizeof(*fw_transfer)) {
964
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
965
0
    }
966
967
0
    if (fw_transfer->action == CXL_FW_XFER_ACTION_ABORT) {
968
        /*
969
         * At this point there aren't any on-going transfers
970
         * running in the bg - this is serialized before this
971
         * call altogether. Just mark the state machine and
972
         * disregard any other input.
973
         */
974
0
        cci->fw.transferring = false;
975
0
        return CXL_MBOX_SUCCESS;
976
0
    }
977
978
0
    offset = fw_transfer->offset * CXL_FW_XFER_ALIGNMENT;
979
0
    length = len - sizeof(*fw_transfer);
980
0
    if (offset + length > CXL_FW_SIZE) {
981
0
        return CXL_MBOX_INVALID_INPUT;
982
0
    }
983
984
0
    if (cci->fw.transferring) {
985
0
        if (fw_transfer->action == CXL_FW_XFER_ACTION_FULL ||
986
0
            fw_transfer->action == CXL_FW_XFER_ACTION_INIT) {
987
0
            return CXL_MBOX_FW_XFER_IN_PROGRESS;
988
0
        }
989
        /*
990
         * Abort partitioned package transfer if over 30 secs
991
         * between parts. As opposed to the explicit ABORT action,
992
         * semantically treat this condition as an error - as
993
         * if a part action were passed without a previous INIT.
994
         */
995
0
        if (difftime(time(NULL), cci->fw.last_partxfer) > 30.0) {
996
0
            cci->fw.transferring = false;
997
0
            return CXL_MBOX_INVALID_INPUT;
998
0
        }
999
0
    } else if (fw_transfer->action == CXL_FW_XFER_ACTION_CONTINUE ||
1000
0
               fw_transfer->action == CXL_FW_XFER_ACTION_END) {
1001
0
        return CXL_MBOX_INVALID_INPUT;
1002
0
    }
1003
1004
    /* allow back-to-back retransmission */
1005
0
    if ((offset != cci->fw.prev_offset || length != cci->fw.prev_len) &&
1006
0
        (fw_transfer->action == CXL_FW_XFER_ACTION_CONTINUE ||
1007
0
         fw_transfer->action == CXL_FW_XFER_ACTION_END)) {
1008
        /* verify no overlaps */
1009
0
        if (offset < cci->fw.prev_offset + cci->fw.prev_len) {
1010
0
            return CXL_MBOX_FW_XFER_OUT_OF_ORDER;
1011
0
        }
1012
0
    }
1013
1014
0
    switch (fw_transfer->action) {
1015
0
    case CXL_FW_XFER_ACTION_FULL: /* ignores offset */
1016
0
    case CXL_FW_XFER_ACTION_END:
1017
0
        if (fw_transfer->slot == 0 ||
1018
0
            fw_transfer->slot == cci->fw.active_slot ||
1019
0
            fw_transfer->slot > CXL_FW_SLOTS) {
1020
0
            return CXL_MBOX_FW_INVALID_SLOT;
1021
0
        }
1022
1023
        /* mark the slot used upon bg completion */
1024
0
        break;
1025
0
    case CXL_FW_XFER_ACTION_INIT:
1026
0
        if (offset != 0) {
1027
0
            return CXL_MBOX_INVALID_INPUT;
1028
0
        }
1029
1030
0
        cci->fw.transferring = true;
1031
0
        cci->fw.prev_offset = offset;
1032
0
        cci->fw.prev_len = length;
1033
0
        break;
1034
0
    case CXL_FW_XFER_ACTION_CONTINUE:
1035
0
        cci->fw.prev_offset = offset;
1036
0
        cci->fw.prev_len = length;
1037
0
        break;
1038
0
    default:
1039
0
        return CXL_MBOX_INVALID_INPUT;
1040
0
    }
1041
1042
0
    if (fw_transfer->action == CXL_FW_XFER_ACTION_FULL) {
1043
0
        cci->bg.runtime = 10 * 1000UL;
1044
0
    } else {
1045
0
        cci->bg.runtime = 2 * 1000UL;
1046
0
    }
1047
    /* keep relevant context for bg completion */
1048
0
    cci->fw.curr_action = fw_transfer->action;
1049
0
    cci->fw.curr_slot = fw_transfer->slot;
1050
0
    *len_out = 0;
1051
1052
0
    return CXL_MBOX_BG_STARTED;
1053
0
}
1054
1055
static void __do_firmware_xfer(CXLCCI *cci)
1056
0
{
1057
0
    switch (cci->fw.curr_action) {
1058
0
    case CXL_FW_XFER_ACTION_FULL:
1059
0
    case CXL_FW_XFER_ACTION_END:
1060
0
        cci->fw.slot[cci->fw.curr_slot - 1] = true;
1061
0
        cci->fw.transferring = false;
1062
0
        break;
1063
0
    case CXL_FW_XFER_ACTION_INIT:
1064
0
    case CXL_FW_XFER_ACTION_CONTINUE:
1065
0
        time(&cci->fw.last_partxfer);
1066
0
        break;
1067
0
    default:
1068
0
        break;
1069
0
    }
1070
0
}
1071
1072
/* CXL r3.1 section 8.2.9.3.3: Activate FW (Opcode 0202h) */
1073
static CXLRetCode cmd_firmware_update_activate(const struct cxl_cmd *cmd,
1074
                                               uint8_t *payload_in,
1075
                                               size_t len,
1076
                                               uint8_t *payload_out,
1077
                                               size_t *len_out,
1078
                                               CXLCCI *cci)
1079
0
{
1080
0
    struct {
1081
0
        uint8_t action;
1082
0
        uint8_t slot;
1083
0
    } QEMU_PACKED *fw_activate = (void *)payload_in;
1084
0
    QEMU_BUILD_BUG_ON(sizeof(*fw_activate) != 0x2);
1085
1086
0
    if (fw_activate->slot == 0 ||
1087
0
        fw_activate->slot == cci->fw.active_slot ||
1088
0
        fw_activate->slot > CXL_FW_SLOTS) {
1089
0
        return CXL_MBOX_FW_INVALID_SLOT;
1090
0
    }
1091
1092
    /* ensure that an actual fw package is there */
1093
0
    if (!cci->fw.slot[fw_activate->slot - 1]) {
1094
0
        return CXL_MBOX_FW_INVALID_SLOT;
1095
0
    }
1096
1097
0
    switch (fw_activate->action) {
1098
0
    case 0: /* online */
1099
0
        cci->fw.active_slot = fw_activate->slot;
1100
0
        break;
1101
0
    case 1: /* reset */
1102
0
        cci->fw.staged_slot = fw_activate->slot;
1103
0
        break;
1104
0
    default:
1105
0
        return CXL_MBOX_INVALID_INPUT;
1106
0
    }
1107
1108
0
    return CXL_MBOX_SUCCESS;
1109
0
}
1110
1111
/* CXL r3.1 Section 8.2.9.4.1: Get Timestamp (Opcode 0300h) */
1112
static CXLRetCode cmd_timestamp_get(const struct cxl_cmd *cmd,
1113
                                    uint8_t *payload_in,
1114
                                    size_t len_in,
1115
                                    uint8_t *payload_out,
1116
                                    size_t *len_out,
1117
                                    CXLCCI *cci)
1118
0
{
1119
0
    CXLDeviceState *cxl_dstate = &CXL_TYPE3(cci->d)->cxl_dstate;
1120
0
    uint64_t final_time = cxl_device_get_timestamp(cxl_dstate);
1121
1122
0
    stq_le_p(payload_out, final_time);
1123
0
    *len_out = 8;
1124
1125
0
    return CXL_MBOX_SUCCESS;
1126
0
}
1127
1128
/* CXL r3.1 Section 8.2.9.4.2: Set Timestamp (Opcode 0301h) */
1129
static CXLRetCode cmd_timestamp_set(const struct cxl_cmd *cmd,
1130
                                    uint8_t *payload_in,
1131
                                    size_t len_in,
1132
                                    uint8_t *payload_out,
1133
                                    size_t *len_out,
1134
                                    CXLCCI *cci)
1135
0
{
1136
0
    CXLDeviceState *cxl_dstate = &CXL_TYPE3(cci->d)->cxl_dstate;
1137
1138
0
    cxl_dstate->timestamp.set = true;
1139
0
    cxl_dstate->timestamp.last_set = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1140
1141
0
    cxl_dstate->timestamp.host_set = le64_to_cpu(*(uint64_t *)payload_in);
1142
1143
0
    *len_out = 0;
1144
0
    return CXL_MBOX_SUCCESS;
1145
0
}
1146
1147
/* CXL r3.1 Section 8.2.9.5.2.1: Command Effects Log (CEL) */
1148
static const QemuUUID cel_uuid = {
1149
    .data = UUID(0x0da9c0b5, 0xbf41, 0x4b78, 0x8f, 0x79,
1150
                 0x96, 0xb1, 0x62, 0x3b, 0x3f, 0x17)
1151
};
1152
1153
/* CXL r3.1 Section 8.2.9.5.1: Get Supported Logs (Opcode 0400h) */
1154
static CXLRetCode cmd_logs_get_supported(const struct cxl_cmd *cmd,
1155
                                         uint8_t *payload_in,
1156
                                         size_t len_in,
1157
                                         uint8_t *payload_out,
1158
                                         size_t *len_out,
1159
                                         CXLCCI *cci)
1160
0
{
1161
0
    struct {
1162
0
        uint16_t entries;
1163
0
        uint8_t rsvd[6];
1164
0
        struct {
1165
0
            QemuUUID uuid;
1166
0
            uint32_t size;
1167
0
        } log_entries[1];
1168
0
    } QEMU_PACKED *supported_logs = (void *)payload_out;
1169
0
    QEMU_BUILD_BUG_ON(sizeof(*supported_logs) != 0x1c);
1170
1171
0
    supported_logs->entries = 1;
1172
0
    supported_logs->log_entries[0].uuid = cel_uuid;
1173
0
    supported_logs->log_entries[0].size = 4 * cci->cel_size;
1174
1175
0
    *len_out = sizeof(*supported_logs);
1176
0
    return CXL_MBOX_SUCCESS;
1177
0
}
1178
1179
/* CXL r3.1 Section 8.2.9.5.2: Get Log (Opcode 0401h) */
1180
static CXLRetCode cmd_logs_get_log(const struct cxl_cmd *cmd,
1181
                                   uint8_t *payload_in,
1182
                                   size_t len_in,
1183
                                   uint8_t *payload_out,
1184
                                   size_t *len_out,
1185
                                   CXLCCI *cci)
1186
0
{
1187
0
    struct {
1188
0
        QemuUUID uuid;
1189
0
        uint32_t offset;
1190
0
        uint32_t length;
1191
0
    } QEMU_PACKED QEMU_ALIGNED(16) *get_log;
1192
1193
0
    get_log = (void *)payload_in;
1194
1195
0
    if (get_log->length > cci->payload_max) {
1196
0
        return CXL_MBOX_INVALID_INPUT;
1197
0
    }
1198
1199
0
    if (!qemu_uuid_is_equal(&get_log->uuid, &cel_uuid)) {
1200
0
        return CXL_MBOX_INVALID_LOG;
1201
0
    }
1202
1203
    /*
1204
     * CXL r3.1 Section 8.2.9.5.2: Get Log (Opcode 0401h)
1205
     *   The device shall return Invalid Input if the Offset or Length
1206
     *   fields attempt to access beyond the size of the log as reported by Get
1207
     *   Supported Log.
1208
     *
1209
     * Only valid for there to be one entry per opcode, but the length + offset
1210
     * may still be greater than that if the inputs are not valid and so access
1211
     * beyond the end of cci->cel_log.
1212
     */
1213
0
    if ((uint64_t)get_log->offset + get_log->length >= sizeof(cci->cel_log)) {
1214
0
        return CXL_MBOX_INVALID_INPUT;
1215
0
    }
1216
1217
    /* Store off everything to local variables so we can wipe out the payload */
1218
0
    *len_out = get_log->length;
1219
1220
0
    memmove(payload_out, cci->cel_log + get_log->offset, get_log->length);
1221
1222
0
    return CXL_MBOX_SUCCESS;
1223
0
}
1224
1225
/* CXL r3.1 section 8.2.9.6: Features */
1226
/*
1227
 * Get Supported Features output payload
1228
 * CXL r3.1 section 8.2.9.6.1 Table 8-96
1229
 */
1230
typedef struct CXLSupportedFeatureHeader {
1231
    uint16_t entries;
1232
    uint16_t nsuppfeats_dev;
1233
    uint32_t reserved;
1234
} QEMU_PACKED CXLSupportedFeatureHeader;
1235
1236
/*
1237
 * Get Supported Features Supported Feature Entry
1238
 * CXL r3.1 section 8.2.9.6.1 Table 8-97
1239
 */
1240
typedef struct CXLSupportedFeatureEntry {
1241
    QemuUUID uuid;
1242
    uint16_t feat_index;
1243
    uint16_t get_feat_size;
1244
    uint16_t set_feat_size;
1245
    uint32_t attr_flags;
1246
    uint8_t get_feat_version;
1247
    uint8_t set_feat_version;
1248
    uint16_t set_feat_effects;
1249
    uint8_t rsvd[18];
1250
} QEMU_PACKED CXLSupportedFeatureEntry;
1251
1252
/*
1253
 * Get Supported Features Supported Feature Entry
1254
 * CXL rev 3.1 section 8.2.9.6.1 Table 8-97
1255
 */
1256
/* Supported Feature Entry : attribute flags */
1257
0
#define CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE BIT(0)
1258
#define CXL_FEAT_ENTRY_ATTR_FLAG_DEEPEST_RESET_PERSISTENCE_MASK GENMASK(3, 1)
1259
#define CXL_FEAT_ENTRY_ATTR_FLAG_PERSIST_ACROSS_FIRMWARE_UPDATE BIT(4)
1260
0
#define CXL_FEAT_ENTRY_ATTR_FLAG_SUPPORT_DEFAULT_SEL BIT(5)
1261
#define CXL_FEAT_ENTRY_ATTR_FLAG_SUPPORT_SAVED_SEL BIT(6)
1262
1263
/* Supported Feature Entry : set feature effects */
1264
#define CXL_FEAT_ENTRY_SFE_CONFIG_CHANGE_COLD_RESET BIT(0)
1265
0
#define CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE BIT(1)
1266
#define CXL_FEAT_ENTRY_SFE_IMMEDIATE_DATA_CHANGE BIT(2)
1267
#define CXL_FEAT_ENTRY_SFE_IMMEDIATE_POLICY_CHANGE BIT(3)
1268
#define CXL_FEAT_ENTRY_SFE_IMMEDIATE_LOG_CHANGE BIT(4)
1269
#define CXL_FEAT_ENTRY_SFE_SECURITY_STATE_CHANGE BIT(5)
1270
#define CXL_FEAT_ENTRY_SFE_BACKGROUND_OPERATION BIT(6)
1271
#define CXL_FEAT_ENTRY_SFE_SUPPORT_SECONDARY_MAILBOX BIT(7)
1272
#define CXL_FEAT_ENTRY_SFE_SUPPORT_ABORT_BACKGROUND_OPERATION BIT(8)
1273
0
#define CXL_FEAT_ENTRY_SFE_CEL_VALID BIT(9)
1274
#define CXL_FEAT_ENTRY_SFE_CONFIG_CHANGE_CONV_RESET BIT(10)
1275
#define CXL_FEAT_ENTRY_SFE_CONFIG_CHANGE_CXL_RESET BIT(11)
1276
1277
enum CXL_SUPPORTED_FEATURES_LIST {
1278
    CXL_FEATURE_PATROL_SCRUB = 0,
1279
    CXL_FEATURE_ECS,
1280
    CXL_FEATURE_SPPR,
1281
    CXL_FEATURE_HPPR,
1282
    CXL_FEATURE_CACHELINE_SPARING,
1283
    CXL_FEATURE_ROW_SPARING,
1284
    CXL_FEATURE_BANK_SPARING,
1285
    CXL_FEATURE_RANK_SPARING,
1286
    CXL_FEATURE_MAX
1287
};
1288
1289
/* Get Feature CXL 3.1 Spec 8.2.9.6.2 */
1290
/*
1291
 * Get Feature input payload
1292
 * CXL r3.1 section 8.2.9.6.2 Table 8-99
1293
 */
1294
/* Get Feature : Payload in selection */
1295
enum CXL_GET_FEATURE_SELECTION {
1296
    CXL_GET_FEATURE_SEL_CURRENT_VALUE,
1297
    CXL_GET_FEATURE_SEL_DEFAULT_VALUE,
1298
    CXL_GET_FEATURE_SEL_SAVED_VALUE,
1299
    CXL_GET_FEATURE_SEL_MAX
1300
};
1301
1302
/* Set Feature CXL 3.1 Spec 8.2.9.6.3 */
1303
/*
1304
 * Set Feature input payload
1305
 * CXL r3.1 section 8.2.9.6.3 Table 8-101
1306
 */
1307
typedef struct CXLSetFeatureInHeader {
1308
        QemuUUID uuid;
1309
        uint32_t flags;
1310
        uint16_t offset;
1311
        uint8_t version;
1312
        uint8_t rsvd[9];
1313
} QEMU_PACKED QEMU_ALIGNED(16) CXLSetFeatureInHeader;
1314
1315
/* Set Feature : Payload in flags */
1316
0
#define CXL_SET_FEATURE_FLAG_DATA_TRANSFER_MASK   0x7
1317
enum CXL_SET_FEATURE_FLAG_DATA_TRANSFER {
1318
    CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER,
1319
    CXL_SET_FEATURE_FLAG_INITIATE_DATA_TRANSFER,
1320
    CXL_SET_FEATURE_FLAG_CONTINUE_DATA_TRANSFER,
1321
    CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER,
1322
    CXL_SET_FEATURE_FLAG_ABORT_DATA_TRANSFER,
1323
    CXL_SET_FEATURE_FLAG_DATA_TRANSFER_MAX
1324
};
1325
0
#define CXL_SET_FEAT_DATA_SAVED_ACROSS_RESET BIT(3)
1326
1327
/* CXL r3.2 section 8.2.10.7.2.1: sPPR Feature Discovery and Configuration */
1328
static const QemuUUID soft_ppr_uuid = {
1329
    .data = UUID(0x892ba475, 0xfad8, 0x474e, 0x9d, 0x3e,
1330
                 0x69, 0x2c, 0x91, 0x75, 0x68, 0xbb)
1331
};
1332
1333
typedef struct CXLMemSoftPPRSetFeature {
1334
        CXLSetFeatureInHeader hdr;
1335
        CXLMemSoftPPRWriteAttrs feat_data;
1336
} QEMU_PACKED QEMU_ALIGNED(16) CXLMemSoftPPRSetFeature;
1337
1338
/* CXL r3.2 section 8.2.10.7.2.2: hPPR Feature Discovery and Configuration */
1339
static const QemuUUID hard_ppr_uuid = {
1340
    .data = UUID(0x80ea4521, 0x786f, 0x4127, 0xaf, 0xb1,
1341
                 0xec, 0x74, 0x59, 0xfb, 0x0e, 0x24)
1342
};
1343
1344
typedef struct CXLMemHardPPRSetFeature {
1345
        CXLSetFeatureInHeader hdr;
1346
        CXLMemHardPPRWriteAttrs feat_data;
1347
} QEMU_PACKED QEMU_ALIGNED(16) CXLMemHardPPRSetFeature;
1348
1349
/* CXL r3.1 section 8.2.9.9.11.1: Device Patrol Scrub Control Feature */
1350
static const QemuUUID patrol_scrub_uuid = {
1351
    .data = UUID(0x96dad7d6, 0xfde8, 0x482b, 0xa7, 0x33,
1352
                 0x75, 0x77, 0x4e, 0x06, 0xdb, 0x8a)
1353
};
1354
1355
typedef struct CXLMemPatrolScrubSetFeature {
1356
        CXLSetFeatureInHeader hdr;
1357
        CXLMemPatrolScrubWriteAttrs feat_data;
1358
} QEMU_PACKED QEMU_ALIGNED(16) CXLMemPatrolScrubSetFeature;
1359
1360
/*
1361
 * CXL r3.1 section 8.2.9.9.11.2:
1362
 * DDR5 Error Check Scrub (ECS) Control Feature
1363
 */
1364
static const QemuUUID ecs_uuid = {
1365
    .data = UUID(0xe5b13f22, 0x2328, 0x4a14, 0xb8, 0xba,
1366
                 0xb9, 0x69, 0x1e, 0x89, 0x33, 0x86)
1367
};
1368
1369
typedef struct CXLMemECSSetFeature {
1370
        CXLSetFeatureInHeader hdr;
1371
        CXLMemECSWriteAttrs feat_data[];
1372
} QEMU_PACKED QEMU_ALIGNED(16) CXLMemECSSetFeature;
1373
1374
/*
1375
 * CXL r3.2 section 8.2.10.7.2.3:
1376
 * Memory Sparing Features Discovery and Configuration
1377
 */
1378
static const QemuUUID cacheline_sparing_uuid = {
1379
    .data = UUID(0x96C33386, 0x91dd, 0x44c7, 0x9e, 0xcb,
1380
                 0xfd, 0xaf, 0x65, 0x03, 0xba, 0xc4)
1381
};
1382
1383
static const QemuUUID row_sparing_uuid = {
1384
    .data = UUID(0x450ebf67, 0xb135, 0x4f97, 0xa4, 0x98,
1385
                 0xc2, 0xd5, 0x7f, 0x27, 0x9b, 0xed)
1386
};
1387
1388
static const QemuUUID bank_sparing_uuid = {
1389
    .data = UUID(0x78b79636, 0x90ac, 0x4b64, 0xa4, 0xef,
1390
                 0xfa, 0xac, 0x5d, 0x18, 0xa8, 0x63)
1391
};
1392
1393
static const QemuUUID rank_sparing_uuid = {
1394
    .data = UUID(0x34dbaff5, 0x0552, 0x4281, 0x8f, 0x76,
1395
                 0xda, 0x0b, 0x5e, 0x7a, 0x76, 0xa7)
1396
};
1397
1398
typedef struct CXLMemSparingSetFeature {
1399
        CXLSetFeatureInHeader hdr;
1400
        CXLMemSparingWriteAttrs feat_data;
1401
} QEMU_PACKED QEMU_ALIGNED(16) CXLMemSparingSetFeature;
1402
1403
/* CXL r3.1 section 8.2.9.6.1: Get Supported Features (Opcode 0500h) */
1404
static CXLRetCode cmd_features_get_supported(const struct cxl_cmd *cmd,
1405
                                             uint8_t *payload_in,
1406
                                             size_t len_in,
1407
                                             uint8_t *payload_out,
1408
                                             size_t *len_out,
1409
                                             CXLCCI *cci)
1410
0
{
1411
0
    struct {
1412
0
        uint32_t count;
1413
0
        uint16_t start_index;
1414
0
        uint16_t reserved;
1415
0
    } QEMU_PACKED QEMU_ALIGNED(16) * get_feats_in = (void *)payload_in;
1416
1417
0
    struct {
1418
0
        CXLSupportedFeatureHeader hdr;
1419
0
        CXLSupportedFeatureEntry feat_entries[];
1420
0
    } QEMU_PACKED QEMU_ALIGNED(16) * get_feats_out = (void *)payload_out;
1421
0
    uint16_t index, req_entries;
1422
0
    uint16_t entry;
1423
1424
0
    if (!object_dynamic_cast(OBJECT(cci->d), TYPE_CXL_TYPE3)) {
1425
0
        return CXL_MBOX_UNSUPPORTED;
1426
0
    }
1427
0
    if (get_feats_in->count < sizeof(CXLSupportedFeatureHeader) ||
1428
0
        get_feats_in->start_index >= CXL_FEATURE_MAX) {
1429
0
        return CXL_MBOX_INVALID_INPUT;
1430
0
    }
1431
1432
0
    req_entries = (get_feats_in->count -
1433
0
                   sizeof(CXLSupportedFeatureHeader)) /
1434
0
                   sizeof(CXLSupportedFeatureEntry);
1435
0
    req_entries = MIN(req_entries,
1436
0
                      (CXL_FEATURE_MAX - get_feats_in->start_index));
1437
1438
0
    for (entry = 0, index = get_feats_in->start_index;
1439
0
         entry < req_entries; index++) {
1440
0
        switch (index) {
1441
0
        case CXL_FEATURE_SPPR:
1442
            /* Fill supported feature entry for soft-PPR */
1443
0
            get_feats_out->feat_entries[entry++] =
1444
0
                           (struct CXLSupportedFeatureEntry) {
1445
0
                .uuid = soft_ppr_uuid,
1446
0
                .feat_index = index,
1447
0
                .get_feat_size = sizeof(CXLMemSoftPPRReadAttrs),
1448
0
                .set_feat_size = sizeof(CXLMemSoftPPRWriteAttrs),
1449
0
                .attr_flags = CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE |
1450
0
                              CXL_FEAT_ENTRY_ATTR_FLAG_SUPPORT_DEFAULT_SEL,
1451
0
                .get_feat_version = CXL_MEMDEV_SPPR_GET_FEATURE_VERSION,
1452
0
                .set_feat_version = CXL_MEMDEV_SPPR_SET_FEATURE_VERSION,
1453
0
                .set_feat_effects = CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE |
1454
0
                                    CXL_FEAT_ENTRY_SFE_CEL_VALID,
1455
0
            };
1456
0
            break;
1457
0
        case CXL_FEATURE_HPPR:
1458
            /* Fill supported feature entry for hard-PPR */
1459
0
            get_feats_out->feat_entries[entry++] =
1460
0
                           (struct CXLSupportedFeatureEntry) {
1461
0
                .uuid = hard_ppr_uuid,
1462
0
                .feat_index = index,
1463
0
                .get_feat_size = sizeof(CXLMemHardPPRReadAttrs),
1464
0
                .set_feat_size = sizeof(CXLMemHardPPRWriteAttrs),
1465
0
                .attr_flags = CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE |
1466
0
                              CXL_FEAT_ENTRY_ATTR_FLAG_SUPPORT_DEFAULT_SEL,
1467
0
                .get_feat_version = CXL_MEMDEV_HPPR_GET_FEATURE_VERSION,
1468
0
                .set_feat_version = CXL_MEMDEV_HPPR_SET_FEATURE_VERSION,
1469
0
                .set_feat_effects = CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE |
1470
0
                                    CXL_FEAT_ENTRY_SFE_CEL_VALID,
1471
0
            };
1472
0
            break;
1473
0
        case  CXL_FEATURE_PATROL_SCRUB:
1474
            /* Fill supported feature entry for device patrol scrub control */
1475
0
            get_feats_out->feat_entries[entry++] =
1476
0
                           (struct CXLSupportedFeatureEntry) {
1477
0
                .uuid = patrol_scrub_uuid,
1478
0
                .feat_index = index,
1479
0
                .get_feat_size = sizeof(CXLMemPatrolScrubReadAttrs),
1480
0
                .set_feat_size = sizeof(CXLMemPatrolScrubWriteAttrs),
1481
0
                .attr_flags = CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE,
1482
0
                .get_feat_version = CXL_MEMDEV_PS_GET_FEATURE_VERSION,
1483
0
                .set_feat_version = CXL_MEMDEV_PS_SET_FEATURE_VERSION,
1484
0
                .set_feat_effects = CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE |
1485
0
                                    CXL_FEAT_ENTRY_SFE_CEL_VALID,
1486
0
            };
1487
0
            break;
1488
0
        case  CXL_FEATURE_ECS:
1489
            /* Fill supported feature entry for device DDR5 ECS control */
1490
0
            get_feats_out->feat_entries[entry++] =
1491
0
                         (struct CXLSupportedFeatureEntry) {
1492
0
                .uuid = ecs_uuid,
1493
0
                .feat_index = index,
1494
0
                .get_feat_size = sizeof(CXLMemECSReadAttrs),
1495
0
                .set_feat_size = sizeof(CXLMemECSWriteAttrs),
1496
0
                .attr_flags = CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE,
1497
0
                .get_feat_version = CXL_ECS_GET_FEATURE_VERSION,
1498
0
                .set_feat_version = CXL_ECS_SET_FEATURE_VERSION,
1499
0
                .set_feat_effects = CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE |
1500
0
                                    CXL_FEAT_ENTRY_SFE_CEL_VALID,
1501
0
            };
1502
0
            break;
1503
0
        case CXL_FEATURE_CACHELINE_SPARING:
1504
            /* Fill supported feature entry for Cacheline Memory Sparing */
1505
0
            get_feats_out->feat_entries[entry++] =
1506
0
                           (struct CXLSupportedFeatureEntry) {
1507
0
                .uuid = cacheline_sparing_uuid,
1508
0
                .feat_index = index,
1509
0
                .get_feat_size = sizeof(CXLMemSparingReadAttrs),
1510
0
                .set_feat_size = sizeof(CXLMemSparingWriteAttrs),
1511
0
                .attr_flags = CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE |
1512
0
                            CXL_FEAT_ENTRY_ATTR_FLAG_SUPPORT_DEFAULT_SEL,
1513
0
                .get_feat_version = CXL_MEMDEV_SPARING_GET_FEATURE_VERSION,
1514
0
                .set_feat_version = CXL_MEMDEV_SPARING_SET_FEATURE_VERSION,
1515
0
                .set_feat_effects = CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE |
1516
0
                                    CXL_FEAT_ENTRY_SFE_CEL_VALID,
1517
0
            };
1518
0
            break;
1519
0
        case CXL_FEATURE_ROW_SPARING:
1520
            /* Fill supported feature entry for Row Memory Sparing */
1521
0
            get_feats_out->feat_entries[entry++] =
1522
0
                           (struct CXLSupportedFeatureEntry) {
1523
0
                .uuid = row_sparing_uuid,
1524
0
                .feat_index = index,
1525
0
                .get_feat_size = sizeof(CXLMemSparingReadAttrs),
1526
0
                .set_feat_size = sizeof(CXLMemSparingWriteAttrs),
1527
0
                .attr_flags = CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE |
1528
0
                            CXL_FEAT_ENTRY_ATTR_FLAG_SUPPORT_DEFAULT_SEL,
1529
0
                .get_feat_version = CXL_MEMDEV_SPARING_GET_FEATURE_VERSION,
1530
0
                .set_feat_version = CXL_MEMDEV_SPARING_SET_FEATURE_VERSION,
1531
0
                .set_feat_effects = CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE |
1532
0
                                    CXL_FEAT_ENTRY_SFE_CEL_VALID,
1533
0
            };
1534
0
            break;
1535
0
        case CXL_FEATURE_BANK_SPARING:
1536
            /* Fill supported feature entry for Bank Memory Sparing */
1537
0
            get_feats_out->feat_entries[entry++] =
1538
0
                           (struct CXLSupportedFeatureEntry) {
1539
0
                .uuid = bank_sparing_uuid,
1540
0
                .feat_index = index,
1541
0
                .get_feat_size = sizeof(CXLMemSparingReadAttrs),
1542
0
                .set_feat_size = sizeof(CXLMemSparingWriteAttrs),
1543
0
                .attr_flags = CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE |
1544
0
                            CXL_FEAT_ENTRY_ATTR_FLAG_SUPPORT_DEFAULT_SEL,
1545
0
                .get_feat_version = CXL_MEMDEV_SPARING_GET_FEATURE_VERSION,
1546
0
                .set_feat_version = CXL_MEMDEV_SPARING_SET_FEATURE_VERSION,
1547
0
                .set_feat_effects = CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE |
1548
0
                                    CXL_FEAT_ENTRY_SFE_CEL_VALID,
1549
0
            };
1550
0
            break;
1551
0
        case CXL_FEATURE_RANK_SPARING:
1552
            /* Fill supported feature entry for Rank Memory Sparing */
1553
0
            get_feats_out->feat_entries[entry++] =
1554
0
                           (struct CXLSupportedFeatureEntry) {
1555
0
                .uuid = rank_sparing_uuid,
1556
0
                .feat_index = index,
1557
0
                .get_feat_size = sizeof(CXLMemSparingReadAttrs),
1558
0
                .set_feat_size = sizeof(CXLMemSparingWriteAttrs),
1559
0
                .attr_flags = CXL_FEAT_ENTRY_ATTR_FLAG_CHANGABLE |
1560
0
                            CXL_FEAT_ENTRY_ATTR_FLAG_SUPPORT_DEFAULT_SEL,
1561
0
                .get_feat_version = CXL_MEMDEV_SPARING_GET_FEATURE_VERSION,
1562
0
                .set_feat_version = CXL_MEMDEV_SPARING_SET_FEATURE_VERSION,
1563
0
                .set_feat_effects = CXL_FEAT_ENTRY_SFE_IMMEDIATE_CONFIG_CHANGE |
1564
0
                                    CXL_FEAT_ENTRY_SFE_CEL_VALID,
1565
0
            };
1566
0
            break;
1567
0
        default:
1568
0
            __builtin_unreachable();
1569
0
        }
1570
0
    }
1571
0
    get_feats_out->hdr.nsuppfeats_dev = CXL_FEATURE_MAX;
1572
0
    get_feats_out->hdr.entries = req_entries;
1573
0
    *len_out = sizeof(CXLSupportedFeatureHeader) +
1574
0
                      req_entries * sizeof(CXLSupportedFeatureEntry);
1575
1576
0
    return CXL_MBOX_SUCCESS;
1577
0
}
1578
1579
/* CXL r3.1 section 8.2.9.6.2: Get Feature (Opcode 0501h) */
1580
static CXLRetCode cmd_features_get_feature(const struct cxl_cmd *cmd,
1581
                                           uint8_t *payload_in,
1582
                                           size_t len_in,
1583
                                           uint8_t *payload_out,
1584
                                           size_t *len_out,
1585
                                           CXLCCI *cci)
1586
0
{
1587
0
    struct {
1588
0
        QemuUUID uuid;
1589
0
        uint16_t offset;
1590
0
        uint16_t count;
1591
0
        uint8_t selection;
1592
0
    } QEMU_PACKED QEMU_ALIGNED(16) * get_feature;
1593
0
    uint16_t bytes_to_copy = 0;
1594
0
    CXLType3Dev *ct3d;
1595
0
    CXLSetFeatureInfo *set_feat_info;
1596
1597
0
    if (!object_dynamic_cast(OBJECT(cci->d), TYPE_CXL_TYPE3)) {
1598
0
        return CXL_MBOX_UNSUPPORTED;
1599
0
    }
1600
1601
0
    ct3d = CXL_TYPE3(cci->d);
1602
0
    get_feature = (void *)payload_in;
1603
1604
0
    set_feat_info = &ct3d->set_feat_info;
1605
0
    if (qemu_uuid_is_equal(&get_feature->uuid, &set_feat_info->uuid)) {
1606
0
        return CXL_MBOX_FEATURE_TRANSFER_IN_PROGRESS;
1607
0
    }
1608
1609
0
    if (get_feature->selection != CXL_GET_FEATURE_SEL_CURRENT_VALUE) {
1610
0
        return CXL_MBOX_UNSUPPORTED;
1611
0
    }
1612
0
    if (get_feature->offset + get_feature->count > cci->payload_max) {
1613
0
        return CXL_MBOX_INVALID_INPUT;
1614
0
    }
1615
1616
0
    if (qemu_uuid_is_equal(&get_feature->uuid, &patrol_scrub_uuid)) {
1617
0
        if (get_feature->offset >= sizeof(CXLMemPatrolScrubReadAttrs)) {
1618
0
            return CXL_MBOX_INVALID_INPUT;
1619
0
        }
1620
0
        bytes_to_copy = sizeof(CXLMemPatrolScrubReadAttrs) -
1621
0
                                             get_feature->offset;
1622
0
        bytes_to_copy = MIN(bytes_to_copy, get_feature->count);
1623
0
        memcpy(payload_out,
1624
0
               (uint8_t *)&ct3d->patrol_scrub_attrs + get_feature->offset,
1625
0
               bytes_to_copy);
1626
0
    } else if (qemu_uuid_is_equal(&get_feature->uuid, &ecs_uuid)) {
1627
0
        if (get_feature->offset >= sizeof(CXLMemECSReadAttrs)) {
1628
0
            return CXL_MBOX_INVALID_INPUT;
1629
0
        }
1630
0
        bytes_to_copy = sizeof(CXLMemECSReadAttrs) - get_feature->offset;
1631
0
        bytes_to_copy = MIN(bytes_to_copy, get_feature->count);
1632
0
        memcpy(payload_out,
1633
0
               (uint8_t *)&ct3d->ecs_attrs + get_feature->offset,
1634
0
               bytes_to_copy);
1635
0
    } else if (qemu_uuid_is_equal(&get_feature->uuid, &soft_ppr_uuid)) {
1636
0
        if (get_feature->offset >= sizeof(CXLMemSoftPPRReadAttrs)) {
1637
0
            return CXL_MBOX_INVALID_INPUT;
1638
0
        }
1639
0
        bytes_to_copy = sizeof(CXLMemSoftPPRReadAttrs) -
1640
0
                        get_feature->offset;
1641
0
        bytes_to_copy = MIN(bytes_to_copy, get_feature->count);
1642
0
        memcpy(payload_out,
1643
0
               (uint8_t *)&ct3d->soft_ppr_attrs + get_feature->offset,
1644
0
               bytes_to_copy);
1645
0
    } else if (qemu_uuid_is_equal(&get_feature->uuid, &hard_ppr_uuid)) {
1646
0
        if (get_feature->offset >= sizeof(CXLMemHardPPRReadAttrs)) {
1647
0
            return CXL_MBOX_INVALID_INPUT;
1648
0
        }
1649
0
        bytes_to_copy = sizeof(CXLMemHardPPRReadAttrs) -
1650
0
                        get_feature->offset;
1651
0
        bytes_to_copy = MIN(bytes_to_copy, get_feature->count);
1652
0
        memcpy(payload_out,
1653
0
               (uint8_t *)&ct3d->hard_ppr_attrs + get_feature->offset,
1654
0
               bytes_to_copy);
1655
0
    } else if (qemu_uuid_is_equal(&get_feature->uuid,
1656
0
                                  &cacheline_sparing_uuid)) {
1657
0
        if (get_feature->offset >= sizeof(CXLMemSparingReadAttrs)) {
1658
0
            return CXL_MBOX_INVALID_INPUT;
1659
0
        }
1660
0
        bytes_to_copy = sizeof(CXLMemSparingReadAttrs) -
1661
0
                                             get_feature->offset;
1662
0
        bytes_to_copy = MIN(bytes_to_copy, get_feature->count);
1663
0
        memcpy(payload_out,
1664
0
               (uint8_t *)&ct3d->cacheline_sparing_attrs + get_feature->offset,
1665
0
               bytes_to_copy);
1666
0
    } else if (qemu_uuid_is_equal(&get_feature->uuid, &row_sparing_uuid)) {
1667
0
        if (get_feature->offset >= sizeof(CXLMemSparingReadAttrs)) {
1668
0
            return CXL_MBOX_INVALID_INPUT;
1669
0
        }
1670
0
        bytes_to_copy = sizeof(CXLMemSparingReadAttrs) -
1671
0
                                             get_feature->offset;
1672
0
        bytes_to_copy = MIN(bytes_to_copy, get_feature->count);
1673
0
        memcpy(payload_out,
1674
0
               (uint8_t *)&ct3d->row_sparing_attrs + get_feature->offset,
1675
0
               bytes_to_copy);
1676
0
    } else if (qemu_uuid_is_equal(&get_feature->uuid, &bank_sparing_uuid)) {
1677
0
        if (get_feature->offset >= sizeof(CXLMemSparingReadAttrs)) {
1678
0
            return CXL_MBOX_INVALID_INPUT;
1679
0
        }
1680
0
        bytes_to_copy = sizeof(CXLMemSparingReadAttrs) -
1681
0
                                             get_feature->offset;
1682
0
        bytes_to_copy = MIN(bytes_to_copy, get_feature->count);
1683
0
        memcpy(payload_out,
1684
0
               (uint8_t *)&ct3d->bank_sparing_attrs + get_feature->offset,
1685
0
               bytes_to_copy);
1686
0
    } else if (qemu_uuid_is_equal(&get_feature->uuid, &rank_sparing_uuid)) {
1687
0
        if (get_feature->offset >= sizeof(CXLMemSparingReadAttrs)) {
1688
0
            return CXL_MBOX_INVALID_INPUT;
1689
0
        }
1690
0
        bytes_to_copy = sizeof(CXLMemSparingReadAttrs) -
1691
0
                                             get_feature->offset;
1692
0
        bytes_to_copy = MIN(bytes_to_copy, get_feature->count);
1693
0
        memcpy(payload_out,
1694
0
               (uint8_t *)&ct3d->rank_sparing_attrs + get_feature->offset,
1695
0
               bytes_to_copy);
1696
0
    } else {
1697
0
        return CXL_MBOX_UNSUPPORTED;
1698
0
    }
1699
1700
0
    *len_out = bytes_to_copy;
1701
1702
0
    return CXL_MBOX_SUCCESS;
1703
0
}
1704
1705
/* CXL r3.1 section 8.2.9.6.3: Set Feature (Opcode 0502h) */
1706
static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
1707
                                           uint8_t *payload_in,
1708
                                           size_t len_in,
1709
                                           uint8_t *payload_out,
1710
                                           size_t *len_out,
1711
                                           CXLCCI *cci)
1712
0
{
1713
0
    CXLSetFeatureInHeader *hdr = (void *)payload_in;
1714
0
    CXLSetFeatureInfo *set_feat_info;
1715
0
    uint16_t bytes_to_copy = 0;
1716
0
    uint8_t data_transfer_flag;
1717
0
    CXLType3Dev *ct3d;
1718
0
    uint16_t count;
1719
1720
0
    if (len_in < sizeof(*hdr)) {
1721
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1722
0
    }
1723
1724
0
    if (!object_dynamic_cast(OBJECT(cci->d), TYPE_CXL_TYPE3)) {
1725
0
        return CXL_MBOX_UNSUPPORTED;
1726
0
    }
1727
0
    ct3d = CXL_TYPE3(cci->d);
1728
0
    set_feat_info = &ct3d->set_feat_info;
1729
1730
0
    if (!qemu_uuid_is_null(&set_feat_info->uuid) &&
1731
0
        !qemu_uuid_is_equal(&hdr->uuid, &set_feat_info->uuid)) {
1732
0
        return CXL_MBOX_FEATURE_TRANSFER_IN_PROGRESS;
1733
0
    }
1734
0
    if (hdr->flags & CXL_SET_FEAT_DATA_SAVED_ACROSS_RESET) {
1735
0
        set_feat_info->data_saved_across_reset = true;
1736
0
    } else {
1737
0
        set_feat_info->data_saved_across_reset = false;
1738
0
    }
1739
1740
0
    data_transfer_flag =
1741
0
              hdr->flags & CXL_SET_FEATURE_FLAG_DATA_TRANSFER_MASK;
1742
0
    if (data_transfer_flag == CXL_SET_FEATURE_FLAG_INITIATE_DATA_TRANSFER) {
1743
0
        set_feat_info->uuid = hdr->uuid;
1744
0
        set_feat_info->data_size = 0;
1745
0
    }
1746
0
    set_feat_info->data_transfer_flag = data_transfer_flag;
1747
0
    set_feat_info->data_offset = hdr->offset;
1748
0
    bytes_to_copy = len_in - sizeof(CXLSetFeatureInHeader);
1749
1750
0
    if (bytes_to_copy == 0) {
1751
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1752
0
    }
1753
1754
0
    if (qemu_uuid_is_equal(&hdr->uuid, &patrol_scrub_uuid)) {
1755
0
        CXLMemPatrolScrubSetFeature *ps_set_feature = (void *)payload_in;
1756
0
        CXLMemPatrolScrubWriteAttrs *ps_write_attrs =
1757
0
                                &ps_set_feature->feat_data;
1758
1759
0
        if (hdr->version != CXL_MEMDEV_PS_SET_FEATURE_VERSION) {
1760
0
            return CXL_MBOX_UNSUPPORTED;
1761
0
        }
1762
1763
0
        if ((uint32_t)hdr->offset + bytes_to_copy >
1764
0
            sizeof(ct3d->patrol_scrub_wr_attrs)) {
1765
0
            return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1766
0
        }
1767
0
        memcpy((uint8_t *)&ct3d->patrol_scrub_wr_attrs + hdr->offset,
1768
0
               ps_write_attrs,
1769
0
               bytes_to_copy);
1770
0
        set_feat_info->data_size += bytes_to_copy;
1771
1772
0
        if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1773
0
            data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
1774
0
            ct3d->patrol_scrub_attrs.scrub_cycle &= ~0xFF;
1775
0
            ct3d->patrol_scrub_attrs.scrub_cycle |=
1776
0
                          ct3d->patrol_scrub_wr_attrs.scrub_cycle_hr & 0xFF;
1777
0
            ct3d->patrol_scrub_attrs.scrub_flags &= ~0x1;
1778
0
            ct3d->patrol_scrub_attrs.scrub_flags |=
1779
0
                          ct3d->patrol_scrub_wr_attrs.scrub_flags & 0x1;
1780
0
        }
1781
0
    } else if (qemu_uuid_is_equal(&hdr->uuid,
1782
0
                                  &ecs_uuid)) {
1783
0
        CXLMemECSSetFeature *ecs_set_feature = (void *)payload_in;
1784
0
        CXLMemECSWriteAttrs *ecs_write_attrs = ecs_set_feature->feat_data;
1785
1786
0
        if (hdr->version != CXL_ECS_SET_FEATURE_VERSION) {
1787
0
            return CXL_MBOX_UNSUPPORTED;
1788
0
        }
1789
1790
0
        if ((uint32_t)hdr->offset + bytes_to_copy >
1791
0
            sizeof(ct3d->ecs_wr_attrs)) {
1792
0
            return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1793
0
        }
1794
0
        memcpy((uint8_t *)&ct3d->ecs_wr_attrs + hdr->offset,
1795
0
               ecs_write_attrs,
1796
0
               bytes_to_copy);
1797
0
        set_feat_info->data_size += bytes_to_copy;
1798
1799
0
        if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1800
0
            data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
1801
0
            ct3d->ecs_attrs.ecs_log_cap = ct3d->ecs_wr_attrs.ecs_log_cap;
1802
0
            for (count = 0; count < CXL_ECS_NUM_MEDIA_FRUS; count++) {
1803
0
                ct3d->ecs_attrs.fru_attrs[count].ecs_config =
1804
0
                        ct3d->ecs_wr_attrs.fru_attrs[count].ecs_config & 0x1F;
1805
0
            }
1806
0
        }
1807
0
    } else if (qemu_uuid_is_equal(&hdr->uuid, &soft_ppr_uuid)) {
1808
0
        CXLMemSoftPPRSetFeature *sppr_set_feature = (void *)payload_in;
1809
0
        CXLMemSoftPPRWriteAttrs *sppr_write_attrs =
1810
0
                            &sppr_set_feature->feat_data;
1811
1812
0
        if (hdr->version != CXL_MEMDEV_SPPR_SET_FEATURE_VERSION) {
1813
0
            return CXL_MBOX_UNSUPPORTED;
1814
0
        }
1815
1816
0
        if ((uint32_t)hdr->offset + bytes_to_copy >
1817
0
            sizeof(ct3d->soft_ppr_wr_attrs)) {
1818
0
            return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1819
0
        }
1820
0
        memcpy((uint8_t *)&ct3d->soft_ppr_wr_attrs + hdr->offset,
1821
0
               sppr_write_attrs, bytes_to_copy);
1822
0
        set_feat_info->data_size += bytes_to_copy;
1823
1824
0
        if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1825
0
            data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
1826
0
            ct3d->soft_ppr_attrs.op_mode = ct3d->soft_ppr_wr_attrs.op_mode;
1827
0
            ct3d->soft_ppr_attrs.sppr_op_mode =
1828
0
                    ct3d->soft_ppr_wr_attrs.sppr_op_mode;
1829
0
        }
1830
0
    } else if (qemu_uuid_is_equal(&hdr->uuid, &hard_ppr_uuid)) {
1831
0
        CXLMemHardPPRSetFeature *hppr_set_feature = (void *)payload_in;
1832
0
        CXLMemHardPPRWriteAttrs *hppr_write_attrs =
1833
0
                            &hppr_set_feature->feat_data;
1834
1835
0
        if (hdr->version != CXL_MEMDEV_HPPR_SET_FEATURE_VERSION) {
1836
0
            return CXL_MBOX_UNSUPPORTED;
1837
0
        }
1838
1839
0
        if ((uint32_t)hdr->offset + bytes_to_copy >
1840
0
            sizeof(ct3d->hard_ppr_wr_attrs)) {
1841
0
            return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1842
0
        }
1843
0
        memcpy((uint8_t *)&ct3d->hard_ppr_wr_attrs + hdr->offset,
1844
0
               hppr_write_attrs, bytes_to_copy);
1845
0
        set_feat_info->data_size += bytes_to_copy;
1846
1847
0
        if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1848
0
            data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
1849
0
            ct3d->hard_ppr_attrs.op_mode = ct3d->hard_ppr_wr_attrs.op_mode;
1850
0
            ct3d->hard_ppr_attrs.hppr_op_mode =
1851
0
                    ct3d->hard_ppr_wr_attrs.hppr_op_mode;
1852
0
        }
1853
0
    } else if (qemu_uuid_is_equal(&hdr->uuid, &cacheline_sparing_uuid)) {
1854
0
        CXLMemSparingSetFeature *mem_sparing_set_feature = (void *)payload_in;
1855
0
        CXLMemSparingWriteAttrs *mem_sparing_write_attrs =
1856
0
                            &mem_sparing_set_feature->feat_data;
1857
1858
0
        if (hdr->version != CXL_MEMDEV_SPARING_SET_FEATURE_VERSION) {
1859
0
            return CXL_MBOX_UNSUPPORTED;
1860
0
        }
1861
1862
0
        if ((uint32_t)hdr->offset + bytes_to_copy >
1863
0
            sizeof(ct3d->cacheline_sparing_wr_attrs)) {
1864
0
            return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1865
0
        }
1866
0
        memcpy((uint8_t *)&ct3d->cacheline_sparing_wr_attrs + hdr->offset,
1867
0
               mem_sparing_write_attrs, bytes_to_copy);
1868
0
        set_feat_info->data_size += bytes_to_copy;
1869
1870
0
        if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1871
0
            data_transfer_flag == CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
1872
0
            ct3d->cacheline_sparing_attrs.op_mode =
1873
0
                                    ct3d->cacheline_sparing_wr_attrs.op_mode;
1874
0
        }
1875
0
    } else if (qemu_uuid_is_equal(&hdr->uuid, &row_sparing_uuid)) {
1876
0
        CXLMemSparingSetFeature *mem_sparing_set_feature = (void *)payload_in;
1877
0
        CXLMemSparingWriteAttrs *mem_sparing_write_attrs =
1878
0
                            &mem_sparing_set_feature->feat_data;
1879
1880
0
        if (hdr->version != CXL_MEMDEV_SPARING_SET_FEATURE_VERSION) {
1881
0
            return CXL_MBOX_UNSUPPORTED;
1882
0
        }
1883
1884
0
        if ((uint32_t)hdr->offset + bytes_to_copy >
1885
0
            sizeof(ct3d->row_sparing_wr_attrs)) {
1886
0
            return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1887
0
        }
1888
0
        memcpy((uint8_t *)&ct3d->row_sparing_wr_attrs + hdr->offset,
1889
0
               mem_sparing_write_attrs, bytes_to_copy);
1890
0
        set_feat_info->data_size += bytes_to_copy;
1891
1892
0
        if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1893
0
            data_transfer_flag == CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
1894
0
            ct3d->row_sparing_attrs.op_mode =
1895
0
                              ct3d->row_sparing_wr_attrs.op_mode;
1896
0
        }
1897
0
    } else if (qemu_uuid_is_equal(&hdr->uuid, &bank_sparing_uuid)) {
1898
0
        CXLMemSparingSetFeature *mem_sparing_set_feature = (void *)payload_in;
1899
0
        CXLMemSparingWriteAttrs *mem_sparing_write_attrs =
1900
0
                            &mem_sparing_set_feature->feat_data;
1901
1902
0
        if (hdr->version != CXL_MEMDEV_SPARING_SET_FEATURE_VERSION) {
1903
0
            return CXL_MBOX_UNSUPPORTED;
1904
0
        }
1905
1906
0
        if ((uint32_t)hdr->offset + bytes_to_copy >
1907
0
            sizeof(ct3d->bank_sparing_wr_attrs)) {
1908
0
            return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1909
0
        }
1910
0
        memcpy((uint8_t *)&ct3d->bank_sparing_wr_attrs + hdr->offset,
1911
0
               mem_sparing_write_attrs, bytes_to_copy);
1912
0
        set_feat_info->data_size += bytes_to_copy;
1913
1914
0
        if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1915
0
            data_transfer_flag == CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
1916
0
            ct3d->bank_sparing_attrs.op_mode =
1917
0
                              ct3d->bank_sparing_wr_attrs.op_mode;
1918
0
        }
1919
0
    } else if (qemu_uuid_is_equal(&hdr->uuid, &rank_sparing_uuid)) {
1920
0
        CXLMemSparingSetFeature *mem_sparing_set_feature = (void *)payload_in;
1921
0
        CXLMemSparingWriteAttrs *mem_sparing_write_attrs =
1922
0
                            &mem_sparing_set_feature->feat_data;
1923
1924
0
        if (hdr->version != CXL_MEMDEV_SPARING_SET_FEATURE_VERSION) {
1925
0
            return CXL_MBOX_UNSUPPORTED;
1926
0
        }
1927
1928
0
        if ((uint32_t)hdr->offset + bytes_to_copy >
1929
0
            sizeof(ct3d->rank_sparing_wr_attrs)) {
1930
0
            return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
1931
0
        }
1932
0
        memcpy((uint8_t *)&ct3d->rank_sparing_wr_attrs + hdr->offset,
1933
0
               mem_sparing_write_attrs, bytes_to_copy);
1934
0
        set_feat_info->data_size += bytes_to_copy;
1935
1936
0
        if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1937
0
            data_transfer_flag == CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
1938
0
            ct3d->rank_sparing_attrs.op_mode =
1939
0
                             ct3d->rank_sparing_wr_attrs.op_mode;
1940
0
        }
1941
0
    } else {
1942
0
        return CXL_MBOX_UNSUPPORTED;
1943
0
    }
1944
1945
0
    if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
1946
0
        data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER ||
1947
0
        data_transfer_flag ==  CXL_SET_FEATURE_FLAG_ABORT_DATA_TRANSFER) {
1948
0
        memset(&set_feat_info->uuid, 0, sizeof(QemuUUID));
1949
0
        if (qemu_uuid_is_equal(&hdr->uuid, &patrol_scrub_uuid)) {
1950
0
            memset(&ct3d->patrol_scrub_wr_attrs, 0, set_feat_info->data_size);
1951
0
        } else if (qemu_uuid_is_equal(&hdr->uuid, &ecs_uuid)) {
1952
0
            memset(&ct3d->ecs_wr_attrs, 0, set_feat_info->data_size);
1953
0
        } else if (qemu_uuid_is_equal(&hdr->uuid, &soft_ppr_uuid)) {
1954
0
            memset(&ct3d->soft_ppr_wr_attrs, 0, set_feat_info->data_size);
1955
0
        } else if (qemu_uuid_is_equal(&hdr->uuid, &hard_ppr_uuid)) {
1956
0
            memset(&ct3d->hard_ppr_wr_attrs, 0, set_feat_info->data_size);
1957
0
        } else if (qemu_uuid_is_equal(&hdr->uuid, &cacheline_sparing_uuid)) {
1958
0
            memset(&ct3d->cacheline_sparing_wr_attrs, 0,
1959
0
                   set_feat_info->data_size);
1960
0
        } else if (qemu_uuid_is_equal(&hdr->uuid, &row_sparing_uuid)) {
1961
0
            memset(&ct3d->row_sparing_wr_attrs, 0, set_feat_info->data_size);
1962
0
        } else if (qemu_uuid_is_equal(&hdr->uuid, &bank_sparing_uuid)) {
1963
0
            memset(&ct3d->bank_sparing_wr_attrs, 0, set_feat_info->data_size);
1964
0
        } else if (qemu_uuid_is_equal(&hdr->uuid, &rank_sparing_uuid)) {
1965
0
            memset(&ct3d->rank_sparing_wr_attrs, 0, set_feat_info->data_size);
1966
0
        }
1967
0
        set_feat_info->data_transfer_flag = 0;
1968
0
        set_feat_info->data_saved_across_reset = false;
1969
0
        set_feat_info->data_offset = 0;
1970
0
        set_feat_info->data_size = 0;
1971
0
    }
1972
1973
0
    return CXL_MBOX_SUCCESS;
1974
0
}
1975
1976
0
#define CXL_MEM_SPARING_FLAGS_QUERY_RESOURCES BIT(0)
1977
#define CXL_MEM_SPARING_FLAGS_HARD_SPARING BIT(1)
1978
0
#define CXL_MEM_SPARING_FLAGS_SUB_CHANNEL_VALID BIT(2)
1979
0
#define CXL_MEM_SPARING_FLAGS_NIB_MASK_VALID BIT(3)
1980
1981
typedef struct CXLMemSparingMaintInPayload {
1982
    uint8_t flags;
1983
    uint8_t channel;
1984
    uint8_t rank;
1985
    uint8_t nibble_mask[3];
1986
    uint8_t bank_group;
1987
    uint8_t bank;
1988
    uint8_t row[3];
1989
    uint16_t column;
1990
    uint8_t sub_channel;
1991
} QEMU_PACKED CXLMemSparingMaintInPayload;
1992
1993
static void cxl_create_mem_sparing_event_records(CXLType3Dev *ct3d,
1994
                            uint8_t maint_op_class, uint8_t maint_op_sub_class,
1995
                            CXLMaintenance *ent,
1996
                            CXLMemSparingMaintInPayload *sparing_pi)
1997
0
{
1998
0
    CXLEventSparing event_rec = {};
1999
2000
0
    cxl_assign_event_header(&event_rec.hdr,
2001
0
                            &sparing_uuid,
2002
0
                            (1 << CXL_EVENT_TYPE_INFO),
2003
0
                            sizeof(event_rec),
2004
0
                            cxl_device_get_timestamp(&ct3d->cxl_dstate),
2005
0
                            1, maint_op_class, 1, maint_op_sub_class,
2006
0
                            0, 0, 0, 0);
2007
0
    if (ent) {
2008
0
        event_rec.flags = 0;
2009
0
        event_rec.result = 0;
2010
0
        stw_le_p(&event_rec.res_avail, 2);
2011
0
        stw_le_p(&event_rec.validity_flags, ent->validity_flags);
2012
0
        event_rec.channel = ent->channel;
2013
0
        event_rec.rank = ent->rank;
2014
0
        st24_le_p(event_rec.nibble_mask, ent->nibble_mask);
2015
0
        event_rec.bank_group = ent->bank_group;
2016
0
        event_rec.bank = ent->bank;
2017
0
        st24_le_p(event_rec.row, ent->row);
2018
0
        stw_le_p(&event_rec.column, ent->column);
2019
0
        event_rec.sub_channel = ent->sub_channel;
2020
0
        if (ent->validity_flags & CXL_MSER_VALID_COMP_ID) {
2021
0
            memcpy(event_rec.component_id, ent->component_id,
2022
0
                   sizeof(event_rec.component_id));
2023
0
        }
2024
0
    } else if (sparing_pi) {
2025
0
        event_rec.flags = CXL_MSER_FLAGS_QUERY_RESOURCES;
2026
0
        event_rec.result = 0;
2027
0
        event_rec.validity_flags = CXL_MSER_VALID_CHANNEL |
2028
0
                                   CXL_MSER_VALID_RANK |
2029
0
                                   CXL_MSER_VALID_NIB_MASK |
2030
0
                                   CXL_MSER_VALID_BANK_GROUP |
2031
0
                                   CXL_MSER_VALID_BANK |
2032
0
                                   CXL_MSER_VALID_ROW |
2033
0
                                   CXL_MSER_VALID_COLUMN;
2034
0
        event_rec.res_avail = 1;
2035
0
        event_rec.channel = sparing_pi->channel;
2036
0
        event_rec.rank = sparing_pi->rank;
2037
0
        if (sparing_pi->flags & CXL_MEM_SPARING_FLAGS_NIB_MASK_VALID) {
2038
0
            memcpy(event_rec.nibble_mask, sparing_pi->nibble_mask,
2039
0
                   sizeof(sparing_pi->nibble_mask));
2040
0
        }
2041
0
        event_rec.bank_group = sparing_pi->bank_group;
2042
0
        event_rec.bank = sparing_pi->bank;
2043
0
        event_rec.column = sparing_pi->column;
2044
0
        memcpy(event_rec.row, sparing_pi->row, sizeof(sparing_pi->row));
2045
0
        if (sparing_pi->flags & CXL_MEM_SPARING_FLAGS_SUB_CHANNEL_VALID) {
2046
0
            event_rec.sub_channel = sparing_pi->sub_channel;
2047
0
            event_rec.validity_flags |= CXL_MSER_VALID_SUB_CHANNEL;
2048
0
        }
2049
0
    } else {
2050
0
        return;
2051
0
    }
2052
2053
0
    if (cxl_event_insert(&ct3d->cxl_dstate,
2054
0
                         CXL_EVENT_TYPE_INFO,
2055
0
                         (CXLEventRecordRaw *)&event_rec)) {
2056
0
        cxl_event_irq_assert(ct3d);
2057
0
    }
2058
0
}
2059
2060
static CXLRetCode cxl_perform_mem_sparing(CXLType3Dev *ct3d, uint8_t sub_class,
2061
                                          void *maint_pi)
2062
0
{
2063
0
    switch (sub_class) {
2064
0
    case CXL_MEMDEV_MAINT_SUBCLASS_CACHELINE_SPARING:
2065
0
        qemu_log("Cacheline Memory Sparing\n");
2066
0
        return CXL_MBOX_SUCCESS;
2067
0
    case CXL_MEMDEV_MAINT_SUBCLASS_ROW_SPARING:
2068
0
        qemu_log("Row Memory Sparing\n");
2069
0
        return CXL_MBOX_SUCCESS;
2070
0
    case CXL_MEMDEV_MAINT_SUBCLASS_BANK_SPARING:
2071
0
        qemu_log("Bank Memory Sparing\n");
2072
0
        return CXL_MBOX_SUCCESS;
2073
0
    case CXL_MEMDEV_MAINT_SUBCLASS_RANK_SPARING:
2074
0
        qemu_log("Rank Memory Sparing\n");
2075
0
        return CXL_MBOX_SUCCESS;
2076
0
    default:
2077
0
        return CXL_MBOX_INVALID_INPUT;
2078
0
    }
2079
0
}
2080
2081
static void cxl_perform_ppr(CXLType3Dev *ct3d, uint64_t dpa)
2082
0
{
2083
0
    CXLMaintenance *ent, *next;
2084
2085
0
    QLIST_FOREACH_SAFE(ent, &ct3d->maint_list, node, next) {
2086
0
        if (dpa == ent->dpa) {
2087
            /* Produce a Memory Sparing Event Record */
2088
0
            if (ct3d->soft_ppr_attrs.sppr_op_mode &
2089
0
                CXL_MEMDEV_SPPR_OP_MODE_MEM_SPARING_EV_REC_EN) {
2090
0
                cxl_create_mem_sparing_event_records(ct3d,
2091
0
                                CXL_MEMDEV_MAINT_CLASS_SPARING,
2092
0
                                CXL_MEMDEV_MAINT_SUBCLASS_CACHELINE_SPARING,
2093
0
                                ent, NULL);
2094
0
            }
2095
0
            QLIST_REMOVE(ent, node);
2096
0
            g_free(ent);
2097
0
            break;
2098
0
        }
2099
0
    }
2100
0
}
2101
2102
/* CXL r3.2 section 8.2.10.7.1 - Perform Maintenance (Opcode 0600h) */
2103
0
#define MAINTENANCE_PPR_QUERY_RESOURCES BIT(0)
2104
2105
static CXLRetCode cmd_media_perform_maintenance(const struct cxl_cmd *cmd,
2106
                                   uint8_t *payload_in, size_t len_in,
2107
                                   uint8_t *payload_out, size_t *len_out,
2108
                                   CXLCCI *cci)
2109
0
{
2110
0
    struct {
2111
0
        uint8_t class;
2112
0
        uint8_t subclass;
2113
0
        union {
2114
0
            struct {
2115
0
                uint8_t flags;
2116
0
                uint64_t dpa;
2117
0
                uint8_t nibble_mask[3];
2118
0
            } QEMU_PACKED ppr;
2119
0
            CXLMemSparingMaintInPayload mem_sparing_pi;
2120
0
        };
2121
0
    } QEMU_PACKED *maint_in = (void *)payload_in;
2122
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2123
2124
0
    if (maintenance_running(cci)) {
2125
0
        return CXL_MBOX_BUSY;
2126
0
    }
2127
2128
0
    switch (maint_in->class) {
2129
0
    case CXL_MEMDEV_MAINT_CLASS_NO_OP:
2130
0
        return CXL_MBOX_SUCCESS; /* nop */
2131
0
    case CXL_MEMDEV_MAINT_CLASS_PPR:
2132
0
        if (maint_in->ppr.flags & MAINTENANCE_PPR_QUERY_RESOURCES) {
2133
0
            return CXL_MBOX_SUCCESS;
2134
0
        }
2135
2136
0
        switch (maint_in->subclass) {
2137
0
        case CXL_MEMDEV_MAINT_SUBCLASS_SPPR:
2138
0
        case CXL_MEMDEV_MAINT_SUBCLASS_HPPR:
2139
0
            cxl_perform_ppr(ct3d, ldq_le_p(&maint_in->ppr.dpa));
2140
0
            return CXL_MBOX_SUCCESS;
2141
0
        default:
2142
0
            return CXL_MBOX_INVALID_INPUT;
2143
0
        }
2144
0
        break;
2145
0
    case CXL_MEMDEV_MAINT_CLASS_SPARING:
2146
0
        if (maint_in->mem_sparing_pi.flags &
2147
0
            CXL_MEM_SPARING_FLAGS_QUERY_RESOURCES) {
2148
            /*
2149
             * CXL r3.2 sect 8.2.10.7.1.4 - Memory Sparing Maintenance Operation
2150
             * Produce Memory Sparing Event record to report resources
2151
             * availability.
2152
             */
2153
0
            cxl_create_mem_sparing_event_records(ct3d, maint_in->class,
2154
0
                                                 maint_in->subclass, NULL,
2155
0
                                                 &maint_in->mem_sparing_pi);
2156
2157
0
            return CXL_MBOX_SUCCESS;
2158
0
        }
2159
2160
0
        switch (maint_in->subclass) {
2161
0
        case CXL_MEMDEV_MAINT_SUBCLASS_CACHELINE_SPARING:
2162
0
        case CXL_MEMDEV_MAINT_SUBCLASS_ROW_SPARING:
2163
0
        case CXL_MEMDEV_MAINT_SUBCLASS_BANK_SPARING:
2164
0
        case CXL_MEMDEV_MAINT_SUBCLASS_RANK_SPARING:
2165
0
            return cxl_perform_mem_sparing(ct3d, maint_in->subclass,
2166
0
                                           &maint_in->mem_sparing_pi);
2167
0
        default:
2168
0
            return CXL_MBOX_INVALID_INPUT;
2169
0
        }
2170
0
        break;
2171
0
    default:
2172
0
        return CXL_MBOX_INVALID_INPUT;
2173
0
    }
2174
2175
0
    return CXL_MBOX_SUCCESS;
2176
0
}
2177
2178
/* CXL r3.1 Section 8.2.9.9.1.1: Identify Memory Device (Opcode 4000h) */
2179
static CXLRetCode cmd_identify_memory_device(const struct cxl_cmd *cmd,
2180
                                             uint8_t *payload_in,
2181
                                             size_t len_in,
2182
                                             uint8_t *payload_out,
2183
                                             size_t *len_out,
2184
                                             CXLCCI *cci)
2185
0
{
2186
0
    struct {
2187
0
        char fw_revision[0x10];
2188
0
        uint64_t total_capacity;
2189
0
        uint64_t volatile_capacity;
2190
0
        uint64_t persistent_capacity;
2191
0
        uint64_t partition_align;
2192
0
        uint16_t info_event_log_size;
2193
0
        uint16_t warning_event_log_size;
2194
0
        uint16_t failure_event_log_size;
2195
0
        uint16_t fatal_event_log_size;
2196
0
        uint32_t lsa_size;
2197
0
        uint8_t poison_list_max_mer[3];
2198
0
        uint16_t inject_poison_limit;
2199
0
        uint8_t poison_caps;
2200
0
        uint8_t qos_telemetry_caps;
2201
0
        uint16_t dc_event_log_size;
2202
0
    } QEMU_PACKED *id;
2203
0
    QEMU_BUILD_BUG_ON(sizeof(*id) != 0x45);
2204
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2205
0
    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
2206
0
    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
2207
2208
0
    if ((!QEMU_IS_ALIGNED(cxl_dstate->vmem_size, CXL_CAPACITY_MULTIPLIER)) ||
2209
0
        (!QEMU_IS_ALIGNED(cxl_dstate->pmem_size, CXL_CAPACITY_MULTIPLIER)) ||
2210
0
        (!QEMU_IS_ALIGNED(ct3d->dc.total_capacity, CXL_CAPACITY_MULTIPLIER))) {
2211
0
        return CXL_MBOX_INTERNAL_ERROR;
2212
0
    }
2213
2214
0
    id = (void *)payload_out;
2215
2216
0
    snprintf(id->fw_revision, 0x10, "BWFW VERSION %02d", 0);
2217
2218
0
    stq_le_p(&id->total_capacity,
2219
0
             cxl_dstate->static_mem_size / CXL_CAPACITY_MULTIPLIER);
2220
0
    stq_le_p(&id->persistent_capacity,
2221
0
             cxl_dstate->pmem_size / CXL_CAPACITY_MULTIPLIER);
2222
0
    stq_le_p(&id->volatile_capacity,
2223
0
             cxl_dstate->vmem_size / CXL_CAPACITY_MULTIPLIER);
2224
0
    stl_le_p(&id->lsa_size, cvc->get_lsa_size(ct3d));
2225
    /* 256 poison records */
2226
0
    st24_le_p(id->poison_list_max_mer, 256);
2227
    /* No limit - so limited by main poison record limit */
2228
0
    stw_le_p(&id->inject_poison_limit, 0);
2229
0
    stw_le_p(&id->dc_event_log_size, CXL_DC_EVENT_LOG_SIZE);
2230
2231
0
    *len_out = sizeof(*id);
2232
0
    return CXL_MBOX_SUCCESS;
2233
0
}
2234
2235
/* CXL r3.1 Section 8.2.9.9.2.1: Get Partition Info (Opcode 4100h) */
2236
static CXLRetCode cmd_ccls_get_partition_info(const struct cxl_cmd *cmd,
2237
                                              uint8_t *payload_in,
2238
                                              size_t len_in,
2239
                                              uint8_t *payload_out,
2240
                                              size_t *len_out,
2241
                                              CXLCCI *cci)
2242
0
{
2243
0
    CXLDeviceState *cxl_dstate = &CXL_TYPE3(cci->d)->cxl_dstate;
2244
0
    struct {
2245
0
        uint64_t active_vmem;
2246
0
        uint64_t active_pmem;
2247
0
        uint64_t next_vmem;
2248
0
        uint64_t next_pmem;
2249
0
    } QEMU_PACKED *part_info = (void *)payload_out;
2250
0
    QEMU_BUILD_BUG_ON(sizeof(*part_info) != 0x20);
2251
0
    CXLType3Dev *ct3d = container_of(cxl_dstate, CXLType3Dev, cxl_dstate);
2252
2253
0
    if ((!QEMU_IS_ALIGNED(cxl_dstate->vmem_size, CXL_CAPACITY_MULTIPLIER)) ||
2254
0
        (!QEMU_IS_ALIGNED(cxl_dstate->pmem_size, CXL_CAPACITY_MULTIPLIER)) ||
2255
0
        (!QEMU_IS_ALIGNED(ct3d->dc.total_capacity, CXL_CAPACITY_MULTIPLIER))) {
2256
0
        return CXL_MBOX_INTERNAL_ERROR;
2257
0
    }
2258
2259
0
    stq_le_p(&part_info->active_vmem,
2260
0
             cxl_dstate->vmem_size / CXL_CAPACITY_MULTIPLIER);
2261
    /*
2262
     * When both next_vmem and next_pmem are 0, there is no pending change to
2263
     * partitioning.
2264
     */
2265
0
    stq_le_p(&part_info->next_vmem, 0);
2266
0
    stq_le_p(&part_info->active_pmem,
2267
0
             cxl_dstate->pmem_size / CXL_CAPACITY_MULTIPLIER);
2268
0
    stq_le_p(&part_info->next_pmem, 0);
2269
2270
0
    *len_out = sizeof(*part_info);
2271
0
    return CXL_MBOX_SUCCESS;
2272
0
}
2273
2274
/* CXL r3.1 Section 8.2.9.9.2.3: Get LSA (Opcode 4102h) */
2275
static CXLRetCode cmd_ccls_get_lsa(const struct cxl_cmd *cmd,
2276
                                   uint8_t *payload_in,
2277
                                   size_t len_in,
2278
                                   uint8_t *payload_out,
2279
                                   size_t *len_out,
2280
                                   CXLCCI *cci)
2281
0
{
2282
0
    struct {
2283
0
        uint32_t offset;
2284
0
        uint32_t length;
2285
0
    } QEMU_PACKED *get_lsa;
2286
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2287
0
    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
2288
0
    uint64_t offset, length;
2289
2290
0
    get_lsa = (void *)payload_in;
2291
0
    offset = get_lsa->offset;
2292
0
    length = get_lsa->length;
2293
2294
0
    if (offset + length > cvc->get_lsa_size(ct3d)) {
2295
0
        *len_out = 0;
2296
0
        return CXL_MBOX_INVALID_INPUT;
2297
0
    }
2298
2299
0
    *len_out = cvc->get_lsa(ct3d, payload_out, length, offset);
2300
0
    return CXL_MBOX_SUCCESS;
2301
0
}
2302
2303
/* CXL r3.1 Section 8.2.9.9.2.4: Set LSA (Opcode 4103h) */
2304
static CXLRetCode cmd_ccls_set_lsa(const struct cxl_cmd *cmd,
2305
                                   uint8_t *payload_in,
2306
                                   size_t len_in,
2307
                                   uint8_t *payload_out,
2308
                                   size_t *len_out,
2309
                                   CXLCCI *cci)
2310
0
{
2311
0
    struct set_lsa_pl {
2312
0
        uint32_t offset;
2313
0
        uint32_t rsvd;
2314
0
        uint8_t data[];
2315
0
    } QEMU_PACKED;
2316
0
    struct set_lsa_pl *set_lsa_payload = (void *)payload_in;
2317
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2318
0
    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
2319
0
    const size_t hdr_len = offsetof(struct set_lsa_pl, data);
2320
2321
0
    *len_out = 0;
2322
0
    if (len_in < hdr_len) {
2323
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
2324
0
    }
2325
2326
0
    if (set_lsa_payload->offset + len_in > cvc->get_lsa_size(ct3d) + hdr_len) {
2327
0
        return CXL_MBOX_INVALID_INPUT;
2328
0
    }
2329
0
    len_in -= hdr_len;
2330
2331
0
    cvc->set_lsa(ct3d, set_lsa_payload->data, len_in, set_lsa_payload->offset);
2332
0
    return CXL_MBOX_SUCCESS;
2333
0
}
2334
2335
/* CXL r3.2 Section 8.2.10.9.3.2 Get Alert Configuration (Opcode 4201h) */
2336
static CXLRetCode cmd_get_alert_config(const struct cxl_cmd *cmd,
2337
                                       uint8_t *payload_in,
2338
                                       size_t len_in,
2339
                                       uint8_t *payload_out,
2340
                                       size_t *len_out,
2341
                                       CXLCCI *cci)
2342
0
{
2343
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2344
0
    CXLAlertConfig *out = (CXLAlertConfig *)payload_out;
2345
2346
0
    memcpy(out, &ct3d->alert_config, sizeof(ct3d->alert_config));
2347
0
    *len_out = sizeof(ct3d->alert_config);
2348
2349
0
    return CXL_MBOX_SUCCESS;
2350
0
}
2351
2352
/* CXL r3.2 Section 8.2.10.9.3.3 Set Alert Configuration (Opcode 4202h) */
2353
static CXLRetCode cmd_set_alert_config(const struct cxl_cmd *cmd,
2354
                                       uint8_t *payload_in,
2355
                                       size_t len_in,
2356
                                       uint8_t *payload_out,
2357
                                       size_t *len_out,
2358
                                       CXLCCI *cci)
2359
0
{
2360
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2361
0
    CXLAlertConfig *alert_config = &ct3d->alert_config;
2362
0
    struct {
2363
0
        uint8_t valid_alert_actions;
2364
0
        uint8_t enable_alert_actions;
2365
0
        uint8_t life_used_warn_thresh;
2366
0
        uint8_t rsvd;
2367
0
        uint16_t over_temp_warn_thresh;
2368
0
        uint16_t under_temp_warn_thresh;
2369
0
        uint16_t cor_vmem_err_warn_thresh;
2370
0
        uint16_t cor_pmem_err_warn_thresh;
2371
0
    } QEMU_PACKED *in = (void *)payload_in;
2372
2373
0
    if (in->valid_alert_actions & CXL_ALERTS_LIFE_USED_WARN_THRESH) {
2374
        /*
2375
         * CXL r3.2 Table 8-149 The life used warning threshold shall be
2376
         * less than the life used critical alert value.
2377
         */
2378
0
        if (in->life_used_warn_thresh >=
2379
0
            alert_config->life_used_crit_alert_thresh) {
2380
0
            return CXL_MBOX_INVALID_INPUT;
2381
0
        }
2382
0
        alert_config->life_used_warn_thresh = in->life_used_warn_thresh;
2383
0
        alert_config->enable_alerts |= CXL_ALERTS_LIFE_USED_WARN_THRESH;
2384
0
    }
2385
2386
0
    if (in->valid_alert_actions & CXL_ALERTS_OVER_TEMP_WARN_THRESH) {
2387
        /*
2388
         * CXL r3.2 Table 8-149 The Device Over-Temperature Warning Threshold
2389
         * shall be less than the the Device Over-Temperature Critical
2390
         * Alert Threshold.
2391
         */
2392
0
        if (in->over_temp_warn_thresh >=
2393
0
            alert_config->over_temp_crit_alert_thresh) {
2394
0
            return CXL_MBOX_INVALID_INPUT;
2395
0
        }
2396
0
        alert_config->over_temp_warn_thresh = in->over_temp_warn_thresh;
2397
0
        alert_config->enable_alerts |= CXL_ALERTS_OVER_TEMP_WARN_THRESH;
2398
0
    }
2399
2400
0
    if (in->valid_alert_actions & CXL_ALERTS_UNDER_TEMP_WARN_THRESH) {
2401
        /*
2402
         * CXL r3.2 Table 8-149 The Device Under-Temperature Warning Threshold
2403
         * shall be higher than the the Device Under-Temperature Critical
2404
         * Alert Threshold.
2405
         */
2406
0
        if (in->under_temp_warn_thresh <=
2407
0
            alert_config->under_temp_crit_alert_thresh) {
2408
0
            return CXL_MBOX_INVALID_INPUT;
2409
0
        }
2410
0
        alert_config->under_temp_warn_thresh = in->under_temp_warn_thresh;
2411
0
        alert_config->enable_alerts |= CXL_ALERTS_UNDER_TEMP_WARN_THRESH;
2412
0
    }
2413
2414
0
    if (in->valid_alert_actions & CXL_ALERTS_COR_VMEM_ERR_WARN_THRESH) {
2415
0
        alert_config->cor_vmem_err_warn_thresh = in->cor_vmem_err_warn_thresh;
2416
0
        alert_config->enable_alerts |= CXL_ALERTS_COR_VMEM_ERR_WARN_THRESH;
2417
0
    }
2418
2419
0
    if (in->valid_alert_actions & CXL_ALERTS_COR_PMEM_ERR_WARN_THRESH) {
2420
0
        alert_config->cor_pmem_err_warn_thresh = in->cor_pmem_err_warn_thresh;
2421
0
        alert_config->enable_alerts |= CXL_ALERTS_COR_PMEM_ERR_WARN_THRESH;
2422
0
    }
2423
0
    return CXL_MBOX_SUCCESS;
2424
0
}
2425
2426
/* Perform the actual device zeroing */
2427
static void __do_sanitization(CXLType3Dev *ct3d)
2428
0
{
2429
0
    MemoryRegion *mr;
2430
2431
0
    if (ct3d->hostvmem) {
2432
0
        mr = host_memory_backend_get_memory(ct3d->hostvmem);
2433
0
        if (mr) {
2434
0
            void *hostmem = memory_region_get_ram_ptr(mr);
2435
0
            memset(hostmem, 0, memory_region_size(mr));
2436
0
        }
2437
0
    }
2438
2439
0
    if (ct3d->hostpmem) {
2440
0
        mr = host_memory_backend_get_memory(ct3d->hostpmem);
2441
0
        if (mr) {
2442
0
            void *hostmem = memory_region_get_ram_ptr(mr);
2443
0
            memset(hostmem, 0, memory_region_size(mr));
2444
0
        }
2445
0
    }
2446
0
    if (ct3d->lsa) {
2447
0
        mr = host_memory_backend_get_memory(ct3d->lsa);
2448
0
        if (mr) {
2449
0
            void *lsa = memory_region_get_ram_ptr(mr);
2450
0
            memset(lsa, 0, memory_region_size(mr));
2451
0
        }
2452
0
    }
2453
0
    cxl_discard_all_event_records(&ct3d->cxl_dstate);
2454
0
}
2455
2456
static int get_sanitize_duration(uint64_t total_mem)
2457
0
{
2458
0
    int secs = 0;
2459
2460
0
    if (total_mem <= 512) {
2461
0
        secs = 4;
2462
0
    } else if (total_mem <= 1024) {
2463
0
        secs = 8;
2464
0
    } else if (total_mem <= 2 * 1024) {
2465
0
        secs = 15;
2466
0
    } else if (total_mem <= 4 * 1024) {
2467
0
        secs = 30;
2468
0
    } else if (total_mem <= 8 * 1024) {
2469
0
        secs = 60;
2470
0
    } else if (total_mem <= 16 * 1024) {
2471
0
        secs = 2 * 60;
2472
0
    } else if (total_mem <= 32 * 1024) {
2473
0
        secs = 4 * 60;
2474
0
    } else if (total_mem <= 64 * 1024) {
2475
0
        secs = 8 * 60;
2476
0
    } else if (total_mem <= 128 * 1024) {
2477
0
        secs = 15 * 60;
2478
0
    } else if (total_mem <= 256 * 1024) {
2479
0
        secs = 30 * 60;
2480
0
    } else if (total_mem <= 512 * 1024) {
2481
0
        secs = 60 * 60;
2482
0
    } else if (total_mem <= 1024 * 1024) {
2483
0
        secs = 120 * 60;
2484
0
    } else {
2485
0
        secs = 240 * 60; /* max 4 hrs */
2486
0
    }
2487
2488
0
    return secs;
2489
0
}
2490
2491
/*
2492
 * CXL r3.1 Section 8.2.9.9.5.1: Sanitize (Opcode 4400h)
2493
 *
2494
 * Once the Sanitize command has started successfully, the device shall be
2495
 * placed in the media disabled state. If the command fails or is interrupted
2496
 * by a reset or power failure, it shall remain in the media disabled state
2497
 * until a successful Sanitize command has been completed. During this state:
2498
 *
2499
 * 1. Memory writes to the device will have no effect, and all memory reads
2500
 * will return random values (no user data returned, even for locations that
2501
 * the failed Sanitize operation didn’t sanitize yet).
2502
 *
2503
 * 2. Mailbox commands shall still be processed in the disabled state, except
2504
 * that commands that access Sanitized areas shall fail with the Media Disabled
2505
 * error code.
2506
 */
2507
static CXLRetCode cmd_sanitize_overwrite(const struct cxl_cmd *cmd,
2508
                                         uint8_t *payload_in,
2509
                                         size_t len_in,
2510
                                         uint8_t *payload_out,
2511
                                         size_t *len_out,
2512
                                         CXLCCI *cci)
2513
0
{
2514
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2515
0
    uint64_t total_mem; /* in Mb */
2516
0
    int secs;
2517
2518
0
    total_mem = (ct3d->cxl_dstate.vmem_size + ct3d->cxl_dstate.pmem_size) >> 20;
2519
0
    secs = get_sanitize_duration(total_mem);
2520
2521
    /* EBUSY other bg cmds as of now */
2522
0
    cci->bg.runtime = secs * 1000UL;
2523
0
    *len_out = 0;
2524
2525
0
    cxl_dev_disable_media(&ct3d->cxl_dstate);
2526
2527
    /* sanitize when done */
2528
0
    return CXL_MBOX_BG_STARTED;
2529
0
}
2530
2531
struct dpa_range_list_entry {
2532
    uint64_t starting_dpa;
2533
    uint64_t length;
2534
} QEMU_PACKED;
2535
2536
struct CXLSanitizeInfo {
2537
    uint32_t dpa_range_count;
2538
    uint8_t fill_value;
2539
    struct dpa_range_list_entry dpa_range_list[];
2540
} QEMU_PACKED;
2541
2542
static uint64_t get_vmr_size(CXLType3Dev *ct3d, MemoryRegion **vmr)
2543
0
{
2544
0
    MemoryRegion *mr;
2545
0
    if (ct3d->hostvmem) {
2546
0
        mr = host_memory_backend_get_memory(ct3d->hostvmem);
2547
0
        if (vmr) {
2548
0
            *vmr = mr;
2549
0
        }
2550
0
        return memory_region_size(mr);
2551
0
    }
2552
0
    return 0;
2553
0
}
2554
2555
static uint64_t get_pmr_size(CXLType3Dev *ct3d, MemoryRegion **pmr)
2556
0
{
2557
0
    MemoryRegion *mr;
2558
0
    if (ct3d->hostpmem) {
2559
0
        mr = host_memory_backend_get_memory(ct3d->hostpmem);
2560
0
        if (pmr) {
2561
0
            *pmr = mr;
2562
0
        }
2563
0
        return memory_region_size(mr);
2564
0
    }
2565
0
    return 0;
2566
0
}
2567
2568
static uint64_t get_dc_size(CXLType3Dev *ct3d, MemoryRegion **dc_mr)
2569
0
{
2570
0
    MemoryRegion *mr;
2571
0
    if (ct3d->dc.host_dc) {
2572
0
        mr = host_memory_backend_get_memory(ct3d->dc.host_dc);
2573
0
        if (dc_mr) {
2574
0
            *dc_mr = mr;
2575
0
        }
2576
0
        return memory_region_size(mr);
2577
0
    }
2578
0
    return 0;
2579
0
}
2580
2581
static int validate_dpa_addr(CXLType3Dev *ct3d, uint64_t dpa_addr,
2582
                             size_t length)
2583
0
{
2584
0
    uint64_t vmr_size, pmr_size, dc_size, dpa_end;
2585
2586
0
    if ((dpa_addr % CXL_CACHE_LINE_SIZE) ||
2587
0
        (length % CXL_CACHE_LINE_SIZE)  ||
2588
0
        (length <= 0)) {
2589
0
        return -EINVAL;
2590
0
    }
2591
2592
0
    vmr_size = get_vmr_size(ct3d, NULL);
2593
0
    pmr_size = get_pmr_size(ct3d, NULL);
2594
0
    dc_size = get_dc_size(ct3d, NULL);
2595
2596
    /* sanitize 64 bit values coming from guest */
2597
0
    if (uadd64_overflow(dpa_addr, length, &dpa_end)) {
2598
0
        return -EINVAL;
2599
0
    }
2600
2601
0
    if (dpa_end > vmr_size + pmr_size + dc_size) {
2602
0
        return -EINVAL;
2603
0
    }
2604
2605
0
    if (dpa_addr > vmr_size + pmr_size) {
2606
0
        if (!ct3_test_region_block_backed(ct3d, dpa_addr, length)) {
2607
0
            return -ENODEV;
2608
0
        }
2609
0
    }
2610
2611
0
    return 0;
2612
0
}
2613
2614
static int sanitize_range(CXLType3Dev *ct3d, uint64_t dpa_addr, size_t length,
2615
                          uint8_t fill_value)
2616
0
{
2617
2618
0
    uint64_t vmr_size, pmr_size;
2619
0
    AddressSpace *as = NULL;
2620
0
    MemTxAttrs mem_attrs = {};
2621
2622
0
    vmr_size = get_vmr_size(ct3d, NULL);
2623
0
    pmr_size = get_pmr_size(ct3d, NULL);
2624
2625
0
    if (dpa_addr < vmr_size) {
2626
0
        as = &ct3d->hostvmem_as;
2627
0
    } else if (dpa_addr < vmr_size + pmr_size) {
2628
0
        as = &ct3d->hostpmem_as;
2629
0
    } else {
2630
0
        if (!ct3_test_region_block_backed(ct3d, dpa_addr, length)) {
2631
0
            return -ENODEV;
2632
0
        }
2633
0
        as = &ct3d->dc.host_dc_as;
2634
0
    }
2635
2636
0
    return address_space_set(as, dpa_addr, fill_value, length, mem_attrs);
2637
0
}
2638
2639
static void cxl_discard_media_op_sanitize(CXLType3Dev *ct3d)
2640
0
{
2641
0
    g_free(ct3d->media_op_sanitize);
2642
0
    ct3d->media_op_sanitize = NULL;
2643
0
}
2644
2645
/* Perform the actual device zeroing */
2646
static void __do_sanitize(CXLType3Dev *ct3d)
2647
0
{
2648
0
    struct CXLSanitizeInfo  *san_info = ct3d->media_op_sanitize;
2649
0
    int dpa_range_count = san_info->dpa_range_count;
2650
0
    int rc = 0;
2651
0
    int i;
2652
2653
0
    for (i = 0; i < dpa_range_count; i++) {
2654
0
        rc = sanitize_range(ct3d, san_info->dpa_range_list[i].starting_dpa,
2655
0
                            san_info->dpa_range_list[i].length,
2656
0
                            san_info->fill_value);
2657
0
        if (rc) {
2658
0
            goto exit;
2659
0
        }
2660
0
    }
2661
0
exit:
2662
0
    cxl_discard_media_op_sanitize(ct3d);
2663
0
    return;
2664
0
}
2665
2666
enum {
2667
    MEDIA_OP_CLASS_GENERAL  = 0x0,
2668
0
        #define MEDIA_OP_GEN_SUBC_DISCOVERY 0x0
2669
    MEDIA_OP_CLASS_SANITIZE = 0x1,
2670
0
        #define MEDIA_OP_SAN_SUBC_SANITIZE 0x0
2671
0
        #define MEDIA_OP_SAN_SUBC_ZERO 0x1
2672
};
2673
2674
struct media_op_supported_list_entry {
2675
    uint8_t media_op_class;
2676
    uint8_t media_op_subclass;
2677
};
2678
2679
struct media_op_discovery_out_pl {
2680
    uint64_t dpa_range_granularity;
2681
    uint16_t total_supported_operations;
2682
    uint16_t num_of_supported_operations;
2683
    struct media_op_supported_list_entry entry[];
2684
} QEMU_PACKED;
2685
2686
static const struct media_op_supported_list_entry media_op_matrix[] = {
2687
    { MEDIA_OP_CLASS_GENERAL, MEDIA_OP_GEN_SUBC_DISCOVERY },
2688
    { MEDIA_OP_CLASS_SANITIZE, MEDIA_OP_SAN_SUBC_SANITIZE },
2689
    { MEDIA_OP_CLASS_SANITIZE, MEDIA_OP_SAN_SUBC_ZERO },
2690
};
2691
2692
static CXLRetCode media_operations_discovery(uint8_t *payload_in,
2693
                                             size_t len_in,
2694
                                             uint8_t *payload_out,
2695
                                             size_t *len_out)
2696
0
{
2697
0
    struct {
2698
0
        uint8_t media_operation_class;
2699
0
        uint8_t media_operation_subclass;
2700
0
        uint8_t rsvd[2];
2701
0
        uint32_t dpa_range_count;
2702
0
        struct {
2703
0
            uint16_t start_index;
2704
0
            uint16_t num_ops;
2705
0
        } discovery_osa;
2706
0
    } QEMU_PACKED *media_op_in_disc_pl = (void *)payload_in;
2707
0
    struct media_op_discovery_out_pl *media_out_pl =
2708
0
        (struct media_op_discovery_out_pl *)payload_out;
2709
0
    int total = ARRAY_SIZE(media_op_matrix) - 1; /* exclude Discovery */
2710
0
    int num_ops, start_index, i;
2711
0
    int count = 0;
2712
2713
0
    if (len_in < sizeof(*media_op_in_disc_pl)) {
2714
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
2715
0
    }
2716
2717
0
    num_ops = media_op_in_disc_pl->discovery_osa.num_ops;
2718
0
    start_index = media_op_in_disc_pl->discovery_osa.start_index;
2719
2720
    /*
2721
     * As per spec CXL r3.2 8.2.10.9.5.3 dpa_range_count should be zero and
2722
     * start index should not exceed the total number of entries for discovery
2723
     * sub class command.
2724
     */
2725
0
    if (media_op_in_disc_pl->dpa_range_count ||
2726
0
        start_index >= total) {
2727
0
        return CXL_MBOX_INVALID_INPUT;
2728
0
    }
2729
2730
0
    media_out_pl->dpa_range_granularity = CXL_CACHE_LINE_SIZE;
2731
0
    media_out_pl->total_supported_operations = total;
2732
2733
0
    num_ops = MIN(num_ops, total - start_index);
2734
0
    for (i = 0; i < num_ops; i++) {
2735
0
        int idx = start_index + i + 1; /* skip Discovery (first entry) */
2736
2737
0
        media_out_pl->entry[count].media_op_class =
2738
0
                media_op_matrix[idx].media_op_class;
2739
0
        media_out_pl->entry[count].media_op_subclass =
2740
0
                media_op_matrix[idx].media_op_subclass;
2741
0
        count++;
2742
0
    }
2743
2744
0
    media_out_pl->num_of_supported_operations = count;
2745
0
    *len_out = sizeof(*media_out_pl) + count * sizeof(*media_out_pl->entry);
2746
0
    return CXL_MBOX_SUCCESS;
2747
0
}
2748
2749
static CXLRetCode media_operations_sanitize(CXLType3Dev *ct3d,
2750
                                            uint8_t *payload_in,
2751
                                            size_t len_in,
2752
                                            uint8_t *payload_out,
2753
                                            size_t *len_out,
2754
                                            uint8_t fill_value,
2755
                                            CXLCCI *cci)
2756
0
{
2757
0
    struct media_operations_sanitize {
2758
0
        uint8_t media_operation_class;
2759
0
        uint8_t media_operation_subclass;
2760
0
        uint8_t rsvd[2];
2761
0
        uint32_t dpa_range_count;
2762
0
        struct dpa_range_list_entry dpa_range_list[];
2763
0
    } QEMU_PACKED *media_op_in_sanitize_pl = (void *)payload_in;
2764
0
    uint32_t dpa_range_count = media_op_in_sanitize_pl->dpa_range_count;
2765
0
    uint64_t total_mem = 0;
2766
0
    size_t dpa_range_list_size;
2767
0
    int secs = 0, i;
2768
2769
0
    if (dpa_range_count == 0) {
2770
0
        return CXL_MBOX_SUCCESS;
2771
0
    }
2772
2773
0
    dpa_range_list_size = dpa_range_count * sizeof(struct dpa_range_list_entry);
2774
0
    if (len_in < (sizeof(*media_op_in_sanitize_pl) + dpa_range_list_size)) {
2775
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
2776
0
    }
2777
2778
0
    for (i = 0; i < dpa_range_count; i++) {
2779
0
        uint64_t start_dpa =
2780
0
            media_op_in_sanitize_pl->dpa_range_list[i].starting_dpa;
2781
0
        uint64_t length = media_op_in_sanitize_pl->dpa_range_list[i].length;
2782
2783
0
        if (validate_dpa_addr(ct3d, start_dpa, length)) {
2784
0
            return CXL_MBOX_INVALID_INPUT;
2785
0
        }
2786
0
        total_mem += length;
2787
0
    }
2788
0
    ct3d->media_op_sanitize = g_malloc0(sizeof(struct CXLSanitizeInfo) +
2789
0
                                        dpa_range_list_size);
2790
2791
0
    ct3d->media_op_sanitize->dpa_range_count = dpa_range_count;
2792
0
    ct3d->media_op_sanitize->fill_value = fill_value;
2793
0
    memcpy(ct3d->media_op_sanitize->dpa_range_list,
2794
0
           media_op_in_sanitize_pl->dpa_range_list,
2795
0
           dpa_range_list_size);
2796
0
    secs = get_sanitize_duration(total_mem >> 20);
2797
2798
    /* EBUSY other bg cmds as of now */
2799
0
    cci->bg.runtime = secs * 1000UL;
2800
0
    *len_out = 0;
2801
    /*
2802
     * media op sanitize is targeted so no need to disable media or
2803
     * clear event logs
2804
     */
2805
0
    return CXL_MBOX_BG_STARTED;
2806
0
}
2807
2808
static CXLRetCode cmd_media_operations(const struct cxl_cmd *cmd,
2809
                                       uint8_t *payload_in,
2810
                                       size_t len_in,
2811
                                       uint8_t *payload_out,
2812
                                       size_t *len_out,
2813
                                       CXLCCI *cci)
2814
0
{
2815
0
    struct {
2816
0
        uint8_t media_operation_class;
2817
0
        uint8_t media_operation_subclass;
2818
0
        uint8_t rsvd[2];
2819
0
        uint32_t dpa_range_count;
2820
0
    } QEMU_PACKED *media_op_in_common_pl = (void *)payload_in;
2821
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2822
0
    uint8_t media_op_cl = 0;
2823
0
    uint8_t media_op_subclass = 0;
2824
2825
0
    if (len_in < sizeof(*media_op_in_common_pl)) {
2826
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
2827
0
    }
2828
2829
0
    media_op_cl = media_op_in_common_pl->media_operation_class;
2830
0
    media_op_subclass = media_op_in_common_pl->media_operation_subclass;
2831
2832
0
    switch (media_op_cl) {
2833
0
    case MEDIA_OP_CLASS_GENERAL:
2834
0
        if (media_op_subclass != MEDIA_OP_GEN_SUBC_DISCOVERY) {
2835
0
            return CXL_MBOX_UNSUPPORTED;
2836
0
        }
2837
2838
0
        return media_operations_discovery(payload_in, len_in, payload_out,
2839
0
                                             len_out);
2840
0
    case MEDIA_OP_CLASS_SANITIZE:
2841
0
        switch (media_op_subclass) {
2842
0
        case MEDIA_OP_SAN_SUBC_SANITIZE:
2843
0
            return media_operations_sanitize(ct3d, payload_in, len_in,
2844
0
                                             payload_out, len_out, 0xF,
2845
0
                                             cci);
2846
0
        case MEDIA_OP_SAN_SUBC_ZERO:
2847
0
            return media_operations_sanitize(ct3d, payload_in, len_in,
2848
0
                                             payload_out, len_out, 0,
2849
0
                                             cci);
2850
0
        default:
2851
0
            return CXL_MBOX_UNSUPPORTED;
2852
0
        }
2853
0
    default:
2854
0
        return CXL_MBOX_UNSUPPORTED;
2855
0
    }
2856
0
}
2857
2858
static CXLRetCode cmd_get_security_state(const struct cxl_cmd *cmd,
2859
                                         uint8_t *payload_in,
2860
                                         size_t len_in,
2861
                                         uint8_t *payload_out,
2862
                                         size_t *len_out,
2863
                                         CXLCCI *cci)
2864
0
{
2865
0
    uint32_t *state = (uint32_t *)payload_out;
2866
2867
0
    *state = 0;
2868
0
    *len_out = 4;
2869
0
    return CXL_MBOX_SUCCESS;
2870
0
}
2871
2872
/*
2873
 * CXL r3.1 Section 8.2.9.9.4.1: Get Poison List (Opcode 4300h)
2874
 *
2875
 * This is very inefficient, but good enough for now!
2876
 * Also the payload will always fit, so no need to handle the MORE flag and
2877
 * make this stateful. We may want to allow longer poison lists to aid
2878
 * testing that kernel functionality.
2879
 */
2880
static CXLRetCode cmd_media_get_poison_list(const struct cxl_cmd *cmd,
2881
                                            uint8_t *payload_in,
2882
                                            size_t len_in,
2883
                                            uint8_t *payload_out,
2884
                                            size_t *len_out,
2885
                                            CXLCCI *cci)
2886
0
{
2887
0
    struct get_poison_list_pl {
2888
0
        uint64_t pa;
2889
0
        uint64_t length;
2890
0
    } QEMU_PACKED;
2891
2892
0
    struct get_poison_list_out_pl {
2893
0
        uint8_t flags;
2894
0
        uint8_t rsvd1;
2895
0
        uint64_t overflow_timestamp;
2896
0
        uint16_t count;
2897
0
        uint8_t rsvd2[0x14];
2898
0
        struct {
2899
0
            uint64_t addr;
2900
0
            uint32_t length;
2901
0
            uint32_t resv;
2902
0
        } QEMU_PACKED records[];
2903
0
    } QEMU_PACKED;
2904
2905
0
    struct get_poison_list_pl *in = (void *)payload_in;
2906
0
    struct get_poison_list_out_pl *out = (void *)payload_out;
2907
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2908
0
    uint16_t record_count = 0, i = 0;
2909
0
    uint64_t query_start, query_length;
2910
0
    CXLPoisonList *poison_list = &ct3d->poison_list;
2911
0
    CXLPoison *ent;
2912
0
    uint16_t out_pl_len;
2913
2914
0
    query_start = ldq_le_p(&in->pa);
2915
    /* 64 byte alignment required */
2916
0
    if (query_start & 0x3f) {
2917
0
        return CXL_MBOX_INVALID_INPUT;
2918
0
    }
2919
0
    query_length = ldq_le_p(&in->length) * CXL_CACHE_LINE_SIZE;
2920
2921
0
    QLIST_FOREACH(ent, poison_list, node) {
2922
        /* Check for no overlap */
2923
0
        if (!ranges_overlap(ent->start, ent->length,
2924
0
                            query_start, query_length)) {
2925
0
            continue;
2926
0
        }
2927
0
        record_count++;
2928
0
    }
2929
0
    out_pl_len = sizeof(*out) + record_count * sizeof(out->records[0]);
2930
0
    assert(out_pl_len <= CXL_MAILBOX_MAX_PAYLOAD_SIZE);
2931
2932
0
    QLIST_FOREACH(ent, poison_list, node) {
2933
0
        uint64_t start, stop;
2934
2935
        /* Check for no overlap */
2936
0
        if (!ranges_overlap(ent->start, ent->length,
2937
0
                            query_start, query_length)) {
2938
0
            continue;
2939
0
        }
2940
2941
        /* Deal with overlap */
2942
0
        start = MAX(ROUND_DOWN(ent->start, 64ull), query_start);
2943
0
        stop = MIN(ROUND_DOWN(ent->start, 64ull) + ent->length,
2944
0
                   query_start + query_length);
2945
0
        stq_le_p(&out->records[i].addr, start | (ent->type & 0x7));
2946
0
        stl_le_p(&out->records[i].length, (stop - start) / CXL_CACHE_LINE_SIZE);
2947
0
        i++;
2948
0
    }
2949
0
    if (ct3d->poison_list_overflowed) {
2950
0
        out->flags = (1 << 1);
2951
0
        stq_le_p(&out->overflow_timestamp, ct3d->poison_list_overflow_ts);
2952
0
    }
2953
0
    if (scan_media_running(cci)) {
2954
0
        out->flags |= (1 << 2);
2955
0
    }
2956
2957
0
    stw_le_p(&out->count, record_count);
2958
0
    *len_out = out_pl_len;
2959
0
    return CXL_MBOX_SUCCESS;
2960
0
}
2961
2962
/* CXL r3.1 Section 8.2.9.9.4.2: Inject Poison (Opcode 4301h) */
2963
static CXLRetCode cmd_media_inject_poison(const struct cxl_cmd *cmd,
2964
                                          uint8_t *payload_in,
2965
                                          size_t len_in,
2966
                                          uint8_t *payload_out,
2967
                                          size_t *len_out,
2968
                                          CXLCCI *cci)
2969
0
{
2970
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
2971
0
    CXLPoisonList *poison_list = &ct3d->poison_list;
2972
0
    CXLPoison *ent;
2973
0
    struct inject_poison_pl {
2974
0
        uint64_t dpa;
2975
0
    };
2976
0
    struct inject_poison_pl *in = (void *)payload_in;
2977
0
    uint64_t dpa = ldq_le_p(&in->dpa);
2978
0
    CXLPoison *p;
2979
2980
0
    QLIST_FOREACH(ent, poison_list, node) {
2981
0
        if (dpa >= ent->start &&
2982
0
            dpa + CXL_CACHE_LINE_SIZE <= ent->start + ent->length) {
2983
0
            return CXL_MBOX_SUCCESS;
2984
0
        }
2985
0
    }
2986
    /*
2987
     * Freeze the list if there is an on-going scan media operation.
2988
     */
2989
0
    if (scan_media_running(cci)) {
2990
        /*
2991
         * XXX: Spec is ambiguous - is this case considered
2992
         * a successful return despite not adding to the list?
2993
         */
2994
0
        goto success;
2995
0
    }
2996
2997
0
    if (ct3d->poison_list_cnt == CXL_POISON_LIST_LIMIT) {
2998
0
        return CXL_MBOX_INJECT_POISON_LIMIT;
2999
0
    }
3000
0
    p = g_new0(CXLPoison, 1);
3001
3002
0
    p->length = CXL_CACHE_LINE_SIZE;
3003
0
    p->start = dpa;
3004
0
    p->type = CXL_POISON_TYPE_INJECTED;
3005
3006
    /*
3007
     * Possible todo: Merge with existing entry if next to it and if same type
3008
     */
3009
0
    QLIST_INSERT_HEAD(poison_list, p, node);
3010
0
    ct3d->poison_list_cnt++;
3011
0
success:
3012
0
    *len_out = 0;
3013
3014
0
    return CXL_MBOX_SUCCESS;
3015
0
}
3016
3017
/* CXL r3.1 Section 8.2.9.9.4.3: Clear Poison (Opcode 4302h */
3018
static CXLRetCode cmd_media_clear_poison(const struct cxl_cmd *cmd,
3019
                                         uint8_t *payload_in,
3020
                                         size_t len_in,
3021
                                         uint8_t *payload_out,
3022
                                         size_t *len_out,
3023
                                         CXLCCI *cci)
3024
0
{
3025
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3026
0
    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
3027
0
    CXLPoisonList *poison_list = &ct3d->poison_list;
3028
0
    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
3029
0
    struct clear_poison_pl {
3030
0
        uint64_t dpa;
3031
0
        uint8_t data[64];
3032
0
    };
3033
0
    CXLPoison *ent;
3034
0
    uint64_t dpa;
3035
3036
0
    struct clear_poison_pl *in = (void *)payload_in;
3037
3038
0
    dpa = ldq_le_p(&in->dpa);
3039
0
    if (dpa + CXL_CACHE_LINE_SIZE > cxl_dstate->static_mem_size +
3040
0
        ct3d->dc.total_capacity) {
3041
0
        return CXL_MBOX_INVALID_PA;
3042
0
    }
3043
3044
    /* Clearing a region with no poison is not an error so always do so */
3045
0
    if (cvc->set_cacheline) {
3046
0
        if (!cvc->set_cacheline(ct3d, dpa, in->data)) {
3047
0
            return CXL_MBOX_INTERNAL_ERROR;
3048
0
        }
3049
0
    }
3050
3051
    /*
3052
     * Freeze the list if there is an on-going scan media operation.
3053
     */
3054
0
    if (scan_media_running(cci)) {
3055
        /*
3056
         * XXX: Spec is ambiguous - is this case considered
3057
         * a successful return despite not removing from the list?
3058
         */
3059
0
        goto success;
3060
0
    }
3061
3062
0
    QLIST_FOREACH(ent, poison_list, node) {
3063
        /*
3064
         * Test for contained in entry. Simpler than general case
3065
         * as clearing 64 bytes and entries 64 byte aligned
3066
         */
3067
0
        if ((dpa >= ent->start) && (dpa < ent->start + ent->length)) {
3068
0
            break;
3069
0
        }
3070
0
    }
3071
0
    if (!ent) {
3072
0
        goto success;
3073
0
    }
3074
3075
0
    QLIST_REMOVE(ent, node);
3076
0
    ct3d->poison_list_cnt--;
3077
3078
0
    if (dpa > ent->start) {
3079
0
        CXLPoison *frag;
3080
        /* Cannot overflow as replacing existing entry */
3081
3082
0
        frag = g_new0(CXLPoison, 1);
3083
3084
0
        frag->start = ent->start;
3085
0
        frag->length = dpa - ent->start;
3086
0
        frag->type = ent->type;
3087
3088
0
        QLIST_INSERT_HEAD(poison_list, frag, node);
3089
0
        ct3d->poison_list_cnt++;
3090
0
    }
3091
3092
0
    if (dpa + CXL_CACHE_LINE_SIZE < ent->start + ent->length) {
3093
0
        CXLPoison *frag;
3094
3095
0
        if (ct3d->poison_list_cnt == CXL_POISON_LIST_LIMIT) {
3096
0
            cxl_set_poison_list_overflowed(ct3d);
3097
0
        } else {
3098
0
            frag = g_new0(CXLPoison, 1);
3099
3100
0
            frag->start = dpa + CXL_CACHE_LINE_SIZE;
3101
0
            frag->length = ent->start + ent->length - frag->start;
3102
0
            frag->type = ent->type;
3103
0
            QLIST_INSERT_HEAD(poison_list, frag, node);
3104
0
            ct3d->poison_list_cnt++;
3105
0
        }
3106
0
    }
3107
    /* Any fragments have been added, free original entry */
3108
0
    g_free(ent);
3109
0
success:
3110
0
    *len_out = 0;
3111
3112
0
    return CXL_MBOX_SUCCESS;
3113
0
}
3114
3115
/*
3116
 * CXL r3.1 section 8.2.9.9.4.4: Get Scan Media Capabilities
3117
 */
3118
static CXLRetCode
3119
cmd_media_get_scan_media_capabilities(const struct cxl_cmd *cmd,
3120
                                      uint8_t *payload_in,
3121
                                      size_t len_in,
3122
                                      uint8_t *payload_out,
3123
                                      size_t *len_out,
3124
                                      CXLCCI *cci)
3125
0
{
3126
0
    struct get_scan_media_capabilities_pl {
3127
0
        uint64_t pa;
3128
0
        uint64_t length;
3129
0
    } QEMU_PACKED;
3130
3131
0
    struct get_scan_media_capabilities_out_pl {
3132
0
        uint32_t estimated_runtime_ms;
3133
0
    };
3134
3135
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3136
0
    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
3137
0
    struct get_scan_media_capabilities_pl *in = (void *)payload_in;
3138
0
    struct get_scan_media_capabilities_out_pl *out = (void *)payload_out;
3139
0
    uint64_t query_start;
3140
0
    uint64_t query_length;
3141
3142
0
    query_start = ldq_le_p(&in->pa);
3143
    /* 64 byte alignment required */
3144
0
    if (query_start & 0x3f) {
3145
0
        return CXL_MBOX_INVALID_INPUT;
3146
0
    }
3147
0
    query_length = ldq_le_p(&in->length) * CXL_CACHE_LINE_SIZE;
3148
3149
0
    if (query_start + query_length > cxl_dstate->static_mem_size) {
3150
0
        return CXL_MBOX_INVALID_PA;
3151
0
    }
3152
3153
    /*
3154
     * Just use 400 nanosecond access/read latency + 100 ns for
3155
     * the cost of updating the poison list. For small enough
3156
     * chunks return at least 1 ms.
3157
     */
3158
0
    stl_le_p(&out->estimated_runtime_ms,
3159
0
             MAX(1, query_length * (0.0005L / 64)));
3160
3161
0
    *len_out = sizeof(*out);
3162
0
    return CXL_MBOX_SUCCESS;
3163
0
}
3164
3165
static void __do_scan_media(CXLType3Dev *ct3d)
3166
0
{
3167
0
    CXLPoison *ent;
3168
0
    unsigned int results_cnt = 0;
3169
3170
0
    QLIST_FOREACH(ent, &ct3d->scan_media_results, node) {
3171
0
        results_cnt++;
3172
0
    }
3173
3174
    /* only scan media may clear the overflow */
3175
0
    if (ct3d->poison_list_overflowed &&
3176
0
        ct3d->poison_list_cnt == results_cnt) {
3177
0
        cxl_clear_poison_list_overflowed(ct3d);
3178
0
    }
3179
    /* scan media has run since last conventional reset */
3180
0
    ct3d->scan_media_hasrun = true;
3181
0
}
3182
3183
/*
3184
 * CXL r3.1 section 8.2.9.9.4.5: Scan Media
3185
 */
3186
static CXLRetCode cmd_media_scan_media(const struct cxl_cmd *cmd,
3187
                                       uint8_t *payload_in,
3188
                                       size_t len_in,
3189
                                       uint8_t *payload_out,
3190
                                       size_t *len_out,
3191
                                       CXLCCI *cci)
3192
0
{
3193
0
    struct scan_media_pl {
3194
0
        uint64_t pa;
3195
0
        uint64_t length;
3196
0
        uint8_t flags;
3197
0
    } QEMU_PACKED;
3198
3199
0
    struct scan_media_pl *in = (void *)payload_in;
3200
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3201
0
    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
3202
0
    uint64_t query_start;
3203
0
    uint64_t query_length;
3204
0
    CXLPoison *ent, *next;
3205
3206
0
    query_start = ldq_le_p(&in->pa);
3207
    /* 64 byte alignment required */
3208
0
    if (query_start & 0x3f) {
3209
0
        return CXL_MBOX_INVALID_INPUT;
3210
0
    }
3211
0
    query_length = ldq_le_p(&in->length) * CXL_CACHE_LINE_SIZE;
3212
3213
0
    if (query_start + query_length > cxl_dstate->static_mem_size) {
3214
0
        return CXL_MBOX_INVALID_PA;
3215
0
    }
3216
0
    if (ct3d->dc.num_regions && query_start + query_length >=
3217
0
            cxl_dstate->static_mem_size + ct3d->dc.total_capacity) {
3218
0
        return CXL_MBOX_INVALID_PA;
3219
0
    }
3220
3221
0
    if (in->flags == 0) { /* TODO */
3222
0
        qemu_log_mask(LOG_UNIMP,
3223
0
                      "Scan Media Event Log is unsupported\n");
3224
0
    }
3225
3226
    /* any previous results are discarded upon a new Scan Media */
3227
0
    QLIST_FOREACH_SAFE(ent, &ct3d->scan_media_results, node, next) {
3228
0
        QLIST_REMOVE(ent, node);
3229
0
        g_free(ent);
3230
0
    }
3231
3232
    /* kill the poison list - it will be recreated */
3233
0
    if (ct3d->poison_list_overflowed) {
3234
0
        QLIST_FOREACH_SAFE(ent, &ct3d->poison_list, node, next) {
3235
0
            QLIST_REMOVE(ent, node);
3236
0
            g_free(ent);
3237
0
            ct3d->poison_list_cnt--;
3238
0
        }
3239
0
    }
3240
3241
    /*
3242
     * Scan the backup list and move corresponding entries
3243
     * into the results list, updating the poison list
3244
     * when possible.
3245
     */
3246
0
    QLIST_FOREACH_SAFE(ent, &ct3d->poison_list_bkp, node, next) {
3247
0
        CXLPoison *res;
3248
3249
0
        if (ent->start >= query_start + query_length ||
3250
0
            ent->start + ent->length <= query_start) {
3251
0
            continue;
3252
0
        }
3253
3254
        /*
3255
         * If a Get Poison List cmd comes in while this
3256
         * scan is being done, it will see the new complete
3257
         * list, while setting the respective flag.
3258
         */
3259
0
        if (ct3d->poison_list_cnt < CXL_POISON_LIST_LIMIT) {
3260
0
            CXLPoison *p = g_new0(CXLPoison, 1);
3261
3262
0
            p->start = ent->start;
3263
0
            p->length = ent->length;
3264
0
            p->type = ent->type;
3265
0
            QLIST_INSERT_HEAD(&ct3d->poison_list, p, node);
3266
0
            ct3d->poison_list_cnt++;
3267
0
        }
3268
3269
0
        res = g_new0(CXLPoison, 1);
3270
0
        res->start = ent->start;
3271
0
        res->length = ent->length;
3272
0
        res->type = ent->type;
3273
0
        QLIST_INSERT_HEAD(&ct3d->scan_media_results, res, node);
3274
3275
0
        QLIST_REMOVE(ent, node);
3276
0
        g_free(ent);
3277
0
    }
3278
3279
0
    cci->bg.runtime = MAX(1, query_length * (0.0005L / 64));
3280
0
    *len_out = 0;
3281
3282
0
    return CXL_MBOX_BG_STARTED;
3283
0
}
3284
3285
/*
3286
 * CXL r3.1 section 8.2.9.9.4.6: Get Scan Media Results
3287
 */
3288
static CXLRetCode cmd_media_get_scan_media_results(const struct cxl_cmd *cmd,
3289
                                                   uint8_t *payload_in,
3290
                                                   size_t len_in,
3291
                                                   uint8_t *payload_out,
3292
                                                   size_t *len_out,
3293
                                                   CXLCCI *cci)
3294
0
{
3295
0
    struct get_scan_media_results_out_pl {
3296
0
        uint64_t dpa_restart;
3297
0
        uint64_t length;
3298
0
        uint8_t flags;
3299
0
        uint8_t rsvd1;
3300
0
        uint16_t count;
3301
0
        uint8_t rsvd2[0xc];
3302
0
        struct {
3303
0
            uint64_t addr;
3304
0
            uint32_t length;
3305
0
            uint32_t resv;
3306
0
        } QEMU_PACKED records[];
3307
0
    } QEMU_PACKED;
3308
3309
0
    struct get_scan_media_results_out_pl *out = (void *)payload_out;
3310
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3311
0
    CXLPoisonList *scan_media_results = &ct3d->scan_media_results;
3312
0
    CXLPoison *ent, *next;
3313
0
    uint16_t total_count = 0, record_count = 0, i = 0;
3314
0
    uint16_t out_pl_len;
3315
3316
0
    if (!ct3d->scan_media_hasrun) {
3317
0
        return CXL_MBOX_UNSUPPORTED;
3318
0
    }
3319
3320
    /*
3321
     * Calculate limits, all entries are within the same address range of the
3322
     * last scan media call.
3323
     */
3324
0
    QLIST_FOREACH(ent, scan_media_results, node) {
3325
0
        size_t rec_size = record_count * sizeof(out->records[0]);
3326
3327
0
        if (sizeof(*out) + rec_size < CXL_MAILBOX_MAX_PAYLOAD_SIZE) {
3328
0
            record_count++;
3329
0
        }
3330
0
        total_count++;
3331
0
    }
3332
3333
0
    out_pl_len = sizeof(*out) + record_count * sizeof(out->records[0]);
3334
0
    assert(out_pl_len <= CXL_MAILBOX_MAX_PAYLOAD_SIZE);
3335
3336
0
    memset(out, 0, out_pl_len);
3337
0
    QLIST_FOREACH_SAFE(ent, scan_media_results, node, next) {
3338
0
        uint64_t start, stop;
3339
3340
0
        if (i == record_count) {
3341
0
            break;
3342
0
        }
3343
3344
0
        start = ROUND_DOWN(ent->start, 64ull);
3345
0
        stop = ROUND_DOWN(ent->start, 64ull) + ent->length;
3346
0
        stq_le_p(&out->records[i].addr, start);
3347
0
        stl_le_p(&out->records[i].length, (stop - start) / CXL_CACHE_LINE_SIZE);
3348
0
        i++;
3349
3350
        /* consume the returning entry */
3351
0
        QLIST_REMOVE(ent, node);
3352
0
        g_free(ent);
3353
0
    }
3354
3355
0
    stw_le_p(&out->count, record_count);
3356
0
    if (total_count > record_count) {
3357
0
        out->flags = (1 << 0); /* More Media Error Records */
3358
0
    }
3359
3360
0
    *len_out = out_pl_len;
3361
0
    return CXL_MBOX_SUCCESS;
3362
0
}
3363
3364
/*
3365
 * CXL r3.1 section 8.2.9.9.9.1: Get Dynamic Capacity Configuration
3366
 * (Opcode: 4800h)
3367
 */
3368
static CXLRetCode cmd_dcd_get_dyn_cap_config(const struct cxl_cmd *cmd,
3369
                                             uint8_t *payload_in,
3370
                                             size_t len_in,
3371
                                             uint8_t *payload_out,
3372
                                             size_t *len_out,
3373
                                             CXLCCI *cci)
3374
0
{
3375
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3376
0
    struct {
3377
0
        uint8_t region_cnt;
3378
0
        uint8_t start_rid;
3379
0
    } QEMU_PACKED *in = (void *)payload_in;
3380
0
    struct {
3381
0
        uint8_t num_regions;
3382
0
        uint8_t regions_returned;
3383
0
        uint8_t rsvd1[6];
3384
0
        struct {
3385
0
            uint64_t base;
3386
0
            uint64_t decode_len;
3387
0
            uint64_t region_len;
3388
0
            uint64_t block_size;
3389
0
            uint32_t dsmadhandle;
3390
0
            uint8_t flags;
3391
0
            uint8_t rsvd2[3];
3392
0
        } QEMU_PACKED records[];
3393
0
    } QEMU_PACKED *out = (void *)payload_out;
3394
0
    struct {
3395
0
        uint32_t num_extents_supported;
3396
0
        uint32_t num_extents_available;
3397
0
        uint32_t num_tags_supported;
3398
0
        uint32_t num_tags_available;
3399
0
    } QEMU_PACKED *extra_out;
3400
0
    uint16_t record_count;
3401
0
    uint16_t i;
3402
0
    uint16_t out_pl_len;
3403
0
    uint8_t start_rid;
3404
3405
0
    start_rid = in->start_rid;
3406
0
    if (start_rid >= ct3d->dc.num_regions) {
3407
0
        return CXL_MBOX_INVALID_INPUT;
3408
0
    }
3409
3410
0
    record_count = MIN(ct3d->dc.num_regions - in->start_rid, in->region_cnt);
3411
3412
0
    out_pl_len = sizeof(*out) + record_count * sizeof(out->records[0]);
3413
0
    extra_out = (void *)(payload_out + out_pl_len);
3414
0
    out_pl_len += sizeof(*extra_out);
3415
0
    assert(out_pl_len <= CXL_MAILBOX_MAX_PAYLOAD_SIZE);
3416
3417
0
    out->num_regions = ct3d->dc.num_regions;
3418
0
    out->regions_returned = record_count;
3419
0
    for (i = 0; i < record_count; i++) {
3420
0
        stq_le_p(&out->records[i].base,
3421
0
                 ct3d->dc.regions[start_rid + i].base);
3422
0
        stq_le_p(&out->records[i].decode_len,
3423
0
                 ct3d->dc.regions[start_rid + i].decode_len /
3424
0
                 CXL_CAPACITY_MULTIPLIER);
3425
0
        stq_le_p(&out->records[i].region_len,
3426
0
                 ct3d->dc.regions[start_rid + i].len);
3427
0
        stq_le_p(&out->records[i].block_size,
3428
0
                 ct3d->dc.regions[start_rid + i].block_size);
3429
0
        stl_le_p(&out->records[i].dsmadhandle,
3430
0
                 ct3d->dc.regions[start_rid + i].dsmadhandle);
3431
0
        out->records[i].flags = ct3d->dc.regions[start_rid + i].flags;
3432
0
    }
3433
    /*
3434
     * TODO: Assign values once extents and tags are introduced
3435
     * to use.
3436
     */
3437
0
    stl_le_p(&extra_out->num_extents_supported, CXL_NUM_EXTENTS_SUPPORTED);
3438
0
    stl_le_p(&extra_out->num_extents_available, CXL_NUM_EXTENTS_SUPPORTED -
3439
0
             ct3d->dc.total_extent_count);
3440
0
    stl_le_p(&extra_out->num_tags_supported, CXL_NUM_TAGS_SUPPORTED);
3441
0
    stl_le_p(&extra_out->num_tags_available, CXL_NUM_TAGS_SUPPORTED);
3442
3443
0
    *len_out = out_pl_len;
3444
0
    return CXL_MBOX_SUCCESS;
3445
0
}
3446
3447
/*
3448
 * CXL r3.1 section 8.2.9.9.9.2:
3449
 * Get Dynamic Capacity Extent List (Opcode 4801h)
3450
 */
3451
static CXLRetCode cmd_dcd_get_dyn_cap_ext_list(const struct cxl_cmd *cmd,
3452
                                               uint8_t *payload_in,
3453
                                               size_t len_in,
3454
                                               uint8_t *payload_out,
3455
                                               size_t *len_out,
3456
                                               CXLCCI *cci)
3457
0
{
3458
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3459
0
    struct {
3460
0
        uint32_t extent_cnt;
3461
0
        uint32_t start_extent_id;
3462
0
    } QEMU_PACKED *in = (void *)payload_in;
3463
0
    struct {
3464
0
        uint32_t count;
3465
0
        uint32_t total_extents;
3466
0
        uint32_t generation_num;
3467
0
        uint8_t rsvd[4];
3468
0
        CXLDCExtentRaw records[];
3469
0
    } QEMU_PACKED *out = (void *)payload_out;
3470
0
    uint32_t start_extent_id = in->start_extent_id;
3471
0
    CXLDCExtentList *extent_list = &ct3d->dc.extents;
3472
0
    uint16_t record_count = 0, i = 0, record_done = 0;
3473
0
    uint16_t out_pl_len, size;
3474
0
    CXLDCExtent *ent;
3475
3476
0
    if (start_extent_id > ct3d->dc.nr_extents_accepted) {
3477
0
        return CXL_MBOX_INVALID_INPUT;
3478
0
    }
3479
3480
0
    record_count = MIN(in->extent_cnt,
3481
0
                       ct3d->dc.total_extent_count - start_extent_id);
3482
0
    size = CXL_MAILBOX_MAX_PAYLOAD_SIZE - sizeof(*out);
3483
0
    record_count = MIN(record_count, size / sizeof(out->records[0]));
3484
0
    out_pl_len = sizeof(*out) + record_count * sizeof(out->records[0]);
3485
3486
0
    stl_le_p(&out->count, record_count);
3487
0
    stl_le_p(&out->total_extents, ct3d->dc.nr_extents_accepted);
3488
0
    stl_le_p(&out->generation_num, ct3d->dc.ext_list_gen_seq);
3489
3490
0
    if (record_count > 0) {
3491
0
        CXLDCExtentRaw *out_rec = &out->records[record_done];
3492
3493
0
        QTAILQ_FOREACH(ent, extent_list, node) {
3494
0
            if (i++ < start_extent_id) {
3495
0
                continue;
3496
0
            }
3497
0
            stq_le_p(&out_rec->start_dpa, ent->start_dpa);
3498
0
            stq_le_p(&out_rec->len, ent->len);
3499
0
            memcpy(&out_rec->tag, ent->tag, 0x10);
3500
0
            stw_le_p(&out_rec->shared_seq, ent->shared_seq);
3501
3502
0
            record_done++;
3503
0
            out_rec++;
3504
0
            if (record_done == record_count) {
3505
0
                break;
3506
0
            }
3507
0
        }
3508
0
    }
3509
3510
0
    *len_out = out_pl_len;
3511
0
    return CXL_MBOX_SUCCESS;
3512
0
}
3513
3514
/*
3515
 * Check whether any bit between addr[nr, nr+size) is set,
3516
 * return true if any bit is set, otherwise return false
3517
 */
3518
bool test_any_bits_set(const unsigned long *addr, unsigned long nr,
3519
                              unsigned long size)
3520
0
{
3521
0
    unsigned long res = find_next_bit(addr, size + nr, nr);
3522
3523
0
    return res < nr + size;
3524
0
}
3525
3526
CXLDCRegion *cxl_find_dc_region(CXLType3Dev *ct3d, uint64_t dpa, uint64_t len)
3527
0
{
3528
0
    int i;
3529
0
    CXLDCRegion *region = &ct3d->dc.regions[0];
3530
3531
0
    if (dpa < region->base ||
3532
0
        dpa >= region->base + ct3d->dc.total_capacity) {
3533
0
        return NULL;
3534
0
    }
3535
3536
    /*
3537
     * CXL r3.1 section 9.13.3: Dynamic Capacity Device (DCD)
3538
     *
3539
     * Regions are used in increasing-DPA order, with Region 0 being used for
3540
     * the lowest DPA of Dynamic Capacity and Region 7 for the highest DPA.
3541
     * So check from the last region to find where the dpa belongs. Extents that
3542
     * cross multiple regions are not allowed.
3543
     */
3544
0
    for (i = ct3d->dc.num_regions - 1; i >= 0; i--) {
3545
0
        region = &ct3d->dc.regions[i];
3546
0
        if (dpa >= region->base) {
3547
0
            if (dpa + len > region->base + region->len) {
3548
0
                return NULL;
3549
0
            }
3550
0
            return region;
3551
0
        }
3552
0
    }
3553
3554
0
    return NULL;
3555
0
}
3556
3557
void cxl_insert_extent_to_extent_list(CXLDCExtentList *list,
3558
                                             uint64_t dpa,
3559
                                             uint64_t len,
3560
                                             uint8_t *tag,
3561
                                             uint16_t shared_seq)
3562
0
{
3563
0
    CXLDCExtent *extent;
3564
3565
0
    extent = g_new0(CXLDCExtent, 1);
3566
0
    extent->start_dpa = dpa;
3567
0
    extent->len = len;
3568
0
    if (tag) {
3569
0
        memcpy(extent->tag, tag, 0x10);
3570
0
    }
3571
0
    extent->shared_seq = shared_seq;
3572
3573
0
    QTAILQ_INSERT_TAIL(list, extent, node);
3574
0
}
3575
3576
void cxl_remove_extent_from_extent_list(CXLDCExtentList *list,
3577
                                        CXLDCExtent *extent)
3578
0
{
3579
0
    QTAILQ_REMOVE(list, extent, node);
3580
0
    g_free(extent);
3581
0
}
3582
3583
/*
3584
 * Add a new extent to the extent "group" if group exists;
3585
 * otherwise, create a new group
3586
 * Return value: the extent group where the extent is inserted.
3587
 */
3588
CXLDCExtentGroup *cxl_insert_extent_to_extent_group(CXLDCExtentGroup *group,
3589
                                                    uint64_t dpa,
3590
                                                    uint64_t len,
3591
                                                    uint8_t *tag,
3592
                                                    uint16_t shared_seq)
3593
0
{
3594
0
    if (!group) {
3595
0
        group = g_new0(CXLDCExtentGroup, 1);
3596
0
        QTAILQ_INIT(&group->list);
3597
0
    }
3598
0
    cxl_insert_extent_to_extent_list(&group->list, dpa, len,
3599
0
                                     tag, shared_seq);
3600
0
    return group;
3601
0
}
3602
3603
void cxl_extent_group_list_insert_tail(CXLDCExtentGroupList *list,
3604
                                       CXLDCExtentGroup *group)
3605
0
{
3606
0
    QTAILQ_INSERT_TAIL(list, group, node);
3607
0
}
3608
3609
uint32_t cxl_extent_group_list_delete_front(CXLDCExtentGroupList *list)
3610
0
{
3611
0
    CXLDCExtent *ent, *ent_next;
3612
0
    CXLDCExtentGroup *group = QTAILQ_FIRST(list);
3613
0
    uint32_t extents_deleted = 0;
3614
3615
0
    QTAILQ_REMOVE(list, group, node);
3616
0
    QTAILQ_FOREACH_SAFE(ent, &group->list, node, ent_next) {
3617
0
        cxl_remove_extent_from_extent_list(&group->list, ent);
3618
0
        extents_deleted++;
3619
0
    }
3620
0
    g_free(group);
3621
3622
0
    return extents_deleted;
3623
0
}
3624
3625
/*
3626
 * CXL r3.1 Table 8-168: Add Dynamic Capacity Response Input Payload
3627
 * CXL r3.1 Table 8-170: Release Dynamic Capacity Input Payload
3628
 */
3629
typedef struct CXLUpdateDCExtentListInPl {
3630
    uint32_t num_entries_updated;
3631
    uint8_t flags;
3632
    uint8_t rsvd[3];
3633
    /* CXL r3.1 Table 8-169: Updated Extent */
3634
    struct {
3635
        uint64_t start_dpa;
3636
        uint64_t len;
3637
        uint8_t rsvd[8];
3638
    } QEMU_PACKED updated_entries[];
3639
} QEMU_PACKED CXLUpdateDCExtentListInPl;
3640
3641
/*
3642
 * For the extents in the extent list to operate, check whether they are valid
3643
 * 1. The extent should be in the range of a valid DC region;
3644
 * 2. The extent should not cross multiple regions;
3645
 * 3. The start DPA and the length of the extent should align with the block
3646
 * size of the region;
3647
 * 4. The address range of multiple extents in the list should not overlap.
3648
 */
3649
static CXLRetCode cxl_detect_malformed_extent_list(CXLType3Dev *ct3d,
3650
        const CXLUpdateDCExtentListInPl *in)
3651
0
{
3652
0
    uint64_t min_block_size = UINT64_MAX;
3653
0
    CXLDCRegion *region;
3654
0
    CXLDCRegion *lastregion = &ct3d->dc.regions[ct3d->dc.num_regions - 1];
3655
0
    g_autofree unsigned long *blk_bitmap = NULL;
3656
0
    uint64_t dpa, len;
3657
0
    uint32_t i;
3658
3659
0
    for (i = 0; i < ct3d->dc.num_regions; i++) {
3660
0
        region = &ct3d->dc.regions[i];
3661
0
        min_block_size = MIN(min_block_size, region->block_size);
3662
0
    }
3663
3664
0
    blk_bitmap = bitmap_new((lastregion->base + lastregion->len -
3665
0
                             ct3d->dc.regions[0].base) / min_block_size);
3666
3667
0
    for (i = 0; i < in->num_entries_updated; i++) {
3668
0
        dpa = in->updated_entries[i].start_dpa;
3669
0
        len = in->updated_entries[i].len;
3670
3671
0
        region = cxl_find_dc_region(ct3d, dpa, len);
3672
0
        if (!region) {
3673
0
            return CXL_MBOX_INVALID_PA;
3674
0
        }
3675
3676
0
        dpa -= ct3d->dc.regions[0].base;
3677
0
        if (dpa % region->block_size || len % region->block_size) {
3678
0
            return CXL_MBOX_INVALID_EXTENT_LIST;
3679
0
        }
3680
        /* the dpa range already covered by some other extents in the list */
3681
0
        if (test_any_bits_set(blk_bitmap, dpa / min_block_size,
3682
0
            len / min_block_size)) {
3683
0
            return CXL_MBOX_INVALID_EXTENT_LIST;
3684
0
        }
3685
0
        bitmap_set(blk_bitmap, dpa / min_block_size, len / min_block_size);
3686
0
   }
3687
3688
0
    return CXL_MBOX_SUCCESS;
3689
0
}
3690
3691
static CXLRetCode cxl_dcd_add_dyn_cap_rsp_dry_run(CXLType3Dev *ct3d,
3692
        const CXLUpdateDCExtentListInPl *in)
3693
0
{
3694
0
    uint32_t i;
3695
0
    CXLDCExtent *ent;
3696
0
    CXLDCExtentGroup *ext_group;
3697
0
    uint64_t dpa, len;
3698
0
    Range range1, range2;
3699
3700
0
    for (i = 0; i < in->num_entries_updated; i++) {
3701
0
        dpa = in->updated_entries[i].start_dpa;
3702
0
        len = in->updated_entries[i].len;
3703
3704
0
        range_init_nofail(&range1, dpa, len);
3705
3706
        /*
3707
         * The host-accepted DPA range must be contained by the first extent
3708
         * group in the pending list
3709
         */
3710
0
        ext_group = QTAILQ_FIRST(&ct3d->dc.extents_pending);
3711
0
        if (!cxl_extents_contains_dpa_range(&ext_group->list, dpa, len)) {
3712
0
            return CXL_MBOX_INVALID_PA;
3713
0
        }
3714
3715
        /* to-be-added range should not overlap with range already accepted */
3716
0
        QTAILQ_FOREACH(ent, &ct3d->dc.extents, node) {
3717
0
            range_init_nofail(&range2, ent->start_dpa, ent->len);
3718
0
            if (range_overlaps_range(&range1, &range2)) {
3719
0
                return CXL_MBOX_INVALID_PA;
3720
0
            }
3721
0
        }
3722
0
    }
3723
0
    return CXL_MBOX_SUCCESS;
3724
0
}
3725
3726
/*
3727
 * CXL r3.1 section 8.2.9.9.9.3: Add Dynamic Capacity Response (Opcode 4802h)
3728
 * An extent is added to the extent list and becomes usable only after the
3729
 * response is processed successfully.
3730
 */
3731
static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const struct cxl_cmd *cmd,
3732
                                          uint8_t *payload_in,
3733
                                          size_t len_in,
3734
                                          uint8_t *payload_out,
3735
                                          size_t *len_out,
3736
                                          CXLCCI *cci)
3737
0
{
3738
0
    CXLUpdateDCExtentListInPl *in = (void *)payload_in;
3739
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3740
0
    CXLDCExtentList *extent_list = &ct3d->dc.extents;
3741
0
    uint32_t i, num;
3742
0
    uint64_t dpa, len;
3743
0
    CXLRetCode ret;
3744
3745
0
    if (len_in < sizeof(*in)) {
3746
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
3747
0
    }
3748
3749
0
    if (in->num_entries_updated == 0) {
3750
0
        num = cxl_extent_group_list_delete_front(&ct3d->dc.extents_pending);
3751
0
        ct3d->dc.total_extent_count -= num;
3752
0
        return CXL_MBOX_SUCCESS;
3753
0
    }
3754
3755
0
    if (len_in <
3756
0
        sizeof(*in) + sizeof(*in->updated_entries) * in->num_entries_updated) {
3757
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
3758
0
    }
3759
3760
    /* Adding extents causes exceeding device's extent tracking ability. */
3761
0
    if (in->num_entries_updated + ct3d->dc.total_extent_count >
3762
0
        CXL_NUM_EXTENTS_SUPPORTED) {
3763
0
        return CXL_MBOX_RESOURCES_EXHAUSTED;
3764
0
    }
3765
3766
0
    ret = cxl_detect_malformed_extent_list(ct3d, in);
3767
0
    if (ret != CXL_MBOX_SUCCESS) {
3768
0
        return ret;
3769
0
    }
3770
3771
0
    ret = cxl_dcd_add_dyn_cap_rsp_dry_run(ct3d, in);
3772
0
    if (ret != CXL_MBOX_SUCCESS) {
3773
0
        return ret;
3774
0
    }
3775
3776
0
    for (i = 0; i < in->num_entries_updated; i++) {
3777
0
        dpa = in->updated_entries[i].start_dpa;
3778
0
        len = in->updated_entries[i].len;
3779
3780
0
        cxl_insert_extent_to_extent_list(extent_list, dpa, len, NULL, 0);
3781
0
        ct3d->dc.total_extent_count += 1;
3782
0
        ct3d->dc.nr_extents_accepted += 1;
3783
0
        ct3_set_region_block_backed(ct3d, dpa, len);
3784
0
    }
3785
    /* Remove the first extent group in the pending list */
3786
0
    num = cxl_extent_group_list_delete_front(&ct3d->dc.extents_pending);
3787
0
    ct3d->dc.total_extent_count -= num;
3788
3789
0
    return CXL_MBOX_SUCCESS;
3790
0
}
3791
3792
/*
3793
 * Copy extent list from src to dst
3794
 * Return value: number of extents copied
3795
 */
3796
static uint32_t copy_extent_list(CXLDCExtentList *dst,
3797
                                 const CXLDCExtentList *src)
3798
0
{
3799
0
    uint32_t cnt = 0;
3800
0
    CXLDCExtent *ent;
3801
3802
0
    if (!dst || !src) {
3803
0
        return 0;
3804
0
    }
3805
3806
0
    QTAILQ_FOREACH(ent, src, node) {
3807
0
        cxl_insert_extent_to_extent_list(dst, ent->start_dpa, ent->len,
3808
0
                                         ent->tag, ent->shared_seq);
3809
0
        cnt++;
3810
0
    }
3811
0
    return cnt;
3812
0
}
3813
3814
static CXLRetCode cxl_dc_extent_release_dry_run(CXLType3Dev *ct3d,
3815
        const CXLUpdateDCExtentListInPl *in, CXLDCExtentList *updated_list,
3816
        uint32_t *updated_list_size)
3817
0
{
3818
0
    CXLDCExtent *ent, *ent_next;
3819
0
    uint64_t dpa, len;
3820
0
    uint32_t i;
3821
0
    int cnt_delta = 0;
3822
0
    CXLRetCode ret = CXL_MBOX_SUCCESS;
3823
3824
0
    QTAILQ_INIT(updated_list);
3825
0
    copy_extent_list(updated_list, &ct3d->dc.extents);
3826
3827
0
    for (i = 0; i < in->num_entries_updated; i++) {
3828
0
        Range range;
3829
3830
0
        dpa = in->updated_entries[i].start_dpa;
3831
0
        len = in->updated_entries[i].len;
3832
3833
        /* Check if the DPA range is not fully backed with valid extents */
3834
0
        if (!ct3_test_region_block_backed(ct3d, dpa, len)) {
3835
0
            ret = CXL_MBOX_INVALID_PA;
3836
0
            goto free_and_exit;
3837
0
        }
3838
3839
        /* After this point, extent overflow is the only error can happen */
3840
0
        while (len > 0) {
3841
0
            QTAILQ_FOREACH(ent, updated_list, node) {
3842
0
                range_init_nofail(&range, ent->start_dpa, ent->len);
3843
3844
0
                if (range_contains(&range, dpa)) {
3845
0
                    uint64_t len1, len2 = 0, len_done = 0;
3846
0
                    uint64_t ent_start_dpa = ent->start_dpa;
3847
0
                    uint64_t ent_len = ent->len;
3848
3849
0
                    len1 = dpa - ent->start_dpa;
3850
                    /* Found the extent or the subset of an existing extent */
3851
0
                    if (range_contains(&range, dpa + len - 1)) {
3852
0
                        len2 = ent_start_dpa + ent_len - dpa - len;
3853
0
                    } else {
3854
0
                        dpa = ent_start_dpa + ent_len;
3855
0
                    }
3856
0
                    len_done = ent_len - len1 - len2;
3857
3858
0
                    cxl_remove_extent_from_extent_list(updated_list, ent);
3859
0
                    cnt_delta--;
3860
3861
0
                    if (len1) {
3862
0
                        cxl_insert_extent_to_extent_list(updated_list,
3863
0
                                                         ent_start_dpa,
3864
0
                                                         len1, NULL, 0);
3865
0
                        cnt_delta++;
3866
0
                    }
3867
0
                    if (len2) {
3868
0
                        cxl_insert_extent_to_extent_list(updated_list,
3869
0
                                                         dpa + len,
3870
0
                                                         len2, NULL, 0);
3871
0
                        cnt_delta++;
3872
0
                    }
3873
3874
0
                    if (cnt_delta + ct3d->dc.total_extent_count >
3875
0
                            CXL_NUM_EXTENTS_SUPPORTED) {
3876
0
                        ret = CXL_MBOX_RESOURCES_EXHAUSTED;
3877
0
                        goto free_and_exit;
3878
0
                    }
3879
3880
0
                    len -= len_done;
3881
0
                    break;
3882
0
                }
3883
0
            }
3884
0
        }
3885
0
    }
3886
0
free_and_exit:
3887
0
    if (ret != CXL_MBOX_SUCCESS) {
3888
0
        QTAILQ_FOREACH_SAFE(ent, updated_list, node, ent_next) {
3889
0
            cxl_remove_extent_from_extent_list(updated_list, ent);
3890
0
        }
3891
0
        *updated_list_size = 0;
3892
0
    } else {
3893
0
        *updated_list_size = ct3d->dc.nr_extents_accepted + cnt_delta;
3894
0
    }
3895
3896
0
    return ret;
3897
0
}
3898
3899
/*
3900
 * CXL r3.1 section 8.2.9.9.9.4: Release Dynamic Capacity (Opcode 4803h)
3901
 */
3902
static CXLRetCode cmd_dcd_release_dyn_cap(const struct cxl_cmd *cmd,
3903
                                          uint8_t *payload_in,
3904
                                          size_t len_in,
3905
                                          uint8_t *payload_out,
3906
                                          size_t *len_out,
3907
                                          CXLCCI *cci)
3908
0
{
3909
0
    CXLUpdateDCExtentListInPl *in = (void *)payload_in;
3910
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3911
0
    CXLDCExtentList updated_list;
3912
0
    CXLDCExtent *ent, *ent_next;
3913
0
    uint32_t updated_list_size;
3914
0
    CXLRetCode ret;
3915
3916
0
    if (len_in < sizeof(*in)) {
3917
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
3918
0
    }
3919
3920
0
    if (in->num_entries_updated == 0) {
3921
0
        return CXL_MBOX_INVALID_INPUT;
3922
0
    }
3923
3924
0
    if (len_in <
3925
0
        sizeof(*in) + sizeof(*in->updated_entries) * in->num_entries_updated) {
3926
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
3927
0
    }
3928
3929
0
    ret = cxl_detect_malformed_extent_list(ct3d, in);
3930
0
    if (ret != CXL_MBOX_SUCCESS) {
3931
0
        return ret;
3932
0
    }
3933
3934
0
    ret = cxl_dc_extent_release_dry_run(ct3d, in, &updated_list,
3935
0
                                        &updated_list_size);
3936
0
    if (ret != CXL_MBOX_SUCCESS) {
3937
0
        return ret;
3938
0
    }
3939
3940
    /*
3941
     * If the dry run release passes, the returned updated_list will
3942
     * be the updated extent list and we just need to clear the extents
3943
     * in the accepted list and copy extents in the updated_list to accepted
3944
     * list and update the extent count;
3945
     */
3946
0
    QTAILQ_FOREACH_SAFE(ent, &ct3d->dc.extents, node, ent_next) {
3947
0
        ct3_clear_region_block_backed(ct3d, ent->start_dpa, ent->len);
3948
0
        cxl_remove_extent_from_extent_list(&ct3d->dc.extents, ent);
3949
0
    }
3950
0
    copy_extent_list(&ct3d->dc.extents, &updated_list);
3951
0
    QTAILQ_FOREACH_SAFE(ent, &updated_list, node, ent_next) {
3952
0
        ct3_set_region_block_backed(ct3d, ent->start_dpa, ent->len);
3953
0
        cxl_remove_extent_from_extent_list(&updated_list, ent);
3954
0
    }
3955
0
    ct3d->dc.total_extent_count += (updated_list_size -
3956
0
                                    ct3d->dc.nr_extents_accepted);
3957
3958
0
    ct3d->dc.nr_extents_accepted = updated_list_size;
3959
3960
0
    return CXL_MBOX_SUCCESS;
3961
0
}
3962
3963
/* CXL r3.2 section 7.6.7.6.1: Get DCD Info (Opcode 5600h) */
3964
static CXLRetCode cmd_fm_get_dcd_info(const struct cxl_cmd *cmd,
3965
                                      uint8_t *payload_in,
3966
                                      size_t len_in,
3967
                                      uint8_t *payload_out,
3968
                                      size_t *len_out,
3969
                                      CXLCCI *cci)
3970
0
{
3971
0
    struct {
3972
0
        uint8_t num_hosts;
3973
0
        uint8_t num_regions_supported;
3974
0
        uint8_t rsvd1[2];
3975
0
        uint16_t supported_add_sel_policy_bitmask;
3976
0
        uint8_t rsvd2[2];
3977
0
        uint16_t supported_removal_policy_bitmask;
3978
0
        uint8_t sanitize_on_release_bitmask;
3979
0
        uint8_t rsvd3;
3980
0
        uint64_t total_dynamic_capacity;
3981
0
        uint64_t region_blk_size_bitmasks[8];
3982
0
    } QEMU_PACKED *out = (void *)payload_out;
3983
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
3984
0
    CXLDCRegion *region;
3985
0
    int i;
3986
3987
0
    out->num_hosts = 1;
3988
0
    out->num_regions_supported = ct3d->dc.num_regions;
3989
0
    stw_le_p(&out->supported_add_sel_policy_bitmask,
3990
0
             BIT(CXL_EXTENT_SELECTION_POLICY_PRESCRIPTIVE));
3991
0
    stw_le_p(&out->supported_removal_policy_bitmask,
3992
0
             BIT(CXL_EXTENT_REMOVAL_POLICY_PRESCRIPTIVE));
3993
0
    out->sanitize_on_release_bitmask = 0;
3994
3995
0
    stq_le_p(&out->total_dynamic_capacity,
3996
0
             ct3d->dc.total_capacity / CXL_CAPACITY_MULTIPLIER);
3997
3998
0
    for (i = 0; i < ct3d->dc.num_regions; i++) {
3999
0
        region = &ct3d->dc.regions[i];
4000
0
        memcpy(&out->region_blk_size_bitmasks[i],
4001
0
               &region->supported_blk_size_bitmask,
4002
0
               sizeof(out->region_blk_size_bitmasks[i]));
4003
0
    }
4004
4005
0
    *len_out = sizeof(*out);
4006
0
    return CXL_MBOX_SUCCESS;
4007
0
}
4008
4009
static void build_dsmas_flags(uint8_t *flags, CXLDCRegion *region)
4010
0
{
4011
0
    *flags = 0;
4012
4013
0
    if (region->nonvolatile) {
4014
0
        *flags |= BIT(CXL_DSMAS_FLAGS_NONVOLATILE);
4015
0
    }
4016
0
    if (region->sharable) {
4017
0
        *flags |= BIT(CXL_DSMAS_FLAGS_SHARABLE);
4018
0
    }
4019
0
    if (region->hw_managed_coherency) {
4020
0
        *flags |= BIT(CXL_DSMAS_FLAGS_HW_MANAGED_COHERENCY);
4021
0
    }
4022
0
    if (region->ic_specific_dc_management) {
4023
0
        *flags |= BIT(CXL_DSMAS_FLAGS_IC_SPECIFIC_DC_MANAGEMENT);
4024
0
    }
4025
0
    if (region->rdonly) {
4026
0
        *flags |= BIT(CXL_DSMAS_FLAGS_RDONLY);
4027
0
    }
4028
0
}
4029
4030
/*
4031
 * CXL r3.2 section 7.6.7.6.2:
4032
 * Get Host DC Region Configuration (Opcode 5601h)
4033
 */
4034
static CXLRetCode cmd_fm_get_host_dc_region_config(const struct cxl_cmd *cmd,
4035
                                                   uint8_t *payload_in,
4036
                                                   size_t len_in,
4037
                                                   uint8_t *payload_out,
4038
                                                   size_t *len_out,
4039
                                                   CXLCCI *cci)
4040
0
{
4041
0
    struct {
4042
0
        uint16_t host_id;
4043
0
        uint8_t region_cnt;
4044
0
        uint8_t start_rid;
4045
0
    } QEMU_PACKED *in = (void *)payload_in;
4046
0
    struct {
4047
0
        uint16_t host_id;
4048
0
        uint8_t num_regions;
4049
0
        uint8_t regions_returned;
4050
0
        struct {
4051
0
            uint64_t base;
4052
0
            uint64_t decode_len;
4053
0
            uint64_t region_len;
4054
0
            uint64_t block_size;
4055
0
            uint8_t flags;
4056
0
            uint8_t rsvd1[3];
4057
0
            uint8_t sanitize;
4058
0
            uint8_t rsvd2[3];
4059
0
        } QEMU_PACKED records[];
4060
0
    } QEMU_PACKED *out = (void *)payload_out;
4061
0
    struct {
4062
0
        uint32_t num_extents_supported;
4063
0
        uint32_t num_extents_available;
4064
0
        uint32_t num_tags_supported;
4065
0
        uint32_t num_tags_available;
4066
0
    } QEMU_PACKED *extra_out;
4067
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4068
0
    uint16_t record_count, out_pl_len, i;
4069
4070
0
    if (in->start_rid >= ct3d->dc.num_regions) {
4071
0
        return CXL_MBOX_INVALID_INPUT;
4072
0
    }
4073
0
    record_count = MIN(ct3d->dc.num_regions - in->start_rid, in->region_cnt);
4074
4075
0
    out_pl_len = sizeof(*out) + record_count * sizeof(out->records[0]);
4076
0
    extra_out = (void *)out + out_pl_len;
4077
0
    out_pl_len += sizeof(*extra_out);
4078
4079
0
    assert(out_pl_len <= CXL_MAILBOX_MAX_PAYLOAD_SIZE);
4080
4081
0
    stw_le_p(&out->host_id, 0);
4082
0
    out->num_regions = ct3d->dc.num_regions;
4083
0
    out->regions_returned = record_count;
4084
4085
0
    for (i = 0; i < record_count; i++) {
4086
0
        stq_le_p(&out->records[i].base,
4087
0
                 ct3d->dc.regions[in->start_rid + i].base);
4088
0
        stq_le_p(&out->records[i].decode_len,
4089
0
                 ct3d->dc.regions[in->start_rid + i].decode_len /
4090
0
                 CXL_CAPACITY_MULTIPLIER);
4091
0
        stq_le_p(&out->records[i].region_len,
4092
0
                 ct3d->dc.regions[in->start_rid + i].len);
4093
0
        stq_le_p(&out->records[i].block_size,
4094
0
                 ct3d->dc.regions[in->start_rid + i].block_size);
4095
0
        build_dsmas_flags(&out->records[i].flags,
4096
0
                          &ct3d->dc.regions[in->start_rid + i]);
4097
        /* Sanitize is bit 0 of flags. */
4098
0
        out->records[i].sanitize =
4099
0
            ct3d->dc.regions[in->start_rid + i].flags & BIT(0);
4100
0
    }
4101
4102
0
    stl_le_p(&extra_out->num_extents_supported, CXL_NUM_EXTENTS_SUPPORTED);
4103
0
    stl_le_p(&extra_out->num_extents_available, CXL_NUM_EXTENTS_SUPPORTED -
4104
0
             ct3d->dc.total_extent_count);
4105
0
    stl_le_p(&extra_out->num_tags_supported, CXL_NUM_TAGS_SUPPORTED);
4106
0
    stl_le_p(&extra_out->num_tags_available, CXL_NUM_TAGS_SUPPORTED);
4107
4108
0
    *len_out = out_pl_len;
4109
0
    return CXL_MBOX_SUCCESS;
4110
0
}
4111
4112
/* CXL r3.2 section 7.6.7.6.3: Set Host DC Region Configuration (Opcode 5602) */
4113
static CXLRetCode cmd_fm_set_dc_region_config(const struct cxl_cmd *cmd,
4114
                                              uint8_t *payload_in,
4115
                                              size_t len_in,
4116
                                              uint8_t *payload_out,
4117
                                              size_t *len_out,
4118
                                              CXLCCI *cci)
4119
0
{
4120
0
    struct {
4121
0
        uint8_t reg_id;
4122
0
        uint8_t rsvd[3];
4123
0
        uint64_t block_sz;
4124
0
        uint8_t flags;
4125
0
        uint8_t rsvd2[3];
4126
0
    } QEMU_PACKED *in = (void *)payload_in;
4127
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4128
0
    CXLEventDynamicCapacity dcEvent = {};
4129
0
    CXLDCRegion *region = &ct3d->dc.regions[in->reg_id];
4130
4131
    /*
4132
     * CXL r3.2 7.6.7.6.3: Set DC Region Configuration
4133
     * This command shall fail with Unsupported when the Sanitize on Release
4134
     * field does not match the region’s configuration... and the device
4135
     * does not support reconfiguration of the Sanitize on Release setting.
4136
     *
4137
     * Currently not reconfigurable, so always fail if sanitize bit (bit 0)
4138
     * doesn't match.
4139
     */
4140
0
    if ((in->flags & 0x1) != (region->flags & 0x1)) {
4141
0
        return CXL_MBOX_UNSUPPORTED;
4142
0
    }
4143
4144
0
    if (in->reg_id >= DCD_MAX_NUM_REGION) {
4145
0
        return CXL_MBOX_UNSUPPORTED;
4146
0
    }
4147
4148
    /* Check that no extents are in the region being reconfigured */
4149
0
    if (!bitmap_empty(region->blk_bitmap, region->len / region->block_size)) {
4150
0
        return CXL_MBOX_UNSUPPORTED;
4151
0
    }
4152
4153
    /* Check that new block size is supported */
4154
0
    if (!is_power_of_2(in->block_sz) ||
4155
0
        !(in->block_sz & region->supported_blk_size_bitmask)) {
4156
0
        return CXL_MBOX_INVALID_INPUT;
4157
0
    }
4158
4159
    /* Return success if new block size == current block size */
4160
0
    if (in->block_sz == region->block_size) {
4161
0
        return CXL_MBOX_SUCCESS;
4162
0
    }
4163
4164
    /* Free bitmap and create new one for new block size. */
4165
0
    qemu_mutex_lock(&region->bitmap_lock);
4166
0
    g_free(region->blk_bitmap);
4167
0
    region->blk_bitmap = bitmap_new(region->len / in->block_sz);
4168
0
    qemu_mutex_unlock(&region->bitmap_lock);
4169
0
    region->block_size = in->block_sz;
4170
4171
    /* Create event record and insert into event log */
4172
0
    cxl_assign_event_header(&dcEvent.hdr,
4173
0
                            &dynamic_capacity_uuid,
4174
0
                            (1 << CXL_EVENT_TYPE_INFO),
4175
0
                            sizeof(dcEvent),
4176
0
                            cxl_device_get_timestamp(&ct3d->cxl_dstate),
4177
0
                            0, 0, 0, 0, 0, 0, 0, 0);
4178
0
    dcEvent.type = DC_EVENT_REGION_CONFIG_UPDATED;
4179
0
    dcEvent.validity_flags = 1;
4180
0
    dcEvent.host_id = 0;
4181
0
    dcEvent.updated_region_id = in->reg_id;
4182
4183
0
    if (cxl_event_insert(&ct3d->cxl_dstate,
4184
0
                         CXL_EVENT_TYPE_DYNAMIC_CAP,
4185
0
                         (CXLEventRecordRaw *)&dcEvent)) {
4186
0
        cxl_event_irq_assert(ct3d);
4187
0
    }
4188
0
    return CXL_MBOX_SUCCESS;
4189
0
}
4190
4191
/* CXL r3.2 section 7.6.7.6.4: Get DC Region Extent Lists (Opcode 5603h) */
4192
static CXLRetCode cmd_fm_get_dc_region_extent_list(const struct cxl_cmd *cmd,
4193
                                                   uint8_t *payload_in,
4194
                                                   size_t len_in,
4195
                                                   uint8_t *payload_out,
4196
                                                   size_t *len_out,
4197
                                                   CXLCCI *cci)
4198
0
{
4199
0
    struct {
4200
0
        uint16_t host_id;
4201
0
        uint8_t rsvd[2];
4202
0
        uint32_t extent_cnt;
4203
0
        uint32_t start_extent_id;
4204
0
    } QEMU_PACKED *in = (void *)payload_in;
4205
0
    struct {
4206
0
        uint16_t host_id;
4207
0
        uint8_t rsvd[2];
4208
0
        uint32_t start_extent_id;
4209
0
        uint32_t extents_returned;
4210
0
        uint32_t total_extents;
4211
0
        uint32_t list_generation_num;
4212
0
        uint8_t rsvd2[4];
4213
0
        CXLDCExtentRaw records[];
4214
0
    } QEMU_PACKED *out = (void *)payload_out;
4215
0
    QEMU_BUILD_BUG_ON(sizeof(*in) != 0xc);
4216
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4217
0
    CXLDCExtent *ent;
4218
0
    CXLDCExtentRaw *out_rec;
4219
0
    uint16_t record_count = 0, record_done = 0, i = 0;
4220
0
    uint16_t out_pl_len, max_size;
4221
4222
0
    if (in->host_id != 0) {
4223
0
        return CXL_MBOX_INVALID_INPUT;
4224
0
    }
4225
4226
0
    if (in->start_extent_id > ct3d->dc.nr_extents_accepted) {
4227
0
        return CXL_MBOX_INVALID_INPUT;
4228
0
    }
4229
4230
0
    record_count = MIN(in->extent_cnt,
4231
0
                       ct3d->dc.nr_extents_accepted - in->start_extent_id);
4232
0
    max_size = CXL_MAILBOX_MAX_PAYLOAD_SIZE - sizeof(*out);
4233
0
    record_count = MIN(record_count, max_size / sizeof(out->records[0]));
4234
0
    out_pl_len = sizeof(*out) + record_count * sizeof(out->records[0]);
4235
4236
0
    stw_le_p(&out->host_id, in->host_id);
4237
0
    stl_le_p(&out->start_extent_id, in->start_extent_id);
4238
0
    stl_le_p(&out->extents_returned, record_count);
4239
0
    stl_le_p(&out->total_extents, ct3d->dc.nr_extents_accepted);
4240
0
    stl_le_p(&out->list_generation_num, ct3d->dc.ext_list_gen_seq);
4241
4242
0
    if (record_count > 0) {
4243
0
        QTAILQ_FOREACH(ent, &ct3d->dc.extents, node) {
4244
0
            if (i++ < in->start_extent_id) {
4245
0
                continue;
4246
0
            }
4247
0
            out_rec = &out->records[record_done];
4248
0
            stq_le_p(&out_rec->start_dpa, ent->start_dpa);
4249
0
            stq_le_p(&out_rec->len, ent->len);
4250
0
            memcpy(&out_rec->tag, ent->tag, 0x10);
4251
0
            stw_le_p(&out_rec->shared_seq, ent->shared_seq);
4252
4253
0
            record_done++;
4254
0
            if (record_done == record_count) {
4255
0
                break;
4256
0
            }
4257
0
        }
4258
0
    }
4259
4260
0
    *len_out = out_pl_len;
4261
0
    return CXL_MBOX_SUCCESS;
4262
0
}
4263
4264
/*
4265
 * Helper function to convert CXLDCExtentRaw to CXLUpdateDCExtentListInPl
4266
 * in order to reuse cxl_detect_malformed_extent_list() function which accepts
4267
 * CXLUpdateDCExtentListInPl as a parameter.
4268
 */
4269
static void convert_raw_extents(CXLDCExtentRaw raw_extents[],
4270
                                CXLUpdateDCExtentListInPl *extent_list,
4271
                                int count)
4272
0
{
4273
0
    int i;
4274
4275
0
    extent_list->num_entries_updated = count;
4276
4277
0
    for (i = 0; i < count; i++) {
4278
0
        extent_list->updated_entries[i].start_dpa = raw_extents[i].start_dpa;
4279
0
        extent_list->updated_entries[i].len = raw_extents[i].len;
4280
0
    }
4281
0
}
4282
4283
/* CXL r3.2 Section 7.6.7.6.5: Initiate Dynamic Capacity Add (Opcode 5604h) */
4284
static CXLRetCode cmd_fm_initiate_dc_add(const struct cxl_cmd *cmd,
4285
                                         uint8_t *payload_in,
4286
                                         size_t len_in,
4287
                                         uint8_t *payload_out,
4288
                                         size_t *len_out,
4289
                                         CXLCCI *cci)
4290
0
{
4291
0
    struct {
4292
0
        uint16_t host_id;
4293
0
        uint8_t selection_policy;
4294
0
        uint8_t reg_num;
4295
0
        uint64_t length;
4296
0
        uint8_t tag[0x10];
4297
0
        uint32_t ext_count;
4298
0
        CXLDCExtentRaw extents[];
4299
0
    } QEMU_PACKED *in = (void *)payload_in;
4300
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4301
0
    int i, rc;
4302
4303
0
    switch (in->selection_policy) {
4304
0
        case CXL_EXTENT_SELECTION_POLICY_PRESCRIPTIVE: {
4305
            /* Adding extents exceeds device's extent tracking ability. */
4306
0
            if (in->ext_count + ct3d->dc.total_extent_count >
4307
0
                CXL_NUM_EXTENTS_SUPPORTED) {
4308
0
                return CXL_MBOX_RESOURCES_EXHAUSTED;
4309
0
            }
4310
4311
0
            g_autofree CXLUpdateDCExtentListInPl *list =
4312
0
                g_malloc0(sizeof(*list) +
4313
0
                    in->ext_count * sizeof(*list->updated_entries));
4314
4315
0
            convert_raw_extents(in->extents, list, in->ext_count);
4316
0
            rc = cxl_detect_malformed_extent_list(ct3d, list);
4317
4318
0
            for (i = 0; i < in->ext_count; i++) {
4319
0
                CXLDCExtentRaw *ext = &in->extents[i];
4320
4321
                /* Check requested extents do not overlap with pending ones. */
4322
0
                if (cxl_extent_groups_overlaps_dpa_range(&ct3d->dc.extents_pending,
4323
0
                                                         ext->start_dpa,
4324
0
                                                         ext->len)) {
4325
0
                    return CXL_MBOX_INVALID_EXTENT_LIST;
4326
0
                }
4327
                /* Check requested extents do not overlap with existing ones. */
4328
0
                if (cxl_extents_overlaps_dpa_range(&ct3d->dc.extents,
4329
0
                                                   ext->start_dpa,
4330
0
                                                   ext->len)) {
4331
0
                    return CXL_MBOX_INVALID_EXTENT_LIST;
4332
0
                }
4333
0
            }
4334
4335
0
            if (rc) {
4336
0
                return rc;
4337
0
            }
4338
4339
0
            CXLDCExtentGroup *group = NULL;
4340
0
            for (i = 0; i < in->ext_count; i++) {
4341
0
                CXLDCExtentRaw *ext = &in->extents[i];
4342
4343
0
                group = cxl_insert_extent_to_extent_group(group, ext->start_dpa,
4344
0
                                                          ext->len, ext->tag,
4345
0
                                                          ext->shared_seq);
4346
0
            }
4347
4348
0
            cxl_extent_group_list_insert_tail(&ct3d->dc.extents_pending, group);
4349
0
            ct3d->dc.total_extent_count += in->ext_count;
4350
0
            cxl_create_dc_event_records_for_extents(ct3d,
4351
0
                                                    DC_EVENT_ADD_CAPACITY,
4352
0
                                                    in->extents,
4353
0
                                                    in->ext_count);
4354
4355
0
            return CXL_MBOX_SUCCESS;
4356
0
        }
4357
0
        default: {
4358
0
            qemu_log_mask(LOG_UNIMP,
4359
0
                          "CXL extent selection policy not supported.\n");
4360
0
            return CXL_MBOX_INVALID_INPUT;
4361
0
        }
4362
0
    }
4363
0
}
4364
4365
0
#define CXL_EXTENT_REMOVAL_POLICY_MASK 0x0F
4366
0
#define CXL_FORCED_REMOVAL_MASK (1 << 4)
4367
/*
4368
 * CXL r3.2 Section 7.6.7.6.6:
4369
 * Initiate Dynamic Capacity Release (Opcode 5605h)
4370
 */
4371
static CXLRetCode cmd_fm_initiate_dc_release(const struct cxl_cmd *cmd,
4372
                                             uint8_t *payload_in,
4373
                                             size_t len_in,
4374
                                             uint8_t *payload_out,
4375
                                             size_t *len_out,
4376
                                             CXLCCI *cci)
4377
0
{
4378
0
    struct {
4379
0
        uint16_t host_id;
4380
0
        uint8_t flags;
4381
0
        uint8_t reg_num;
4382
0
        uint64_t length;
4383
0
        uint8_t tag[0x10];
4384
0
        uint32_t ext_count;
4385
0
        CXLDCExtentRaw extents[];
4386
0
    } QEMU_PACKED *in = (void *)payload_in;
4387
0
    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4388
0
    int i, rc;
4389
4390
0
    switch (in->flags & CXL_EXTENT_REMOVAL_POLICY_MASK) {
4391
0
        case CXL_EXTENT_REMOVAL_POLICY_PRESCRIPTIVE: {
4392
0
            CXLDCExtentList updated_list;
4393
0
            uint32_t updated_list_size;
4394
0
            g_autofree CXLUpdateDCExtentListInPl *list =
4395
0
                g_malloc0(sizeof(*list) +
4396
0
                    in->ext_count * sizeof(*list->updated_entries));
4397
4398
0
            convert_raw_extents(in->extents, list, in->ext_count);
4399
0
            rc = cxl_detect_malformed_extent_list(ct3d, list);
4400
0
            if (rc) {
4401
0
                return rc;
4402
0
            }
4403
4404
            /*
4405
             * Fail with Invalid PA if an extent is pending and Forced Removal
4406
             * flag not set.
4407
             */
4408
0
            if (!(in->flags & CXL_FORCED_REMOVAL_MASK)) {
4409
0
                for (i = 0; i < in->ext_count; i++) {
4410
0
                    CXLDCExtentRaw ext = in->extents[i];
4411
                    /*
4412
                     * Check requested extents don't overlap with pending
4413
                     * extents.
4414
                     */
4415
0
                    if (cxl_extent_groups_overlaps_dpa_range(
4416
0
                            &ct3d->dc.extents_pending,
4417
0
                            ext.start_dpa,
4418
0
                            ext.len)) {
4419
0
                        return CXL_MBOX_INVALID_PA;
4420
0
                    }
4421
0
                }
4422
0
            }
4423
4424
0
            rc = cxl_dc_extent_release_dry_run(ct3d,
4425
0
                                               list,
4426
0
                                               &updated_list,
4427
0
                                               &updated_list_size);
4428
0
            if (rc) {
4429
0
                return rc;
4430
0
            }
4431
0
            cxl_create_dc_event_records_for_extents(ct3d,
4432
0
                                                    DC_EVENT_RELEASE_CAPACITY,
4433
0
                                                    in->extents,
4434
0
                                                    in->ext_count);
4435
0
            return CXL_MBOX_SUCCESS;
4436
0
        }
4437
0
        default: {
4438
0
            qemu_log_mask(LOG_UNIMP,
4439
0
                "CXL extent removal policy not supported.\n");
4440
0
            return CXL_MBOX_INVALID_INPUT;
4441
0
        }
4442
0
    }
4443
0
}
4444
4445
static const struct cxl_cmd cxl_cmd_set[256][256] = {
4446
    [INFOSTAT][BACKGROUND_OPERATION_ABORT] = { "BACKGROUND_OPERATION_ABORT",
4447
        cmd_infostat_bg_op_abort, 0, 0 },
4448
    [EVENTS][GET_RECORDS] = { "EVENTS_GET_RECORDS",
4449
        cmd_events_get_records, 1, 0 },
4450
    [EVENTS][CLEAR_RECORDS] = { "EVENTS_CLEAR_RECORDS",
4451
        cmd_events_clear_records, ~0, CXL_MBOX_IMMEDIATE_LOG_CHANGE },
4452
    [EVENTS][GET_INTERRUPT_POLICY] = { "EVENTS_GET_INTERRUPT_POLICY",
4453
                                      cmd_events_get_interrupt_policy, 0, 0 },
4454
    [EVENTS][SET_INTERRUPT_POLICY] = { "EVENTS_SET_INTERRUPT_POLICY",
4455
                                      cmd_events_set_interrupt_policy,
4456
                                      ~0, CXL_MBOX_IMMEDIATE_CONFIG_CHANGE },
4457
    [FIRMWARE_UPDATE][GET_INFO] = { "FIRMWARE_UPDATE_GET_INFO",
4458
        cmd_firmware_update_get_info, 0, 0 },
4459
    [FIRMWARE_UPDATE][TRANSFER] = { "FIRMWARE_UPDATE_TRANSFER",
4460
        cmd_firmware_update_transfer, ~0,
4461
        CXL_MBOX_BACKGROUND_OPERATION | CXL_MBOX_BACKGROUND_OPERATION_ABORT },
4462
    [FIRMWARE_UPDATE][ACTIVATE] = { "FIRMWARE_UPDATE_ACTIVATE",
4463
        cmd_firmware_update_activate, 2,
4464
        CXL_MBOX_BACKGROUND_OPERATION | CXL_MBOX_BACKGROUND_OPERATION_ABORT },
4465
    [TIMESTAMP][GET] = { "TIMESTAMP_GET", cmd_timestamp_get, 0, 0 },
4466
    [TIMESTAMP][SET] = { "TIMESTAMP_SET", cmd_timestamp_set,
4467
                         8, CXL_MBOX_IMMEDIATE_POLICY_CHANGE },
4468
    [LOGS][GET_SUPPORTED] = { "LOGS_GET_SUPPORTED", cmd_logs_get_supported,
4469
                              0, 0 },
4470
    [LOGS][GET_LOG] = { "LOGS_GET_LOG", cmd_logs_get_log, 0x18, 0 },
4471
    [FEATURES][GET_SUPPORTED] = { "FEATURES_GET_SUPPORTED",
4472
                                  cmd_features_get_supported, 0x8, 0 },
4473
    [FEATURES][GET_FEATURE] = { "FEATURES_GET_FEATURE",
4474
                                cmd_features_get_feature, 0x15, 0 },
4475
    [FEATURES][SET_FEATURE] = { "FEATURES_SET_FEATURE",
4476
                                cmd_features_set_feature,
4477
                                ~0,
4478
                                (CXL_MBOX_IMMEDIATE_CONFIG_CHANGE |
4479
                                 CXL_MBOX_IMMEDIATE_DATA_CHANGE |
4480
                                 CXL_MBOX_IMMEDIATE_POLICY_CHANGE |
4481
                                 CXL_MBOX_IMMEDIATE_LOG_CHANGE |
4482
                                 CXL_MBOX_SECURITY_STATE_CHANGE)},
4483
    [MAINTENANCE][PERFORM] = { "MAINTENANCE_PERFORM",
4484
                               cmd_media_perform_maintenance, ~0,
4485
                               CXL_MBOX_IMMEDIATE_CONFIG_CHANGE |
4486
                               CXL_MBOX_IMMEDIATE_DATA_CHANGE |
4487
                               CXL_MBOX_IMMEDIATE_LOG_CHANGE |
4488
                               CXL_MBOX_BACKGROUND_OPERATION },
4489
    [IDENTIFY][MEMORY_DEVICE] = { "IDENTIFY_MEMORY_DEVICE",
4490
        cmd_identify_memory_device, 0, 0 },
4491
    [CCLS][GET_PARTITION_INFO] = { "CCLS_GET_PARTITION_INFO",
4492
        cmd_ccls_get_partition_info, 0, 0 },
4493
    [CCLS][GET_LSA] = { "CCLS_GET_LSA", cmd_ccls_get_lsa, 8, 0 },
4494
    [CCLS][SET_LSA] = { "CCLS_SET_LSA", cmd_ccls_set_lsa,
4495
        ~0, CXL_MBOX_IMMEDIATE_CONFIG_CHANGE | CXL_MBOX_IMMEDIATE_DATA_CHANGE },
4496
    [HEALTH_INFO_ALERTS][GET_ALERT_CONFIG] = {
4497
        "HEALTH_INFO_ALERTS_GET_ALERT_CONFIG",
4498
        cmd_get_alert_config, 0, 0 },
4499
    [HEALTH_INFO_ALERTS][SET_ALERT_CONFIG] = {
4500
        "HEALTH_INFO_ALERTS_SET_ALERT_CONFIG",
4501
        cmd_set_alert_config, 12, CXL_MBOX_IMMEDIATE_POLICY_CHANGE },
4502
    [SANITIZE][OVERWRITE] = { "SANITIZE_OVERWRITE", cmd_sanitize_overwrite, 0,
4503
        (CXL_MBOX_IMMEDIATE_DATA_CHANGE |
4504
         CXL_MBOX_SECURITY_STATE_CHANGE |
4505
         CXL_MBOX_BACKGROUND_OPERATION |
4506
         CXL_MBOX_BACKGROUND_OPERATION_ABORT)},
4507
    [SANITIZE][MEDIA_OPERATIONS] = { "MEDIA_OPERATIONS", cmd_media_operations,
4508
        ~0,
4509
        (CXL_MBOX_IMMEDIATE_DATA_CHANGE |
4510
         CXL_MBOX_BACKGROUND_OPERATION)},
4511
    [PERSISTENT_MEM][GET_SECURITY_STATE] = { "GET_SECURITY_STATE",
4512
        cmd_get_security_state, 0, 0 },
4513
    [MEDIA_AND_POISON][GET_POISON_LIST] = { "MEDIA_AND_POISON_GET_POISON_LIST",
4514
        cmd_media_get_poison_list, 16, 0 },
4515
    [MEDIA_AND_POISON][INJECT_POISON] = { "MEDIA_AND_POISON_INJECT_POISON",
4516
        cmd_media_inject_poison, 8, 0 },
4517
    [MEDIA_AND_POISON][CLEAR_POISON] = { "MEDIA_AND_POISON_CLEAR_POISON",
4518
        cmd_media_clear_poison, 72, 0 },
4519
    [MEDIA_AND_POISON][GET_SCAN_MEDIA_CAPABILITIES] = {
4520
        "MEDIA_AND_POISON_GET_SCAN_MEDIA_CAPABILITIES",
4521
        cmd_media_get_scan_media_capabilities, 16, 0 },
4522
    [MEDIA_AND_POISON][SCAN_MEDIA] = { "MEDIA_AND_POISON_SCAN_MEDIA",
4523
        cmd_media_scan_media, 17,
4524
        (CXL_MBOX_BACKGROUND_OPERATION | CXL_MBOX_BACKGROUND_OPERATION_ABORT)},
4525
    [MEDIA_AND_POISON][GET_SCAN_MEDIA_RESULTS] = {
4526
        "MEDIA_AND_POISON_GET_SCAN_MEDIA_RESULTS",
4527
        cmd_media_get_scan_media_results, 0, 0 },
4528
};
4529
4530
static const struct cxl_cmd cxl_cmd_set_dcd[256][256] = {
4531
    [DCD_CONFIG][GET_DC_CONFIG] = { "DCD_GET_DC_CONFIG",
4532
        cmd_dcd_get_dyn_cap_config, 2, 0 },
4533
    [DCD_CONFIG][GET_DYN_CAP_EXT_LIST] = {
4534
        "DCD_GET_DYNAMIC_CAPACITY_EXTENT_LIST", cmd_dcd_get_dyn_cap_ext_list,
4535
        8, 0 },
4536
    [DCD_CONFIG][ADD_DYN_CAP_RSP] = {
4537
        "DCD_ADD_DYNAMIC_CAPACITY_RESPONSE", cmd_dcd_add_dyn_cap_rsp,
4538
        ~0, CXL_MBOX_IMMEDIATE_DATA_CHANGE },
4539
    [DCD_CONFIG][RELEASE_DYN_CAP] = {
4540
        "DCD_RELEASE_DYNAMIC_CAPACITY", cmd_dcd_release_dyn_cap,
4541
        ~0, CXL_MBOX_IMMEDIATE_DATA_CHANGE },
4542
};
4543
4544
static const struct cxl_cmd cxl_cmd_set_sw[256][256] = {
4545
    [INFOSTAT][IS_IDENTIFY] = { "IDENTIFY", cmd_infostat_identify, 0, 0 },
4546
    [INFOSTAT][BACKGROUND_OPERATION_STATUS] = { "BACKGROUND_OPERATION_STATUS",
4547
        cmd_infostat_bg_op_sts, 0, 0 },
4548
    [INFOSTAT][BACKGROUND_OPERATION_ABORT] = { "BACKGROUND_OPERATION_ABORT",
4549
        cmd_infostat_bg_op_abort, 0, 0 },
4550
    [TIMESTAMP][GET] = { "TIMESTAMP_GET", cmd_timestamp_get, 0, 0 },
4551
    [TIMESTAMP][SET] = { "TIMESTAMP_SET", cmd_timestamp_set, 8,
4552
                         CXL_MBOX_IMMEDIATE_POLICY_CHANGE },
4553
    [LOGS][GET_SUPPORTED] = { "LOGS_GET_SUPPORTED", cmd_logs_get_supported, 0,
4554
                              0 },
4555
    [LOGS][GET_LOG] = { "LOGS_GET_LOG", cmd_logs_get_log, 0x18, 0 },
4556
    [PHYSICAL_SWITCH][IDENTIFY_SWITCH_DEVICE] = { "IDENTIFY_SWITCH_DEVICE",
4557
        cmd_identify_switch_device, 0, 0 },
4558
    [PHYSICAL_SWITCH][GET_PHYSICAL_PORT_STATE] = { "SWITCH_PHYSICAL_PORT_STATS",
4559
        cmd_get_physical_port_state, ~0, 0 },
4560
    [PHYSICAL_SWITCH][PHYSICAL_PORT_CONTROL] = { "SWITCH_PHYSICAL_PORT_CONTROL",
4561
        cmd_physical_port_control, 2, 0 },
4562
    [TUNNEL][MANAGEMENT_COMMAND] = { "TUNNEL_MANAGEMENT_COMMAND",
4563
                                     cmd_tunnel_management_cmd, ~0, 0 },
4564
};
4565
4566
static const struct cxl_cmd cxl_cmd_set_fm_dcd[256][256] = {
4567
    [FMAPI_DCD_MGMT][GET_DCD_INFO] = { "GET_DCD_INFO",
4568
        cmd_fm_get_dcd_info, 0, 0 },
4569
    [FMAPI_DCD_MGMT][GET_HOST_DC_REGION_CONFIG] = { "GET_HOST_DC_REGION_CONFIG",
4570
        cmd_fm_get_host_dc_region_config, 4, 0 },
4571
    [FMAPI_DCD_MGMT][SET_DC_REGION_CONFIG] = { "SET_DC_REGION_CONFIG",
4572
        cmd_fm_set_dc_region_config, 16,
4573
        (CXL_MBOX_CONFIG_CHANGE_COLD_RESET |
4574
         CXL_MBOX_CONFIG_CHANGE_CONV_RESET |
4575
         CXL_MBOX_CONFIG_CHANGE_CXL_RESET |
4576
         CXL_MBOX_IMMEDIATE_CONFIG_CHANGE |
4577
         CXL_MBOX_IMMEDIATE_DATA_CHANGE) },
4578
    [FMAPI_DCD_MGMT][GET_DC_REGION_EXTENT_LIST] = { "GET_DC_REGION_EXTENT_LIST",
4579
        cmd_fm_get_dc_region_extent_list, 12, 0 },
4580
    [FMAPI_DCD_MGMT][INITIATE_DC_ADD] = { "INIT_DC_ADD",
4581
        cmd_fm_initiate_dc_add, ~0,
4582
        (CXL_MBOX_CONFIG_CHANGE_COLD_RESET |
4583
        CXL_MBOX_CONFIG_CHANGE_CONV_RESET |
4584
        CXL_MBOX_CONFIG_CHANGE_CXL_RESET |
4585
        CXL_MBOX_IMMEDIATE_CONFIG_CHANGE |
4586
        CXL_MBOX_IMMEDIATE_DATA_CHANGE) },
4587
    [FMAPI_DCD_MGMT][INITIATE_DC_RELEASE] = { "INIT_DC_RELEASE",
4588
        cmd_fm_initiate_dc_release, ~0,
4589
        (CXL_MBOX_CONFIG_CHANGE_COLD_RESET |
4590
         CXL_MBOX_CONFIG_CHANGE_CONV_RESET |
4591
         CXL_MBOX_CONFIG_CHANGE_CXL_RESET |
4592
         CXL_MBOX_IMMEDIATE_CONFIG_CHANGE |
4593
         CXL_MBOX_IMMEDIATE_DATA_CHANGE) },
4594
};
4595
4596
/*
4597
 * While the command is executing in the background, the device should
4598
 * update the percentage complete in the Background Command Status Register
4599
 * at least once per second.
4600
 */
4601
4602
0
#define CXL_MBOX_BG_UPDATE_FREQ 1000UL
4603
4604
int cxl_process_cci_message(CXLCCI *cci, uint8_t set, uint8_t cmd,
4605
                            size_t len_in, uint8_t *pl_in, size_t *len_out,
4606
                            uint8_t *pl_out, bool *bg_started)
4607
0
{
4608
0
    int ret;
4609
0
    const struct cxl_cmd *cxl_cmd;
4610
0
    opcode_handler h;
4611
0
    CXLDeviceState *cxl_dstate;
4612
4613
0
    *len_out = 0;
4614
0
    cxl_cmd = &cci->cxl_cmd_set[set][cmd];
4615
0
    h = cxl_cmd->handler;
4616
0
    if (!h) {
4617
0
        qemu_log_mask(LOG_UNIMP, "Command %04xh not implemented\n",
4618
0
                      set << 8 | cmd);
4619
0
        return CXL_MBOX_UNSUPPORTED;
4620
0
    }
4621
4622
0
    if (len_in != cxl_cmd->in && cxl_cmd->in != ~0) {
4623
0
        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
4624
0
    }
4625
4626
    /* Only one bg command at a time */
4627
0
    if ((cxl_cmd->effect & CXL_MBOX_BACKGROUND_OPERATION) &&
4628
0
        cci->bg.runtime > 0) {
4629
0
        return CXL_MBOX_BUSY;
4630
0
    }
4631
4632
    /* forbid any selected commands while the media is disabled */
4633
0
    if (object_dynamic_cast(OBJECT(cci->d), TYPE_CXL_TYPE3)) {
4634
0
        cxl_dstate = &CXL_TYPE3(cci->d)->cxl_dstate;
4635
4636
0
        if (cxl_dev_media_disabled(cxl_dstate)) {
4637
0
            if (h == cmd_events_get_records ||
4638
0
                h == cmd_ccls_get_partition_info ||
4639
0
                h == cmd_ccls_set_lsa ||
4640
0
                h == cmd_ccls_get_lsa ||
4641
0
                h == cmd_logs_get_log ||
4642
0
                h == cmd_media_get_poison_list ||
4643
0
                h == cmd_media_inject_poison ||
4644
0
                h == cmd_media_clear_poison ||
4645
0
                h == cmd_sanitize_overwrite ||
4646
0
                h == cmd_firmware_update_transfer ||
4647
0
                h == cmd_firmware_update_activate) {
4648
0
                return CXL_MBOX_MEDIA_DISABLED;
4649
0
            }
4650
0
        }
4651
0
    }
4652
4653
0
    ret = (*h)(cxl_cmd, pl_in, len_in, pl_out, len_out, cci);
4654
0
    if ((cxl_cmd->effect & CXL_MBOX_BACKGROUND_OPERATION) &&
4655
0
        ret == CXL_MBOX_BG_STARTED) {
4656
0
        *bg_started = true;
4657
0
    } else {
4658
0
        *bg_started = false;
4659
0
    }
4660
4661
    /* Set bg and the return code */
4662
0
    if (*bg_started) {
4663
0
        uint64_t now;
4664
4665
0
        cci->bg.opcode = (set << 8) | cmd;
4666
4667
0
        cci->bg.complete_pct = 0;
4668
0
        cci->bg.aborted = false;
4669
0
        cci->bg.ret_code = 0;
4670
4671
0
        now = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
4672
0
        cci->bg.starttime = now;
4673
0
        timer_mod(cci->bg.timer, now + CXL_MBOX_BG_UPDATE_FREQ);
4674
0
    }
4675
4676
0
    return ret;
4677
0
}
4678
4679
static void bg_timercb(void *opaque)
4680
0
{
4681
0
    CXLCCI *cci = opaque;
4682
0
    uint64_t now, total_time;
4683
4684
0
    qemu_mutex_lock(&cci->bg.lock);
4685
4686
0
    now = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
4687
0
    total_time = cci->bg.starttime + cci->bg.runtime;
4688
4689
0
    if (now >= total_time) { /* we are done */
4690
0
        uint16_t ret = CXL_MBOX_SUCCESS;
4691
4692
0
        cci->bg.complete_pct = 100;
4693
0
        cci->bg.ret_code = ret;
4694
0
        switch (cci->bg.opcode) {
4695
0
        case 0x0201: /* fw transfer */
4696
0
            __do_firmware_xfer(cci);
4697
0
            break;
4698
0
        case 0x4400: /* sanitize */
4699
0
        {
4700
0
            CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4701
4702
0
            __do_sanitization(ct3d);
4703
0
            cxl_dev_enable_media(&ct3d->cxl_dstate);
4704
0
        }
4705
0
        break;
4706
0
        case 0x4402: /* Media Operations sanitize */
4707
0
        {
4708
0
            CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4709
0
            __do_sanitize(ct3d);
4710
0
        }
4711
0
        break;
4712
0
        case 0x4304: /* scan media */
4713
0
        {
4714
0
            CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4715
4716
0
            __do_scan_media(ct3d);
4717
0
            break;
4718
0
        }
4719
0
        default:
4720
0
            __builtin_unreachable();
4721
0
            break;
4722
0
        }
4723
0
    } else {
4724
        /* estimate only */
4725
0
        cci->bg.complete_pct =
4726
0
            100 * (now - cci->bg.starttime) / cci->bg.runtime;
4727
0
        timer_mod(cci->bg.timer, now + CXL_MBOX_BG_UPDATE_FREQ);
4728
0
    }
4729
4730
0
    if (cci->bg.complete_pct == 100) {
4731
        /* TODO: generalize to switch CCI */
4732
0
        CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
4733
0
        CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
4734
0
        PCIDevice *pdev = PCI_DEVICE(cci->d);
4735
4736
0
        cci->bg.starttime = 0;
4737
        /* registers are updated, allow new bg-capable cmds */
4738
0
        cci->bg.runtime = 0;
4739
4740
0
        if (msix_enabled(pdev)) {
4741
0
            msix_notify(pdev, cxl_dstate->mbox_msi_n);
4742
0
        } else if (msi_enabled(pdev)) {
4743
0
            msi_notify(pdev, cxl_dstate->mbox_msi_n);
4744
0
        }
4745
0
    }
4746
4747
0
    qemu_mutex_unlock(&cci->bg.lock);
4748
0
}
4749
4750
static void cxl_rebuild_cel(CXLCCI *cci)
4751
0
{
4752
0
    cci->cel_size = 0; /* Reset for a fresh build */
4753
0
    for (int set = 0; set < 256; set++) {
4754
0
        for (int cmd = 0; cmd < 256; cmd++) {
4755
0
            if (cci->cxl_cmd_set[set][cmd].handler) {
4756
0
                const struct cxl_cmd *c = &cci->cxl_cmd_set[set][cmd];
4757
0
                struct cel_log *log =
4758
0
                    &cci->cel_log[cci->cel_size];
4759
4760
0
                log->opcode = (set << 8) | cmd;
4761
0
                log->effect = c->effect;
4762
0
                cci->cel_size++;
4763
0
            }
4764
0
        }
4765
0
    }
4766
0
}
4767
4768
void cxl_init_physical_port_control(CXLPhyPortPerst *perst)
4769
0
{
4770
0
    qemu_mutex_init(&perst->lock);
4771
0
    perst->issued_assert_perst = false;
4772
    /*
4773
     * Assert PERST involves physical port to be in
4774
     * hold reset phase for minimum 100ms. No other
4775
     * physical port control requests are entertained
4776
     * until Deassert PERST command.
4777
     */
4778
0
    perst->asrt_time = ASSERT_WAIT_TIME_MS;
4779
0
}
4780
4781
void cxl_init_cci(CXLCCI *cci, size_t payload_max)
4782
0
{
4783
0
    cci->payload_max = payload_max;
4784
0
    cxl_rebuild_cel(cci);
4785
4786
0
    cci->bg.complete_pct = 0;
4787
0
    cci->bg.starttime = 0;
4788
0
    cci->bg.runtime = 0;
4789
0
    cci->bg.aborted = false;
4790
0
    cci->bg.timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
4791
0
                                 bg_timercb, cci);
4792
0
    qemu_mutex_init(&cci->bg.lock);
4793
4794
0
    memset(&cci->fw, 0, sizeof(cci->fw));
4795
0
    cci->fw.active_slot = 1;
4796
0
    cci->fw.slot[cci->fw.active_slot - 1] = true;
4797
0
    cci->initialized = true;
4798
0
}
4799
4800
void cxl_destroy_cci(CXLCCI *cci)
4801
0
{
4802
0
    qemu_mutex_destroy(&cci->bg.lock);
4803
0
    timer_free(cci->bg.timer);
4804
0
    cci->bg.timer = NULL;
4805
0
    cci->initialized = false;
4806
0
}
4807
4808
static void cxl_copy_cci_commands(CXLCCI *cci, const struct cxl_cmd (*cxl_cmds)[256])
4809
0
{
4810
0
    for (int set = 0; set < 256; set++) {
4811
0
        for (int cmd = 0; cmd < 256; cmd++) {
4812
0
            if (cxl_cmds[set][cmd].handler) {
4813
0
                cci->cxl_cmd_set[set][cmd] = cxl_cmds[set][cmd];
4814
0
            }
4815
0
        }
4816
0
    }
4817
0
}
4818
4819
void cxl_add_cci_commands(CXLCCI *cci, const struct cxl_cmd (*cxl_cmd_set)[256],
4820
                                 size_t payload_max)
4821
0
{
4822
0
    cci->payload_max = MAX(payload_max, cci->payload_max);
4823
0
    cxl_copy_cci_commands(cci, cxl_cmd_set);
4824
0
    cxl_rebuild_cel(cci);
4825
0
}
4826
4827
void cxl_initialize_mailbox_swcci(CXLCCI *cci, DeviceState *intf,
4828
                                  DeviceState *d, size_t payload_max)
4829
0
{
4830
0
    cxl_copy_cci_commands(cci, cxl_cmd_set_sw);
4831
0
    cci->d = d;
4832
0
    cci->intf = intf;
4833
0
    cxl_init_cci(cci, payload_max);
4834
0
}
4835
4836
void cxl_initialize_mailbox_t3(CXLCCI *cci, DeviceState *d, size_t payload_max)
4837
0
{
4838
0
    CXLType3Dev *ct3d = CXL_TYPE3(d);
4839
4840
0
    cxl_copy_cci_commands(cci, cxl_cmd_set);
4841
0
    if (ct3d->dc.num_regions) {
4842
0
        cxl_copy_cci_commands(cci, cxl_cmd_set_dcd);
4843
0
    }
4844
0
    cci->d = d;
4845
4846
    /* No separation for PCI MB as protocol handled in PCI device */
4847
0
    cci->intf = d;
4848
0
    cxl_init_cci(cci, payload_max);
4849
0
}
4850
4851
void cxl_destroy_mailbox_t3(CXLType3Dev *ct3d)
4852
0
{
4853
0
    if (ct3d->cci.initialized) {
4854
0
        cxl_destroy_cci(&ct3d->cci);
4855
0
    }
4856
    /*
4857
     * An in-flight Media Operations sanitize is only advanced by this CCI's
4858
     * background timer; with the CCI gone the operation can never complete,
4859
     * so its state would otherwise be leaked (CXL r4.0 Section 8.2.9.4).
4860
     */
4861
0
    cxl_discard_media_op_sanitize(ct3d);
4862
0
}
4863
4864
static const struct cxl_cmd cxl_cmd_set_t3_ld[256][256] = {
4865
    [INFOSTAT][IS_IDENTIFY] = { "IDENTIFY", cmd_infostat_identify, 0, 0 },
4866
    [LOGS][GET_SUPPORTED] = { "LOGS_GET_SUPPORTED", cmd_logs_get_supported, 0,
4867
                              0 },
4868
    [LOGS][GET_LOG] = { "LOGS_GET_LOG", cmd_logs_get_log, 0x18, 0 },
4869
};
4870
4871
void cxl_initialize_t3_ld_cci(CXLCCI *cci, DeviceState *d, DeviceState *intf,
4872
                               size_t payload_max)
4873
0
{
4874
0
    cxl_copy_cci_commands(cci, cxl_cmd_set_t3_ld);
4875
0
    cci->d = d;
4876
0
    cci->intf = intf;
4877
0
    cxl_init_cci(cci, payload_max);
4878
0
}
4879
4880
static const struct cxl_cmd cxl_cmd_set_t3_fm_owned_ld_mctp[256][256] = {
4881
    [INFOSTAT][IS_IDENTIFY] = { "IDENTIFY", cmd_infostat_identify, 0,  0},
4882
    [INFOSTAT][GET_RESPONSE_MSG_LIMIT] = { "GET_RESPONSE_MSG_LIMIT",
4883
                                           cmd_get_response_msg_limit, 0, 0 },
4884
    [INFOSTAT][SET_RESPONSE_MSG_LIMIT] = { "SET_RESPONSE_MSG_LIMIT",
4885
                                           cmd_set_response_msg_limit, 1, 0 },
4886
    [LOGS][GET_SUPPORTED] = { "LOGS_GET_SUPPORTED", cmd_logs_get_supported, 0,
4887
                              0 },
4888
    [LOGS][GET_LOG] = { "LOGS_GET_LOG", cmd_logs_get_log, 0x18, 0 },
4889
    [TIMESTAMP][GET] = { "TIMESTAMP_GET", cmd_timestamp_get, 0, 0 },
4890
    [TUNNEL][MANAGEMENT_COMMAND] = { "TUNNEL_MANAGEMENT_COMMAND",
4891
                                     cmd_tunnel_management_cmd, ~0, 0 },
4892
};
4893
4894
void cxl_initialize_t3_fm_owned_ld_mctpcci(CXLCCI *cci, DeviceState *d,
4895
                                           DeviceState *intf,
4896
                                           size_t payload_max)
4897
0
{
4898
0
    CXLType3Dev *ct3d = CXL_TYPE3(d);
4899
4900
0
    cxl_copy_cci_commands(cci, cxl_cmd_set_t3_fm_owned_ld_mctp);
4901
0
    if (ct3d->dc.num_regions) {
4902
0
        cxl_copy_cci_commands(cci, cxl_cmd_set_fm_dcd);
4903
0
    }
4904
0
    cci->d = d;
4905
0
    cci->intf = intf;
4906
0
    cxl_init_cci(cci, payload_max);
4907
0
}