3 * Copyright � 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
18 #include <linux/uuid.h>
21 #include "visorbus_private.h"
23 #include "periodic_work.h"
24 #include "vbuschannel.h"
25 #include "guestlinuxdebug.h"
26 #include "vmcallinterface.h"
28 #define MYDRVNAME "visorbus"
30 /* module parameters */
31 static int visorbus_debug;
32 static int visorbus_forcematch;
33 static int visorbus_forcenomatch;
34 static int visorbus_debugref;
35 #define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
37 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
38 #define POLLJIFFIES_TESTWORK 100
39 #define POLLJIFFIES_NORMALCHANNEL 10
41 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
42 static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
43 static void fix_vbus_dev_info(struct visor_device *visordev);
45 /* BUS type attributes
47 * define & implement display of bus attributes under
52 static ssize_t version_show(struct bus_type *bus, char *buf)
54 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
57 static BUS_ATTR_RO(version);
59 static struct attribute *visorbus_bus_attrs[] = {
60 &bus_attr_version.attr,
64 static const struct attribute_group visorbus_bus_group = {
65 .attrs = visorbus_bus_attrs,
68 static const struct attribute_group *visorbus_bus_groups[] = {
74 /** This describes the TYPE of bus.
75 * (Don't confuse this with an INSTANCE of the bus.)
77 struct bus_type visorbus_type = {
79 .match = visorbus_match,
80 .uevent = visorbus_uevent,
81 .bus_groups = visorbus_bus_groups,
84 static struct delayed_work periodic_work;
86 /* YES, we need 2 workqueues.
87 * The reason is, workitems on the test queue may need to cancel
88 * workitems on the other queue. You will be in for trouble if you try to
89 * do this with workitems queued on the same workqueue.
91 static struct workqueue_struct *periodic_test_workqueue;
92 static struct workqueue_struct *periodic_dev_workqueue;
93 static long long bus_count; /** number of bus instances */
94 /** ever-increasing */
96 static void chipset_bus_create(struct visorchipset_bus_info *bus_info);
97 static void chipset_bus_destroy(struct visorchipset_bus_info *bus_info);
98 static void chipset_device_create(struct visorchipset_device_info *dev_info);
99 static void chipset_device_destroy(struct visorchipset_device_info *dev_info);
100 static void chipset_device_pause(struct visorchipset_device_info *dev_info);
101 static void chipset_device_resume(struct visorchipset_device_info *dev_info);
103 /** These functions are implemented herein, and are called by the chipset
104 * driver to notify us about specific events.
106 static struct visorchipset_busdev_notifiers chipset_notifiers = {
107 .bus_create = chipset_bus_create,
108 .bus_destroy = chipset_bus_destroy,
109 .device_create = chipset_device_create,
110 .device_destroy = chipset_device_destroy,
111 .device_pause = chipset_device_pause,
112 .device_resume = chipset_device_resume,
115 /** These functions are implemented in the chipset driver, and we call them
116 * herein when we want to acknowledge a specific event.
118 static struct visorchipset_busdev_responders chipset_responders;
120 /* filled in with info about parent chipset driver when we register with it */
121 static struct ultra_vbus_deviceinfo chipset_driverinfo;
122 /* filled in with info about this driver, wrt it servicing client busses */
123 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
125 /** list of visor_device structs, linked via .list_all */
126 static LIST_HEAD(list_all_bus_instances);
127 /** list of visor_device structs, linked via .list_all */
128 static LIST_HEAD(list_all_device_instances);
131 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
133 if (add_uevent_var(env, "VERSION=%s", VERSION))
138 /* This is called automatically upon adding a visor_device (device_add), or
139 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
140 * provided driver can control the specified device.
143 visorbus_match(struct device *xdev, struct device_driver *xdrv)
145 uuid_le channel_type;
148 struct visor_device *dev;
149 struct visor_driver *drv;
151 dev = to_visor_device(xdev);
152 drv = to_visor_driver(xdrv);
153 channel_type = visorchannel_get_uuid(dev->visorchannel);
154 if (visorbus_forcematch) {
158 if (visorbus_forcenomatch)
161 if (!drv->channel_types)
164 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
165 (drv->channel_types[i].name);
167 if (uuid_le_cmp(drv->channel_types[i].guid,
168 channel_type) == 0) {
176 /** This is called when device_unregister() is called for the bus device
177 * instance, after all other tasks involved with destroying the device
181 visorbus_release_busdevice(struct device *xdev)
183 struct visor_device *dev = dev_get_drvdata(xdev);
185 dev_set_drvdata(xdev, NULL);
190 /** This is called when device_unregister() is called for each child
194 visorbus_release_device(struct device *xdev)
196 struct visor_device *dev = to_visor_device(xdev);
198 if (dev->periodic_work) {
199 visor_periodic_work_destroy(dev->periodic_work);
200 dev->periodic_work = NULL;
202 if (dev->visorchannel) {
203 visorchannel_destroy(dev->visorchannel);
204 dev->visorchannel = NULL;
209 /* Implement publishing of device node attributes under:
211 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
215 #define to_devmajorminor_attr(_attr) \
216 container_of(_attr, struct devmajorminor_attribute, attr)
217 #define to_visor_device_from_kobjdevmajorminor(obj) \
218 container_of(obj, struct visor_device, kobjdevmajorminor)
220 struct devmajorminor_attribute {
221 struct attribute attr;
223 ssize_t (*show)(struct visor_device *, int slot, char *buf);
224 ssize_t (*store)(struct visor_device *, int slot, const char *buf,
228 static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
230 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
232 if (slot < 0 || slot >= maxdevnodes)
234 return snprintf(buf, PAGE_SIZE, "%d:%d\n",
235 dev->devnodes[slot].major, dev->devnodes[slot].minor);
239 devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
241 struct devmajorminor_attribute *devmajorminor_attr =
242 to_devmajorminor_attr(attr);
243 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
246 if (devmajorminor_attr->show)
247 ret = devmajorminor_attr->show(dev,
248 devmajorminor_attr->slot, buf);
253 devmajorminor_attr_store(struct kobject *kobj,
254 struct attribute *attr, const char *buf, size_t count)
256 struct devmajorminor_attribute *devmajorminor_attr =
257 to_devmajorminor_attr(attr);
258 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
261 if (devmajorminor_attr->store)
262 ret = devmajorminor_attr->store(dev,
263 devmajorminor_attr->slot,
268 static int register_devmajorminor_attributes(struct visor_device *dev);
271 devmajorminor_create_file(struct visor_device *dev, const char *name,
272 int major, int minor)
274 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
275 struct devmajorminor_attribute *myattr = NULL;
276 int x = -1, rc = 0, slot = -1;
278 register_devmajorminor_attributes(dev);
279 for (slot = 0; slot < maxdevnodes; slot++)
280 if (!dev->devnodes[slot].attr)
282 if (slot == maxdevnodes) {
286 myattr = kmalloc(sizeof(*myattr), GFP_KERNEL);
291 memset(myattr, 0, sizeof(struct devmajorminor_attribute));
292 myattr->show = DEVMAJORMINOR_ATTR;
293 myattr->store = NULL;
295 myattr->attr.name = name;
296 myattr->attr.mode = S_IRUGO;
297 dev->devnodes[slot].attr = myattr;
298 dev->devnodes[slot].major = major;
299 dev->devnodes[slot].minor = minor;
300 x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
305 kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
310 dev->devnodes[slot].attr = NULL;
316 devmajorminor_remove_file(struct visor_device *dev, int slot)
318 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
319 struct devmajorminor_attribute *myattr = NULL;
321 if (slot < 0 || slot >= maxdevnodes)
323 myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
326 sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
327 kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
328 dev->devnodes[slot].attr = NULL;
333 devmajorminor_remove_all_files(struct visor_device *dev)
336 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
338 for (i = 0; i < maxdevnodes; i++)
339 devmajorminor_remove_file(dev, i);
342 static const struct sysfs_ops devmajorminor_sysfs_ops = {
343 .show = devmajorminor_attr_show,
344 .store = devmajorminor_attr_store,
347 static struct kobj_type devmajorminor_kobj_type = {
348 .sysfs_ops = &devmajorminor_sysfs_ops
352 register_devmajorminor_attributes(struct visor_device *dev)
356 if (dev->kobjdevmajorminor.parent)
357 goto away; /* already registered */
358 x = kobject_init_and_add(&dev->kobjdevmajorminor,
359 &devmajorminor_kobj_type, &dev->device.kobj,
366 kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
373 unregister_devmajorminor_attributes(struct visor_device *dev)
375 if (!dev->kobjdevmajorminor.parent)
376 return; /* already unregistered */
377 devmajorminor_remove_all_files(dev);
379 kobject_del(&dev->kobjdevmajorminor);
380 kobject_put(&dev->kobjdevmajorminor);
381 dev->kobjdevmajorminor.parent = NULL;
384 /* begin implementation of specific channel attributes to appear under
385 * /sys/bus/visorbus<x>/dev<y>/channel
387 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
390 struct visor_device *vdev = to_visor_device(dev);
392 if (!vdev->visorchannel)
394 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
395 visorchannel_get_physaddr(vdev->visorchannel));
398 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
401 struct visor_device *vdev = to_visor_device(dev);
403 if (!vdev->visorchannel)
405 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
406 visorchannel_get_nbytes(vdev->visorchannel));
409 static ssize_t clientpartition_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 struct visor_device *vdev = to_visor_device(dev);
414 if (!vdev->visorchannel)
416 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
417 visorchannel_get_clientpartition(vdev->visorchannel));
420 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
423 struct visor_device *vdev = to_visor_device(dev);
426 if (!vdev->visorchannel)
428 return snprintf(buf, PAGE_SIZE, "%s\n",
429 visorchannel_id(vdev->visorchannel, s));
432 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
435 struct visor_device *vdev = to_visor_device(dev);
438 if (!vdev->visorchannel)
440 return snprintf(buf, PAGE_SIZE, "%s\n",
441 visorchannel_zoneid(vdev->visorchannel, s));
444 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
447 struct visor_device *vdev = to_visor_device(dev);
449 struct bus_type *xbus = dev->bus;
450 struct device_driver *xdrv = dev->driver;
451 struct visor_driver *drv = NULL;
453 if (!vdev->visorchannel || !xbus || !xdrv)
455 i = xbus->match(dev, xdrv);
458 drv = to_visor_driver(xdrv);
459 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
462 static DEVICE_ATTR_RO(physaddr);
463 static DEVICE_ATTR_RO(nbytes);
464 static DEVICE_ATTR_RO(clientpartition);
465 static DEVICE_ATTR_RO(typeguid);
466 static DEVICE_ATTR_RO(zoneguid);
467 static DEVICE_ATTR_RO(typename);
469 static struct attribute *channel_attrs[] = {
470 &dev_attr_physaddr.attr,
471 &dev_attr_nbytes.attr,
472 &dev_attr_clientpartition.attr,
473 &dev_attr_typeguid.attr,
474 &dev_attr_zoneguid.attr,
475 &dev_attr_typename.attr,
478 static struct attribute_group channel_attr_grp = {
480 .attrs = channel_attrs,
483 static const struct attribute_group *visorbus_dev_groups[] = {
488 /* end implementation of specific channel attributes */
490 /* BUS instance attributes
492 * define & implement display of bus attributes under
493 * /sys/bus/visorbus/busses/visorbus<n>.
495 * This is a bit hoaky because the kernel does not yet have the infrastructure
496 * to separate bus INSTANCE attributes from bus TYPE attributes...
497 * so we roll our own. See businst.c / businst.h.
501 static ssize_t partition_handle_show(struct device *dev,
502 struct device_attribute *attr,
504 struct visor_device *vdev = to_visor_device(dev);
505 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
507 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", handle);
510 static ssize_t partition_guid_show(struct device *dev,
511 struct device_attribute *attr,
513 struct visor_device *vdev = to_visor_device(dev);
515 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
518 static ssize_t partition_name_show(struct device *dev,
519 struct device_attribute *attr,
521 struct visor_device *vdev = to_visor_device(dev);
523 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
526 static ssize_t channel_addr_show(struct device *dev,
527 struct device_attribute *attr,
529 struct visor_device *vdev = to_visor_device(dev);
530 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
532 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", addr);
535 static ssize_t channel_bytes_show(struct device *dev,
536 struct device_attribute *attr,
538 struct visor_device *vdev = to_visor_device(dev);
539 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
541 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", nbytes);
544 static ssize_t channel_id_show(struct device *dev,
545 struct device_attribute *attr,
547 struct visor_device *vdev = to_visor_device(dev);
550 if (vdev->visorchannel) {
551 visorchannel_id(vdev->visorchannel, buf);
558 static ssize_t client_bus_info_show(struct device *dev,
559 struct device_attribute *attr,
561 struct visor_device *vdev = to_visor_device(dev);
562 struct visorchannel *channel = vdev->visorchannel;
564 int i, x, remain = PAGE_SIZE;
568 struct ultra_vbus_deviceinfo dev_info;
573 partition_name = vdev->name;
574 x = snprintf(p, remain,
575 "Client device / client driver info for %s partition (vbus #%ld):\n",
576 partition_name, vdev->chipset_dev_no);
579 x = visorchannel_read(channel,
581 spar_vbus_channel_protocol,
583 &dev_info, sizeof(dev_info));
585 x = vbuschannel_devinfo_to_string(&dev_info, p,
590 x = visorchannel_read(channel,
592 spar_vbus_channel_protocol,
594 &dev_info, sizeof(dev_info));
596 x = vbuschannel_devinfo_to_string(&dev_info, p,
601 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
603 while (off + sizeof(dev_info) <=
604 visorchannel_get_nbytes(channel)) {
605 x = visorchannel_read(channel,
606 off, &dev_info, sizeof(dev_info));
608 x = vbuschannel_devinfo_to_string
609 (&dev_info, p, remain, i);
613 off += sizeof(dev_info);
617 return PAGE_SIZE - remain;
620 static DEVICE_ATTR_RO(partition_handle);
621 static DEVICE_ATTR_RO(partition_guid);
622 static DEVICE_ATTR_RO(partition_name);
623 static DEVICE_ATTR_RO(channel_addr);
624 static DEVICE_ATTR_RO(channel_bytes);
625 static DEVICE_ATTR_RO(channel_id);
626 static DEVICE_ATTR_RO(client_bus_info);
628 static struct attribute *dev_attrs[] = {
629 &dev_attr_partition_handle.attr,
630 &dev_attr_partition_guid.attr,
631 &dev_attr_partition_name.attr,
632 &dev_attr_channel_addr.attr,
633 &dev_attr_channel_bytes.attr,
634 &dev_attr_channel_id.attr,
635 &dev_attr_client_bus_info.attr,
639 static struct attribute_group dev_attr_grp = {
643 static const struct attribute_group *visorbus_groups[] = {
650 * define & implement display of driver attributes under
651 * /sys/bus/visorbus/drivers/<drivername>.
656 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
658 struct visor_driver *drv = to_visor_driver(xdrv);
660 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
664 register_driver_attributes(struct visor_driver *drv)
667 struct driver_attribute version =
668 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
669 drv->version_attr = version;
670 rc = driver_create_file(&drv->driver, &drv->version_attr);
675 unregister_driver_attributes(struct visor_driver *drv)
677 driver_remove_file(&drv->driver, &drv->version_attr);
681 dev_periodic_work(void *xdev)
683 struct visor_device *dev = (struct visor_device *)xdev;
684 struct visor_driver *drv = to_visor_driver(dev->device.driver);
686 down(&dev->visordriver_callback_lock);
687 if (drv->channel_interrupt)
688 drv->channel_interrupt(dev);
689 up(&dev->visordriver_callback_lock);
690 if (!visor_periodic_work_nextperiod(dev->periodic_work))
691 put_device(&dev->device);
695 dev_start_periodic_work(struct visor_device *dev)
697 if (dev->being_removed)
699 /* now up by at least 2 */
700 get_device(&dev->device);
701 if (!visor_periodic_work_start(dev->periodic_work))
702 put_device(&dev->device);
706 dev_stop_periodic_work(struct visor_device *dev)
708 if (visor_periodic_work_stop(dev->periodic_work))
709 put_device(&dev->device);
712 /** This is called automatically upon adding a visor_device (device_add), or
713 * adding a visor_driver (visorbus_register_visor_driver), but only after
714 * visorbus_match has returned 1 to indicate a successful match between
718 visordriver_probe_device(struct device *xdev)
721 struct visor_driver *drv;
722 struct visor_device *dev;
724 drv = to_visor_driver(xdev->driver);
725 dev = to_visor_device(xdev);
726 down(&dev->visordriver_callback_lock);
727 dev->being_removed = false;
729 * ensure that the dev->being_removed flag is cleared before
733 get_device(&dev->device);
735 up(&dev->visordriver_callback_lock);
739 rc = drv->probe(dev);
743 fix_vbus_dev_info(dev);
744 up(&dev->visordriver_callback_lock);
748 put_device(&dev->device);
749 /* We could get here more than once if the child driver module is
750 * unloaded and re-loaded while devices are present. That's why we
751 * need a flag to be sure that we only respond to the device_create
752 * once. We cannot respond to the device_create prior to here,
753 * because until we call drv->probe() above, the channel has not been
756 if (!dev->responded_to_device_create) {
757 struct visorchipset_device_info dev_info;
759 if (!visorchipset_get_device_info(dev->chipset_bus_no,
762 /* hmm, what to do here */
765 dev->responded_to_device_create = true;
766 if (chipset_responders.device_create)
767 (*chipset_responders.device_create)(&dev_info, rc);
772 /** This is called when device_unregister() is called for each child device
773 * instance, to notify the appropriate visorbus_driver that the device is
774 * going away, and to decrease the reference count of the device.
777 visordriver_remove_device(struct device *xdev)
780 struct visor_device *dev;
781 struct visor_driver *drv;
783 dev = to_visor_device(xdev);
784 drv = to_visor_driver(xdev->driver);
785 down(&dev->visordriver_callback_lock);
786 dev->being_removed = true;
788 * ensure that the dev->being_removed flag is set before we start the
796 up(&dev->visordriver_callback_lock);
797 dev_stop_periodic_work(dev);
798 devmajorminor_remove_all_files(dev);
800 put_device(&dev->device);
805 /** A particular type of visor driver calls this function to register
806 * the driver. The caller MUST fill in the following fields within the
808 * name, version, owner, channel_types, probe, remove
810 * Here's how the whole Linux bus / driver / device model works.
812 * At system start-up, the visorbus kernel module is loaded, which registers
813 * visorbus_type as a bus type, using bus_register().
815 * All kernel modules that support particular device types on a
816 * visorbus bus are loaded. Each of these kernel modules calls
817 * visorbus_register_visor_driver() in their init functions, passing a
818 * visor_driver struct. visorbus_register_visor_driver() in turn calls
819 * register_driver(&visor_driver.driver). This .driver member is
820 * initialized with generic methods (like probe), whose sole responsibility
821 * is to act as a broker for the real methods, which are within the
822 * visor_driver struct. (This is the way the subclass behavior is
823 * implemented, since visor_driver is essentially a subclass of the
824 * generic driver.) Whenever a driver_register() happens, core bus code in
825 * the kernel does (see device_attach() in drivers/base/dd.c):
827 * for each dev associated with the bus (the bus that driver is on) that
828 * does not yet have a driver
829 * if bus.match(dev,newdriver) == yes_matched ** .match specified
830 * ** during bus_register().
831 * newdriver.probe(dev) ** for visor drivers, this will call
832 * ** the generic driver.probe implemented in visorbus.c,
833 * ** which in turn calls the probe specified within the
834 * ** struct visor_driver (which was specified by the
835 * ** actual device driver as part of
836 * ** visorbus_register_visor_driver()).
838 * The above dance also happens when a new device appears.
839 * So the question is, how are devices created within the system?
840 * Basically, just call device_add(dev). See pci_bus_add_devices().
841 * pci_scan_device() shows an example of how to build a device struct. It
842 * returns the newly-created struct to pci_scan_single_device(), who adds it
843 * to the list of devices at PCIBUS.devices. That list of devices is what
844 * is traversed by pci_bus_add_devices().
847 int visorbus_register_visor_driver(struct visor_driver *drv)
851 drv->driver.name = drv->name;
852 drv->driver.bus = &visorbus_type;
853 drv->driver.probe = visordriver_probe_device;
854 drv->driver.remove = visordriver_remove_device;
855 drv->driver.owner = drv->owner;
857 /* driver_register does this:
858 * bus_add_driver(drv)
859 * ->if (drv.bus) ** (bus_type) **
861 * for each dev with bus type of drv.bus
862 * if (!dev.drv) ** no driver assigned yet **
863 * if (bus.match(dev,drv)) [visorbus_match]
865 * if (!drv.probe(dev)) [visordriver_probe_device]
869 rc = driver_register(&drv->driver);
872 rc = register_driver_attributes(drv);
875 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
877 /** A particular type of visor driver calls this function to unregister
878 * the driver, i.e., within its module_exit function.
881 visorbus_unregister_visor_driver(struct visor_driver *drv)
883 unregister_driver_attributes(drv);
884 driver_unregister(&drv->driver);
886 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
889 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
890 void *dest, unsigned long nbytes)
892 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
894 EXPORT_SYMBOL_GPL(visorbus_read_channel);
897 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
898 void *src, unsigned long nbytes)
900 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
902 EXPORT_SYMBOL_GPL(visorbus_write_channel);
905 visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
906 unsigned long nbytes)
908 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
910 EXPORT_SYMBOL_GPL(visorbus_clear_channel);
913 visorbus_registerdevnode(struct visor_device *dev,
914 const char *name, int major, int minor)
916 return devmajorminor_create_file(dev, name, major, minor);
918 EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
920 /** We don't really have a real interrupt, so for now we just call the
921 * interrupt function periodically...
924 visorbus_enable_channel_interrupts(struct visor_device *dev)
926 dev_start_periodic_work(dev);
928 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
931 visorbus_disable_channel_interrupts(struct visor_device *dev)
933 dev_stop_periodic_work(dev);
935 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
937 /** This is how everything starts from the device end.
938 * This function is called when a channel first appears via a ControlVM
939 * message. In response, this function allocates a visor_device to
940 * correspond to the new channel, and attempts to connect it the appropriate
941 * driver. If the appropriate driver is found, the visor_driver.probe()
942 * function for that driver will be called, and will be passed the new
943 * visor_device that we just created.
945 * It's ok if the appropriate driver is not yet loaded, because in that case
946 * the new device struct will just stick around in the bus' list of devices.
947 * When the appropriate driver calls visorbus_register_visor_driver(), the
948 * visor_driver.probe() for the new driver will be called with the new
952 create_visor_device(struct visor_device *bdev,
953 struct visorchipset_device_info *dev_info,
954 u64 partition_handle)
957 struct visor_device *dev = NULL;
958 bool gotten = false, registered1 = false, registered2 = false;
959 u32 chipset_bus_no = dev_info->bus_no;
960 u32 chipset_dev_no = dev_info->dev_no;
962 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
963 POSTCODE_SEVERITY_INFO);
964 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
966 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
971 memset(dev, 0, sizeof(struct visor_device));
972 dev->visorchannel = dev_info->visorchannel;
973 dev->channel_type_guid = dev_info->channel_type_guid;
974 dev->chipset_bus_no = chipset_bus_no;
975 dev->chipset_dev_no = chipset_dev_no;
976 dev->device.parent = &bdev->device;
977 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
978 dev->device.bus = &visorbus_type;
979 dev->device.groups = visorbus_dev_groups;
980 device_initialize(&dev->device);
981 dev->device.release = visorbus_release_device;
982 /* keep a reference just for us (now 2) */
983 get_device(&dev->device);
986 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
987 periodic_dev_workqueue,
989 dev, dev_name(&dev->device));
990 if (!dev->periodic_work) {
991 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
996 /* bus_id must be a unique name with respect to this bus TYPE
997 * (NOT bus instance). That's why we need to include the bus
998 * number within the name.
1000 dev_set_name(&dev->device, "vbus%u:dev%u",
1001 chipset_bus_no, chipset_dev_no);
1003 /* device_add does this:
1004 * bus_add_device(dev)
1005 * ->device_attach(dev)
1006 * ->for each driver drv registered on the bus that dev is on
1007 * if (dev.drv) ** device already has a driver **
1008 * ** not sure we could ever get here... **
1010 * if (bus.match(dev,drv)) [visorbus_match]
1012 * if (!drv.probe(dev)) [visordriver_probe_device]
1015 * Note that device_add does NOT fail if no driver failed to
1016 * claim the device. The device will be linked onto
1017 * bus_type.klist_devices regardless (use bus_for_each_dev).
1019 rc = device_add(&dev->device);
1021 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
1026 /* note: device_register is simply device_initialize + device_add */
1029 rc = register_devmajorminor_attributes(dev);
1031 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1042 unregister_devmajorminor_attributes(dev);
1044 put_device(&dev->device);
1047 list_add_tail(&dev->list_all, &list_all_device_instances);
1053 remove_visor_device(struct visor_device *dev)
1055 list_del(&dev->list_all);
1056 unregister_devmajorminor_attributes(dev);
1057 put_device(&dev->device);
1058 device_unregister(&dev->device);
1061 static struct visor_device *
1062 find_visor_device_by_channel(struct visorchannel *channel)
1064 struct list_head *listentry, *listtmp;
1066 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1067 struct visor_device *dev = list_entry(listentry,
1068 struct visor_device,
1070 if (dev->visorchannel == channel)
1077 init_vbus_channel(struct visorchannel *chan)
1080 unsigned long allocated_bytes = visorchannel_get_nbytes(chan);
1081 struct spar_vbus_channel_protocol *x =
1082 kmalloc(sizeof(struct spar_vbus_channel_protocol),
1085 POSTCODE_LINUX_3(VBUS_CHANNEL_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1088 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1091 if (visorchannel_clear(chan, 0, 0, allocated_bytes) < 0) {
1092 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1093 POSTCODE_SEVERITY_ERR);
1096 if (visorchannel_read
1097 (chan, 0, x, sizeof(struct spar_vbus_channel_protocol)) < 0) {
1098 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1099 POSTCODE_SEVERITY_ERR);
1102 if (!SPAR_VBUS_CHANNEL_OK_SERVER(allocated_bytes)) {
1103 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1104 POSTCODE_SEVERITY_ERR);
1108 if (visorchannel_write
1109 (chan, 0, x, sizeof(struct spar_vbus_channel_protocol)) < 0) {
1110 POSTCODE_LINUX_3(VBUS_CHANNEL_FAILURE_PC, chan,
1111 POSTCODE_SEVERITY_ERR);
1115 POSTCODE_LINUX_3(VBUS_CHANNEL_EXIT_PC, chan, POSTCODE_SEVERITY_INFO);
1125 get_vbus_header_info(struct visorchannel *chan,
1126 struct spar_vbus_headerinfo *hdr_info)
1130 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
1132 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1133 sizeof(*hdr_info)) < 0) {
1136 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
1138 if (hdr_info->device_info_struct_bytes <
1139 sizeof(struct ultra_vbus_deviceinfo)) {
1147 /* Write the contents of <info> to the struct
1148 * spar_vbus_channel_protocol.chp_info. */
1151 write_vbus_chp_info(struct visorchannel *chan,
1152 struct spar_vbus_headerinfo *hdr_info,
1153 struct ultra_vbus_deviceinfo *info)
1155 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1157 if (hdr_info->chp_info_offset == 0)
1160 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1165 /* Write the contents of <info> to the struct
1166 * spar_vbus_channel_protocol.bus_info. */
1169 write_vbus_bus_info(struct visorchannel *chan,
1170 struct spar_vbus_headerinfo *hdr_info,
1171 struct ultra_vbus_deviceinfo *info)
1173 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1175 if (hdr_info->bus_info_offset == 0)
1178 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1183 /* Write the contents of <info> to the
1184 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1187 write_vbus_dev_info(struct visorchannel *chan,
1188 struct spar_vbus_headerinfo *hdr_info,
1189 struct ultra_vbus_deviceinfo *info, int devix)
1192 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1193 (hdr_info->device_info_struct_bytes * devix);
1195 if (hdr_info->dev_info_offset == 0)
1198 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1203 /* For a child device just created on a client bus, fill in
1204 * information about the driver that is controlling this device into
1205 * the the appropriate slot within the vbus channel of the bus
1209 fix_vbus_dev_info(struct visor_device *visordev)
1212 struct visorchipset_bus_info bus_info;
1213 struct visor_device *dev = NULL;
1214 struct visor_driver *visordrv;
1215 int bus_no = visordev->chipset_bus_no;
1216 int dev_no = visordev->chipset_dev_no;
1217 struct ultra_vbus_deviceinfo dev_info;
1218 const char *chan_type_name = NULL;
1219 struct spar_vbus_headerinfo *hdr_info;
1221 if (!visordev->device.driver)
1224 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
1226 visordrv = to_visor_driver(visordev->device.driver);
1227 if (!visorchipset_get_bus_info(bus_no, &bus_info))
1230 dev = (struct visor_device *)(bus_info.bus_driver_context);
1237 /* Within the list of device types (by GUID) that the driver
1238 * says it supports, find out which one of those types matches
1239 * the type of this device, so that we can include the device
1242 for (i = 0; visordrv->channel_types[i].name; i++) {
1243 if (memcmp(&visordrv->channel_types[i].guid,
1244 &visordev->channel_type_guid,
1245 sizeof(visordrv->channel_types[i].guid)) == 0) {
1246 chan_type_name = visordrv->channel_types[i].name;
1251 bus_device_info_init(&dev_info, chan_type_name,
1252 visordrv->name, visordrv->version,
1254 write_vbus_dev_info(dev->visorchannel, hdr_info, &dev_info, dev_no);
1256 /* Re-write bus+chipset info, because it is possible that this
1257 * was previously written by our evil counterpart, virtpci.
1259 write_vbus_chp_info(dev->visorchannel, hdr_info, &chipset_driverinfo);
1260 write_vbus_bus_info(dev->visorchannel, hdr_info, &clientbus_driverinfo);
1263 /** Create a device instance for the visor bus itself.
1265 static struct visor_device *
1266 create_bus_instance(struct visorchipset_bus_info *bus_info)
1268 struct visor_device *rc = NULL;
1269 struct visor_device *dev = NULL;
1270 int id = bus_info->bus_no;
1271 struct spar_vbus_headerinfo *hdr_info;
1273 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1274 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1276 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1281 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1287 dev_set_name(&dev->device, "visorbus%d", id);
1288 dev->device.bus = &visorbus_type;
1289 dev->device.groups = visorbus_groups;
1290 dev->device.release = visorbus_release_busdevice;
1291 if (device_register(&dev->device) < 0) {
1292 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1293 POSTCODE_SEVERITY_ERR);
1297 dev->chipset_bus_no = id;
1298 dev->visorchannel = bus_info->visorchannel;
1299 if (bus_info->flags.server) {
1300 init_vbus_channel(dev->visorchannel);
1302 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1303 dev->vbus_hdr_info = (void *)hdr_info;
1304 write_vbus_chp_info(dev->visorchannel, hdr_info,
1305 &chipset_driverinfo);
1306 write_vbus_bus_info(dev->visorchannel, hdr_info,
1307 &clientbus_driverinfo);
1313 list_add_tail(&dev->list_all, &list_all_bus_instances);
1315 dev = dev; /* for testing ONLY */
1316 dev_set_drvdata(&dev->device, dev);
1328 /** Remove a device instance for the visor bus itself.
1331 remove_bus_instance(struct visor_device *dev)
1333 /* Note that this will result in the release method for
1334 * dev->dev being called, which will call
1335 * visorbus_release_busdevice(). This has something to do with
1336 * the put_device() done in device_unregister(), but I have never
1337 * successfully been able to trace thru the code to see where/how
1338 * release() gets called. But I know it does.
1341 if (dev->visorchannel) {
1342 visorchannel_destroy(dev->visorchannel);
1343 dev->visorchannel = NULL;
1345 kfree(dev->vbus_hdr_info);
1346 list_del(&dev->list_all);
1347 device_unregister(&dev->device);
1350 /** Create and register the one-and-only one instance of
1351 * the visor bus type (visorbus_type).
1354 create_bus_type(void)
1358 rc = bus_register(&visorbus_type);
1362 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1365 remove_bus_type(void)
1367 bus_unregister(&visorbus_type);
1370 /** Remove all child visor bus device instances.
1373 remove_all_visor_devices(void)
1375 struct list_head *listentry, *listtmp;
1377 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1378 struct visor_device *dev = list_entry(listentry,
1379 struct visor_device,
1381 remove_visor_device(dev);
1386 chipset_bus_create(struct visorchipset_bus_info *bus_info)
1388 struct visor_device *dev;
1390 u32 bus_no = bus_info->bus_no;
1392 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1393 dev = create_bus_instance(bus_info);
1396 if (!visorchipset_set_bus_context(bus_info, dev))
1398 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1402 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1403 POSTCODE_SEVERITY_ERR);
1406 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1407 POSTCODE_SEVERITY_INFO);
1408 if (chipset_responders.bus_create)
1409 (*chipset_responders.bus_create) (bus_info, rc);
1413 chipset_bus_destroy(struct visorchipset_bus_info *bus_info)
1415 struct visor_device *dev;
1418 dev = (struct visor_device *)(bus_info->bus_driver_context);
1421 remove_bus_instance(dev);
1422 if (!visorchipset_set_bus_context(bus_info, NULL))
1428 if (chipset_responders.bus_destroy)
1429 (*chipset_responders.bus_destroy)(bus_info, rc);
1433 chipset_device_create(struct visorchipset_device_info *dev_info)
1435 struct visorchipset_bus_info bus_info;
1436 struct visor_device *dev = NULL;
1438 u32 bus_no = dev_info->bus_no;
1439 u32 dev_no = dev_info->dev_no;
1441 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1442 POSTCODE_SEVERITY_INFO);
1444 if (!visorchipset_get_bus_info(bus_no, &bus_info))
1446 POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, dev_no, bus_no,
1447 POSTCODE_SEVERITY_INFO);
1451 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1452 POSTCODE_SEVERITY_ERR);
1455 dev = (struct visor_device *)(bus_info.bus_driver_context);
1456 rc = create_visor_device(dev, dev_info, bus_info.partition_handle);
1457 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1458 POSTCODE_SEVERITY_INFO);
1460 if (chipset_responders.device_create)
1461 (*chipset_responders.device_create)(dev_info, rc);
1465 chipset_device_destroy(struct visorchipset_device_info *dev_info)
1467 struct visor_device *dev;
1470 dev = find_visor_device_by_channel(dev_info->visorchannel);
1478 if (chipset_responders.device_destroy)
1479 (*chipset_responders.device_destroy) (dev_info, rc);
1480 remove_visor_device(dev);
1483 /* This is the callback function specified for a function driver, to
1484 * be called when a pending "pause device" operation has been
1488 pause_state_change_complete(struct visor_device *dev, int status,
1491 struct visorchipset_device_info *dev_info = info;
1496 dev->pausing = false;
1497 if (!chipset_responders.device_pause) /* this can never happen! */
1500 /* Notify the chipset driver that the pause is complete, which
1501 * will presumably want to send some sort of response to the
1503 (*chipset_responders.device_pause) (dev_info, status);
1506 /* This is the callback function specified for a function driver, to
1507 * be called when a pending "resume device" operation has been
1511 resume_state_change_complete(struct visor_device *dev, int status,
1514 struct visorchipset_device_info *dev_info = info;
1519 dev->resuming = false;
1520 if (!chipset_responders.device_resume) /* this can never happen! */
1523 /* Notify the chipset driver that the resume is complete,
1524 * which will presumably want to send some sort of response to
1526 (*chipset_responders.device_resume) (dev_info, status);
1529 /* Tell the subordinate function driver for a specific device to pause
1530 * or resume that device. Result is returned asynchronously via a
1531 * callback function.
1534 initiate_chipset_device_pause_resume(struct visorchipset_device_info *dev_info,
1537 struct visor_device *dev = NULL;
1539 struct visor_driver *drv = NULL;
1540 void (*notify_func)(struct visorchipset_device_info *dev_info,
1541 int response) = NULL;
1544 notify_func = chipset_responders.device_pause;
1546 notify_func = chipset_responders.device_resume;
1550 dev = find_visor_device_by_channel(dev_info->visorchannel);
1554 drv = to_visor_driver(dev->device.driver);
1558 if (dev->pausing || dev->resuming)
1561 /* Note that even though both drv->pause() and drv->resume
1562 * specify a callback function, it is NOT necessary for us to
1563 * increment our local module usage count. Reason is, there
1564 * is already a linkage dependency between child function
1565 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1566 * visorbus while child function drivers are still running.
1572 dev->pausing = true;
1573 x = drv->pause(dev, pause_state_change_complete,
1576 /* This should be done at BUS resume time, but an
1577 * existing problem prevents us from ever getting a bus
1578 * resume... This hack would fail to work should we
1579 * ever have a bus that contains NO devices, since we
1580 * would never even get here in that case. */
1581 fix_vbus_dev_info(dev);
1585 dev->resuming = true;
1586 x = drv->resume(dev, resume_state_change_complete,
1591 dev->pausing = false;
1593 dev->resuming = false;
1600 (*notify_func)(dev_info, rc);
1605 chipset_device_pause(struct visorchipset_device_info *dev_info)
1607 initiate_chipset_device_pause_resume(dev_info, true);
1611 chipset_device_resume(struct visorchipset_device_info *dev_info)
1613 initiate_chipset_device_pause_resume(dev_info, false);
1616 struct channel_size_info {
1618 unsigned long min_size;
1619 unsigned long max_size;
1627 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1628 bus_device_info_init(&clientbus_driverinfo,
1629 "clientbus", "visorbus",
1632 rc = create_bus_type();
1634 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1638 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1639 if (!periodic_dev_workqueue) {
1640 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
1645 /* This enables us to receive notifications when devices appear for
1646 * which this service partition is to be a server for.
1648 visorchipset_register_busdev(&chipset_notifiers,
1649 &chipset_responders,
1650 &chipset_driverinfo);
1656 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
1657 POSTCODE_SEVERITY_ERR);
1664 struct list_head *listentry, *listtmp;
1666 visorchipset_register_busdev(NULL, NULL, NULL);
1667 remove_all_visor_devices();
1669 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1670 destroy_workqueue(periodic_dev_workqueue);
1671 periodic_dev_workqueue = NULL;
1673 if (periodic_test_workqueue) {
1674 cancel_delayed_work(&periodic_work);
1675 flush_workqueue(periodic_test_workqueue);
1676 destroy_workqueue(periodic_test_workqueue);
1677 periodic_test_workqueue = NULL;
1680 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1681 struct visor_device *dev = list_entry(listentry,
1685 remove_bus_instance(dev);
1690 module_param_named(debug, visorbus_debug, int, S_IRUGO);
1691 MODULE_PARM_DESC(visorbus_debug, "1 to debug");
1693 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1694 MODULE_PARM_DESC(visorbus_forcematch,
1695 "1 to force a successful dev <--> drv match");
1697 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1698 MODULE_PARM_DESC(visorbus_forcenomatch,
1699 "1 to force an UNsuccessful dev <--> drv match");
1701 module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1702 MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");
1704 MODULE_AUTHOR("Unisys");
1705 MODULE_LICENSE("GPL");
1706 MODULE_DESCRIPTION("Supervisor bus driver for service partition: ver " VERSION);
1707 MODULE_VERSION(VERSION);