2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2008 David E. O'Brien
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. Neither the name of the author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
38 #include <sys/fcntl.h>
39 #include <sys/filio.h>
41 #include <sys/ioccom.h>
42 #include <sys/malloc.h>
43 #include <sys/memrange.h>
44 #include <sys/pciio.h>
46 #include <sys/syscall.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 #include <sys/sysproto.h>
51 #include <sys/systm.h>
54 #include <compat/freebsd32/freebsd32.h>
55 #include <compat/freebsd32/freebsd32_ioctl.h>
56 #include <compat/freebsd32/freebsd32_misc.h>
57 #include <compat/freebsd32/freebsd32_proto.h>
59 CTASSERT(sizeof(struct mem_range_op32) == 12);
62 freebsd32_ioctl_memrange(struct thread *td,
63 struct freebsd32_ioctl_args *uap, struct file *fp)
65 struct mem_range_op mro;
66 struct mem_range_op32 mro32;
70 if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
73 PTRIN_CP(mro32, mro, mo_desc);
74 CP(mro32, mro, mo_arg[0]);
75 CP(mro32, mro, mo_arg[1]);
77 com = _IOC_NEWTYPE(uap->com, struct mem_range_op);
79 if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
82 if ( (com & IOC_OUT) ) {
83 CP(mro, mro32, mo_arg[0]);
84 CP(mro, mro32, mo_arg[1]);
86 error = copyout(&mro32, uap->data, sizeof(mro32));
93 freebsd32_ioctl_barmmap(struct thread *td,
94 struct freebsd32_ioctl_args *uap, struct file *fp)
96 struct pci_bar_mmap32 pbm32;
97 struct pci_bar_mmap pbm;
100 error = copyin(uap->data, &pbm32, sizeof(pbm32));
103 PTRIN_CP(pbm32, pbm, pbm_map_base);
104 CP(pbm32, pbm, pbm_sel);
105 CP(pbm32, pbm, pbm_reg);
106 CP(pbm32, pbm, pbm_flags);
107 CP(pbm32, pbm, pbm_memattr);
108 pbm.pbm_bar_length = PAIR32TO64(uint64_t, pbm32.pbm_bar_length);
109 error = fo_ioctl(fp, PCIOCBARMMAP, (caddr_t)&pbm, td->td_ucred, td);
111 PTROUT_CP(pbm, pbm32, pbm_map_base);
112 CP(pbm, pbm32, pbm_map_length);
113 #if BYTE_ORDER == LITTLE_ENDIAN
114 pbm32.pbm_bar_length1 = pbm.pbm_bar_length;
115 pbm32.pbm_bar_length2 = pbm.pbm_bar_length >> 32;
117 pbm32.pbm_bar_length1 = pbm.pbm_bar_length >> 32;
118 pbm32.pbm_bar_length2 = pbm.pbm_bar_length;
120 CP(pbm, pbm32, pbm_bar_off);
121 error = copyout(&pbm32, uap->data, sizeof(pbm32));
127 freebsd32_ioctl_sg(struct thread *td,
128 struct freebsd32_ioctl_args *uap, struct file *fp)
131 struct sg_io_hdr32 io32;
134 if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
137 CP(io32, io, interface_id);
138 CP(io32, io, dxfer_direction);
139 CP(io32, io, cmd_len);
140 CP(io32, io, mx_sb_len);
141 CP(io32, io, iovec_count);
142 CP(io32, io, dxfer_len);
143 PTRIN_CP(io32, io, dxferp);
144 PTRIN_CP(io32, io, cmdp);
145 PTRIN_CP(io32, io, sbp);
146 CP(io32, io, timeout);
148 CP(io32, io, pack_id);
149 PTRIN_CP(io32, io, usr_ptr);
150 CP(io32, io, status);
151 CP(io32, io, masked_status);
152 CP(io32, io, msg_status);
153 CP(io32, io, sb_len_wr);
154 CP(io32, io, host_status);
155 CP(io32, io, driver_status);
157 CP(io32, io, duration);
160 if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
163 CP(io, io32, interface_id);
164 CP(io, io32, dxfer_direction);
165 CP(io, io32, cmd_len);
166 CP(io, io32, mx_sb_len);
167 CP(io, io32, iovec_count);
168 CP(io, io32, dxfer_len);
169 PTROUT_CP(io, io32, dxferp);
170 PTROUT_CP(io, io32, cmdp);
171 PTROUT_CP(io, io32, sbp);
172 CP(io, io32, timeout);
174 CP(io, io32, pack_id);
175 PTROUT_CP(io, io32, usr_ptr);
176 CP(io, io32, status);
177 CP(io, io32, masked_status);
178 CP(io, io32, msg_status);
179 CP(io, io32, sb_len_wr);
180 CP(io, io32, host_status);
181 CP(io, io32, driver_status);
183 CP(io, io32, duration);
186 error = copyout(&io32, uap->data, sizeof(io32));
192 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
194 struct ioctl_args ap /*{
203 error = fget(td, uap->fd, cap_rights_init_one(&rights, CAP_IOCTL), &fp);
206 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
212 case MEMRANGE_GET32: /* FALLTHROUGH */
214 error = freebsd32_ioctl_memrange(td, uap, fp);
218 error = freebsd32_ioctl_sg(td, uap, fp);
221 case PCIOCBARMMAP_32:
222 error = freebsd32_ioctl_barmmap(td, uap, fp);
229 PTRIN_CP(*uap, ap, data);
230 return sys_ioctl(td, &ap);