2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include <sys/ioctl.h>
19 #include <vppinfra/types.h>
20 #include <vlib/vlib.h>
21 #include <vlib/pci/pci.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/ip/ip4_packet.h>
24 #include <vnet/ip/ip6_packet.h>
25 #include <vnet/devices/virtio/virtio.h>
26 #include <vnet/devices/virtio/virtio_pci_modern.h>
27 #include <vnet/devices/virtio/pci.h>
31 virtio_pci_modern_get_device_features (vlib_main_t * vm, virtio_if_t * vif)
33 u64 features_lo, features_hi;
34 virtio_pci_reg_write_u32 (vif, VIRTIO_DEVICE_FEATURE_SELECT_OFFSET (vif),
35 VIRTIO_FEATURE_SELECT_LO);
37 virtio_pci_reg_read_u32 (vif, VIRTIO_DEVICE_FEATURE_OFFSET (vif));
38 virtio_pci_reg_write_u32 (vif, VIRTIO_DEVICE_FEATURE_SELECT_OFFSET (vif),
39 VIRTIO_FEATURE_SELECT_HI);
41 virtio_pci_reg_read_u32 (vif, VIRTIO_DEVICE_FEATURE_OFFSET (vif));
42 u64 features = ((features_hi << 32) | features_lo);
47 virtio_pci_modern_get_driver_features (vlib_main_t * vm, virtio_if_t * vif)
49 u64 features_lo, features_hi;
50 virtio_pci_reg_write_u32 (vif, VIRTIO_DRIVER_FEATURE_SELECT_OFFSET (vif),
51 VIRTIO_FEATURE_SELECT_LO);
53 virtio_pci_reg_read_u32 (vif, VIRTIO_DRIVER_FEATURE_OFFSET (vif));
54 virtio_pci_reg_write_u32 (vif, VIRTIO_DRIVER_FEATURE_SELECT_OFFSET (vif),
55 VIRTIO_FEATURE_SELECT_HI);
57 virtio_pci_reg_read_u32 (vif, VIRTIO_DRIVER_FEATURE_OFFSET (vif));
59 vif->features = ((features_hi << 32) | features_lo);
64 virtio_pci_modern_set_driver_features (vlib_main_t * vm, virtio_if_t * vif,
67 u32 features_lo = (u32) features, features_hi = (u32) (features >> 32);
68 virtio_pci_reg_write_u32 (vif, VIRTIO_DRIVER_FEATURE_SELECT_OFFSET (vif),
69 VIRTIO_FEATURE_SELECT_LO);
70 virtio_pci_reg_write_u32 (vif, VIRTIO_DRIVER_FEATURE_OFFSET (vif),
72 virtio_pci_reg_write_u32 (vif, VIRTIO_DRIVER_FEATURE_SELECT_OFFSET (vif),
73 VIRTIO_FEATURE_SELECT_HI);
74 virtio_pci_reg_write_u32 (vif, VIRTIO_DRIVER_FEATURE_OFFSET (vif),
77 if (features != virtio_pci_modern_get_driver_features (vm, vif))
79 clib_warning ("modern set guest features failed!");
84 virtio_pci_modern_get_msix_config (virtio_if_t * vif)
88 virtio_pci_reg_read_u16 (vif, VIRTIO_MSIX_CONFIG_VECTOR_OFFSET (vif));
93 virtio_pci_modern_set_msix_config (vlib_main_t * vm, virtio_if_t * vif,
96 virtio_pci_reg_write_u16 (vif, VIRTIO_MSIX_CONFIG_VECTOR_OFFSET (vif),
98 return virtio_pci_modern_get_msix_config (vif);
102 virtio_pci_modern_get_num_queues (virtio_if_t * vif)
105 num_queues = virtio_pci_reg_read_u16 (vif, VIRTIO_NUM_QUEUES_OFFSET (vif));
110 virtio_pci_modern_get_status (vlib_main_t * vm, virtio_if_t * vif)
113 status = virtio_pci_reg_read_u8 (vif, VIRTIO_DEVICE_STATUS_OFFSET (vif));
118 virtio_pci_modern_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status)
120 if (status != VIRTIO_CONFIG_STATUS_RESET)
121 status |= virtio_pci_modern_get_status (vm, vif);
122 virtio_pci_reg_write_u8 (vif, VIRTIO_DEVICE_STATUS_OFFSET (vif), status);
126 virtio_pci_modern_reset (vlib_main_t * vm, virtio_if_t * vif)
128 virtio_pci_modern_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET);
129 return virtio_pci_modern_get_status (vm, vif);
133 virtio_pci_modern_get_config_generation (virtio_if_t * vif)
135 u8 config_generation = 0;
137 virtio_pci_reg_read_u8 (vif, VIRTIO_CONFIG_GENERATION_OFFSET (vif));
138 return config_generation;
142 virtio_pci_modern_set_queue_select (virtio_if_t * vif, u16 queue_select)
144 virtio_pci_reg_write_u16 (vif, VIRTIO_QUEUE_SELECT_OFFSET (vif),
149 virtio_pci_modern_get_queue_size (vlib_main_t * vm, virtio_if_t * vif,
153 virtio_pci_modern_set_queue_select (vif, queue_id);
154 queue_size = virtio_pci_reg_read_u16 (vif, VIRTIO_QUEUE_SIZE_OFFSET (vif));
159 virtio_pci_modern_set_queue_size (vlib_main_t * vm, virtio_if_t * vif,
160 u16 queue_id, u16 queue_size)
162 if (!is_pow2 (queue_size))
167 virtio_pci_reg_write_u16 (vif, VIRTIO_QUEUE_SIZE_OFFSET (vif), queue_size);
171 virtio_pci_modern_get_queue_msix_vector (virtio_if_t * vif)
173 u16 queue_msix_vector = 0;
175 virtio_pci_reg_read_u16 (vif, VIRTIO_QUEUE_MSIX_VECTOR_OFFSET (vif));
176 return queue_msix_vector;
180 virtio_pci_modern_set_queue_msix_vector (vlib_main_t * vm, virtio_if_t * vif,
181 u16 queue_msix_vector, u16 queue_id)
183 virtio_pci_modern_set_queue_select (vif, queue_id);
184 virtio_pci_reg_write_u16 (vif, VIRTIO_QUEUE_MSIX_VECTOR_OFFSET (vif),
186 return virtio_pci_modern_get_queue_msix_vector (vif);
190 virtio_pci_modern_get_queue_enable (virtio_if_t * vif, u16 queue_id)
192 u16 queue_enable = 0;
193 virtio_pci_modern_set_queue_select (vif, queue_id);
195 virtio_pci_reg_read_u16 (vif, VIRTIO_QUEUE_ENABLE_OFFSET (vif));
200 virtio_pci_modern_set_queue_enable (virtio_if_t * vif, u16 queue_id,
203 virtio_pci_modern_set_queue_select (vif, queue_id);
204 virtio_pci_reg_write_u16 (vif, VIRTIO_QUEUE_ENABLE_OFFSET (vif),
209 virtio_pci_modern_get_queue_notify_off (vlib_main_t * vm, virtio_if_t * vif,
212 u16 queue_notify_off = 0;
213 virtio_pci_modern_set_queue_select (vif, queue_id);
215 virtio_pci_reg_read_u16 (vif, VIRTIO_QUEUE_NOTIFY_OFF_OFFSET (vif));
216 return queue_notify_off;
220 virtio_pci_modern_get_queue_desc (virtio_if_t * vif)
223 queue_desc = virtio_pci_reg_read_u64 (vif, VIRTIO_QUEUE_DESC_OFFSET (vif));
228 virtio_pci_modern_set_queue_desc (virtio_if_t * vif, u64 queue_desc)
230 virtio_pci_reg_write_u64 (vif, VIRTIO_QUEUE_DESC_OFFSET (vif), queue_desc);
234 virtio_pci_modern_get_queue_driver (virtio_if_t * vif)
236 u64 queue_driver = 0;
238 virtio_pci_reg_read_u64 (vif, VIRTIO_QUEUE_DRIVER_OFFSET (vif));
243 virtio_pci_modern_set_queue_driver (virtio_if_t * vif, u64 queue_driver)
245 virtio_pci_reg_write_u64 (vif, VIRTIO_QUEUE_DRIVER_OFFSET (vif),
250 virtio_pci_modern_get_queue_device (virtio_if_t * vif)
252 u64 queue_device = 0;
254 virtio_pci_reg_read_u64 (vif, VIRTIO_QUEUE_DEVICE_OFFSET (vif));
259 virtio_pci_modern_set_queue_device (virtio_if_t * vif, u64 queue_device)
261 virtio_pci_reg_write_u64 (vif, VIRTIO_QUEUE_DEVICE_OFFSET (vif),
266 virtio_pci_modern_setup_queue (vlib_main_t *vm, virtio_if_t *vif, u16 queue_id,
267 vnet_virtio_vring_t *vring)
269 u64 desc, avail, used;
271 virtio_pci_modern_set_queue_select (vif, queue_id);
275 desc = vlib_physmem_get_pa (vm, vring->packed_desc);
276 avail = vlib_physmem_get_pa (vm, vring->driver_event);
277 used = vlib_physmem_get_pa (vm, vring->device_event);
281 desc = vlib_physmem_get_pa (vm, vring->desc);
282 avail = vlib_physmem_get_pa (vm, vring->avail);
283 used = vlib_physmem_get_pa (vm, vring->used);
286 virtio_pci_modern_set_queue_desc (vif, desc);
287 if (desc != virtio_pci_modern_get_queue_desc (vif))
290 virtio_pci_modern_set_queue_driver (vif, avail);
291 if (avail != virtio_pci_modern_get_queue_driver (vif))
294 virtio_pci_modern_set_queue_device (vif, used);
295 if (used != virtio_pci_modern_get_queue_device (vif))
298 virtio_pci_modern_set_queue_enable (vif, queue_id, 1);
300 if (virtio_pci_modern_get_queue_enable (vif, queue_id))
307 virtio_pci_modern_del_queue (vlib_main_t * vm, virtio_if_t * vif,
310 virtio_pci_modern_set_queue_select (vif, queue_id);
311 virtio_pci_modern_set_queue_enable (vif, queue_id, 0);
312 virtio_pci_modern_set_queue_desc (vif, 0);
313 virtio_pci_modern_set_queue_driver (vif, 0);
314 virtio_pci_modern_set_queue_device (vif, 0);
318 virtio_pci_modern_get_device_mac (vlib_main_t * vm, virtio_if_t * vif)
320 vif->mac_addr32 = virtio_pci_reg_read_u32 (vif, VIRTIO_MAC_OFFSET (vif));
322 virtio_pci_reg_read_u16 (vif, VIRTIO_MAC_OFFSET (vif) + 4);
326 virtio_pci_modern_set_device_mac (vlib_main_t * vm, virtio_if_t * vif)
328 virtio_pci_reg_write_u32 (vif, VIRTIO_MAC_OFFSET (vif), vif->mac_addr32);
329 virtio_pci_reg_write_u16 (vif, VIRTIO_MAC_OFFSET (vif) + 4,
334 virtio_pci_modern_get_device_status (vlib_main_t * vm, virtio_if_t * vif)
337 status = virtio_pci_reg_read_u16 (vif, VIRTIO_STATUS_OFFSET (vif));
342 virtio_pci_modern_get_max_virtqueue_pairs (vlib_main_t * vm,
345 u16 max_virtqueue_pairs = 0;
346 max_virtqueue_pairs =
347 virtio_pci_reg_read_u16 (vif, VIRTIO_MAX_VIRTQUEUE_PAIRS_OFFSET (vif));
348 u16 supported_queues = virtio_pci_modern_get_num_queues (vif);
349 virtio_log_debug (vif, "max-virtqueue-pairs %u, supported-queues %u",
350 max_virtqueue_pairs, supported_queues);
351 return max_virtqueue_pairs;
355 virtio_pci_modern_get_device_mtu (vlib_main_t * vm, virtio_if_t * vif)
358 mtu = virtio_pci_reg_read_u16 (vif, VIRTIO_MTU_OFFSET (vif));
363 virtio_pci_modern_read_config (vlib_main_t * vm, virtio_if_t * vif, void *dst,
369 config_count = virtio_pci_modern_get_config_generation (vif);
370 virtio_pci_modern_get_device_mac (vm, vif);
371 u16 status = virtio_pci_modern_get_device_status (vm, vif);
372 u16 max_queue_pairs =
373 virtio_pci_modern_get_max_virtqueue_pairs (vm, vif);
374 u16 mtu = virtio_pci_modern_get_device_mtu (vm, vif);
375 virtio_log_debug (vif, "status %u, max_queue_pairs %u, mtu %u", status,
376 max_queue_pairs, mtu);
378 while (config_count != virtio_pci_modern_get_config_generation (vif));
382 virtio_pci_modern_write_config (vlib_main_t * vm, virtio_if_t * vif,
383 void *src, int len, u32 addr)
389 virtio_pci_modern_get_isr (vlib_main_t * vm, virtio_if_t * vif)
391 return virtio_pci_reg_read_u8 (vif, VIRTIO_ISR_OFFSET (vif));
395 virtio_pci_modern_notify_queue (vlib_main_t * vm, virtio_if_t * vif,
396 u16 queue_id, u16 queue_notify_off)
398 virtio_pci_reg_write_u16 (vif,
399 VIRTIO_NOTIFICATION_OFFSET (vif) +
400 queue_notify_off, queue_id);
404 virtio_pci_modern_device_debug_config_space (vlib_main_t * vm,
407 // do nothing for now
410 const virtio_pci_func_t virtio_pci_modern_func = {
411 .read_config = virtio_pci_modern_read_config,
412 .write_config = virtio_pci_modern_write_config,
413 .get_device_features = virtio_pci_modern_get_device_features,
414 .get_driver_features = virtio_pci_modern_get_driver_features,
415 .set_driver_features = virtio_pci_modern_set_driver_features,
416 .get_status = virtio_pci_modern_get_status,
417 .set_status = virtio_pci_modern_set_status,
418 .device_reset = virtio_pci_modern_reset,
419 .get_isr = virtio_pci_modern_get_isr,
420 .get_queue_size = virtio_pci_modern_get_queue_size,
421 .set_queue_size = virtio_pci_modern_set_queue_size,
422 .setup_queue = virtio_pci_modern_setup_queue,
423 .del_queue = virtio_pci_modern_del_queue,
424 .get_queue_notify_off = virtio_pci_modern_get_queue_notify_off,
425 .notify_queue = virtio_pci_modern_notify_queue,
426 .set_config_irq = virtio_pci_modern_set_msix_config,
427 .set_queue_irq = virtio_pci_modern_set_queue_msix_vector,
428 .get_mac = virtio_pci_modern_get_device_mac,
429 .set_mac = virtio_pci_modern_set_device_mac,
430 .get_device_status = virtio_pci_modern_get_device_status,
431 .get_max_queue_pairs = virtio_pci_modern_get_max_virtqueue_pairs,
432 .get_mtu = virtio_pci_modern_get_device_mtu,
433 .device_debug_config_space = virtio_pci_modern_device_debug_config_space,
437 * fd.io coding-style-patch-verification: ON
440 * eval: (c-set-style "gnu")