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[] = {
73 /** This describes the TYPE of bus.
74 * (Don't confuse this with an INSTANCE of the bus.)
76 struct bus_type visorbus_type = {
78 .match = visorbus_match,
79 .uevent = visorbus_uevent,
80 .bus_groups = visorbus_bus_groups,
83 static struct delayed_work periodic_work;
85 /* YES, we need 2 workqueues.
86 * The reason is, workitems on the test queue may need to cancel
87 * workitems on the other queue. You will be in for trouble if you try to
88 * do this with workitems queued on the same workqueue.
90 static struct workqueue_struct *periodic_test_workqueue;
91 static struct workqueue_struct *periodic_dev_workqueue;
92 static long long bus_count; /** number of bus instances */
93 /** ever-increasing */
95 static void chipset_bus_create(struct visor_device *bus_info);
96 static void chipset_bus_destroy(struct visor_device *bus_info);
97 static void chipset_device_create(struct visor_device *dev_info);
98 static void chipset_device_destroy(struct visor_device *dev_info);
99 static void chipset_device_pause(struct visor_device *dev_info);
100 static void chipset_device_resume(struct visor_device *dev_info);
102 /** These functions are implemented herein, and are called by the chipset
103 * driver to notify us about specific events.
105 static struct visorchipset_busdev_notifiers chipset_notifiers = {
106 .bus_create = chipset_bus_create,
107 .bus_destroy = chipset_bus_destroy,
108 .device_create = chipset_device_create,
109 .device_destroy = chipset_device_destroy,
110 .device_pause = chipset_device_pause,
111 .device_resume = chipset_device_resume,
114 /** These functions are implemented in the chipset driver, and we call them
115 * herein when we want to acknowledge a specific event.
117 static struct visorchipset_busdev_responders chipset_responders;
119 /* filled in with info about parent chipset driver when we register with it */
120 static struct ultra_vbus_deviceinfo chipset_driverinfo;
121 /* filled in with info about this driver, wrt it servicing client busses */
122 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
124 /** list of visor_device structs, linked via .list_all */
125 static LIST_HEAD(list_all_bus_instances);
126 /** list of visor_device structs, linked via .list_all */
127 static LIST_HEAD(list_all_device_instances);
130 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
132 if (add_uevent_var(env, "VERSION=%s", VERSION))
137 /* This is called automatically upon adding a visor_device (device_add), or
138 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
139 * provided driver can control the specified device.
142 visorbus_match(struct device *xdev, struct device_driver *xdrv)
144 uuid_le channel_type;
147 struct visor_device *dev;
148 struct visor_driver *drv;
150 dev = to_visor_device(xdev);
151 drv = to_visor_driver(xdrv);
152 channel_type = visorchannel_get_uuid(dev->visorchannel);
153 if (visorbus_forcematch) {
157 if (visorbus_forcenomatch)
160 if (!drv->channel_types)
163 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
164 (drv->channel_types[i].name);
166 if (uuid_le_cmp(drv->channel_types[i].guid,
167 channel_type) == 0) {
175 /** This is called when device_unregister() is called for the bus device
176 * instance, after all other tasks involved with destroying the device
180 visorbus_release_busdevice(struct device *xdev)
182 struct visor_device *dev = dev_get_drvdata(xdev);
184 dev_set_drvdata(xdev, NULL);
188 /** This is called when device_unregister() is called for each child
192 visorbus_release_device(struct device *xdev)
194 struct visor_device *dev = to_visor_device(xdev);
196 if (dev->periodic_work) {
197 visor_periodic_work_destroy(dev->periodic_work);
198 dev->periodic_work = NULL;
200 if (dev->visorchannel) {
201 visorchannel_destroy(dev->visorchannel);
202 dev->visorchannel = NULL;
207 /* Implement publishing of device node attributes under:
209 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
213 #define to_devmajorminor_attr(_attr) \
214 container_of(_attr, struct devmajorminor_attribute, attr)
215 #define to_visor_device_from_kobjdevmajorminor(obj) \
216 container_of(obj, struct visor_device, kobjdevmajorminor)
218 struct devmajorminor_attribute {
219 struct attribute attr;
221 ssize_t (*show)(struct visor_device *, int slot, char *buf);
222 ssize_t (*store)(struct visor_device *, int slot, const char *buf,
226 static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
228 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
230 if (slot < 0 || slot >= maxdevnodes)
232 return snprintf(buf, PAGE_SIZE, "%d:%d\n",
233 dev->devnodes[slot].major, dev->devnodes[slot].minor);
237 devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
239 struct devmajorminor_attribute *devmajorminor_attr =
240 to_devmajorminor_attr(attr);
241 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
244 if (devmajorminor_attr->show)
245 ret = devmajorminor_attr->show(dev,
246 devmajorminor_attr->slot, buf);
251 devmajorminor_attr_store(struct kobject *kobj,
252 struct attribute *attr, const char *buf, size_t count)
254 struct devmajorminor_attribute *devmajorminor_attr =
255 to_devmajorminor_attr(attr);
256 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
259 if (devmajorminor_attr->store)
260 ret = devmajorminor_attr->store(dev,
261 devmajorminor_attr->slot,
266 static int register_devmajorminor_attributes(struct visor_device *dev);
269 devmajorminor_create_file(struct visor_device *dev, const char *name,
270 int major, int minor)
272 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
273 struct devmajorminor_attribute *myattr = NULL;
274 int x = -1, rc = 0, slot = -1;
276 register_devmajorminor_attributes(dev);
277 for (slot = 0; slot < maxdevnodes; slot++)
278 if (!dev->devnodes[slot].attr)
280 if (slot == maxdevnodes) {
284 myattr = kmalloc(sizeof(*myattr), GFP_KERNEL);
289 memset(myattr, 0, sizeof(struct devmajorminor_attribute));
290 myattr->show = DEVMAJORMINOR_ATTR;
291 myattr->store = NULL;
293 myattr->attr.name = name;
294 myattr->attr.mode = S_IRUGO;
295 dev->devnodes[slot].attr = myattr;
296 dev->devnodes[slot].major = major;
297 dev->devnodes[slot].minor = minor;
298 x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
303 kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
308 dev->devnodes[slot].attr = NULL;
314 devmajorminor_remove_file(struct visor_device *dev, int slot)
316 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
317 struct devmajorminor_attribute *myattr = NULL;
319 if (slot < 0 || slot >= maxdevnodes)
321 myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
324 sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
325 kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
326 dev->devnodes[slot].attr = NULL;
331 devmajorminor_remove_all_files(struct visor_device *dev)
334 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
336 for (i = 0; i < maxdevnodes; i++)
337 devmajorminor_remove_file(dev, i);
340 static const struct sysfs_ops devmajorminor_sysfs_ops = {
341 .show = devmajorminor_attr_show,
342 .store = devmajorminor_attr_store,
345 static struct kobj_type devmajorminor_kobj_type = {
346 .sysfs_ops = &devmajorminor_sysfs_ops
350 register_devmajorminor_attributes(struct visor_device *dev)
354 if (dev->kobjdevmajorminor.parent)
355 goto away; /* already registered */
356 x = kobject_init_and_add(&dev->kobjdevmajorminor,
357 &devmajorminor_kobj_type, &dev->device.kobj,
364 kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
371 unregister_devmajorminor_attributes(struct visor_device *dev)
373 if (!dev->kobjdevmajorminor.parent)
374 return; /* already unregistered */
375 devmajorminor_remove_all_files(dev);
377 kobject_del(&dev->kobjdevmajorminor);
378 kobject_put(&dev->kobjdevmajorminor);
379 dev->kobjdevmajorminor.parent = NULL;
382 /* begin implementation of specific channel attributes to appear under
383 * /sys/bus/visorbus<x>/dev<y>/channel
385 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
388 struct visor_device *vdev = to_visor_device(dev);
390 if (!vdev->visorchannel)
392 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
393 visorchannel_get_physaddr(vdev->visorchannel));
396 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
399 struct visor_device *vdev = to_visor_device(dev);
401 if (!vdev->visorchannel)
403 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
404 visorchannel_get_nbytes(vdev->visorchannel));
407 static ssize_t clientpartition_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
410 struct visor_device *vdev = to_visor_device(dev);
412 if (!vdev->visorchannel)
414 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
415 visorchannel_get_clientpartition(vdev->visorchannel));
418 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
421 struct visor_device *vdev = to_visor_device(dev);
424 if (!vdev->visorchannel)
426 return snprintf(buf, PAGE_SIZE, "%s\n",
427 visorchannel_id(vdev->visorchannel, s));
430 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
433 struct visor_device *vdev = to_visor_device(dev);
436 if (!vdev->visorchannel)
438 return snprintf(buf, PAGE_SIZE, "%s\n",
439 visorchannel_zoneid(vdev->visorchannel, s));
442 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
445 struct visor_device *vdev = to_visor_device(dev);
447 struct bus_type *xbus = dev->bus;
448 struct device_driver *xdrv = dev->driver;
449 struct visor_driver *drv = NULL;
451 if (!vdev->visorchannel || !xbus || !xdrv)
453 i = xbus->match(dev, xdrv);
456 drv = to_visor_driver(xdrv);
457 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
460 static DEVICE_ATTR_RO(physaddr);
461 static DEVICE_ATTR_RO(nbytes);
462 static DEVICE_ATTR_RO(clientpartition);
463 static DEVICE_ATTR_RO(typeguid);
464 static DEVICE_ATTR_RO(zoneguid);
465 static DEVICE_ATTR_RO(typename);
467 static struct attribute *channel_attrs[] = {
468 &dev_attr_physaddr.attr,
469 &dev_attr_nbytes.attr,
470 &dev_attr_clientpartition.attr,
471 &dev_attr_typeguid.attr,
472 &dev_attr_zoneguid.attr,
473 &dev_attr_typename.attr,
477 static struct attribute_group channel_attr_grp = {
479 .attrs = channel_attrs,
482 static const struct attribute_group *visorbus_dev_groups[] = {
487 /* end implementation of specific channel attributes */
489 /* BUS instance attributes
491 * define & implement display of bus attributes under
492 * /sys/bus/visorbus/busses/visorbus<n>.
494 * This is a bit hoaky because the kernel does not yet have the infrastructure
495 * to separate bus INSTANCE attributes from bus TYPE attributes...
496 * so we roll our own. See businst.c / businst.h.
500 static ssize_t partition_handle_show(struct device *dev,
501 struct device_attribute *attr,
503 struct visor_device *vdev = to_visor_device(dev);
504 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
506 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", handle);
509 static ssize_t partition_guid_show(struct device *dev,
510 struct device_attribute *attr,
512 struct visor_device *vdev = to_visor_device(dev);
514 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
517 static ssize_t partition_name_show(struct device *dev,
518 struct device_attribute *attr,
520 struct visor_device *vdev = to_visor_device(dev);
522 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
525 static ssize_t channel_addr_show(struct device *dev,
526 struct device_attribute *attr,
528 struct visor_device *vdev = to_visor_device(dev);
529 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
531 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", addr);
534 static ssize_t channel_bytes_show(struct device *dev,
535 struct device_attribute *attr,
537 struct visor_device *vdev = to_visor_device(dev);
538 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
540 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", nbytes);
543 static ssize_t channel_id_show(struct device *dev,
544 struct device_attribute *attr,
546 struct visor_device *vdev = to_visor_device(dev);
549 if (vdev->visorchannel) {
550 visorchannel_id(vdev->visorchannel, buf);
557 static ssize_t client_bus_info_show(struct device *dev,
558 struct device_attribute *attr,
560 struct visor_device *vdev = to_visor_device(dev);
561 struct visorchannel *channel = vdev->visorchannel;
563 int i, x, remain = PAGE_SIZE;
567 struct ultra_vbus_deviceinfo dev_info;
572 partition_name = vdev->name;
573 x = snprintf(p, remain,
574 "Client device / client driver info for %s partition (vbus #%d):\n",
575 partition_name, vdev->chipset_dev_no);
578 x = visorchannel_read(channel,
580 spar_vbus_channel_protocol,
582 &dev_info, sizeof(dev_info));
584 x = vbuschannel_devinfo_to_string(&dev_info, p,
589 x = visorchannel_read(channel,
591 spar_vbus_channel_protocol,
593 &dev_info, sizeof(dev_info));
595 x = vbuschannel_devinfo_to_string(&dev_info, p,
600 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
602 while (off + sizeof(dev_info) <=
603 visorchannel_get_nbytes(channel)) {
604 x = visorchannel_read(channel,
605 off, &dev_info, sizeof(dev_info));
607 x = vbuschannel_devinfo_to_string
608 (&dev_info, p, remain, i);
612 off += sizeof(dev_info);
616 return PAGE_SIZE - remain;
619 static DEVICE_ATTR_RO(partition_handle);
620 static DEVICE_ATTR_RO(partition_guid);
621 static DEVICE_ATTR_RO(partition_name);
622 static DEVICE_ATTR_RO(channel_addr);
623 static DEVICE_ATTR_RO(channel_bytes);
624 static DEVICE_ATTR_RO(channel_id);
625 static DEVICE_ATTR_RO(client_bus_info);
627 static struct attribute *dev_attrs[] = {
628 &dev_attr_partition_handle.attr,
629 &dev_attr_partition_guid.attr,
630 &dev_attr_partition_name.attr,
631 &dev_attr_channel_addr.attr,
632 &dev_attr_channel_bytes.attr,
633 &dev_attr_channel_id.attr,
634 &dev_attr_client_bus_info.attr,
638 static struct attribute_group dev_attr_grp = {
642 static const struct attribute_group *visorbus_groups[] = {
649 * define & implement display of driver attributes under
650 * /sys/bus/visorbus/drivers/<drivername>.
655 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
657 struct visor_driver *drv = to_visor_driver(xdrv);
659 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
663 register_driver_attributes(struct visor_driver *drv)
666 struct driver_attribute version =
667 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
668 drv->version_attr = version;
669 rc = driver_create_file(&drv->driver, &drv->version_attr);
674 unregister_driver_attributes(struct visor_driver *drv)
676 driver_remove_file(&drv->driver, &drv->version_attr);
680 dev_periodic_work(void *xdev)
682 struct visor_device *dev = (struct visor_device *)xdev;
683 struct visor_driver *drv = to_visor_driver(dev->device.driver);
685 down(&dev->visordriver_callback_lock);
686 if (drv->channel_interrupt)
687 drv->channel_interrupt(dev);
688 up(&dev->visordriver_callback_lock);
689 if (!visor_periodic_work_nextperiod(dev->periodic_work))
690 put_device(&dev->device);
694 dev_start_periodic_work(struct visor_device *dev)
696 if (dev->being_removed)
698 /* now up by at least 2 */
699 get_device(&dev->device);
700 if (!visor_periodic_work_start(dev->periodic_work))
701 put_device(&dev->device);
705 dev_stop_periodic_work(struct visor_device *dev)
707 if (visor_periodic_work_stop(dev->periodic_work))
708 put_device(&dev->device);
711 /** This is called automatically upon adding a visor_device (device_add), or
712 * adding a visor_driver (visorbus_register_visor_driver), but only after
713 * visorbus_match has returned 1 to indicate a successful match between
717 visordriver_probe_device(struct device *xdev)
720 struct visor_driver *drv;
721 struct visor_device *dev;
723 drv = to_visor_driver(xdev->driver);
724 dev = to_visor_device(xdev);
725 down(&dev->visordriver_callback_lock);
726 dev->being_removed = false;
728 * ensure that the dev->being_removed flag is cleared before
732 get_device(&dev->device);
734 up(&dev->visordriver_callback_lock);
738 rc = drv->probe(dev);
742 fix_vbus_dev_info(dev);
743 up(&dev->visordriver_callback_lock);
747 put_device(&dev->device);
751 /** This is called when device_unregister() is called for each child device
752 * instance, to notify the appropriate visorbus_driver that the device is
753 * going away, and to decrease the reference count of the device.
756 visordriver_remove_device(struct device *xdev)
758 struct visor_device *dev;
759 struct visor_driver *drv;
761 dev = to_visor_device(xdev);
762 drv = to_visor_driver(xdev->driver);
763 down(&dev->visordriver_callback_lock);
764 dev->being_removed = true;
766 * ensure that the dev->being_removed flag is set before we start the
774 up(&dev->visordriver_callback_lock);
775 dev_stop_periodic_work(dev);
776 devmajorminor_remove_all_files(dev);
778 put_device(&dev->device);
783 /** A particular type of visor driver calls this function to register
784 * the driver. The caller MUST fill in the following fields within the
786 * name, version, owner, channel_types, probe, remove
788 * Here's how the whole Linux bus / driver / device model works.
790 * At system start-up, the visorbus kernel module is loaded, which registers
791 * visorbus_type as a bus type, using bus_register().
793 * All kernel modules that support particular device types on a
794 * visorbus bus are loaded. Each of these kernel modules calls
795 * visorbus_register_visor_driver() in their init functions, passing a
796 * visor_driver struct. visorbus_register_visor_driver() in turn calls
797 * register_driver(&visor_driver.driver). This .driver member is
798 * initialized with generic methods (like probe), whose sole responsibility
799 * is to act as a broker for the real methods, which are within the
800 * visor_driver struct. (This is the way the subclass behavior is
801 * implemented, since visor_driver is essentially a subclass of the
802 * generic driver.) Whenever a driver_register() happens, core bus code in
803 * the kernel does (see device_attach() in drivers/base/dd.c):
805 * for each dev associated with the bus (the bus that driver is on) that
806 * does not yet have a driver
807 * if bus.match(dev,newdriver) == yes_matched ** .match specified
808 * ** during bus_register().
809 * newdriver.probe(dev) ** for visor drivers, this will call
810 * ** the generic driver.probe implemented in visorbus.c,
811 * ** which in turn calls the probe specified within the
812 * ** struct visor_driver (which was specified by the
813 * ** actual device driver as part of
814 * ** visorbus_register_visor_driver()).
816 * The above dance also happens when a new device appears.
817 * So the question is, how are devices created within the system?
818 * Basically, just call device_add(dev). See pci_bus_add_devices().
819 * pci_scan_device() shows an example of how to build a device struct. It
820 * returns the newly-created struct to pci_scan_single_device(), who adds it
821 * to the list of devices at PCIBUS.devices. That list of devices is what
822 * is traversed by pci_bus_add_devices().
825 int visorbus_register_visor_driver(struct visor_driver *drv)
829 drv->driver.name = drv->name;
830 drv->driver.bus = &visorbus_type;
831 drv->driver.probe = visordriver_probe_device;
832 drv->driver.remove = visordriver_remove_device;
833 drv->driver.owner = drv->owner;
835 /* driver_register does this:
836 * bus_add_driver(drv)
837 * ->if (drv.bus) ** (bus_type) **
839 * for each dev with bus type of drv.bus
840 * if (!dev.drv) ** no driver assigned yet **
841 * if (bus.match(dev,drv)) [visorbus_match]
843 * if (!drv.probe(dev)) [visordriver_probe_device]
847 rc = driver_register(&drv->driver);
850 rc = register_driver_attributes(drv);
853 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
855 /** A particular type of visor driver calls this function to unregister
856 * the driver, i.e., within its module_exit function.
859 visorbus_unregister_visor_driver(struct visor_driver *drv)
861 unregister_driver_attributes(drv);
862 driver_unregister(&drv->driver);
864 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
867 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
868 void *dest, unsigned long nbytes)
870 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
872 EXPORT_SYMBOL_GPL(visorbus_read_channel);
875 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
876 void *src, unsigned long nbytes)
878 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
880 EXPORT_SYMBOL_GPL(visorbus_write_channel);
883 visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
884 unsigned long nbytes)
886 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
888 EXPORT_SYMBOL_GPL(visorbus_clear_channel);
891 visorbus_registerdevnode(struct visor_device *dev,
892 const char *name, int major, int minor)
894 return devmajorminor_create_file(dev, name, major, minor);
896 EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
898 /** We don't really have a real interrupt, so for now we just call the
899 * interrupt function periodically...
902 visorbus_enable_channel_interrupts(struct visor_device *dev)
904 dev_start_periodic_work(dev);
906 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
909 visorbus_disable_channel_interrupts(struct visor_device *dev)
911 dev_stop_periodic_work(dev);
913 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
915 /** This is how everything starts from the device end.
916 * This function is called when a channel first appears via a ControlVM
917 * message. In response, this function allocates a visor_device to
918 * correspond to the new channel, and attempts to connect it the appropriate
919 * driver. If the appropriate driver is found, the visor_driver.probe()
920 * function for that driver will be called, and will be passed the new
921 * visor_device that we just created.
923 * It's ok if the appropriate driver is not yet loaded, because in that case
924 * the new device struct will just stick around in the bus' list of devices.
925 * When the appropriate driver calls visorbus_register_visor_driver(), the
926 * visor_driver.probe() for the new driver will be called with the new
930 create_visor_device(struct visor_device *dev)
933 u32 chipset_bus_no = dev->chipset_bus_no;
934 u32 chipset_dev_no = dev->chipset_dev_no;
936 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
937 POSTCODE_SEVERITY_INFO);
939 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
940 dev->device.bus = &visorbus_type;
941 dev->device.groups = visorbus_dev_groups;
942 device_initialize(&dev->device);
943 dev->device.release = visorbus_release_device;
944 /* keep a reference just for us (now 2) */
945 get_device(&dev->device);
947 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
948 periodic_dev_workqueue,
950 dev, dev_name(&dev->device));
951 if (!dev->periodic_work) {
952 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
957 /* bus_id must be a unique name with respect to this bus TYPE
958 * (NOT bus instance). That's why we need to include the bus
959 * number within the name.
961 dev_set_name(&dev->device, "vbus%u:dev%u",
962 chipset_bus_no, chipset_dev_no);
964 /* device_add does this:
965 * bus_add_device(dev)
966 * ->device_attach(dev)
967 * ->for each driver drv registered on the bus that dev is on
968 * if (dev.drv) ** device already has a driver **
969 * ** not sure we could ever get here... **
971 * if (bus.match(dev,drv)) [visorbus_match]
973 * if (!drv.probe(dev)) [visordriver_probe_device]
976 * Note that device_add does NOT fail if no driver failed to
977 * claim the device. The device will be linked onto
978 * bus_type.klist_devices regardless (use bus_for_each_dev).
980 rc = device_add(&dev->device);
982 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
987 rc = register_devmajorminor_attributes(dev);
989 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
994 list_add_tail(&dev->list_all, &list_all_device_instances);
998 device_unregister(&dev->device);
1000 put_device(&dev->device);
1005 remove_visor_device(struct visor_device *dev)
1007 list_del(&dev->list_all);
1008 unregister_devmajorminor_attributes(dev);
1009 put_device(&dev->device);
1010 device_unregister(&dev->device);
1014 get_vbus_header_info(struct visorchannel *chan,
1015 struct spar_vbus_headerinfo *hdr_info)
1019 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
1021 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1022 sizeof(*hdr_info)) < 0) {
1025 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
1027 if (hdr_info->device_info_struct_bytes <
1028 sizeof(struct ultra_vbus_deviceinfo)) {
1036 /* Write the contents of <info> to the struct
1037 * spar_vbus_channel_protocol.chp_info. */
1040 write_vbus_chp_info(struct visorchannel *chan,
1041 struct spar_vbus_headerinfo *hdr_info,
1042 struct ultra_vbus_deviceinfo *info)
1044 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1046 if (hdr_info->chp_info_offset == 0)
1049 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1054 /* Write the contents of <info> to the struct
1055 * spar_vbus_channel_protocol.bus_info. */
1058 write_vbus_bus_info(struct visorchannel *chan,
1059 struct spar_vbus_headerinfo *hdr_info,
1060 struct ultra_vbus_deviceinfo *info)
1062 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1064 if (hdr_info->bus_info_offset == 0)
1067 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1072 /* Write the contents of <info> to the
1073 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1076 write_vbus_dev_info(struct visorchannel *chan,
1077 struct spar_vbus_headerinfo *hdr_info,
1078 struct ultra_vbus_deviceinfo *info, int devix)
1081 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1082 (hdr_info->device_info_struct_bytes * devix);
1084 if (hdr_info->dev_info_offset == 0)
1087 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1092 /* For a child device just created on a client bus, fill in
1093 * information about the driver that is controlling this device into
1094 * the the appropriate slot within the vbus channel of the bus
1098 fix_vbus_dev_info(struct visor_device *visordev)
1101 struct visor_device *bdev;
1102 struct visor_driver *visordrv;
1103 int bus_no = visordev->chipset_bus_no;
1104 int dev_no = visordev->chipset_dev_no;
1105 struct ultra_vbus_deviceinfo dev_info;
1106 const char *chan_type_name = NULL;
1107 struct spar_vbus_headerinfo *hdr_info;
1109 if (!visordev->device.driver)
1112 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
1116 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
1120 visordrv = to_visor_driver(visordev->device.driver);
1122 /* Within the list of device types (by GUID) that the driver
1123 * says it supports, find out which one of those types matches
1124 * the type of this device, so that we can include the device
1127 for (i = 0; visordrv->channel_types[i].name; i++) {
1128 if (memcmp(&visordrv->channel_types[i].guid,
1129 &visordev->channel_type_guid,
1130 sizeof(visordrv->channel_types[i].guid)) == 0) {
1131 chan_type_name = visordrv->channel_types[i].name;
1136 bus_device_info_init(&dev_info, chan_type_name,
1137 visordrv->name, visordrv->version,
1139 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
1141 /* Re-write bus+chipset info, because it is possible that this
1142 * was previously written by our evil counterpart, virtpci.
1144 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
1145 write_vbus_bus_info(bdev->visorchannel, hdr_info,
1146 &clientbus_driverinfo);
1149 /** Create a device instance for the visor bus itself.
1152 create_bus_instance(struct visor_device *dev)
1155 int id = dev->chipset_bus_no;
1156 struct spar_vbus_headerinfo *hdr_info;
1158 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1160 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1166 dev_set_name(&dev->device, "visorbus%d", id);
1167 dev->device.bus = &visorbus_type;
1168 dev->device.groups = visorbus_groups;
1169 dev->device.release = visorbus_release_busdevice;
1171 if (device_register(&dev->device) < 0) {
1172 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1173 POSTCODE_SEVERITY_ERR);
1178 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1179 dev->vbus_hdr_info = (void *)hdr_info;
1180 write_vbus_chp_info(dev->visorchannel, hdr_info,
1181 &chipset_driverinfo);
1182 write_vbus_bus_info(dev->visorchannel, hdr_info,
1183 &clientbus_driverinfo);
1188 list_add_tail(&dev->list_all, &list_all_bus_instances);
1189 dev_set_drvdata(&dev->device, dev);
1198 /** Remove a device instance for the visor bus itself.
1201 remove_bus_instance(struct visor_device *dev)
1203 /* Note that this will result in the release method for
1204 * dev->dev being called, which will call
1205 * visorbus_release_busdevice(). This has something to do with
1206 * the put_device() done in device_unregister(), but I have never
1207 * successfully been able to trace thru the code to see where/how
1208 * release() gets called. But I know it does.
1211 if (dev->visorchannel) {
1212 visorchannel_destroy(dev->visorchannel);
1213 dev->visorchannel = NULL;
1215 kfree(dev->vbus_hdr_info);
1216 list_del(&dev->list_all);
1217 device_unregister(&dev->device);
1220 /** Create and register the one-and-only one instance of
1221 * the visor bus type (visorbus_type).
1224 create_bus_type(void)
1228 rc = bus_register(&visorbus_type);
1232 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1235 remove_bus_type(void)
1237 bus_unregister(&visorbus_type);
1240 /** Remove all child visor bus device instances.
1243 remove_all_visor_devices(void)
1245 struct list_head *listentry, *listtmp;
1247 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1248 struct visor_device *dev = list_entry(listentry,
1249 struct visor_device,
1251 remove_visor_device(dev);
1256 chipset_bus_create(struct visor_device *dev)
1259 u32 bus_no = dev->chipset_bus_no;
1261 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1262 rc = create_bus_instance(dev);
1263 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1266 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1267 POSTCODE_SEVERITY_ERR);
1269 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1270 POSTCODE_SEVERITY_INFO);
1272 if (chipset_responders.bus_create)
1273 (*chipset_responders.bus_create) (dev, rc);
1277 chipset_bus_destroy(struct visor_device *dev)
1279 remove_bus_instance(dev);
1280 if (chipset_responders.bus_destroy)
1281 (*chipset_responders.bus_destroy)(dev, 0);
1285 chipset_device_create(struct visor_device *dev_info)
1288 u32 bus_no = dev_info->chipset_bus_no;
1289 u32 dev_no = dev_info->chipset_dev_no;
1291 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1292 POSTCODE_SEVERITY_INFO);
1294 rc = create_visor_device(dev_info);
1295 if (chipset_responders.device_create)
1296 chipset_responders.device_create(dev_info, rc);
1299 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1300 POSTCODE_SEVERITY_ERR);
1302 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1303 POSTCODE_SEVERITY_INFO);
1307 chipset_device_destroy(struct visor_device *dev_info)
1309 remove_visor_device(dev_info);
1311 if (chipset_responders.device_destroy)
1312 (*chipset_responders.device_destroy) (dev_info, 0);
1315 /* This is the callback function specified for a function driver, to
1316 * be called when a pending "pause device" operation has been
1320 pause_state_change_complete(struct visor_device *dev, int status)
1325 dev->pausing = false;
1326 if (!chipset_responders.device_pause) /* this can never happen! */
1329 /* Notify the chipset driver that the pause is complete, which
1330 * will presumably want to send some sort of response to the
1332 (*chipset_responders.device_pause) (dev, status);
1335 /* This is the callback function specified for a function driver, to
1336 * be called when a pending "resume device" operation has been
1340 resume_state_change_complete(struct visor_device *dev, int status)
1345 dev->resuming = false;
1346 if (!chipset_responders.device_resume) /* this can never happen! */
1349 /* Notify the chipset driver that the resume is complete,
1350 * which will presumably want to send some sort of response to
1352 (*chipset_responders.device_resume) (dev, status);
1355 /* Tell the subordinate function driver for a specific device to pause
1356 * or resume that device. Result is returned asynchronously via a
1357 * callback function.
1360 initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
1363 struct visor_driver *drv = NULL;
1364 void (*notify_func)(struct visor_device *dev, int response) = NULL;
1367 notify_func = chipset_responders.device_pause;
1369 notify_func = chipset_responders.device_resume;
1373 drv = to_visor_driver(dev->device.driver);
1377 if (dev->pausing || dev->resuming)
1380 /* Note that even though both drv->pause() and drv->resume
1381 * specify a callback function, it is NOT necessary for us to
1382 * increment our local module usage count. Reason is, there
1383 * is already a linkage dependency between child function
1384 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1385 * visorbus while child function drivers are still running.
1391 dev->pausing = true;
1392 x = drv->pause(dev, pause_state_change_complete);
1394 /* This should be done at BUS resume time, but an
1395 * existing problem prevents us from ever getting a bus
1396 * resume... This hack would fail to work should we
1397 * ever have a bus that contains NO devices, since we
1398 * would never even get here in that case. */
1399 fix_vbus_dev_info(dev);
1403 dev->resuming = true;
1404 x = drv->resume(dev, resume_state_change_complete);
1408 dev->pausing = false;
1410 dev->resuming = false;
1417 (*notify_func)(dev, rc);
1422 chipset_device_pause(struct visor_device *dev_info)
1424 initiate_chipset_device_pause_resume(dev_info, true);
1428 chipset_device_resume(struct visor_device *dev_info)
1430 initiate_chipset_device_pause_resume(dev_info, false);
1433 struct channel_size_info {
1435 unsigned long min_size;
1436 unsigned long max_size;
1444 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1445 bus_device_info_init(&clientbus_driverinfo,
1446 "clientbus", "visorbus",
1449 rc = create_bus_type();
1451 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1455 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1456 if (!periodic_dev_workqueue) {
1457 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
1462 /* This enables us to receive notifications when devices appear for
1463 * which this service partition is to be a server for.
1465 visorchipset_register_busdev(&chipset_notifiers,
1466 &chipset_responders,
1467 &chipset_driverinfo);
1473 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
1474 POSTCODE_SEVERITY_ERR);
1481 struct list_head *listentry, *listtmp;
1483 visorchipset_register_busdev(NULL, NULL, NULL);
1484 remove_all_visor_devices();
1486 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1487 destroy_workqueue(periodic_dev_workqueue);
1488 periodic_dev_workqueue = NULL;
1490 if (periodic_test_workqueue) {
1491 cancel_delayed_work(&periodic_work);
1492 flush_workqueue(periodic_test_workqueue);
1493 destroy_workqueue(periodic_test_workqueue);
1494 periodic_test_workqueue = NULL;
1497 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1498 struct visor_device *dev = list_entry(listentry,
1502 remove_bus_instance(dev);
1507 module_param_named(debug, visorbus_debug, int, S_IRUGO);
1508 MODULE_PARM_DESC(visorbus_debug, "1 to debug");
1510 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1511 MODULE_PARM_DESC(visorbus_forcematch,
1512 "1 to force a successful dev <--> drv match");
1514 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1515 MODULE_PARM_DESC(visorbus_forcenomatch,
1516 "1 to force an UNsuccessful dev <--> drv match");
1518 module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1519 MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");