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:
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.
16 #ifndef __included_vmnet_vmnet_h__
17 #define __included_vmnet_vmnet_h__
19 #define foreach_vmxnet3_tx_func_error \
20 _(ERROR_PACKETS, "error packets") \
21 _(LINK_DOWN, "link down") \
22 _(NO_FREE_SLOTS, "no free tx slots")
26 #define _(f,s) VMXNET3_TX_ERROR_##f,
27 foreach_vmxnet3_tx_func_error
30 } vmxnet3_tx_func_error_t;
32 #define foreach_vmxnet3_rxmode_flags \
33 _(0, UCAST, "unicast") \
34 _(1, MCAST, "multicast") \
35 _(2, BCAST, "broadcast") \
36 _(3, ALL_MULTI, "all multicast") \
37 _(4, PROMISC, "promiscuous")
41 #define _(a, b, c) VMXNET3_RXMODE_##b = (1 << a),
42 foreach_vmxnet3_rxmode_flags
47 #define VMXNET3_REG_IMR 0x0000 /* Interrupt Mask Register */
48 #define VMXNET3_REG_TXPROD 0x0600 /* Tx Producer Index */
49 #define VMXNET3_REG_RXPROD 0x0800 /* Rx Producer Index for ring 1 */
50 #define VMXNET3_REG_RXPROD2 0x0A00 /* Rx Producer Index for ring 2 */
54 #define VMXNET3_REG_VRRS 0x0000 /* VMXNET3 Revision Report Selection */
55 #define VMXNET3_REG_UVRS 0x0008 /* UPT Version Report Selection */
56 #define VMXNET3_REG_DSAL 0x0010 /* Driver Shared Address Low */
57 #define VMXNET3_REG_DSAH 0x0018 /* Driver Shared Address High */
58 #define VMXNET3_REG_CMD 0x0020 /* Command */
59 #define VMXNET3_REG_MACL 0x0028 /* MAC Address Low */
60 #define VMXNET3_REG_MACH 0x0030 /* MAC Address High */
61 #define VMXNET3_REG_ICR 0x0038 /* Interrupt Cause Register */
62 #define VMXNET3_REG_ECR 0x0040 /* Event Cause Register */
64 #define VMXNET3_VLAN_LEN 4
65 #define VMXNET3_FCS_LEN 4
66 #define VMXNET3_MTU (1514 + VMXNET3_VLAN_LEN + VMXNET3_FCS_LEN)
68 #define VMXNET3_RXF_BTYPE (1 << 14) /* rx body buffer type */
69 #define VMXNET3_RXF_GEN (1 << 31) /* rx generation */
70 #define VMXNET3_RXCF_IP6 (1 << 20) /* rx ip6 packet */
71 #define VMXNET3_RXCF_IP4 (1 << 21) /* rx ip4 packet */
72 #define VMXNET3_RXCF_GEN (1 << 31) /* rx completion generation */
73 #define VMXNET3_RXC_INDEX (0xFFF) /* rx completion index mask */
75 #define VMXNET3_TXF_GEN (1 << 14) /* tx generation */
76 #define VMXNET3_TXF_EOP (1 << 12) /* tx end of packet */
77 #define VMXNET3_TXF_CQ (1 << 13) /* tx completion request */
78 #define VMXNET3_TXCF_GEN (1 << 31) /* tx completion generation */
79 #define VMXNET3_TXC_INDEX (0xFFF) /* tx completion index mask */
81 #define VMXNET3_RX_RING_SIZE 2
82 #define VMXNET3_INPUT_REFILL_THRESHOLD 32
83 #define VMXNET3_NUM_TX_DESC 1024
84 #define VMXNET3_NUM_TX_COMP VMXNET3_NUM_TX_DESC
85 #define VMXNET3_NUM_RX_DESC 1024
86 #define VMXNET3_NUM_RX_COMP VMXNET3_NUM_RX_DESC
88 #define VMXNET3_VERSION_MAGIC 0x69505845
89 #define VMXNET3_SHARED_MAGIC 0xbabefee1
90 #define VMXNET3_VERSION_SELECT 1
91 #define VMXNET3_UPT_VERSION_SELECT 1
92 #define VMXNET3_MAX_INTRS 25
93 #define VMXNET3_IC_DISABLE_ALL 0x1
95 #define VMXNET3_GOS_BITS_32 (1 << 0)
96 #define VMXNET3_GOS_BITS_64 (2 << 0)
97 #define VMXNET3_GOS_TYPE_LINUX (1 << 2)
98 #define VMXNET3_RXCL_LEN_MASK (0x3FFF) // 14 bits
99 #define VMXNET3_RXCL_ERROR (1 << 14)
100 #define VMXNET3_RXCI_EOP (1 << 14)
101 #define VMXNET3_RXCI_SOP (1 << 15)
103 #define foreach_vmxnet3_device_flags \
104 _(0, INITIALIZED, "initialized") \
105 _(1, ERROR, "error") \
106 _(2, ADMIN_UP, "admin-up") \
108 _(4, LINK_UP, "link-up") \
109 _(5, SHARED_TXQ_LOCK, "shared-txq-lock") \
114 #define _(a, b, c) VMXNET3_DEVICE_F_##b = (1 << a),
115 foreach_vmxnet3_device_flags
119 #define foreach_vmxnet3_set_cmds \
120 _(0, ACTIVATE_DEV, "activate device") \
121 _(1, QUIESCE_DEV, "quiesce device") \
122 _(2, RESET_DEV, "reset device") \
123 _(3, UPDATE_RX_MODE, "update rx mode") \
124 _(4, UPDATE_MAC_FILTERS, "update mac filters") \
125 _(5, UPDATE_VLAN_FILTERS, "update vlan filters") \
126 _(6, UPDATE_RSSIDT, "update rss idt") \
127 _(7, UPDATE_IML, "update iml") \
128 _(8, UPDATE_PMCFG, "update pm cfg") \
129 _(9, UPDATE_FEATURE, "update feature") \
130 _(10, STOP_EMULATION, "stop emulation") \
131 _(11, LOAD_PLUGIN, "load plugin") \
132 _(12, ACTIVATE_VF, "activate vf") \
133 _(13, RESERVED3, "reserved 3") \
134 _(14, RESERVED4, "reservced 4") \
135 _(15, REGISTER_MEMREGS, "register mem regs")
139 #define _(a, b, c) VMXNET3_CMD_##b = (a + 0xCAFE0000),
140 foreach_vmxnet3_set_cmds
144 #define foreach_vmxnet3_get_cmds \
145 _(0, GET_QUEUE_STATUS, "get queue status") \
146 _(1, GET_STATS, "get stats") \
147 _(2, GET_LINK, "get link") \
148 _(3, GET_PERM_MAC_LO, "get perm mac lo") \
149 _(4, GET_PERM_MAC_HI, "get perm mac hi") \
150 _(5, GET_DID_LO, "get did lo") \
151 _(6, GET_DID_HI, "get did hi") \
152 _(7, GET_DEV_EXTRA_INFO, "get dev extra info") \
153 _(8, GET_CONF_INTR, "get conf intr") \
154 _(9, GET_ADAPTIVE_RING_INFO, "get adaptive ring info") \
155 _(10, GET_TXDATA_DESC_SIZE, "gte txdata desc size") \
156 _(11, RESERVED5, "reserved5")
160 #define _(a, b, c) VMXNET3_CMD_##b = (a + 0xF00D0000),
161 foreach_vmxnet3_get_cmds
165 typedef CLIB_PACKED (struct
167 u32 version; u32 guest_info; u32 version_support;
168 u32 upt_version_support; u64 upt_features;
169 u64 driver_data_address; u64 queue_desc_address;
170 u32 driver_data_len; u32 queue_desc_len;
172 u16 max_num_rx_sg; u8 num_tx_queues; u8 num_rx_queues;
174 }) vmxnet3_misc_config;
176 typedef CLIB_PACKED (struct
181 u8 moderation_level[VMXNET3_MAX_INTRS]; u32 control;
183 }) vmxnet3_interrupt_config;
185 typedef CLIB_PACKED (struct
189 u16 pad; u64 multicast_address; u8 vlan_filter[512];
190 }) vmxnet3_rx_filter_config;
192 typedef CLIB_PACKED (struct
194 u32 version; u32 length;
196 }) vmxnet3_variable_config;
198 typedef CLIB_PACKED (struct
202 vmxnet3_misc_config misc;
203 vmxnet3_interrupt_config interrupt;
204 vmxnet3_rx_filter_config rx_filter;
205 vmxnet3_variable_config rss;
206 vmxnet3_variable_config pattern;
207 vmxnet3_variable_config plugin; u32 ecr;
211 typedef CLIB_PACKED (struct
216 }) vmxnet3_queue_status;
218 typedef CLIB_PACKED (struct
220 u32 num_deferred; u32 threshold;
222 }) vmxnet3_tx_queue_control;
224 typedef CLIB_PACKED (struct
228 u64 comp_address; u64 driver_data_address; u64 pad;
231 u32 num_comp; u32 driver_data_len; u8 intr_index;
233 }) vmxnet3_tx_queue_config;
235 typedef CLIB_PACKED (struct
239 u64 ucast_pkts; u64 ucast_bytes; u64 mcast_pkts;
241 u64 bcast_pkts; u64 bcast_bytes; u64 error_pkts;
245 typedef CLIB_PACKED (struct
247 vmxnet3_tx_queue_control ctrl;
248 vmxnet3_tx_queue_config cfg;
249 vmxnet3_queue_status status; vmxnet3_tx_stats stats;
253 typedef CLIB_PACKED (struct
255 u8 update_prod; u8 pad[7];
257 }) vmxnet3_rx_queue_control;
259 typedef CLIB_PACKED (struct
262 u64 comp_address; u64 driver_data_address; u64 pad;
264 u32 num_comp; u32 driver_data_len; u8 intr_index;
266 }) vmxnet3_rx_queue_config;
268 typedef CLIB_PACKED (struct
272 u64 ucast_pkts; u64 ucast_bytes; u64 mcast_pkts;
274 u64 bcast_pkts; u64 bcast_bytes; u64 nobuf_pkts;
278 typedef CLIB_PACKED (struct
280 vmxnet3_rx_queue_control ctrl;
281 vmxnet3_rx_queue_config cfg;
282 vmxnet3_queue_status status; vmxnet3_rx_stats stats;
286 typedef CLIB_PACKED (struct
288 vmxnet3_tx_queue tx; vmxnet3_rx_queue rx;
293 * buffer length -- bits 0-13
294 * buffer type -- bit 14
295 * descriptor type -- bit 15
296 * reserved -- bits 16-30
297 * generation -- bit 31
299 typedef CLIB_PACKED (struct
308 * RX desc index -- bits 0-11
310 * end of packet -- bit 14
311 * start of packet -- bit 15
312 * ring ID -- bits 16-25
313 * RSS hash type -- bits 26-29
314 * checksum not calculated -- bit 30
317 * rss: RSS hash value
320 * data length -- bits 0-13
322 * tag is stripped -- bit 15
323 * tag stripped -- bits 16-31
326 * checksum -- bits 0 - 15
327 * tcp/udp checksum correct-- bit 16
328 * udp packet -- bit 17
329 * tcp packet -- bit 18
330 * ip checksum correct -- bit 19
333 * ip fragment -- bit 22
334 * frame crc correct -- bit 23
335 * completion type -- bits 24-30
336 * generation -- bit 31
338 typedef CLIB_PACKED (struct
347 * TX desc index -- bits 0-11
351 * reserved -- bits 0-23
352 * completion type -- bits 24-30
353 * generation -- bit 31
355 typedef CLIB_PACKED (struct
364 * length -- bits 0-13
365 * generation -- bit 14
367 * descriptor type -- bit 16
369 * MSS, checksum offset -- bits 18-31
371 * header length -- bits 0-9
372 * offload mode -- bits 10-11
373 * end of packet -- bit 12
374 * completion request -- bit 13
376 * vlan tag insertion -- bit 15
377 * tag to insert -- bits 16-31
379 typedef CLIB_PACKED (struct
387 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
398 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
401 } vmxnet3_rx_comp_ring;
405 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
408 vmxnet3_rx_ring rx_ring[VMXNET3_RX_RING_SIZE];
409 vmxnet3_rx_desc *rx_desc[VMXNET3_RX_RING_SIZE];
410 vmxnet3_rx_comp *rx_comp;
411 vmxnet3_rx_comp_ring rx_comp_ring;
416 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
425 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
428 } vmxnet3_tx_comp_ring;
432 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
434 clib_spinlock_t lock;
436 vmxnet3_tx_desc *tx_desc;
437 vmxnet3_tx_comp *tx_comp;
438 vmxnet3_tx_ring tx_ring;
439 vmxnet3_tx_comp_ring tx_comp_ring;
442 typedef CLIB_PACKED (struct
444 vmxnet3_queues queues; vmxnet3_shared shared;
449 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
451 u32 per_interface_next_index;
456 vlib_pci_dev_handle_t pci_dev_handle;
457 vlib_pci_addr_t pci_addr;
481 vmxnet3_device_t *devices;
482 vlib_physmem_region_index_t physmem_region;
483 u32 physmem_region_alloc;
487 extern vmxnet3_main_t vmxnet3_main;
491 vlib_pci_addr_t addr;
499 } vmxnet3_create_if_args_t;
505 vlib_buffer_t buffer;
506 } vmxnet3_input_trace_t;
508 void vmxnet3_create_if (vlib_main_t * vm, vmxnet3_create_if_args_t * args);
509 void vmxnet3_delete_if (vlib_main_t * vm, vmxnet3_device_t * ad);
511 extern clib_error_t *vmxnet3_plugin_api_hookup (vlib_main_t * vm);
512 extern vlib_node_registration_t vmxnet3_input_node;
513 extern vnet_device_class_t vmxnet3_device_class;
516 format_function_t format_vmxnet3_device;
517 format_function_t format_vmxnet3_device_name;
518 format_function_t format_vmxnet3_input_trace;
520 static_always_inline void
521 vmxnet3_reg_write (vmxnet3_device_t * vd, u8 bar, u32 addr, u32 val)
523 *(volatile u32 *) ((u8 *) vd->bar[bar] + addr) = val;
526 static_always_inline u32
527 vmxnet3_reg_read (vmxnet3_device_t * vd, u8 bar, u32 addr)
529 return *(volatile u32 *) (vd->bar[bar] + addr);
532 static_always_inline uword
533 vmxnet3_dma_addr (vlib_main_t * vm, vmxnet3_device_t * vd, void *p)
535 vmxnet3_main_t *vmxm = &vmxnet3_main;
537 return (vd->flags & VMXNET3_DEVICE_F_IOVA) ? pointer_to_uword (p) :
538 vlib_physmem_virtual_to_physical (vm, vmxm->physmem_region, p);
541 static_always_inline void
542 vmxnet3_rx_ring_advance_produce (vmxnet3_rxq_t * rxq, vmxnet3_rx_ring * ring)
545 if (PREDICT_FALSE (ring->produce == rxq->size))
548 ring->gen ^= VMXNET3_RXF_GEN;
552 static_always_inline clib_error_t *
553 vmxnet3_rxq_refill_ring0 (vlib_main_t * vm, vmxnet3_device_t * vd,
556 vmxnet3_rx_desc *rxd;
557 u16 n_refill, n_alloc;
558 vmxnet3_rx_ring *ring;
560 ring = &rxq->rx_ring[0];
561 n_refill = rxq->size - ring->fill;
563 if (PREDICT_TRUE (n_refill <= VMXNET3_INPUT_REFILL_THRESHOLD))
567 vlib_buffer_alloc_to_ring (vm, ring->bufs, ring->produce, rxq->size,
569 if (PREDICT_FALSE (n_alloc != n_refill))
572 vlib_buffer_free_from_ring (vm, ring->bufs, ring->produce, rxq->size,
574 return clib_error_return (0, "buffer alloc failed");
579 rxd = &rxq->rx_desc[0][ring->produce];
581 vlib_get_buffer_data_physical_address (vm, ring->bufs[ring->produce]);
582 rxd->flags = ring->gen | VLIB_BUFFER_DATA_SIZE;
584 vmxnet3_rx_ring_advance_produce (rxq, ring);
589 vmxnet3_reg_write (vd, 0, VMXNET3_REG_RXPROD, ring->produce);
594 static_always_inline clib_error_t *
595 vmxnet3_rxq_refill_ring1 (vlib_main_t * vm, vmxnet3_device_t * vd,
598 vmxnet3_rx_desc *rxd;
599 u16 n_refill, n_alloc;
600 vmxnet3_rx_ring *ring;
602 ring = &rxq->rx_ring[1];
603 n_refill = rxq->size - ring->fill;
605 if (PREDICT_TRUE (n_refill <= VMXNET3_INPUT_REFILL_THRESHOLD))
609 vlib_buffer_alloc_to_ring (vm, ring->bufs, ring->produce, rxq->size,
611 if (PREDICT_FALSE (n_alloc != n_refill))
614 vlib_buffer_free_from_ring (vm, ring->bufs, ring->produce, rxq->size,
616 return clib_error_return (0, "buffer alloc failed");
621 rxd = &rxq->rx_desc[1][ring->produce];
623 vlib_get_buffer_data_physical_address (vm, ring->bufs[ring->produce]);
624 rxd->flags = ring->gen | VLIB_BUFFER_DATA_SIZE | VMXNET3_RXF_BTYPE;
626 vmxnet3_rx_ring_advance_produce (rxq, ring);
631 vmxnet3_reg_write (vd, 0, VMXNET3_REG_RXPROD2, ring->produce);
636 #endif /* __included_vmnet_vmnet_h__ */
638 * fd.io coding-style-patch-verification: ON
641 * eval: (c-set-style "gnu")