1 | /*
|
---|
2 | * Copyright (c) 2018 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 | #include "virtio-net.h"
|
---|
30 |
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdint.h>
|
---|
33 |
|
---|
34 | #include <as.h>
|
---|
35 | #include <ddf/driver.h>
|
---|
36 | #include <ddf/log.h>
|
---|
37 | #include <ops/nic.h>
|
---|
38 | #include <pci_dev_iface.h>
|
---|
39 |
|
---|
40 | #include <nic.h>
|
---|
41 |
|
---|
42 | #include <virtio-pci.h>
|
---|
43 |
|
---|
44 | #define NAME "virtio-net"
|
---|
45 |
|
---|
46 | #define VIRTIO_NET_NUM_QUEUES 3
|
---|
47 |
|
---|
48 | #define RX_QUEUE_1 0
|
---|
49 | #define TX_QUEUE_1 1
|
---|
50 | #define CT_QUEUE_1 2
|
---|
51 |
|
---|
52 | #define BUFFER_SIZE 2048
|
---|
53 | #define RX_BUF_SIZE BUFFER_SIZE
|
---|
54 | #define TX_BUF_SIZE BUFFER_SIZE
|
---|
55 | #define CT_BUF_SIZE BUFFER_SIZE
|
---|
56 |
|
---|
57 |
|
---|
58 | static errno_t virtio_net_setup_bufs(unsigned int buffers, size_t size,
|
---|
59 | bool write, void *buf[], uintptr_t buf_p[])
|
---|
60 | {
|
---|
61 | /*
|
---|
62 | * Allocate all buffers at once in one large chung.
|
---|
63 | */
|
---|
64 | void *virt = AS_AREA_ANY;
|
---|
65 | uintptr_t phys;
|
---|
66 | errno_t rc = dmamem_map_anonymous(buffers * size, 0,
|
---|
67 | write ? AS_AREA_WRITE : AS_AREA_READ, 0, &phys, &virt);
|
---|
68 | if (rc != EOK)
|
---|
69 | return rc;
|
---|
70 |
|
---|
71 | ddf_msg(LVL_NOTE, "DMA buffers: %p-%p", virt, virt + buffers * size);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Calculate addresses of the individual buffers for easy access.
|
---|
75 | */
|
---|
76 | for (unsigned i = 0; i < buffers; i++) {
|
---|
77 | buf[i] = virt + i * size;
|
---|
78 | buf_p[i] = phys + i * size;
|
---|
79 | }
|
---|
80 |
|
---|
81 | return EOK;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void virtio_net_teardown_bufs(void *buf[])
|
---|
85 | {
|
---|
86 | if (buf[0]) {
|
---|
87 | dmamem_unmap_anonymous(buf[0]);
|
---|
88 | buf[0] = NULL;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | static void virtio_net_create_buf_free_list(virtio_dev_t *vdev, uint16_t num,
|
---|
93 | uint16_t size, uint16_t *head)
|
---|
94 | {
|
---|
95 | for (unsigned i = 0; i < size; i++) {
|
---|
96 | virtio_virtq_set_desc(vdev, num, i, 0, 0,
|
---|
97 | VIRTQ_DESC_F_NEXT, (i + 1 == size) ? -1U : i + 1);
|
---|
98 | }
|
---|
99 | *head = 0;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static errno_t virtio_net_initialize(ddf_dev_t *dev)
|
---|
103 | {
|
---|
104 | nic_t *nic_data = nic_create_and_bind(dev);
|
---|
105 | if (!nic_data)
|
---|
106 | return ENOMEM;
|
---|
107 |
|
---|
108 | virtio_net_t *virtio_net = calloc(1, sizeof(virtio_net_t));
|
---|
109 | if (!virtio_net) {
|
---|
110 | nic_unbind_and_destroy(dev);
|
---|
111 | return ENOMEM;
|
---|
112 | }
|
---|
113 |
|
---|
114 | nic_set_specific(nic_data, virtio_net);
|
---|
115 |
|
---|
116 | errno_t rc = virtio_pci_dev_initialize(dev, &virtio_net->virtio_dev);
|
---|
117 | if (rc != EOK)
|
---|
118 | return rc;
|
---|
119 |
|
---|
120 | virtio_dev_t *vdev = &virtio_net->virtio_dev;
|
---|
121 | virtio_pci_common_cfg_t *cfg = virtio_net->virtio_dev.common_cfg;
|
---|
122 | virtio_net_cfg_t *netcfg = virtio_net->virtio_dev.device_cfg;
|
---|
123 |
|
---|
124 | /* Reset the device and negotiate the feature bits */
|
---|
125 | rc = virtio_device_setup_start(vdev,
|
---|
126 | VIRTIO_NET_F_MAC | VIRTIO_NET_F_CTRL_VQ);
|
---|
127 | if (rc != EOK)
|
---|
128 | goto fail;
|
---|
129 |
|
---|
130 | /* Perform device-specific setup */
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Discover and configure the virtqueues
|
---|
134 | */
|
---|
135 | uint16_t num_queues = pio_read_le16(&cfg->num_queues);
|
---|
136 | if (num_queues != VIRTIO_NET_NUM_QUEUES) {
|
---|
137 | ddf_msg(LVL_NOTE, "Unsupported number of virtqueues: %u",
|
---|
138 | num_queues);
|
---|
139 | goto fail;
|
---|
140 | }
|
---|
141 |
|
---|
142 | vdev->queues = calloc(sizeof(virtq_t), num_queues);
|
---|
143 | if (!vdev->queues) {
|
---|
144 | rc = ENOMEM;
|
---|
145 | goto fail;
|
---|
146 | }
|
---|
147 |
|
---|
148 | rc = virtio_virtq_setup(vdev, RX_QUEUE_1, RX_BUFFERS);
|
---|
149 | if (rc != EOK)
|
---|
150 | goto fail;
|
---|
151 | rc = virtio_virtq_setup(vdev, TX_QUEUE_1, TX_BUFFERS);
|
---|
152 | if (rc != EOK)
|
---|
153 | goto fail;
|
---|
154 | rc = virtio_virtq_setup(vdev, CT_QUEUE_1, CT_BUFFERS);
|
---|
155 | if (rc != EOK)
|
---|
156 | goto fail;
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Setup DMA buffers
|
---|
160 | */
|
---|
161 | rc = virtio_net_setup_bufs(RX_BUFFERS, RX_BUF_SIZE, false,
|
---|
162 | virtio_net->rx_buf, virtio_net->rx_buf_p);
|
---|
163 | if (rc != EOK)
|
---|
164 | goto fail;
|
---|
165 | rc = virtio_net_setup_bufs(TX_BUFFERS, TX_BUF_SIZE, true,
|
---|
166 | virtio_net->tx_buf, virtio_net->tx_buf_p);
|
---|
167 | if (rc != EOK)
|
---|
168 | goto fail;
|
---|
169 | rc = virtio_net_setup_bufs(CT_BUFFERS, CT_BUF_SIZE, true,
|
---|
170 | virtio_net->ct_buf, virtio_net->ct_buf_p);
|
---|
171 | if (rc != EOK)
|
---|
172 | goto fail;
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Give all RX buffers to the NIC
|
---|
176 | */
|
---|
177 | for (unsigned i = 0; i < RX_BUFFERS; i++) {
|
---|
178 | /*
|
---|
179 | * Associtate the buffer with the descriptor, set length and
|
---|
180 | * flags.
|
---|
181 | */
|
---|
182 | virtio_virtq_set_desc(vdev, RX_QUEUE_1, i,
|
---|
183 | virtio_net->rx_buf_p[i], RX_BUF_SIZE, VIRTQ_DESC_F_WRITE,
|
---|
184 | 0);
|
---|
185 | /*
|
---|
186 | * Put the set descriptor into the available ring of the RX
|
---|
187 | * queue.
|
---|
188 | */
|
---|
189 | virtio_virtq_produce_available(vdev, RX_QUEUE_1, i);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Put all TX and CT buffers on a free list
|
---|
194 | */
|
---|
195 | virtio_net_create_buf_free_list(vdev, TX_QUEUE_1, TX_BUFFERS,
|
---|
196 | &virtio_net->tx_free_head);
|
---|
197 | virtio_net_create_buf_free_list(vdev, CT_QUEUE_1, CT_BUFFERS,
|
---|
198 | &virtio_net->ct_free_head);
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Read the MAC address
|
---|
202 | */
|
---|
203 | nic_address_t nic_addr;
|
---|
204 | for (unsigned i = 0; i < 6; i++)
|
---|
205 | nic_addr.address[i] = pio_read_8(&netcfg->mac[i]);
|
---|
206 | rc = nic_report_address(nic_data, &nic_addr);
|
---|
207 | if (rc != EOK)
|
---|
208 | goto fail;
|
---|
209 |
|
---|
210 | ddf_msg(LVL_NOTE, "MAC address: %02x:%02x:%02x:%02x:%02x:%02x",
|
---|
211 | nic_addr.address[0], nic_addr.address[1], nic_addr.address[2],
|
---|
212 | nic_addr.address[3], nic_addr.address[4], nic_addr.address[5]);
|
---|
213 |
|
---|
214 | /* Go live */
|
---|
215 | virtio_device_setup_finalize(vdev);
|
---|
216 |
|
---|
217 | return EOK;
|
---|
218 |
|
---|
219 | fail:
|
---|
220 | virtio_net_teardown_bufs(virtio_net->rx_buf);
|
---|
221 | virtio_net_teardown_bufs(virtio_net->tx_buf);
|
---|
222 | virtio_net_teardown_bufs(virtio_net->ct_buf);
|
---|
223 |
|
---|
224 | virtio_device_setup_fail(vdev);
|
---|
225 | virtio_pci_dev_cleanup(vdev);
|
---|
226 | return rc;
|
---|
227 | }
|
---|
228 |
|
---|
229 | static errno_t virtio_net_dev_add(ddf_dev_t *dev)
|
---|
230 | {
|
---|
231 | ddf_msg(LVL_NOTE, "%s %s (handle = %zu)", __func__,
|
---|
232 | ddf_dev_get_name(dev), ddf_dev_get_handle(dev));
|
---|
233 |
|
---|
234 | errno_t rc = virtio_net_initialize(dev);
|
---|
235 | if (rc != EOK)
|
---|
236 | return rc;
|
---|
237 |
|
---|
238 | return ENOTSUP;
|
---|
239 | }
|
---|
240 |
|
---|
241 | static ddf_dev_ops_t virtio_net_dev_ops;
|
---|
242 |
|
---|
243 | static driver_ops_t virtio_net_driver_ops = {
|
---|
244 | .dev_add = virtio_net_dev_add
|
---|
245 | };
|
---|
246 |
|
---|
247 | static driver_t virtio_net_driver = {
|
---|
248 | .name = NAME,
|
---|
249 | .driver_ops = &virtio_net_driver_ops
|
---|
250 | };
|
---|
251 |
|
---|
252 | static nic_iface_t virtio_net_nic_iface;
|
---|
253 |
|
---|
254 | int main(void)
|
---|
255 | {
|
---|
256 | printf("%s: HelenOS virtio-net driver\n", NAME);
|
---|
257 |
|
---|
258 | if (nic_driver_init(NAME) != EOK)
|
---|
259 | return 1;
|
---|
260 |
|
---|
261 | nic_driver_implement(&virtio_net_driver_ops, &virtio_net_dev_ops,
|
---|
262 | &virtio_net_nic_iface);
|
---|
263 |
|
---|
264 | (void) ddf_log_init(NAME);
|
---|
265 | return ddf_driver_main(&virtio_net_driver);
|
---|
266 | }
|
---|