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/cdefs.h> 33ae528485SDavid E. O'Brien __FBSDID("$FreeBSD$"); 34ae528485SDavid E. O'Brien 35ae528485SDavid E. O'Brien #include <sys/param.h> 364a144410SRobert Watson #include <sys/capsicum.h> 37ae528485SDavid E. O'Brien #include <sys/cdio.h> 38ae528485SDavid E. O'Brien #include <sys/fcntl.h> 397eac36edSEd Schouten #include <sys/filio.h> 40ae528485SDavid E. O'Brien #include <sys/file.h> 41ae528485SDavid E. O'Brien #include <sys/ioccom.h> 42bfac1583SKonstantin Belousov #include <sys/malloc.h> 43bfac1583SKonstantin Belousov #include <sys/memrange.h> 444ee107ddSKonstantin Belousov #include <sys/pciio.h> 45ae528485SDavid E. O'Brien #include <sys/proc.h> 46ae528485SDavid E. O'Brien #include <sys/syscall.h> 47ae528485SDavid E. O'Brien #include <sys/syscallsubr.h> 48ae528485SDavid E. O'Brien #include <sys/sysctl.h> 49ae528485SDavid E. O'Brien #include <sys/sysent.h> 50ae528485SDavid E. O'Brien #include <sys/sysproto.h> 51ae528485SDavid E. O'Brien #include <sys/systm.h> 5234a77b97SBrooks Davis #include <sys/uio.h> 53ae528485SDavid E. O'Brien 54ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32.h> 55ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32_ioctl.h> 5687842989SKonstantin Belousov #include <compat/freebsd32/freebsd32_misc.h> 57ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32_proto.h> 58ae528485SDavid E. O'Brien 59bfac1583SKonstantin Belousov CTASSERT(sizeof(struct mem_range_op32) == 12); 60ae528485SDavid E. O'Brien 61c750e17cSDavid E. O'Brien static int 62bfac1583SKonstantin Belousov freebsd32_ioctl_memrange(struct thread *td, 63bfac1583SKonstantin Belousov struct freebsd32_ioctl_args *uap, struct file *fp) 64bfac1583SKonstantin Belousov { 65bfac1583SKonstantin Belousov struct mem_range_op mro; 66bfac1583SKonstantin Belousov struct mem_range_op32 mro32; 67bfac1583SKonstantin Belousov int error; 68bfac1583SKonstantin Belousov u_long com; 69bfac1583SKonstantin Belousov 70bfac1583SKonstantin Belousov if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0) 71bfac1583SKonstantin Belousov return (error); 72bfac1583SKonstantin Belousov 73bfac1583SKonstantin Belousov PTRIN_CP(mro32, mro, mo_desc); 74bfac1583SKonstantin Belousov CP(mro32, mro, mo_arg[0]); 75bfac1583SKonstantin Belousov CP(mro32, mro, mo_arg[1]); 76bfac1583SKonstantin Belousov 77bfac1583SKonstantin Belousov com = 0; 78bfac1583SKonstantin Belousov switch (uap->com) { 79bfac1583SKonstantin Belousov case MEMRANGE_GET32: 80bfac1583SKonstantin Belousov com = MEMRANGE_GET; 81bfac1583SKonstantin Belousov break; 82bfac1583SKonstantin Belousov 83bfac1583SKonstantin Belousov case MEMRANGE_SET32: 84bfac1583SKonstantin Belousov com = MEMRANGE_SET; 85bfac1583SKonstantin Belousov break; 86bfac1583SKonstantin Belousov 87bfac1583SKonstantin Belousov default: 88bfac1583SKonstantin Belousov panic("%s: unknown MEMRANGE %#x", __func__, uap->com); 89bfac1583SKonstantin Belousov } 90bfac1583SKonstantin Belousov 91bfac1583SKonstantin Belousov if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0) 92bfac1583SKonstantin Belousov return (error); 93bfac1583SKonstantin Belousov 94bfac1583SKonstantin Belousov if ( (com & IOC_OUT) ) { 95bfac1583SKonstantin Belousov CP(mro, mro32, mo_arg[0]); 96bfac1583SKonstantin Belousov CP(mro, mro32, mo_arg[1]); 97bfac1583SKonstantin Belousov 98bfac1583SKonstantin Belousov error = copyout(&mro32, uap->data, sizeof(mro32)); 99bfac1583SKonstantin Belousov } 100bfac1583SKonstantin Belousov 101bfac1583SKonstantin Belousov return (error); 102bfac1583SKonstantin Belousov } 103bfac1583SKonstantin Belousov 10478272eedSKonstantin Belousov static int 10587842989SKonstantin Belousov freebsd32_ioctl_barmmap(struct thread *td, 10687842989SKonstantin Belousov struct freebsd32_ioctl_args *uap, struct file *fp) 10787842989SKonstantin Belousov { 10887842989SKonstantin Belousov struct pci_bar_mmap32 pbm32; 10987842989SKonstantin Belousov struct pci_bar_mmap pbm; 11087842989SKonstantin Belousov int error; 11187842989SKonstantin Belousov 11287842989SKonstantin Belousov error = copyin(uap->data, &pbm32, sizeof(pbm32)); 11387842989SKonstantin Belousov if (error != 0) 11487842989SKonstantin Belousov return (error); 11587842989SKonstantin Belousov PTRIN_CP(pbm32, pbm, pbm_map_base); 11687842989SKonstantin Belousov CP(pbm32, pbm, pbm_sel); 11787842989SKonstantin Belousov CP(pbm32, pbm, pbm_reg); 11887842989SKonstantin Belousov CP(pbm32, pbm, pbm_flags); 11987842989SKonstantin Belousov CP(pbm32, pbm, pbm_memattr); 12087842989SKonstantin Belousov pbm.pbm_bar_length = PAIR32TO64(uint64_t, pbm32.pbm_bar_length); 12187842989SKonstantin Belousov error = fo_ioctl(fp, PCIOCBARMMAP, (caddr_t)&pbm, td->td_ucred, td); 12287842989SKonstantin Belousov if (error == 0) { 12387842989SKonstantin Belousov PTROUT_CP(pbm, pbm32, pbm_map_base); 12487842989SKonstantin Belousov CP(pbm, pbm32, pbm_map_length); 12587842989SKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN 12687842989SKonstantin Belousov pbm32.pbm_bar_length1 = pbm.pbm_bar_length; 12787842989SKonstantin Belousov pbm32.pbm_bar_length2 = pbm.pbm_bar_length >> 32; 12887842989SKonstantin Belousov #else 12987842989SKonstantin Belousov pbm32.pbm_bar_length1 = pbm.pbm_bar_length >> 32; 13087842989SKonstantin Belousov pbm32.pbm_bar_length2 = pbm.pbm_bar_length; 13187842989SKonstantin Belousov #endif 13287842989SKonstantin Belousov CP(pbm, pbm32, pbm_bar_off); 13387842989SKonstantin Belousov error = copyout(&pbm32, uap->data, sizeof(pbm32)); 13487842989SKonstantin Belousov } 13587842989SKonstantin Belousov return (error); 13687842989SKonstantin Belousov } 13787842989SKonstantin Belousov 13887842989SKonstantin Belousov static int 139fcaf473cSAlexander Motin freebsd32_ioctl_sg(struct thread *td, 140fcaf473cSAlexander Motin struct freebsd32_ioctl_args *uap, struct file *fp) 141fcaf473cSAlexander Motin { 142fcaf473cSAlexander Motin struct sg_io_hdr io; 143fcaf473cSAlexander Motin struct sg_io_hdr32 io32; 144fcaf473cSAlexander Motin int error; 145fcaf473cSAlexander Motin 146fcaf473cSAlexander Motin if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0) 147fcaf473cSAlexander Motin return (error); 148fcaf473cSAlexander Motin 149fcaf473cSAlexander Motin CP(io32, io, interface_id); 150fcaf473cSAlexander Motin CP(io32, io, dxfer_direction); 151fcaf473cSAlexander Motin CP(io32, io, cmd_len); 152fcaf473cSAlexander Motin CP(io32, io, mx_sb_len); 153fcaf473cSAlexander Motin CP(io32, io, iovec_count); 154fcaf473cSAlexander Motin CP(io32, io, dxfer_len); 155fcaf473cSAlexander Motin PTRIN_CP(io32, io, dxferp); 156fcaf473cSAlexander Motin PTRIN_CP(io32, io, cmdp); 157fcaf473cSAlexander Motin PTRIN_CP(io32, io, sbp); 158fcaf473cSAlexander Motin CP(io32, io, timeout); 159fcaf473cSAlexander Motin CP(io32, io, flags); 160fcaf473cSAlexander Motin CP(io32, io, pack_id); 161fcaf473cSAlexander Motin PTRIN_CP(io32, io, usr_ptr); 162fcaf473cSAlexander Motin CP(io32, io, status); 163fcaf473cSAlexander Motin CP(io32, io, masked_status); 164fcaf473cSAlexander Motin CP(io32, io, msg_status); 165fcaf473cSAlexander Motin CP(io32, io, sb_len_wr); 166fcaf473cSAlexander Motin CP(io32, io, host_status); 167fcaf473cSAlexander Motin CP(io32, io, driver_status); 168fcaf473cSAlexander Motin CP(io32, io, resid); 169fcaf473cSAlexander Motin CP(io32, io, duration); 170fcaf473cSAlexander Motin CP(io32, io, info); 171fcaf473cSAlexander Motin 172fcaf473cSAlexander Motin if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0) 173fcaf473cSAlexander Motin return (error); 174fcaf473cSAlexander Motin 175fcaf473cSAlexander Motin CP(io, io32, interface_id); 176fcaf473cSAlexander Motin CP(io, io32, dxfer_direction); 177fcaf473cSAlexander Motin CP(io, io32, cmd_len); 178fcaf473cSAlexander Motin CP(io, io32, mx_sb_len); 179fcaf473cSAlexander Motin CP(io, io32, iovec_count); 180fcaf473cSAlexander Motin CP(io, io32, dxfer_len); 181fcaf473cSAlexander Motin PTROUT_CP(io, io32, dxferp); 182fcaf473cSAlexander Motin PTROUT_CP(io, io32, cmdp); 183fcaf473cSAlexander Motin PTROUT_CP(io, io32, sbp); 184fcaf473cSAlexander Motin CP(io, io32, timeout); 185fcaf473cSAlexander Motin CP(io, io32, flags); 186fcaf473cSAlexander Motin CP(io, io32, pack_id); 187fcaf473cSAlexander Motin PTROUT_CP(io, io32, usr_ptr); 188fcaf473cSAlexander Motin CP(io, io32, status); 189fcaf473cSAlexander Motin CP(io, io32, masked_status); 190fcaf473cSAlexander Motin CP(io, io32, msg_status); 191fcaf473cSAlexander Motin CP(io, io32, sb_len_wr); 192fcaf473cSAlexander Motin CP(io, io32, host_status); 193fcaf473cSAlexander Motin CP(io, io32, driver_status); 194fcaf473cSAlexander Motin CP(io, io32, resid); 195fcaf473cSAlexander Motin CP(io, io32, duration); 196fcaf473cSAlexander Motin CP(io, io32, info); 197fcaf473cSAlexander Motin 198fcaf473cSAlexander Motin error = copyout(&io32, uap->data, sizeof(io32)); 199fcaf473cSAlexander Motin 200fcaf473cSAlexander Motin return (error); 201fcaf473cSAlexander Motin } 202fcaf473cSAlexander Motin 203ae528485SDavid E. O'Brien int 204ae528485SDavid E. O'Brien freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap) 205ae528485SDavid E. O'Brien { 206ae528485SDavid E. O'Brien struct ioctl_args ap /*{ 207ae528485SDavid E. O'Brien int fd; 208ae528485SDavid E. O'Brien u_long com; 209ae528485SDavid E. O'Brien caddr_t data; 210ae528485SDavid E. O'Brien }*/ ; 211ae528485SDavid E. O'Brien struct file *fp; 2127008be5bSPawel Jakub Dawidek cap_rights_t rights; 213ae528485SDavid E. O'Brien int error; 214ae528485SDavid E. O'Brien 215*6b3a9a0fSMateusz Guzik error = fget(td, uap->fd, cap_rights_init_one(&rights, CAP_IOCTL), &fp); 2167008be5bSPawel Jakub Dawidek if (error != 0) 217ae528485SDavid E. O'Brien return (error); 218ae528485SDavid E. O'Brien if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 219ae528485SDavid E. O'Brien fdrop(fp, td); 220ae528485SDavid E. O'Brien return (EBADF); 221ae528485SDavid E. O'Brien } 222ae528485SDavid E. O'Brien 223ae528485SDavid E. O'Brien switch (uap->com) { 224bfac1583SKonstantin Belousov case MEMRANGE_GET32: /* FALLTHROUGH */ 225bfac1583SKonstantin Belousov case MEMRANGE_SET32: 226bfac1583SKonstantin Belousov error = freebsd32_ioctl_memrange(td, uap, fp); 227bfac1583SKonstantin Belousov break; 228bfac1583SKonstantin Belousov 229fcaf473cSAlexander Motin case SG_IO_32: 230fcaf473cSAlexander Motin error = freebsd32_ioctl_sg(td, uap, fp); 231fcaf473cSAlexander Motin break; 232fcaf473cSAlexander Motin 23387842989SKonstantin Belousov case PCIOCBARMMAP_32: 23487842989SKonstantin Belousov error = freebsd32_ioctl_barmmap(td, uap, fp); 23587842989SKonstantin Belousov break; 23687842989SKonstantin Belousov 237ae528485SDavid E. O'Brien default: 238ae528485SDavid E. O'Brien fdrop(fp, td); 239ae528485SDavid E. O'Brien ap.fd = uap->fd; 240ae528485SDavid E. O'Brien ap.com = uap->com; 241ae528485SDavid E. O'Brien PTRIN_CP(*uap, ap, data); 2428451d0ddSKip Macy return sys_ioctl(td, &ap); 243ae528485SDavid E. O'Brien } 2441e67ebb1SKonstantin Belousov 2451e67ebb1SKonstantin Belousov fdrop(fp, td); 24687842989SKonstantin Belousov return (error); 247ae528485SDavid E. O'Brien } 248