| 1 | /* | 
|---|
| 2 | * Copyright (c) 2019 Jakub Jermar | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #ifndef _VIRTIO_BLK_H_ | 
|---|
| 30 | #define _VIRTIO_BLK_H_ | 
|---|
| 31 |  | 
|---|
| 32 | #include <virtio-pci.h> | 
|---|
| 33 | #include <bd_srv.h> | 
|---|
| 34 | #include <abi/cap.h> | 
|---|
| 35 |  | 
|---|
| 36 | #include <fibril_synch.h> | 
|---|
| 37 |  | 
|---|
| 38 | #define VIRTIO_BLK_BLOCK_SIZE   512 | 
|---|
| 39 |  | 
|---|
| 40 | /* Operation types. */ | 
|---|
| 41 | #define VIRTIO_BLK_T_IN         0 | 
|---|
| 42 | #define VIRTIO_BLK_T_OUT        1 | 
|---|
| 43 |  | 
|---|
| 44 | /* Status codes returned by the device. */ | 
|---|
| 45 | #define VIRTIO_BLK_S_OK         0 | 
|---|
| 46 | #define VIRTIO_BLK_S_IOERR      1 | 
|---|
| 47 | #define VIRTIO_BLK_S_UNSUPP     2 | 
|---|
| 48 |  | 
|---|
| 49 | #define RQ_BUFFERS      32 | 
|---|
| 50 |  | 
|---|
| 51 | /** Device is read-only. */ | 
|---|
| 52 | #define VIRTIO_BLK_F_RO         (1U << 5) | 
|---|
| 53 |  | 
|---|
| 54 | typedef struct { | 
|---|
| 55 | uint32_t type; | 
|---|
| 56 | uint32_t reserved; | 
|---|
| 57 | uint64_t sector; | 
|---|
| 58 | } virtio_blk_req_header_t; | 
|---|
| 59 |  | 
|---|
| 60 | typedef struct { | 
|---|
| 61 | uint8_t status; | 
|---|
| 62 | } virtio_blk_req_footer_t; | 
|---|
| 63 |  | 
|---|
| 64 | typedef struct { | 
|---|
| 65 | uint64_t capacity; | 
|---|
| 66 | } virtio_blk_cfg_t; | 
|---|
| 67 |  | 
|---|
| 68 | typedef struct { | 
|---|
| 69 | virtio_dev_t virtio_dev; | 
|---|
| 70 |  | 
|---|
| 71 | void *rq_header[RQ_BUFFERS]; | 
|---|
| 72 | uintptr_t rq_header_p[RQ_BUFFERS]; | 
|---|
| 73 |  | 
|---|
| 74 | void *rq_buf[RQ_BUFFERS]; | 
|---|
| 75 | uintptr_t rq_buf_p[RQ_BUFFERS]; | 
|---|
| 76 |  | 
|---|
| 77 | void *rq_footer[RQ_BUFFERS]; | 
|---|
| 78 | uintptr_t rq_footer_p[RQ_BUFFERS]; | 
|---|
| 79 |  | 
|---|
| 80 | uint16_t rq_free_head; | 
|---|
| 81 |  | 
|---|
| 82 | int irq; | 
|---|
| 83 | cap_irq_handle_t irq_handle; | 
|---|
| 84 |  | 
|---|
| 85 | bd_srvs_t bds; | 
|---|
| 86 |  | 
|---|
| 87 | fibril_mutex_t free_lock; | 
|---|
| 88 | fibril_condvar_t free_cv; | 
|---|
| 89 |  | 
|---|
| 90 | fibril_mutex_t completion_lock[RQ_BUFFERS]; | 
|---|
| 91 | fibril_condvar_t completion_cv[RQ_BUFFERS]; | 
|---|
| 92 | } virtio_blk_t; | 
|---|
| 93 |  | 
|---|
| 94 | #endif | 
|---|