Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include <vlib/vlib.h> |
| 17 | #include <vlib/pci/pci.h> |
| 18 | #include <vnet/ethernet/ethernet.h> |
| 19 | #include <vnet/ip/ip4_packet.h> |
| 20 | #include <vnet/ip/ip6_packet.h> |
| 21 | #include <vnet/devices/virtio/virtio.h> |
| 22 | #include <vnet/devices/virtio/virtio_pci_legacy.h> |
| 23 | #include <vnet/devices/virtio/pci.h> |
| 24 | |
| 25 | #define PCI_CONFIG_SIZE(vif) ((vif->msix_enabled == VIRTIO_MSIX_ENABLED) ? \ |
| 26 | 24 : 20) |
| 27 | |
| 28 | static void |
| 29 | virtio_pci_legacy_read_config (vlib_main_t * vm, virtio_if_t * vif, void *dst, |
| 30 | int len, u32 addr) |
| 31 | { |
| 32 | u32 size = 0; |
| 33 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 34 | |
| 35 | while (len > 0) |
| 36 | { |
| 37 | if (len >= 4) |
| 38 | { |
| 39 | size = 4; |
| 40 | vlib_pci_read_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst); |
| 41 | } |
| 42 | else if (len >= 2) |
| 43 | { |
| 44 | size = 2; |
| 45 | vlib_pci_read_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | size = 1; |
| 50 | vlib_pci_read_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst); |
| 51 | } |
| 52 | dst = (u8 *) dst + size; |
| 53 | addr += size; |
| 54 | len -= size; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void |
| 59 | virtio_pci_legacy_write_config (vlib_main_t * vm, virtio_if_t * vif, |
| 60 | void *src, int len, u32 addr) |
| 61 | { |
| 62 | u32 size = 0; |
| 63 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 64 | |
| 65 | while (len > 0) |
| 66 | { |
| 67 | if (len >= 4) |
| 68 | { |
| 69 | size = 4; |
| 70 | vlib_pci_write_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src); |
| 71 | } |
| 72 | else if (len >= 2) |
| 73 | { |
| 74 | size = 2; |
| 75 | vlib_pci_write_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | size = 1; |
| 80 | vlib_pci_write_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src); |
| 81 | } |
| 82 | src = (u8 *) src + size; |
| 83 | addr += size; |
| 84 | len -= size; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static u64 |
| 89 | virtio_pci_legacy_get_host_features (vlib_main_t * vm, virtio_if_t * vif) |
| 90 | { |
| 91 | u32 host_features; |
| 92 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES, |
| 93 | &host_features); |
| 94 | return host_features; |
| 95 | } |
| 96 | |
| 97 | static u64 |
| 98 | virtio_pci_legacy_get_guest_features (vlib_main_t * vm, virtio_if_t * vif) |
| 99 | { |
| 100 | u32 guest_features = 0; |
| 101 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 102 | &guest_features); |
| 103 | vif->features = guest_features; |
| 104 | return guest_features; |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | virtio_pci_legacy_set_guest_features (vlib_main_t * vm, virtio_if_t * vif, |
| 109 | u64 guest_features) |
| 110 | { |
| 111 | if ((guest_features >> 32) != 0) |
| 112 | { |
| 113 | clib_warning ("only 32 bit features are allowed for legacy virtio!"); |
| 114 | } |
| 115 | u32 features = 0; |
Mohsin Kazmi | 133c91c1 | 2020-08-20 17:18:56 +0200 | [diff] [blame^] | 116 | u32 gf = (u32) guest_features; |
| 117 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 118 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
Mohsin Kazmi | 133c91c1 | 2020-08-20 17:18:56 +0200 | [diff] [blame^] | 119 | &gf); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 120 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 121 | &features); |
| 122 | if (features != (u32) guest_features) |
| 123 | { |
| 124 | clib_warning ("legacy set guest features failed!"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static u8 |
| 129 | virtio_pci_legacy_get_status (vlib_main_t * vm, virtio_if_t * vif) |
| 130 | { |
| 131 | u8 status = 0; |
| 132 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status); |
| 133 | return status; |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | virtio_pci_legacy_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status) |
| 138 | { |
| 139 | if (status != VIRTIO_CONFIG_STATUS_RESET) |
| 140 | status |= virtio_pci_legacy_get_status (vm, vif); |
| 141 | vlib_pci_write_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status); |
| 142 | } |
| 143 | |
| 144 | static u8 |
| 145 | virtio_pci_legacy_reset (vlib_main_t * vm, virtio_if_t * vif) |
| 146 | { |
| 147 | virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET); |
| 148 | return virtio_pci_legacy_get_status (vm, vif); |
| 149 | } |
| 150 | |
| 151 | static u8 |
| 152 | virtio_pci_legacy_get_isr (vlib_main_t * vm, virtio_if_t * vif) |
| 153 | { |
| 154 | u8 isr = 0; |
| 155 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &isr); |
| 156 | return isr; |
| 157 | } |
| 158 | |
| 159 | static u16 |
| 160 | virtio_pci_legacy_get_queue_num (vlib_main_t * vm, virtio_if_t * vif, |
| 161 | u16 queue_id) |
| 162 | { |
| 163 | u16 queue_num = 0; |
| 164 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 165 | &queue_id); |
| 166 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM, |
| 167 | &queue_num); |
| 168 | return queue_num; |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | virtio_pci_legacy_set_queue_num (vlib_main_t * vm, virtio_if_t * vif, |
| 173 | u16 queue_id, u16 queue_size) |
| 174 | { |
| 175 | /* do nothing */ |
| 176 | } |
| 177 | |
| 178 | static u8 |
| 179 | virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 180 | u16 queue_id, void *p) |
| 181 | { |
| 182 | u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
Mohsin Kazmi | 133c91c1 | 2020-08-20 17:18:56 +0200 | [diff] [blame^] | 183 | u32 addr2 = 0, a = (u32) addr; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 184 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 185 | &queue_id); |
Mohsin Kazmi | 133c91c1 | 2020-08-20 17:18:56 +0200 | [diff] [blame^] | 186 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &a); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 187 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 188 | &addr2); |
Mohsin Kazmi | 133c91c1 | 2020-08-20 17:18:56 +0200 | [diff] [blame^] | 189 | if (addr == addr2) |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 190 | return 0; |
| 191 | |
| 192 | clib_warning ("legacy queue setup failed!"); |
| 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 198 | u16 queue_id) |
| 199 | { |
| 200 | u32 src = 0; |
| 201 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 202 | &queue_id); |
| 203 | vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src); |
| 204 | } |
| 205 | |
| 206 | inline void |
| 207 | virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif, |
| 208 | u16 queue_id) |
| 209 | { |
| 210 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY, |
| 211 | &queue_id); |
| 212 | } |
| 213 | |
| 214 | /* Enable one vector (0) for Link State Intrerrupt */ |
| 215 | static u16 |
| 216 | virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif, |
| 217 | u16 vec) |
| 218 | { |
| 219 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 220 | &vec); |
| 221 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 222 | &vec); |
| 223 | return vec; |
| 224 | } |
| 225 | |
| 226 | static u16 |
| 227 | virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec, |
| 228 | u16 queue_id) |
| 229 | { |
| 230 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 231 | &queue_id); |
| 232 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 233 | &vec); |
| 234 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 235 | &vec); |
| 236 | return vec; |
| 237 | } |
| 238 | |
| 239 | static void |
| 240 | virtio_pci_legacy_get_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 241 | { |
| 242 | virtio_pci_legacy_read_config (vm, vif, vif->mac_addr, |
| 243 | sizeof (vif->mac_addr), 0); |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | virtio_pci_legacy_set_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 248 | { |
| 249 | virtio_pci_legacy_write_config (vm, vif, vif->mac_addr, |
| 250 | sizeof (vif->mac_addr), 0); |
| 251 | } |
| 252 | |
| 253 | static u16 |
| 254 | virtio_pci_legacy_get_device_status (vlib_main_t * vm, virtio_if_t * vif) |
| 255 | { |
| 256 | u16 status = 0; |
| 257 | virtio_pci_legacy_read_config (vm, vif, &status, |
| 258 | sizeof (status), |
| 259 | STRUCT_OFFSET_OF |
| 260 | (virtio_net_config_t, status)); |
| 261 | return status; |
| 262 | } |
| 263 | |
| 264 | static u16 |
| 265 | virtio_pci_legacy_get_max_queue_pairs (vlib_main_t * vm, virtio_if_t * vif) |
| 266 | { |
| 267 | virtio_net_config_t config; |
| 268 | virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs, |
| 269 | sizeof (config.max_virtqueue_pairs), |
| 270 | STRUCT_OFFSET_OF |
| 271 | (virtio_net_config_t, max_virtqueue_pairs)); |
| 272 | return config.max_virtqueue_pairs; |
| 273 | } |
| 274 | |
| 275 | static u16 |
| 276 | virtio_pci_legacy_get_mtu (vlib_main_t * vm, virtio_if_t * vif) |
| 277 | { |
| 278 | virtio_net_config_t config; |
| 279 | virtio_pci_legacy_read_config (vm, vif, &config.mtu, |
| 280 | sizeof (config.mtu), |
| 281 | STRUCT_OFFSET_OF (virtio_net_config_t, mtu)); |
| 282 | return config.mtu; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | static void |
| 287 | virtio_pci_legacy_device_debug_config_space (vlib_main_t * vm, |
| 288 | virtio_if_t * vif) |
| 289 | { |
| 290 | u32 data_u32; |
| 291 | u16 data_u16; |
| 292 | u8 data_u8; |
| 293 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES, |
| 294 | &data_u32); |
| 295 | vlib_cli_output (vm, "remote features 0x%lx", data_u32); |
| 296 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES, |
| 297 | &data_u32); |
| 298 | vlib_cli_output (vm, "guest features 0x%lx", data_u32); |
| 299 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, |
| 300 | &data_u32); |
| 301 | vlib_cli_output (vm, "queue address 0x%lx", data_u32); |
| 302 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM, |
| 303 | &data_u16); |
| 304 | vlib_cli_output (vm, "queue size 0x%x", data_u16); |
| 305 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 306 | &data_u16); |
| 307 | vlib_cli_output (vm, "queue select 0x%x", data_u16); |
| 308 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY, |
| 309 | &data_u16); |
| 310 | vlib_cli_output (vm, "queue notify 0x%x", data_u16); |
| 311 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8); |
| 312 | vlib_cli_output (vm, "status 0x%x", data_u8); |
| 313 | vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8); |
| 314 | vlib_cli_output (vm, "isr 0x%x", data_u8); |
| 315 | |
| 316 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
| 317 | { |
| 318 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR, |
| 319 | &data_u16); |
| 320 | vlib_cli_output (vm, "config vector 0x%x", data_u16); |
| 321 | u16 queue_id = 0; |
| 322 | vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, |
| 323 | &queue_id); |
| 324 | vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR, |
| 325 | &data_u16); |
| 326 | vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16); |
| 327 | } |
| 328 | |
| 329 | u8 mac[6]; |
| 330 | virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0); |
| 331 | vlib_cli_output (vm, "mac %U", format_ethernet_address, mac); |
| 332 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */ |
| 333 | 6); |
| 334 | vlib_cli_output (vm, "link up/down status 0x%x", data_u16); |
| 335 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), |
| 336 | /* offset to max_virtqueue */ 8); |
| 337 | vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16); |
| 338 | virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */ |
| 339 | 10); |
| 340 | vlib_cli_output (vm, "mtu 0x%x", data_u16); |
| 341 | |
| 342 | u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4; |
| 343 | i += a; |
| 344 | i &= ~a; |
| 345 | for (; i < 64; i += 4) |
| 346 | { |
| 347 | u32 data = 0; |
| 348 | vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data); |
| 349 | vlib_cli_output (vm, "0x%lx", data); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | const virtio_pci_func_t virtio_pci_legacy_func = { |
| 354 | .read_config = virtio_pci_legacy_read_config, |
| 355 | .write_config = virtio_pci_legacy_write_config, |
| 356 | .get_device_features = virtio_pci_legacy_get_host_features, |
| 357 | .get_driver_features = virtio_pci_legacy_get_guest_features, |
| 358 | .set_driver_features = virtio_pci_legacy_set_guest_features, |
| 359 | .get_status = virtio_pci_legacy_get_status, |
| 360 | .set_status = virtio_pci_legacy_set_status, |
| 361 | .device_reset = virtio_pci_legacy_reset, |
| 362 | .get_isr = virtio_pci_legacy_get_isr, |
| 363 | .get_queue_size = virtio_pci_legacy_get_queue_num, |
| 364 | .set_queue_size = virtio_pci_legacy_set_queue_num, |
| 365 | .setup_queue = virtio_pci_legacy_setup_queue, |
| 366 | .del_queue = virtio_pci_legacy_del_queue, |
| 367 | .notify_queue = virtio_pci_legacy_notify_queue, |
| 368 | .set_config_irq = virtio_pci_legacy_set_config_irq, |
| 369 | .set_queue_irq = virtio_pci_legacy_set_queue_irq, |
| 370 | .get_mac = virtio_pci_legacy_get_mac, |
| 371 | .set_mac = virtio_pci_legacy_set_mac, |
| 372 | .get_device_status = virtio_pci_legacy_get_device_status, |
| 373 | .get_max_queue_pairs = virtio_pci_legacy_get_max_queue_pairs, |
| 374 | .get_mtu = virtio_pci_legacy_get_mtu, |
| 375 | .device_debug_config_space = virtio_pci_legacy_device_debug_config_space, |
| 376 | }; |
| 377 | |
| 378 | /* |
| 379 | * fd.io coding-style-patch-verification: ON |
| 380 | * |
| 381 | * Local Variables: |
| 382 | * eval: (c-set-style "gnu") |
| 383 | * End: |
| 384 | */ |