xref: /src/sys/compat/linuxkpi/common/include/linux/module.h (revision 1f4fbcf342f41fecc8dff4e0d9a1f9bdb1cdf784)
1aa0a1e58SJeff Roberson /*-
2aa0a1e58SJeff Roberson  * Copyright (c) 2010 Isilon Systems, Inc.
3aa0a1e58SJeff Roberson  * Copyright (c) 2010 iX Systems, Inc.
4aa0a1e58SJeff Roberson  * Copyright (c) 2010 Panasas, Inc.
5c7818b48SHans Petter Selasky  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6aa0a1e58SJeff Roberson  * All rights reserved.
7aa0a1e58SJeff Roberson  *
8aa0a1e58SJeff Roberson  * Redistribution and use in source and binary forms, with or without
9aa0a1e58SJeff Roberson  * modification, are permitted provided that the following conditions
10aa0a1e58SJeff Roberson  * are met:
11aa0a1e58SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
12aa0a1e58SJeff Roberson  *    notice unmodified, this list of conditions, and the following
13aa0a1e58SJeff Roberson  *    disclaimer.
14aa0a1e58SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
15aa0a1e58SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
16aa0a1e58SJeff Roberson  *    documentation and/or other materials provided with the distribution.
17aa0a1e58SJeff Roberson  *
18aa0a1e58SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19aa0a1e58SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20aa0a1e58SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21aa0a1e58SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22aa0a1e58SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23aa0a1e58SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24aa0a1e58SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25aa0a1e58SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26aa0a1e58SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27aa0a1e58SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28aa0a1e58SJeff Roberson  */
29307f78f3SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_MODULE_H_
30307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_MODULE_H_
31aa0a1e58SJeff Roberson 
32d39d7c86SHans Petter Selasky #include <sys/types.h>
33df38ada2SBjoern A. Zeeb #include <sys/param.h>
34d39d7c86SHans Petter Selasky #include <sys/module.h>
3528a59100SAustin Shafer #include <sys/queue.h>
3628a59100SAustin Shafer #include <sys/linker.h>
37d39d7c86SHans Petter Selasky 
38aa0a1e58SJeff Roberson #include <linux/list.h>
39aa0a1e58SJeff Roberson #include <linux/compiler.h>
4043b4c006SBjoern A. Zeeb #include <linux/stringify.h>
419b6197dfSMark Johnston #include <linux/kmod.h>
42aa0a1e58SJeff Roberson #include <linux/kobject.h>
438e7baabcSHans Petter Selasky #include <linux/sysfs.h>
44aa0a1e58SJeff Roberson #include <linux/moduleparam.h>
45aa0a1e58SJeff Roberson #include <linux/slab.h>
46a041e75aSHans Petter Selasky #include <linux/export.h>
47aa0a1e58SJeff Roberson 
48aa0a1e58SJeff Roberson #define MODULE_AUTHOR(name)
49aa0a1e58SJeff Roberson #define MODULE_DESCRIPTION(name)
50aa0a1e58SJeff Roberson #define MODULE_LICENSE(name)
51cb564d24SMark Johnston #define	MODULE_INFO(tag, info)
52cb564d24SMark Johnston #define	MODULE_FIRMWARE(firmware)
53c5eec7b5SBjoern A. Zeeb #define	MODULE_SUPPORTED_DEVICE(name)
54f6fb9b52SBjoern A. Zeeb #define	MODULE_IMPORT_NS(_name)
55c7818b48SHans Petter Selasky 
56*1f4fbcf3SBjoern A. Zeeb /* Linux has an empty element at the end of the ID table -> nitems() - 1. */
57*1f4fbcf3SBjoern A. Zeeb #define	MODULE_DEVICE_TABLE(_bus, _table)				\
58*1f4fbcf3SBjoern A. Zeeb 									\
59*1f4fbcf3SBjoern A. Zeeb static device_method_t _ ## _bus ## _ ## _table ## _methods[] = {	\
60*1f4fbcf3SBjoern A. Zeeb 	DEVMETHOD_END							\
61*1f4fbcf3SBjoern A. Zeeb };									\
62*1f4fbcf3SBjoern A. Zeeb 									\
63*1f4fbcf3SBjoern A. Zeeb static driver_t _ ## _bus ## _ ## _table ## _driver = {			\
64*1f4fbcf3SBjoern A. Zeeb 	"lkpi_" #_bus #_table,						\
65*1f4fbcf3SBjoern A. Zeeb 	_ ## _bus ## _ ## _table ## _methods,				\
66*1f4fbcf3SBjoern A. Zeeb 	0								\
67*1f4fbcf3SBjoern A. Zeeb };									\
68*1f4fbcf3SBjoern A. Zeeb 									\
69*1f4fbcf3SBjoern A. Zeeb DRIVER_MODULE(lkpi_ ## _table, _bus, _ ## _bus ## _ ## _table ## _driver,\
70*1f4fbcf3SBjoern A. Zeeb 	0, 0);								\
71*1f4fbcf3SBjoern A. Zeeb 									\
72*1f4fbcf3SBjoern A. Zeeb MODULE_DEVICE_TABLE_BUS_ ## _bus(_bus, _table)
73*1f4fbcf3SBjoern A. Zeeb 
7428a59100SAustin Shafer /*
7528a59100SAustin Shafer  * THIS_MODULE is used to differentiate modules on Linux. We currently
7628a59100SAustin Shafer  * completely stub out any Linux struct module usage, but THIS_MODULE is still
7728a59100SAustin Shafer  * used to populate the "owner" fields of various drivers.  Even though we
7828a59100SAustin Shafer  * don't actually dereference these "owner" fields they are still used by
7928a59100SAustin Shafer  * drivers to check if devices/dmabufs/etc come from different modules. For
8028a59100SAustin Shafer  * example, during DRM GEM import some drivers check if the dmabuf's owner
8128a59100SAustin Shafer  * matches the dev's owner. If they match because they are both NULL drivers
8228a59100SAustin Shafer  * may incorrectly think two resources come from the same module.
8328a59100SAustin Shafer  *
8428a59100SAustin Shafer  * To handle this we specify an undefined symbol __this_linker_file, which
8528a59100SAustin Shafer  * will get special treatment from the linker when resolving. This will
8628a59100SAustin Shafer  * populate the usages of __this_linker_file with the linker_file_t of the
8728a59100SAustin Shafer  * module.
8828a59100SAustin Shafer  */
8928a59100SAustin Shafer #ifdef KLD_MODULE
9028a59100SAustin Shafer #define	THIS_MODULE	((struct module *)&__this_linker_file)
9128a59100SAustin Shafer #else
92aa0a1e58SJeff Roberson #define	THIS_MODULE	((struct module *)0)
9328a59100SAustin Shafer #endif
94aa0a1e58SJeff Roberson 
958961c483SHans Petter Selasky #define	__MODULE_STRING(x) __stringify(x)
96aa0a1e58SJeff Roberson 
975cb6b3afSHans Petter Selasky /* OFED pre-module initialization */
984184fb4dSHans Petter Selasky #define	SI_SUB_OFED_PREINIT	(SI_SUB_ROOT_CONF - 2)
995cb6b3afSHans Petter Selasky /* OFED default module initialization */
1004184fb4dSHans Petter Selasky #define	SI_SUB_OFED_MODINIT	(SI_SUB_ROOT_CONF - 1)
1015cb6b3afSHans Petter Selasky 
102aa0a1e58SJeff Roberson #include <sys/linker.h>
103aa0a1e58SJeff Roberson 
104aa0a1e58SJeff Roberson static inline void
_module_run(void * arg)105aa0a1e58SJeff Roberson _module_run(void *arg)
106aa0a1e58SJeff Roberson {
107aa0a1e58SJeff Roberson 	void (*fn)(void);
108aa0a1e58SJeff Roberson #ifdef OFED_DEBUG_INIT
109aa0a1e58SJeff Roberson 	char name[1024];
110aa0a1e58SJeff Roberson 	caddr_t pc;
111aa0a1e58SJeff Roberson 	long offset;
112aa0a1e58SJeff Roberson 
113aa0a1e58SJeff Roberson 	pc = (caddr_t)arg;
114aa0a1e58SJeff Roberson 	if (linker_search_symbol_name(pc, name, sizeof(name), &offset) != 0)
115aa0a1e58SJeff Roberson 		printf("Running ??? (%p)\n", pc);
116aa0a1e58SJeff Roberson 	else
117aa0a1e58SJeff Roberson 		printf("Running %s (%p)\n", name, pc);
118aa0a1e58SJeff Roberson #endif
119aa0a1e58SJeff Roberson 	fn = arg;
120aa0a1e58SJeff Roberson 	fn();
121aa0a1e58SJeff Roberson }
122aa0a1e58SJeff Roberson 
123aa0a1e58SJeff Roberson #define	module_init(fn)							\
1245cb6b3afSHans Petter Selasky 	SYSINIT(fn, SI_SUB_OFED_MODINIT, SI_ORDER_FIRST, _module_run, (fn))
125aa0a1e58SJeff Roberson 
126c7818b48SHans Petter Selasky #define	module_exit(fn)						\
127c7818b48SHans Petter Selasky 	SYSUNINIT(fn, SI_SUB_OFED_MODINIT, SI_ORDER_SECOND, _module_run, (fn))
128c7818b48SHans Petter Selasky 
129aa0a1e58SJeff Roberson /*
130c7818b48SHans Petter Selasky  * The following two macros are a workaround for not having a module
131c7818b48SHans Petter Selasky  * load and unload order resolver:
132aa0a1e58SJeff Roberson  */
133aa0a1e58SJeff Roberson #define	module_init_order(fn, order)					\
1345cb6b3afSHans Petter Selasky 	SYSINIT(fn, SI_SUB_OFED_MODINIT, (order), _module_run, (fn))
135aa0a1e58SJeff Roberson 
136c7818b48SHans Petter Selasky #define	module_exit_order(fn, order)				\
137c7818b48SHans Petter Selasky 	SYSUNINIT(fn, SI_SUB_OFED_MODINIT, (order), _module_run, (fn))
138aa0a1e58SJeff Roberson 
139aa0a1e58SJeff Roberson #define	module_get(module)
140aa0a1e58SJeff Roberson #define	module_put(module)
141aa0a1e58SJeff Roberson #define	try_module_get(module)	1
142aa0a1e58SJeff Roberson 
1437e1a02baSMark Johnston #define	postcore_initcall(fn)	module_init(fn)
1447e1a02baSMark Johnston 
145307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_MODULE_H_ */
146