1ae528485SDavid E. O'Brien /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4ae528485SDavid E. O'Brien * Copyright (c) 2008 David E. O'Brien
5ae528485SDavid E. O'Brien * All rights reserved.
6ae528485SDavid E. O'Brien *
7ae528485SDavid E. O'Brien * Redistribution and use in source and binary forms, with or without
8ae528485SDavid E. O'Brien * modification, are permitted provided that the following conditions
9ae528485SDavid E. O'Brien * are met:
10ae528485SDavid E. O'Brien * 1. Redistributions of source code must retain the above copyright
11ae528485SDavid E. O'Brien * notice, this list of conditions and the following disclaimer.
12ae528485SDavid E. O'Brien * 2. Redistributions in binary form must reproduce the above copyright
13ae528485SDavid E. O'Brien * notice, this list of conditions and the following disclaimer in the
14ae528485SDavid E. O'Brien * documentation and/or other materials provided with the distribution.
15ae528485SDavid E. O'Brien * 3. Neither the name of the author nor the names of its contributors
16ae528485SDavid E. O'Brien * may be used to endorse or promote products derived from this software
17ae528485SDavid E. O'Brien * without specific prior written permission.
18ae528485SDavid E. O'Brien *
19ae528485SDavid E. O'Brien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20ae528485SDavid E. O'Brien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ae528485SDavid E. O'Brien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ae528485SDavid E. O'Brien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23ae528485SDavid E. O'Brien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ae528485SDavid E. O'Brien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ae528485SDavid E. O'Brien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ae528485SDavid E. O'Brien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ae528485SDavid E. O'Brien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ae528485SDavid E. O'Brien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ae528485SDavid E. O'Brien * SUCH DAMAGE.
30ae528485SDavid E. O'Brien */
31ae528485SDavid E. O'Brien
32ae528485SDavid E. O'Brien #include <sys/param.h>
334a144410SRobert Watson #include <sys/capsicum.h>
34ae528485SDavid E. O'Brien #include <sys/cdio.h>
35ae528485SDavid E. O'Brien #include <sys/fcntl.h>
367eac36edSEd Schouten #include <sys/filio.h>
37ae528485SDavid E. O'Brien #include <sys/file.h>
38ae528485SDavid E. O'Brien #include <sys/ioccom.h>
39bfac1583SKonstantin Belousov #include <sys/malloc.h>
40bfac1583SKonstantin Belousov #include <sys/memrange.h>
414ee107ddSKonstantin Belousov #include <sys/pciio.h>
42ae528485SDavid E. O'Brien #include <sys/proc.h>
43ae528485SDavid E. O'Brien #include <sys/syscall.h>
44ae528485SDavid E. O'Brien #include <sys/syscallsubr.h>
45ae528485SDavid E. O'Brien #include <sys/sysctl.h>
46ae528485SDavid E. O'Brien #include <sys/sysproto.h>
47ae528485SDavid E. O'Brien #include <sys/systm.h>
4834a77b97SBrooks Davis #include <sys/uio.h>
49ae528485SDavid E. O'Brien
50ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32.h>
51ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32_ioctl.h>
5287842989SKonstantin Belousov #include <compat/freebsd32/freebsd32_misc.h>
53ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32_proto.h>
54ae528485SDavid E. O'Brien
55bfac1583SKonstantin Belousov CTASSERT(sizeof(struct mem_range_op32) == 12);
56ae528485SDavid E. O'Brien
57c750e17cSDavid E. O'Brien static int
freebsd32_ioctl_memrange(struct thread * td,struct freebsd32_ioctl_args * uap,struct file * fp)58bfac1583SKonstantin Belousov freebsd32_ioctl_memrange(struct thread *td,
59bfac1583SKonstantin Belousov struct freebsd32_ioctl_args *uap, struct file *fp)
60bfac1583SKonstantin Belousov {
61bfac1583SKonstantin Belousov struct mem_range_op mro;
62bfac1583SKonstantin Belousov struct mem_range_op32 mro32;
63bfac1583SKonstantin Belousov int error;
64bfac1583SKonstantin Belousov u_long com;
65bfac1583SKonstantin Belousov
66bfac1583SKonstantin Belousov if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
67bfac1583SKonstantin Belousov return (error);
68bfac1583SKonstantin Belousov
69bfac1583SKonstantin Belousov PTRIN_CP(mro32, mro, mo_desc);
70bfac1583SKonstantin Belousov CP(mro32, mro, mo_arg[0]);
71bfac1583SKonstantin Belousov CP(mro32, mro, mo_arg[1]);
72bfac1583SKonstantin Belousov
73*5b5a48c7SBrooks Davis com = _IOC_NEWTYPE(uap->com, struct mem_range_op);
74bfac1583SKonstantin Belousov
75bfac1583SKonstantin Belousov if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
76bfac1583SKonstantin Belousov return (error);
77bfac1583SKonstantin Belousov
78bfac1583SKonstantin Belousov if ( (com & IOC_OUT) ) {
79bfac1583SKonstantin Belousov CP(mro, mro32, mo_arg[0]);
80bfac1583SKonstantin Belousov CP(mro, mro32, mo_arg[1]);
81bfac1583SKonstantin Belousov
82bfac1583SKonstantin Belousov error = copyout(&mro32, uap->data, sizeof(mro32));
83bfac1583SKonstantin Belousov }
84bfac1583SKonstantin Belousov
85bfac1583SKonstantin Belousov return (error);
86bfac1583SKonstantin Belousov }
87bfac1583SKonstantin Belousov
8878272eedSKonstantin Belousov static int
freebsd32_ioctl_barmmap(struct thread * td,struct freebsd32_ioctl_args * uap,struct file * fp)8987842989SKonstantin Belousov freebsd32_ioctl_barmmap(struct thread *td,
9087842989SKonstantin Belousov struct freebsd32_ioctl_args *uap, struct file *fp)
9187842989SKonstantin Belousov {
9287842989SKonstantin Belousov struct pci_bar_mmap32 pbm32;
9387842989SKonstantin Belousov struct pci_bar_mmap pbm;
9487842989SKonstantin Belousov int error;
9587842989SKonstantin Belousov
9687842989SKonstantin Belousov error = copyin(uap->data, &pbm32, sizeof(pbm32));
9787842989SKonstantin Belousov if (error != 0)
9887842989SKonstantin Belousov return (error);
9987842989SKonstantin Belousov PTRIN_CP(pbm32, pbm, pbm_map_base);
10087842989SKonstantin Belousov CP(pbm32, pbm, pbm_sel);
10187842989SKonstantin Belousov CP(pbm32, pbm, pbm_reg);
10287842989SKonstantin Belousov CP(pbm32, pbm, pbm_flags);
10387842989SKonstantin Belousov CP(pbm32, pbm, pbm_memattr);
10487842989SKonstantin Belousov pbm.pbm_bar_length = PAIR32TO64(uint64_t, pbm32.pbm_bar_length);
10587842989SKonstantin Belousov error = fo_ioctl(fp, PCIOCBARMMAP, (caddr_t)&pbm, td->td_ucred, td);
10687842989SKonstantin Belousov if (error == 0) {
10787842989SKonstantin Belousov PTROUT_CP(pbm, pbm32, pbm_map_base);
10887842989SKonstantin Belousov CP(pbm, pbm32, pbm_map_length);
10987842989SKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
11087842989SKonstantin Belousov pbm32.pbm_bar_length1 = pbm.pbm_bar_length;
11187842989SKonstantin Belousov pbm32.pbm_bar_length2 = pbm.pbm_bar_length >> 32;
11287842989SKonstantin Belousov #else
11387842989SKonstantin Belousov pbm32.pbm_bar_length1 = pbm.pbm_bar_length >> 32;
11487842989SKonstantin Belousov pbm32.pbm_bar_length2 = pbm.pbm_bar_length;
11587842989SKonstantin Belousov #endif
11687842989SKonstantin Belousov CP(pbm, pbm32, pbm_bar_off);
11787842989SKonstantin Belousov error = copyout(&pbm32, uap->data, sizeof(pbm32));
11887842989SKonstantin Belousov }
11987842989SKonstantin Belousov return (error);
12087842989SKonstantin Belousov }
12187842989SKonstantin Belousov
12287842989SKonstantin Belousov static int
freebsd32_ioctl_sg(struct thread * td,struct freebsd32_ioctl_args * uap,struct file * fp)123fcaf473cSAlexander Motin freebsd32_ioctl_sg(struct thread *td,
124fcaf473cSAlexander Motin struct freebsd32_ioctl_args *uap, struct file *fp)
125fcaf473cSAlexander Motin {
126fcaf473cSAlexander Motin struct sg_io_hdr io;
127fcaf473cSAlexander Motin struct sg_io_hdr32 io32;
128fcaf473cSAlexander Motin int error;
129fcaf473cSAlexander Motin
130fcaf473cSAlexander Motin if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
131fcaf473cSAlexander Motin return (error);
132fcaf473cSAlexander Motin
133fcaf473cSAlexander Motin CP(io32, io, interface_id);
134fcaf473cSAlexander Motin CP(io32, io, dxfer_direction);
135fcaf473cSAlexander Motin CP(io32, io, cmd_len);
136fcaf473cSAlexander Motin CP(io32, io, mx_sb_len);
137fcaf473cSAlexander Motin CP(io32, io, iovec_count);
138fcaf473cSAlexander Motin CP(io32, io, dxfer_len);
139fcaf473cSAlexander Motin PTRIN_CP(io32, io, dxferp);
140fcaf473cSAlexander Motin PTRIN_CP(io32, io, cmdp);
141fcaf473cSAlexander Motin PTRIN_CP(io32, io, sbp);
142fcaf473cSAlexander Motin CP(io32, io, timeout);
143fcaf473cSAlexander Motin CP(io32, io, flags);
144fcaf473cSAlexander Motin CP(io32, io, pack_id);
145fcaf473cSAlexander Motin PTRIN_CP(io32, io, usr_ptr);
146fcaf473cSAlexander Motin CP(io32, io, status);
147fcaf473cSAlexander Motin CP(io32, io, masked_status);
148fcaf473cSAlexander Motin CP(io32, io, msg_status);
149fcaf473cSAlexander Motin CP(io32, io, sb_len_wr);
150fcaf473cSAlexander Motin CP(io32, io, host_status);
151fcaf473cSAlexander Motin CP(io32, io, driver_status);
152fcaf473cSAlexander Motin CP(io32, io, resid);
153fcaf473cSAlexander Motin CP(io32, io, duration);
154fcaf473cSAlexander Motin CP(io32, io, info);
155fcaf473cSAlexander Motin
156fcaf473cSAlexander Motin if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
157fcaf473cSAlexander Motin return (error);
158fcaf473cSAlexander Motin
159fcaf473cSAlexander Motin CP(io, io32, interface_id);
160fcaf473cSAlexander Motin CP(io, io32, dxfer_direction);
161fcaf473cSAlexander Motin CP(io, io32, cmd_len);
162fcaf473cSAlexander Motin CP(io, io32, mx_sb_len);
163fcaf473cSAlexander Motin CP(io, io32, iovec_count);
164fcaf473cSAlexander Motin CP(io, io32, dxfer_len);
165fcaf473cSAlexander Motin PTROUT_CP(io, io32, dxferp);
166fcaf473cSAlexander Motin PTROUT_CP(io, io32, cmdp);
167fcaf473cSAlexander Motin PTROUT_CP(io, io32, sbp);
168fcaf473cSAlexander Motin CP(io, io32, timeout);
169fcaf473cSAlexander Motin CP(io, io32, flags);
170fcaf473cSAlexander Motin CP(io, io32, pack_id);
171fcaf473cSAlexander Motin PTROUT_CP(io, io32, usr_ptr);
172fcaf473cSAlexander Motin CP(io, io32, status);
173fcaf473cSAlexander Motin CP(io, io32, masked_status);
174fcaf473cSAlexander Motin CP(io, io32, msg_status);
175fcaf473cSAlexander Motin CP(io, io32, sb_len_wr);
176fcaf473cSAlexander Motin CP(io, io32, host_status);
177fcaf473cSAlexander Motin CP(io, io32, driver_status);
178fcaf473cSAlexander Motin CP(io, io32, resid);
179fcaf473cSAlexander Motin CP(io, io32, duration);
180fcaf473cSAlexander Motin CP(io, io32, info);
181fcaf473cSAlexander Motin
182fcaf473cSAlexander Motin error = copyout(&io32, uap->data, sizeof(io32));
183fcaf473cSAlexander Motin
184fcaf473cSAlexander Motin return (error);
185fcaf473cSAlexander Motin }
186fcaf473cSAlexander Motin
187ae528485SDavid E. O'Brien int
freebsd32_ioctl(struct thread * td,struct freebsd32_ioctl_args * uap)188ae528485SDavid E. O'Brien freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
189ae528485SDavid E. O'Brien {
190ae528485SDavid E. O'Brien struct ioctl_args ap /*{
191ae528485SDavid E. O'Brien int fd;
192ae528485SDavid E. O'Brien u_long com;
193ae528485SDavid E. O'Brien caddr_t data;
194ae528485SDavid E. O'Brien }*/ ;
195ae528485SDavid E. O'Brien struct file *fp;
1967008be5bSPawel Jakub Dawidek cap_rights_t rights;
197ae528485SDavid E. O'Brien int error;
198ae528485SDavid E. O'Brien
1996b3a9a0fSMateusz Guzik error = fget(td, uap->fd, cap_rights_init_one(&rights, CAP_IOCTL), &fp);
2007008be5bSPawel Jakub Dawidek if (error != 0)
201ae528485SDavid E. O'Brien return (error);
202ae528485SDavid E. O'Brien if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
203ae528485SDavid E. O'Brien fdrop(fp, td);
204ae528485SDavid E. O'Brien return (EBADF);
205ae528485SDavid E. O'Brien }
206ae528485SDavid E. O'Brien
207ae528485SDavid E. O'Brien switch (uap->com) {
208bfac1583SKonstantin Belousov case MEMRANGE_GET32: /* FALLTHROUGH */
209bfac1583SKonstantin Belousov case MEMRANGE_SET32:
210bfac1583SKonstantin Belousov error = freebsd32_ioctl_memrange(td, uap, fp);
211bfac1583SKonstantin Belousov break;
212bfac1583SKonstantin Belousov
213fcaf473cSAlexander Motin case SG_IO_32:
214fcaf473cSAlexander Motin error = freebsd32_ioctl_sg(td, uap, fp);
215fcaf473cSAlexander Motin break;
216fcaf473cSAlexander Motin
21787842989SKonstantin Belousov case PCIOCBARMMAP_32:
21887842989SKonstantin Belousov error = freebsd32_ioctl_barmmap(td, uap, fp);
21987842989SKonstantin Belousov break;
22087842989SKonstantin Belousov
221ae528485SDavid E. O'Brien default:
222ae528485SDavid E. O'Brien fdrop(fp, td);
223ae528485SDavid E. O'Brien ap.fd = uap->fd;
224ae528485SDavid E. O'Brien ap.com = uap->com;
225ae528485SDavid E. O'Brien PTRIN_CP(*uap, ap, data);
2268451d0ddSKip Macy return sys_ioctl(td, &ap);
227ae528485SDavid E. O'Brien }
2281e67ebb1SKonstantin Belousov
2291e67ebb1SKonstantin Belousov fdrop(fp, td);
23087842989SKonstantin Belousov return (error);
231ae528485SDavid E. O'Brien }
232