Cleanup the device if initialization fails
[helenos.git] / uspace / lib / virtio / virtio-pci.h
blob12bea375985c934191344a7d524335dc8ae24f71
1 /*
2 * Copyright (c) 2018 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @file VIRTIO PCI definitions
32 #ifndef _VIRTIO_PCI_H_
33 #define _VIRTIO_PCI_H_
35 #include <ddf/driver.h>
36 #include <pci_dev_iface.h>
37 #include <ddi.h>
39 #define VIRTIO_PCI_CAP_TYPE(c) ((c) + 3)
40 #define VIRTIO_PCI_CAP_BAR(c) ((c) + 4)
41 #define VIRTIO_PCI_CAP_OFFSET(c) ((c) + 8)
42 #define VIRTIO_PCI_CAP_LENGTH(c) ((c) + 12)
43 #define VIRTIO_PCI_CAP_END(c) ((c) + 16)
45 #define VIRTIO_PCI_CAP_COMMON_CFG 1
46 #define VIRTIO_PCI_CAP_NOTIFY_CFG 2
47 #define VIRTIO_PCI_CAP_ISR_CFG 3
48 #define VIRTIO_PCI_CAP_DEVICE_CFG 4
49 #define VIRTIO_PCI_CAP_PCI_CFG 5
51 #define VIRTIO_DEV_STATUS_RESET 0
52 #define VIRTIO_DEV_STATUS_ACKNOWLEDGE 1
53 #define VIRTIO_DEV_STATUS_DRIVER 2
54 #define VIRTIO_DEV_STATUS_DRIVER_OK 4
55 #define VIRTIO_DEV_STATUS_FEATURES_OK 8
56 #define VIRTIO_DEV_STATUS_DEVICE_NEEDS_RESET 64
57 #define VIRTIO_DEV_STATUS_FAILED 128
59 /** Common configuration structure layout according to VIRTIO v1. */
60 typedef struct virtio_pci_common_cfg {
61 ioport32_t device_feature_select;
62 const ioport32_t device_feature;
63 ioport32_t driver_feature_select;
64 ioport32_t driver_feature;
65 ioport16_t msix_config;
66 const ioport16_t num_queues;
67 ioport8_t device_status;
68 const ioport8_t config_generation;
69 ioport16_t queue_select;
70 ioport16_t queue_size;
71 ioport16_t queue_msix_vector;
72 ioport16_t queue_enable;
73 const ioport16_t queue_notif_off;
74 ioport64_t queue_desc;
75 ioport64_t queue_avail;
76 ioport64_t queue_used;
77 } virtio_pci_common_cfg_t;
79 typedef struct {
80 struct {
81 bool mapped;
82 void *mapped_base;
83 size_t mapped_size;
84 } bar[PCI_BAR_COUNT];
86 /** Commong configuration structure */
87 virtio_pci_common_cfg_t *common_cfg;
89 /** Notification base address */
90 ioport16_t *notify_base;
91 /** Notification offset multiplier */
92 uint32_t notify_off_multiplier;
94 /** INT#x interrupt ISR register */
95 ioport8_t *isr;
97 /** Device-specific configuration */
98 void *device_cfg;
99 } virtio_dev_t;
101 extern errno_t virtio_pci_dev_initialize(ddf_dev_t *, virtio_dev_t *);
102 extern errno_t virtio_pci_dev_cleanup(virtio_dev_t *);
104 #endif
106 /** @}