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
13 #include "virtio-scsi.h"
15 #define VRING_WAIT_REPLY_TIMEOUT 3
17 static VRing block
[VIRTIO_MAX_VQS
];
18 static char ring_area
[VIRTIO_RING_SIZE
* VIRTIO_MAX_VQS
]
19 __attribute__((__aligned__(PAGE_SIZE
)));
21 static char chsc_page
[PAGE_SIZE
] __attribute__((__aligned__(PAGE_SIZE
)));
27 .ring_area
= ring_area
,
28 .wait_reply_timeout
= VRING_WAIT_REPLY_TIMEOUT
,
29 .schid
= { .one
= 1 },
30 .scsi_block_size
= VIRTIO_SCSI_BLOCK_SIZE
,
34 VDev
*virtio_get_device(void)
39 VirtioDevType
virtio_get_device_type(void)
41 return vdev
.senseid
.cu_model
;
44 /* virtio spec v1.0 para 4.3.3.2 */
45 static long kvm_hypercall(unsigned long nr
, unsigned long param1
,
46 unsigned long param2
, unsigned long param3
)
48 register ulong r_nr
asm("1") = nr
;
49 register ulong r_param1
asm("2") = param1
;
50 register ulong r_param2
asm("3") = param2
;
51 register ulong r_param3
asm("4") = param3
;
52 register long retval
asm("2");
54 asm volatile ("diag 2,4,0x500"
56 : "d" (r_nr
), "0" (r_param1
), "r"(r_param2
), "d"(r_param3
)
62 static long virtio_notify(SubChannelId schid
, int vq_idx
, long cookie
)
64 return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY
, *(u32
*)&schid
,
68 /***********************************************
70 ***********************************************/
72 static int drain_irqs(SubChannelId schid
)
78 /* FIXME: make use of TPI, for that enable subchannel and isc */
79 if (tsch(schid
, &irb
)) {
80 /* Might want to differentiate error codes later on. */
83 } else if (irb
.scsw
.dstat
!= 0xc) {
91 static int run_ccw(VDev
*vdev
, int cmd
, void *ptr
, int len
)
98 /* start command processing */
99 stsch_err(vdev
->schid
, &schib
);
100 schib
.scsw
.ctrl
= SCSW_FCTL_START_FUNC
;
101 msch(vdev
->schid
, &schib
);
103 /* start subchannel command */
105 orb
.cpa
= (u32
)(long)&ccw
;
112 r
= ssch(vdev
->schid
, &orb
);
114 * XXX Wait until device is done processing the CCW. For now we can
115 * assume that a simple tsch will have finished the CCW processing,
116 * but the architecture allows for asynchronous operation
119 r
= drain_irqs(vdev
->schid
);
124 static void vring_init(VRing
*vr
, VqInfo
*info
)
126 void *p
= (void *) info
->queue
;
128 debug_print_addr("init p", p
);
129 vr
->id
= info
->index
;
132 vr
->avail
= p
+ info
->num
* sizeof(VRingDesc
);
133 vr
->used
= (void *)(((unsigned long)&vr
->avail
->ring
[info
->num
]
134 + info
->align
- 1) & ~(info
->align
- 1));
136 /* Zero out all relevant field */
137 vr
->avail
->flags
= 0;
140 /* We're running with interrupts off anyways, so don't bother */
141 vr
->used
->flags
= VRING_USED_F_NO_NOTIFY
;
147 debug_print_addr("init vr", vr
);
150 static bool vring_notify(VRing
*vr
)
152 vr
->cookie
= virtio_notify(vr
->schid
, vr
->id
, vr
->cookie
);
153 return vr
->cookie
>= 0;
156 static void vring_send_buf(VRing
*vr
, void *p
, int len
, int flags
)
158 /* For follow-up chains we need to keep the first entry point */
159 if (!(flags
& VRING_HIDDEN_IS_CHAIN
)) {
160 vr
->avail
->ring
[vr
->avail
->idx
% vr
->num
] = vr
->next_idx
;
163 vr
->desc
[vr
->next_idx
].addr
= (ulong
)p
;
164 vr
->desc
[vr
->next_idx
].len
= len
;
165 vr
->desc
[vr
->next_idx
].flags
= flags
& ~VRING_HIDDEN_IS_CHAIN
;
166 vr
->desc
[vr
->next_idx
].next
= vr
->next_idx
;
167 vr
->desc
[vr
->next_idx
].next
++;
170 /* Chains only have a single ID */
171 if (!(flags
& VRING_DESC_F_NEXT
)) {
176 static u64
get_clock(void)
180 asm volatile("stck %0" : "=Q" (r
) : : "cc");
184 ulong
get_second(void)
186 return (get_clock() >> 12) / 1000000;
189 static int vr_poll(VRing
*vr
)
191 if (vr
->used
->idx
== vr
->used_idx
) {
197 vr
->used_idx
= vr
->used
->idx
;
200 vr
->desc
[0].flags
= 0;
201 return 1; /* vr has been updated */
205 * Wait for the host to reply.
207 * timeout is in seconds if > 0.
209 * Returns 0 on success, 1 on timeout.
211 static int vring_wait_reply(void)
213 ulong target_second
= get_second() + vdev
.wait_reply_timeout
;
215 /* Wait for any queue to be updated by the host */
219 for (i
= 0; i
< vdev
.nr_vqs
; i
++) {
220 r
+= vr_poll(&vdev
.vrings
[i
]);
226 } while (!vdev
.wait_reply_timeout
|| (get_second() < target_second
));
231 int virtio_run(VDev
*vdev
, int vqid
, VirtioCmd
*cmd
)
233 VRing
*vr
= &vdev
->vrings
[vqid
];
237 vring_send_buf(vr
, cmd
[i
].data
, cmd
[i
].size
,
238 cmd
[i
].flags
| (i
? VRING_HIDDEN_IS_CHAIN
: 0));
239 } while (cmd
[i
++].flags
& VRING_DESC_F_NEXT
);
242 if (drain_irqs(vr
->schid
)) {
248 /***********************************************
250 ***********************************************/
252 static int virtio_blk_read_many(VDev
*vdev
,
253 ulong sector
, void *load_addr
, int sec_num
)
255 VirtioBlkOuthdr out_hdr
;
257 VRing
*vr
= &vdev
->vrings
[vdev
->cmd_vr_idx
];
259 /* Tell the host we want to read */
260 out_hdr
.type
= VIRTIO_BLK_T_IN
;
262 out_hdr
.sector
= virtio_sector_adjust(sector
);
264 vring_send_buf(vr
, &out_hdr
, sizeof(out_hdr
), VRING_DESC_F_NEXT
);
266 /* This is where we want to receive data */
267 vring_send_buf(vr
, load_addr
, virtio_get_block_size() * sec_num
,
268 VRING_DESC_F_WRITE
| VRING_HIDDEN_IS_CHAIN
|
272 vring_send_buf(vr
, &status
, sizeof(u8
),
273 VRING_DESC_F_WRITE
| VRING_HIDDEN_IS_CHAIN
);
275 /* Now we can tell the host to read */
278 if (drain_irqs(vr
->schid
)) {
279 /* Well, whatever status is supposed to contain... */
285 int virtio_read_many(ulong sector
, void *load_addr
, int sec_num
)
287 switch (vdev
.senseid
.cu_model
) {
288 case VIRTIO_ID_BLOCK
:
289 return virtio_blk_read_many(&vdev
, sector
, load_addr
, sec_num
);
291 return virtio_scsi_read_many(&vdev
, sector
, load_addr
, sec_num
);
293 panic("\n! No readable IPL device !\n");
297 unsigned long virtio_load_direct(ulong rec_list1
, ulong rec_list2
,
298 ulong subchan_id
, void *load_addr
)
302 int sec_num
= ((rec_list2
>> 32) & 0xffff) + 1;
303 int sec_len
= rec_list2
>> 48;
304 ulong addr
= (ulong
)load_addr
;
306 if (sec_len
!= virtio_get_block_size()) {
311 status
= virtio_read_many(sec
, (void *)addr
, sec_num
);
315 addr
+= sec_num
* virtio_get_block_size();
320 int virtio_read(ulong sector
, void *load_addr
)
322 return virtio_read_many(sector
, load_addr
, 1);
326 * Other supported value pairs, if any, would need to be added here.
327 * Note: head count is always 15.
329 static inline u8
virtio_eckd_sectors_for_block_size(int size
)
344 VirtioGDN
virtio_guessed_disk_nature(void)
346 return vdev
.guessed_disk_nature
;
349 void virtio_assume_scsi(void)
351 switch (vdev
.senseid
.cu_model
) {
352 case VIRTIO_ID_BLOCK
:
353 vdev
.guessed_disk_nature
= VIRTIO_GDN_SCSI
;
354 vdev
.config
.blk
.blk_size
= VIRTIO_SCSI_BLOCK_SIZE
;
355 vdev
.config
.blk
.physical_block_exp
= 0;
359 vdev
.scsi_block_size
= VIRTIO_SCSI_BLOCK_SIZE
;
364 void virtio_assume_iso9660(void)
366 switch (vdev
.senseid
.cu_model
) {
367 case VIRTIO_ID_BLOCK
:
368 vdev
.guessed_disk_nature
= VIRTIO_GDN_SCSI
;
369 vdev
.config
.blk
.blk_size
= VIRTIO_ISO_BLOCK_SIZE
;
370 vdev
.config
.blk
.physical_block_exp
= 0;
371 vdev
.blk_factor
= VIRTIO_ISO_BLOCK_SIZE
/ VIRTIO_SECTOR_SIZE
;
374 vdev
.scsi_block_size
= VIRTIO_ISO_BLOCK_SIZE
;
379 void virtio_assume_eckd(void)
381 vdev
.guessed_disk_nature
= VIRTIO_GDN_DASD
;
383 vdev
.config
.blk
.physical_block_exp
= 0;
384 switch (vdev
.senseid
.cu_model
) {
385 case VIRTIO_ID_BLOCK
:
386 vdev
.config
.blk
.blk_size
= 4096;
389 vdev
.config
.blk
.blk_size
= vdev
.scsi_block_size
;
392 vdev
.config
.blk
.geometry
.heads
= 15;
393 vdev
.config
.blk
.geometry
.sectors
=
394 virtio_eckd_sectors_for_block_size(vdev
.config
.blk
.blk_size
);
397 bool virtio_disk_is_scsi(void)
399 if (vdev
.guessed_disk_nature
== VIRTIO_GDN_SCSI
) {
402 switch (vdev
.senseid
.cu_model
) {
403 case VIRTIO_ID_BLOCK
:
404 return (vdev
.config
.blk
.geometry
.heads
== 255)
405 && (vdev
.config
.blk
.geometry
.sectors
== 63)
406 && (virtio_get_block_size() == VIRTIO_SCSI_BLOCK_SIZE
);
413 bool virtio_disk_is_eckd(void)
415 const int block_size
= virtio_get_block_size();
417 if (vdev
.guessed_disk_nature
== VIRTIO_GDN_DASD
) {
420 switch (vdev
.senseid
.cu_model
) {
421 case VIRTIO_ID_BLOCK
:
422 return (vdev
.config
.blk
.geometry
.heads
== 15)
423 && (vdev
.config
.blk
.geometry
.sectors
==
424 virtio_eckd_sectors_for_block_size(block_size
));
431 bool virtio_ipl_disk_is_valid(void)
433 return virtio_disk_is_scsi() || virtio_disk_is_eckd();
436 int virtio_get_block_size(void)
438 switch (vdev
.senseid
.cu_model
) {
439 case VIRTIO_ID_BLOCK
:
440 return vdev
.config
.blk
.blk_size
<< vdev
.config
.blk
.physical_block_exp
;
442 return vdev
.scsi_block_size
;
447 uint8_t virtio_get_heads(void)
449 switch (vdev
.senseid
.cu_model
) {
450 case VIRTIO_ID_BLOCK
:
451 return vdev
.config
.blk
.geometry
.heads
;
453 return vdev
.guessed_disk_nature
== VIRTIO_GDN_DASD
454 ? vdev
.config
.blk
.geometry
.heads
: 255;
459 uint8_t virtio_get_sectors(void)
461 switch (vdev
.senseid
.cu_model
) {
462 case VIRTIO_ID_BLOCK
:
463 return vdev
.config
.blk
.geometry
.sectors
;
465 return vdev
.guessed_disk_nature
== VIRTIO_GDN_DASD
466 ? vdev
.config
.blk
.geometry
.sectors
: 63;
471 uint64_t virtio_get_blocks(void)
473 const uint64_t factor
= virtio_get_block_size() / VIRTIO_SECTOR_SIZE
;
474 switch (vdev
.senseid
.cu_model
) {
475 case VIRTIO_ID_BLOCK
:
476 return vdev
.config
.blk
.capacity
/ factor
;
478 return vdev
.scsi_last_block
/ factor
;
483 static void virtio_setup_ccw(VDev
*vdev
)
486 unsigned char status
= VIRTIO_CONFIG_S_DRIVER_OK
;
488 IPL_assert(virtio_is_supported(vdev
->schid
), "PE");
489 /* device ID has been established now */
491 vdev
->config
.blk
.blk_size
= 0; /* mark "illegal" - setup started... */
492 vdev
->guessed_disk_nature
= VIRTIO_GDN_NONE
;
494 run_ccw(vdev
, CCW_CMD_VDEV_RESET
, NULL
, 0);
496 switch (vdev
->senseid
.cu_model
) {
497 case VIRTIO_ID_BLOCK
:
499 vdev
->cmd_vr_idx
= 0;
500 cfg_size
= sizeof(vdev
->config
.blk
);
504 vdev
->cmd_vr_idx
= VR_REQUEST
;
505 cfg_size
= sizeof(vdev
->config
.scsi
);
508 panic("Unsupported virtio device\n");
510 IPL_assert(run_ccw(vdev
, CCW_CMD_READ_CONF
, &vdev
->config
, cfg_size
) == 0,
511 "Could not get block device configuration");
514 * Skipping CCW_CMD_READ_FEAT. We're not doing anything fancy, and
515 * we'll just stop dead anyway if anything does not work like we
519 for (i
= 0; i
< vdev
->nr_vqs
; i
++) {
521 .queue
= (unsigned long long) ring_area
+ (i
* VIRTIO_RING_SIZE
),
522 .align
= KVM_S390_VIRTIO_RING_ALIGN
,
532 run_ccw(vdev
, CCW_CMD_READ_VQ_CONF
, &config
, sizeof(config
)) == 0,
533 "Could not get block device VQ configuration");
534 info
.num
= config
.num
;
535 vring_init(&vdev
->vrings
[i
], &info
);
536 vdev
->vrings
[i
].schid
= vdev
->schid
;
537 IPL_assert(run_ccw(vdev
, CCW_CMD_SET_VQ
, &info
, sizeof(info
)) == 0,
538 "Cannot set VQ info");
541 run_ccw(vdev
, CCW_CMD_WRITE_STATUS
, &status
, sizeof(status
)) == 0,
542 "Could not write status to host");
545 void virtio_setup_device(SubChannelId schid
)
548 virtio_setup_ccw(&vdev
);
550 switch (vdev
.senseid
.cu_model
) {
551 case VIRTIO_ID_BLOCK
:
552 sclp_print("Using virtio-blk.\n");
553 if (!virtio_ipl_disk_is_valid()) {
554 /* make sure all getters but blocksize return 0 for
557 memset(&vdev
.config
.blk
, 0, sizeof(vdev
.config
.blk
));
558 virtio_assume_scsi();
562 IPL_assert(vdev
.config
.scsi
.sense_size
== VIRTIO_SCSI_SENSE_SIZE
,
563 "Config: sense size mismatch");
564 IPL_assert(vdev
.config
.scsi
.cdb_size
== VIRTIO_SCSI_CDB_SIZE
,
565 "Config: CDB size mismatch");
567 sclp_print("Using virtio-scsi.\n");
568 virtio_scsi_setup(&vdev
);
571 panic("\n! No IPL device available !\n");
575 bool virtio_is_supported(SubChannelId schid
)
578 memset(&vdev
.senseid
, 0, sizeof(vdev
.senseid
));
579 /* run sense id command */
580 if (run_ccw(&vdev
, CCW_CMD_SENSE_ID
, &vdev
.senseid
, sizeof(vdev
.senseid
))) {
583 if (vdev
.senseid
.cu_type
== 0x3832) {
584 switch (vdev
.senseid
.cu_model
) {
585 case VIRTIO_ID_BLOCK
:
593 int enable_mss_facility(void)
596 ChscAreaSda
*sda_area
= (ChscAreaSda
*) chsc_page
;
598 memset(sda_area
, 0, PAGE_SIZE
);
599 sda_area
->request
.length
= 0x0400;
600 sda_area
->request
.code
= 0x0031;
601 sda_area
->operation_code
= 0x2;
603 ret
= chsc(sda_area
);
604 if ((ret
== 0) && (sda_area
->response
.code
== 0x0001)) {