net/mlx5: fix typos and code style
[dpdk.git] / drivers / net / virtio / virtqueue.h
index d8ae5cd..7fcde56 100644 (file)
 struct rte_mbuf;
 
 /*
- * Per virtio_config.h in Linux.
+ * Per virtio_ring.h in Linux.
  *     For virtio_pci on SMP, we don't need to order with respect to MMIO
  *     accesses through relaxed memory I/O windows, so smp_mb() et al are
  *     sufficient.
  *
+ *     For using virtio to talk to real devices (eg. vDPA) we do need real
+ *     barriers.
  */
-#define virtio_mb()    rte_smp_mb()
-#define virtio_rmb()   rte_smp_rmb()
-#define virtio_wmb()   rte_smp_wmb()
+static inline void
+virtio_mb(uint8_t weak_barriers)
+{
+       if (weak_barriers)
+               rte_smp_mb();
+       else
+               rte_mb();
+}
+
+static inline void
+virtio_rmb(uint8_t weak_barriers)
+{
+       if (weak_barriers)
+               rte_smp_rmb();
+       else
+               rte_cio_rmb();
+}
+
+static inline void
+virtio_wmb(uint8_t weak_barriers)
+{
+       if (weak_barriers)
+               rte_smp_wmb();
+       else
+               rte_cio_wmb();
+}
 
 #ifdef RTE_PMD_PACKET_PREFETCH
 #define rte_packet_prefetch(p)  rte_prefetch1(p)
@@ -256,7 +281,7 @@ struct virtio_tx_region {
 };
 
 static inline int
-desc_is_used(struct vring_packed_desc *desc, struct virtqueue *vq)
+__desc_is_used(struct vring_packed_desc *desc, bool wrap_counter)
 {
        uint16_t used, avail, flags;
 
@@ -264,7 +289,13 @@ desc_is_used(struct vring_packed_desc *desc, struct virtqueue *vq)
        used = !!(flags & VRING_DESC_F_USED(1));
        avail = !!(flags & VRING_DESC_F_AVAIL(1));
 
-       return avail == used && used == vq->used_wrap_counter;
+       return avail == used && used == wrap_counter;
+}
+
+static inline int
+desc_is_used(struct vring_packed_desc *desc, struct virtqueue *vq)
+{
+       return __desc_is_used(desc, vq->used_wrap_counter);
 }
 
 
@@ -325,7 +356,7 @@ virtqueue_enable_intr_packed(struct virtqueue *vq)
 
 
        if (vq->event_flags_shadow == RING_EVENT_FLAGS_DISABLE) {
-               virtio_wmb();
+               virtio_wmb(vq->hw->weak_barriers);
                vq->event_flags_shadow = RING_EVENT_FLAGS_ENABLE;
                *event_flags = vq->event_flags_shadow;
        }
@@ -391,7 +422,7 @@ void vq_ring_free_inorder(struct virtqueue *vq, uint16_t desc_idx,
 static inline void
 vq_update_avail_idx(struct virtqueue *vq)
 {
-       virtio_wmb();
+       virtio_wmb(vq->hw->weak_barriers);
        vq->vq_ring.avail->idx = vq->vq_avail_idx;
 }
 
@@ -415,6 +446,11 @@ vq_update_avail_ring(struct virtqueue *vq, uint16_t desc_idx)
 static inline int
 virtqueue_kick_prepare(struct virtqueue *vq)
 {
+       /*
+        * Ensure updated avail->idx is visible to vhost before reading
+        * the used->flags.
+        */
+       virtio_mb(vq->hw->weak_barriers);
        return !(vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY);
 }
 
@@ -423,20 +459,22 @@ virtqueue_kick_prepare_packed(struct virtqueue *vq)
 {
        uint16_t flags;
 
-       virtio_mb();
+       /*
+        * Ensure updated data is visible to vhost before reading the flags.
+        */
+       virtio_mb(vq->hw->weak_barriers);
        flags = vq->ring_packed.device_event->desc_event_flags;
 
        return flags != RING_EVENT_FLAGS_DISABLE;
 }
 
+/*
+ * virtqueue_kick_prepare*() or the virtio_wmb() should be called
+ * before this function to be sure that all the data is visible to vhost.
+ */
 static inline void
 virtqueue_notify(struct virtqueue *vq)
 {
-       /*
-        * Ensure updated avail->idx is visible to host.
-        * For virtio on IA, the notificaiton is through io port operation
-        * which is a serialization instruction itself.
-        */
        VTPCI_OPS(vq->hw)->notify_queue(vq->hw, vq);
 }