18c2ecf20Sopenharmony_ci#ifndef _LINUX_VIRTIO_BLK_H 28c2ecf20Sopenharmony_ci#define _LINUX_VIRTIO_BLK_H 38c2ecf20Sopenharmony_ci/* This header is BSD licensed so anyone can use the definitions to implement 48c2ecf20Sopenharmony_ci * compatible drivers/servers. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 78c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 88c2ecf20Sopenharmony_ci * are met: 98c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 108c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 118c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 128c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 138c2ecf20Sopenharmony_ci * documentation and/or other materials provided with the distribution. 148c2ecf20Sopenharmony_ci * 3. Neither the name of IBM nor the names of its contributors 158c2ecf20Sopenharmony_ci * may be used to endorse or promote products derived from this software 168c2ecf20Sopenharmony_ci * without specific prior written permission. 178c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 188c2ecf20Sopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198c2ecf20Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208c2ecf20Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 218c2ecf20Sopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228c2ecf20Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238c2ecf20Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248c2ecf20Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258c2ecf20Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268c2ecf20Sopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278c2ecf20Sopenharmony_ci * SUCH DAMAGE. */ 288c2ecf20Sopenharmony_ci#include <linux/types.h> 298c2ecf20Sopenharmony_ci#include <linux/virtio_ids.h> 308c2ecf20Sopenharmony_ci#include <linux/virtio_config.h> 318c2ecf20Sopenharmony_ci#include <linux/virtio_types.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/* Feature bits */ 348c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_SIZE_MAX 1 /* Indicates maximum segment size */ 358c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_SEG_MAX 2 /* Indicates maximum # of segments */ 368c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_GEOMETRY 4 /* Legacy geometry available */ 378c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_RO 5 /* Disk is read-only */ 388c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_BLK_SIZE 6 /* Block size of disk is available*/ 398c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_TOPOLOGY 10 /* Topology information is available */ 408c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_MQ 12 /* support more than one vq */ 418c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_DISCARD 13 /* DISCARD is supported */ 428c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_WRITE_ZEROES 14 /* WRITE ZEROES is supported */ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* Legacy feature bits */ 458c2ecf20Sopenharmony_ci#ifndef VIRTIO_BLK_NO_LEGACY 468c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_BARRIER 0 /* Does host support barriers? */ 478c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */ 488c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_FLUSH 9 /* Flush command supported */ 498c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */ 508c2ecf20Sopenharmony_ci#ifndef __KERNEL__ 518c2ecf20Sopenharmony_ci/* Old (deprecated) name for VIRTIO_BLK_F_FLUSH. */ 528c2ecf20Sopenharmony_ci#define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH 538c2ecf20Sopenharmony_ci#endif 548c2ecf20Sopenharmony_ci#endif /* !VIRTIO_BLK_NO_LEGACY */ 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci#define VIRTIO_BLK_ID_BYTES 20 /* ID string length */ 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistruct virtio_blk_config { 598c2ecf20Sopenharmony_ci /* The capacity (in 512-byte sectors). */ 608c2ecf20Sopenharmony_ci __virtio64 capacity; 618c2ecf20Sopenharmony_ci /* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */ 628c2ecf20Sopenharmony_ci __virtio32 size_max; 638c2ecf20Sopenharmony_ci /* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */ 648c2ecf20Sopenharmony_ci __virtio32 seg_max; 658c2ecf20Sopenharmony_ci /* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */ 668c2ecf20Sopenharmony_ci struct virtio_blk_geometry { 678c2ecf20Sopenharmony_ci __virtio16 cylinders; 688c2ecf20Sopenharmony_ci __u8 heads; 698c2ecf20Sopenharmony_ci __u8 sectors; 708c2ecf20Sopenharmony_ci } geometry; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */ 738c2ecf20Sopenharmony_ci __virtio32 blk_size; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */ 768c2ecf20Sopenharmony_ci /* exponent for physical block per logical block. */ 778c2ecf20Sopenharmony_ci __u8 physical_block_exp; 788c2ecf20Sopenharmony_ci /* alignment offset in logical blocks. */ 798c2ecf20Sopenharmony_ci __u8 alignment_offset; 808c2ecf20Sopenharmony_ci /* minimum I/O size without performance penalty in logical blocks. */ 818c2ecf20Sopenharmony_ci __virtio16 min_io_size; 828c2ecf20Sopenharmony_ci /* optimal sustained I/O size in logical blocks. */ 838c2ecf20Sopenharmony_ci __virtio32 opt_io_size; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */ 868c2ecf20Sopenharmony_ci __u8 wce; 878c2ecf20Sopenharmony_ci __u8 unused; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci /* number of vqs, only available when VIRTIO_BLK_F_MQ is set */ 908c2ecf20Sopenharmony_ci __virtio16 num_queues; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* the next 3 entries are guarded by VIRTIO_BLK_F_DISCARD */ 938c2ecf20Sopenharmony_ci /* 948c2ecf20Sopenharmony_ci * The maximum discard sectors (in 512-byte sectors) for 958c2ecf20Sopenharmony_ci * one segment. 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_ci __virtio32 max_discard_sectors; 988c2ecf20Sopenharmony_ci /* 998c2ecf20Sopenharmony_ci * The maximum number of discard segments in a 1008c2ecf20Sopenharmony_ci * discard command. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ci __virtio32 max_discard_seg; 1038c2ecf20Sopenharmony_ci /* Discard commands must be aligned to this number of sectors. */ 1048c2ecf20Sopenharmony_ci __virtio32 discard_sector_alignment; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* the next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES */ 1078c2ecf20Sopenharmony_ci /* 1088c2ecf20Sopenharmony_ci * The maximum number of write zeroes sectors (in 512-byte sectors) in 1098c2ecf20Sopenharmony_ci * one segment. 1108c2ecf20Sopenharmony_ci */ 1118c2ecf20Sopenharmony_ci __virtio32 max_write_zeroes_sectors; 1128c2ecf20Sopenharmony_ci /* 1138c2ecf20Sopenharmony_ci * The maximum number of segments in a write zeroes 1148c2ecf20Sopenharmony_ci * command. 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_ci __virtio32 max_write_zeroes_seg; 1178c2ecf20Sopenharmony_ci /* 1188c2ecf20Sopenharmony_ci * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the 1198c2ecf20Sopenharmony_ci * deallocation of one or more of the sectors. 1208c2ecf20Sopenharmony_ci */ 1218c2ecf20Sopenharmony_ci __u8 write_zeroes_may_unmap; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci __u8 unused1[3]; 1248c2ecf20Sopenharmony_ci} __attribute__((packed)); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/* 1278c2ecf20Sopenharmony_ci * Command types 1288c2ecf20Sopenharmony_ci * 1298c2ecf20Sopenharmony_ci * Usage is a bit tricky as some bits are used as flags and some are not. 1308c2ecf20Sopenharmony_ci * 1318c2ecf20Sopenharmony_ci * Rules: 1328c2ecf20Sopenharmony_ci * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or 1338c2ecf20Sopenharmony_ci * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own 1348c2ecf20Sopenharmony_ci * and may not be combined with any of the other flags. 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci/* These two define direction. */ 1388c2ecf20Sopenharmony_ci#define VIRTIO_BLK_T_IN 0 1398c2ecf20Sopenharmony_ci#define VIRTIO_BLK_T_OUT 1 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci#ifndef VIRTIO_BLK_NO_LEGACY 1428c2ecf20Sopenharmony_ci/* This bit says it's a scsi command, not an actual read or write. */ 1438c2ecf20Sopenharmony_ci#define VIRTIO_BLK_T_SCSI_CMD 2 1448c2ecf20Sopenharmony_ci#endif /* VIRTIO_BLK_NO_LEGACY */ 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci/* Cache flush command */ 1478c2ecf20Sopenharmony_ci#define VIRTIO_BLK_T_FLUSH 4 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci/* Get device ID command */ 1508c2ecf20Sopenharmony_ci#define VIRTIO_BLK_T_GET_ID 8 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci/* Discard command */ 1538c2ecf20Sopenharmony_ci#define VIRTIO_BLK_T_DISCARD 11 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci/* Write zeroes command */ 1568c2ecf20Sopenharmony_ci#define VIRTIO_BLK_T_WRITE_ZEROES 13 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci#ifndef VIRTIO_BLK_NO_LEGACY 1598c2ecf20Sopenharmony_ci/* Barrier before this op. */ 1608c2ecf20Sopenharmony_ci#define VIRTIO_BLK_T_BARRIER 0x80000000 1618c2ecf20Sopenharmony_ci#endif /* !VIRTIO_BLK_NO_LEGACY */ 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci/* 1648c2ecf20Sopenharmony_ci * This comes first in the read scatter-gather list. 1658c2ecf20Sopenharmony_ci * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, 1668c2ecf20Sopenharmony_ci * this is the first element of the read scatter-gather list. 1678c2ecf20Sopenharmony_ci */ 1688c2ecf20Sopenharmony_cistruct virtio_blk_outhdr { 1698c2ecf20Sopenharmony_ci /* VIRTIO_BLK_T* */ 1708c2ecf20Sopenharmony_ci __virtio32 type; 1718c2ecf20Sopenharmony_ci /* io priority. */ 1728c2ecf20Sopenharmony_ci __virtio32 ioprio; 1738c2ecf20Sopenharmony_ci /* Sector (ie. 512 byte offset) */ 1748c2ecf20Sopenharmony_ci __virtio64 sector; 1758c2ecf20Sopenharmony_ci}; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci/* Unmap this range (only valid for write zeroes command) */ 1788c2ecf20Sopenharmony_ci#define VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP 0x00000001 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci/* Discard/write zeroes range for each request. */ 1818c2ecf20Sopenharmony_cistruct virtio_blk_discard_write_zeroes { 1828c2ecf20Sopenharmony_ci /* discard/write zeroes start sector */ 1838c2ecf20Sopenharmony_ci __le64 sector; 1848c2ecf20Sopenharmony_ci /* number of discard/write zeroes sectors */ 1858c2ecf20Sopenharmony_ci __le32 num_sectors; 1868c2ecf20Sopenharmony_ci /* flags for this range */ 1878c2ecf20Sopenharmony_ci __le32 flags; 1888c2ecf20Sopenharmony_ci}; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci#ifndef VIRTIO_BLK_NO_LEGACY 1918c2ecf20Sopenharmony_cistruct virtio_scsi_inhdr { 1928c2ecf20Sopenharmony_ci __virtio32 errors; 1938c2ecf20Sopenharmony_ci __virtio32 data_len; 1948c2ecf20Sopenharmony_ci __virtio32 sense_len; 1958c2ecf20Sopenharmony_ci __virtio32 residual; 1968c2ecf20Sopenharmony_ci}; 1978c2ecf20Sopenharmony_ci#endif /* !VIRTIO_BLK_NO_LEGACY */ 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci/* And this is the final byte of the write scatter-gather list. */ 2008c2ecf20Sopenharmony_ci#define VIRTIO_BLK_S_OK 0 2018c2ecf20Sopenharmony_ci#define VIRTIO_BLK_S_IOERR 1 2028c2ecf20Sopenharmony_ci#define VIRTIO_BLK_S_UNSUPP 2 2038c2ecf20Sopenharmony_ci#endif /* _LINUX_VIRTIO_BLK_H */ 204