8d001e83118fb3576c3f6fcbfca0536d65450623
[dpdk.git] / drivers / net / virtio / virtio_pci.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdint.h>
34
35 #include "virtio_pci.h"
36 #include "virtio_logs.h"
37 #include "virtqueue.h"
38
39 static void
40 legacy_read_dev_config(struct virtio_hw *hw, size_t offset,
41                        void *dst, int length)
42 {
43         uint64_t off;
44         uint8_t *d;
45         int size;
46
47         off = VIRTIO_PCI_CONFIG(hw) + offset;
48         for (d = dst; length > 0; d += size, off += size, length -= size) {
49                 if (length >= 4) {
50                         size = 4;
51                         *(uint32_t *)d = VIRTIO_READ_REG_4(hw, off);
52                 } else if (length >= 2) {
53                         size = 2;
54                         *(uint16_t *)d = VIRTIO_READ_REG_2(hw, off);
55                 } else {
56                         size = 1;
57                         *d = VIRTIO_READ_REG_1(hw, off);
58                 }
59         }
60 }
61
62 static void
63 legacy_write_dev_config(struct virtio_hw *hw, size_t offset,
64                         const void *src, int length)
65 {
66         uint64_t off;
67         const uint8_t *s;
68         int size;
69
70         off = VIRTIO_PCI_CONFIG(hw) + offset;
71         for (s = src; length > 0; s += size, off += size, length -= size) {
72                 if (length >= 4) {
73                         size = 4;
74                         VIRTIO_WRITE_REG_4(hw, off, *(const uint32_t *)s);
75                 } else if (length >= 2) {
76                         size = 2;
77                         VIRTIO_WRITE_REG_2(hw, off, *(const uint16_t *)s);
78                 } else {
79                         size = 1;
80                         VIRTIO_WRITE_REG_1(hw, off, *s);
81                 }
82         }
83 }
84
85 static uint32_t
86 legacy_get_features(struct virtio_hw *hw)
87 {
88         return VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
89 }
90
91 static void
92 legacy_set_features(struct virtio_hw *hw, uint32_t features)
93 {
94         VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_GUEST_FEATURES, features);
95 }
96
97 static uint8_t
98 legacy_get_status(struct virtio_hw *hw)
99 {
100         return VIRTIO_READ_REG_1(hw, VIRTIO_PCI_STATUS);
101 }
102
103 static void
104 legacy_set_status(struct virtio_hw *hw, uint8_t status)
105 {
106         VIRTIO_WRITE_REG_1(hw, VIRTIO_PCI_STATUS, status);
107 }
108
109 static void
110 legacy_reset(struct virtio_hw *hw)
111 {
112         legacy_set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
113 }
114
115 static uint8_t
116 legacy_get_isr(struct virtio_hw *hw)
117 {
118         return VIRTIO_READ_REG_1(hw, VIRTIO_PCI_ISR);
119 }
120
121 /* Enable one vector (0) for Link State Intrerrupt */
122 static uint16_t
123 legacy_set_config_irq(struct virtio_hw *hw, uint16_t vec)
124 {
125         VIRTIO_WRITE_REG_2(hw, VIRTIO_MSI_CONFIG_VECTOR, vec);
126         return VIRTIO_READ_REG_2(hw, VIRTIO_MSI_CONFIG_VECTOR);
127 }
128
129 static uint16_t
130 legacy_get_queue_num(struct virtio_hw *hw, uint16_t queue_id)
131 {
132         VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, queue_id);
133         return VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
134 }
135
136 static void
137 legacy_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
138 {
139         VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vq->vq_queue_index);
140
141         VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
142                 vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
143 }
144
145 static void
146 legacy_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
147 {
148         VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vq->vq_queue_index);
149
150         VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN, 0);
151 }
152
153 static void
154 legacy_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
155 {
156         VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_NOTIFY, vq->vq_queue_index);
157 }
158
159
160 static const struct virtio_pci_ops legacy_ops = {
161         .read_dev_cfg   = legacy_read_dev_config,
162         .write_dev_cfg  = legacy_write_dev_config,
163         .reset          = legacy_reset,
164         .get_status     = legacy_get_status,
165         .set_status     = legacy_set_status,
166         .get_features   = legacy_get_features,
167         .set_features   = legacy_set_features,
168         .get_isr        = legacy_get_isr,
169         .set_config_irq = legacy_set_config_irq,
170         .get_queue_num  = legacy_get_queue_num,
171         .setup_queue    = legacy_setup_queue,
172         .del_queue      = legacy_del_queue,
173         .notify_queue   = legacy_notify_queue,
174 };
175
176
177 void
178 vtpci_read_dev_config(struct virtio_hw *hw, size_t offset,
179                       void *dst, int length)
180 {
181         hw->vtpci_ops->read_dev_cfg(hw, offset, dst, length);
182 }
183
184 void
185 vtpci_write_dev_config(struct virtio_hw *hw, size_t offset,
186                        const void *src, int length)
187 {
188         hw->vtpci_ops->write_dev_cfg(hw, offset, src, length);
189 }
190
191 uint32_t
192 vtpci_negotiate_features(struct virtio_hw *hw, uint32_t host_features)
193 {
194         uint32_t features;
195
196         /*
197          * Limit negotiated features to what the driver, virtqueue, and
198          * host all support.
199          */
200         features = host_features & hw->guest_features;
201         hw->vtpci_ops->set_features(hw, features);
202
203         return features;
204 }
205
206 void
207 vtpci_reset(struct virtio_hw *hw)
208 {
209         hw->vtpci_ops->set_status(hw, VIRTIO_CONFIG_STATUS_RESET);
210         /* flush status write */
211         hw->vtpci_ops->get_status(hw);
212 }
213
214 void
215 vtpci_reinit_complete(struct virtio_hw *hw)
216 {
217         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
218 }
219
220 void
221 vtpci_set_status(struct virtio_hw *hw, uint8_t status)
222 {
223         if (status != VIRTIO_CONFIG_STATUS_RESET)
224                 status |= hw->vtpci_ops->get_status(hw);
225
226         hw->vtpci_ops->set_status(hw, status);
227 }
228
229 uint8_t
230 vtpci_isr(struct virtio_hw *hw)
231 {
232         return hw->vtpci_ops->get_isr(hw);
233 }
234
235
236 /* Enable one vector (0) for Link State Intrerrupt */
237 uint16_t
238 vtpci_irq_config(struct virtio_hw *hw, uint16_t vec)
239 {
240         return hw->vtpci_ops->set_config_irq(hw, vec);
241 }
242
243 int
244 vtpci_init(struct rte_pci_device *dev __rte_unused, struct virtio_hw *hw)
245 {
246         hw->vtpci_ops = &legacy_ops;
247
248         return 0;
249 }