]>
Commit | Line | Data |
---|---|---|
a5cf2bb4 CH |
1 | /* |
2 | * virtio ccw target implementation | |
3 | * | |
4 | * Copyright 2012 IBM Corp. | |
5 | * Author(s): Cornelia Huck <[email protected]> | |
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.h" | |
19 | #include "hw/virtio-serial.h" | |
20 | #include "hw/virtio-net.h" | |
21 | #include "hw/sysbus.h" | |
22 | #include "qemu/bitops.h" | |
23 | #include "hw/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 = -EOPNOTSUPP; | |
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 | if (!vdev) { | |
560 | return -1; | |
561 | } | |
562 | ||
563 | return virtio_ccw_device_init(dev, vdev); | |
564 | } | |
565 | ||
566 | static int virtio_ccw_net_exit(VirtioCcwDevice *dev) | |
567 | { | |
568 | virtio_net_exit(dev->vdev); | |
569 | return virtio_ccw_exit(dev); | |
570 | } | |
571 | ||
572 | static int virtio_ccw_blk_init(VirtioCcwDevice *dev) | |
573 | { | |
574 | VirtIODevice *vdev; | |
575 | ||
576 | vdev = virtio_blk_init((DeviceState *)dev, &dev->blk); | |
577 | if (!vdev) { | |
578 | return -1; | |
579 | } | |
580 | ||
581 | return virtio_ccw_device_init(dev, vdev); | |
582 | } | |
583 | ||
584 | static int virtio_ccw_blk_exit(VirtioCcwDevice *dev) | |
585 | { | |
586 | virtio_blk_exit(dev->vdev); | |
587 | blockdev_mark_auto_del(dev->blk.conf.bs); | |
588 | return virtio_ccw_exit(dev); | |
589 | } | |
590 | ||
591 | static int virtio_ccw_serial_init(VirtioCcwDevice *dev) | |
592 | { | |
593 | VirtIODevice *vdev; | |
594 | ||
595 | vdev = virtio_serial_init((DeviceState *)dev, &dev->serial); | |
596 | if (!vdev) { | |
597 | return -1; | |
598 | } | |
599 | ||
600 | return virtio_ccw_device_init(dev, vdev); | |
601 | } | |
602 | ||
603 | static int virtio_ccw_serial_exit(VirtioCcwDevice *dev) | |
604 | { | |
605 | virtio_serial_exit(dev->vdev); | |
606 | return virtio_ccw_exit(dev); | |
607 | } | |
608 | ||
609 | static int virtio_ccw_balloon_init(VirtioCcwDevice *dev) | |
610 | { | |
611 | VirtIODevice *vdev; | |
612 | ||
613 | vdev = virtio_balloon_init((DeviceState *)dev); | |
614 | if (!vdev) { | |
615 | return -1; | |
616 | } | |
617 | ||
618 | return virtio_ccw_device_init(dev, vdev); | |
619 | } | |
620 | ||
621 | static int virtio_ccw_balloon_exit(VirtioCcwDevice *dev) | |
622 | { | |
623 | virtio_balloon_exit(dev->vdev); | |
624 | return virtio_ccw_exit(dev); | |
625 | } | |
626 | ||
627 | static int virtio_ccw_scsi_init(VirtioCcwDevice *dev) | |
628 | { | |
629 | VirtIODevice *vdev; | |
630 | ||
631 | vdev = virtio_scsi_init((DeviceState *)dev, &dev->scsi); | |
632 | if (!vdev) { | |
633 | return -1; | |
634 | } | |
635 | ||
636 | return virtio_ccw_device_init(dev, vdev); | |
637 | } | |
638 | ||
639 | static int virtio_ccw_scsi_exit(VirtioCcwDevice *dev) | |
640 | { | |
641 | virtio_scsi_exit(dev->vdev); | |
642 | return virtio_ccw_exit(dev); | |
643 | } | |
644 | ||
645 | /* DeviceState to VirtioCcwDevice. Note: used on datapath, | |
646 | * be careful and test performance if you change this. | |
647 | */ | |
648 | static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d) | |
649 | { | |
650 | return container_of(d, VirtioCcwDevice, parent_obj); | |
651 | } | |
652 | ||
653 | static void virtio_ccw_notify(DeviceState *d, uint16_t vector) | |
654 | { | |
655 | VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d); | |
656 | SubchDev *sch = dev->sch; | |
657 | uint64_t indicators; | |
658 | ||
659 | if (vector >= 128) { | |
660 | return; | |
661 | } | |
662 | ||
663 | if (vector < VIRTIO_PCI_QUEUE_MAX) { | |
664 | indicators = ldq_phys(dev->indicators); | |
665 | set_bit(vector, &indicators); | |
666 | stq_phys(dev->indicators, indicators); | |
667 | } else { | |
668 | vector = 0; | |
669 | indicators = ldq_phys(dev->indicators2); | |
670 | set_bit(vector, &indicators); | |
671 | stq_phys(dev->indicators2, indicators); | |
672 | } | |
673 | ||
674 | css_conditional_io_interrupt(sch); | |
675 | ||
676 | } | |
677 | ||
678 | static unsigned virtio_ccw_get_features(DeviceState *d) | |
679 | { | |
680 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); | |
681 | ||
682 | /* Only the first 32 feature bits are used. */ | |
683 | return dev->host_features[0]; | |
684 | } | |
685 | ||
686 | static void virtio_ccw_reset(DeviceState *d) | |
687 | { | |
688 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); | |
689 | ||
690 | virtio_reset(dev->vdev); | |
691 | css_reset_sch(dev->sch); | |
692 | } | |
693 | ||
694 | /**************** Virtio-ccw Bus Device Descriptions *******************/ | |
695 | ||
696 | static const VirtIOBindings virtio_ccw_bindings = { | |
697 | .notify = virtio_ccw_notify, | |
698 | .get_features = virtio_ccw_get_features, | |
699 | }; | |
700 | ||
701 | static Property virtio_ccw_net_properties[] = { | |
702 | DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), | |
703 | DEFINE_VIRTIO_NET_FEATURES(VirtioCcwDevice, host_features[0]), | |
704 | DEFINE_NIC_PROPERTIES(VirtioCcwDevice, nic), | |
705 | DEFINE_PROP_UINT32("x-txtimer", VirtioCcwDevice, | |
706 | net.txtimer, TX_TIMER_INTERVAL), | |
707 | DEFINE_PROP_INT32("x-txburst", VirtioCcwDevice, | |
708 | net.txburst, TX_BURST), | |
709 | DEFINE_PROP_STRING("tx", VirtioCcwDevice, net.tx), | |
710 | DEFINE_PROP_END_OF_LIST(), | |
711 | }; | |
712 | ||
713 | static void virtio_ccw_net_class_init(ObjectClass *klass, void *data) | |
714 | { | |
715 | DeviceClass *dc = DEVICE_CLASS(klass); | |
716 | VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); | |
717 | ||
718 | k->init = virtio_ccw_net_init; | |
719 | k->exit = virtio_ccw_net_exit; | |
720 | dc->reset = virtio_ccw_reset; | |
721 | dc->props = virtio_ccw_net_properties; | |
722 | } | |
723 | ||
724 | static const TypeInfo virtio_ccw_net = { | |
725 | .name = "virtio-net-ccw", | |
726 | .parent = TYPE_VIRTIO_CCW_DEVICE, | |
727 | .instance_size = sizeof(VirtioCcwDevice), | |
728 | .class_init = virtio_ccw_net_class_init, | |
729 | }; | |
730 | ||
731 | static Property virtio_ccw_blk_properties[] = { | |
732 | DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), | |
733 | DEFINE_BLOCK_PROPERTIES(VirtioCcwDevice, blk.conf), | |
734 | DEFINE_PROP_STRING("serial", VirtioCcwDevice, blk.serial), | |
735 | #ifdef __linux__ | |
736 | DEFINE_PROP_BIT("scsi", VirtioCcwDevice, blk.scsi, 0, true), | |
737 | #endif | |
738 | DEFINE_VIRTIO_BLK_FEATURES(VirtioCcwDevice, host_features[0]), | |
739 | DEFINE_PROP_END_OF_LIST(), | |
740 | }; | |
741 | ||
742 | static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data) | |
743 | { | |
744 | DeviceClass *dc = DEVICE_CLASS(klass); | |
745 | VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); | |
746 | ||
747 | k->init = virtio_ccw_blk_init; | |
748 | k->exit = virtio_ccw_blk_exit; | |
749 | dc->reset = virtio_ccw_reset; | |
750 | dc->props = virtio_ccw_blk_properties; | |
751 | } | |
752 | ||
753 | static const TypeInfo virtio_ccw_blk = { | |
754 | .name = "virtio-blk-ccw", | |
755 | .parent = TYPE_VIRTIO_CCW_DEVICE, | |
756 | .instance_size = sizeof(VirtioCcwDevice), | |
757 | .class_init = virtio_ccw_blk_class_init, | |
758 | }; | |
759 | ||
760 | static Property virtio_ccw_serial_properties[] = { | |
761 | DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), | |
762 | DEFINE_PROP_UINT32("max_ports", VirtioCcwDevice, | |
763 | serial.max_virtserial_ports, 31), | |
764 | DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]), | |
765 | DEFINE_PROP_END_OF_LIST(), | |
766 | }; | |
767 | ||
768 | static void virtio_ccw_serial_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_serial_init; | |
774 | k->exit = virtio_ccw_serial_exit; | |
775 | dc->reset = virtio_ccw_reset; | |
776 | dc->props = virtio_ccw_serial_properties; | |
777 | } | |
778 | ||
779 | static const TypeInfo virtio_ccw_serial = { | |
780 | .name = "virtio-serial-ccw", | |
781 | .parent = TYPE_VIRTIO_CCW_DEVICE, | |
782 | .instance_size = sizeof(VirtioCcwDevice), | |
783 | .class_init = virtio_ccw_serial_class_init, | |
784 | }; | |
785 | ||
786 | static Property virtio_ccw_balloon_properties[] = { | |
787 | DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), | |
788 | DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]), | |
789 | DEFINE_PROP_END_OF_LIST(), | |
790 | }; | |
791 | ||
792 | static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data) | |
793 | { | |
794 | DeviceClass *dc = DEVICE_CLASS(klass); | |
795 | VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); | |
796 | ||
797 | k->init = virtio_ccw_balloon_init; | |
798 | k->exit = virtio_ccw_balloon_exit; | |
799 | dc->reset = virtio_ccw_reset; | |
800 | dc->props = virtio_ccw_balloon_properties; | |
801 | } | |
802 | ||
803 | static const TypeInfo virtio_ccw_balloon = { | |
804 | .name = "virtio-balloon-ccw", | |
805 | .parent = TYPE_VIRTIO_CCW_DEVICE, | |
806 | .instance_size = sizeof(VirtioCcwDevice), | |
807 | .class_init = virtio_ccw_balloon_class_init, | |
808 | }; | |
809 | ||
810 | static Property virtio_ccw_scsi_properties[] = { | |
811 | DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), | |
812 | DEFINE_VIRTIO_SCSI_PROPERTIES(VirtioCcwDevice, host_features[0], scsi), | |
813 | DEFINE_PROP_END_OF_LIST(), | |
814 | }; | |
815 | ||
816 | static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data) | |
817 | { | |
818 | DeviceClass *dc = DEVICE_CLASS(klass); | |
819 | VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); | |
820 | ||
821 | k->init = virtio_ccw_scsi_init; | |
822 | k->exit = virtio_ccw_scsi_exit; | |
823 | dc->reset = virtio_ccw_reset; | |
824 | dc->props = virtio_ccw_scsi_properties; | |
825 | } | |
826 | ||
827 | static const TypeInfo virtio_ccw_scsi = { | |
828 | .name = "virtio-scsi-ccw", | |
829 | .parent = TYPE_VIRTIO_CCW_DEVICE, | |
830 | .instance_size = sizeof(VirtioCcwDevice), | |
831 | .class_init = virtio_ccw_scsi_class_init, | |
832 | }; | |
833 | ||
834 | static int virtio_ccw_busdev_init(DeviceState *dev) | |
835 | { | |
836 | VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; | |
837 | VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev); | |
838 | ||
839 | virtio_ccw_bus_new(&_dev->bus, _dev); | |
840 | ||
841 | return _info->init(_dev); | |
842 | } | |
843 | ||
844 | static int virtio_ccw_busdev_exit(DeviceState *dev) | |
845 | { | |
846 | VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; | |
847 | VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev); | |
848 | ||
849 | return _info->exit(_dev); | |
850 | } | |
851 | ||
852 | static int virtio_ccw_busdev_unplug(DeviceState *dev) | |
853 | { | |
854 | VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; | |
855 | SubchDev *sch = _dev->sch; | |
856 | ||
857 | /* | |
858 | * We should arrive here only for device_del, since we don't support | |
859 | * direct hot(un)plug of channels, but only through virtio. | |
860 | */ | |
861 | assert(sch != NULL); | |
862 | /* Subchannel is now disabled and no longer valid. */ | |
863 | sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA | | |
864 | PMCW_FLAGS_MASK_DNV); | |
865 | ||
866 | css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0); | |
867 | ||
868 | object_unparent(OBJECT(dev)); | |
869 | qdev_free(dev); | |
870 | return 0; | |
871 | } | |
872 | ||
873 | static void virtio_ccw_device_class_init(ObjectClass *klass, void *data) | |
874 | { | |
875 | DeviceClass *dc = DEVICE_CLASS(klass); | |
876 | ||
877 | dc->init = virtio_ccw_busdev_init; | |
878 | dc->exit = virtio_ccw_busdev_exit; | |
879 | dc->unplug = virtio_ccw_busdev_unplug; | |
880 | dc->bus_type = TYPE_VIRTUAL_CSS_BUS; | |
881 | ||
882 | } | |
883 | ||
884 | static const TypeInfo virtio_ccw_device_info = { | |
885 | .name = TYPE_VIRTIO_CCW_DEVICE, | |
886 | .parent = TYPE_DEVICE, | |
887 | .instance_size = sizeof(VirtioCcwDevice), | |
888 | .class_init = virtio_ccw_device_class_init, | |
889 | .class_size = sizeof(VirtIOCCWDeviceClass), | |
890 | .abstract = true, | |
891 | }; | |
892 | ||
893 | /***************** Virtual-css Bus Bridge Device ********************/ | |
894 | /* Only required to have the virtio bus as child in the system bus */ | |
895 | ||
896 | static int virtual_css_bridge_init(SysBusDevice *dev) | |
897 | { | |
898 | /* nothing */ | |
899 | return 0; | |
900 | } | |
901 | ||
902 | static void virtual_css_bridge_class_init(ObjectClass *klass, void *data) | |
903 | { | |
904 | DeviceClass *dc = DEVICE_CLASS(klass); | |
905 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); | |
906 | ||
907 | k->init = virtual_css_bridge_init; | |
908 | dc->no_user = 1; | |
909 | } | |
910 | ||
911 | static const TypeInfo virtual_css_bridge_info = { | |
912 | .name = "virtual-css-bridge", | |
913 | .parent = TYPE_SYS_BUS_DEVICE, | |
914 | .instance_size = sizeof(SysBusDevice), | |
915 | .class_init = virtual_css_bridge_class_init, | |
916 | }; | |
917 | ||
918 | /* virtio-ccw-bus */ | |
919 | ||
920 | void virtio_ccw_bus_new(VirtioBusState *bus, VirtioCcwDevice *dev) | |
921 | { | |
922 | DeviceState *qdev = DEVICE(dev); | |
923 | BusState *qbus; | |
924 | ||
925 | qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_CCW_BUS, qdev, NULL); | |
926 | qbus = BUS(bus); | |
927 | qbus->allow_hotplug = 0; | |
928 | } | |
929 | ||
930 | static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data) | |
931 | { | |
932 | VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); | |
933 | BusClass *bus_class = BUS_CLASS(klass); | |
934 | ||
935 | bus_class->max_dev = 1; | |
936 | k->notify = virtio_ccw_notify; | |
937 | k->get_features = virtio_ccw_get_features; | |
938 | } | |
939 | ||
940 | static const TypeInfo virtio_ccw_bus_info = { | |
941 | .name = TYPE_VIRTIO_CCW_BUS, | |
942 | .parent = TYPE_VIRTIO_BUS, | |
943 | .instance_size = sizeof(VirtioCcwBusState), | |
944 | .class_init = virtio_ccw_bus_class_init, | |
945 | }; | |
946 | ||
947 | static void virtio_ccw_register(void) | |
948 | { | |
949 | type_register_static(&virtio_ccw_bus_info); | |
950 | type_register_static(&virtual_css_bus_info); | |
951 | type_register_static(&virtio_ccw_device_info); | |
952 | type_register_static(&virtio_ccw_serial); | |
953 | type_register_static(&virtio_ccw_blk); | |
954 | type_register_static(&virtio_ccw_net); | |
955 | type_register_static(&virtio_ccw_balloon); | |
956 | type_register_static(&virtio_ccw_scsi); | |
957 | type_register_static(&virtual_css_bridge_info); | |
958 | } | |
959 | ||
960 | type_init(virtio_ccw_register) |