3 * Copyright � 2010 - 2015 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
17 #include <linux/uuid.h>
20 #include "visorbus_private.h"
22 #include "periodic_work.h"
23 #include "vbuschannel.h"
24 #include "guestlinuxdebug.h"
25 #include "vmcallinterface.h"
27 #define MYDRVNAME "visorbus"
29 /* module parameters */
30 static int visorbus_debug;
31 static int visorbus_forcematch;
32 static int visorbus_forcenomatch;
33 static int visorbus_debugref;
34 #define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
36 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
37 #define POLLJIFFIES_TESTWORK 100
38 #define POLLJIFFIES_NORMALCHANNEL 10
40 static int busreg_rc = -ENODEV; /* stores the result from bus registration */
42 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
43 static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
44 static void fix_vbus_dev_info(struct visor_device *visordev);
46 /* BUS type attributes
48 * define & implement display of bus attributes under
53 static ssize_t version_show(struct bus_type *bus, char *buf)
55 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
58 static BUS_ATTR_RO(version);
60 static struct attribute *visorbus_bus_attrs[] = {
61 &bus_attr_version.attr,
65 static const struct attribute_group visorbus_bus_group = {
66 .attrs = visorbus_bus_attrs,
69 static const struct attribute_group *visorbus_bus_groups[] = {
75 * DEVICE type attributes
77 * The modalias file will contain the guid of the device.
79 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
82 struct visor_device *vdev;
85 vdev = to_visor_device(dev);
86 guid = visorchannel_get_uuid(vdev->visorchannel);
87 return snprintf(buf, PAGE_SIZE, "visorbus:%pUl\n", &guid);
89 static DEVICE_ATTR_RO(modalias);
91 static struct attribute *visorbus_dev_attrs[] = {
92 &dev_attr_modalias.attr,
96 /* sysfs example for bridge-only sysfs files using device_type's */
97 static const struct attribute_group visorbus_dev_group = {
98 .attrs = visorbus_dev_attrs,
101 static const struct attribute_group *visorbus_dev_groups[] = {
106 /** This describes the TYPE of bus.
107 * (Don't confuse this with an INSTANCE of the bus.)
109 struct bus_type visorbus_type = {
111 .match = visorbus_match,
112 .uevent = visorbus_uevent,
113 .dev_groups = visorbus_dev_groups,
114 .bus_groups = visorbus_bus_groups,
117 static struct delayed_work periodic_work;
119 /* YES, we need 2 workqueues.
120 * The reason is, workitems on the test queue may need to cancel
121 * workitems on the other queue. You will be in for trouble if you try to
122 * do this with workitems queued on the same workqueue.
124 static struct workqueue_struct *periodic_test_workqueue;
125 static struct workqueue_struct *periodic_dev_workqueue;
126 static long long bus_count; /** number of bus instances */
127 /** ever-increasing */
129 static void chipset_bus_create(struct visor_device *bus_info);
130 static void chipset_bus_destroy(struct visor_device *bus_info);
131 static void chipset_device_create(struct visor_device *dev_info);
132 static void chipset_device_destroy(struct visor_device *dev_info);
133 static void chipset_device_pause(struct visor_device *dev_info);
134 static void chipset_device_resume(struct visor_device *dev_info);
136 /** These functions are implemented herein, and are called by the chipset
137 * driver to notify us about specific events.
139 static struct visorchipset_busdev_notifiers chipset_notifiers = {
140 .bus_create = chipset_bus_create,
141 .bus_destroy = chipset_bus_destroy,
142 .device_create = chipset_device_create,
143 .device_destroy = chipset_device_destroy,
144 .device_pause = chipset_device_pause,
145 .device_resume = chipset_device_resume,
148 /** These functions are implemented in the chipset driver, and we call them
149 * herein when we want to acknowledge a specific event.
151 static struct visorchipset_busdev_responders chipset_responders;
153 /* filled in with info about parent chipset driver when we register with it */
154 static struct ultra_vbus_deviceinfo chipset_driverinfo;
155 /* filled in with info about this driver, wrt it servicing client busses */
156 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
158 /** list of visor_device structs, linked via .list_all */
159 static LIST_HEAD(list_all_bus_instances);
160 /** list of visor_device structs, linked via .list_all */
161 static LIST_HEAD(list_all_device_instances);
164 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
166 struct visor_device *dev;
169 dev = to_visor_device(xdev);
170 guid = visorchannel_get_uuid(dev->visorchannel);
172 if (add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid))
177 /* This is called automatically upon adding a visor_device (device_add), or
178 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
179 * provided driver can control the specified device.
182 visorbus_match(struct device *xdev, struct device_driver *xdrv)
184 uuid_le channel_type;
187 struct visor_device *dev;
188 struct visor_driver *drv;
190 dev = to_visor_device(xdev);
191 drv = to_visor_driver(xdrv);
192 channel_type = visorchannel_get_uuid(dev->visorchannel);
193 if (visorbus_forcematch) {
197 if (visorbus_forcenomatch)
200 if (!drv->channel_types)
203 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
204 (drv->channel_types[i].name);
206 if (uuid_le_cmp(drv->channel_types[i].guid,
207 channel_type) == 0) {
215 /** This is called when device_unregister() is called for the bus device
216 * instance, after all other tasks involved with destroying the device
220 visorbus_release_busdevice(struct device *xdev)
222 struct visor_device *dev = dev_get_drvdata(xdev);
224 dev_set_drvdata(xdev, NULL);
228 /** This is called when device_unregister() is called for each child
232 visorbus_release_device(struct device *xdev)
234 struct visor_device *dev = to_visor_device(xdev);
236 if (dev->periodic_work) {
237 visor_periodic_work_destroy(dev->periodic_work);
238 dev->periodic_work = NULL;
240 if (dev->visorchannel) {
241 visorchannel_destroy(dev->visorchannel);
242 dev->visorchannel = NULL;
247 /* Implement publishing of device node attributes under:
249 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
253 #define to_devmajorminor_attr(_attr) \
254 container_of(_attr, struct devmajorminor_attribute, attr)
255 #define to_visor_device_from_kobjdevmajorminor(obj) \
256 container_of(obj, struct visor_device, kobjdevmajorminor)
258 struct devmajorminor_attribute {
259 struct attribute attr;
261 ssize_t (*show)(struct visor_device *, int slot, char *buf);
262 ssize_t (*store)(struct visor_device *, int slot, const char *buf,
266 static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
268 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
270 if (slot < 0 || slot >= maxdevnodes)
272 return snprintf(buf, PAGE_SIZE, "%d:%d\n",
273 dev->devnodes[slot].major, dev->devnodes[slot].minor);
277 devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
279 struct devmajorminor_attribute *devmajorminor_attr =
280 to_devmajorminor_attr(attr);
281 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
284 if (devmajorminor_attr->show)
285 ret = devmajorminor_attr->show(dev,
286 devmajorminor_attr->slot, buf);
291 devmajorminor_attr_store(struct kobject *kobj,
292 struct attribute *attr, const char *buf, size_t count)
294 struct devmajorminor_attribute *devmajorminor_attr =
295 to_devmajorminor_attr(attr);
296 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
299 if (devmajorminor_attr->store)
300 ret = devmajorminor_attr->store(dev,
301 devmajorminor_attr->slot,
306 static int register_devmajorminor_attributes(struct visor_device *dev);
309 devmajorminor_create_file(struct visor_device *dev, const char *name,
310 int major, int minor)
312 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
313 struct devmajorminor_attribute *myattr = NULL;
314 int x = -1, rc = 0, slot = -1;
316 register_devmajorminor_attributes(dev);
317 for (slot = 0; slot < maxdevnodes; slot++)
318 if (!dev->devnodes[slot].attr)
320 if (slot == maxdevnodes) {
324 myattr = kzalloc(sizeof(*myattr), GFP_KERNEL);
329 myattr->show = DEVMAJORMINOR_ATTR;
330 myattr->store = NULL;
332 myattr->attr.name = name;
333 myattr->attr.mode = S_IRUGO;
334 dev->devnodes[slot].attr = myattr;
335 dev->devnodes[slot].major = major;
336 dev->devnodes[slot].minor = minor;
337 x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
342 kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
347 dev->devnodes[slot].attr = NULL;
353 devmajorminor_remove_file(struct visor_device *dev, int slot)
355 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
356 struct devmajorminor_attribute *myattr = NULL;
358 if (slot < 0 || slot >= maxdevnodes)
360 myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
363 sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
364 kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
365 dev->devnodes[slot].attr = NULL;
370 devmajorminor_remove_all_files(struct visor_device *dev)
373 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
375 for (i = 0; i < maxdevnodes; i++)
376 devmajorminor_remove_file(dev, i);
379 static const struct sysfs_ops devmajorminor_sysfs_ops = {
380 .show = devmajorminor_attr_show,
381 .store = devmajorminor_attr_store,
384 static struct kobj_type devmajorminor_kobj_type = {
385 .sysfs_ops = &devmajorminor_sysfs_ops
389 register_devmajorminor_attributes(struct visor_device *dev)
393 if (dev->kobjdevmajorminor.parent)
394 goto away; /* already registered */
395 x = kobject_init_and_add(&dev->kobjdevmajorminor,
396 &devmajorminor_kobj_type, &dev->device.kobj,
403 kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
410 unregister_devmajorminor_attributes(struct visor_device *dev)
412 if (!dev->kobjdevmajorminor.parent)
413 return; /* already unregistered */
414 devmajorminor_remove_all_files(dev);
416 kobject_del(&dev->kobjdevmajorminor);
417 kobject_put(&dev->kobjdevmajorminor);
418 dev->kobjdevmajorminor.parent = NULL;
421 /* begin implementation of specific channel attributes to appear under
422 * /sys/bus/visorbus<x>/dev<y>/channel
424 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
427 struct visor_device *vdev = to_visor_device(dev);
429 if (!vdev->visorchannel)
431 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
432 visorchannel_get_physaddr(vdev->visorchannel));
435 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
438 struct visor_device *vdev = to_visor_device(dev);
440 if (!vdev->visorchannel)
442 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
443 visorchannel_get_nbytes(vdev->visorchannel));
446 static ssize_t clientpartition_show(struct device *dev,
447 struct device_attribute *attr, char *buf)
449 struct visor_device *vdev = to_visor_device(dev);
451 if (!vdev->visorchannel)
453 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
454 visorchannel_get_clientpartition(vdev->visorchannel));
457 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
460 struct visor_device *vdev = to_visor_device(dev);
463 if (!vdev->visorchannel)
465 return snprintf(buf, PAGE_SIZE, "%s\n",
466 visorchannel_id(vdev->visorchannel, s));
469 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
472 struct visor_device *vdev = to_visor_device(dev);
475 if (!vdev->visorchannel)
477 return snprintf(buf, PAGE_SIZE, "%s\n",
478 visorchannel_zoneid(vdev->visorchannel, s));
481 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
484 struct visor_device *vdev = to_visor_device(dev);
486 struct bus_type *xbus = dev->bus;
487 struct device_driver *xdrv = dev->driver;
488 struct visor_driver *drv = NULL;
490 if (!vdev->visorchannel || !xbus || !xdrv)
492 i = xbus->match(dev, xdrv);
495 drv = to_visor_driver(xdrv);
496 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
499 static DEVICE_ATTR_RO(physaddr);
500 static DEVICE_ATTR_RO(nbytes);
501 static DEVICE_ATTR_RO(clientpartition);
502 static DEVICE_ATTR_RO(typeguid);
503 static DEVICE_ATTR_RO(zoneguid);
504 static DEVICE_ATTR_RO(typename);
506 static struct attribute *channel_attrs[] = {
507 &dev_attr_physaddr.attr,
508 &dev_attr_nbytes.attr,
509 &dev_attr_clientpartition.attr,
510 &dev_attr_typeguid.attr,
511 &dev_attr_zoneguid.attr,
512 &dev_attr_typename.attr,
516 static struct attribute_group channel_attr_grp = {
518 .attrs = channel_attrs,
521 static const struct attribute_group *visorbus_channel_groups[] = {
526 /* end implementation of specific channel attributes */
528 /* BUS instance attributes
530 * define & implement display of bus attributes under
531 * /sys/bus/visorbus/busses/visorbus<n>.
533 * This is a bit hoaky because the kernel does not yet have the infrastructure
534 * to separate bus INSTANCE attributes from bus TYPE attributes...
535 * so we roll our own. See businst.c / businst.h.
539 static ssize_t partition_handle_show(struct device *dev,
540 struct device_attribute *attr,
542 struct visor_device *vdev = to_visor_device(dev);
543 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
545 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", handle);
548 static ssize_t partition_guid_show(struct device *dev,
549 struct device_attribute *attr,
551 struct visor_device *vdev = to_visor_device(dev);
553 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
556 static ssize_t partition_name_show(struct device *dev,
557 struct device_attribute *attr,
559 struct visor_device *vdev = to_visor_device(dev);
561 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
564 static ssize_t channel_addr_show(struct device *dev,
565 struct device_attribute *attr,
567 struct visor_device *vdev = to_visor_device(dev);
568 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
570 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", addr);
573 static ssize_t channel_bytes_show(struct device *dev,
574 struct device_attribute *attr,
576 struct visor_device *vdev = to_visor_device(dev);
577 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
579 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", nbytes);
582 static ssize_t channel_id_show(struct device *dev,
583 struct device_attribute *attr,
585 struct visor_device *vdev = to_visor_device(dev);
588 if (vdev->visorchannel) {
589 visorchannel_id(vdev->visorchannel, buf);
596 static ssize_t client_bus_info_show(struct device *dev,
597 struct device_attribute *attr,
599 struct visor_device *vdev = to_visor_device(dev);
600 struct visorchannel *channel = vdev->visorchannel;
602 int i, x, remain = PAGE_SIZE;
606 struct ultra_vbus_deviceinfo dev_info;
611 partition_name = vdev->name;
612 x = snprintf(p, remain,
613 "Client device / client driver info for %s partition (vbus #%d):\n",
614 partition_name, vdev->chipset_dev_no);
617 x = visorchannel_read(channel,
619 spar_vbus_channel_protocol,
621 &dev_info, sizeof(dev_info));
623 x = vbuschannel_devinfo_to_string(&dev_info, p,
628 x = visorchannel_read(channel,
630 spar_vbus_channel_protocol,
632 &dev_info, sizeof(dev_info));
634 x = vbuschannel_devinfo_to_string(&dev_info, p,
639 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
641 while (off + sizeof(dev_info) <=
642 visorchannel_get_nbytes(channel)) {
643 x = visorchannel_read(channel,
644 off, &dev_info, sizeof(dev_info));
646 x = vbuschannel_devinfo_to_string
647 (&dev_info, p, remain, i);
651 off += sizeof(dev_info);
655 return PAGE_SIZE - remain;
658 static DEVICE_ATTR_RO(partition_handle);
659 static DEVICE_ATTR_RO(partition_guid);
660 static DEVICE_ATTR_RO(partition_name);
661 static DEVICE_ATTR_RO(channel_addr);
662 static DEVICE_ATTR_RO(channel_bytes);
663 static DEVICE_ATTR_RO(channel_id);
664 static DEVICE_ATTR_RO(client_bus_info);
666 static struct attribute *dev_attrs[] = {
667 &dev_attr_partition_handle.attr,
668 &dev_attr_partition_guid.attr,
669 &dev_attr_partition_name.attr,
670 &dev_attr_channel_addr.attr,
671 &dev_attr_channel_bytes.attr,
672 &dev_attr_channel_id.attr,
673 &dev_attr_client_bus_info.attr,
677 static struct attribute_group dev_attr_grp = {
681 static const struct attribute_group *visorbus_groups[] = {
688 * define & implement display of driver attributes under
689 * /sys/bus/visorbus/drivers/<drivername>.
694 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
696 struct visor_driver *drv = to_visor_driver(xdrv);
698 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
702 register_driver_attributes(struct visor_driver *drv)
705 struct driver_attribute version =
706 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
707 drv->version_attr = version;
708 rc = driver_create_file(&drv->driver, &drv->version_attr);
713 unregister_driver_attributes(struct visor_driver *drv)
715 driver_remove_file(&drv->driver, &drv->version_attr);
719 dev_periodic_work(void *xdev)
721 struct visor_device *dev = xdev;
722 struct visor_driver *drv = to_visor_driver(dev->device.driver);
724 down(&dev->visordriver_callback_lock);
725 if (drv->channel_interrupt)
726 drv->channel_interrupt(dev);
727 up(&dev->visordriver_callback_lock);
728 if (!visor_periodic_work_nextperiod(dev->periodic_work))
729 put_device(&dev->device);
733 dev_start_periodic_work(struct visor_device *dev)
735 if (dev->being_removed)
737 /* now up by at least 2 */
738 get_device(&dev->device);
739 if (!visor_periodic_work_start(dev->periodic_work))
740 put_device(&dev->device);
744 dev_stop_periodic_work(struct visor_device *dev)
746 if (visor_periodic_work_stop(dev->periodic_work))
747 put_device(&dev->device);
750 /** This is called automatically upon adding a visor_device (device_add), or
751 * adding a visor_driver (visorbus_register_visor_driver), but only after
752 * visorbus_match has returned 1 to indicate a successful match between
756 visordriver_probe_device(struct device *xdev)
759 struct visor_driver *drv;
760 struct visor_device *dev;
762 drv = to_visor_driver(xdev->driver);
763 dev = to_visor_device(xdev);
764 down(&dev->visordriver_callback_lock);
765 dev->being_removed = false;
767 * ensure that the dev->being_removed flag is cleared before
771 get_device(&dev->device);
773 up(&dev->visordriver_callback_lock);
777 rc = drv->probe(dev);
781 fix_vbus_dev_info(dev);
782 up(&dev->visordriver_callback_lock);
786 put_device(&dev->device);
790 /** This is called when device_unregister() is called for each child device
791 * instance, to notify the appropriate visorbus_driver that the device is
792 * going away, and to decrease the reference count of the device.
795 visordriver_remove_device(struct device *xdev)
797 struct visor_device *dev;
798 struct visor_driver *drv;
800 dev = to_visor_device(xdev);
801 drv = to_visor_driver(xdev->driver);
802 down(&dev->visordriver_callback_lock);
803 dev->being_removed = true;
805 * ensure that the dev->being_removed flag is set before we start the
813 up(&dev->visordriver_callback_lock);
814 dev_stop_periodic_work(dev);
815 devmajorminor_remove_all_files(dev);
817 put_device(&dev->device);
822 /** A particular type of visor driver calls this function to register
823 * the driver. The caller MUST fill in the following fields within the
825 * name, version, owner, channel_types, probe, remove
827 * Here's how the whole Linux bus / driver / device model works.
829 * At system start-up, the visorbus kernel module is loaded, which registers
830 * visorbus_type as a bus type, using bus_register().
832 * All kernel modules that support particular device types on a
833 * visorbus bus are loaded. Each of these kernel modules calls
834 * visorbus_register_visor_driver() in their init functions, passing a
835 * visor_driver struct. visorbus_register_visor_driver() in turn calls
836 * register_driver(&visor_driver.driver). This .driver member is
837 * initialized with generic methods (like probe), whose sole responsibility
838 * is to act as a broker for the real methods, which are within the
839 * visor_driver struct. (This is the way the subclass behavior is
840 * implemented, since visor_driver is essentially a subclass of the
841 * generic driver.) Whenever a driver_register() happens, core bus code in
842 * the kernel does (see device_attach() in drivers/base/dd.c):
844 * for each dev associated with the bus (the bus that driver is on) that
845 * does not yet have a driver
846 * if bus.match(dev,newdriver) == yes_matched ** .match specified
847 * ** during bus_register().
848 * newdriver.probe(dev) ** for visor drivers, this will call
849 * ** the generic driver.probe implemented in visorbus.c,
850 * ** which in turn calls the probe specified within the
851 * ** struct visor_driver (which was specified by the
852 * ** actual device driver as part of
853 * ** visorbus_register_visor_driver()).
855 * The above dance also happens when a new device appears.
856 * So the question is, how are devices created within the system?
857 * Basically, just call device_add(dev). See pci_bus_add_devices().
858 * pci_scan_device() shows an example of how to build a device struct. It
859 * returns the newly-created struct to pci_scan_single_device(), who adds it
860 * to the list of devices at PCIBUS.devices. That list of devices is what
861 * is traversed by pci_bus_add_devices().
864 int visorbus_register_visor_driver(struct visor_driver *drv)
869 return -ENODEV; /*can't register on a nonexistent bus*/
871 drv->driver.name = drv->name;
872 drv->driver.bus = &visorbus_type;
873 drv->driver.probe = visordriver_probe_device;
874 drv->driver.remove = visordriver_remove_device;
875 drv->driver.owner = drv->owner;
877 /* driver_register does this:
878 * bus_add_driver(drv)
879 * ->if (drv.bus) ** (bus_type) **
881 * for each dev with bus type of drv.bus
882 * if (!dev.drv) ** no driver assigned yet **
883 * if (bus.match(dev,drv)) [visorbus_match]
885 * if (!drv.probe(dev)) [visordriver_probe_device]
889 rc = driver_register(&drv->driver);
892 rc = register_driver_attributes(drv);
894 driver_unregister(&drv->driver);
897 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
899 /** A particular type of visor driver calls this function to unregister
900 * the driver, i.e., within its module_exit function.
903 visorbus_unregister_visor_driver(struct visor_driver *drv)
905 unregister_driver_attributes(drv);
906 driver_unregister(&drv->driver);
908 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
911 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
912 void *dest, unsigned long nbytes)
914 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
916 EXPORT_SYMBOL_GPL(visorbus_read_channel);
919 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
920 void *src, unsigned long nbytes)
922 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
924 EXPORT_SYMBOL_GPL(visorbus_write_channel);
927 visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
928 unsigned long nbytes)
930 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
932 EXPORT_SYMBOL_GPL(visorbus_clear_channel);
935 visorbus_registerdevnode(struct visor_device *dev,
936 const char *name, int major, int minor)
938 return devmajorminor_create_file(dev, name, major, minor);
940 EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
942 /** We don't really have a real interrupt, so for now we just call the
943 * interrupt function periodically...
946 visorbus_enable_channel_interrupts(struct visor_device *dev)
948 dev_start_periodic_work(dev);
950 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
953 visorbus_disable_channel_interrupts(struct visor_device *dev)
955 dev_stop_periodic_work(dev);
957 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
959 /** This is how everything starts from the device end.
960 * This function is called when a channel first appears via a ControlVM
961 * message. In response, this function allocates a visor_device to
962 * correspond to the new channel, and attempts to connect it the appropriate
963 * driver. If the appropriate driver is found, the visor_driver.probe()
964 * function for that driver will be called, and will be passed the new
965 * visor_device that we just created.
967 * It's ok if the appropriate driver is not yet loaded, because in that case
968 * the new device struct will just stick around in the bus' list of devices.
969 * When the appropriate driver calls visorbus_register_visor_driver(), the
970 * visor_driver.probe() for the new driver will be called with the new
974 create_visor_device(struct visor_device *dev)
977 u32 chipset_bus_no = dev->chipset_bus_no;
978 u32 chipset_dev_no = dev->chipset_dev_no;
980 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
981 POSTCODE_SEVERITY_INFO);
983 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
984 dev->device.bus = &visorbus_type;
985 dev->device.groups = visorbus_channel_groups;
986 device_initialize(&dev->device);
987 dev->device.release = visorbus_release_device;
988 /* keep a reference just for us (now 2) */
989 get_device(&dev->device);
991 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
992 periodic_dev_workqueue,
994 dev, dev_name(&dev->device));
995 if (!dev->periodic_work) {
996 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
1001 /* bus_id must be a unique name with respect to this bus TYPE
1002 * (NOT bus instance). That's why we need to include the bus
1003 * number within the name.
1005 dev_set_name(&dev->device, "vbus%u:dev%u",
1006 chipset_bus_no, chipset_dev_no);
1008 /* device_add does this:
1009 * bus_add_device(dev)
1010 * ->device_attach(dev)
1011 * ->for each driver drv registered on the bus that dev is on
1012 * if (dev.drv) ** device already has a driver **
1013 * ** not sure we could ever get here... **
1015 * if (bus.match(dev,drv)) [visorbus_match]
1017 * if (!drv.probe(dev)) [visordriver_probe_device]
1020 * Note that device_add does NOT fail if no driver failed to
1021 * claim the device. The device will be linked onto
1022 * bus_type.klist_devices regardless (use bus_for_each_dev).
1024 rc = device_add(&dev->device);
1026 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
1031 rc = register_devmajorminor_attributes(dev);
1033 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1038 list_add_tail(&dev->list_all, &list_all_device_instances);
1042 device_unregister(&dev->device);
1044 put_device(&dev->device);
1049 remove_visor_device(struct visor_device *dev)
1051 list_del(&dev->list_all);
1052 unregister_devmajorminor_attributes(dev);
1053 put_device(&dev->device);
1054 device_unregister(&dev->device);
1058 get_vbus_header_info(struct visorchannel *chan,
1059 struct spar_vbus_headerinfo *hdr_info)
1063 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
1065 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1066 sizeof(*hdr_info)) < 0) {
1069 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
1071 if (hdr_info->device_info_struct_bytes <
1072 sizeof(struct ultra_vbus_deviceinfo)) {
1080 /* Write the contents of <info> to the struct
1081 * spar_vbus_channel_protocol.chp_info. */
1084 write_vbus_chp_info(struct visorchannel *chan,
1085 struct spar_vbus_headerinfo *hdr_info,
1086 struct ultra_vbus_deviceinfo *info)
1088 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1090 if (hdr_info->chp_info_offset == 0)
1093 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1098 /* Write the contents of <info> to the struct
1099 * spar_vbus_channel_protocol.bus_info. */
1102 write_vbus_bus_info(struct visorchannel *chan,
1103 struct spar_vbus_headerinfo *hdr_info,
1104 struct ultra_vbus_deviceinfo *info)
1106 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1108 if (hdr_info->bus_info_offset == 0)
1111 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1116 /* Write the contents of <info> to the
1117 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1120 write_vbus_dev_info(struct visorchannel *chan,
1121 struct spar_vbus_headerinfo *hdr_info,
1122 struct ultra_vbus_deviceinfo *info, int devix)
1125 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1126 (hdr_info->device_info_struct_bytes * devix);
1128 if (hdr_info->dev_info_offset == 0)
1131 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1136 /* For a child device just created on a client bus, fill in
1137 * information about the driver that is controlling this device into
1138 * the the appropriate slot within the vbus channel of the bus
1142 fix_vbus_dev_info(struct visor_device *visordev)
1145 struct visor_device *bdev;
1146 struct visor_driver *visordrv;
1147 int bus_no = visordev->chipset_bus_no;
1148 int dev_no = visordev->chipset_dev_no;
1149 struct ultra_vbus_deviceinfo dev_info;
1150 const char *chan_type_name = NULL;
1151 struct spar_vbus_headerinfo *hdr_info;
1153 if (!visordev->device.driver)
1156 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
1160 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
1164 visordrv = to_visor_driver(visordev->device.driver);
1166 /* Within the list of device types (by GUID) that the driver
1167 * says it supports, find out which one of those types matches
1168 * the type of this device, so that we can include the device
1171 for (i = 0; visordrv->channel_types[i].name; i++) {
1172 if (memcmp(&visordrv->channel_types[i].guid,
1173 &visordev->channel_type_guid,
1174 sizeof(visordrv->channel_types[i].guid)) == 0) {
1175 chan_type_name = visordrv->channel_types[i].name;
1180 bus_device_info_init(&dev_info, chan_type_name,
1181 visordrv->name, visordrv->version,
1183 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
1185 /* Re-write bus+chipset info, because it is possible that this
1186 * was previously written by our evil counterpart, virtpci.
1188 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
1189 write_vbus_bus_info(bdev->visorchannel, hdr_info,
1190 &clientbus_driverinfo);
1193 /** Create a device instance for the visor bus itself.
1196 create_bus_instance(struct visor_device *dev)
1199 int id = dev->chipset_bus_no;
1200 struct spar_vbus_headerinfo *hdr_info;
1202 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1204 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1210 dev_set_name(&dev->device, "visorbus%d", id);
1211 dev->device.bus = &visorbus_type;
1212 dev->device.groups = visorbus_groups;
1213 dev->device.release = visorbus_release_busdevice;
1215 if (device_register(&dev->device) < 0) {
1216 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1217 POSTCODE_SEVERITY_ERR);
1222 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1223 dev->vbus_hdr_info = (void *)hdr_info;
1224 write_vbus_chp_info(dev->visorchannel, hdr_info,
1225 &chipset_driverinfo);
1226 write_vbus_bus_info(dev->visorchannel, hdr_info,
1227 &clientbus_driverinfo);
1232 list_add_tail(&dev->list_all, &list_all_bus_instances);
1233 dev_set_drvdata(&dev->device, dev);
1242 /** Remove a device instance for the visor bus itself.
1245 remove_bus_instance(struct visor_device *dev)
1247 /* Note that this will result in the release method for
1248 * dev->dev being called, which will call
1249 * visorbus_release_busdevice(). This has something to do with
1250 * the put_device() done in device_unregister(), but I have never
1251 * successfully been able to trace thru the code to see where/how
1252 * release() gets called. But I know it does.
1255 if (dev->visorchannel) {
1256 visorchannel_destroy(dev->visorchannel);
1257 dev->visorchannel = NULL;
1259 kfree(dev->vbus_hdr_info);
1260 list_del(&dev->list_all);
1261 device_unregister(&dev->device);
1264 /** Create and register the one-and-only one instance of
1265 * the visor bus type (visorbus_type).
1268 create_bus_type(void)
1270 busreg_rc = bus_register(&visorbus_type);
1274 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1277 remove_bus_type(void)
1279 bus_unregister(&visorbus_type);
1282 /** Remove all child visor bus device instances.
1285 remove_all_visor_devices(void)
1287 struct list_head *listentry, *listtmp;
1289 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1290 struct visor_device *dev = list_entry(listentry,
1291 struct visor_device,
1293 remove_visor_device(dev);
1298 chipset_bus_create(struct visor_device *dev)
1301 u32 bus_no = dev->chipset_bus_no;
1303 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1304 rc = create_bus_instance(dev);
1305 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1308 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1309 POSTCODE_SEVERITY_ERR);
1311 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1312 POSTCODE_SEVERITY_INFO);
1314 if (chipset_responders.bus_create)
1315 (*chipset_responders.bus_create) (dev, rc);
1319 chipset_bus_destroy(struct visor_device *dev)
1321 remove_bus_instance(dev);
1322 if (chipset_responders.bus_destroy)
1323 (*chipset_responders.bus_destroy)(dev, 0);
1327 chipset_device_create(struct visor_device *dev_info)
1330 u32 bus_no = dev_info->chipset_bus_no;
1331 u32 dev_no = dev_info->chipset_dev_no;
1333 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1334 POSTCODE_SEVERITY_INFO);
1336 rc = create_visor_device(dev_info);
1337 if (chipset_responders.device_create)
1338 chipset_responders.device_create(dev_info, rc);
1341 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1342 POSTCODE_SEVERITY_ERR);
1344 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1345 POSTCODE_SEVERITY_INFO);
1349 chipset_device_destroy(struct visor_device *dev_info)
1351 remove_visor_device(dev_info);
1353 if (chipset_responders.device_destroy)
1354 (*chipset_responders.device_destroy) (dev_info, 0);
1357 /* This is the callback function specified for a function driver, to
1358 * be called when a pending "pause device" operation has been
1362 pause_state_change_complete(struct visor_device *dev, int status)
1367 dev->pausing = false;
1368 if (!chipset_responders.device_pause) /* this can never happen! */
1371 /* Notify the chipset driver that the pause is complete, which
1372 * will presumably want to send some sort of response to the
1374 (*chipset_responders.device_pause) (dev, status);
1377 /* This is the callback function specified for a function driver, to
1378 * be called when a pending "resume device" operation has been
1382 resume_state_change_complete(struct visor_device *dev, int status)
1387 dev->resuming = false;
1388 if (!chipset_responders.device_resume) /* this can never happen! */
1391 /* Notify the chipset driver that the resume is complete,
1392 * which will presumably want to send some sort of response to
1394 (*chipset_responders.device_resume) (dev, status);
1397 /* Tell the subordinate function driver for a specific device to pause
1398 * or resume that device. Result is returned asynchronously via a
1399 * callback function.
1402 initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
1405 struct visor_driver *drv = NULL;
1406 void (*notify_func)(struct visor_device *dev, int response) = NULL;
1409 notify_func = chipset_responders.device_pause;
1411 notify_func = chipset_responders.device_resume;
1415 drv = to_visor_driver(dev->device.driver);
1419 if (dev->pausing || dev->resuming)
1422 /* Note that even though both drv->pause() and drv->resume
1423 * specify a callback function, it is NOT necessary for us to
1424 * increment our local module usage count. Reason is, there
1425 * is already a linkage dependency between child function
1426 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1427 * visorbus while child function drivers are still running.
1433 dev->pausing = true;
1434 x = drv->pause(dev, pause_state_change_complete);
1436 /* This should be done at BUS resume time, but an
1437 * existing problem prevents us from ever getting a bus
1438 * resume... This hack would fail to work should we
1439 * ever have a bus that contains NO devices, since we
1440 * would never even get here in that case. */
1441 fix_vbus_dev_info(dev);
1445 dev->resuming = true;
1446 x = drv->resume(dev, resume_state_change_complete);
1450 dev->pausing = false;
1452 dev->resuming = false;
1459 (*notify_func)(dev, rc);
1464 chipset_device_pause(struct visor_device *dev_info)
1466 initiate_chipset_device_pause_resume(dev_info, true);
1470 chipset_device_resume(struct visor_device *dev_info)
1472 initiate_chipset_device_pause_resume(dev_info, false);
1475 struct channel_size_info {
1477 unsigned long min_size;
1478 unsigned long max_size;
1486 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1487 bus_device_info_init(&clientbus_driverinfo,
1488 "clientbus", "visorbus",
1491 rc = create_bus_type();
1493 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1497 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1498 if (!periodic_dev_workqueue) {
1499 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
1504 /* This enables us to receive notifications when devices appear for
1505 * which this service partition is to be a server for.
1507 visorchipset_register_busdev(&chipset_notifiers,
1508 &chipset_responders,
1509 &chipset_driverinfo);
1515 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
1516 POSTCODE_SEVERITY_ERR);
1523 struct list_head *listentry, *listtmp;
1525 visorchipset_register_busdev(NULL, NULL, NULL);
1526 remove_all_visor_devices();
1528 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1529 destroy_workqueue(periodic_dev_workqueue);
1530 periodic_dev_workqueue = NULL;
1532 if (periodic_test_workqueue) {
1533 cancel_delayed_work(&periodic_work);
1534 flush_workqueue(periodic_test_workqueue);
1535 destroy_workqueue(periodic_test_workqueue);
1536 periodic_test_workqueue = NULL;
1539 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1540 struct visor_device *dev = list_entry(listentry,
1541 struct visor_device,
1543 remove_bus_instance(dev);
1548 module_param_named(debug, visorbus_debug, int, S_IRUGO);
1549 MODULE_PARM_DESC(visorbus_debug, "1 to debug");
1551 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1552 MODULE_PARM_DESC(visorbus_forcematch,
1553 "1 to force a successful dev <--> drv match");
1555 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1556 MODULE_PARM_DESC(visorbus_forcenomatch,
1557 "1 to force an UNsuccessful dev <--> drv match");
1559 module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1560 MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");