Statistics
| Branch: | Revision:

root / hw / s390x / virtio-ccw.c @ 0434e30a

History | View | Annotate | Download (30.1 kB)

1
/*
2
 * virtio ccw target implementation
3
 *
4
 * Copyright 2012 IBM Corp.
5
 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6
 *
7
 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8
 * your option) any later version. See the COPYING file in the top-level
9
 * directory.
10
 */
11

    
12
#include "hw/hw.h"
13
#include "block/block.h"
14
#include "sysemu/blockdev.h"
15
#include "sysemu/sysemu.h"
16
#include "net/net.h"
17
#include "monitor/monitor.h"
18
#include "hw/virtio/virtio.h"
19
#include "hw/virtio/virtio-serial.h"
20
#include "hw/virtio/virtio-net.h"
21
#include "hw/sysbus.h"
22
#include "qemu/bitops.h"
23
#include "hw/virtio/virtio-bus.h"
24

    
25
#include "ioinst.h"
26
#include "css.h"
27
#include "virtio-ccw.h"
28
#include "trace.h"
29

    
30
static int virtual_css_bus_reset(BusState *qbus)
31
{
32
    /* This should actually be modelled via the generic css */
33
    css_reset();
34

    
35
    /* we dont traverse ourself, return 0 */
36
    return 0;
37
}
38

    
39

    
40
static void virtual_css_bus_class_init(ObjectClass *klass, void *data)
41
{
42
    BusClass *k = BUS_CLASS(klass);
43

    
44
    k->reset = virtual_css_bus_reset;
45
}
46

    
47
static const TypeInfo virtual_css_bus_info = {
48
    .name = TYPE_VIRTUAL_CSS_BUS,
49
    .parent = TYPE_BUS,
50
    .instance_size = sizeof(VirtualCssBus),
51
    .class_init = virtual_css_bus_class_init,
52
};
53

    
54
static const VirtIOBindings virtio_ccw_bindings;
55

    
56
VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)
57
{
58
    VirtIODevice *vdev = NULL;
59

    
60
    if (sch->driver_data) {
61
        vdev = ((VirtioCcwDevice *)sch->driver_data)->vdev;
62
    }
63
    return vdev;
64
}
65

    
66
VirtualCssBus *virtual_css_bus_init(void)
67
{
68
    VirtualCssBus *cbus;
69
    BusState *bus;
70
    DeviceState *dev;
71

    
72
    /* Create bridge device */
73
    dev = qdev_create(NULL, "virtual-css-bridge");
74
    qdev_init_nofail(dev);
75

    
76
    /* Create bus on bridge device */
77
    bus = qbus_create(TYPE_VIRTUAL_CSS_BUS, dev, "virtual-css");
78
    cbus = VIRTUAL_CSS_BUS(bus);
79

    
80
    /* Enable hotplugging */
81
    bus->allow_hotplug = 1;
82

    
83
    return cbus;
84
}
85

    
86
/* Communication blocks used by several channel commands. */
87
typedef struct VqInfoBlock {
88
    uint64_t queue;
89
    uint32_t align;
90
    uint16_t index;
91
    uint16_t num;
92
} QEMU_PACKED VqInfoBlock;
93

    
94
typedef struct VqConfigBlock {
95
    uint16_t index;
96
    uint16_t num_max;
97
} QEMU_PACKED VqConfigBlock;
98

    
99
typedef struct VirtioFeatDesc {
100
    uint32_t features;
101
    uint8_t index;
102
} QEMU_PACKED VirtioFeatDesc;
103

    
104
/* Specify where the virtqueues for the subchannel are in guest memory. */
105
static int virtio_ccw_set_vqs(SubchDev *sch, uint64_t addr, uint32_t align,
106
                              uint16_t index, uint16_t num)
107
{
108
    VirtioCcwDevice *dev = sch->driver_data;
109

    
110
    if (index > VIRTIO_PCI_QUEUE_MAX) {
111
        return -EINVAL;
112
    }
113

    
114
    /* Current code in virtio.c relies on 4K alignment. */
115
    if (addr && (align != 4096)) {
116
        return -EINVAL;
117
    }
118

    
119
    if (!dev) {
120
        return -EINVAL;
121
    }
122

    
123
    virtio_queue_set_addr(dev->vdev, index, addr);
124
    if (!addr) {
125
        virtio_queue_set_vector(dev->vdev, index, 0);
126
    } else {
127
        /* Fail if we don't have a big enough queue. */
128
        /* TODO: Add interface to handle vring.num changing */
129
        if (virtio_queue_get_num(dev->vdev, index) > num) {
130
            return -EINVAL;
131
        }
132
        virtio_queue_set_vector(dev->vdev, index, index);
133
    }
134
    /* tell notify handler in case of config change */
135
    dev->vdev->config_vector = VIRTIO_PCI_QUEUE_MAX;
136
    return 0;
137
}
138

    
139
static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
140
{
141
    int ret;
142
    VqInfoBlock info;
143
    uint8_t status;
144
    VirtioFeatDesc features;
145
    void *config;
146
    hwaddr indicators;
147
    VqConfigBlock vq_config;
148
    VirtioCcwDevice *dev = sch->driver_data;
149
    bool check_len;
150
    int len;
151
    hwaddr hw_len;
152

    
153
    if (!dev) {
154
        return -EINVAL;
155
    }
156

    
157
    trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid,
158
                                   ccw.cmd_code);
159
    check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
160

    
161
    /* Look at the command. */
162
    switch (ccw.cmd_code) {
163
    case CCW_CMD_SET_VQ:
164
        if (check_len) {
165
            if (ccw.count != sizeof(info)) {
166
                ret = -EINVAL;
167
                break;
168
            }
169
        } else if (ccw.count < sizeof(info)) {
170
            /* Can't execute command. */
171
            ret = -EINVAL;
172
            break;
173
        }
174
        if (!ccw.cda) {
175
            ret = -EFAULT;
176
        } else {
177
            info.queue = ldq_phys(ccw.cda);
178
            info.align = ldl_phys(ccw.cda + sizeof(info.queue));
179
            info.index = lduw_phys(ccw.cda + sizeof(info.queue)
180
                                   + sizeof(info.align));
181
            info.num = lduw_phys(ccw.cda + sizeof(info.queue)
182
                                 + sizeof(info.align)
183
                                 + sizeof(info.index));
184
            ret = virtio_ccw_set_vqs(sch, info.queue, info.align, info.index,
185
                                     info.num);
186
            sch->curr_status.scsw.count = 0;
187
        }
188
        break;
189
    case CCW_CMD_VDEV_RESET:
190
        virtio_reset(dev->vdev);
191
        ret = 0;
192
        break;
193
    case CCW_CMD_READ_FEAT:
194
        if (check_len) {
195
            if (ccw.count != sizeof(features)) {
196
                ret = -EINVAL;
197
                break;
198
            }
199
        } else if (ccw.count < sizeof(features)) {
200
            /* Can't execute command. */
201
            ret = -EINVAL;
202
            break;
203
        }
204
        if (!ccw.cda) {
205
            ret = -EFAULT;
206
        } else {
207
            features.index = ldub_phys(ccw.cda + sizeof(features.features));
208
            if (features.index < ARRAY_SIZE(dev->host_features)) {
209
                features.features = dev->host_features[features.index];
210
            } else {
211
                /* Return zeroes if the guest supports more feature bits. */
212
                features.features = 0;
213
            }
214
            stl_le_phys(ccw.cda, features.features);
215
            sch->curr_status.scsw.count = ccw.count - sizeof(features);
216
            ret = 0;
217
        }
218
        break;
219
    case CCW_CMD_WRITE_FEAT:
220
        if (check_len) {
221
            if (ccw.count != sizeof(features)) {
222
                ret = -EINVAL;
223
                break;
224
            }
225
        } else if (ccw.count < sizeof(features)) {
226
            /* Can't execute command. */
227
            ret = -EINVAL;
228
            break;
229
        }
230
        if (!ccw.cda) {
231
            ret = -EFAULT;
232
        } else {
233
            features.index = ldub_phys(ccw.cda + sizeof(features.features));
234
            features.features = ldl_le_phys(ccw.cda);
235
            if (features.index < ARRAY_SIZE(dev->host_features)) {
236
                if (dev->vdev->set_features) {
237
                    dev->vdev->set_features(dev->vdev, features.features);
238
                }
239
                dev->vdev->guest_features = features.features;
240
            } else {
241
                /*
242
                 * If the guest supports more feature bits, assert that it
243
                 * passes us zeroes for those we don't support.
244
                 */
245
                if (features.features) {
246
                    fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n",
247
                            features.index, features.features);
248
                    /* XXX: do a unit check here? */
249
                }
250
            }
251
            sch->curr_status.scsw.count = ccw.count - sizeof(features);
252
            ret = 0;
253
        }
254
        break;
255
    case CCW_CMD_READ_CONF:
256
        if (check_len) {
257
            if (ccw.count > dev->vdev->config_len) {
258
                ret = -EINVAL;
259
                break;
260
            }
261
        }
262
        len = MIN(ccw.count, dev->vdev->config_len);
263
        if (!ccw.cda) {
264
            ret = -EFAULT;
265
        } else {
266
            dev->vdev->get_config(dev->vdev, dev->vdev->config);
267
            /* XXX config space endianness */
268
            cpu_physical_memory_write(ccw.cda, dev->vdev->config, len);
269
            sch->curr_status.scsw.count = ccw.count - len;
270
            ret = 0;
271
        }
272
        break;
273
    case CCW_CMD_WRITE_CONF:
274
        if (check_len) {
275
            if (ccw.count > dev->vdev->config_len) {
276
                ret = -EINVAL;
277
                break;
278
            }
279
        }
280
        len = MIN(ccw.count, dev->vdev->config_len);
281
        hw_len = len;
282
        if (!ccw.cda) {
283
            ret = -EFAULT;
284
        } else {
285
            config = cpu_physical_memory_map(ccw.cda, &hw_len, 0);
286
            if (!config) {
287
                ret = -EFAULT;
288
            } else {
289
                len = hw_len;
290
                /* XXX config space endianness */
291
                memcpy(dev->vdev->config, config, len);
292
                cpu_physical_memory_unmap(config, hw_len, 0, hw_len);
293
                if (dev->vdev->set_config) {
294
                    dev->vdev->set_config(dev->vdev, dev->vdev->config);
295
                }
296
                sch->curr_status.scsw.count = ccw.count - len;
297
                ret = 0;
298
            }
299
        }
300
        break;
301
    case CCW_CMD_WRITE_STATUS:
302
        if (check_len) {
303
            if (ccw.count != sizeof(status)) {
304
                ret = -EINVAL;
305
                break;
306
            }
307
        } else if (ccw.count < sizeof(status)) {
308
            /* Can't execute command. */
309
            ret = -EINVAL;
310
            break;
311
        }
312
        if (!ccw.cda) {
313
            ret = -EFAULT;
314
        } else {
315
            status = ldub_phys(ccw.cda);
316
            virtio_set_status(dev->vdev, status);
317
            if (dev->vdev->status == 0) {
318
                virtio_reset(dev->vdev);
319
            }
320
            sch->curr_status.scsw.count = ccw.count - sizeof(status);
321
            ret = 0;
322
        }
323
        break;
324
    case CCW_CMD_SET_IND:
325
        if (check_len) {
326
            if (ccw.count != sizeof(indicators)) {
327
                ret = -EINVAL;
328
                break;
329
            }
330
        } else if (ccw.count < sizeof(indicators)) {
331
            /* Can't execute command. */
332
            ret = -EINVAL;
333
            break;
334
        }
335
        indicators = ldq_phys(ccw.cda);
336
        if (!indicators) {
337
            ret = -EFAULT;
338
        } else {
339
            dev->indicators = indicators;
340
            sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
341
            ret = 0;
342
        }
343
        break;
344
    case CCW_CMD_SET_CONF_IND:
345
        if (check_len) {
346
            if (ccw.count != sizeof(indicators)) {
347
                ret = -EINVAL;
348
                break;
349
            }
350
        } else if (ccw.count < sizeof(indicators)) {
351
            /* Can't execute command. */
352
            ret = -EINVAL;
353
            break;
354
        }
355
        indicators = ldq_phys(ccw.cda);
356
        if (!indicators) {
357
            ret = -EFAULT;
358
        } else {
359
            dev->indicators2 = indicators;
360
            sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
361
            ret = 0;
362
        }
363
        break;
364
    case CCW_CMD_READ_VQ_CONF:
365
        if (check_len) {
366
            if (ccw.count != sizeof(vq_config)) {
367
                ret = -EINVAL;
368
                break;
369
            }
370
        } else if (ccw.count < sizeof(vq_config)) {
371
            /* Can't execute command. */
372
            ret = -EINVAL;
373
            break;
374
        }
375
        if (!ccw.cda) {
376
            ret = -EFAULT;
377
        } else {
378
            vq_config.index = lduw_phys(ccw.cda);
379
            vq_config.num_max = virtio_queue_get_num(dev->vdev,
380
                                                     vq_config.index);
381
            stw_phys(ccw.cda + sizeof(vq_config.index), vq_config.num_max);
382
            sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
383
            ret = 0;
384
        }
385
        break;
386
    default:
387
        ret = -ENOSYS;
388
        break;
389
    }
390
    return ret;
391
}
392

    
393
static int virtio_ccw_device_init(VirtioCcwDevice *dev, VirtIODevice *vdev)
394
{
395
    unsigned int cssid = 0;
396
    unsigned int ssid = 0;
397
    unsigned int schid;
398
    unsigned int devno;
399
    bool have_devno = false;
400
    bool found = false;
401
    SubchDev *sch;
402
    int ret;
403
    int num;
404
    DeviceState *parent = DEVICE(dev);
405

    
406
    sch = g_malloc0(sizeof(SubchDev));
407

    
408
    sch->driver_data = dev;
409
    dev->sch = sch;
410

    
411
    dev->vdev = vdev;
412
    dev->indicators = 0;
413

    
414
    /* Initialize subchannel structure. */
415
    sch->channel_prog = 0x0;
416
    sch->last_cmd_valid = false;
417
    sch->orb = NULL;
418
    /*
419
     * Use a device number if provided. Otherwise, fall back to subchannel
420
     * number.
421
     */
422
    if (dev->bus_id) {
423
        num = sscanf(dev->bus_id, "%x.%x.%04x", &cssid, &ssid, &devno);
424
        if (num == 3) {
425
            if ((cssid > MAX_CSSID) || (ssid > MAX_SSID)) {
426
                ret = -EINVAL;
427
                error_report("Invalid cssid or ssid: cssid %x, ssid %x",
428
                             cssid, ssid);
429
                goto out_err;
430
            }
431
            /* Enforce use of virtual cssid. */
432
            if (cssid != VIRTUAL_CSSID) {
433
                ret = -EINVAL;
434
                error_report("cssid %x not valid for virtio devices", cssid);
435
                goto out_err;
436
            }
437
            if (css_devno_used(cssid, ssid, devno)) {
438
                ret = -EEXIST;
439
                error_report("Device %x.%x.%04x already exists", cssid, ssid,
440
                             devno);
441
                goto out_err;
442
            }
443
            sch->cssid = cssid;
444
            sch->ssid = ssid;
445
            sch->devno = devno;
446
            have_devno = true;
447
        } else {
448
            ret = -EINVAL;
449
            error_report("Malformed devno parameter '%s'", dev->bus_id);
450
            goto out_err;
451
        }
452
    }
453

    
454
    /* Find the next free id. */
455
    if (have_devno) {
456
        for (schid = 0; schid <= MAX_SCHID; schid++) {
457
            if (!css_find_subch(1, cssid, ssid, schid)) {
458
                sch->schid = schid;
459
                css_subch_assign(cssid, ssid, schid, devno, sch);
460
                found = true;
461
                break;
462
            }
463
        }
464
        if (!found) {
465
            ret = -ENODEV;
466
            error_report("No free subchannel found for %x.%x.%04x", cssid, ssid,
467
                         devno);
468
            goto out_err;
469
        }
470
        trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
471
                                    "user-configured");
472
    } else {
473
        cssid = VIRTUAL_CSSID;
474
        for (ssid = 0; ssid <= MAX_SSID; ssid++) {
475
            for (schid = 0; schid <= MAX_SCHID; schid++) {
476
                if (!css_find_subch(1, cssid, ssid, schid)) {
477
                    sch->cssid = cssid;
478
                    sch->ssid = ssid;
479
                    sch->schid = schid;
480
                    devno = schid;
481
                    /*
482
                     * If the devno is already taken, look further in this
483
                     * subchannel set.
484
                     */
485
                    while (css_devno_used(cssid, ssid, devno)) {
486
                        if (devno == MAX_SCHID) {
487
                            devno = 0;
488
                        } else if (devno == schid - 1) {
489
                            ret = -ENODEV;
490
                            error_report("No free devno found");
491
                            goto out_err;
492
                        } else {
493
                            devno++;
494
                        }
495
                    }
496
                    sch->devno = devno;
497
                    css_subch_assign(cssid, ssid, schid, devno, sch);
498
                    found = true;
499
                    break;
500
                }
501
            }
502
            if (found) {
503
                break;
504
            }
505
        }
506
        if (!found) {
507
            ret = -ENODEV;
508
            error_report("Virtual channel subsystem is full!");
509
            goto out_err;
510
        }
511
        trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
512
                                    "auto-configured");
513
    }
514

    
515
    /* Build initial schib. */
516
    css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
517

    
518
    sch->ccw_cb = virtio_ccw_cb;
519

    
520
    /* Build senseid data. */
521
    memset(&sch->id, 0, sizeof(SenseId));
522
    sch->id.reserved = 0xff;
523
    sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
524
    sch->id.cu_model = dev->vdev->device_id;
525

    
526
    virtio_bind_device(vdev, &virtio_ccw_bindings, DEVICE(dev));
527
    /* Only the first 32 feature bits are used. */
528
    dev->host_features[0] = vdev->get_features(vdev, dev->host_features[0]);
529
    dev->host_features[0] |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
530
    dev->host_features[0] |= 0x1 << VIRTIO_F_BAD_FEATURE;
531

    
532
    css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
533
                          parent->hotplugged, 1);
534
    return 0;
535

    
536
out_err:
537
    dev->sch = NULL;
538
    g_free(sch);
539
    return ret;
540
}
541

    
542
static int virtio_ccw_exit(VirtioCcwDevice *dev)
543
{
544
    SubchDev *sch = dev->sch;
545

    
546
    if (sch) {
547
        css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
548
        g_free(sch);
549
    }
550
    dev->indicators = 0;
551
    return 0;
552
}
553

    
554
static int virtio_ccw_net_init(VirtioCcwDevice *dev)
555
{
556
    VirtIODevice *vdev;
557

    
558
    vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net,
559
                           dev->host_features[0]);
560
    if (!vdev) {
561
        return -1;
562
    }
563

    
564
    return virtio_ccw_device_init(dev, vdev);
565
}
566

    
567
static int virtio_ccw_net_exit(VirtioCcwDevice *dev)
568
{
569
    virtio_net_exit(dev->vdev);
570
    return virtio_ccw_exit(dev);
571
}
572

    
573
static int virtio_ccw_blk_init(VirtioCcwDevice *ccw_dev)
574
{
575
    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
576
    DeviceState *vdev = DEVICE(&dev->vdev);
577
    virtio_blk_set_conf(vdev, &(dev->blk));
578
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
579
    if (qdev_init(vdev) < 0) {
580
        return -1;
581
    }
582

    
583
    return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
584
}
585

    
586
static void virtio_ccw_blk_instance_init(Object *obj)
587
{
588
    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
589
    object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BLK);
590
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
591
}
592

    
593
static int virtio_ccw_serial_init(VirtioCcwDevice *dev)
594
{
595
    VirtIODevice *vdev;
596

    
597
    vdev = virtio_serial_init((DeviceState *)dev, &dev->serial);
598
    if (!vdev) {
599
        return -1;
600
    }
601

    
602
    return virtio_ccw_device_init(dev, vdev);
603
}
604

    
605
static int virtio_ccw_serial_exit(VirtioCcwDevice *dev)
606
{
607
    virtio_serial_exit(dev->vdev);
608
    return virtio_ccw_exit(dev);
609
}
610

    
611
static int virtio_ccw_balloon_init(VirtioCcwDevice *ccw_dev)
612
{
613
    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
614
    DeviceState *vdev = DEVICE(&dev->vdev);
615

    
616
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
617
    if (qdev_init(vdev) < 0) {
618
        return -1;
619
    }
620

    
621
    return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
622
}
623

    
624
static void virtio_ccw_balloon_instance_init(Object *obj)
625
{
626
    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
627
    object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BALLOON);
628
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
629
}
630

    
631
static int virtio_ccw_scsi_init(VirtioCcwDevice *ccw_dev)
632
{
633
    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
634
    DeviceState *vdev = DEVICE(&dev->vdev);
635

    
636
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
637
    if (qdev_init(vdev) < 0) {
638
        return -1;
639
    }
640

    
641
    return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
642
}
643

    
644
static void virtio_ccw_scsi_instance_init(Object *obj)
645
{
646
    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
647
    object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SCSI);
648
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
649
}
650

    
651
static int virtio_ccw_rng_init(VirtioCcwDevice *dev)
652
{
653
    VirtIODevice *vdev;
654

    
655
    if (dev->rng.rng == NULL) {
656
        dev->rng.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
657
        object_property_add_child(OBJECT(dev), "default-backend",
658
                                  OBJECT(dev->rng.default_backend), NULL);
659
        object_property_set_link(OBJECT(dev), OBJECT(dev->rng.default_backend),
660
                                 "rng", NULL);
661
    }
662
    vdev = virtio_rng_init((DeviceState *)dev, &dev->rng);
663
    if (!vdev) {
664
        return -1;
665
    }
666
    return virtio_ccw_device_init(dev, vdev);
667
}
668

    
669
static int virtio_ccw_rng_exit(VirtioCcwDevice *dev)
670
{
671
    virtio_rng_exit(dev->vdev);
672
    return virtio_ccw_exit(dev);
673
}
674

    
675
/* DeviceState to VirtioCcwDevice. Note: used on datapath,
676
 * be careful and test performance if you change this.
677
 */
678
static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
679
{
680
    return container_of(d, VirtioCcwDevice, parent_obj);
681
}
682

    
683
static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
684
{
685
    VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
686
    SubchDev *sch = dev->sch;
687
    uint64_t indicators;
688

    
689
    if (vector >= 128) {
690
        return;
691
    }
692

    
693
    if (vector < VIRTIO_PCI_QUEUE_MAX) {
694
        indicators = ldq_phys(dev->indicators);
695
        indicators |= 1ULL << vector;
696
        stq_phys(dev->indicators, indicators);
697
    } else {
698
        vector = 0;
699
        indicators = ldq_phys(dev->indicators2);
700
        indicators |= 1ULL << vector;
701
        stq_phys(dev->indicators2, indicators);
702
    }
703

    
704
    css_conditional_io_interrupt(sch);
705

    
706
}
707

    
708
static unsigned virtio_ccw_get_features(DeviceState *d)
709
{
710
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
711

    
712
    /* Only the first 32 feature bits are used. */
713
    return dev->host_features[0];
714
}
715

    
716
static void virtio_ccw_reset(DeviceState *d)
717
{
718
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
719

    
720
    virtio_reset(dev->vdev);
721
    css_reset_sch(dev->sch);
722
}
723

    
724
/**************** Virtio-ccw Bus Device Descriptions *******************/
725

    
726
static const VirtIOBindings virtio_ccw_bindings = {
727
    .notify = virtio_ccw_notify,
728
    .get_features = virtio_ccw_get_features,
729
};
730

    
731
static Property virtio_ccw_net_properties[] = {
732
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
733
    DEFINE_VIRTIO_NET_FEATURES(VirtioCcwDevice, host_features[0]),
734
    DEFINE_NIC_PROPERTIES(VirtioCcwDevice, nic),
735
    DEFINE_PROP_UINT32("x-txtimer", VirtioCcwDevice,
736
                       net.txtimer, TX_TIMER_INTERVAL),
737
    DEFINE_PROP_INT32("x-txburst", VirtioCcwDevice,
738
                      net.txburst, TX_BURST),
739
    DEFINE_PROP_STRING("tx", VirtioCcwDevice, net.tx),
740
    DEFINE_PROP_END_OF_LIST(),
741
};
742

    
743
static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
744
{
745
    DeviceClass *dc = DEVICE_CLASS(klass);
746
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
747

    
748
    k->init = virtio_ccw_net_init;
749
    k->exit = virtio_ccw_net_exit;
750
    dc->reset = virtio_ccw_reset;
751
    dc->props = virtio_ccw_net_properties;
752
}
753

    
754
static const TypeInfo virtio_ccw_net = {
755
    .name          = "virtio-net-ccw",
756
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
757
    .instance_size = sizeof(VirtioCcwDevice),
758
    .class_init    = virtio_ccw_net_class_init,
759
};
760

    
761
static Property virtio_ccw_blk_properties[] = {
762
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
763
    DEFINE_VIRTIO_BLK_FEATURES(VirtioCcwDevice, host_features[0]),
764
    DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkCcw, blk),
765
    DEFINE_PROP_END_OF_LIST(),
766
};
767

    
768
static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
769
{
770
    DeviceClass *dc = DEVICE_CLASS(klass);
771
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
772

    
773
    k->init = virtio_ccw_blk_init;
774
    k->exit = virtio_ccw_exit;
775
    dc->reset = virtio_ccw_reset;
776
    dc->props = virtio_ccw_blk_properties;
777
}
778

    
779
static const TypeInfo virtio_ccw_blk = {
780
    .name          = TYPE_VIRTIO_BLK_CCW,
781
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
782
    .instance_size = sizeof(VirtIOBlkCcw),
783
    .instance_init = virtio_ccw_blk_instance_init,
784
    .class_init    = virtio_ccw_blk_class_init,
785
};
786

    
787
static Property virtio_ccw_serial_properties[] = {
788
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
789
    DEFINE_PROP_UINT32("max_ports", VirtioCcwDevice,
790
                       serial.max_virtserial_ports, 31),
791
    DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
792
    DEFINE_PROP_END_OF_LIST(),
793
};
794

    
795
static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
796
{
797
    DeviceClass *dc = DEVICE_CLASS(klass);
798
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
799

    
800
    k->init = virtio_ccw_serial_init;
801
    k->exit = virtio_ccw_serial_exit;
802
    dc->reset = virtio_ccw_reset;
803
    dc->props = virtio_ccw_serial_properties;
804
}
805

    
806
static const TypeInfo virtio_ccw_serial = {
807
    .name          = "virtio-serial-ccw",
808
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
809
    .instance_size = sizeof(VirtioCcwDevice),
810
    .class_init    = virtio_ccw_serial_class_init,
811
};
812

    
813
static Property virtio_ccw_balloon_properties[] = {
814
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
815
    DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
816
    DEFINE_PROP_END_OF_LIST(),
817
};
818

    
819
static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
820
{
821
    DeviceClass *dc = DEVICE_CLASS(klass);
822
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
823

    
824
    k->init = virtio_ccw_balloon_init;
825
    k->exit = virtio_ccw_exit;
826
    dc->reset = virtio_ccw_reset;
827
    dc->props = virtio_ccw_balloon_properties;
828
}
829

    
830
static const TypeInfo virtio_ccw_balloon = {
831
    .name          = TYPE_VIRTIO_BALLOON_CCW,
832
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
833
    .instance_size = sizeof(VirtIOBalloonCcw),
834
    .instance_init = virtio_ccw_balloon_instance_init,
835
    .class_init    = virtio_ccw_balloon_class_init,
836
};
837

    
838
static Property virtio_ccw_scsi_properties[] = {
839
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
840
    DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSICcw, vdev.conf),
841
    DEFINE_VIRTIO_SCSI_FEATURES(VirtioCcwDevice, host_features[0]),
842
    DEFINE_PROP_END_OF_LIST(),
843
};
844

    
845
static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
846
{
847
    DeviceClass *dc = DEVICE_CLASS(klass);
848
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
849

    
850
    k->init = virtio_ccw_scsi_init;
851
    k->exit = virtio_ccw_exit;
852
    dc->reset = virtio_ccw_reset;
853
    dc->props = virtio_ccw_scsi_properties;
854
}
855

    
856
static const TypeInfo virtio_ccw_scsi = {
857
    .name          = TYPE_VIRTIO_SCSI_CCW,
858
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
859
    .instance_size = sizeof(VirtIOSCSICcw),
860
    .instance_init = virtio_ccw_scsi_instance_init,
861
    .class_init    = virtio_ccw_scsi_class_init,
862
};
863

    
864
static void virtio_ccw_rng_initfn(Object *obj)
865
{
866
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(obj);
867

    
868
    object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
869
                             (Object **)&dev->rng.rng, NULL);
870
}
871

    
872
static Property virtio_ccw_rng_properties[] = {
873
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
874
    DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
875
    DEFINE_PROP_UINT64("max-bytes", VirtioCcwDevice, rng.max_bytes, INT64_MAX),
876
    DEFINE_PROP_UINT32("period", VirtioCcwDevice, rng.period_ms, 1 << 16),
877
    DEFINE_PROP_END_OF_LIST(),
878
};
879

    
880
static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
881
{
882
    DeviceClass *dc = DEVICE_CLASS(klass);
883
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
884

    
885
    k->init = virtio_ccw_rng_init;
886
    k->exit = virtio_ccw_rng_exit;
887
    dc->reset = virtio_ccw_reset;
888
    dc->props = virtio_ccw_rng_properties;
889
}
890

    
891
static const TypeInfo virtio_ccw_rng = {
892
    .name          = "virtio-rng-ccw",
893
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
894
    .instance_size = sizeof(VirtioCcwDevice),
895
    .instance_init = virtio_ccw_rng_initfn,
896
    .class_init    = virtio_ccw_rng_class_init,
897
};
898

    
899
static int virtio_ccw_busdev_init(DeviceState *dev)
900
{
901
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
902
    VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
903

    
904
    virtio_ccw_bus_new(&_dev->bus, _dev);
905

    
906
    return _info->init(_dev);
907
}
908

    
909
static int virtio_ccw_busdev_exit(DeviceState *dev)
910
{
911
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
912
    VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
913

    
914
    return _info->exit(_dev);
915
}
916

    
917
static int virtio_ccw_busdev_unplug(DeviceState *dev)
918
{
919
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
920
    SubchDev *sch = _dev->sch;
921

    
922
    /*
923
     * We should arrive here only for device_del, since we don't support
924
     * direct hot(un)plug of channels, but only through virtio.
925
     */
926
    assert(sch != NULL);
927
    /* Subchannel is now disabled and no longer valid. */
928
    sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
929
                                     PMCW_FLAGS_MASK_DNV);
930

    
931
    css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
932

    
933
    qdev_free(dev);
934
    return 0;
935
}
936

    
937
static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
938
{
939
    DeviceClass *dc = DEVICE_CLASS(klass);
940

    
941
    dc->init = virtio_ccw_busdev_init;
942
    dc->exit = virtio_ccw_busdev_exit;
943
    dc->unplug = virtio_ccw_busdev_unplug;
944
    dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
945

    
946
}
947

    
948
static const TypeInfo virtio_ccw_device_info = {
949
    .name = TYPE_VIRTIO_CCW_DEVICE,
950
    .parent = TYPE_DEVICE,
951
    .instance_size = sizeof(VirtioCcwDevice),
952
    .class_init = virtio_ccw_device_class_init,
953
    .class_size = sizeof(VirtIOCCWDeviceClass),
954
    .abstract = true,
955
};
956

    
957
/***************** Virtual-css Bus Bridge Device ********************/
958
/* Only required to have the virtio bus as child in the system bus */
959

    
960
static int virtual_css_bridge_init(SysBusDevice *dev)
961
{
962
    /* nothing */
963
    return 0;
964
}
965

    
966
static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
967
{
968
    DeviceClass *dc = DEVICE_CLASS(klass);
969
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
970

    
971
    k->init = virtual_css_bridge_init;
972
    dc->no_user = 1;
973
}
974

    
975
static const TypeInfo virtual_css_bridge_info = {
976
    .name          = "virtual-css-bridge",
977
    .parent        = TYPE_SYS_BUS_DEVICE,
978
    .instance_size = sizeof(SysBusDevice),
979
    .class_init    = virtual_css_bridge_class_init,
980
};
981

    
982
/* virtio-ccw-bus */
983

    
984
void virtio_ccw_bus_new(VirtioBusState *bus, VirtioCcwDevice *dev)
985
{
986
    DeviceState *qdev = DEVICE(dev);
987
    BusState *qbus;
988

    
989
    qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_CCW_BUS, qdev, NULL);
990
    qbus = BUS(bus);
991
    qbus->allow_hotplug = 1;
992
}
993

    
994
static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
995
{
996
    VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
997
    BusClass *bus_class = BUS_CLASS(klass);
998

    
999
    bus_class->max_dev = 1;
1000
    k->notify = virtio_ccw_notify;
1001
    k->get_features = virtio_ccw_get_features;
1002
}
1003

    
1004
static const TypeInfo virtio_ccw_bus_info = {
1005
    .name = TYPE_VIRTIO_CCW_BUS,
1006
    .parent = TYPE_VIRTIO_BUS,
1007
    .instance_size = sizeof(VirtioCcwBusState),
1008
    .class_init = virtio_ccw_bus_class_init,
1009
};
1010

    
1011
static void virtio_ccw_register(void)
1012
{
1013
    type_register_static(&virtio_ccw_bus_info);
1014
    type_register_static(&virtual_css_bus_info);
1015
    type_register_static(&virtio_ccw_device_info);
1016
    type_register_static(&virtio_ccw_serial);
1017
    type_register_static(&virtio_ccw_blk);
1018
    type_register_static(&virtio_ccw_net);
1019
    type_register_static(&virtio_ccw_balloon);
1020
    type_register_static(&virtio_ccw_scsi);
1021
    type_register_static(&virtio_ccw_rng);
1022
    type_register_static(&virtual_css_bridge_info);
1023
}
1024

    
1025
type_init(virtio_ccw_register)