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