xref: /linux/include/linux/mdev.h (revision 89345d5177aa0f6d678251e1e0870b0eeb1ab510)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
27b96953bSKirti Wankhede /*
37b96953bSKirti Wankhede  * Mediated device definition
47b96953bSKirti Wankhede  *
57b96953bSKirti Wankhede  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
67b96953bSKirti Wankhede  *     Author: Neo Jia <cjia@nvidia.com>
77b96953bSKirti Wankhede  *             Kirti Wankhede <kwankhede@nvidia.com>
87b96953bSKirti Wankhede  */
97b96953bSKirti Wankhede 
107b96953bSKirti Wankhede #ifndef MDEV_H
117b96953bSKirti Wankhede #define MDEV_H
127b96953bSKirti Wankhede 
13bdef2b78SChristoph Hellwig #include <linux/device.h>
14bdef2b78SChristoph Hellwig #include <linux/uuid.h>
15bdef2b78SChristoph Hellwig 
16417fd5bfSJason Gunthorpe struct mdev_type;
17417fd5bfSJason Gunthorpe 
182a3d15f2SJason Gunthorpe struct mdev_device {
192a3d15f2SJason Gunthorpe 	struct device dev;
202a3d15f2SJason Gunthorpe 	guid_t uuid;
212a3d15f2SJason Gunthorpe 	struct list_head next;
22417fd5bfSJason Gunthorpe 	struct mdev_type *type;
232a3d15f2SJason Gunthorpe 	bool active;
242a3d15f2SJason Gunthorpe };
252a3d15f2SJason Gunthorpe 
26*89345d51SChristoph Hellwig /* embedded into the struct device that the mdev devices hang off */
27*89345d51SChristoph Hellwig struct mdev_parent {
28*89345d51SChristoph Hellwig 	struct device *dev;
29*89345d51SChristoph Hellwig 	struct mdev_driver *mdev_driver;
30*89345d51SChristoph Hellwig 	struct kset *mdev_types_kset;
31*89345d51SChristoph Hellwig 	struct list_head type_list;
32*89345d51SChristoph Hellwig 	/* Synchronize device creation/removal with parent unregistration */
33*89345d51SChristoph Hellwig 	struct rw_semaphore unreg_sem;
34*89345d51SChristoph Hellwig };
35*89345d51SChristoph Hellwig 
362a3d15f2SJason Gunthorpe static inline struct mdev_device *to_mdev_device(struct device *dev)
372a3d15f2SJason Gunthorpe {
382a3d15f2SJason Gunthorpe 	return container_of(dev, struct mdev_device, dev);
392a3d15f2SJason Gunthorpe }
407b96953bSKirti Wankhede 
4115fcc44bSJason Gunthorpe unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
429169cff1SJason Gunthorpe unsigned int mtype_get_type_group_id(struct mdev_type *mtype);
439169cff1SJason Gunthorpe struct device *mtype_get_parent_dev(struct mdev_type *mtype);
4415fcc44bSJason Gunthorpe 
457b96953bSKirti Wankhede /* interface for exporting mdev supported type attributes */
467b96953bSKirti Wankhede struct mdev_type_attribute {
477b96953bSKirti Wankhede 	struct attribute attr;
489169cff1SJason Gunthorpe 	ssize_t (*show)(struct mdev_type *mtype,
499169cff1SJason Gunthorpe 			struct mdev_type_attribute *attr, char *buf);
509169cff1SJason Gunthorpe 	ssize_t (*store)(struct mdev_type *mtype,
519169cff1SJason Gunthorpe 			 struct mdev_type_attribute *attr, const char *buf,
529169cff1SJason Gunthorpe 			 size_t count);
537b96953bSKirti Wankhede };
547b96953bSKirti Wankhede 
557b96953bSKirti Wankhede #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
567b96953bSKirti Wankhede struct mdev_type_attribute mdev_type_attr_##_name =		\
577b96953bSKirti Wankhede 	__ATTR(_name, _mode, _show, _store)
587b96953bSKirti Wankhede #define MDEV_TYPE_ATTR_RW(_name) \
597b96953bSKirti Wankhede 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
607b96953bSKirti Wankhede #define MDEV_TYPE_ATTR_RO(_name) \
617b96953bSKirti Wankhede 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
627b96953bSKirti Wankhede #define MDEV_TYPE_ATTR_WO(_name) \
637b96953bSKirti Wankhede 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
647b96953bSKirti Wankhede 
657b96953bSKirti Wankhede /**
667b96953bSKirti Wankhede  * struct mdev_driver - Mediated device driver
677b96953bSKirti Wankhede  * @probe: called when new device created
687b96953bSKirti Wankhede  * @remove: called when device removed
696b42f491SJason Gunthorpe  * @supported_type_groups: Attributes to define supported types. It is mandatory
706b42f491SJason Gunthorpe  *			to provide supported types.
717b96953bSKirti Wankhede  * @driver: device driver structure
727b96953bSKirti Wankhede  *
737b96953bSKirti Wankhede  **/
747b96953bSKirti Wankhede struct mdev_driver {
752a3d15f2SJason Gunthorpe 	int (*probe)(struct mdev_device *dev);
762a3d15f2SJason Gunthorpe 	void (*remove)(struct mdev_device *dev);
776b42f491SJason Gunthorpe 	struct attribute_group **supported_type_groups;
787b96953bSKirti Wankhede 	struct device_driver driver;
797b96953bSKirti Wankhede };
807b96953bSKirti Wankhede 
817b96953bSKirti Wankhede extern struct bus_type mdev_bus_type;
827b96953bSKirti Wankhede 
83*89345d51SChristoph Hellwig int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
84*89345d51SChristoph Hellwig 		struct mdev_driver *mdev_driver);
85*89345d51SChristoph Hellwig void mdev_unregister_parent(struct mdev_parent *parent);
867b96953bSKirti Wankhede 
8791b9969dSJason Gunthorpe int mdev_register_driver(struct mdev_driver *drv);
8850732af3SParav Pandit void mdev_unregister_driver(struct mdev_driver *drv);
897b96953bSKirti Wankhede 
9050732af3SParav Pandit struct device *mdev_parent_dev(struct mdev_device *mdev);
912a3d15f2SJason Gunthorpe static inline struct device *mdev_dev(struct mdev_device *mdev)
922a3d15f2SJason Gunthorpe {
932a3d15f2SJason Gunthorpe 	return &mdev->dev;
942a3d15f2SJason Gunthorpe }
952a3d15f2SJason Gunthorpe static inline struct mdev_device *mdev_from_dev(struct device *dev)
962a3d15f2SJason Gunthorpe {
972a3d15f2SJason Gunthorpe 	return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
982a3d15f2SJason Gunthorpe }
999372e6feSAlex Williamson 
1007b96953bSKirti Wankhede #endif /* MDEV_H */
101