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-2016 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  * $FreeBSD$
30  */
31 #ifndef	_LINUXKPI_LINUX_DEVICE_H_
32 #define	_LINUXKPI_LINUX_DEVICE_H_
33 
34 #include <linux/err.h>
35 #include <linux/types.h>
36 #include <linux/kobject.h>
37 #include <linux/sysfs.h>
38 #include <linux/list.h>
39 #include <linux/compiler.h>
40 #include <linux/types.h>
41 #include <linux/module.h>
42 #include <linux/workqueue.h>
43 #include <linux/kdev_t.h>
44 #include <linux/backlight.h>
45 #include <linux/pm.h>
46 #include <linux/idr.h>
47 #include <linux/ratelimit.h>	/* via linux/dev_printk.h */
48 #include <asm/atomic.h>
49 
50 #include <sys/bus.h>
51 #include <sys/backlight.h>
52 
53 struct device;
54 struct fwnode_handle;
55 
56 struct class {
57 	const char	*name;
58 	struct module	*owner;
59 	struct kobject	kobj;
60 	devclass_t	bsdclass;
61 	const struct dev_pm_ops *pm;
62 	const struct attribute_group **dev_groups;
63 	void		(*class_release)(struct class *class);
64 	void		(*dev_release)(struct device *dev);
65 	char *		(*devnode)(struct device *dev, umode_t *mode);
66 };
67 
68 struct dev_pm_ops {
69 	int (*prepare)(struct device *dev);
70 	int (*suspend)(struct device *dev);
71 	int (*suspend_late)(struct device *dev);
72 	int (*resume)(struct device *dev);
73 	int (*resume_early)(struct device *dev);
74 	int (*freeze)(struct device *dev);
75 	int (*freeze_late)(struct device *dev);
76 	int (*thaw)(struct device *dev);
77 	int (*thaw_early)(struct device *dev);
78 	int (*poweroff)(struct device *dev);
79 	int (*poweroff_late)(struct device *dev);
80 	int (*restore)(struct device *dev);
81 	int (*restore_early)(struct device *dev);
82 	int (*runtime_suspend)(struct device *dev);
83 	int (*runtime_resume)(struct device *dev);
84 	int (*runtime_idle)(struct device *dev);
85 };
86 
87 struct device_driver {
88 	const char	*name;
89 	const struct dev_pm_ops *pm;
90 };
91 
92 struct device_type {
93 	const char	*name;
94 };
95 
96 struct device {
97 	struct device	*parent;
98 	struct list_head irqents;
99 	device_t	bsddev;
100 	/*
101 	 * The following flag is used to determine if the LinuxKPI is
102 	 * responsible for detaching the BSD device or not. If the
103 	 * LinuxKPI got the BSD device using devclass_get_device(), it
104 	 * must not try to detach or delete it, because it's already
105 	 * done somewhere else.
106 	 */
107 	bool		bsddev_attached_here;
108 	struct device_driver *driver;
109 	struct device_type *type;
110 	dev_t		devt;
111 	struct class	*class;
112 	void		(*release)(struct device *dev);
113 	struct kobject	kobj;
114 	void		*dma_priv;
115 	void		*driver_data;
116 	unsigned int	irq;
117 #define	LINUX_IRQ_INVALID	65535
118 	unsigned int	irq_start;
119 	unsigned int	irq_end;
120 	const struct attribute_group **groups;
121 	struct fwnode_handle *fwnode;
122 	struct cdev	*backlight_dev;
123 	struct backlight_device	*bd;
124 
125 	spinlock_t	devres_lock;
126 	struct list_head devres_head;
127 };
128 
129 extern struct device linux_root_device;
130 extern struct kobject linux_class_root;
131 extern const struct kobj_type linux_dev_ktype;
132 extern const struct kobj_type linux_class_ktype;
133 
134 struct class_attribute {
135 	struct attribute attr;
136 	ssize_t (*show)(struct class *, struct class_attribute *, char *);
137 	ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
138 	const void *(*namespace)(struct class *, const struct class_attribute *);
139 };
140 
141 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
142 	struct class_attribute class_attr_##_name =			\
143 	    { { #_name, NULL, _mode }, _show, _store }
144 
145 struct device_attribute {
146 	struct attribute	attr;
147 	ssize_t			(*show)(struct device *,
148 					struct device_attribute *, char *);
149 	ssize_t			(*store)(struct device *,
150 					struct device_attribute *, const char *,
151 					size_t);
152 };
153 
154 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
155 	struct device_attribute dev_attr_##_name =			\
156 	    __ATTR(_name, _mode, _show, _store)
157 #define	DEVICE_ATTR_RO(_name)						\
158 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
159 #define	DEVICE_ATTR_WO(_name)						\
160 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
161 #define	DEVICE_ATTR_RW(_name)						\
162 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
163 
164 /* Simple class attribute that is just a static string */
165 struct class_attribute_string {
166 	struct class_attribute attr;
167 	char *str;
168 };
169 
170 static inline ssize_t
171 show_class_attr_string(struct class *class,
172 				struct class_attribute *attr, char *buf)
173 {
174 	struct class_attribute_string *cs;
175 	cs = container_of(attr, struct class_attribute_string, attr);
176 	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
177 }
178 
179 /* Currently read-only only */
180 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
181 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
182 #define CLASS_ATTR_STRING(_name, _mode, _str) \
183 	struct class_attribute_string class_attr_##_name = \
184 		_CLASS_ATTR_STRING(_name, _mode, _str)
185 
186 #define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
187 #define	dev_crit(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
188 #define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
189 #define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
190 #define	dev_notice(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
191 #define	dev_emerg(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
192 #define	dev_dbg(dev, fmt, ...)	do { } while (0)
193 #define	dev_printk(lvl, dev, fmt, ...)					\
194 	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
195 
196 #define dev_info_once(dev, ...) do {		\
197 	static bool __dev_info_once;		\
198 	if (!__dev_info_once) {			\
199 	__dev_info_once = true;			\
200 	dev_info(dev, __VA_ARGS__);		\
201 	}					\
202 } while (0)
203 
204 #define	dev_err_once(dev, ...) do {		\
205 	static bool __dev_err_once;		\
206 	if (!__dev_err_once) {			\
207 		__dev_err_once = 1;		\
208 		dev_err(dev, __VA_ARGS__);	\
209 	}					\
210 } while (0)
211 
212 #define	dev_err_ratelimited(dev, ...) do {	\
213 	static linux_ratelimit_t __ratelimited;	\
214 	if (linux_ratelimited(&__ratelimited))	\
215 		dev_err(dev, __VA_ARGS__);	\
216 } while (0)
217 
218 #define	dev_warn_ratelimited(dev, ...) do {	\
219 	static linux_ratelimit_t __ratelimited;	\
220 	if (linux_ratelimited(&__ratelimited))	\
221 		dev_warn(dev, __VA_ARGS__);	\
222 } while (0)
223 
224 /* Public and LinuxKPI internal devres functions. */
225 void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t);
226 void lkpi_devres_add(struct device *, void *);
227 void lkpi_devres_free(void *);
228 void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *),
229     int (*match)(struct device *, void *, void *), void *);
230 int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *),
231     int (*match)(struct device *, void *, void *), void *);
232 #define	devres_alloc(_r, _s, _g)	lkpi_devres_alloc(_r, _s, _g)
233 #define	devres_add(_d, _p)		lkpi_devres_add(_d, _p)
234 #define	devres_free(_p)			lkpi_devres_free(_p)
235 #define	devres_find(_d, _rfn, _mfn, _mp) \
236 					lkpi_devres_find(_d, _rfn, _mfn, _mp)
237 #define	devres_destroy(_d, _rfn, _mfn, _mp) \
238 					lkpi_devres_destroy(_d, _rfn, _mfn, _mp)
239 void lkpi_devres_release_free_list(struct device *);
240 void lkpi_devres_unlink(struct device *, void *);
241 void lkpi_devm_kmalloc_release(struct device *, void *);
242 
243 static inline const char *
244 dev_driver_string(const struct device *dev)
245 {
246 	driver_t *drv;
247 	const char *str = "";
248 
249 	if (dev->bsddev != NULL) {
250 		drv = device_get_driver(dev->bsddev);
251 		if (drv != NULL)
252 			str = drv->name;
253 	}
254 
255 	return (str);
256 }
257 
258 static inline void *
259 dev_get_drvdata(const struct device *dev)
260 {
261 
262 	return dev->driver_data;
263 }
264 
265 static inline void
266 dev_set_drvdata(struct device *dev, void *data)
267 {
268 
269 	dev->driver_data = data;
270 }
271 
272 static inline struct device *
273 get_device(struct device *dev)
274 {
275 
276 	if (dev)
277 		kobject_get(&dev->kobj);
278 
279 	return (dev);
280 }
281 
282 static inline char *
283 dev_name(const struct device *dev)
284 {
285 
286 	return kobject_name(&dev->kobj);
287 }
288 
289 #define	dev_set_name(_dev, _fmt, ...)					\
290 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
291 
292 static inline void
293 put_device(struct device *dev)
294 {
295 
296 	if (dev)
297 		kobject_put(&dev->kobj);
298 }
299 
300 struct class *class_create(struct module *owner, const char *name);
301 
302 static inline int
303 class_register(struct class *class)
304 {
305 
306 	class->bsdclass = devclass_create(class->name);
307 	kobject_init(&class->kobj, &linux_class_ktype);
308 	kobject_set_name(&class->kobj, class->name);
309 	kobject_add(&class->kobj, &linux_class_root, class->name);
310 
311 	return (0);
312 }
313 
314 static inline void
315 class_unregister(struct class *class)
316 {
317 
318 	kobject_put(&class->kobj);
319 }
320 
321 static inline struct device *kobj_to_dev(struct kobject *kobj)
322 {
323 	return container_of(kobj, struct device, kobj);
324 }
325 
326 struct device *device_create(struct class *class, struct device *parent,
327 	    dev_t devt, void *drvdata, const char *fmt, ...);
328 struct device *device_create_groups_vargs(struct class *class, struct device *parent,
329     dev_t devt, void *drvdata, const struct attribute_group **groups,
330     const char *fmt, va_list args);
331 
332 /*
333  * Devices are registered and created for exporting to sysfs. Create
334  * implies register and register assumes the device fields have been
335  * setup appropriately before being called.
336  */
337 static inline void
338 device_initialize(struct device *dev)
339 {
340 	device_t bsddev = NULL;
341 	int unit = -1;
342 
343 	if (dev->devt) {
344 		unit = MINOR(dev->devt);
345 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
346 		dev->bsddev_attached_here = false;
347 	} else if (dev->parent == NULL) {
348 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
349 		dev->bsddev_attached_here = false;
350 	} else {
351 		dev->bsddev_attached_here = true;
352 	}
353 
354 	if (bsddev == NULL && dev->parent != NULL) {
355 		bsddev = device_add_child(dev->parent->bsddev,
356 		    dev->class->kobj.name, unit);
357 	}
358 
359 	if (bsddev != NULL)
360 		device_set_softc(bsddev, dev);
361 
362 	dev->bsddev = bsddev;
363 	MPASS(dev->bsddev != NULL);
364 	kobject_init(&dev->kobj, &linux_dev_ktype);
365 
366 	spin_lock_init(&dev->devres_lock);
367 	INIT_LIST_HEAD(&dev->devres_head);
368 }
369 
370 static inline int
371 device_add(struct device *dev)
372 {
373 	if (dev->bsddev != NULL) {
374 		if (dev->devt == 0)
375 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
376 	}
377 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
378 
379 	if (dev->groups)
380 		return (sysfs_create_groups(&dev->kobj, dev->groups));
381 
382 	return (0);
383 }
384 
385 static inline void
386 device_create_release(struct device *dev)
387 {
388 	kfree(dev);
389 }
390 
391 static inline struct device *
392 device_create_with_groups(struct class *class,
393     struct device *parent, dev_t devt, void *drvdata,
394     const struct attribute_group **groups, const char *fmt, ...)
395 {
396 	va_list vargs;
397 	struct device *dev;
398 
399 	va_start(vargs, fmt);
400 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
401 	    groups, fmt, vargs);
402 	va_end(vargs);
403 	return dev;
404 }
405 
406 static inline bool
407 device_is_registered(struct device *dev)
408 {
409 
410 	return (dev->bsddev != NULL);
411 }
412 
413 static inline int
414 device_register(struct device *dev)
415 {
416 	device_t bsddev = NULL;
417 	int unit = -1;
418 
419 	if (device_is_registered(dev))
420 		goto done;
421 
422 	if (dev->devt) {
423 		unit = MINOR(dev->devt);
424 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
425 		dev->bsddev_attached_here = false;
426 	} else if (dev->parent == NULL) {
427 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
428 		dev->bsddev_attached_here = false;
429 	} else {
430 		dev->bsddev_attached_here = true;
431 	}
432 	if (bsddev == NULL && dev->parent != NULL) {
433 		bsddev = device_add_child(dev->parent->bsddev,
434 		    dev->class->kobj.name, unit);
435 	}
436 	if (bsddev != NULL) {
437 		if (dev->devt == 0)
438 			dev->devt = makedev(0, device_get_unit(bsddev));
439 		device_set_softc(bsddev, dev);
440 	}
441 	dev->bsddev = bsddev;
442 done:
443 	kobject_init(&dev->kobj, &linux_dev_ktype);
444 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
445 
446 	sysfs_create_groups(&dev->kobj, dev->class->dev_groups);
447 
448 	return (0);
449 }
450 
451 static inline void
452 device_unregister(struct device *dev)
453 {
454 	device_t bsddev;
455 
456 	sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);
457 
458 	bsddev = dev->bsddev;
459 	dev->bsddev = NULL;
460 
461 	if (bsddev != NULL && dev->bsddev_attached_here) {
462 		bus_topo_lock();
463 		device_delete_child(device_get_parent(bsddev), bsddev);
464 		bus_topo_unlock();
465 	}
466 	put_device(dev);
467 }
468 
469 static inline void
470 device_del(struct device *dev)
471 {
472 	device_t bsddev;
473 
474 	bsddev = dev->bsddev;
475 	dev->bsddev = NULL;
476 
477 	if (bsddev != NULL && dev->bsddev_attached_here) {
478 		bus_topo_lock();
479 		device_delete_child(device_get_parent(bsddev), bsddev);
480 		bus_topo_unlock();
481 	}
482 }
483 
484 static inline void
485 device_destroy(struct class *class, dev_t devt)
486 {
487 	device_t bsddev;
488 	int unit;
489 
490 	unit = MINOR(devt);
491 	bsddev = devclass_get_device(class->bsdclass, unit);
492 	if (bsddev != NULL)
493 		device_unregister(device_get_softc(bsddev));
494 }
495 
496 static inline void
497 device_release_driver(struct device *dev)
498 {
499 
500 #if 0
501 	/* This leads to panics. Disable temporarily. Keep to rework. */
502 
503 	/* We also need to cleanup LinuxKPI bits. What else? */
504 	lkpi_devres_release_free_list(dev);
505 	dev_set_drvdata(dev, NULL);
506 	/* Do not call dev->release! */
507 
508 	bus_topo_lock();
509 	if (device_is_attached(dev->bsddev))
510 		device_detach(dev->bsddev);
511 	bus_topo_unlock();
512 #endif
513 }
514 
515 static inline int
516 device_reprobe(struct device *dev)
517 {
518 	int error;
519 
520 	device_release_driver(dev);
521 	bus_topo_lock();
522 	error = device_probe_and_attach(dev->bsddev);
523 	bus_topo_unlock();
524 
525 	return (-error);
526 }
527 
528 #define	dev_pm_set_driver_flags(dev, flags) do { \
529 } while (0)
530 
531 static inline void
532 linux_class_kfree(struct class *class)
533 {
534 
535 	kfree(class);
536 }
537 
538 static inline void
539 class_destroy(struct class *class)
540 {
541 
542 	if (class == NULL)
543 		return;
544 	class_unregister(class);
545 }
546 
547 static inline int
548 device_create_file(struct device *dev, const struct device_attribute *attr)
549 {
550 
551 	if (dev)
552 		return sysfs_create_file(&dev->kobj, &attr->attr);
553 	return -EINVAL;
554 }
555 
556 static inline void
557 device_remove_file(struct device *dev, const struct device_attribute *attr)
558 {
559 
560 	if (dev)
561 		sysfs_remove_file(&dev->kobj, &attr->attr);
562 }
563 
564 static inline int
565 class_create_file(struct class *class, const struct class_attribute *attr)
566 {
567 
568 	if (class)
569 		return sysfs_create_file(&class->kobj, &attr->attr);
570 	return -EINVAL;
571 }
572 
573 static inline void
574 class_remove_file(struct class *class, const struct class_attribute *attr)
575 {
576 
577 	if (class)
578 		sysfs_remove_file(&class->kobj, &attr->attr);
579 }
580 
581 #define	dev_to_node(dev) linux_dev_to_node(dev)
582 #define	of_node_to_nid(node) -1
583 int linux_dev_to_node(struct device *);
584 
585 char *kvasprintf(gfp_t, const char *, va_list);
586 char *kasprintf(gfp_t, const char *, ...);
587 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...);
588 
589 #define	devm_kasprintf(_dev, _gfp, _fmt, ...)			\
590     lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__)
591 
592 static __inline void *
593 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
594 {
595 	void *p;
596 
597 	p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp);
598 	if (p != NULL)
599 		lkpi_devres_add(dev, p);
600 
601 	return (p);
602 }
603 
604 #define	devm_kzalloc(_dev, _size, _gfp)				\
605     devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO)
606 
607 #define	devm_kcalloc(_dev, _sizen, _size, _gfp)			\
608     devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO)
609 
610 #endif	/* _LINUXKPI_LINUX_DEVICE_H_ */
611