1/*
2 * Copyright (c) 2015, AVAGO Tech. All rights reserved. Author: Kashyap Desai,
3 * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Kashyap Desai,
4 * Sibananda Sahu Support: freebsdraid@avagotech.com
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer. 2. Redistributions
12 * in binary form must reproduce the above copyright notice, this list of
13 * conditions and the following disclaimer in the documentation and/or other
14 * materials provided with the distribution. 3. Neither the name of the
15 * <ORGANIZATION> nor the names of its contributors may be used to endorse or
16 * promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND 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 COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * The views and conclusions contained in the software and documentation are
32 * those of the authors and should not be interpreted as representing
33 * official policies,either expressed or implied, of the FreeBSD Project.
34 *
35 * Send feedback to: <megaraidfbsd@avagotech.com> Mail to: AVAGO TECHNOLOGIES, 1621
36 * Barber Lane, Milpitas, CA 95035 ATTN: MegaRaid FreeBSD
37 *
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: stable/11/sys/dev/mrsas/mrsas_linux.c 340180 2018-11-06 12:57:38Z emaste $");
42
43#include <sys/param.h>
44#include <sys/systm.h>
45
46#if (__FreeBSD_version >= 1001511)
47#include <sys/capsicum.h>
48#elif (__FreeBSD_version > 900000)
49#include <sys/capability.h>
50#endif
51
52#include <sys/conf.h>
53#include <sys/kernel.h>
54#include <sys/module.h>
55#include <sys/file.h>
56#include <sys/proc.h>
57#include <machine/bus.h>
58
59#if defined(__amd64__)			/* Assume amd64 wants 32 bit Linux */
60#include <machine/../linux32/linux.h>
61#include <machine/../linux32/linux32_proto.h>
62#else
63#include <machine/../linux/linux.h>
64#include <machine/../linux/linux_proto.h>
65#endif
66#include <compat/linux/linux_ioctl.h>
67#include <compat/linux/linux_util.h>
68
69#include <dev/mrsas/mrsas.h>
70#include <dev/mrsas/mrsas_ioctl.h>
71
72/* There are multiple ioctl number ranges that need to be handled */
73#define	MRSAS_LINUX_IOCTL_MIN  0x4d00
74#define	MRSAS_LINUX_IOCTL_MAX  0x4d01
75
76static linux_ioctl_function_t mrsas_linux_ioctl;
77static struct linux_ioctl_handler mrsas_linux_handler = {mrsas_linux_ioctl,
78	MRSAS_LINUX_IOCTL_MIN,
79MRSAS_LINUX_IOCTL_MAX};
80
81SYSINIT(mrsas_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
82    linux_ioctl_register_handler, &mrsas_linux_handler);
83SYSUNINIT(mrsas_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
84    linux_ioctl_unregister_handler, &mrsas_linux_handler);
85
86static struct linux_device_handler mrsas_device_handler =
87{"mrsas", "megaraid_sas", "mrsas0", "megaraid_sas_ioctl_node", -1, 0, 1};
88
89SYSINIT(mrsas_register2, SI_SUB_KLD, SI_ORDER_MIDDLE,
90    linux_device_register_handler, &mrsas_device_handler);
91SYSUNINIT(mrsas_unregister2, SI_SUB_KLD, SI_ORDER_MIDDLE,
92    linux_device_unregister_handler, &mrsas_device_handler);
93
94static int
95mrsas_linux_modevent(module_t mod __unused, int cmd __unused, void *data __unused)
96{
97	return (0);
98}
99
100/*
101 * mrsas_linux_ioctl:	linux emulator IOCtl commands entry point.
102 *
103 * This function is the entry point for IOCtls from linux binaries.
104 * It calls the mrsas_ioctl function for processing
105 * depending on the IOCTL command received.
106 */
107static int
108mrsas_linux_ioctl(struct thread *p, struct linux_ioctl_args *args)
109{
110#if (__FreeBSD_version >= 1000000)
111	cap_rights_t rights;
112
113#endif
114	struct file *fp;
115	int error;
116	u_long cmd = args->cmd;
117
118	if (cmd != MRSAS_LINUX_CMD32) {
119		error = ENOTSUP;
120		goto END;
121	}
122#if (__FreeBSD_version >= 1000000)
123	error = fget(p, args->fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
124#elif (__FreeBSD_version <= 900000)
125	error = fget(p, args->fd, &fp);
126#else					/* For FreeBSD version greater than
127					 * 9.0.0 but less than 10.0.0 */
128	error = fget(p, args->fd, CAP_IOCTL, &fp);
129#endif
130	if (error != 0)
131		goto END;
132
133	error = fo_ioctl(fp, cmd, (caddr_t)args->arg, p->td_ucred, p);
134	fdrop(fp, p);
135END:
136	return (error);
137}
138
139DEV_MODULE(mrsas_linux, mrsas_linux_modevent, NULL);
140MODULE_DEPEND(mrsas, linux, 1, 1, 1);
141