4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
6 * This work is licensed under the terms of the GNU GPL, version 2 or (at
7 * your option) any later version. See the COPYING file in the top-level
15 #include "virtio-scsi.h"
18 #include "s390-time.h"
20 #define VRING_WAIT_REPLY_TIMEOUT 30
22 static VRing block
[VIRTIO_MAX_VQS
];
23 static char ring_area
[VIRTIO_RING_SIZE
* VIRTIO_MAX_VQS
]
24 __attribute__((__aligned__(PAGE_SIZE
)));
30 .ring_area
= ring_area
,
31 .wait_reply_timeout
= VRING_WAIT_REPLY_TIMEOUT
,
32 .schid
= { .one
= 1 },
33 .scsi_block_size
= VIRTIO_SCSI_BLOCK_SIZE
,
37 VDev
*virtio_get_device(void)
42 VirtioDevType
virtio_get_device_type(void)
44 return vdev
.senseid
.cu_model
;
47 /* virtio spec v1.0 para 4.3.3.2 */
48 static long kvm_hypercall(unsigned long nr
, unsigned long param1
,
49 unsigned long param2
, unsigned long param3
)
51 register ulong r_nr
asm("1") = nr
;
52 register ulong r_param1
asm("2") = param1
;
53 register ulong r_param2
asm("3") = param2
;
54 register ulong r_param3
asm("4") = param3
;
55 register long retval
asm("2");
57 asm volatile ("diag %%r2,%%r4,0x500"
59 : "d" (r_nr
), "0" (r_param1
), "r"(r_param2
), "d"(r_param3
)
65 static long virtio_notify(SubChannelId schid
, int vq_idx
, long cookie
)
67 return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY
, *(u32
*)&schid
,
71 /***********************************************
73 ***********************************************/
75 int drain_irqs(SubChannelId schid
)
81 /* FIXME: make use of TPI, for that enable subchannel and isc */
82 if (tsch(schid
, &irb
)) {
83 /* Might want to differentiate error codes later on. */
86 } else if (irb
.scsw
.dstat
!= 0xc) {
94 static int run_ccw(VDev
*vdev
, int cmd
, void *ptr
, int len
, bool sli
)
103 ccw
.flags
|= CCW_FLAG_SLI
;
106 return do_cio(vdev
->schid
, vdev
->senseid
.cu_type
, ptr2u32(&ccw
), CCW_FMT1
);
109 static void vring_init(VRing
*vr
, VqInfo
*info
)
111 void *p
= (void *) info
->queue
;
113 debug_print_addr("init p", p
);
114 vr
->id
= info
->index
;
117 vr
->avail
= p
+ info
->num
* sizeof(VRingDesc
);
118 vr
->used
= (void *)(((unsigned long)&vr
->avail
->ring
[info
->num
]
119 + info
->align
- 1) & ~(info
->align
- 1));
121 /* Zero out all relevant field */
122 vr
->avail
->flags
= 0;
125 /* We're running with interrupts off anyways, so don't bother */
126 vr
->used
->flags
= VRING_USED_F_NO_NOTIFY
;
132 debug_print_addr("init vr", vr
);
135 bool vring_notify(VRing
*vr
)
137 vr
->cookie
= virtio_notify(vr
->schid
, vr
->id
, vr
->cookie
);
138 return vr
->cookie
>= 0;
141 void vring_send_buf(VRing
*vr
, void *p
, int len
, int flags
)
143 /* For follow-up chains we need to keep the first entry point */
144 if (!(flags
& VRING_HIDDEN_IS_CHAIN
)) {
145 vr
->avail
->ring
[vr
->avail
->idx
% vr
->num
] = vr
->next_idx
;
148 vr
->desc
[vr
->next_idx
].addr
= (ulong
)p
;
149 vr
->desc
[vr
->next_idx
].len
= len
;
150 vr
->desc
[vr
->next_idx
].flags
= flags
& ~VRING_HIDDEN_IS_CHAIN
;
151 vr
->desc
[vr
->next_idx
].next
= vr
->next_idx
;
152 vr
->desc
[vr
->next_idx
].next
++;
155 /* Chains only have a single ID */
156 if (!(flags
& VRING_DESC_F_NEXT
)) {
161 int vr_poll(VRing
*vr
)
163 if (vr
->used
->idx
== vr
->used_idx
) {
169 vr
->used_idx
= vr
->used
->idx
;
172 vr
->desc
[0].flags
= 0;
173 return 1; /* vr has been updated */
177 * Wait for the host to reply.
179 * timeout is in seconds if > 0.
181 * Returns 0 on success, 1 on timeout.
183 int vring_wait_reply(void)
185 ulong target_second
= get_time_seconds() + vdev
.wait_reply_timeout
;
187 /* Wait for any queue to be updated by the host */
191 for (i
= 0; i
< vdev
.nr_vqs
; i
++) {
192 r
+= vr_poll(&vdev
.vrings
[i
]);
198 } while (!vdev
.wait_reply_timeout
|| (get_time_seconds() < target_second
));
203 int virtio_run(VDev
*vdev
, int vqid
, VirtioCmd
*cmd
)
205 VRing
*vr
= &vdev
->vrings
[vqid
];
209 vring_send_buf(vr
, cmd
[i
].data
, cmd
[i
].size
,
210 cmd
[i
].flags
| (i
? VRING_HIDDEN_IS_CHAIN
: 0));
211 } while (cmd
[i
++].flags
& VRING_DESC_F_NEXT
);
214 if (drain_irqs(vr
->schid
)) {
220 void virtio_setup_ccw(VDev
*vdev
)
222 int i
, rc
, cfg_size
= 0;
224 struct VirtioFeatureDesc
{
227 } __attribute__((packed
)) feats
;
229 IPL_assert(virtio_is_supported(vdev
->schid
), "PE");
230 /* device ID has been established now */
232 vdev
->config
.blk
.blk_size
= 0; /* mark "illegal" - setup started... */
233 vdev
->guessed_disk_nature
= VIRTIO_GDN_NONE
;
235 run_ccw(vdev
, CCW_CMD_VDEV_RESET
, NULL
, 0, false);
237 status
= VIRTIO_CONFIG_S_ACKNOWLEDGE
;
238 rc
= run_ccw(vdev
, CCW_CMD_WRITE_STATUS
, &status
, sizeof(status
), false);
239 IPL_assert(rc
== 0, "Could not write ACKNOWLEDGE status to host");
241 switch (vdev
->senseid
.cu_model
) {
244 vdev
->cmd_vr_idx
= 0;
245 cfg_size
= sizeof(vdev
->config
.net
);
247 case VIRTIO_ID_BLOCK
:
249 vdev
->cmd_vr_idx
= 0;
250 cfg_size
= sizeof(vdev
->config
.blk
);
254 vdev
->cmd_vr_idx
= VR_REQUEST
;
255 cfg_size
= sizeof(vdev
->config
.scsi
);
258 panic("Unsupported virtio device\n");
261 status
|= VIRTIO_CONFIG_S_DRIVER
;
262 rc
= run_ccw(vdev
, CCW_CMD_WRITE_STATUS
, &status
, sizeof(status
), false);
263 IPL_assert(rc
== 0, "Could not write DRIVER status to host");
265 /* Feature negotiation */
266 for (i
= 0; i
< ARRAY_SIZE(vdev
->guest_features
); i
++) {
269 rc
= run_ccw(vdev
, CCW_CMD_READ_FEAT
, &feats
, sizeof(feats
), false);
270 IPL_assert(rc
== 0, "Could not get features bits");
271 vdev
->guest_features
[i
] &= bswap32(feats
.features
);
272 feats
.features
= bswap32(vdev
->guest_features
[i
]);
273 rc
= run_ccw(vdev
, CCW_CMD_WRITE_FEAT
, &feats
, sizeof(feats
), false);
274 IPL_assert(rc
== 0, "Could not set features bits");
277 rc
= run_ccw(vdev
, CCW_CMD_READ_CONF
, &vdev
->config
, cfg_size
, false);
278 IPL_assert(rc
== 0, "Could not get virtio device configuration");
280 for (i
= 0; i
< vdev
->nr_vqs
; i
++) {
282 .queue
= (unsigned long long) ring_area
+ (i
* VIRTIO_RING_SIZE
),
283 .align
= KVM_S390_VIRTIO_RING_ALIGN
,
292 rc
= run_ccw(vdev
, CCW_CMD_READ_VQ_CONF
, &config
, sizeof(config
), false);
293 IPL_assert(rc
== 0, "Could not get virtio device VQ configuration");
294 info
.num
= config
.num
;
295 vring_init(&vdev
->vrings
[i
], &info
);
296 vdev
->vrings
[i
].schid
= vdev
->schid
;
298 run_ccw(vdev
, CCW_CMD_SET_VQ
, &info
, sizeof(info
), false) == 0,
299 "Cannot set VQ info");
302 status
|= VIRTIO_CONFIG_S_DRIVER_OK
;
303 rc
= run_ccw(vdev
, CCW_CMD_WRITE_STATUS
, &status
, sizeof(status
), false);
304 IPL_assert(rc
== 0, "Could not write DRIVER_OK status to host");
307 bool virtio_is_supported(SubChannelId schid
)
310 memset(&vdev
.senseid
, 0, sizeof(vdev
.senseid
));
313 * Run sense id command.
314 * The size of the senseid data differs between devices (notably,
315 * between virtio devices and dasds), so specify the largest possible
316 * size and suppress the incorrect length indication for smaller sizes.
318 if (run_ccw(&vdev
, CCW_CMD_SENSE_ID
, &vdev
.senseid
, sizeof(vdev
.senseid
),
322 if (vdev
.senseid
.cu_type
== 0x3832) {
323 switch (vdev
.senseid
.cu_model
) {
324 case VIRTIO_ID_BLOCK
: