]> Git Repo - qemu.git/blame - hw/s390x/css.c
s390x/css: handle ccw-0 TIC correctly
[qemu.git] / hw / s390x / css.c
CommitLineData
df1fe5bb
CH
1/*
2 * Channel subsystem base support.
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/qdev.h>
13#include "qemu/bitops.h"
fdfba1a2 14#include "exec/address-spaces.h"
df1fe5bb
CH
15#include "cpu.h"
16#include "ioinst.h"
17#include "css.h"
18#include "trace.h"
03cf077a 19#include "hw/s390x/s390_flic.h"
df1fe5bb
CH
20
21typedef struct CrwContainer {
22 CRW crw;
23 QTAILQ_ENTRY(CrwContainer) sibling;
24} CrwContainer;
25
26typedef struct ChpInfo {
27 uint8_t in_use;
28 uint8_t type;
29 uint8_t is_virtual;
30} ChpInfo;
31
32typedef struct SubchSet {
33 SubchDev *sch[MAX_SCHID + 1];
34 unsigned long schids_used[BITS_TO_LONGS(MAX_SCHID + 1)];
35 unsigned long devnos_used[BITS_TO_LONGS(MAX_SCHID + 1)];
36} SubchSet;
37
38typedef struct CssImage {
39 SubchSet *sch_set[MAX_SSID + 1];
40 ChpInfo chpids[MAX_CHPID + 1];
41} CssImage;
42
03cf077a
CH
43typedef struct IoAdapter {
44 uint32_t id;
45 uint8_t type;
46 uint8_t isc;
47 QTAILQ_ENTRY(IoAdapter) sibling;
48} IoAdapter;
49
df1fe5bb
CH
50typedef struct ChannelSubSys {
51 QTAILQ_HEAD(, CrwContainer) pending_crws;
52 bool do_crw_mchk;
53 bool crws_lost;
54 uint8_t max_cssid;
55 uint8_t max_ssid;
56 bool chnmon_active;
57 uint64_t chnmon_area;
58 CssImage *css[MAX_CSSID + 1];
59 uint8_t default_cssid;
03cf077a 60 QTAILQ_HEAD(, IoAdapter) io_adapters;
df1fe5bb
CH
61} ChannelSubSys;
62
63static ChannelSubSys *channel_subsys;
64
65int css_create_css_image(uint8_t cssid, bool default_image)
66{
67 trace_css_new_image(cssid, default_image ? "(default)" : "");
68 if (cssid > MAX_CSSID) {
69 return -EINVAL;
70 }
71 if (channel_subsys->css[cssid]) {
72 return -EBUSY;
73 }
74 channel_subsys->css[cssid] = g_malloc0(sizeof(CssImage));
75 if (default_image) {
76 channel_subsys->default_cssid = cssid;
77 }
78 return 0;
79}
80
03cf077a
CH
81int css_register_io_adapter(uint8_t type, uint8_t isc, bool swap,
82 bool maskable, uint32_t *id)
83{
84 IoAdapter *adapter;
85 bool found = false;
86 int ret;
87 S390FLICState *fs = s390_get_flic();
88 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
89
90 *id = 0;
91 QTAILQ_FOREACH(adapter, &channel_subsys->io_adapters, sibling) {
92 if ((adapter->type == type) && (adapter->isc == isc)) {
93 *id = adapter->id;
94 found = true;
95 ret = 0;
96 break;
97 }
98 if (adapter->id >= *id) {
99 *id = adapter->id + 1;
100 }
101 }
102 if (found) {
103 goto out;
104 }
105 adapter = g_new0(IoAdapter, 1);
106 ret = fsc->register_io_adapter(fs, *id, isc, swap, maskable);
107 if (ret == 0) {
108 adapter->id = *id;
109 adapter->isc = isc;
110 adapter->type = type;
111 QTAILQ_INSERT_TAIL(&channel_subsys->io_adapters, adapter, sibling);
112 } else {
113 g_free(adapter);
114 fprintf(stderr, "Unexpected error %d when registering adapter %d\n",
115 ret, *id);
116 }
117out:
118 return ret;
119}
120
b4436a0b 121uint16_t css_build_subchannel_id(SubchDev *sch)
df1fe5bb
CH
122{
123 if (channel_subsys->max_cssid > 0) {
124 return (sch->cssid << 8) | (1 << 3) | (sch->ssid << 1) | 1;
125 }
126 return (sch->ssid << 1) | 1;
127}
128
129static void css_inject_io_interrupt(SubchDev *sch)
130{
df1fe5bb
CH
131 uint8_t isc = (sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ISC) >> 11;
132
133 trace_css_io_interrupt(sch->cssid, sch->ssid, sch->schid,
134 sch->curr_status.pmcw.intparm, isc, "");
de13d216 135 s390_io_interrupt(css_build_subchannel_id(sch),
df1fe5bb
CH
136 sch->schid,
137 sch->curr_status.pmcw.intparm,
91b0a8f3 138 isc << 27);
df1fe5bb
CH
139}
140
141void css_conditional_io_interrupt(SubchDev *sch)
142{
143 /*
144 * If the subchannel is not currently status pending, make it pending
145 * with alert status.
146 */
147 if (!(sch->curr_status.scsw.ctrl & SCSW_STCTL_STATUS_PEND)) {
df1fe5bb
CH
148 uint8_t isc = (sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ISC) >> 11;
149
150 trace_css_io_interrupt(sch->cssid, sch->ssid, sch->schid,
151 sch->curr_status.pmcw.intparm, isc,
152 "(unsolicited)");
153 sch->curr_status.scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
154 sch->curr_status.scsw.ctrl |=
155 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
156 /* Inject an I/O interrupt. */
de13d216 157 s390_io_interrupt(css_build_subchannel_id(sch),
df1fe5bb
CH
158 sch->schid,
159 sch->curr_status.pmcw.intparm,
91b0a8f3 160 isc << 27);
df1fe5bb
CH
161 }
162}
163
7e749462
CH
164void css_adapter_interrupt(uint8_t isc)
165{
7e749462
CH
166 uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
167
168 trace_css_adapter_interrupt(isc);
de13d216 169 s390_io_interrupt(0, 0, 0, io_int_word);
7e749462
CH
170}
171
df1fe5bb
CH
172static void sch_handle_clear_func(SubchDev *sch)
173{
174 PMCW *p = &sch->curr_status.pmcw;
175 SCSW *s = &sch->curr_status.scsw;
176 int path;
177
178 /* Path management: In our simple css, we always choose the only path. */
179 path = 0x80;
180
4c293dc6 181 /* Reset values prior to 'issuing the clear signal'. */
df1fe5bb
CH
182 p->lpum = 0;
183 p->pom = 0xff;
184 s->flags &= ~SCSW_FLAGS_MASK_PNO;
185
186 /* We always 'attempt to issue the clear signal', and we always succeed. */
df1fe5bb
CH
187 sch->channel_prog = 0x0;
188 sch->last_cmd_valid = false;
189 s->ctrl &= ~SCSW_ACTL_CLEAR_PEND;
190 s->ctrl |= SCSW_STCTL_STATUS_PEND;
191
192 s->dstat = 0;
193 s->cstat = 0;
194 p->lpum = path;
195
196}
197
198static void sch_handle_halt_func(SubchDev *sch)
199{
200
201 PMCW *p = &sch->curr_status.pmcw;
202 SCSW *s = &sch->curr_status.scsw;
2ed982b6 203 hwaddr curr_ccw = sch->channel_prog;
df1fe5bb
CH
204 int path;
205
206 /* Path management: In our simple css, we always choose the only path. */
207 path = 0x80;
208
209 /* We always 'attempt to issue the halt signal', and we always succeed. */
df1fe5bb
CH
210 sch->channel_prog = 0x0;
211 sch->last_cmd_valid = false;
212 s->ctrl &= ~SCSW_ACTL_HALT_PEND;
213 s->ctrl |= SCSW_STCTL_STATUS_PEND;
214
215 if ((s->ctrl & (SCSW_ACTL_SUBCH_ACTIVE | SCSW_ACTL_DEVICE_ACTIVE)) ||
216 !((s->ctrl & SCSW_ACTL_START_PEND) ||
217 (s->ctrl & SCSW_ACTL_SUSP))) {
218 s->dstat = SCSW_DSTAT_DEVICE_END;
219 }
2ed982b6
CH
220 if ((s->ctrl & (SCSW_ACTL_SUBCH_ACTIVE | SCSW_ACTL_DEVICE_ACTIVE)) ||
221 (s->ctrl & SCSW_ACTL_SUSP)) {
222 s->cpa = curr_ccw + 8;
223 }
df1fe5bb
CH
224 s->cstat = 0;
225 p->lpum = path;
226
227}
228
229static void copy_sense_id_to_guest(SenseId *dest, SenseId *src)
230{
231 int i;
232
233 dest->reserved = src->reserved;
234 dest->cu_type = cpu_to_be16(src->cu_type);
235 dest->cu_model = src->cu_model;
236 dest->dev_type = cpu_to_be16(src->dev_type);
237 dest->dev_model = src->dev_model;
238 dest->unused = src->unused;
239 for (i = 0; i < ARRAY_SIZE(dest->ciw); i++) {
240 dest->ciw[i].type = src->ciw[i].type;
241 dest->ciw[i].command = src->ciw[i].command;
242 dest->ciw[i].count = cpu_to_be16(src->ciw[i].count);
243 }
244}
245
a327c921 246static CCW1 copy_ccw_from_guest(hwaddr addr, bool fmt1)
df1fe5bb 247{
a327c921
CH
248 CCW0 tmp0;
249 CCW1 tmp1;
df1fe5bb
CH
250 CCW1 ret;
251
a327c921
CH
252 if (fmt1) {
253 cpu_physical_memory_read(addr, &tmp1, sizeof(tmp1));
254 ret.cmd_code = tmp1.cmd_code;
255 ret.flags = tmp1.flags;
256 ret.count = be16_to_cpu(tmp1.count);
257 ret.cda = be32_to_cpu(tmp1.cda);
258 } else {
259 cpu_physical_memory_read(addr, &tmp0, sizeof(tmp0));
260 ret.cmd_code = tmp0.cmd_code;
261 ret.flags = tmp0.flags;
262 ret.count = be16_to_cpu(tmp0.count);
263 ret.cda = be16_to_cpu(tmp0.cda1) | (tmp0.cda0 << 16);
fde8206b
PM
264 if ((ret.cmd_code & 0x0f) == CCW_CMD_TIC) {
265 ret.cmd_code &= 0x0f;
266 }
a327c921 267 }
df1fe5bb
CH
268 return ret;
269}
270
271static int css_interpret_ccw(SubchDev *sch, hwaddr ccw_addr)
272{
273 int ret;
274 bool check_len;
275 int len;
276 CCW1 ccw;
277
278 if (!ccw_addr) {
279 return -EIO;
280 }
281
a327c921
CH
282 /* Translate everything to format-1 ccws - the information is the same. */
283 ccw = copy_ccw_from_guest(ccw_addr, sch->ccw_fmt_1);
df1fe5bb
CH
284
285 /* Check for invalid command codes. */
286 if ((ccw.cmd_code & 0x0f) == 0) {
287 return -EINVAL;
288 }
289 if (((ccw.cmd_code & 0x0f) == CCW_CMD_TIC) &&
290 ((ccw.cmd_code & 0xf0) != 0)) {
291 return -EINVAL;
292 }
293
294 if (ccw.flags & CCW_FLAG_SUSPEND) {
8d034a6f 295 return -EINPROGRESS;
df1fe5bb
CH
296 }
297
298 check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
299
e8601dd5
CH
300 if (!ccw.cda) {
301 if (sch->ccw_no_data_cnt == 255) {
302 return -EINVAL;
303 }
304 sch->ccw_no_data_cnt++;
305 }
306
df1fe5bb
CH
307 /* Look at the command. */
308 switch (ccw.cmd_code) {
309 case CCW_CMD_NOOP:
310 /* Nothing to do. */
311 ret = 0;
312 break;
313 case CCW_CMD_BASIC_SENSE:
314 if (check_len) {
315 if (ccw.count != sizeof(sch->sense_data)) {
316 ret = -EINVAL;
317 break;
318 }
319 }
320 len = MIN(ccw.count, sizeof(sch->sense_data));
321 cpu_physical_memory_write(ccw.cda, sch->sense_data, len);
322 sch->curr_status.scsw.count = ccw.count - len;
323 memset(sch->sense_data, 0, sizeof(sch->sense_data));
324 ret = 0;
325 break;
326 case CCW_CMD_SENSE_ID:
327 {
328 SenseId sense_id;
329
330 copy_sense_id_to_guest(&sense_id, &sch->id);
331 /* Sense ID information is device specific. */
332 if (check_len) {
333 if (ccw.count != sizeof(sense_id)) {
334 ret = -EINVAL;
335 break;
336 }
337 }
338 len = MIN(ccw.count, sizeof(sense_id));
339 /*
340 * Only indicate 0xff in the first sense byte if we actually
341 * have enough place to store at least bytes 0-3.
342 */
343 if (len >= 4) {
344 sense_id.reserved = 0xff;
345 } else {
346 sense_id.reserved = 0;
347 }
348 cpu_physical_memory_write(ccw.cda, &sense_id, len);
349 sch->curr_status.scsw.count = ccw.count - len;
350 ret = 0;
351 break;
352 }
353 case CCW_CMD_TIC:
354 if (sch->last_cmd_valid && (sch->last_cmd.cmd_code == CCW_CMD_TIC)) {
355 ret = -EINVAL;
356 break;
357 }
358 if (ccw.flags & (CCW_FLAG_CC | CCW_FLAG_DC)) {
359 ret = -EINVAL;
360 break;
361 }
362 sch->channel_prog = ccw.cda;
363 ret = -EAGAIN;
364 break;
365 default:
366 if (sch->ccw_cb) {
367 /* Handle device specific commands. */
368 ret = sch->ccw_cb(sch, ccw);
369 } else {
8d034a6f 370 ret = -ENOSYS;
df1fe5bb
CH
371 }
372 break;
373 }
374 sch->last_cmd = ccw;
375 sch->last_cmd_valid = true;
376 if (ret == 0) {
377 if (ccw.flags & CCW_FLAG_CC) {
378 sch->channel_prog += 8;
379 ret = -EAGAIN;
380 }
381 }
382
383 return ret;
384}
385
56bf1a8e 386static void sch_handle_start_func(SubchDev *sch, ORB *orb)
df1fe5bb
CH
387{
388
389 PMCW *p = &sch->curr_status.pmcw;
390 SCSW *s = &sch->curr_status.scsw;
df1fe5bb
CH
391 int path;
392 int ret;
393
394 /* Path management: In our simple css, we always choose the only path. */
395 path = 0x80;
396
397 if (!(s->ctrl & SCSW_ACTL_SUSP)) {
398 /* Look at the orb and try to execute the channel program. */
56bf1a8e 399 assert(orb != NULL); /* resume does not pass an orb */
df1fe5bb
CH
400 p->intparm = orb->intparm;
401 if (!(orb->lpm & path)) {
402 /* Generate a deferred cc 3 condition. */
403 s->flags |= SCSW_FLAGS_MASK_CC;
404 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
405 s->ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
406 return;
407 }
a327c921 408 sch->ccw_fmt_1 = !!(orb->ctrl0 & ORB_CTRL0_MASK_FMT);
e8601dd5 409 sch->ccw_no_data_cnt = 0;
df1fe5bb
CH
410 } else {
411 s->ctrl &= ~(SCSW_ACTL_SUSP | SCSW_ACTL_RESUME_PEND);
412 }
413 sch->last_cmd_valid = false;
414 do {
415 ret = css_interpret_ccw(sch, sch->channel_prog);
416 switch (ret) {
417 case -EAGAIN:
418 /* ccw chain, continue processing */
419 break;
420 case 0:
421 /* success */
422 s->ctrl &= ~SCSW_ACTL_START_PEND;
423 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
424 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
425 SCSW_STCTL_STATUS_PEND;
426 s->dstat = SCSW_DSTAT_CHANNEL_END | SCSW_DSTAT_DEVICE_END;
2ed982b6 427 s->cpa = sch->channel_prog + 8;
df1fe5bb 428 break;
8d034a6f 429 case -ENOSYS:
df1fe5bb
CH
430 /* unsupported command, generate unit check (command reject) */
431 s->ctrl &= ~SCSW_ACTL_START_PEND;
432 s->dstat = SCSW_DSTAT_UNIT_CHECK;
433 /* Set sense bit 0 in ecw0. */
434 sch->sense_data[0] = 0x80;
435 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
436 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
437 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
2ed982b6 438 s->cpa = sch->channel_prog + 8;
df1fe5bb
CH
439 break;
440 case -EFAULT:
441 /* memory problem, generate channel data check */
442 s->ctrl &= ~SCSW_ACTL_START_PEND;
443 s->cstat = SCSW_CSTAT_DATA_CHECK;
444 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
445 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
446 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
2ed982b6 447 s->cpa = sch->channel_prog + 8;
df1fe5bb
CH
448 break;
449 case -EBUSY:
450 /* subchannel busy, generate deferred cc 1 */
451 s->flags &= ~SCSW_FLAGS_MASK_CC;
452 s->flags |= (1 << 8);
453 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
454 s->ctrl |= SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
455 break;
8d034a6f 456 case -EINPROGRESS:
df1fe5bb
CH
457 /* channel program has been suspended */
458 s->ctrl &= ~SCSW_ACTL_START_PEND;
459 s->ctrl |= SCSW_ACTL_SUSP;
460 break;
461 default:
462 /* error, generate channel program check */
463 s->ctrl &= ~SCSW_ACTL_START_PEND;
464 s->cstat = SCSW_CSTAT_PROG_CHECK;
465 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
466 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
467 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
2ed982b6 468 s->cpa = sch->channel_prog + 8;
df1fe5bb
CH
469 break;
470 }
471 } while (ret == -EAGAIN);
472
473}
474
475/*
476 * On real machines, this would run asynchronously to the main vcpus.
477 * We might want to make some parts of the ssch handling (interpreting
478 * read/writes) asynchronous later on if we start supporting more than
479 * our current very simple devices.
480 */
56bf1a8e 481static void do_subchannel_work(SubchDev *sch, ORB *orb)
df1fe5bb
CH
482{
483
484 SCSW *s = &sch->curr_status.scsw;
485
486 if (s->ctrl & SCSW_FCTL_CLEAR_FUNC) {
487 sch_handle_clear_func(sch);
488 } else if (s->ctrl & SCSW_FCTL_HALT_FUNC) {
489 sch_handle_halt_func(sch);
490 } else if (s->ctrl & SCSW_FCTL_START_FUNC) {
56bf1a8e 491 sch_handle_start_func(sch, orb);
df1fe5bb
CH
492 } else {
493 /* Cannot happen. */
494 return;
495 }
496 css_inject_io_interrupt(sch);
497}
498
499static void copy_pmcw_to_guest(PMCW *dest, const PMCW *src)
500{
501 int i;
502
503 dest->intparm = cpu_to_be32(src->intparm);
504 dest->flags = cpu_to_be16(src->flags);
505 dest->devno = cpu_to_be16(src->devno);
506 dest->lpm = src->lpm;
507 dest->pnom = src->pnom;
508 dest->lpum = src->lpum;
509 dest->pim = src->pim;
510 dest->mbi = cpu_to_be16(src->mbi);
511 dest->pom = src->pom;
512 dest->pam = src->pam;
513 for (i = 0; i < ARRAY_SIZE(dest->chpid); i++) {
514 dest->chpid[i] = src->chpid[i];
515 }
516 dest->chars = cpu_to_be32(src->chars);
517}
518
519static void copy_scsw_to_guest(SCSW *dest, const SCSW *src)
520{
521 dest->flags = cpu_to_be16(src->flags);
522 dest->ctrl = cpu_to_be16(src->ctrl);
523 dest->cpa = cpu_to_be32(src->cpa);
524 dest->dstat = src->dstat;
525 dest->cstat = src->cstat;
526 dest->count = cpu_to_be16(src->count);
527}
528
529static void copy_schib_to_guest(SCHIB *dest, const SCHIB *src)
530{
531 int i;
532
533 copy_pmcw_to_guest(&dest->pmcw, &src->pmcw);
534 copy_scsw_to_guest(&dest->scsw, &src->scsw);
535 dest->mba = cpu_to_be64(src->mba);
536 for (i = 0; i < ARRAY_SIZE(dest->mda); i++) {
537 dest->mda[i] = src->mda[i];
538 }
539}
540
541int css_do_stsch(SubchDev *sch, SCHIB *schib)
542{
543 /* Use current status. */
544 copy_schib_to_guest(schib, &sch->curr_status);
545 return 0;
546}
547
548static void copy_pmcw_from_guest(PMCW *dest, const PMCW *src)
549{
550 int i;
551
552 dest->intparm = be32_to_cpu(src->intparm);
553 dest->flags = be16_to_cpu(src->flags);
554 dest->devno = be16_to_cpu(src->devno);
555 dest->lpm = src->lpm;
556 dest->pnom = src->pnom;
557 dest->lpum = src->lpum;
558 dest->pim = src->pim;
559 dest->mbi = be16_to_cpu(src->mbi);
560 dest->pom = src->pom;
561 dest->pam = src->pam;
562 for (i = 0; i < ARRAY_SIZE(dest->chpid); i++) {
563 dest->chpid[i] = src->chpid[i];
564 }
565 dest->chars = be32_to_cpu(src->chars);
566}
567
568static void copy_scsw_from_guest(SCSW *dest, const SCSW *src)
569{
570 dest->flags = be16_to_cpu(src->flags);
571 dest->ctrl = be16_to_cpu(src->ctrl);
572 dest->cpa = be32_to_cpu(src->cpa);
573 dest->dstat = src->dstat;
574 dest->cstat = src->cstat;
575 dest->count = be16_to_cpu(src->count);
576}
577
578static void copy_schib_from_guest(SCHIB *dest, const SCHIB *src)
579{
580 int i;
581
582 copy_pmcw_from_guest(&dest->pmcw, &src->pmcw);
583 copy_scsw_from_guest(&dest->scsw, &src->scsw);
584 dest->mba = be64_to_cpu(src->mba);
585 for (i = 0; i < ARRAY_SIZE(dest->mda); i++) {
586 dest->mda[i] = src->mda[i];
587 }
588}
589
bffd09cd 590int css_do_msch(SubchDev *sch, const SCHIB *orig_schib)
df1fe5bb
CH
591{
592 SCSW *s = &sch->curr_status.scsw;
593 PMCW *p = &sch->curr_status.pmcw;
62ac4a52 594 uint16_t oldflags;
df1fe5bb
CH
595 int ret;
596 SCHIB schib;
597
598 if (!(sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_DNV)) {
599 ret = 0;
600 goto out;
601 }
602
603 if (s->ctrl & SCSW_STCTL_STATUS_PEND) {
604 ret = -EINPROGRESS;
605 goto out;
606 }
607
608 if (s->ctrl &
609 (SCSW_FCTL_START_FUNC|SCSW_FCTL_HALT_FUNC|SCSW_FCTL_CLEAR_FUNC)) {
610 ret = -EBUSY;
611 goto out;
612 }
613
614 copy_schib_from_guest(&schib, orig_schib);
615 /* Only update the program-modifiable fields. */
616 p->intparm = schib.pmcw.intparm;
62ac4a52 617 oldflags = p->flags;
df1fe5bb
CH
618 p->flags &= ~(PMCW_FLAGS_MASK_ISC | PMCW_FLAGS_MASK_ENA |
619 PMCW_FLAGS_MASK_LM | PMCW_FLAGS_MASK_MME |
620 PMCW_FLAGS_MASK_MP);
621 p->flags |= schib.pmcw.flags &
622 (PMCW_FLAGS_MASK_ISC | PMCW_FLAGS_MASK_ENA |
623 PMCW_FLAGS_MASK_LM | PMCW_FLAGS_MASK_MME |
624 PMCW_FLAGS_MASK_MP);
625 p->lpm = schib.pmcw.lpm;
626 p->mbi = schib.pmcw.mbi;
627 p->pom = schib.pmcw.pom;
628 p->chars &= ~(PMCW_CHARS_MASK_MBFC | PMCW_CHARS_MASK_CSENSE);
629 p->chars |= schib.pmcw.chars &
630 (PMCW_CHARS_MASK_MBFC | PMCW_CHARS_MASK_CSENSE);
631 sch->curr_status.mba = schib.mba;
632
62ac4a52
TH
633 /* Has the channel been disabled? */
634 if (sch->disable_cb && (oldflags & PMCW_FLAGS_MASK_ENA) != 0
635 && (p->flags & PMCW_FLAGS_MASK_ENA) == 0) {
636 sch->disable_cb(sch);
637 }
638
df1fe5bb
CH
639 ret = 0;
640
641out:
642 return ret;
643}
644
645int css_do_xsch(SubchDev *sch)
646{
647 SCSW *s = &sch->curr_status.scsw;
648 PMCW *p = &sch->curr_status.pmcw;
649 int ret;
650
651 if (!(p->flags & (PMCW_FLAGS_MASK_DNV | PMCW_FLAGS_MASK_ENA))) {
652 ret = -ENODEV;
653 goto out;
654 }
655
656 if (!(s->ctrl & SCSW_CTRL_MASK_FCTL) ||
657 ((s->ctrl & SCSW_CTRL_MASK_FCTL) != SCSW_FCTL_START_FUNC) ||
658 (!(s->ctrl &
659 (SCSW_ACTL_RESUME_PEND | SCSW_ACTL_START_PEND | SCSW_ACTL_SUSP))) ||
660 (s->ctrl & SCSW_ACTL_SUBCH_ACTIVE)) {
661 ret = -EINPROGRESS;
662 goto out;
663 }
664
665 if (s->ctrl & SCSW_CTRL_MASK_STCTL) {
666 ret = -EBUSY;
667 goto out;
668 }
669
670 /* Cancel the current operation. */
671 s->ctrl &= ~(SCSW_FCTL_START_FUNC |
672 SCSW_ACTL_RESUME_PEND |
673 SCSW_ACTL_START_PEND |
674 SCSW_ACTL_SUSP);
675 sch->channel_prog = 0x0;
676 sch->last_cmd_valid = false;
df1fe5bb
CH
677 s->dstat = 0;
678 s->cstat = 0;
679 ret = 0;
680
681out:
682 return ret;
683}
684
685int css_do_csch(SubchDev *sch)
686{
687 SCSW *s = &sch->curr_status.scsw;
688 PMCW *p = &sch->curr_status.pmcw;
689 int ret;
690
691 if (!(p->flags & (PMCW_FLAGS_MASK_DNV | PMCW_FLAGS_MASK_ENA))) {
692 ret = -ENODEV;
693 goto out;
694 }
695
696 /* Trigger the clear function. */
697 s->ctrl &= ~(SCSW_CTRL_MASK_FCTL | SCSW_CTRL_MASK_ACTL);
698 s->ctrl |= SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_CLEAR_FUNC;
699
56bf1a8e 700 do_subchannel_work(sch, NULL);
df1fe5bb
CH
701 ret = 0;
702
703out:
704 return ret;
705}
706
707int css_do_hsch(SubchDev *sch)
708{
709 SCSW *s = &sch->curr_status.scsw;
710 PMCW *p = &sch->curr_status.pmcw;
711 int ret;
712
713 if (!(p->flags & (PMCW_FLAGS_MASK_DNV | PMCW_FLAGS_MASK_ENA))) {
714 ret = -ENODEV;
715 goto out;
716 }
717
718 if (((s->ctrl & SCSW_CTRL_MASK_STCTL) == SCSW_STCTL_STATUS_PEND) ||
719 (s->ctrl & (SCSW_STCTL_PRIMARY |
720 SCSW_STCTL_SECONDARY |
721 SCSW_STCTL_ALERT))) {
722 ret = -EINPROGRESS;
723 goto out;
724 }
725
726 if (s->ctrl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC)) {
727 ret = -EBUSY;
728 goto out;
729 }
730
731 /* Trigger the halt function. */
732 s->ctrl |= SCSW_FCTL_HALT_FUNC;
733 s->ctrl &= ~SCSW_FCTL_START_FUNC;
734 if (((s->ctrl & SCSW_CTRL_MASK_ACTL) ==
735 (SCSW_ACTL_SUBCH_ACTIVE | SCSW_ACTL_DEVICE_ACTIVE)) &&
736 ((s->ctrl & SCSW_CTRL_MASK_STCTL) == SCSW_STCTL_INTERMEDIATE)) {
737 s->ctrl &= ~SCSW_STCTL_STATUS_PEND;
738 }
739 s->ctrl |= SCSW_ACTL_HALT_PEND;
740
56bf1a8e 741 do_subchannel_work(sch, NULL);
df1fe5bb
CH
742 ret = 0;
743
744out:
745 return ret;
746}
747
748static void css_update_chnmon(SubchDev *sch)
749{
750 if (!(sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_MME)) {
751 /* Not active. */
752 return;
753 }
754 /* The counter is conveniently located at the beginning of the struct. */
755 if (sch->curr_status.pmcw.chars & PMCW_CHARS_MASK_MBFC) {
756 /* Format 1, per-subchannel area. */
757 uint32_t count;
758
42874d3a
PM
759 count = address_space_ldl(&address_space_memory,
760 sch->curr_status.mba,
761 MEMTXATTRS_UNSPECIFIED,
762 NULL);
df1fe5bb 763 count++;
42874d3a
PM
764 address_space_stl(&address_space_memory, sch->curr_status.mba, count,
765 MEMTXATTRS_UNSPECIFIED, NULL);
df1fe5bb
CH
766 } else {
767 /* Format 0, global area. */
768 uint32_t offset;
769 uint16_t count;
770
771 offset = sch->curr_status.pmcw.mbi << 5;
42874d3a
PM
772 count = address_space_lduw(&address_space_memory,
773 channel_subsys->chnmon_area + offset,
774 MEMTXATTRS_UNSPECIFIED,
775 NULL);
df1fe5bb 776 count++;
42874d3a
PM
777 address_space_stw(&address_space_memory,
778 channel_subsys->chnmon_area + offset, count,
779 MEMTXATTRS_UNSPECIFIED, NULL);
df1fe5bb
CH
780 }
781}
782
783int css_do_ssch(SubchDev *sch, ORB *orb)
784{
785 SCSW *s = &sch->curr_status.scsw;
786 PMCW *p = &sch->curr_status.pmcw;
787 int ret;
788
789 if (!(p->flags & (PMCW_FLAGS_MASK_DNV | PMCW_FLAGS_MASK_ENA))) {
790 ret = -ENODEV;
791 goto out;
792 }
793
794 if (s->ctrl & SCSW_STCTL_STATUS_PEND) {
795 ret = -EINPROGRESS;
796 goto out;
797 }
798
799 if (s->ctrl & (SCSW_FCTL_START_FUNC |
800 SCSW_FCTL_HALT_FUNC |
801 SCSW_FCTL_CLEAR_FUNC)) {
802 ret = -EBUSY;
803 goto out;
804 }
805
806 /* If monitoring is active, update counter. */
807 if (channel_subsys->chnmon_active) {
808 css_update_chnmon(sch);
809 }
df1fe5bb
CH
810 sch->channel_prog = orb->cpa;
811 /* Trigger the start function. */
812 s->ctrl |= (SCSW_FCTL_START_FUNC | SCSW_ACTL_START_PEND);
813 s->flags &= ~SCSW_FLAGS_MASK_PNO;
814
56bf1a8e 815 do_subchannel_work(sch, orb);
df1fe5bb
CH
816 ret = 0;
817
818out:
819 return ret;
820}
821
b7b6348a
TH
822static void copy_irb_to_guest(IRB *dest, const IRB *src, PMCW *pmcw,
823 int *irb_len)
df1fe5bb
CH
824{
825 int i;
f068d320
CH
826 uint16_t stctl = src->scsw.ctrl & SCSW_CTRL_MASK_STCTL;
827 uint16_t actl = src->scsw.ctrl & SCSW_CTRL_MASK_ACTL;
df1fe5bb
CH
828
829 copy_scsw_to_guest(&dest->scsw, &src->scsw);
830
831 for (i = 0; i < ARRAY_SIZE(dest->esw); i++) {
832 dest->esw[i] = cpu_to_be32(src->esw[i]);
833 }
834 for (i = 0; i < ARRAY_SIZE(dest->ecw); i++) {
835 dest->ecw[i] = cpu_to_be32(src->ecw[i]);
836 }
b7b6348a
TH
837 *irb_len = sizeof(*dest) - sizeof(dest->emw);
838
f068d320
CH
839 /* extended measurements enabled? */
840 if ((src->scsw.flags & SCSW_FLAGS_MASK_ESWF) ||
841 !(pmcw->flags & PMCW_FLAGS_MASK_TF) ||
842 !(pmcw->chars & PMCW_CHARS_MASK_XMWME)) {
843 return;
844 }
845 /* extended measurements pending? */
846 if (!(stctl & SCSW_STCTL_STATUS_PEND)) {
847 return;
848 }
849 if ((stctl & SCSW_STCTL_PRIMARY) ||
850 (stctl == SCSW_STCTL_SECONDARY) ||
851 ((stctl & SCSW_STCTL_INTERMEDIATE) && (actl & SCSW_ACTL_SUSP))) {
852 for (i = 0; i < ARRAY_SIZE(dest->emw); i++) {
853 dest->emw[i] = cpu_to_be32(src->emw[i]);
854 }
df1fe5bb 855 }
b7b6348a 856 *irb_len = sizeof(*dest);
df1fe5bb
CH
857}
858
b7b6348a 859int css_do_tsch_get_irb(SubchDev *sch, IRB *target_irb, int *irb_len)
df1fe5bb
CH
860{
861 SCSW *s = &sch->curr_status.scsw;
862 PMCW *p = &sch->curr_status.pmcw;
863 uint16_t stctl;
df1fe5bb 864 IRB irb;
df1fe5bb
CH
865
866 if (!(p->flags & (PMCW_FLAGS_MASK_DNV | PMCW_FLAGS_MASK_ENA))) {
b7b6348a 867 return 3;
df1fe5bb
CH
868 }
869
870 stctl = s->ctrl & SCSW_CTRL_MASK_STCTL;
df1fe5bb
CH
871
872 /* Prepare the irb for the guest. */
873 memset(&irb, 0, sizeof(IRB));
874
875 /* Copy scsw from current status. */
876 memcpy(&irb.scsw, s, sizeof(SCSW));
877 if (stctl & SCSW_STCTL_STATUS_PEND) {
878 if (s->cstat & (SCSW_CSTAT_DATA_CHECK |
879 SCSW_CSTAT_CHN_CTRL_CHK |
880 SCSW_CSTAT_INTF_CTRL_CHK)) {
881 irb.scsw.flags |= SCSW_FLAGS_MASK_ESWF;
882 irb.esw[0] = 0x04804000;
883 } else {
884 irb.esw[0] = 0x00800000;
885 }
886 /* If a unit check is pending, copy sense data. */
887 if ((s->dstat & SCSW_DSTAT_UNIT_CHECK) &&
888 (p->chars & PMCW_CHARS_MASK_CSENSE)) {
889 irb.scsw.flags |= SCSW_FLAGS_MASK_ESWF | SCSW_FLAGS_MASK_ECTL;
890 memcpy(irb.ecw, sch->sense_data, sizeof(sch->sense_data));
8312976e 891 irb.esw[1] = 0x01000000 | (sizeof(sch->sense_data) << 8);
df1fe5bb
CH
892 }
893 }
894 /* Store the irb to the guest. */
b7b6348a
TH
895 copy_irb_to_guest(target_irb, &irb, p, irb_len);
896
897 return ((stctl & SCSW_STCTL_STATUS_PEND) == 0);
898}
899
900void css_do_tsch_update_subch(SubchDev *sch)
901{
902 SCSW *s = &sch->curr_status.scsw;
903 PMCW *p = &sch->curr_status.pmcw;
904 uint16_t stctl;
905 uint16_t fctl;
906 uint16_t actl;
907
908 stctl = s->ctrl & SCSW_CTRL_MASK_STCTL;
909 fctl = s->ctrl & SCSW_CTRL_MASK_FCTL;
910 actl = s->ctrl & SCSW_CTRL_MASK_ACTL;
df1fe5bb
CH
911
912 /* Clear conditions on subchannel, if applicable. */
913 if (stctl & SCSW_STCTL_STATUS_PEND) {
914 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
915 if ((stctl != (SCSW_STCTL_INTERMEDIATE | SCSW_STCTL_STATUS_PEND)) ||
916 ((fctl & SCSW_FCTL_HALT_FUNC) &&
917 (actl & SCSW_ACTL_SUSP))) {
918 s->ctrl &= ~SCSW_CTRL_MASK_FCTL;
919 }
920 if (stctl != (SCSW_STCTL_INTERMEDIATE | SCSW_STCTL_STATUS_PEND)) {
921 s->flags &= ~SCSW_FLAGS_MASK_PNO;
922 s->ctrl &= ~(SCSW_ACTL_RESUME_PEND |
923 SCSW_ACTL_START_PEND |
924 SCSW_ACTL_HALT_PEND |
925 SCSW_ACTL_CLEAR_PEND |
926 SCSW_ACTL_SUSP);
927 } else {
928 if ((actl & SCSW_ACTL_SUSP) &&
929 (fctl & SCSW_FCTL_START_FUNC)) {
930 s->flags &= ~SCSW_FLAGS_MASK_PNO;
931 if (fctl & SCSW_FCTL_HALT_FUNC) {
932 s->ctrl &= ~(SCSW_ACTL_RESUME_PEND |
933 SCSW_ACTL_START_PEND |
934 SCSW_ACTL_HALT_PEND |
935 SCSW_ACTL_CLEAR_PEND |
936 SCSW_ACTL_SUSP);
937 } else {
938 s->ctrl &= ~SCSW_ACTL_RESUME_PEND;
939 }
940 }
941 }
942 /* Clear pending sense data. */
943 if (p->chars & PMCW_CHARS_MASK_CSENSE) {
944 memset(sch->sense_data, 0 , sizeof(sch->sense_data));
945 }
946 }
df1fe5bb
CH
947}
948
949static void copy_crw_to_guest(CRW *dest, const CRW *src)
950{
951 dest->flags = cpu_to_be16(src->flags);
952 dest->rsid = cpu_to_be16(src->rsid);
953}
954
955int css_do_stcrw(CRW *crw)
956{
957 CrwContainer *crw_cont;
958 int ret;
959
960 crw_cont = QTAILQ_FIRST(&channel_subsys->pending_crws);
961 if (crw_cont) {
962 QTAILQ_REMOVE(&channel_subsys->pending_crws, crw_cont, sibling);
963 copy_crw_to_guest(crw, &crw_cont->crw);
964 g_free(crw_cont);
965 ret = 0;
966 } else {
967 /* List was empty, turn crw machine checks on again. */
968 memset(crw, 0, sizeof(*crw));
969 channel_subsys->do_crw_mchk = true;
970 ret = 1;
971 }
972
973 return ret;
974}
975
7f74f0aa
TH
976static void copy_crw_from_guest(CRW *dest, const CRW *src)
977{
978 dest->flags = be16_to_cpu(src->flags);
979 dest->rsid = be16_to_cpu(src->rsid);
980}
981
982void css_undo_stcrw(CRW *crw)
983{
984 CrwContainer *crw_cont;
985
986 crw_cont = g_try_malloc0(sizeof(CrwContainer));
987 if (!crw_cont) {
988 channel_subsys->crws_lost = true;
989 return;
990 }
991 copy_crw_from_guest(&crw_cont->crw, crw);
992
993 QTAILQ_INSERT_HEAD(&channel_subsys->pending_crws, crw_cont, sibling);
994}
995
50c8d9bf 996int css_do_tpi(IOIntCode *int_code, int lowcore)
df1fe5bb
CH
997{
998 /* No pending interrupts for !KVM. */
999 return 0;
1000 }
1001
1002int css_collect_chp_desc(int m, uint8_t cssid, uint8_t f_chpid, uint8_t l_chpid,
1003 int rfmt, void *buf)
1004{
1005 int i, desc_size;
1006 uint32_t words[8];
1007 uint32_t chpid_type_word;
1008 CssImage *css;
1009
1010 if (!m && !cssid) {
1011 css = channel_subsys->css[channel_subsys->default_cssid];
1012 } else {
1013 css = channel_subsys->css[cssid];
1014 }
1015 if (!css) {
1016 return 0;
1017 }
1018 desc_size = 0;
1019 for (i = f_chpid; i <= l_chpid; i++) {
1020 if (css->chpids[i].in_use) {
1021 chpid_type_word = 0x80000000 | (css->chpids[i].type << 8) | i;
1022 if (rfmt == 0) {
1023 words[0] = cpu_to_be32(chpid_type_word);
1024 words[1] = 0;
1025 memcpy(buf + desc_size, words, 8);
1026 desc_size += 8;
1027 } else if (rfmt == 1) {
1028 words[0] = cpu_to_be32(chpid_type_word);
1029 words[1] = 0;
1030 words[2] = 0;
1031 words[3] = 0;
1032 words[4] = 0;
1033 words[5] = 0;
1034 words[6] = 0;
1035 words[7] = 0;
1036 memcpy(buf + desc_size, words, 32);
1037 desc_size += 32;
1038 }
1039 }
1040 }
1041 return desc_size;
1042}
1043
1044void css_do_schm(uint8_t mbk, int update, int dct, uint64_t mbo)
1045{
1046 /* dct is currently ignored (not really meaningful for our devices) */
1047 /* TODO: Don't ignore mbk. */
1048 if (update && !channel_subsys->chnmon_active) {
1049 /* Enable measuring. */
1050 channel_subsys->chnmon_area = mbo;
1051 channel_subsys->chnmon_active = true;
1052 }
1053 if (!update && channel_subsys->chnmon_active) {
1054 /* Disable measuring. */
1055 channel_subsys->chnmon_area = 0;
1056 channel_subsys->chnmon_active = false;
1057 }
1058}
1059
1060int css_do_rsch(SubchDev *sch)
1061{
1062 SCSW *s = &sch->curr_status.scsw;
1063 PMCW *p = &sch->curr_status.pmcw;
1064 int ret;
1065
1066 if (!(p->flags & (PMCW_FLAGS_MASK_DNV | PMCW_FLAGS_MASK_ENA))) {
1067 ret = -ENODEV;
1068 goto out;
1069 }
1070
1071 if (s->ctrl & SCSW_STCTL_STATUS_PEND) {
1072 ret = -EINPROGRESS;
1073 goto out;
1074 }
1075
1076 if (((s->ctrl & SCSW_CTRL_MASK_FCTL) != SCSW_FCTL_START_FUNC) ||
1077 (s->ctrl & SCSW_ACTL_RESUME_PEND) ||
1078 (!(s->ctrl & SCSW_ACTL_SUSP))) {
1079 ret = -EINVAL;
1080 goto out;
1081 }
1082
1083 /* If monitoring is active, update counter. */
1084 if (channel_subsys->chnmon_active) {
1085 css_update_chnmon(sch);
1086 }
1087
1088 s->ctrl |= SCSW_ACTL_RESUME_PEND;
56bf1a8e 1089 do_subchannel_work(sch, NULL);
df1fe5bb
CH
1090 ret = 0;
1091
1092out:
1093 return ret;
1094}
1095
1096int css_do_rchp(uint8_t cssid, uint8_t chpid)
1097{
1098 uint8_t real_cssid;
1099
1100 if (cssid > channel_subsys->max_cssid) {
1101 return -EINVAL;
1102 }
1103 if (channel_subsys->max_cssid == 0) {
1104 real_cssid = channel_subsys->default_cssid;
1105 } else {
1106 real_cssid = cssid;
1107 }
1108 if (!channel_subsys->css[real_cssid]) {
1109 return -EINVAL;
1110 }
1111
1112 if (!channel_subsys->css[real_cssid]->chpids[chpid].in_use) {
1113 return -ENODEV;
1114 }
1115
1116 if (!channel_subsys->css[real_cssid]->chpids[chpid].is_virtual) {
1117 fprintf(stderr,
1118 "rchp unsupported for non-virtual chpid %x.%02x!\n",
1119 real_cssid, chpid);
1120 return -ENODEV;
1121 }
1122
1123 /* We don't really use a channel path, so we're done here. */
1124 css_queue_crw(CRW_RSC_CHP, CRW_ERC_INIT,
1125 channel_subsys->max_cssid > 0 ? 1 : 0, chpid);
1126 if (channel_subsys->max_cssid > 0) {
1127 css_queue_crw(CRW_RSC_CHP, CRW_ERC_INIT, 0, real_cssid << 8);
1128 }
1129 return 0;
1130}
1131
38dd7cc7 1132bool css_schid_final(int m, uint8_t cssid, uint8_t ssid, uint16_t schid)
df1fe5bb
CH
1133{
1134 SubchSet *set;
38dd7cc7 1135 uint8_t real_cssid;
df1fe5bb 1136
38dd7cc7
CB
1137 real_cssid = (!m && (cssid == 0)) ? channel_subsys->default_cssid : cssid;
1138 if (real_cssid > MAX_CSSID || ssid > MAX_SSID ||
1139 !channel_subsys->css[real_cssid] ||
1140 !channel_subsys->css[real_cssid]->sch_set[ssid]) {
df1fe5bb
CH
1141 return true;
1142 }
38dd7cc7 1143 set = channel_subsys->css[real_cssid]->sch_set[ssid];
df1fe5bb
CH
1144 return schid > find_last_bit(set->schids_used,
1145 (MAX_SCHID + 1) / sizeof(unsigned long));
1146}
1147
1148static int css_add_virtual_chpid(uint8_t cssid, uint8_t chpid, uint8_t type)
1149{
1150 CssImage *css;
1151
1152 trace_css_chpid_add(cssid, chpid, type);
1153 if (cssid > MAX_CSSID) {
1154 return -EINVAL;
1155 }
1156 css = channel_subsys->css[cssid];
1157 if (!css) {
1158 return -EINVAL;
1159 }
1160 if (css->chpids[chpid].in_use) {
1161 return -EEXIST;
1162 }
1163 css->chpids[chpid].in_use = 1;
1164 css->chpids[chpid].type = type;
1165 css->chpids[chpid].is_virtual = 1;
1166
1167 css_generate_chp_crws(cssid, chpid);
1168
1169 return 0;
1170}
1171
1172void css_sch_build_virtual_schib(SubchDev *sch, uint8_t chpid, uint8_t type)
1173{
1174 PMCW *p = &sch->curr_status.pmcw;
1175 SCSW *s = &sch->curr_status.scsw;
1176 int i;
1177 CssImage *css = channel_subsys->css[sch->cssid];
1178
1179 assert(css != NULL);
1180 memset(p, 0, sizeof(PMCW));
1181 p->flags |= PMCW_FLAGS_MASK_DNV;
1182 p->devno = sch->devno;
1183 /* single path */
1184 p->pim = 0x80;
1185 p->pom = 0xff;
1186 p->pam = 0x80;
1187 p->chpid[0] = chpid;
1188 if (!css->chpids[chpid].in_use) {
1189 css_add_virtual_chpid(sch->cssid, chpid, type);
1190 }
1191
1192 memset(s, 0, sizeof(SCSW));
1193 sch->curr_status.mba = 0;
1194 for (i = 0; i < ARRAY_SIZE(sch->curr_status.mda); i++) {
1195 sch->curr_status.mda[i] = 0;
1196 }
1197}
1198
1199SubchDev *css_find_subch(uint8_t m, uint8_t cssid, uint8_t ssid, uint16_t schid)
1200{
1201 uint8_t real_cssid;
1202
1203 real_cssid = (!m && (cssid == 0)) ? channel_subsys->default_cssid : cssid;
1204
1205 if (!channel_subsys->css[real_cssid]) {
1206 return NULL;
1207 }
1208
1209 if (!channel_subsys->css[real_cssid]->sch_set[ssid]) {
1210 return NULL;
1211 }
1212
1213 return channel_subsys->css[real_cssid]->sch_set[ssid]->sch[schid];
1214}
1215
1216bool css_subch_visible(SubchDev *sch)
1217{
1218 if (sch->ssid > channel_subsys->max_ssid) {
1219 return false;
1220 }
1221
1222 if (sch->cssid != channel_subsys->default_cssid) {
1223 return (channel_subsys->max_cssid > 0);
1224 }
1225
1226 return true;
1227}
1228
1229bool css_present(uint8_t cssid)
1230{
1231 return (channel_subsys->css[cssid] != NULL);
1232}
1233
1234bool css_devno_used(uint8_t cssid, uint8_t ssid, uint16_t devno)
1235{
1236 if (!channel_subsys->css[cssid]) {
1237 return false;
1238 }
1239 if (!channel_subsys->css[cssid]->sch_set[ssid]) {
1240 return false;
1241 }
1242
1243 return !!test_bit(devno,
1244 channel_subsys->css[cssid]->sch_set[ssid]->devnos_used);
1245}
1246
1247void css_subch_assign(uint8_t cssid, uint8_t ssid, uint16_t schid,
1248 uint16_t devno, SubchDev *sch)
1249{
1250 CssImage *css;
1251 SubchSet *s_set;
1252
1253 trace_css_assign_subch(sch ? "assign" : "deassign", cssid, ssid, schid,
1254 devno);
1255 if (!channel_subsys->css[cssid]) {
1256 fprintf(stderr,
1257 "Suspicious call to %s (%x.%x.%04x) for non-existing css!\n",
1258 __func__, cssid, ssid, schid);
1259 return;
1260 }
1261 css = channel_subsys->css[cssid];
1262
1263 if (!css->sch_set[ssid]) {
1264 css->sch_set[ssid] = g_malloc0(sizeof(SubchSet));
1265 }
1266 s_set = css->sch_set[ssid];
1267
1268 s_set->sch[schid] = sch;
1269 if (sch) {
1270 set_bit(schid, s_set->schids_used);
1271 set_bit(devno, s_set->devnos_used);
1272 } else {
1273 clear_bit(schid, s_set->schids_used);
1274 clear_bit(devno, s_set->devnos_used);
1275 }
1276}
1277
1278void css_queue_crw(uint8_t rsc, uint8_t erc, int chain, uint16_t rsid)
1279{
1280 CrwContainer *crw_cont;
1281
1282 trace_css_crw(rsc, erc, rsid, chain ? "(chained)" : "");
1283 /* TODO: Maybe use a static crw pool? */
1284 crw_cont = g_try_malloc0(sizeof(CrwContainer));
1285 if (!crw_cont) {
1286 channel_subsys->crws_lost = true;
1287 return;
1288 }
1289 crw_cont->crw.flags = (rsc << 8) | erc;
1290 if (chain) {
1291 crw_cont->crw.flags |= CRW_FLAGS_MASK_C;
1292 }
1293 crw_cont->crw.rsid = rsid;
1294 if (channel_subsys->crws_lost) {
1295 crw_cont->crw.flags |= CRW_FLAGS_MASK_R;
1296 channel_subsys->crws_lost = false;
1297 }
1298
1299 QTAILQ_INSERT_TAIL(&channel_subsys->pending_crws, crw_cont, sibling);
1300
1301 if (channel_subsys->do_crw_mchk) {
df1fe5bb
CH
1302 channel_subsys->do_crw_mchk = false;
1303 /* Inject crw pending machine check. */
de13d216 1304 s390_crw_mchk();
df1fe5bb
CH
1305 }
1306}
1307
1308void css_generate_sch_crws(uint8_t cssid, uint8_t ssid, uint16_t schid,
1309 int hotplugged, int add)
1310{
1311 uint8_t guest_cssid;
1312 bool chain_crw;
1313
1314 if (add && !hotplugged) {
1315 return;
1316 }
1317 if (channel_subsys->max_cssid == 0) {
1318 /* Default cssid shows up as 0. */
1319 guest_cssid = (cssid == channel_subsys->default_cssid) ? 0 : cssid;
1320 } else {
1321 /* Show real cssid to the guest. */
1322 guest_cssid = cssid;
1323 }
1324 /*
1325 * Only notify for higher subchannel sets/channel subsystems if the
1326 * guest has enabled it.
1327 */
1328 if ((ssid > channel_subsys->max_ssid) ||
1329 (guest_cssid > channel_subsys->max_cssid) ||
1330 ((channel_subsys->max_cssid == 0) &&
1331 (cssid != channel_subsys->default_cssid))) {
1332 return;
1333 }
1334 chain_crw = (channel_subsys->max_ssid > 0) ||
1335 (channel_subsys->max_cssid > 0);
1336 css_queue_crw(CRW_RSC_SUBCH, CRW_ERC_IPI, chain_crw ? 1 : 0, schid);
1337 if (chain_crw) {
1338 css_queue_crw(CRW_RSC_SUBCH, CRW_ERC_IPI, 0,
1339 (guest_cssid << 8) | (ssid << 4));
1340 }
1341}
1342
1343void css_generate_chp_crws(uint8_t cssid, uint8_t chpid)
1344{
1345 /* TODO */
1346}
1347
8cba80c3
FB
1348void css_generate_css_crws(uint8_t cssid)
1349{
1350 css_queue_crw(CRW_RSC_CSS, 0, 0, cssid);
1351}
1352
df1fe5bb
CH
1353int css_enable_mcsse(void)
1354{
1355 trace_css_enable_facility("mcsse");
1356 channel_subsys->max_cssid = MAX_CSSID;
1357 return 0;
1358}
1359
1360int css_enable_mss(void)
1361{
1362 trace_css_enable_facility("mss");
1363 channel_subsys->max_ssid = MAX_SSID;
1364 return 0;
1365}
1366
bcb2b582
JF
1367void subch_device_save(SubchDev *s, QEMUFile *f)
1368{
1369 int i;
1370
1371 qemu_put_byte(f, s->cssid);
1372 qemu_put_byte(f, s->ssid);
1373 qemu_put_be16(f, s->schid);
1374 qemu_put_be16(f, s->devno);
1375 qemu_put_byte(f, s->thinint_active);
1376 /* SCHIB */
1377 /* PMCW */
1378 qemu_put_be32(f, s->curr_status.pmcw.intparm);
1379 qemu_put_be16(f, s->curr_status.pmcw.flags);
1380 qemu_put_be16(f, s->curr_status.pmcw.devno);
1381 qemu_put_byte(f, s->curr_status.pmcw.lpm);
1382 qemu_put_byte(f, s->curr_status.pmcw.pnom);
1383 qemu_put_byte(f, s->curr_status.pmcw.lpum);
1384 qemu_put_byte(f, s->curr_status.pmcw.pim);
1385 qemu_put_be16(f, s->curr_status.pmcw.mbi);
1386 qemu_put_byte(f, s->curr_status.pmcw.pom);
1387 qemu_put_byte(f, s->curr_status.pmcw.pam);
1388 qemu_put_buffer(f, s->curr_status.pmcw.chpid, 8);
1389 qemu_put_be32(f, s->curr_status.pmcw.chars);
1390 /* SCSW */
1391 qemu_put_be16(f, s->curr_status.scsw.flags);
1392 qemu_put_be16(f, s->curr_status.scsw.ctrl);
1393 qemu_put_be32(f, s->curr_status.scsw.cpa);
1394 qemu_put_byte(f, s->curr_status.scsw.dstat);
1395 qemu_put_byte(f, s->curr_status.scsw.cstat);
1396 qemu_put_be16(f, s->curr_status.scsw.count);
1397 qemu_put_be64(f, s->curr_status.mba);
1398 qemu_put_buffer(f, s->curr_status.mda, 4);
1399 /* end SCHIB */
1400 qemu_put_buffer(f, s->sense_data, 32);
1401 qemu_put_be64(f, s->channel_prog);
1402 /* last cmd */
1403 qemu_put_byte(f, s->last_cmd.cmd_code);
1404 qemu_put_byte(f, s->last_cmd.flags);
1405 qemu_put_be16(f, s->last_cmd.count);
1406 qemu_put_be32(f, s->last_cmd.cda);
1407 qemu_put_byte(f, s->last_cmd_valid);
1408 qemu_put_byte(f, s->id.reserved);
1409 qemu_put_be16(f, s->id.cu_type);
1410 qemu_put_byte(f, s->id.cu_model);
1411 qemu_put_be16(f, s->id.dev_type);
1412 qemu_put_byte(f, s->id.dev_model);
1413 qemu_put_byte(f, s->id.unused);
1414 for (i = 0; i < ARRAY_SIZE(s->id.ciw); i++) {
1415 qemu_put_byte(f, s->id.ciw[i].type);
1416 qemu_put_byte(f, s->id.ciw[i].command);
1417 qemu_put_be16(f, s->id.ciw[i].count);
1418 }
a327c921 1419 qemu_put_byte(f, s->ccw_fmt_1);
e8601dd5 1420 qemu_put_byte(f, s->ccw_no_data_cnt);
bcb2b582
JF
1421 return;
1422}
1423
1424int subch_device_load(SubchDev *s, QEMUFile *f)
1425{
1426 int i;
1427
1428 s->cssid = qemu_get_byte(f);
1429 s->ssid = qemu_get_byte(f);
1430 s->schid = qemu_get_be16(f);
1431 s->devno = qemu_get_be16(f);
1432 s->thinint_active = qemu_get_byte(f);
1433 /* SCHIB */
1434 /* PMCW */
1435 s->curr_status.pmcw.intparm = qemu_get_be32(f);
1436 s->curr_status.pmcw.flags = qemu_get_be16(f);
1437 s->curr_status.pmcw.devno = qemu_get_be16(f);
1438 s->curr_status.pmcw.lpm = qemu_get_byte(f);
1439 s->curr_status.pmcw.pnom = qemu_get_byte(f);
1440 s->curr_status.pmcw.lpum = qemu_get_byte(f);
1441 s->curr_status.pmcw.pim = qemu_get_byte(f);
1442 s->curr_status.pmcw.mbi = qemu_get_be16(f);
1443 s->curr_status.pmcw.pom = qemu_get_byte(f);
1444 s->curr_status.pmcw.pam = qemu_get_byte(f);
1445 qemu_get_buffer(f, s->curr_status.pmcw.chpid, 8);
1446 s->curr_status.pmcw.chars = qemu_get_be32(f);
1447 /* SCSW */
1448 s->curr_status.scsw.flags = qemu_get_be16(f);
1449 s->curr_status.scsw.ctrl = qemu_get_be16(f);
1450 s->curr_status.scsw.cpa = qemu_get_be32(f);
1451 s->curr_status.scsw.dstat = qemu_get_byte(f);
1452 s->curr_status.scsw.cstat = qemu_get_byte(f);
1453 s->curr_status.scsw.count = qemu_get_be16(f);
1454 s->curr_status.mba = qemu_get_be64(f);
1455 qemu_get_buffer(f, s->curr_status.mda, 4);
1456 /* end SCHIB */
1457 qemu_get_buffer(f, s->sense_data, 32);
1458 s->channel_prog = qemu_get_be64(f);
1459 /* last cmd */
1460 s->last_cmd.cmd_code = qemu_get_byte(f);
1461 s->last_cmd.flags = qemu_get_byte(f);
1462 s->last_cmd.count = qemu_get_be16(f);
1463 s->last_cmd.cda = qemu_get_be32(f);
1464 s->last_cmd_valid = qemu_get_byte(f);
1465 s->id.reserved = qemu_get_byte(f);
1466 s->id.cu_type = qemu_get_be16(f);
1467 s->id.cu_model = qemu_get_byte(f);
1468 s->id.dev_type = qemu_get_be16(f);
1469 s->id.dev_model = qemu_get_byte(f);
1470 s->id.unused = qemu_get_byte(f);
1471 for (i = 0; i < ARRAY_SIZE(s->id.ciw); i++) {
1472 s->id.ciw[i].type = qemu_get_byte(f);
1473 s->id.ciw[i].command = qemu_get_byte(f);
1474 s->id.ciw[i].count = qemu_get_be16(f);
1475 }
a327c921 1476 s->ccw_fmt_1 = qemu_get_byte(f);
e8601dd5 1477 s->ccw_no_data_cnt = qemu_get_byte(f);
ec7353a1
CH
1478 /*
1479 * Hack alert. We don't migrate the channel subsystem status (no
1480 * device!), but we need to find out if the guest enabled mss/mcss-e.
1481 * If the subchannel is enabled, it certainly was able to access it,
1482 * so adjust the max_ssid/max_cssid values for relevant ssid/cssid
1483 * values. This is not watertight, but better than nothing.
1484 */
1485 if (s->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA) {
1486 if (s->ssid) {
1487 channel_subsys->max_ssid = MAX_SSID;
1488 }
1489 if (s->cssid != channel_subsys->default_cssid) {
1490 channel_subsys->max_cssid = MAX_CSSID;
1491 }
1492 }
bcb2b582
JF
1493 return 0;
1494}
1495
1496
df1fe5bb
CH
1497static void css_init(void)
1498{
1499 channel_subsys = g_malloc0(sizeof(*channel_subsys));
1500 QTAILQ_INIT(&channel_subsys->pending_crws);
1501 channel_subsys->do_crw_mchk = true;
1502 channel_subsys->crws_lost = false;
1503 channel_subsys->chnmon_active = false;
03cf077a 1504 QTAILQ_INIT(&channel_subsys->io_adapters);
df1fe5bb
CH
1505}
1506machine_init(css_init);
1507
1508void css_reset_sch(SubchDev *sch)
1509{
1510 PMCW *p = &sch->curr_status.pmcw;
1511
62ac4a52
TH
1512 if ((p->flags & PMCW_FLAGS_MASK_ENA) != 0 && sch->disable_cb) {
1513 sch->disable_cb(sch);
1514 }
1515
df1fe5bb
CH
1516 p->intparm = 0;
1517 p->flags &= ~(PMCW_FLAGS_MASK_ISC | PMCW_FLAGS_MASK_ENA |
1518 PMCW_FLAGS_MASK_LM | PMCW_FLAGS_MASK_MME |
1519 PMCW_FLAGS_MASK_MP | PMCW_FLAGS_MASK_TF);
1520 p->flags |= PMCW_FLAGS_MASK_DNV;
1521 p->devno = sch->devno;
1522 p->pim = 0x80;
1523 p->lpm = p->pim;
1524 p->pnom = 0;
1525 p->lpum = 0;
1526 p->mbi = 0;
1527 p->pom = 0xff;
1528 p->pam = 0x80;
1529 p->chars &= ~(PMCW_CHARS_MASK_MBFC | PMCW_CHARS_MASK_XMWME |
1530 PMCW_CHARS_MASK_CSENSE);
1531
1532 memset(&sch->curr_status.scsw, 0, sizeof(sch->curr_status.scsw));
1533 sch->curr_status.mba = 0;
1534
1535 sch->channel_prog = 0x0;
1536 sch->last_cmd_valid = false;
7e749462 1537 sch->thinint_active = false;
df1fe5bb
CH
1538}
1539
1540void css_reset(void)
1541{
1542 CrwContainer *crw_cont;
1543
1544 /* Clean up monitoring. */
1545 channel_subsys->chnmon_active = false;
1546 channel_subsys->chnmon_area = 0;
1547
1548 /* Clear pending CRWs. */
1549 while ((crw_cont = QTAILQ_FIRST(&channel_subsys->pending_crws))) {
1550 QTAILQ_REMOVE(&channel_subsys->pending_crws, crw_cont, sibling);
1551 g_free(crw_cont);
1552 }
1553 channel_subsys->do_crw_mchk = true;
1554 channel_subsys->crws_lost = false;
1555
1556 /* Reset maximum ids. */
1557 channel_subsys->max_cssid = 0;
1558 channel_subsys->max_ssid = 0;
1559}
This page took 0.375957 seconds and 4 git commands to generate.