1/*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice unmodified, this list of conditions, and the following
13 *    disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29#ifndef	_LINUXKPI_LINUX_KOBJECT_H_
30#define	_LINUXKPI_LINUX_KOBJECT_H_
31
32#include <machine/stdarg.h>
33
34#include <linux/kernel.h>
35#include <linux/kref.h>
36#include <linux/list.h>
37#include <linux/slab.h>
38#include <linux/spinlock.h>
39#include <linux/wait.h>
40#include <linux/workqueue.h>
41
42struct kobject;
43struct kset;
44struct sysctl_oid;
45
46#define	KOBJ_CHANGE		0x01
47
48struct kobj_type {
49	void (*release)(struct kobject *kobj);
50	const struct sysfs_ops *sysfs_ops;
51	struct attribute **default_attrs;
52	const struct attribute_group **default_groups;
53};
54
55extern const struct kobj_type linux_kfree_type;
56
57struct kobject {
58	struct kobject		*parent;
59	char			*name;
60	struct kref		kref;
61	const struct kobj_type	*ktype;
62	struct list_head	entry;
63	struct sysctl_oid	*oidp;
64	struct kset		*kset;
65};
66
67extern struct kobject *mm_kobj;
68
69struct attribute {
70	const char	*name;
71	struct module	*owner;
72	mode_t		mode;
73};
74
75extern const struct sysfs_ops kobj_sysfs_ops;
76
77struct kobj_attribute {
78	struct attribute attr;
79	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
80	    char *buf);
81	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
82	    const char *buf, size_t count);
83};
84
85struct kset_uevent_ops {
86	/* TODO */
87};
88
89struct kset {
90	struct list_head	list;
91	spinlock_t		list_lock;
92	struct kobject		kobj;
93	const struct kset_uevent_ops *uevent_ops;
94};
95
96static inline void
97kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
98{
99
100	kref_init(&kobj->kref);
101	INIT_LIST_HEAD(&kobj->entry);
102	kobj->ktype = ktype;
103	kobj->oidp = NULL;
104}
105
106void linux_kobject_release(struct kref *kref);
107
108static inline void
109kobject_put(struct kobject *kobj)
110{
111
112	if (kobj)
113		kref_put(&kobj->kref, linux_kobject_release);
114}
115
116static inline struct kobject *
117kobject_get(struct kobject *kobj)
118{
119
120	if (kobj)
121		kref_get(&kobj->kref);
122	return kobj;
123}
124
125struct kobject *kobject_create(void);
126int	kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list);
127int	kobject_add(struct kobject *kobj, struct kobject *parent,
128	    const char *fmt, ...);
129
130static inline struct kobject *
131kobject_create_and_add(const char *name, struct kobject *parent)
132{
133	struct kobject *kobj;
134
135	kobj = kobject_create();
136	if (kobj == NULL)
137		return (NULL);
138	if (kobject_add(kobj, parent, "%s", name) == 0)
139		return (kobj);
140	kobject_put(kobj);
141
142	return (NULL);
143}
144
145static inline void
146kobject_del(struct kobject *kobj __unused)
147{
148}
149
150static inline char *
151kobject_name(const struct kobject *kobj)
152{
153
154	return kobj->name;
155}
156
157int	kobject_set_name(struct kobject *kobj, const char *fmt, ...);
158int	kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
159	    struct kobject *parent, const char *fmt, ...);
160
161static __inline void
162kobject_uevent_env(struct kobject *kobj, int action, char *envp[])
163{
164
165	/*
166	 * iwlwifi(4) sends an INACCESSIBLE event when it detects that the card
167	 * (pice endpoint) is gone and it attempts a removal cleanup.
168	 * Not sure if we do anything related to udev/sysfs at the moment or
169	 * need a shortcut or simply ignore it (for now).
170	 */
171}
172
173void	kset_init(struct kset *kset);
174int	kset_register(struct kset *kset);
175void	kset_unregister(struct kset *kset);
176struct kset * kset_create_and_add(const char *name,
177    const struct kset_uevent_ops *u, struct kobject *parent_kobj);
178
179static inline struct kset *
180to_kset(struct kobject *kobj)
181{
182	if (kobj != NULL)
183		return container_of(kobj, struct kset, kobj);
184	else
185		return NULL;
186}
187
188static inline struct kset *
189kset_get(struct kset *kset)
190{
191	if (kset != NULL) {
192		struct kobject *kobj;
193
194		kobj = kobject_get(&kset->kobj);
195		return to_kset(kobj);
196	} else {
197		return NULL;
198	}
199}
200
201static inline void
202kset_put(struct kset *kset)
203{
204	if (kset != NULL)
205		kobject_put(&kset->kobj);
206}
207
208void linux_kobject_kfree_name(struct kobject *kobj);
209
210#endif /* _LINUXKPI_LINUX_KOBJECT_H_ */
211