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