2017-05-30 21:13:50 +00:00
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _VIRTIO_PCI_H_
|
|
|
|
#define _VIRTIO_PCI_H_
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2017-10-04 10:19:04 +00:00
|
|
|
#include <linux/virtio_config.h>
|
|
|
|
#include <linux/virtio_pci.h>
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
#include <rte_pci.h>
|
|
|
|
|
2017-09-21 17:44:03 +00:00
|
|
|
#include "spdk/env.h"
|
2017-09-01 17:33:53 +00:00
|
|
|
#include "virtio_dev.h"
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
struct virtqueue;
|
|
|
|
|
2017-10-04 11:19:30 +00:00
|
|
|
#define VIRTIO_MAX_DEVICES 128
|
|
|
|
|
2017-10-04 10:19:04 +00:00
|
|
|
/* Extra status define for readability */
|
|
|
|
#define VIRTIO_CONFIG_S_RESET 0
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
struct virtio_pci_ops {
|
2017-09-01 17:33:53 +00:00
|
|
|
void (*read_dev_cfg)(struct virtio_dev *hw, size_t offset,
|
2017-05-30 21:13:50 +00:00
|
|
|
void *dst, int len);
|
2017-09-01 17:33:53 +00:00
|
|
|
void (*write_dev_cfg)(struct virtio_dev *hw, size_t offset,
|
2017-05-30 21:13:50 +00:00
|
|
|
const void *src, int len);
|
2017-09-01 17:33:53 +00:00
|
|
|
uint8_t (*get_status)(struct virtio_dev *hw);
|
|
|
|
void (*set_status)(struct virtio_dev *hw, uint8_t status);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-10-05 12:46:24 +00:00
|
|
|
/**
|
|
|
|
* Get device features. The features might be already
|
|
|
|
* negotiated with driver (guest) features.
|
|
|
|
*/
|
|
|
|
uint64_t (*get_features)(struct virtio_dev *vdev);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Negotiate and set device features.
|
|
|
|
* The negotiation can fail with return code -1.
|
|
|
|
* This function should also set vdev->negotiated_features field.
|
|
|
|
*/
|
|
|
|
int (*set_features)(struct virtio_dev *vdev, uint64_t features);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
uint8_t (*get_isr)(struct virtio_dev *hw);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
uint16_t (*set_config_irq)(struct virtio_dev *hw, uint16_t vec);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-29 11:03:45 +00:00
|
|
|
/** Deinit and free virtio device */
|
|
|
|
void (*free_vdev)(struct virtio_dev *vdev);
|
2017-09-01 17:33:53 +00:00
|
|
|
uint16_t (*set_queue_irq)(struct virtio_dev *hw, struct virtqueue *vq,
|
2017-05-30 21:13:50 +00:00
|
|
|
uint16_t vec);
|
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
uint16_t (*get_queue_num)(struct virtio_dev *hw, uint16_t queue_id);
|
|
|
|
int (*setup_queue)(struct virtio_dev *hw, struct virtqueue *vq);
|
|
|
|
void (*del_queue)(struct virtio_dev *hw, struct virtqueue *vq);
|
|
|
|
void (*notify_queue)(struct virtio_dev *hw, struct virtqueue *vq);
|
2017-05-30 21:13:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct virtio_hw {
|
2017-09-01 17:33:53 +00:00
|
|
|
struct virtio_dev vdev;
|
2017-05-30 21:13:50 +00:00
|
|
|
uint8_t use_msix;
|
|
|
|
uint32_t notify_off_multiplier;
|
|
|
|
uint8_t *isr;
|
|
|
|
uint16_t *notify_base;
|
2017-09-29 11:19:30 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
/** Mem-mapped resources from given PCI BAR */
|
|
|
|
void *vaddr;
|
|
|
|
|
|
|
|
/** Length of the address space */
|
|
|
|
uint32_t len;
|
|
|
|
} pci_bar[6];
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
struct virtio_pci_common_cfg *common_cfg;
|
2017-09-21 17:44:03 +00:00
|
|
|
struct spdk_pci_device *pci_dev;
|
2017-05-30 21:13:50 +00:00
|
|
|
struct virtio_scsi_config *dev_cfg;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* While virtio_hw is stored in shared memory, this structure stores
|
|
|
|
* some infos that may vary in the multiple process model locally.
|
|
|
|
* For example, the vtpci_ops pointer.
|
|
|
|
*/
|
2017-09-01 17:33:53 +00:00
|
|
|
struct vtpci_internal {
|
2017-05-30 21:13:50 +00:00
|
|
|
const struct virtio_pci_ops *vtpci_ops;
|
|
|
|
struct rte_pci_ioport io;
|
|
|
|
};
|
|
|
|
|
2017-09-25 17:57:57 +00:00
|
|
|
struct virtio_driver {
|
2017-10-04 11:19:30 +00:00
|
|
|
struct vtpci_internal internal[VIRTIO_MAX_DEVICES];
|
2017-09-25 18:41:05 +00:00
|
|
|
TAILQ_HEAD(, virtio_dev) init_ctrlrs;
|
|
|
|
TAILQ_HEAD(, virtio_dev) attached_ctrlrs;
|
2017-09-25 17:57:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern struct virtio_driver g_virtio_driver;
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
static inline int
|
2017-09-01 17:33:53 +00:00
|
|
|
vtpci_with_feature(struct virtio_dev *dev, uint64_t bit)
|
2017-05-30 21:13:50 +00:00
|
|
|
{
|
2017-10-05 12:46:24 +00:00
|
|
|
return (dev->negotiated_features & (1ULL << bit)) != 0;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 11:19:30 +00:00
|
|
|
/**
|
|
|
|
* Init all compatible Virtio PCI devices.
|
|
|
|
*/
|
|
|
|
int vtpci_enumerate_pci(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init virtual PCI layer for given device. This
|
|
|
|
* will set vdev->id field.
|
|
|
|
*/
|
|
|
|
int vtpci_init(struct virtio_dev *vdev, const struct virtio_pci_ops *ops);
|
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
void vtpci_reset(struct virtio_dev *);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
void vtpci_reinit_complete(struct virtio_dev *);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
uint8_t vtpci_get_status(struct virtio_dev *);
|
|
|
|
void vtpci_set_status(struct virtio_dev *, uint8_t);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
uint64_t vtpci_negotiate_features(struct virtio_dev *, uint64_t);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
void vtpci_write_dev_config(struct virtio_dev *, size_t, const void *, int);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
void vtpci_read_dev_config(struct virtio_dev *, size_t, void *, int);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
uint8_t vtpci_isr(struct virtio_dev *);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-10-04 11:19:30 +00:00
|
|
|
const struct virtio_pci_ops *vtpci_ops(struct virtio_dev *dev);
|
|
|
|
|
|
|
|
void vtpci_deinit(uint32_t id);
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
extern const struct virtio_pci_ops virtio_user_ops;
|
|
|
|
|
|
|
|
#endif /* _VIRTIO_PCI_H_ */
|