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 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
41 static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
42 static void fix_vbus_dev_info(struct visor_device *visordev);
44 /* BUS type attributes
46 * define & implement display of bus attributes under
51 static ssize_t version_show(struct bus_type *bus, char *buf)
53 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
56 static BUS_ATTR_RO(version);
58 static struct attribute *visorbus_bus_attrs[] = {
59 &bus_attr_version.attr,
63 static const struct attribute_group visorbus_bus_group = {
64 .attrs = visorbus_bus_attrs,
67 static const struct attribute_group *visorbus_bus_groups[] = {
73 * DEVICE type attributes
75 * The modalias file will contain the guid of the device.
77 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
80 struct visor_device *vdev;
83 vdev = to_visor_device(dev);
84 guid = visorchannel_get_uuid(vdev->visorchannel);
85 return snprintf(buf, PAGE_SIZE, "visorbus:%pUl\n", &guid);
87 static DEVICE_ATTR_RO(modalias);
89 static struct attribute *visorbus_dev_attrs[] = {
90 &dev_attr_modalias.attr,
94 /* sysfs example for bridge-only sysfs files using device_type's */
95 static const struct attribute_group visorbus_dev_group = {
96 .attrs = visorbus_dev_attrs,
99 static const struct attribute_group *visorbus_dev_groups[] = {
104 /** This describes the TYPE of bus.
105 * (Don't confuse this with an INSTANCE of the bus.)
107 struct bus_type visorbus_type = {
109 .match = visorbus_match,
110 .uevent = visorbus_uevent,
111 .dev_groups = visorbus_dev_groups,
112 .bus_groups = visorbus_bus_groups,
115 static struct delayed_work periodic_work;
117 /* YES, we need 2 workqueues.
118 * The reason is, workitems on the test queue may need to cancel
119 * workitems on the other queue. You will be in for trouble if you try to
120 * do this with workitems queued on the same workqueue.
122 static struct workqueue_struct *periodic_test_workqueue;
123 static struct workqueue_struct *periodic_dev_workqueue;
124 static long long bus_count; /** number of bus instances */
125 /** ever-increasing */
127 static void chipset_bus_create(struct visor_device *bus_info);
128 static void chipset_bus_destroy(struct visor_device *bus_info);
129 static void chipset_device_create(struct visor_device *dev_info);
130 static void chipset_device_destroy(struct visor_device *dev_info);
131 static void chipset_device_pause(struct visor_device *dev_info);
132 static void chipset_device_resume(struct visor_device *dev_info);
134 /** These functions are implemented herein, and are called by the chipset
135 * driver to notify us about specific events.
137 static struct visorchipset_busdev_notifiers chipset_notifiers = {
138 .bus_create = chipset_bus_create,
139 .bus_destroy = chipset_bus_destroy,
140 .device_create = chipset_device_create,
141 .device_destroy = chipset_device_destroy,
142 .device_pause = chipset_device_pause,
143 .device_resume = chipset_device_resume,
146 /** These functions are implemented in the chipset driver, and we call them
147 * herein when we want to acknowledge a specific event.
149 static struct visorchipset_busdev_responders chipset_responders;
151 /* filled in with info about parent chipset driver when we register with it */
152 static struct ultra_vbus_deviceinfo chipset_driverinfo;
153 /* filled in with info about this driver, wrt it servicing client busses */
154 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
156 /** list of visor_device structs, linked via .list_all */
157 static LIST_HEAD(list_all_bus_instances);
158 /** list of visor_device structs, linked via .list_all */
159 static LIST_HEAD(list_all_device_instances);
162 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
164 struct visor_device *dev;
167 dev = to_visor_device(xdev);
168 guid = visorchannel_get_uuid(dev->visorchannel);
170 if (add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid))
175 /* This is called automatically upon adding a visor_device (device_add), or
176 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
177 * provided driver can control the specified device.
180 visorbus_match(struct device *xdev, struct device_driver *xdrv)
182 uuid_le channel_type;
185 struct visor_device *dev;
186 struct visor_driver *drv;
188 dev = to_visor_device(xdev);
189 drv = to_visor_driver(xdrv);
190 channel_type = visorchannel_get_uuid(dev->visorchannel);
191 if (visorbus_forcematch) {
195 if (visorbus_forcenomatch)
198 if (!drv->channel_types)
201 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
202 (drv->channel_types[i].name);
204 if (uuid_le_cmp(drv->channel_types[i].guid,
205 channel_type) == 0) {
213 /** This is called when device_unregister() is called for the bus device
214 * instance, after all other tasks involved with destroying the device
218 visorbus_release_busdevice(struct device *xdev)
220 struct visor_device *dev = dev_get_drvdata(xdev);
222 dev_set_drvdata(xdev, NULL);
226 /** This is called when device_unregister() is called for each child
230 visorbus_release_device(struct device *xdev)
232 struct visor_device *dev = to_visor_device(xdev);
234 if (dev->periodic_work) {
235 visor_periodic_work_destroy(dev->periodic_work);
236 dev->periodic_work = NULL;
238 if (dev->visorchannel) {
239 visorchannel_destroy(dev->visorchannel);
240 dev->visorchannel = NULL;
245 /* Implement publishing of device node attributes under:
247 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
251 #define to_devmajorminor_attr(_attr) \
252 container_of(_attr, struct devmajorminor_attribute, attr)
253 #define to_visor_device_from_kobjdevmajorminor(obj) \
254 container_of(obj, struct visor_device, kobjdevmajorminor)
256 struct devmajorminor_attribute {
257 struct attribute attr;
259 ssize_t (*show)(struct visor_device *, int slot, char *buf);
260 ssize_t (*store)(struct visor_device *, int slot, const char *buf,
264 static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
266 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
268 if (slot < 0 || slot >= maxdevnodes)
270 return snprintf(buf, PAGE_SIZE, "%d:%d\n",
271 dev->devnodes[slot].major, dev->devnodes[slot].minor);
275 devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
277 struct devmajorminor_attribute *devmajorminor_attr =
278 to_devmajorminor_attr(attr);
279 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
282 if (devmajorminor_attr->show)
283 ret = devmajorminor_attr->show(dev,
284 devmajorminor_attr->slot, buf);
289 devmajorminor_attr_store(struct kobject *kobj,
290 struct attribute *attr, const char *buf, size_t count)
292 struct devmajorminor_attribute *devmajorminor_attr =
293 to_devmajorminor_attr(attr);
294 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
297 if (devmajorminor_attr->store)
298 ret = devmajorminor_attr->store(dev,
299 devmajorminor_attr->slot,
304 static int register_devmajorminor_attributes(struct visor_device *dev);
307 devmajorminor_create_file(struct visor_device *dev, const char *name,
308 int major, int minor)
310 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
311 struct devmajorminor_attribute *myattr = NULL;
312 int x = -1, rc = 0, slot = -1;
314 register_devmajorminor_attributes(dev);
315 for (slot = 0; slot < maxdevnodes; slot++)
316 if (!dev->devnodes[slot].attr)
318 if (slot == maxdevnodes) {
322 myattr = kzalloc(sizeof(*myattr), GFP_KERNEL);
327 myattr->show = DEVMAJORMINOR_ATTR;
328 myattr->store = NULL;
330 myattr->attr.name = name;
331 myattr->attr.mode = S_IRUGO;
332 dev->devnodes[slot].attr = myattr;
333 dev->devnodes[slot].major = major;
334 dev->devnodes[slot].minor = minor;
335 x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
340 kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
345 dev->devnodes[slot].attr = NULL;
351 devmajorminor_remove_file(struct visor_device *dev, int slot)
353 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
354 struct devmajorminor_attribute *myattr = NULL;
356 if (slot < 0 || slot >= maxdevnodes)
358 myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
361 sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
362 kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
363 dev->devnodes[slot].attr = NULL;
368 devmajorminor_remove_all_files(struct visor_device *dev)
371 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
373 for (i = 0; i < maxdevnodes; i++)
374 devmajorminor_remove_file(dev, i);
377 static const struct sysfs_ops devmajorminor_sysfs_ops = {
378 .show = devmajorminor_attr_show,
379 .store = devmajorminor_attr_store,
382 static struct kobj_type devmajorminor_kobj_type = {
383 .sysfs_ops = &devmajorminor_sysfs_ops
387 register_devmajorminor_attributes(struct visor_device *dev)
391 if (dev->kobjdevmajorminor.parent)
392 goto away; /* already registered */
393 x = kobject_init_and_add(&dev->kobjdevmajorminor,
394 &devmajorminor_kobj_type, &dev->device.kobj,
401 kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
408 unregister_devmajorminor_attributes(struct visor_device *dev)
410 if (!dev->kobjdevmajorminor.parent)
411 return; /* already unregistered */
412 devmajorminor_remove_all_files(dev);
414 kobject_del(&dev->kobjdevmajorminor);
415 kobject_put(&dev->kobjdevmajorminor);
416 dev->kobjdevmajorminor.parent = NULL;
419 /* begin implementation of specific channel attributes to appear under
420 * /sys/bus/visorbus<x>/dev<y>/channel
422 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
425 struct visor_device *vdev = to_visor_device(dev);
427 if (!vdev->visorchannel)
429 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
430 visorchannel_get_physaddr(vdev->visorchannel));
433 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
436 struct visor_device *vdev = to_visor_device(dev);
438 if (!vdev->visorchannel)
440 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
441 visorchannel_get_nbytes(vdev->visorchannel));
444 static ssize_t clientpartition_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct visor_device *vdev = to_visor_device(dev);
449 if (!vdev->visorchannel)
451 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
452 visorchannel_get_clientpartition(vdev->visorchannel));
455 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
458 struct visor_device *vdev = to_visor_device(dev);
461 if (!vdev->visorchannel)
463 return snprintf(buf, PAGE_SIZE, "%s\n",
464 visorchannel_id(vdev->visorchannel, s));
467 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
470 struct visor_device *vdev = to_visor_device(dev);
473 if (!vdev->visorchannel)
475 return snprintf(buf, PAGE_SIZE, "%s\n",
476 visorchannel_zoneid(vdev->visorchannel, s));
479 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
482 struct visor_device *vdev = to_visor_device(dev);
484 struct bus_type *xbus = dev->bus;
485 struct device_driver *xdrv = dev->driver;
486 struct visor_driver *drv = NULL;
488 if (!vdev->visorchannel || !xbus || !xdrv)
490 i = xbus->match(dev, xdrv);
493 drv = to_visor_driver(xdrv);
494 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
497 static DEVICE_ATTR_RO(physaddr);
498 static DEVICE_ATTR_RO(nbytes);
499 static DEVICE_ATTR_RO(clientpartition);
500 static DEVICE_ATTR_RO(typeguid);
501 static DEVICE_ATTR_RO(zoneguid);
502 static DEVICE_ATTR_RO(typename);
504 static struct attribute *channel_attrs[] = {
505 &dev_attr_physaddr.attr,
506 &dev_attr_nbytes.attr,
507 &dev_attr_clientpartition.attr,
508 &dev_attr_typeguid.attr,
509 &dev_attr_zoneguid.attr,
510 &dev_attr_typename.attr,
514 static struct attribute_group channel_attr_grp = {
516 .attrs = channel_attrs,
519 static const struct attribute_group *visorbus_channel_groups[] = {
524 /* end implementation of specific channel attributes */
526 /* BUS instance attributes
528 * define & implement display of bus attributes under
529 * /sys/bus/visorbus/busses/visorbus<n>.
531 * This is a bit hoaky because the kernel does not yet have the infrastructure
532 * to separate bus INSTANCE attributes from bus TYPE attributes...
533 * so we roll our own. See businst.c / businst.h.
537 static ssize_t partition_handle_show(struct device *dev,
538 struct device_attribute *attr,
540 struct visor_device *vdev = to_visor_device(dev);
541 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
543 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", handle);
546 static ssize_t partition_guid_show(struct device *dev,
547 struct device_attribute *attr,
549 struct visor_device *vdev = to_visor_device(dev);
551 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
554 static ssize_t partition_name_show(struct device *dev,
555 struct device_attribute *attr,
557 struct visor_device *vdev = to_visor_device(dev);
559 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
562 static ssize_t channel_addr_show(struct device *dev,
563 struct device_attribute *attr,
565 struct visor_device *vdev = to_visor_device(dev);
566 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
568 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", addr);
571 static ssize_t channel_bytes_show(struct device *dev,
572 struct device_attribute *attr,
574 struct visor_device *vdev = to_visor_device(dev);
575 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
577 return snprintf(buf, PAGE_SIZE, "0x%Lx\n", nbytes);
580 static ssize_t channel_id_show(struct device *dev,
581 struct device_attribute *attr,
583 struct visor_device *vdev = to_visor_device(dev);
586 if (vdev->visorchannel) {
587 visorchannel_id(vdev->visorchannel, buf);
594 static ssize_t client_bus_info_show(struct device *dev,
595 struct device_attribute *attr,
597 struct visor_device *vdev = to_visor_device(dev);
598 struct visorchannel *channel = vdev->visorchannel;
600 int i, x, remain = PAGE_SIZE;
604 struct ultra_vbus_deviceinfo dev_info;
609 partition_name = vdev->name;
610 x = snprintf(p, remain,
611 "Client device / client driver info for %s partition (vbus #%d):\n",
612 partition_name, vdev->chipset_dev_no);
615 x = visorchannel_read(channel,
617 spar_vbus_channel_protocol,
619 &dev_info, sizeof(dev_info));
621 x = vbuschannel_devinfo_to_string(&dev_info, p,
626 x = visorchannel_read(channel,
628 spar_vbus_channel_protocol,
630 &dev_info, sizeof(dev_info));
632 x = vbuschannel_devinfo_to_string(&dev_info, p,
637 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
639 while (off + sizeof(dev_info) <=
640 visorchannel_get_nbytes(channel)) {
641 x = visorchannel_read(channel,
642 off, &dev_info, sizeof(dev_info));
644 x = vbuschannel_devinfo_to_string
645 (&dev_info, p, remain, i);
649 off += sizeof(dev_info);
653 return PAGE_SIZE - remain;
656 static DEVICE_ATTR_RO(partition_handle);
657 static DEVICE_ATTR_RO(partition_guid);
658 static DEVICE_ATTR_RO(partition_name);
659 static DEVICE_ATTR_RO(channel_addr);
660 static DEVICE_ATTR_RO(channel_bytes);
661 static DEVICE_ATTR_RO(channel_id);
662 static DEVICE_ATTR_RO(client_bus_info);
664 static struct attribute *dev_attrs[] = {
665 &dev_attr_partition_handle.attr,
666 &dev_attr_partition_guid.attr,
667 &dev_attr_partition_name.attr,
668 &dev_attr_channel_addr.attr,
669 &dev_attr_channel_bytes.attr,
670 &dev_attr_channel_id.attr,
671 &dev_attr_client_bus_info.attr,
675 static struct attribute_group dev_attr_grp = {
679 static const struct attribute_group *visorbus_groups[] = {
686 * define & implement display of driver attributes under
687 * /sys/bus/visorbus/drivers/<drivername>.
692 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
694 struct visor_driver *drv = to_visor_driver(xdrv);
696 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
700 register_driver_attributes(struct visor_driver *drv)
703 struct driver_attribute version =
704 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
705 drv->version_attr = version;
706 rc = driver_create_file(&drv->driver, &drv->version_attr);
711 unregister_driver_attributes(struct visor_driver *drv)
713 driver_remove_file(&drv->driver, &drv->version_attr);
717 dev_periodic_work(void *xdev)
719 struct visor_device *dev = xdev;
720 struct visor_driver *drv = to_visor_driver(dev->device.driver);
722 down(&dev->visordriver_callback_lock);
723 if (drv->channel_interrupt)
724 drv->channel_interrupt(dev);
725 up(&dev->visordriver_callback_lock);
726 if (!visor_periodic_work_nextperiod(dev->periodic_work))
727 put_device(&dev->device);
731 dev_start_periodic_work(struct visor_device *dev)
733 if (dev->being_removed)
735 /* now up by at least 2 */
736 get_device(&dev->device);
737 if (!visor_periodic_work_start(dev->periodic_work))
738 put_device(&dev->device);
742 dev_stop_periodic_work(struct visor_device *dev)
744 if (visor_periodic_work_stop(dev->periodic_work))
745 put_device(&dev->device);
748 /** This is called automatically upon adding a visor_device (device_add), or
749 * adding a visor_driver (visorbus_register_visor_driver), but only after
750 * visorbus_match has returned 1 to indicate a successful match between
754 visordriver_probe_device(struct device *xdev)
757 struct visor_driver *drv;
758 struct visor_device *dev;
760 drv = to_visor_driver(xdev->driver);
761 dev = to_visor_device(xdev);
762 down(&dev->visordriver_callback_lock);
763 dev->being_removed = false;
765 * ensure that the dev->being_removed flag is cleared before
769 get_device(&dev->device);
771 up(&dev->visordriver_callback_lock);
775 rc = drv->probe(dev);
779 fix_vbus_dev_info(dev);
780 up(&dev->visordriver_callback_lock);
784 put_device(&dev->device);
788 /** This is called when device_unregister() is called for each child device
789 * instance, to notify the appropriate visorbus_driver that the device is
790 * going away, and to decrease the reference count of the device.
793 visordriver_remove_device(struct device *xdev)
795 struct visor_device *dev;
796 struct visor_driver *drv;
798 dev = to_visor_device(xdev);
799 drv = to_visor_driver(xdev->driver);
800 down(&dev->visordriver_callback_lock);
801 dev->being_removed = true;
803 * ensure that the dev->being_removed flag is set before we start the
811 up(&dev->visordriver_callback_lock);
812 dev_stop_periodic_work(dev);
813 devmajorminor_remove_all_files(dev);
815 put_device(&dev->device);
820 /** A particular type of visor driver calls this function to register
821 * the driver. The caller MUST fill in the following fields within the
823 * name, version, owner, channel_types, probe, remove
825 * Here's how the whole Linux bus / driver / device model works.
827 * At system start-up, the visorbus kernel module is loaded, which registers
828 * visorbus_type as a bus type, using bus_register().
830 * All kernel modules that support particular device types on a
831 * visorbus bus are loaded. Each of these kernel modules calls
832 * visorbus_register_visor_driver() in their init functions, passing a
833 * visor_driver struct. visorbus_register_visor_driver() in turn calls
834 * register_driver(&visor_driver.driver). This .driver member is
835 * initialized with generic methods (like probe), whose sole responsibility
836 * is to act as a broker for the real methods, which are within the
837 * visor_driver struct. (This is the way the subclass behavior is
838 * implemented, since visor_driver is essentially a subclass of the
839 * generic driver.) Whenever a driver_register() happens, core bus code in
840 * the kernel does (see device_attach() in drivers/base/dd.c):
842 * for each dev associated with the bus (the bus that driver is on) that
843 * does not yet have a driver
844 * if bus.match(dev,newdriver) == yes_matched ** .match specified
845 * ** during bus_register().
846 * newdriver.probe(dev) ** for visor drivers, this will call
847 * ** the generic driver.probe implemented in visorbus.c,
848 * ** which in turn calls the probe specified within the
849 * ** struct visor_driver (which was specified by the
850 * ** actual device driver as part of
851 * ** visorbus_register_visor_driver()).
853 * The above dance also happens when a new device appears.
854 * So the question is, how are devices created within the system?
855 * Basically, just call device_add(dev). See pci_bus_add_devices().
856 * pci_scan_device() shows an example of how to build a device struct. It
857 * returns the newly-created struct to pci_scan_single_device(), who adds it
858 * to the list of devices at PCIBUS.devices. That list of devices is what
859 * is traversed by pci_bus_add_devices().
862 int visorbus_register_visor_driver(struct visor_driver *drv)
866 drv->driver.name = drv->name;
867 drv->driver.bus = &visorbus_type;
868 drv->driver.probe = visordriver_probe_device;
869 drv->driver.remove = visordriver_remove_device;
870 drv->driver.owner = drv->owner;
872 /* driver_register does this:
873 * bus_add_driver(drv)
874 * ->if (drv.bus) ** (bus_type) **
876 * for each dev with bus type of drv.bus
877 * if (!dev.drv) ** no driver assigned yet **
878 * if (bus.match(dev,drv)) [visorbus_match]
880 * if (!drv.probe(dev)) [visordriver_probe_device]
884 rc = driver_register(&drv->driver);
887 rc = register_driver_attributes(drv);
890 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
892 /** A particular type of visor driver calls this function to unregister
893 * the driver, i.e., within its module_exit function.
896 visorbus_unregister_visor_driver(struct visor_driver *drv)
898 unregister_driver_attributes(drv);
899 driver_unregister(&drv->driver);
901 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
904 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
905 void *dest, unsigned long nbytes)
907 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
909 EXPORT_SYMBOL_GPL(visorbus_read_channel);
912 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
913 void *src, unsigned long nbytes)
915 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
917 EXPORT_SYMBOL_GPL(visorbus_write_channel);
920 visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
921 unsigned long nbytes)
923 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
925 EXPORT_SYMBOL_GPL(visorbus_clear_channel);
928 visorbus_registerdevnode(struct visor_device *dev,
929 const char *name, int major, int minor)
931 return devmajorminor_create_file(dev, name, major, minor);
933 EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
935 /** We don't really have a real interrupt, so for now we just call the
936 * interrupt function periodically...
939 visorbus_enable_channel_interrupts(struct visor_device *dev)
941 dev_start_periodic_work(dev);
943 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
946 visorbus_disable_channel_interrupts(struct visor_device *dev)
948 dev_stop_periodic_work(dev);
950 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
952 /** This is how everything starts from the device end.
953 * This function is called when a channel first appears via a ControlVM
954 * message. In response, this function allocates a visor_device to
955 * correspond to the new channel, and attempts to connect it the appropriate
956 * driver. If the appropriate driver is found, the visor_driver.probe()
957 * function for that driver will be called, and will be passed the new
958 * visor_device that we just created.
960 * It's ok if the appropriate driver is not yet loaded, because in that case
961 * the new device struct will just stick around in the bus' list of devices.
962 * When the appropriate driver calls visorbus_register_visor_driver(), the
963 * visor_driver.probe() for the new driver will be called with the new
967 create_visor_device(struct visor_device *dev)
970 u32 chipset_bus_no = dev->chipset_bus_no;
971 u32 chipset_dev_no = dev->chipset_dev_no;
973 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
974 POSTCODE_SEVERITY_INFO);
976 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
977 dev->device.bus = &visorbus_type;
978 dev->device.groups = visorbus_channel_groups;
979 device_initialize(&dev->device);
980 dev->device.release = visorbus_release_device;
981 /* keep a reference just for us (now 2) */
982 get_device(&dev->device);
984 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
985 periodic_dev_workqueue,
987 dev, dev_name(&dev->device));
988 if (!dev->periodic_work) {
989 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
994 /* bus_id must be a unique name with respect to this bus TYPE
995 * (NOT bus instance). That's why we need to include the bus
996 * number within the name.
998 dev_set_name(&dev->device, "vbus%u:dev%u",
999 chipset_bus_no, chipset_dev_no);
1001 /* device_add does this:
1002 * bus_add_device(dev)
1003 * ->device_attach(dev)
1004 * ->for each driver drv registered on the bus that dev is on
1005 * if (dev.drv) ** device already has a driver **
1006 * ** not sure we could ever get here... **
1008 * if (bus.match(dev,drv)) [visorbus_match]
1010 * if (!drv.probe(dev)) [visordriver_probe_device]
1013 * Note that device_add does NOT fail if no driver failed to
1014 * claim the device. The device will be linked onto
1015 * bus_type.klist_devices regardless (use bus_for_each_dev).
1017 rc = device_add(&dev->device);
1019 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
1024 rc = register_devmajorminor_attributes(dev);
1026 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1031 list_add_tail(&dev->list_all, &list_all_device_instances);
1035 device_unregister(&dev->device);
1037 put_device(&dev->device);
1042 remove_visor_device(struct visor_device *dev)
1044 list_del(&dev->list_all);
1045 unregister_devmajorminor_attributes(dev);
1046 put_device(&dev->device);
1047 device_unregister(&dev->device);
1051 get_vbus_header_info(struct visorchannel *chan,
1052 struct spar_vbus_headerinfo *hdr_info)
1056 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
1058 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1059 sizeof(*hdr_info)) < 0) {
1062 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
1064 if (hdr_info->device_info_struct_bytes <
1065 sizeof(struct ultra_vbus_deviceinfo)) {
1073 /* Write the contents of <info> to the struct
1074 * spar_vbus_channel_protocol.chp_info. */
1077 write_vbus_chp_info(struct visorchannel *chan,
1078 struct spar_vbus_headerinfo *hdr_info,
1079 struct ultra_vbus_deviceinfo *info)
1081 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1083 if (hdr_info->chp_info_offset == 0)
1086 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1091 /* Write the contents of <info> to the struct
1092 * spar_vbus_channel_protocol.bus_info. */
1095 write_vbus_bus_info(struct visorchannel *chan,
1096 struct spar_vbus_headerinfo *hdr_info,
1097 struct ultra_vbus_deviceinfo *info)
1099 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1101 if (hdr_info->bus_info_offset == 0)
1104 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1109 /* Write the contents of <info> to the
1110 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1113 write_vbus_dev_info(struct visorchannel *chan,
1114 struct spar_vbus_headerinfo *hdr_info,
1115 struct ultra_vbus_deviceinfo *info, int devix)
1118 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1119 (hdr_info->device_info_struct_bytes * devix);
1121 if (hdr_info->dev_info_offset == 0)
1124 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1129 /* For a child device just created on a client bus, fill in
1130 * information about the driver that is controlling this device into
1131 * the the appropriate slot within the vbus channel of the bus
1135 fix_vbus_dev_info(struct visor_device *visordev)
1138 struct visor_device *bdev;
1139 struct visor_driver *visordrv;
1140 int bus_no = visordev->chipset_bus_no;
1141 int dev_no = visordev->chipset_dev_no;
1142 struct ultra_vbus_deviceinfo dev_info;
1143 const char *chan_type_name = NULL;
1144 struct spar_vbus_headerinfo *hdr_info;
1146 if (!visordev->device.driver)
1149 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
1153 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
1157 visordrv = to_visor_driver(visordev->device.driver);
1159 /* Within the list of device types (by GUID) that the driver
1160 * says it supports, find out which one of those types matches
1161 * the type of this device, so that we can include the device
1164 for (i = 0; visordrv->channel_types[i].name; i++) {
1165 if (memcmp(&visordrv->channel_types[i].guid,
1166 &visordev->channel_type_guid,
1167 sizeof(visordrv->channel_types[i].guid)) == 0) {
1168 chan_type_name = visordrv->channel_types[i].name;
1173 bus_device_info_init(&dev_info, chan_type_name,
1174 visordrv->name, visordrv->version,
1176 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
1178 /* Re-write bus+chipset info, because it is possible that this
1179 * was previously written by our evil counterpart, virtpci.
1181 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
1182 write_vbus_bus_info(bdev->visorchannel, hdr_info,
1183 &clientbus_driverinfo);
1186 /** Create a device instance for the visor bus itself.
1189 create_bus_instance(struct visor_device *dev)
1192 int id = dev->chipset_bus_no;
1193 struct spar_vbus_headerinfo *hdr_info;
1195 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1197 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1203 dev_set_name(&dev->device, "visorbus%d", id);
1204 dev->device.bus = &visorbus_type;
1205 dev->device.groups = visorbus_groups;
1206 dev->device.release = visorbus_release_busdevice;
1208 if (device_register(&dev->device) < 0) {
1209 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1210 POSTCODE_SEVERITY_ERR);
1215 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1216 dev->vbus_hdr_info = (void *)hdr_info;
1217 write_vbus_chp_info(dev->visorchannel, hdr_info,
1218 &chipset_driverinfo);
1219 write_vbus_bus_info(dev->visorchannel, hdr_info,
1220 &clientbus_driverinfo);
1225 list_add_tail(&dev->list_all, &list_all_bus_instances);
1226 dev_set_drvdata(&dev->device, dev);
1235 /** Remove a device instance for the visor bus itself.
1238 remove_bus_instance(struct visor_device *dev)
1240 /* Note that this will result in the release method for
1241 * dev->dev being called, which will call
1242 * visorbus_release_busdevice(). This has something to do with
1243 * the put_device() done in device_unregister(), but I have never
1244 * successfully been able to trace thru the code to see where/how
1245 * release() gets called. But I know it does.
1248 if (dev->visorchannel) {
1249 visorchannel_destroy(dev->visorchannel);
1250 dev->visorchannel = NULL;
1252 kfree(dev->vbus_hdr_info);
1253 list_del(&dev->list_all);
1254 device_unregister(&dev->device);
1257 /** Create and register the one-and-only one instance of
1258 * the visor bus type (visorbus_type).
1261 create_bus_type(void)
1265 rc = bus_register(&visorbus_type);
1269 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1272 remove_bus_type(void)
1274 bus_unregister(&visorbus_type);
1277 /** Remove all child visor bus device instances.
1280 remove_all_visor_devices(void)
1282 struct list_head *listentry, *listtmp;
1284 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1285 struct visor_device *dev = list_entry(listentry,
1286 struct visor_device,
1288 remove_visor_device(dev);
1293 chipset_bus_create(struct visor_device *dev)
1296 u32 bus_no = dev->chipset_bus_no;
1298 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1299 rc = create_bus_instance(dev);
1300 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1303 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1304 POSTCODE_SEVERITY_ERR);
1306 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1307 POSTCODE_SEVERITY_INFO);
1309 if (chipset_responders.bus_create)
1310 (*chipset_responders.bus_create) (dev, rc);
1314 chipset_bus_destroy(struct visor_device *dev)
1316 remove_bus_instance(dev);
1317 if (chipset_responders.bus_destroy)
1318 (*chipset_responders.bus_destroy)(dev, 0);
1322 chipset_device_create(struct visor_device *dev_info)
1325 u32 bus_no = dev_info->chipset_bus_no;
1326 u32 dev_no = dev_info->chipset_dev_no;
1328 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1329 POSTCODE_SEVERITY_INFO);
1331 rc = create_visor_device(dev_info);
1332 if (chipset_responders.device_create)
1333 chipset_responders.device_create(dev_info, rc);
1336 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1337 POSTCODE_SEVERITY_ERR);
1339 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1340 POSTCODE_SEVERITY_INFO);
1344 chipset_device_destroy(struct visor_device *dev_info)
1346 remove_visor_device(dev_info);
1348 if (chipset_responders.device_destroy)
1349 (*chipset_responders.device_destroy) (dev_info, 0);
1352 /* This is the callback function specified for a function driver, to
1353 * be called when a pending "pause device" operation has been
1357 pause_state_change_complete(struct visor_device *dev, int status)
1362 dev->pausing = false;
1363 if (!chipset_responders.device_pause) /* this can never happen! */
1366 /* Notify the chipset driver that the pause is complete, which
1367 * will presumably want to send some sort of response to the
1369 (*chipset_responders.device_pause) (dev, status);
1372 /* This is the callback function specified for a function driver, to
1373 * be called when a pending "resume device" operation has been
1377 resume_state_change_complete(struct visor_device *dev, int status)
1382 dev->resuming = false;
1383 if (!chipset_responders.device_resume) /* this can never happen! */
1386 /* Notify the chipset driver that the resume is complete,
1387 * which will presumably want to send some sort of response to
1389 (*chipset_responders.device_resume) (dev, status);
1392 /* Tell the subordinate function driver for a specific device to pause
1393 * or resume that device. Result is returned asynchronously via a
1394 * callback function.
1397 initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
1400 struct visor_driver *drv = NULL;
1401 void (*notify_func)(struct visor_device *dev, int response) = NULL;
1404 notify_func = chipset_responders.device_pause;
1406 notify_func = chipset_responders.device_resume;
1410 drv = to_visor_driver(dev->device.driver);
1414 if (dev->pausing || dev->resuming)
1417 /* Note that even though both drv->pause() and drv->resume
1418 * specify a callback function, it is NOT necessary for us to
1419 * increment our local module usage count. Reason is, there
1420 * is already a linkage dependency between child function
1421 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1422 * visorbus while child function drivers are still running.
1428 dev->pausing = true;
1429 x = drv->pause(dev, pause_state_change_complete);
1431 /* This should be done at BUS resume time, but an
1432 * existing problem prevents us from ever getting a bus
1433 * resume... This hack would fail to work should we
1434 * ever have a bus that contains NO devices, since we
1435 * would never even get here in that case. */
1436 fix_vbus_dev_info(dev);
1440 dev->resuming = true;
1441 x = drv->resume(dev, resume_state_change_complete);
1445 dev->pausing = false;
1447 dev->resuming = false;
1454 (*notify_func)(dev, rc);
1459 chipset_device_pause(struct visor_device *dev_info)
1461 initiate_chipset_device_pause_resume(dev_info, true);
1465 chipset_device_resume(struct visor_device *dev_info)
1467 initiate_chipset_device_pause_resume(dev_info, false);
1470 struct channel_size_info {
1472 unsigned long min_size;
1473 unsigned long max_size;
1481 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1482 bus_device_info_init(&clientbus_driverinfo,
1483 "clientbus", "visorbus",
1486 rc = create_bus_type();
1488 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1492 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1493 if (!periodic_dev_workqueue) {
1494 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
1499 /* This enables us to receive notifications when devices appear for
1500 * which this service partition is to be a server for.
1502 visorchipset_register_busdev(&chipset_notifiers,
1503 &chipset_responders,
1504 &chipset_driverinfo);
1510 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
1511 POSTCODE_SEVERITY_ERR);
1518 struct list_head *listentry, *listtmp;
1520 visorchipset_register_busdev(NULL, NULL, NULL);
1521 remove_all_visor_devices();
1523 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1524 destroy_workqueue(periodic_dev_workqueue);
1525 periodic_dev_workqueue = NULL;
1527 if (periodic_test_workqueue) {
1528 cancel_delayed_work(&periodic_work);
1529 flush_workqueue(periodic_test_workqueue);
1530 destroy_workqueue(periodic_test_workqueue);
1531 periodic_test_workqueue = NULL;
1534 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1535 struct visor_device *dev = list_entry(listentry,
1536 struct visor_device,
1538 remove_bus_instance(dev);
1543 module_param_named(debug, visorbus_debug, int, S_IRUGO);
1544 MODULE_PARM_DESC(visorbus_debug, "1 to debug");
1546 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1547 MODULE_PARM_DESC(visorbus_forcematch,
1548 "1 to force a successful dev <--> drv match");
1550 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1551 MODULE_PARM_DESC(visorbus_forcenomatch,
1552 "1 to force an UNsuccessful dev <--> drv match");
1554 module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1555 MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");