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 #define VRING_WAIT_REPLY_TIMEOUT 30
20 static VRing block
[VIRTIO_MAX_VQS
];
21 static char ring_area
[VIRTIO_RING_SIZE
* VIRTIO_MAX_VQS
]
22 __attribute__((__aligned__(PAGE_SIZE
)));
28 .ring_area
= ring_area
,
29 .wait_reply_timeout
= VRING_WAIT_REPLY_TIMEOUT
,
30 .schid
= { .one
= 1 },
31 .scsi_block_size
= VIRTIO_SCSI_BLOCK_SIZE
,
35 VDev
*virtio_get_device(void)
40 VirtioDevType
virtio_get_device_type(void)
42 return vdev
.senseid
.cu_model
;
45 /* virtio spec v1.0 para 4.3.3.2 */
46 static long kvm_hypercall(unsigned long nr
, unsigned long param1
,
47 unsigned long param2
, unsigned long param3
)
49 register ulong r_nr
asm("1") = nr
;
50 register ulong r_param1
asm("2") = param1
;
51 register ulong r_param2
asm("3") = param2
;
52 register ulong r_param3
asm("4") = param3
;
53 register long retval
asm("2");
55 asm volatile ("diag 2,4,0x500"
57 : "d" (r_nr
), "0" (r_param1
), "r"(r_param2
), "d"(r_param3
)
63 static long virtio_notify(SubChannelId schid
, int vq_idx
, long cookie
)
65 return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY
, *(u32
*)&schid
,
69 /***********************************************
71 ***********************************************/
73 int drain_irqs(SubChannelId schid
)
79 /* FIXME: make use of TPI, for that enable subchannel and isc */
80 if (tsch(schid
, &irb
)) {
81 /* Might want to differentiate error codes later on. */
84 } else if (irb
.scsw
.dstat
!= 0xc) {
92 static int run_ccw(VDev
*vdev
, int cmd
, void *ptr
, int len
)
98 enable_subchannel(vdev
->schid
);
100 /* start subchannel command */
102 orb
.cpa
= (u32
)(long)&ccw
;
109 r
= ssch(vdev
->schid
, &orb
);
111 * XXX Wait until device is done processing the CCW. For now we can
112 * assume that a simple tsch will have finished the CCW processing,
113 * but the architecture allows for asynchronous operation
116 r
= drain_irqs(vdev
->schid
);
121 static void vring_init(VRing
*vr
, VqInfo
*info
)
123 void *p
= (void *) info
->queue
;
125 debug_print_addr("init p", p
);
126 vr
->id
= info
->index
;
129 vr
->avail
= p
+ info
->num
* sizeof(VRingDesc
);
130 vr
->used
= (void *)(((unsigned long)&vr
->avail
->ring
[info
->num
]
131 + info
->align
- 1) & ~(info
->align
- 1));
133 /* Zero out all relevant field */
134 vr
->avail
->flags
= 0;
137 /* We're running with interrupts off anyways, so don't bother */
138 vr
->used
->flags
= VRING_USED_F_NO_NOTIFY
;
144 debug_print_addr("init vr", vr
);
147 bool vring_notify(VRing
*vr
)
149 vr
->cookie
= virtio_notify(vr
->schid
, vr
->id
, vr
->cookie
);
150 return vr
->cookie
>= 0;
153 void vring_send_buf(VRing
*vr
, void *p
, int len
, int flags
)
155 /* For follow-up chains we need to keep the first entry point */
156 if (!(flags
& VRING_HIDDEN_IS_CHAIN
)) {
157 vr
->avail
->ring
[vr
->avail
->idx
% vr
->num
] = vr
->next_idx
;
160 vr
->desc
[vr
->next_idx
].addr
= (ulong
)p
;
161 vr
->desc
[vr
->next_idx
].len
= len
;
162 vr
->desc
[vr
->next_idx
].flags
= flags
& ~VRING_HIDDEN_IS_CHAIN
;
163 vr
->desc
[vr
->next_idx
].next
= vr
->next_idx
;
164 vr
->desc
[vr
->next_idx
].next
++;
167 /* Chains only have a single ID */
168 if (!(flags
& VRING_DESC_F_NEXT
)) {
177 asm volatile("stck %0" : "=Q" (r
) : : "cc");
181 ulong
get_second(void)
183 return (get_clock() >> 12) / 1000000;
186 int vr_poll(VRing
*vr
)
188 if (vr
->used
->idx
== vr
->used_idx
) {
194 vr
->used_idx
= vr
->used
->idx
;
197 vr
->desc
[0].flags
= 0;
198 return 1; /* vr has been updated */
202 * Wait for the host to reply.
204 * timeout is in seconds if > 0.
206 * Returns 0 on success, 1 on timeout.
208 int vring_wait_reply(void)
210 ulong target_second
= get_second() + vdev
.wait_reply_timeout
;
212 /* Wait for any queue to be updated by the host */
216 for (i
= 0; i
< vdev
.nr_vqs
; i
++) {
217 r
+= vr_poll(&vdev
.vrings
[i
]);
223 } while (!vdev
.wait_reply_timeout
|| (get_second() < target_second
));
228 int virtio_run(VDev
*vdev
, int vqid
, VirtioCmd
*cmd
)
230 VRing
*vr
= &vdev
->vrings
[vqid
];
234 vring_send_buf(vr
, cmd
[i
].data
, cmd
[i
].size
,
235 cmd
[i
].flags
| (i
? VRING_HIDDEN_IS_CHAIN
: 0));
236 } while (cmd
[i
++].flags
& VRING_DESC_F_NEXT
);
239 if (drain_irqs(vr
->schid
)) {
245 void virtio_setup_ccw(VDev
*vdev
)
247 int i
, rc
, cfg_size
= 0;
248 unsigned char status
= VIRTIO_CONFIG_S_DRIVER_OK
;
249 struct VirtioFeatureDesc
{
252 } __attribute__((packed
)) feats
;
254 IPL_assert(virtio_is_supported(vdev
->schid
), "PE");
255 /* device ID has been established now */
257 vdev
->config
.blk
.blk_size
= 0; /* mark "illegal" - setup started... */
258 vdev
->guessed_disk_nature
= VIRTIO_GDN_NONE
;
260 run_ccw(vdev
, CCW_CMD_VDEV_RESET
, NULL
, 0);
262 switch (vdev
->senseid
.cu_model
) {
265 vdev
->cmd_vr_idx
= 0;
266 cfg_size
= sizeof(vdev
->config
.net
);
268 case VIRTIO_ID_BLOCK
:
270 vdev
->cmd_vr_idx
= 0;
271 cfg_size
= sizeof(vdev
->config
.blk
);
275 vdev
->cmd_vr_idx
= VR_REQUEST
;
276 cfg_size
= sizeof(vdev
->config
.scsi
);
279 panic("Unsupported virtio device\n");
281 IPL_assert(run_ccw(vdev
, CCW_CMD_READ_CONF
, &vdev
->config
, cfg_size
) == 0,
282 "Could not get block device configuration");
284 /* Feature negotiation */
285 for (i
= 0; i
< ARRAY_SIZE(vdev
->guest_features
); i
++) {
288 rc
= run_ccw(vdev
, CCW_CMD_READ_FEAT
, &feats
, sizeof(feats
));
289 IPL_assert(rc
== 0, "Could not get features bits");
290 vdev
->guest_features
[i
] &= bswap32(feats
.features
);
291 feats
.features
= bswap32(vdev
->guest_features
[i
]);
292 rc
= run_ccw(vdev
, CCW_CMD_WRITE_FEAT
, &feats
, sizeof(feats
));
293 IPL_assert(rc
== 0, "Could not set features bits");
296 for (i
= 0; i
< vdev
->nr_vqs
; i
++) {
298 .queue
= (unsigned long long) ring_area
+ (i
* VIRTIO_RING_SIZE
),
299 .align
= KVM_S390_VIRTIO_RING_ALIGN
,
309 run_ccw(vdev
, CCW_CMD_READ_VQ_CONF
, &config
, sizeof(config
)) == 0,
310 "Could not get block device VQ configuration");
311 info
.num
= config
.num
;
312 vring_init(&vdev
->vrings
[i
], &info
);
313 vdev
->vrings
[i
].schid
= vdev
->schid
;
314 IPL_assert(run_ccw(vdev
, CCW_CMD_SET_VQ
, &info
, sizeof(info
)) == 0,
315 "Cannot set VQ info");
318 run_ccw(vdev
, CCW_CMD_WRITE_STATUS
, &status
, sizeof(status
)) == 0,
319 "Could not write status to host");
322 bool virtio_is_supported(SubChannelId schid
)
325 memset(&vdev
.senseid
, 0, sizeof(vdev
.senseid
));
326 /* run sense id command */
327 if (run_ccw(&vdev
, CCW_CMD_SENSE_ID
, &vdev
.senseid
, sizeof(vdev
.senseid
))) {
330 if (vdev
.senseid
.cu_type
== 0x3832) {
331 switch (vdev
.senseid
.cu_model
) {
332 case VIRTIO_ID_BLOCK
: