iPXE
Data Structures | Macros | Functions
virtio-pci.h File Reference
#include <ipxe/dma.h>

Go to the source code of this file.

Data Structures

struct  virtio_pci_cap
 
struct  virtio_pci_notify_cap
 
struct  virtio_pci_cfg_cap
 
struct  virtio_pci_common_cfg
 
struct  virtio_pci_region
 
struct  virtio_pci_modern_device
 

Macros

#define VIRTIO_PCI_HOST_FEATURES   0
 
#define VIRTIO_PCI_GUEST_FEATURES   4
 
#define VIRTIO_PCI_QUEUE_PFN   8
 
#define VIRTIO_PCI_QUEUE_NUM   12
 
#define VIRTIO_PCI_QUEUE_SEL   14
 
#define VIRTIO_PCI_QUEUE_NOTIFY   16
 
#define VIRTIO_PCI_STATUS   18
 
#define VIRTIO_PCI_ISR   19
 
#define VIRTIO_PCI_ISR_CONFIG   0x2
 
#define VIRTIO_PCI_CONFIG   20
 
#define VIRTIO_PCI_ABI_VERSION   0
 
#define VIRTIO_PCI_CAP_COMMON_CFG   1 /* Common configuration */
 
#define VIRTIO_PCI_CAP_NOTIFY_CFG   2 /* Notifications */
 
#define VIRTIO_PCI_CAP_ISR_CFG   3 /* ISR access */
 
#define VIRTIO_PCI_CAP_DEVICE_CFG   4 /* Device specific configuration */
 
#define VIRTIO_PCI_CAP_PCI_CFG   5 /* PCI configuration access */
 
#define __u8   uint8_t
 
#define __le16   uint16_t
 
#define __le32   uint32_t
 
#define __le64   uint64_t
 
#define VIRTIO_PCI_REGION_TYPE_MASK   0x00000003
 
#define VIRTIO_PCI_REGION_MEMORY   0x00000001
 
#define VIRTIO_PCI_REGION_PORT   0x00000002
 
#define VIRTIO_PCI_REGION_PCI_CONFIG   0x00000003
 
#define COMMON_OFFSET(field)   offsetof(struct virtio_pci_common_cfg, field)
 

Functions

static u32 vp_get_features (unsigned int ioaddr)
 
static void vp_set_features (unsigned int ioaddr, u32 features)
 
static void vp_get (unsigned int ioaddr, unsigned offset, void *buf, unsigned len)
 
static u8 vp_get_status (unsigned int ioaddr)
 
static void vp_set_status (unsigned int ioaddr, u8 status)
 
static u8 vp_get_isr (unsigned int ioaddr)
 
static void vp_reset (unsigned int ioaddr)
 
static void vp_notify (unsigned int ioaddr, int queue_index)
 
static void vp_del_vq (unsigned int ioaddr, int queue_index)
 
void vp_free_vq (struct vring_virtqueue *vq)
 
int vp_find_vq (unsigned int ioaddr, int queue_index, struct vring_virtqueue *vq, struct dma_device *dma_dev, size_t header_size)
 
void vpm_iowrite8 (struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u8 data, size_t offset)
 
void vpm_iowrite16 (struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u16 data, size_t offset)
 
void vpm_iowrite32 (struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u32 data, size_t offset)
 
static void vpm_iowrite64 (struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u64 data, size_t offset_lo, size_t offset_hi)
 
u8 vpm_ioread8 (struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
 
u16 vpm_ioread16 (struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
 
u32 vpm_ioread32 (struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
 
static void vpm_reset (struct virtio_pci_modern_device *vdev)
 
static u8 vpm_get_status (struct virtio_pci_modern_device *vdev)
 
static void vpm_add_status (struct virtio_pci_modern_device *vdev, u8 status)
 
static u64 vpm_get_features (struct virtio_pci_modern_device *vdev)
 
static void vpm_set_features (struct virtio_pci_modern_device *vdev, u64 features)
 
static void vpm_get (struct virtio_pci_modern_device *vdev, unsigned offset, void *buf, unsigned len)
 
static u8 vpm_get_isr (struct virtio_pci_modern_device *vdev)
 
void vpm_notify (struct virtio_pci_modern_device *vdev, struct vring_virtqueue *vq)
 
int vpm_find_vqs (struct virtio_pci_modern_device *vdev, unsigned nvqs, struct vring_virtqueue *vqs, struct dma_device *dma_dev, size_t header_size)
 
int virtio_pci_find_capability (struct pci_device *pci, uint8_t cfg_type)
 
int virtio_pci_map_capability (struct pci_device *pci, int cap, size_t minlen, u32 align, u32 start, u32 size, struct virtio_pci_region *region)
 
void virtio_pci_unmap_capability (struct virtio_pci_region *region)
 

Macro Definition Documentation

◆ VIRTIO_PCI_HOST_FEATURES

#define VIRTIO_PCI_HOST_FEATURES   0

Definition at line 7 of file virtio-pci.h.

◆ VIRTIO_PCI_GUEST_FEATURES

#define VIRTIO_PCI_GUEST_FEATURES   4

Definition at line 10 of file virtio-pci.h.

◆ VIRTIO_PCI_QUEUE_PFN

#define VIRTIO_PCI_QUEUE_PFN   8

Definition at line 13 of file virtio-pci.h.

◆ VIRTIO_PCI_QUEUE_NUM

#define VIRTIO_PCI_QUEUE_NUM   12

Definition at line 16 of file virtio-pci.h.

◆ VIRTIO_PCI_QUEUE_SEL

#define VIRTIO_PCI_QUEUE_SEL   14

Definition at line 19 of file virtio-pci.h.

◆ VIRTIO_PCI_QUEUE_NOTIFY

#define VIRTIO_PCI_QUEUE_NOTIFY   16

Definition at line 22 of file virtio-pci.h.

◆ VIRTIO_PCI_STATUS

#define VIRTIO_PCI_STATUS   18

Definition at line 25 of file virtio-pci.h.

◆ VIRTIO_PCI_ISR

#define VIRTIO_PCI_ISR   19

Definition at line 30 of file virtio-pci.h.

◆ VIRTIO_PCI_ISR_CONFIG

#define VIRTIO_PCI_ISR_CONFIG   0x2

Definition at line 33 of file virtio-pci.h.

◆ VIRTIO_PCI_CONFIG

#define VIRTIO_PCI_CONFIG   20

Definition at line 37 of file virtio-pci.h.

◆ VIRTIO_PCI_ABI_VERSION

#define VIRTIO_PCI_ABI_VERSION   0

Definition at line 40 of file virtio-pci.h.

◆ VIRTIO_PCI_CAP_COMMON_CFG

#define VIRTIO_PCI_CAP_COMMON_CFG   1 /* Common configuration */

Definition at line 43 of file virtio-pci.h.

◆ VIRTIO_PCI_CAP_NOTIFY_CFG

#define VIRTIO_PCI_CAP_NOTIFY_CFG   2 /* Notifications */

Definition at line 44 of file virtio-pci.h.

◆ VIRTIO_PCI_CAP_ISR_CFG

#define VIRTIO_PCI_CAP_ISR_CFG   3 /* ISR access */

Definition at line 45 of file virtio-pci.h.

◆ VIRTIO_PCI_CAP_DEVICE_CFG

#define VIRTIO_PCI_CAP_DEVICE_CFG   4 /* Device specific configuration */

Definition at line 46 of file virtio-pci.h.

◆ VIRTIO_PCI_CAP_PCI_CFG

#define VIRTIO_PCI_CAP_PCI_CFG   5 /* PCI configuration access */

Definition at line 47 of file virtio-pci.h.

◆ __u8

#define __u8   uint8_t

Definition at line 49 of file virtio-pci.h.

◆ __le16

#define __le16   uint16_t

Definition at line 50 of file virtio-pci.h.

◆ __le32

#define __le32   uint32_t

Definition at line 51 of file virtio-pci.h.

◆ __le64

#define __le64   uint64_t

Definition at line 52 of file virtio-pci.h.

◆ VIRTIO_PCI_REGION_TYPE_MASK

#define VIRTIO_PCI_REGION_TYPE_MASK   0x00000003

Definition at line 110 of file virtio-pci.h.

◆ VIRTIO_PCI_REGION_MEMORY

#define VIRTIO_PCI_REGION_MEMORY   0x00000001

Definition at line 112 of file virtio-pci.h.

◆ VIRTIO_PCI_REGION_PORT

#define VIRTIO_PCI_REGION_PORT   0x00000002

Definition at line 114 of file virtio-pci.h.

◆ VIRTIO_PCI_REGION_PCI_CONFIG

#define VIRTIO_PCI_REGION_PCI_CONFIG   0x00000003

Definition at line 116 of file virtio-pci.h.

◆ COMMON_OFFSET

#define COMMON_OFFSET (   field)    offsetof(struct virtio_pci_common_cfg, field)

Definition at line 239 of file virtio-pci.h.

Function Documentation

◆ vp_get_features()

static u32 vp_get_features ( unsigned int  ioaddr)
inlinestatic

Definition at line 140 of file virtio-pci.h.

141 {
143 }
#define VIRTIO_PCI_HOST_FEATURES
Definition: virtio-pci.h:7
static unsigned long ioaddr
Definition: davicom.c:129
uint32_t inl(volatile uint32_t *io_addr)
Read 32-bit dword from I/O-mapped device.

References inl(), ioaddr, and VIRTIO_PCI_HOST_FEATURES.

Referenced by virtnet_open_legacy(), and virtnet_probe_legacy().

◆ vp_set_features()

static void vp_set_features ( unsigned int  ioaddr,
u32  features 
)
inlinestatic

Definition at line 145 of file virtio-pci.h.

146 {
148 }
static unsigned long ioaddr
Definition: davicom.c:129
#define outl(data, io_addr)
Definition: io.h:329
uint32_t features
Supported features.
Definition: ena.h:16
#define VIRTIO_PCI_GUEST_FEATURES
Definition: virtio-pci.h:10

References features, ioaddr, outl, and VIRTIO_PCI_GUEST_FEATURES.

Referenced by virtnet_open_legacy().

◆ vp_get()

static void vp_get ( unsigned int  ioaddr,
unsigned  offset,
void *  buf,
unsigned  len 
)
inlinestatic

Definition at line 150 of file virtio-pci.h.

152 {
153  u8 *ptr = buf;
154  unsigned i;
155 
156  for (i = 0; i < len; i++)
157  ptr[i] = inb(ioaddr + VIRTIO_PCI_CONFIG + offset + i);
158 }
static unsigned long ioaddr
Definition: davicom.c:129
#define VIRTIO_PCI_CONFIG
Definition: virtio-pci.h:37
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
uint32_t len
Length.
Definition: ena.h:14
uint8_t u8
Definition: stdint.h:19

References inb(), ioaddr, len, offset, and VIRTIO_PCI_CONFIG.

Referenced by virtnet_probe_legacy().

◆ vp_get_status()

static u8 vp_get_status ( unsigned int  ioaddr)
inlinestatic

Definition at line 160 of file virtio-pci.h.

161 {
162  return inb(ioaddr + VIRTIO_PCI_STATUS);
163 }
#define VIRTIO_PCI_STATUS
Definition: virtio-pci.h:25
static unsigned long ioaddr
Definition: davicom.c:129
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.

References inb(), ioaddr, and VIRTIO_PCI_STATUS.

◆ vp_set_status()

static void vp_set_status ( unsigned int  ioaddr,
u8  status 
)
inlinestatic

Definition at line 165 of file virtio-pci.h.

166 {
167  if (status == 0) /* reset */
168  return;
170 }
#define VIRTIO_PCI_STATUS
Definition: virtio-pci.h:25
static unsigned long ioaddr
Definition: davicom.c:129
uint8_t status
Status.
Definition: ena.h:16
#define outb(data, io_addr)
Definition: io.h:309

References ioaddr, outb, status, and VIRTIO_PCI_STATUS.

Referenced by virtnet_open_legacy().

◆ vp_get_isr()

static u8 vp_get_isr ( unsigned int  ioaddr)
inlinestatic

Definition at line 172 of file virtio-pci.h.

173 {
174  return inb(ioaddr + VIRTIO_PCI_ISR);
175 }
static unsigned long ioaddr
Definition: davicom.c:129
#define VIRTIO_PCI_ISR
Definition: virtio-pci.h:30
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.

References inb(), ioaddr, and VIRTIO_PCI_ISR.

Referenced by virtnet_poll().

◆ vp_reset()

static void vp_reset ( unsigned int  ioaddr)
inlinestatic

Definition at line 177 of file virtio-pci.h.

178 {
180  (void)inb(ioaddr + VIRTIO_PCI_ISR);
181 }
#define VIRTIO_PCI_STATUS
Definition: virtio-pci.h:25
static unsigned long ioaddr
Definition: davicom.c:129
#define VIRTIO_PCI_ISR
Definition: virtio-pci.h:30
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.
#define outb(data, io_addr)
Definition: io.h:309

References inb(), ioaddr, outb, VIRTIO_PCI_ISR, and VIRTIO_PCI_STATUS.

Referenced by virtnet_close(), virtnet_open_legacy(), and virtnet_probe_legacy().

◆ vp_notify()

static void vp_notify ( unsigned int  ioaddr,
int  queue_index 
)
inlinestatic

Definition at line 183 of file virtio-pci.h.

184 {
185  outw(queue_index, ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
186 }
#define outw(data, io_addr)
Definition: io.h:319
#define VIRTIO_PCI_QUEUE_NOTIFY
Definition: virtio-pci.h:22
static unsigned long ioaddr
Definition: davicom.c:129

References ioaddr, outw, and VIRTIO_PCI_QUEUE_NOTIFY.

Referenced by vring_kick().

◆ vp_del_vq()

static void vp_del_vq ( unsigned int  ioaddr,
int  queue_index 
)
inlinestatic

Definition at line 188 of file virtio-pci.h.

189 {
190  /* select the queue */
191 
192  outw(queue_index, ioaddr + VIRTIO_PCI_QUEUE_SEL);
193 
194  /* deactivate the queue */
195 
197 }
#define outw(data, io_addr)
Definition: io.h:319
#define VIRTIO_PCI_QUEUE_PFN
Definition: virtio-pci.h:13
static unsigned long ioaddr
Definition: davicom.c:129
#define VIRTIO_PCI_QUEUE_SEL
Definition: virtio-pci.h:19
#define outl(data, io_addr)
Definition: io.h:329

References ioaddr, outl, outw, VIRTIO_PCI_QUEUE_PFN, and VIRTIO_PCI_QUEUE_SEL.

◆ vp_free_vq()

void vp_free_vq ( struct vring_virtqueue vq)

Definition at line 48 of file virtio-pci.c.

49 {
50  if (vq->queue && vq->queue_size) {
51  dma_free(&vq->map, vq->queue, vq->queue_size);
52  vq->queue = NULL;
53  vq->vdata = NULL;
54  vq->queue_size = 0;
55  }
56 }
unsigned char * queue
Definition: virtio-ring.h:77
size_t queue_size
Definition: virtio-ring.h:78
void dma_free(struct dma_mapping *map, void *addr, size_t len)
Unmap and free DMA-coherent buffer.
struct dma_mapping map
Definition: virtio-ring.h:79
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References dma_free(), vring_virtqueue::map, NULL, vring_virtqueue::queue, vring_virtqueue::queue_size, and vring_virtqueue::vdata.

Referenced by virtnet_free_virtqueues().

◆ vp_find_vq()

int vp_find_vq ( unsigned int  ioaddr,
int  queue_index,
struct vring_virtqueue vq,
struct dma_device dma_dev,
size_t  header_size 
)

Definition at line 58 of file virtio-pci.c.

61 {
62  struct vring * vr = &vq->vring;
63  u16 num;
64  int rc;
65 
66  /* select the queue */
67 
68  outw(queue_index, ioaddr + VIRTIO_PCI_QUEUE_SEL);
69 
70  /* check if the queue is available */
71 
73  if (!num) {
74  DBG("VIRTIO-PCI ERROR: queue size is 0\n");
75  return -1;
76  }
77 
78  /* check if the queue is already active */
79 
81  DBG("VIRTIO-PCI ERROR: queue already active\n");
82  return -1;
83  }
84 
85  vq->queue_index = queue_index;
86  vq->dma = dma_dev;
87 
88  /* initialize the queue */
89  rc = vp_alloc_vq(vq, num, header_size);
90  if (rc) {
91  DBG("VIRTIO-PCI ERROR: failed to allocate queue memory\n");
92  return rc;
93  }
94  vring_init(vr, num, vq->queue);
95 
96  /* activate the queue
97  *
98  * NOTE: vr->desc is initialized by vring_init()
99  */
100 
102 
103  return num;
104 }
uint16_t u16
Definition: stdint.h:21
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned char * queue
Definition: virtio-ring.h:77
uint16_t inw(volatile uint16_t *io_addr)
Read 16-bit word from I/O-mapped device.
struct vring vring
Definition: virtio-ring.h:81
struct dma_device * dma
Definition: virtio-ring.h:80
#define outw(data, io_addr)
Definition: io.h:319
static void vring_init(struct vring *vr, unsigned int num, unsigned char *queue)
Definition: virtio-ring.h:96
#define VIRTIO_PCI_QUEUE_PFN
Definition: virtio-pci.h:13
uint32_t num
Definition: multiboot.h:12
static unsigned long ioaddr
Definition: davicom.c:129
struct dma_mapping map
Definition: virtio-ring.h:79
#define VIRTIO_PCI_QUEUE_SEL
Definition: virtio-pci.h:19
#define outl(data, io_addr)
Definition: io.h:329
#define PAGE_SHIFT
Page shift.
Definition: io.h:13
struct vring_desc * desc
Definition: virtio-ring.h:65
#define VIRTIO_PCI_QUEUE_NUM
Definition: virtio-pci.h:16
uint32_t inl(volatile uint32_t *io_addr)
Read 32-bit dword from I/O-mapped device.
static int vp_alloc_vq(struct vring_virtqueue *vq, u16 num, size_t header_size)
Definition: virtio-pci.c:25
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
static __always_inline physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
Definition: dma.h:436

References DBG, vring::desc, vring_virtqueue::dma, dma(), inl(), inw(), ioaddr, vring_virtqueue::map, num, outl, outw, PAGE_SHIFT, vring_virtqueue::queue, vring_virtqueue::queue_index, rc, VIRTIO_PCI_QUEUE_NUM, VIRTIO_PCI_QUEUE_PFN, VIRTIO_PCI_QUEUE_SEL, vp_alloc_vq(), vring_virtqueue::vring, and vring_init().

Referenced by virtnet_open_legacy().

◆ vpm_iowrite8()

void vpm_iowrite8 ( struct virtio_pci_modern_device vdev,
struct virtio_pci_region region,
u8  data,
size_t  offset 
)

Definition at line 119 of file virtio-pci.c.

121 {
122  switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
124  writeb(data, region->base + offset);
125  break;
127  outb(data, region->base + offset);
128  break;
130  prep_pci_cfg_cap(vdev, region, offset, 1);
131  pci_write_config_byte(vdev->pci, CFG_POS(vdev, pci_cfg_data), data);
132  break;
133  default:
134  assert(0);
135  break;
136  }
137 }
struct pci_device * pci
Definition: virtio-pci.h:122
#define VIRTIO_PCI_REGION_PCI_CONFIG
Definition: virtio-pci.h:116
#define VIRTIO_PCI_REGION_MEMORY
Definition: virtio-pci.h:112
void writeb(uint8_t data, volatile uint8_t *io_addr)
Write byte to memory-mapped device.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void prep_pci_cfg_cap(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset, u32 length)
Definition: virtio-pci.c:109
#define CFG_POS(vdev, field)
Definition: virtio-pci.c:106
int pci_write_config_byte(struct pci_device *pci, unsigned int where, uint8_t value)
Write byte to PCI configuration space.
#define VIRTIO_PCI_REGION_TYPE_MASK
Definition: virtio-pci.h:110
#define outb(data, io_addr)
Definition: io.h:309
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define VIRTIO_PCI_REGION_PORT
Definition: virtio-pci.h:114

References assert(), virtio_pci_region::base, CFG_POS, data, virtio_pci_region::flags, offset, outb, virtio_pci_modern_device::pci, pci_write_config_byte(), prep_pci_cfg_cap(), VIRTIO_PCI_REGION_MEMORY, VIRTIO_PCI_REGION_PCI_CONFIG, VIRTIO_PCI_REGION_PORT, VIRTIO_PCI_REGION_TYPE_MASK, and writeb().

Referenced by vpm_add_status(), and vpm_reset().

◆ vpm_iowrite16()

void vpm_iowrite16 ( struct virtio_pci_modern_device vdev,
struct virtio_pci_region region,
u16  data,
size_t  offset 
)

Definition at line 139 of file virtio-pci.c.

141 {
142  data = cpu_to_le16(data);
143  switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
145  writew(data, region->base + offset);
146  break;
148  outw(data, region->base + offset);
149  break;
151  prep_pci_cfg_cap(vdev, region, offset, 2);
152  pci_write_config_word(vdev->pci, CFG_POS(vdev, pci_cfg_data), data);
153  break;
154  default:
155  assert(0);
156  break;
157  }
158 }
struct pci_device * pci
Definition: virtio-pci.h:122
#define VIRTIO_PCI_REGION_PCI_CONFIG
Definition: virtio-pci.h:116
#define VIRTIO_PCI_REGION_MEMORY
Definition: virtio-pci.h:112
#define outw(data, io_addr)
Definition: io.h:319
int pci_write_config_word(struct pci_device *pci, unsigned int where, uint16_t value)
Write 16-bit word to PCI configuration space.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void prep_pci_cfg_cap(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset, u32 length)
Definition: virtio-pci.c:109
#define CFG_POS(vdev, field)
Definition: virtio-pci.c:106
#define VIRTIO_PCI_REGION_TYPE_MASK
Definition: virtio-pci.h:110
#define writew
Definition: w89c840.c:159
#define cpu_to_le16(value)
Definition: byteswap.h:106
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define VIRTIO_PCI_REGION_PORT
Definition: virtio-pci.h:114

References assert(), virtio_pci_region::base, CFG_POS, cpu_to_le16, data, virtio_pci_region::flags, offset, outw, virtio_pci_modern_device::pci, pci_write_config_word(), prep_pci_cfg_cap(), VIRTIO_PCI_REGION_MEMORY, VIRTIO_PCI_REGION_PCI_CONFIG, VIRTIO_PCI_REGION_PORT, VIRTIO_PCI_REGION_TYPE_MASK, and writew.

Referenced by vpm_find_vqs(), and vpm_notify().

◆ vpm_iowrite32()

void vpm_iowrite32 ( struct virtio_pci_modern_device vdev,
struct virtio_pci_region region,
u32  data,
size_t  offset 
)

Definition at line 160 of file virtio-pci.c.

162 {
163  data = cpu_to_le32(data);
164  switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
166  writel(data, region->base + offset);
167  break;
169  outl(data, region->base + offset);
170  break;
172  prep_pci_cfg_cap(vdev, region, offset, 4);
173  pci_write_config_dword(vdev->pci, CFG_POS(vdev, pci_cfg_data), data);
174  break;
175  default:
176  assert(0);
177  break;
178  }
179 }
struct pci_device * pci
Definition: virtio-pci.h:122
#define VIRTIO_PCI_REGION_PCI_CONFIG
Definition: virtio-pci.h:116
#define VIRTIO_PCI_REGION_MEMORY
Definition: virtio-pci.h:112
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void writel(uint32_t data, volatile uint32_t *io_addr)
Write 32-bit dword to memory-mapped device.
static void prep_pci_cfg_cap(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset, u32 length)
Definition: virtio-pci.c:109
#define cpu_to_le32(value)
Definition: byteswap.h:107
#define CFG_POS(vdev, field)
Definition: virtio-pci.c:106
#define outl(data, io_addr)
Definition: io.h:329
#define VIRTIO_PCI_REGION_TYPE_MASK
Definition: virtio-pci.h:110
int pci_write_config_dword(struct pci_device *pci, unsigned int where, uint32_t value)
Write 32-bit dword to PCI configuration space.
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define VIRTIO_PCI_REGION_PORT
Definition: virtio-pci.h:114

References assert(), virtio_pci_region::base, CFG_POS, cpu_to_le32, data, virtio_pci_region::flags, offset, outl, virtio_pci_modern_device::pci, pci_write_config_dword(), prep_pci_cfg_cap(), VIRTIO_PCI_REGION_MEMORY, VIRTIO_PCI_REGION_PCI_CONFIG, VIRTIO_PCI_REGION_PORT, VIRTIO_PCI_REGION_TYPE_MASK, and writel().

Referenced by vpm_get_features(), vpm_iowrite64(), and vpm_set_features().

◆ vpm_iowrite64()

static void vpm_iowrite64 ( struct virtio_pci_modern_device vdev,
struct virtio_pci_region region,
u64  data,
size_t  offset_lo,
size_t  offset_hi 
)
inlinestatic

Definition at line 220 of file virtio-pci.h.

223 {
224  vpm_iowrite32(vdev, region, (u32)data, offset_lo);
225  vpm_iowrite32(vdev, region, data >> 32, offset_hi);
226 }
void vpm_iowrite32(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u32 data, size_t offset)
Definition: virtio-pci.c:160
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint32_t u32
Definition: stdint.h:23

References data, and vpm_iowrite32().

Referenced by vpm_find_vqs().

◆ vpm_ioread8()

u8 vpm_ioread8 ( struct virtio_pci_modern_device vdev,
struct virtio_pci_region region,
size_t  offset 
)

Definition at line 181 of file virtio-pci.c.

183 {
184  uint8_t data;
185  switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
187  data = readb(region->base + offset);
188  break;
190  data = inb(region->base + offset);
191  break;
193  prep_pci_cfg_cap(vdev, region, offset, 1);
194  pci_read_config_byte(vdev->pci, CFG_POS(vdev, pci_cfg_data), &data);
195  break;
196  default:
197  assert(0);
198  data = 0;
199  break;
200  }
201  return data;
202 }
struct pci_device * pci
Definition: virtio-pci.h:122
uint8_t readb(volatile uint8_t *io_addr)
Read byte from memory-mapped device.
#define VIRTIO_PCI_REGION_PCI_CONFIG
Definition: virtio-pci.h:116
#define VIRTIO_PCI_REGION_MEMORY
Definition: virtio-pci.h:112
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void prep_pci_cfg_cap(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset, u32 length)
Definition: virtio-pci.c:109
#define CFG_POS(vdev, field)
Definition: virtio-pci.c:106
#define VIRTIO_PCI_REGION_TYPE_MASK
Definition: virtio-pci.h:110
unsigned char uint8_t
Definition: stdint.h:10
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define VIRTIO_PCI_REGION_PORT
Definition: virtio-pci.h:114
int pci_read_config_byte(struct pci_device *pci, unsigned int where, uint8_t *value)
Read byte from PCI configuration space.

References assert(), virtio_pci_region::base, CFG_POS, data, virtio_pci_region::flags, inb(), offset, virtio_pci_modern_device::pci, pci_read_config_byte(), prep_pci_cfg_cap(), readb(), VIRTIO_PCI_REGION_MEMORY, VIRTIO_PCI_REGION_PCI_CONFIG, VIRTIO_PCI_REGION_PORT, and VIRTIO_PCI_REGION_TYPE_MASK.

Referenced by vpm_add_status(), vpm_get(), vpm_get_isr(), vpm_get_status(), and vpm_reset().

◆ vpm_ioread16()

u16 vpm_ioread16 ( struct virtio_pci_modern_device vdev,
struct virtio_pci_region region,
size_t  offset 
)

Definition at line 204 of file virtio-pci.c.

206 {
207  uint16_t data;
208  switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
210  data = readw(region->base + offset);
211  break;
213  data = inw(region->base + offset);
214  break;
216  prep_pci_cfg_cap(vdev, region, offset, 2);
217  pci_read_config_word(vdev->pci, CFG_POS(vdev, pci_cfg_data), &data);
218  break;
219  default:
220  assert(0);
221  data = 0;
222  break;
223  }
224  return le16_to_cpu(data);
225 }
struct pci_device * pci
Definition: virtio-pci.h:122
unsigned short uint16_t
Definition: stdint.h:11
#define VIRTIO_PCI_REGION_PCI_CONFIG
Definition: virtio-pci.h:116
uint16_t inw(volatile uint16_t *io_addr)
Read 16-bit word from I/O-mapped device.
#define VIRTIO_PCI_REGION_MEMORY
Definition: virtio-pci.h:112
uint16_t readw(volatile uint16_t *io_addr)
Read 16-bit word from memory-mapped device.
int pci_read_config_word(struct pci_device *pci, unsigned int where, uint16_t *value)
Read 16-bit word from PCI configuration space.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void prep_pci_cfg_cap(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset, u32 length)
Definition: virtio-pci.c:109
#define CFG_POS(vdev, field)
Definition: virtio-pci.c:106
#define VIRTIO_PCI_REGION_TYPE_MASK
Definition: virtio-pci.h:110
#define le16_to_cpu(value)
Definition: byteswap.h:112
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define VIRTIO_PCI_REGION_PORT
Definition: virtio-pci.h:114

References assert(), virtio_pci_region::base, CFG_POS, data, virtio_pci_region::flags, inw(), le16_to_cpu, offset, virtio_pci_modern_device::pci, pci_read_config_word(), prep_pci_cfg_cap(), readw(), VIRTIO_PCI_REGION_MEMORY, VIRTIO_PCI_REGION_PCI_CONFIG, VIRTIO_PCI_REGION_PORT, and VIRTIO_PCI_REGION_TYPE_MASK.

Referenced by vpm_find_vqs().

◆ vpm_ioread32()

u32 vpm_ioread32 ( struct virtio_pci_modern_device vdev,
struct virtio_pci_region region,
size_t  offset 
)

Definition at line 227 of file virtio-pci.c.

229 {
230  uint32_t data;
231  switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
233  data = readl(region->base + offset);
234  break;
236  data = inl(region->base + offset);
237  break;
239  prep_pci_cfg_cap(vdev, region, offset, 4);
240  pci_read_config_dword(vdev->pci, CFG_POS(vdev, pci_cfg_data), &data);
241  break;
242  default:
243  assert(0);
244  data = 0;
245  break;
246  }
247  return le32_to_cpu(data);
248 }
struct pci_device * pci
Definition: virtio-pci.h:122
#define VIRTIO_PCI_REGION_PCI_CONFIG
Definition: virtio-pci.h:116
#define le32_to_cpu(value)
Definition: byteswap.h:113
#define VIRTIO_PCI_REGION_MEMORY
Definition: virtio-pci.h:112
uint32_t readl(volatile uint32_t *io_addr)
Read 32-bit dword from memory-mapped device.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
int pci_read_config_dword(struct pci_device *pci, unsigned int where, uint32_t *value)
Read 32-bit dword from PCI configuration space.
static void prep_pci_cfg_cap(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset, u32 length)
Definition: virtio-pci.c:109
#define CFG_POS(vdev, field)
Definition: virtio-pci.c:106
#define VIRTIO_PCI_REGION_TYPE_MASK
Definition: virtio-pci.h:110
unsigned int uint32_t
Definition: stdint.h:12
uint32_t inl(volatile uint32_t *io_addr)
Read 32-bit dword from I/O-mapped device.
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define VIRTIO_PCI_REGION_PORT
Definition: virtio-pci.h:114

References assert(), virtio_pci_region::base, CFG_POS, data, virtio_pci_region::flags, inl(), le32_to_cpu, offset, virtio_pci_modern_device::pci, pci_read_config_dword(), prep_pci_cfg_cap(), readl(), VIRTIO_PCI_REGION_MEMORY, VIRTIO_PCI_REGION_PCI_CONFIG, VIRTIO_PCI_REGION_PORT, and VIRTIO_PCI_REGION_TYPE_MASK.

Referenced by vpm_get_features().

◆ vpm_reset()

static void vpm_reset ( struct virtio_pci_modern_device vdev)
inlinestatic

Definition at line 241 of file virtio-pci.h.

242 {
243  vpm_iowrite8(vdev, &vdev->common, 0, COMMON_OFFSET(device_status));
244  while (vpm_ioread8(vdev, &vdev->common, COMMON_OFFSET(device_status)))
245  mdelay(1);
246 }
u8 vpm_ioread8(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
Definition: virtio-pci.c:181
#define COMMON_OFFSET(field)
Definition: virtio-pci.h:239
struct virtio_pci_region common
Definition: virtio-pci.h:128
void vpm_iowrite8(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u8 data, size_t offset)
Definition: virtio-pci.c:119
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78

References virtio_pci_modern_device::common, COMMON_OFFSET, mdelay(), vpm_ioread8(), and vpm_iowrite8().

Referenced by virtnet_close(), and virtnet_probe_modern().

◆ vpm_get_status()

static u8 vpm_get_status ( struct virtio_pci_modern_device vdev)
inlinestatic

Definition at line 248 of file virtio-pci.h.

249 {
250  return vpm_ioread8(vdev, &vdev->common, COMMON_OFFSET(device_status));
251 }
u8 vpm_ioread8(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
Definition: virtio-pci.c:181
#define COMMON_OFFSET(field)
Definition: virtio-pci.h:239
struct virtio_pci_region common
Definition: virtio-pci.h:128

References virtio_pci_modern_device::common, COMMON_OFFSET, and vpm_ioread8().

Referenced by virtnet_open_modern().

◆ vpm_add_status()

static void vpm_add_status ( struct virtio_pci_modern_device vdev,
u8  status 
)
inlinestatic

Definition at line 253 of file virtio-pci.h.

255 {
256  u8 curr_status = vpm_ioread8(vdev, &vdev->common, COMMON_OFFSET(device_status));
257  vpm_iowrite8(vdev, &vdev->common,
258  curr_status | status, COMMON_OFFSET(device_status));
259 }
u8 vpm_ioread8(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
Definition: virtio-pci.c:181
#define COMMON_OFFSET(field)
Definition: virtio-pci.h:239
struct virtio_pci_region common
Definition: virtio-pci.h:128
void vpm_iowrite8(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u8 data, size_t offset)
Definition: virtio-pci.c:119
uint8_t status
Status.
Definition: ena.h:16
uint8_t u8
Definition: stdint.h:19

References virtio_pci_modern_device::common, COMMON_OFFSET, status, vpm_ioread8(), and vpm_iowrite8().

Referenced by virtnet_open_modern(), and virtnet_probe_modern().

◆ vpm_get_features()

static u64 vpm_get_features ( struct virtio_pci_modern_device vdev)
inlinestatic

Definition at line 261 of file virtio-pci.h.

262 {
263  u32 features_lo, features_hi;
264 
265  vpm_iowrite32(vdev, &vdev->common, 0, COMMON_OFFSET(device_feature_select));
266  features_lo = vpm_ioread32(vdev, &vdev->common, COMMON_OFFSET(device_feature));
267  vpm_iowrite32(vdev, &vdev->common, 1, COMMON_OFFSET(device_feature_select));
268  features_hi = vpm_ioread32(vdev, &vdev->common, COMMON_OFFSET(device_feature));
269 
270  return ((u64)features_hi << 32) | features_lo;
271 }
void vpm_iowrite32(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u32 data, size_t offset)
Definition: virtio-pci.c:160
uint64_t u64
Definition: stdint.h:25
#define COMMON_OFFSET(field)
Definition: virtio-pci.h:239
struct virtio_pci_region common
Definition: virtio-pci.h:128
u32 vpm_ioread32(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
Definition: virtio-pci.c:227
uint32_t u32
Definition: stdint.h:23

References virtio_pci_modern_device::common, COMMON_OFFSET, vpm_ioread32(), and vpm_iowrite32().

Referenced by virtnet_open_modern(), and virtnet_probe_modern().

◆ vpm_set_features()

static void vpm_set_features ( struct virtio_pci_modern_device vdev,
u64  features 
)
inlinestatic

Definition at line 273 of file virtio-pci.h.

275 {
276  u32 features_lo = (u32)features;
277  u32 features_hi = features >> 32;
278 
279  vpm_iowrite32(vdev, &vdev->common, 0, COMMON_OFFSET(guest_feature_select));
280  vpm_iowrite32(vdev, &vdev->common, features_lo, COMMON_OFFSET(guest_feature));
281  vpm_iowrite32(vdev, &vdev->common, 1, COMMON_OFFSET(guest_feature_select));
282  vpm_iowrite32(vdev, &vdev->common, features_hi, COMMON_OFFSET(guest_feature));
283 }
void vpm_iowrite32(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u32 data, size_t offset)
Definition: virtio-pci.c:160
#define u32
Definition: vga.h:21
#define COMMON_OFFSET(field)
Definition: virtio-pci.h:239
uint32_t features
Supported features.
Definition: ena.h:16
struct virtio_pci_region common
Definition: virtio-pci.h:128
uint32_t u32
Definition: stdint.h:23

References virtio_pci_modern_device::common, COMMON_OFFSET, features, u32, and vpm_iowrite32().

Referenced by virtnet_open_modern().

◆ vpm_get()

static void vpm_get ( struct virtio_pci_modern_device vdev,
unsigned  offset,
void *  buf,
unsigned  len 
)
inlinestatic

Definition at line 285 of file virtio-pci.h.

287 {
288  u8 *ptr = buf;
289  unsigned i;
290 
291  for (i = 0; i < len; i++)
292  ptr[i] = vpm_ioread8(vdev, &vdev->device, offset + i);
293 }
u8 vpm_ioread8(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
Definition: virtio-pci.c:181
struct virtio_pci_region device
Definition: virtio-pci.h:131
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
uint32_t len
Length.
Definition: ena.h:14
uint8_t u8
Definition: stdint.h:19

References virtio_pci_modern_device::device, len, offset, and vpm_ioread8().

Referenced by virtnet_probe_modern().

◆ vpm_get_isr()

static u8 vpm_get_isr ( struct virtio_pci_modern_device vdev)
inlinestatic

Definition at line 295 of file virtio-pci.h.

296 {
297  return vpm_ioread8(vdev, &vdev->isr, 0);
298 }
u8 vpm_ioread8(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
Definition: virtio-pci.c:181
struct virtio_pci_region isr
Definition: virtio-pci.h:134

References virtio_pci_modern_device::isr, and vpm_ioread8().

Referenced by virtnet_poll().

◆ vpm_notify()

void vpm_notify ( struct virtio_pci_modern_device vdev,
struct vring_virtqueue vq 
)

Definition at line 354 of file virtio-pci.c.

356 {
357  vpm_iowrite16(vdev, &vq->notification, (u16)vq->queue_index, 0);
358 }
uint16_t u16
Definition: stdint.h:21
struct virtio_pci_region notification
Definition: virtio-ring.h:88
void vpm_iowrite16(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u16 data, size_t offset)
Definition: virtio-pci.c:139

References vring_virtqueue::notification, vring_virtqueue::queue_index, and vpm_iowrite16().

Referenced by vring_kick().

◆ vpm_find_vqs()

int vpm_find_vqs ( struct virtio_pci_modern_device vdev,
unsigned  nvqs,
struct vring_virtqueue vqs,
struct dma_device dma_dev,
size_t  header_size 
)

Definition at line 360 of file virtio-pci.c.

363 {
364  unsigned i;
365  struct vring_virtqueue *vq;
366  u16 size, off;
367  u32 notify_offset_multiplier;
368  int err;
369 
370  if (nvqs > vpm_ioread16(vdev, &vdev->common, COMMON_OFFSET(num_queues))) {
371  return -ENOENT;
372  }
373 
374  /* Read notify_off_multiplier from config space. */
377  notify_off_multiplier),
378  &notify_offset_multiplier);
379 
380  for (i = 0; i < nvqs; i++) {
381  /* Select the queue we're interested in */
382  vpm_iowrite16(vdev, &vdev->common, (u16)i, COMMON_OFFSET(queue_select));
383 
384  /* Check if queue is either not available or already active. */
385  size = vpm_ioread16(vdev, &vdev->common, COMMON_OFFSET(queue_size));
386  /* QEMU has a bug where queues don't revert to inactive on device
387  * reset. Skip checking the queue_enable field until it is fixed.
388  */
389  if (!size /*|| vpm_ioread16(vdev, &vdev->common.queue_enable)*/)
390  return -ENOENT;
391 
392  if (size & (size - 1)) {
393  DBG("VIRTIO-PCI %p: bad queue size %d\n", vdev, size);
394  return -EINVAL;
395  }
396 
397  if (size > MAX_QUEUE_NUM) {
398  /* iPXE networking tends to be not perf critical so there's no
399  * need to accept large queue sizes.
400  */
402  }
403 
404  vq = &vqs[i];
405  vq->queue_index = i;
406  vq->dma = dma_dev;
407 
408  /* get offset of notification word for this vq */
409  off = vpm_ioread16(vdev, &vdev->common, COMMON_OFFSET(queue_notify_off));
410 
411  err = vp_alloc_vq(vq, size, header_size);
412  if (err) {
413  DBG("VIRTIO-PCI %p: failed to allocate queue memory\n", vdev);
414  return err;
415  }
416  vring_init(&vq->vring, size, vq->queue);
417 
418  /* activate the queue */
420 
421  vpm_iowrite64(vdev, &vdev->common,
422  dma(&vq->map, vq->vring.desc),
423  COMMON_OFFSET(queue_desc_lo),
424  COMMON_OFFSET(queue_desc_hi));
425  vpm_iowrite64(vdev, &vdev->common,
426  dma(&vq->map, vq->vring.avail),
427  COMMON_OFFSET(queue_avail_lo),
428  COMMON_OFFSET(queue_avail_hi));
429  vpm_iowrite64(vdev, &vdev->common,
430  dma(&vq->map, vq->vring.used),
431  COMMON_OFFSET(queue_used_lo),
432  COMMON_OFFSET(queue_used_hi));
433 
434  err = virtio_pci_map_capability(vdev->pci,
435  vdev->notify_cap_pos, 2, 2,
436  off * notify_offset_multiplier, 2,
437  &vq->notification);
438  if (err) {
439  return err;
440  }
441  }
442 
443  /* Select and activate all queues. Has to be done last: once we do
444  * this, there's no way to go back except reset.
445  */
446  for (i = 0; i < nvqs; i++) {
447  vq = &vqs[i];
448  vpm_iowrite16(vdev, &vdev->common, (u16)vq->queue_index,
449  COMMON_OFFSET(queue_select));
450  vpm_iowrite16(vdev, &vdev->common, 1, COMMON_OFFSET(queue_enable));
451  }
452  return 0;
453 }
uint16_t u16
Definition: stdint.h:21
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct pci_device * pci
Definition: virtio-pci.h:122
unsigned char * queue
Definition: virtio-ring.h:77
struct vring vring
Definition: virtio-ring.h:81
size_t queue_size
Definition: virtio-ring.h:78
struct dma_device * dma
Definition: virtio-ring.h:80
struct vring_avail * avail
Definition: virtio-ring.h:66
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
#define ENOENT
No such file or directory.
Definition: errno.h:514
u16 vpm_ioread16(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, size_t offset)
Definition: virtio-pci.c:204
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
int virtio_pci_map_capability(struct pci_device *pci, int cap, size_t minlen, u32 align, u32 start, u32 size, struct virtio_pci_region *region)
Definition: virtio-pci.c:276
static void vring_init(struct vring *vr, unsigned int num, unsigned char *queue)
Definition: virtio-ring.h:96
int pci_read_config_dword(struct pci_device *pci, unsigned int where, uint32_t *value)
Read 32-bit dword from PCI configuration space.
#define MAX_QUEUE_NUM
Definition: virtio-ring.h:26
static void vpm_iowrite64(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u64 data, size_t offset_lo, size_t offset_hi)
Definition: virtio-pci.h:220
struct dma_mapping map
Definition: virtio-ring.h:79
#define COMMON_OFFSET(field)
Definition: virtio-pci.h:239
struct virtio_pci_region notification
Definition: virtio-ring.h:88
struct virtio_pci_region common
Definition: virtio-pci.h:128
struct vring_desc * desc
Definition: virtio-ring.h:65
static int vp_alloc_vq(struct vring_virtqueue *vq, u16 num, size_t header_size)
Definition: virtio-pci.c:25
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
static __always_inline physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
Definition: dma.h:436
struct vring_used * used
Definition: virtio-ring.h:67
uint32_t u32
Definition: stdint.h:23
void vpm_iowrite16(struct virtio_pci_modern_device *vdev, struct virtio_pci_region *region, u16 data, size_t offset)
Definition: virtio-pci.c:139

References vring::avail, virtio_pci_modern_device::common, COMMON_OFFSET, DBG, vring::desc, vring_virtqueue::dma, dma(), EINVAL, ENOENT, vring_virtqueue::map, MAX_QUEUE_NUM, vring_virtqueue::notification, virtio_pci_modern_device::notify_cap_pos, offsetof, virtio_pci_modern_device::pci, pci_read_config_dword(), vring_virtqueue::queue, vring_virtqueue::queue_index, vring_virtqueue::queue_size, size, vring::used, virtio_pci_map_capability(), vp_alloc_vq(), vpm_ioread16(), vpm_iowrite16(), vpm_iowrite64(), vring_virtqueue::vring, and vring_init().

Referenced by virtnet_open_modern().

◆ virtio_pci_find_capability()

int virtio_pci_find_capability ( struct pci_device pci,
uint8_t  cfg_type 
)

Definition at line 250 of file virtio-pci.c.

251 {
252  int pos;
253  uint8_t type, bar;
254 
255  for (pos = pci_find_capability(pci, PCI_CAP_ID_VNDR);
256  pos > 0;
257  pos = pci_find_next_capability(pci, pos, PCI_CAP_ID_VNDR)) {
258 
259  pci_read_config_byte(pci, pos + offsetof(struct virtio_pci_cap,
260  cfg_type), &type);
261  pci_read_config_byte(pci, pos + offsetof(struct virtio_pci_cap,
262  bar), &bar);
263 
264  /* Ignore structures with reserved BAR values */
265  if (bar > 0x5) {
266  continue;
267  }
268 
269  if (type == cfg_type) {
270  return pos;
271  }
272  }
273  return 0;
274 }
int pci_find_capability(struct pci_device *pci, int cap)
Look for a PCI capability.
Definition: pciextra.c:38
uint32_t type
Operating system type.
Definition: ena.h:12
#define PCI_CAP_ID_VNDR
Vendor-specific.
Definition: pci.h:96
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
int pci_find_next_capability(struct pci_device *pci, int pos, int cap)
Look for another PCI capability.
Definition: pciextra.c:75
unsigned char uint8_t
Definition: stdint.h:10
int pci_read_config_byte(struct pci_device *pci, unsigned int where, uint8_t *value)
Read byte from PCI configuration space.

References offsetof, PCI_CAP_ID_VNDR, pci_find_capability(), pci_find_next_capability(), pci_read_config_byte(), and type.

Referenced by virtnet_probe_modern().

◆ virtio_pci_map_capability()

int virtio_pci_map_capability ( struct pci_device pci,
int  cap,
size_t  minlen,
u32  align,
u32  start,
u32  size,
struct virtio_pci_region region 
)

Definition at line 276 of file virtio-pci.c.

279 {
280  u8 bar;
281  u32 offset, length, base_raw;
282  unsigned long base;
283 
284  pci_read_config_byte(pci, cap + offsetof(struct virtio_pci_cap, bar), &bar);
286  &offset);
288  &length);
289 
290  if (length <= start) {
291  DBG("VIRTIO-PCI bad capability len %d (>%d expected)\n", length, start);
292  return -EINVAL;
293  }
294  if (length - start < minlen) {
295  DBG("VIRTIO-PCI bad capability len %d (>=%zd expected)\n", length, minlen);
296  return -EINVAL;
297  }
298  length -= start;
299  if (start + offset < offset) {
300  DBG("VIRTIO-PCI map wrap-around %d+%d\n", start, offset);
301  return -EINVAL;
302  }
303  offset += start;
304  if (offset & (align - 1)) {
305  DBG("VIRTIO-PCI offset %d not aligned to %d\n", offset, align);
306  return -EINVAL;
307  }
308  if (length > size) {
309  length = size;
310  }
311 
312  if (minlen + offset < minlen ||
313  minlen + offset > pci_bar_size(pci, PCI_BASE_ADDRESS(bar))) {
314  DBG("VIRTIO-PCI map virtio %zd@%d out of range on bar %i length %ld\n",
315  minlen, offset,
316  bar, pci_bar_size(pci, PCI_BASE_ADDRESS(bar)));
317  return -EINVAL;
318  }
319 
320  region->base = NULL;
321  region->length = length;
322  region->bar = bar;
323 
324  base = pci_bar_start(pci, PCI_BASE_ADDRESS(bar));
325  if (base) {
326  pci_read_config_dword(pci, PCI_BASE_ADDRESS(bar), &base_raw);
327 
328  if (base_raw & PCI_BASE_ADDRESS_SPACE_IO) {
329  /* Region accessed using port I/O */
330  region->base = (void *)(base + offset);
331  region->flags = VIRTIO_PCI_REGION_PORT;
332  } else {
333  /* Region mapped into memory space */
334  region->base = pci_ioremap(pci, base + offset, length);
336  }
337  }
338  if (!region->base) {
339  /* Region accessed via PCI config space window */
340  region->base = (void *)(intptr_t)offset;
342  }
343  return 0;
344 }
static __always_inline void struct dma_mapping size_t size_t align
Definition: dma.h:222
uint32_t base
Base.
Definition: librm.h:252
#define EINVAL
Invalid argument.
Definition: errno.h:428
u16 length
Definition: sky2.h:9
#define VIRTIO_PCI_REGION_PCI_CONFIG
Definition: virtio-pci.h:116
#define VIRTIO_PCI_REGION_MEMORY
Definition: virtio-pci.h:112
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
unsigned long intptr_t
Definition: stdint.h:21
#define PCI_BASE_ADDRESS(n)
PCI base address registers.
Definition: pci.h:61
uint32_t start
Starting offset.
Definition: netvsc.h:12
int pci_read_config_dword(struct pci_device *pci, unsigned int where, uint32_t *value)
Read 32-bit dword from PCI configuration space.
unsigned long pci_bar_start(struct pci_device *pci, unsigned int reg)
Find the start of a PCI BAR.
Definition: pci.c:96
unsigned long pci_bar_size(struct pci_device *pci, unsigned int reg)
Find the size of a PCI BAR.
Definition: pciextra.c:92
#define PCI_BASE_ADDRESS_SPACE_IO
I/O BAR.
Definition: pci.h:68
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
void * pci_ioremap(struct pci_device *pci, unsigned long bus_addr, size_t len)
Map PCI bus address as an I/O address.
#define VIRTIO_PCI_REGION_PORT
Definition: virtio-pci.h:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
uint8_t u8
Definition: stdint.h:19
uint32_t u32
Definition: stdint.h:23
int pci_read_config_byte(struct pci_device *pci, unsigned int where, uint8_t *value)
Read byte from PCI configuration space.

References align, virtio_pci_region::bar, virtio_pci_region::base, base, DBG, EINVAL, virtio_pci_region::flags, length, virtio_pci_region::length, NULL, offset, offsetof, pci_bar_size(), pci_bar_start(), PCI_BASE_ADDRESS, PCI_BASE_ADDRESS_SPACE_IO, pci_ioremap(), pci_read_config_byte(), pci_read_config_dword(), size, start, VIRTIO_PCI_REGION_MEMORY, VIRTIO_PCI_REGION_PCI_CONFIG, and VIRTIO_PCI_REGION_PORT.

Referenced by virtnet_probe_modern(), and vpm_find_vqs().

◆ virtio_pci_unmap_capability()

void virtio_pci_unmap_capability ( struct virtio_pci_region region)

Definition at line 346 of file virtio-pci.c.

347 {
348  unsigned region_type = region->flags & VIRTIO_PCI_REGION_TYPE_MASK;
349  if (region_type == VIRTIO_PCI_REGION_MEMORY) {
350  iounmap(region->base);
351  }
352 }
#define VIRTIO_PCI_REGION_MEMORY
Definition: virtio-pci.h:112
#define VIRTIO_PCI_REGION_TYPE_MASK
Definition: virtio-pci.h:110
void iounmap(volatile const void *io_addr)
Unmap I/O address.

References virtio_pci_region::base, virtio_pci_region::flags, iounmap(), VIRTIO_PCI_REGION_MEMORY, and VIRTIO_PCI_REGION_TYPE_MASK.

Referenced by virtnet_free_virtqueues(), virtnet_probe_modern(), and virtnet_remove().