1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011 NetApp, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31
32 #include <machine/vmm.h>
33
34 #include <string.h>
35
36 #include "vmmapi.h"
37 #include "internal.h"
38
39 int
vm_assign_pptdev(struct vmctx * ctx,int bus,int slot,int func)40 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
41 {
42 struct vm_pptdev pptdev;
43
44 bzero(&pptdev, sizeof(pptdev));
45 pptdev.bus = bus;
46 pptdev.slot = slot;
47 pptdev.func = func;
48
49 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
50 }
51
52 int
vm_unassign_pptdev(struct vmctx * ctx,int bus,int slot,int func)53 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
54 {
55 struct vm_pptdev pptdev;
56
57 bzero(&pptdev, sizeof(pptdev));
58 pptdev.bus = bus;
59 pptdev.slot = slot;
60 pptdev.func = func;
61
62 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
63 }
64
65 int
vm_map_pptdev_mmio(struct vmctx * ctx,int bus,int slot,int func,vm_paddr_t gpa,size_t len,vm_paddr_t hpa)66 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
67 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
68 {
69 struct vm_pptdev_mmio pptmmio;
70
71 bzero(&pptmmio, sizeof(pptmmio));
72 pptmmio.bus = bus;
73 pptmmio.slot = slot;
74 pptmmio.func = func;
75 pptmmio.gpa = gpa;
76 pptmmio.len = len;
77 pptmmio.hpa = hpa;
78
79 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
80 }
81
82 int
vm_unmap_pptdev_mmio(struct vmctx * ctx,int bus,int slot,int func,vm_paddr_t gpa,size_t len)83 vm_unmap_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
84 vm_paddr_t gpa, size_t len)
85 {
86 struct vm_pptdev_mmio pptmmio;
87
88 bzero(&pptmmio, sizeof(pptmmio));
89 pptmmio.bus = bus;
90 pptmmio.slot = slot;
91 pptmmio.func = func;
92 pptmmio.gpa = gpa;
93 pptmmio.len = len;
94
95 return (ioctl(ctx->fd, VM_UNMAP_PPTDEV_MMIO, &pptmmio));
96 }
97
98 int
vm_setup_pptdev_msi(struct vmctx * ctx,int bus,int slot,int func,uint64_t addr,uint64_t msg,int numvec)99 vm_setup_pptdev_msi(struct vmctx *ctx, int bus, int slot, int func,
100 uint64_t addr, uint64_t msg, int numvec)
101 {
102 struct vm_pptdev_msi pptmsi;
103
104 bzero(&pptmsi, sizeof(pptmsi));
105 pptmsi.bus = bus;
106 pptmsi.slot = slot;
107 pptmsi.func = func;
108 pptmsi.msg = msg;
109 pptmsi.addr = addr;
110 pptmsi.numvec = numvec;
111
112 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
113 }
114
115 int
vm_setup_pptdev_msix(struct vmctx * ctx,int bus,int slot,int func,int idx,uint64_t addr,uint64_t msg,uint32_t vector_control)116 vm_setup_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func,
117 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
118 {
119 struct vm_pptdev_msix pptmsix;
120
121 bzero(&pptmsix, sizeof(pptmsix));
122 pptmsix.bus = bus;
123 pptmsix.slot = slot;
124 pptmsix.func = func;
125 pptmsix.idx = idx;
126 pptmsix.msg = msg;
127 pptmsix.addr = addr;
128 pptmsix.vector_control = vector_control;
129
130 return (ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix));
131 }
132
133 int
vm_disable_pptdev_msix(struct vmctx * ctx,int bus,int slot,int func)134 vm_disable_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func)
135 {
136 struct vm_pptdev ppt;
137
138 bzero(&ppt, sizeof(ppt));
139 ppt.bus = bus;
140 ppt.slot = slot;
141 ppt.func = func;
142
143 return (ioctl(ctx->fd, VM_PPTDEV_DISABLE_MSIX, &ppt));
144 }
145