New upstream version 18.02
[deb_dpdk.git] / drivers / net / virtio / virtio_pci.c
index e6da680..6bd22e5 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
  */
 #include <stdint.h>
 
@@ -57,7 +28,8 @@
  * The remaining space is defined by each driver as the per-driver
  * configuration space.
  */
-#define VIRTIO_PCI_CONFIG(hw) (((hw)->use_msix) ? 24 : 20)
+#define VIRTIO_PCI_CONFIG(hw) \
+               (((hw)->use_msix == VIRTIO_MSIX_ENABLED) ? 24 : 20)
 
 static inline int
 check_vq_phys_addr_ok(struct virtqueue *vq)
@@ -553,7 +525,7 @@ get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap)
        uint32_t offset = cap->offset;
        uint8_t *base;
 
-       if (bar > 5) {
+       if (bar >= PCI_MAX_RESOURCE) {
                PMD_INIT_LOG(ERR, "invalid bar: %u", bar);
                return NULL;
        }
@@ -617,7 +589,9 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
                        uint16_t flags = ((uint16_t *)&cap)[1];
 
                        if (flags & PCI_MSIX_ENABLE)
-                               hw->use_msix = 1;
+                               hw->use_msix = VIRTIO_MSIX_ENABLED;
+                       else
+                               hw->use_msix = VIRTIO_MSIX_DISABLED;
                }
 
                if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
@@ -710,3 +684,39 @@ vtpci_init(struct rte_pci_device *dev, struct virtio_hw *hw)
 
        return 0;
 }
+
+enum virtio_msix_status
+vtpci_msix_detect(struct rte_pci_device *dev)
+{
+       uint8_t pos;
+       struct virtio_pci_cap cap;
+       int ret;
+
+       ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
+       if (ret < 0) {
+               PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+               return VIRTIO_MSIX_NONE;
+       }
+
+       while (pos) {
+               ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
+               if (ret < 0) {
+                       PMD_INIT_LOG(ERR,
+                               "failed to read pci cap at pos: %x", pos);
+                       break;
+               }
+
+               if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
+                       uint16_t flags = ((uint16_t *)&cap)[1];
+
+                       if (flags & PCI_MSIX_ENABLE)
+                               return VIRTIO_MSIX_ENABLED;
+                       else
+                               return VIRTIO_MSIX_DISABLED;
+               }
+
+               pos = cap.cap_next;
+       }
+
+       return VIRTIO_MSIX_NONE;
+}