source: mainline/uspace/drv/nic/virtio-net/virtio-net.c@ 121ee4fd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 121ee4fd was 121ee4fd, checked in by Jakub Jermar <jakub@…>, 8 years ago

Add least effort virtio_net_send implementation

  • Property mode set to 100644
File size: 9.3 KB
Line 
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
57static ddf_dev_ops_t virtio_net_dev_ops;
58
59static errno_t virtio_net_dev_add(ddf_dev_t *dev);
60
61static driver_ops_t virtio_net_driver_ops = {
62 .dev_add = virtio_net_dev_add
63};
64
65static driver_t virtio_net_driver = {
66 .name = NAME,
67 .driver_ops = &virtio_net_driver_ops
68};
69
70static errno_t virtio_net_setup_bufs(unsigned int buffers, size_t size,
71 bool write, void *buf[], uintptr_t buf_p[])
72{
73 /*
74 * Allocate all buffers at once in one large chung.
75 */
76 void *virt = AS_AREA_ANY;
77 uintptr_t phys;
78 errno_t rc = dmamem_map_anonymous(buffers * size, 0,
79 write ? AS_AREA_WRITE : AS_AREA_READ, 0, &phys, &virt);
80 if (rc != EOK)
81 return rc;
82
83 ddf_msg(LVL_NOTE, "DMA buffers: %p-%p", virt, virt + buffers * size);
84
85 /*
86 * Calculate addresses of the individual buffers for easy access.
87 */
88 for (unsigned i = 0; i < buffers; i++) {
89 buf[i] = virt + i * size;
90 buf_p[i] = phys + i * size;
91 }
92
93 return EOK;
94}
95
96static void virtio_net_teardown_bufs(void *buf[])
97{
98 if (buf[0]) {
99 dmamem_unmap_anonymous(buf[0]);
100 buf[0] = NULL;
101 }
102}
103
104static void virtio_net_create_buf_free_list(virtio_dev_t *vdev, uint16_t num,
105 uint16_t size, uint16_t *head)
106{
107 for (unsigned i = 0; i < size; i++) {
108 virtio_virtq_set_desc(vdev, num, i, 0, 0,
109 VIRTQ_DESC_F_NEXT, (i + 1 == size) ? -1U : i + 1);
110 }
111 *head = 0;
112}
113
114static errno_t virtio_net_initialize(ddf_dev_t *dev)
115{
116 nic_t *nic_data = nic_create_and_bind(dev);
117 if (!nic_data)
118 return ENOMEM;
119
120 virtio_net_t *virtio_net = calloc(1, sizeof(virtio_net_t));
121 if (!virtio_net) {
122 nic_unbind_and_destroy(dev);
123 return ENOMEM;
124 }
125
126 nic_set_specific(nic_data, virtio_net);
127
128 errno_t rc = virtio_pci_dev_initialize(dev, &virtio_net->virtio_dev);
129 if (rc != EOK)
130 return rc;
131
132 virtio_dev_t *vdev = &virtio_net->virtio_dev;
133 virtio_pci_common_cfg_t *cfg = virtio_net->virtio_dev.common_cfg;
134 virtio_net_cfg_t *netcfg = virtio_net->virtio_dev.device_cfg;
135
136 /* Reset the device and negotiate the feature bits */
137 rc = virtio_device_setup_start(vdev,
138 VIRTIO_NET_F_MAC | VIRTIO_NET_F_CTRL_VQ);
139 if (rc != EOK)
140 goto fail;
141
142 /* Perform device-specific setup */
143
144 /*
145 * Discover and configure the virtqueues
146 */
147 uint16_t num_queues = pio_read_le16(&cfg->num_queues);
148 if (num_queues != VIRTIO_NET_NUM_QUEUES) {
149 ddf_msg(LVL_NOTE, "Unsupported number of virtqueues: %u",
150 num_queues);
151 goto fail;
152 }
153
154 vdev->queues = calloc(sizeof(virtq_t), num_queues);
155 if (!vdev->queues) {
156 rc = ENOMEM;
157 goto fail;
158 }
159
160 rc = virtio_virtq_setup(vdev, RX_QUEUE_1, RX_BUFFERS);
161 if (rc != EOK)
162 goto fail;
163 rc = virtio_virtq_setup(vdev, TX_QUEUE_1, TX_BUFFERS);
164 if (rc != EOK)
165 goto fail;
166 rc = virtio_virtq_setup(vdev, CT_QUEUE_1, CT_BUFFERS);
167 if (rc != EOK)
168 goto fail;
169
170 /*
171 * Setup DMA buffers
172 */
173 rc = virtio_net_setup_bufs(RX_BUFFERS, RX_BUF_SIZE, false,
174 virtio_net->rx_buf, virtio_net->rx_buf_p);
175 if (rc != EOK)
176 goto fail;
177 rc = virtio_net_setup_bufs(TX_BUFFERS, TX_BUF_SIZE, true,
178 virtio_net->tx_buf, virtio_net->tx_buf_p);
179 if (rc != EOK)
180 goto fail;
181 rc = virtio_net_setup_bufs(CT_BUFFERS, CT_BUF_SIZE, true,
182 virtio_net->ct_buf, virtio_net->ct_buf_p);
183 if (rc != EOK)
184 goto fail;
185
186 /*
187 * Give all RX buffers to the NIC
188 */
189 for (unsigned i = 0; i < RX_BUFFERS; i++) {
190 /*
191 * Associtate the buffer with the descriptor, set length and
192 * flags.
193 */
194 virtio_virtq_set_desc(vdev, RX_QUEUE_1, i,
195 virtio_net->rx_buf_p[i], RX_BUF_SIZE, VIRTQ_DESC_F_WRITE,
196 0);
197 /*
198 * Put the set descriptor into the available ring of the RX
199 * queue.
200 */
201 virtio_virtq_produce_available(vdev, RX_QUEUE_1, i);
202 }
203
204 /*
205 * Put all TX and CT buffers on a free list
206 */
207 virtio_net_create_buf_free_list(vdev, TX_QUEUE_1, TX_BUFFERS,
208 &virtio_net->tx_free_head);
209 virtio_net_create_buf_free_list(vdev, CT_QUEUE_1, CT_BUFFERS,
210 &virtio_net->ct_free_head);
211
212 /*
213 * Read the MAC address
214 */
215 nic_address_t nic_addr;
216 for (unsigned i = 0; i < 6; i++)
217 nic_addr.address[i] = pio_read_8(&netcfg->mac[i]);
218 rc = nic_report_address(nic_data, &nic_addr);
219 if (rc != EOK)
220 goto fail;
221
222 ddf_msg(LVL_NOTE, "MAC address: %02x:%02x:%02x:%02x:%02x:%02x",
223 nic_addr.address[0], nic_addr.address[1], nic_addr.address[2],
224 nic_addr.address[3], nic_addr.address[4], nic_addr.address[5]);
225
226 /* Go live */
227 virtio_device_setup_finalize(vdev);
228
229 return EOK;
230
231fail:
232 virtio_net_teardown_bufs(virtio_net->rx_buf);
233 virtio_net_teardown_bufs(virtio_net->tx_buf);
234 virtio_net_teardown_bufs(virtio_net->ct_buf);
235
236 virtio_device_setup_fail(vdev);
237 virtio_pci_dev_cleanup(vdev);
238 return rc;
239}
240
241static void virtio_net_uninitialize(ddf_dev_t *dev)
242{
243 nic_t *nic = ddf_dev_data_get(dev);
244 virtio_net_t *virtio_net = (virtio_net_t *) nic_get_specific(nic);
245
246 virtio_net_teardown_bufs(virtio_net->rx_buf);
247 virtio_net_teardown_bufs(virtio_net->tx_buf);
248 virtio_net_teardown_bufs(virtio_net->ct_buf);
249
250 virtio_device_setup_fail(&virtio_net->virtio_dev);
251 virtio_pci_dev_cleanup(&virtio_net->virtio_dev);
252}
253
254static void virtio_net_send(nic_t *nic, void *data, size_t size)
255{
256 // TODO
257}
258
259static errno_t virtio_net_on_broadcast_mode_change(nic_t *nic,
260 nic_broadcast_mode_t new_mode)
261{
262 switch (new_mode) {
263 case NIC_BROADCAST_BLOCKED:
264 return ENOTSUP;
265 case NIC_BROADCAST_ACCEPTED:
266 return EOK;
267 default:
268 return ENOTSUP;
269 }
270}
271
272static errno_t virtio_net_dev_add(ddf_dev_t *dev)
273{
274 ddf_msg(LVL_NOTE, "%s %s (handle = %zu)", __func__,
275 ddf_dev_get_name(dev), ddf_dev_get_handle(dev));
276
277 errno_t rc = virtio_net_initialize(dev);
278 if (rc != EOK)
279 return rc;
280
281 ddf_fun_t *fun = ddf_fun_create(dev, fun_exposed, "port0");
282 if (fun == NULL) {
283 rc = ENOMEM;
284 goto error;
285 }
286 nic_t *nic = ddf_dev_data_get(dev);
287 nic_set_ddf_fun(nic, fun);
288 ddf_fun_set_ops(fun, &virtio_net_dev_ops);
289
290 nic_set_send_frame_handler(nic, virtio_net_send);
291 nic_set_filtering_change_handlers(nic, NULL, NULL,
292 virtio_net_on_broadcast_mode_change, NULL, NULL);
293
294 rc = ddf_fun_bind(fun);
295 if (rc != EOK) {
296 ddf_msg(LVL_ERROR, "Failed binding device function");
297 goto uninitialize;
298 }
299
300 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
301 if (rc != EOK) {
302 ddf_msg(LVL_ERROR, "Failed adding function to category");
303 goto unbind;
304 }
305
306 ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
307 ddf_dev_get_name(dev));
308
309 return EOK;
310
311unbind:
312 ddf_fun_unbind(fun);
313uninitialize:
314 virtio_net_uninitialize(dev);
315error:
316 return rc;
317}
318
319static errno_t virtio_net_get_device_info(ddf_fun_t *fun,
320 nic_device_info_t *info)
321{
322 nic_t *nic_data = nic_get_from_ddf_fun(fun);
323 if (!nic_data)
324 return ENOENT;
325
326 str_cpy(info->vendor_name, sizeof(info->vendor_name), "Red Hat, Inc.");
327 str_cpy(info->model_name, sizeof(info->model_name),
328 "Virtio network device");
329
330 return EOK;
331}
332
333static errno_t virtio_net_get_cable_state(ddf_fun_t *fun,
334 nic_cable_state_t *state)
335{
336 *state = NIC_CS_PLUGGED;
337 return EOK;
338}
339
340static errno_t virtio_net_get_operation_mode(ddf_fun_t *fun, int *speed,
341 nic_channel_mode_t *duplex, nic_role_t *role)
342{
343 *speed = 1000;
344 *duplex = NIC_CM_FULL_DUPLEX;
345 *role = NIC_ROLE_UNKNOWN;
346 return EOK;
347}
348
349static nic_iface_t virtio_net_nic_iface = {
350 .get_device_info = virtio_net_get_device_info,
351 .get_cable_state = virtio_net_get_cable_state,
352 .get_operation_mode = virtio_net_get_operation_mode,
353};
354
355int main(void)
356{
357 printf("%s: HelenOS virtio-net driver\n", NAME);
358
359 if (nic_driver_init(NAME) != EOK)
360 return 1;
361
362 nic_driver_implement(&virtio_net_driver_ops, &virtio_net_dev_ops,
363 &virtio_net_nic_iface);
364
365 (void) ddf_log_init(NAME);
366 return ddf_driver_main(&virtio_net_driver);
367}
Note: See TracBrowser for help on using the repository browser.