130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#ifndef _LINUX_VIRTIO_BLK_H
230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define _LINUX_VIRTIO_BLK_H
330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* This header is BSD licensed so anyone can use the definitions to implement
430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * compatible drivers/servers.
530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Redistribution and use in source and binary forms, with or without
730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * modification, are permitted provided that the following conditions
830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * are met:
930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * 1. Redistributions of source code must retain the above copyright
1030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *    notice, this list of conditions and the following disclaimer.
1130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * 2. Redistributions in binary form must reproduce the above copyright
1230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *    notice, this list of conditions and the following disclaimer in the
1330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *    documentation and/or other materials provided with the distribution.
1430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * 3. Neither the name of IBM nor the names of its contributors
1530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *    may be used to endorse or promote products derived from this software
1630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *    without specific prior written permission.
1730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
1830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
2130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * SUCH DAMAGE. */
2830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#include <linux/types.h>
2930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#include <linux/virtio_ids.h>
3030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#include <linux/virtio_config.h>
3130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
3230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* Feature bits */
3330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_BARRIER	0	/* Does host support barriers? */
3430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_SIZE_MAX	1	/* Indicates maximum segment size */
3530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_SEG_MAX	2	/* Indicates maximum # of segments */
3630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_GEOMETRY	4	/* Legacy geometry available  */
3730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_RO		5	/* Disk is read-only */
3830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_BLK_SIZE	6	/* Block size of disk is available*/
3930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_SCSI	7	/* Supports scsi command passthru */
4030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_WCE	9	/* Writeback mode enabled after reset */
4130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_TOPOLOGY	10	/* Topology information is available */
4230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_CONFIG_WCE	11	/* Writeback mode available in config */
4330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
4430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#ifndef __KERNEL__
4530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* Old (deprecated) name for VIRTIO_BLK_F_WCE. */
4630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_F_FLUSH VIRTIO_BLK_F_WCE
4730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#endif
4830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
4930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_ID_BYTES	20	/* ID string length */
5030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
5130692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct virtio_blk_config {
5230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* The capacity (in 512-byte sectors). */
5330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u64 capacity;
5430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
5530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 size_max;
5630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
5730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 seg_max;
5830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* geometry the device (if VIRTIO_BLK_F_GEOMETRY) */
5930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	struct virtio_blk_geometry {
6030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		__u16 cylinders;
6130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		__u8 heads;
6230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		__u8 sectors;
6330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	} geometry;
6430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
6530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
6630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 blk_size;
6730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
6830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY  */
6930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* exponent for physical block per logical block. */
7030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u8 physical_block_exp;
7130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* alignment offset in logical blocks. */
7230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u8 alignment_offset;
7330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* minimum I/O size without performance penalty in logical blocks. */
7430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u16 min_io_size;
7530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* optimal sustained I/O size in logical blocks. */
7630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 opt_io_size;
7730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
7830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
7930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u8 wce;
8030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng} __attribute__((packed));
8130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
8230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/*
8330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Command types
8430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
8530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Usage is a bit tricky as some bits are used as flags and some are not.
8630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
8730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Rules:
8830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
8930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
9030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *   and may not be combined with any of the other flags.
9130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
9230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
9330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* These two define direction. */
9430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_T_IN		0
9530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_T_OUT	1
9630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
9730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* This bit says it's a scsi command, not an actual read or write. */
9830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_T_SCSI_CMD	2
9930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
10030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* Cache flush command */
10130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_T_FLUSH	4
10230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
10330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* Get device ID command */
10430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_T_GET_ID    8
10530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
10630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* Barrier before this op. */
10730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_T_BARRIER	0x80000000
10830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
10930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* This is the first element of the read scatter-gather list. */
11030692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct virtio_blk_outhdr {
11130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* VIRTIO_BLK_T* */
11230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 type;
11330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* io priority. */
11430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 ioprio;
11530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* Sector (ie. 512 byte offset) */
11630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u64 sector;
11730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
11830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
11930692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct virtio_scsi_inhdr {
12030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 errors;
12130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 data_len;
12230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 sense_len;
12330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 residual;
12430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
12530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
12630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* And this is the final byte of the write scatter-gather list. */
12730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_S_OK		0
12830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_S_IOERR	1
12930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define VIRTIO_BLK_S_UNSUPP	2
13030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#endif /* _LINUX_VIRTIO_BLK_H */
131