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 "vbuschannel.h"
23 #include "guestlinuxdebug.h"
24 #include "vmcallinterface.h"
26 #define MYDRVNAME "visorbus"
28 /* module parameters */
29 static int visorbus_forcematch;
30 static int visorbus_forcenomatch;
32 #define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
34 /* Display string that is guaranteed to be no longer the 99 characters*/
37 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
38 #define POLLJIFFIES_TESTWORK 100
39 #define POLLJIFFIES_NORMALCHANNEL 10
41 static int busreg_rc = -ENODEV; /* stores the result from bus registration */
43 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
44 static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
45 static void fix_vbus_dev_info(struct visor_device *visordev);
47 /* BUS type attributes
49 * define & implement display of bus attributes under
54 static ssize_t version_show(struct bus_type *bus, char *buf)
56 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
59 static BUS_ATTR_RO(version);
61 static struct attribute *visorbus_bus_attrs[] = {
62 &bus_attr_version.attr,
66 static const struct attribute_group visorbus_bus_group = {
67 .attrs = visorbus_bus_attrs,
70 static const struct attribute_group *visorbus_bus_groups[] = {
76 * DEVICE type attributes
78 * The modalias file will contain the guid of the device.
80 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
83 struct visor_device *vdev;
86 vdev = to_visor_device(dev);
87 guid = visorchannel_get_uuid(vdev->visorchannel);
88 return snprintf(buf, PAGE_SIZE, "visorbus:%pUl\n", &guid);
90 static DEVICE_ATTR_RO(modalias);
92 static struct attribute *visorbus_dev_attrs[] = {
93 &dev_attr_modalias.attr,
97 /* sysfs example for bridge-only sysfs files using device_type's */
98 static const struct attribute_group visorbus_dev_group = {
99 .attrs = visorbus_dev_attrs,
102 static const struct attribute_group *visorbus_dev_groups[] = {
107 /** This describes the TYPE of bus.
108 * (Don't confuse this with an INSTANCE of the bus.)
110 struct bus_type visorbus_type = {
112 .match = visorbus_match,
113 .uevent = visorbus_uevent,
114 .dev_groups = visorbus_dev_groups,
115 .bus_groups = visorbus_bus_groups,
118 static long long bus_count; /** number of bus instances */
119 /** ever-increasing */
121 static void chipset_bus_create(struct visor_device *bus_info);
122 static void chipset_bus_destroy(struct visor_device *bus_info);
123 static void chipset_device_create(struct visor_device *dev_info);
124 static void chipset_device_destroy(struct visor_device *dev_info);
125 static void chipset_device_pause(struct visor_device *dev_info);
126 static void chipset_device_resume(struct visor_device *dev_info);
128 /** These functions are implemented herein, and are called by the chipset
129 * driver to notify us about specific events.
131 static struct visorchipset_busdev_notifiers chipset_notifiers = {
132 .bus_create = chipset_bus_create,
133 .bus_destroy = chipset_bus_destroy,
134 .device_create = chipset_device_create,
135 .device_destroy = chipset_device_destroy,
136 .device_pause = chipset_device_pause,
137 .device_resume = chipset_device_resume,
140 /** These functions are implemented in the chipset driver, and we call them
141 * herein when we want to acknowledge a specific event.
143 static struct visorchipset_busdev_responders chipset_responders;
145 /* filled in with info about parent chipset driver when we register with it */
146 static struct ultra_vbus_deviceinfo chipset_driverinfo;
147 /* filled in with info about this driver, wrt it servicing client busses */
148 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
150 /** list of visor_device structs, linked via .list_all */
151 static LIST_HEAD(list_all_bus_instances);
152 /** list of visor_device structs, linked via .list_all */
153 static LIST_HEAD(list_all_device_instances);
156 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
158 struct visor_device *dev;
161 dev = to_visor_device(xdev);
162 guid = visorchannel_get_uuid(dev->visorchannel);
164 if (add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid))
169 /* This is called automatically upon adding a visor_device (device_add), or
170 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
171 * provided driver can control the specified device.
174 visorbus_match(struct device *xdev, struct device_driver *xdrv)
176 uuid_le channel_type;
178 struct visor_device *dev;
179 struct visor_driver *drv;
181 dev = to_visor_device(xdev);
182 drv = to_visor_driver(xdrv);
183 channel_type = visorchannel_get_uuid(dev->visorchannel);
185 if (visorbus_forcematch)
187 if (visorbus_forcenomatch)
189 if (!drv->channel_types)
193 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
194 (drv->channel_types[i].name);
196 if (uuid_le_cmp(drv->channel_types[i].guid,
203 /** This is called when device_unregister() is called for the bus device
204 * instance, after all other tasks involved with destroying the device
208 visorbus_release_busdevice(struct device *xdev)
210 struct visor_device *dev = dev_get_drvdata(xdev);
215 /** This is called when device_unregister() is called for each child
219 visorbus_release_device(struct device *xdev)
221 struct visor_device *dev = to_visor_device(xdev);
223 if (dev->visorchannel) {
224 visorchannel_destroy(dev->visorchannel);
225 dev->visorchannel = NULL;
230 /* begin implementation of specific channel attributes to appear under
231 * /sys/bus/visorbus<x>/dev<y>/channel
233 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
236 struct visor_device *vdev = to_visor_device(dev);
238 if (!vdev->visorchannel)
240 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
241 visorchannel_get_physaddr(vdev->visorchannel));
244 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
247 struct visor_device *vdev = to_visor_device(dev);
249 if (!vdev->visorchannel)
251 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
252 visorchannel_get_nbytes(vdev->visorchannel));
255 static ssize_t clientpartition_show(struct device *dev,
256 struct device_attribute *attr, char *buf)
258 struct visor_device *vdev = to_visor_device(dev);
260 if (!vdev->visorchannel)
262 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
263 visorchannel_get_clientpartition(vdev->visorchannel));
266 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
269 struct visor_device *vdev = to_visor_device(dev);
270 char typeid[LINESIZE];
272 if (!vdev->visorchannel)
274 return snprintf(buf, PAGE_SIZE, "%s\n",
275 visorchannel_id(vdev->visorchannel, typeid));
278 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
281 struct visor_device *vdev = to_visor_device(dev);
282 char zoneid[LINESIZE];
284 if (!vdev->visorchannel)
286 return snprintf(buf, PAGE_SIZE, "%s\n",
287 visorchannel_zoneid(vdev->visorchannel, zoneid));
290 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
293 struct visor_device *vdev = to_visor_device(dev);
295 struct bus_type *xbus = dev->bus;
296 struct device_driver *xdrv = dev->driver;
297 struct visor_driver *drv = NULL;
299 if (!vdev->visorchannel || !xbus || !xdrv)
301 i = xbus->match(dev, xdrv);
304 drv = to_visor_driver(xdrv);
305 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
308 static DEVICE_ATTR_RO(physaddr);
309 static DEVICE_ATTR_RO(nbytes);
310 static DEVICE_ATTR_RO(clientpartition);
311 static DEVICE_ATTR_RO(typeguid);
312 static DEVICE_ATTR_RO(zoneguid);
313 static DEVICE_ATTR_RO(typename);
315 static struct attribute *channel_attrs[] = {
316 &dev_attr_physaddr.attr,
317 &dev_attr_nbytes.attr,
318 &dev_attr_clientpartition.attr,
319 &dev_attr_typeguid.attr,
320 &dev_attr_zoneguid.attr,
321 &dev_attr_typename.attr,
325 static struct attribute_group channel_attr_grp = {
327 .attrs = channel_attrs,
330 static const struct attribute_group *visorbus_channel_groups[] = {
335 /* end implementation of specific channel attributes */
337 /* BUS instance attributes
339 * define & implement display of bus attributes under
340 * /sys/bus/visorbus/busses/visorbus<n>.
342 * This is a bit hoaky because the kernel does not yet have the infrastructure
343 * to separate bus INSTANCE attributes from bus TYPE attributes...
344 * so we roll our own. See businst.c / businst.h.
348 static ssize_t partition_handle_show(struct device *dev,
349 struct device_attribute *attr,
351 struct visor_device *vdev = to_visor_device(dev);
352 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
354 return snprintf(buf, PAGE_SIZE, "0x%llx\n", handle);
357 static ssize_t partition_guid_show(struct device *dev,
358 struct device_attribute *attr,
360 struct visor_device *vdev = to_visor_device(dev);
362 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
365 static ssize_t partition_name_show(struct device *dev,
366 struct device_attribute *attr,
368 struct visor_device *vdev = to_visor_device(dev);
370 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
373 static ssize_t channel_addr_show(struct device *dev,
374 struct device_attribute *attr,
376 struct visor_device *vdev = to_visor_device(dev);
377 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
379 return snprintf(buf, PAGE_SIZE, "0x%llx\n", addr);
382 static ssize_t channel_bytes_show(struct device *dev,
383 struct device_attribute *attr,
385 struct visor_device *vdev = to_visor_device(dev);
386 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
388 return snprintf(buf, PAGE_SIZE, "0x%llx\n", nbytes);
391 static ssize_t channel_id_show(struct device *dev,
392 struct device_attribute *attr,
394 struct visor_device *vdev = to_visor_device(dev);
397 if (vdev->visorchannel) {
398 visorchannel_id(vdev->visorchannel, buf);
405 static ssize_t client_bus_info_show(struct device *dev,
406 struct device_attribute *attr,
408 struct visor_device *vdev = to_visor_device(dev);
409 struct visorchannel *channel = vdev->visorchannel;
411 int i, shift, remain = PAGE_SIZE;
415 struct ultra_vbus_deviceinfo dev_info;
420 partition_name = vdev->name;
421 shift = snprintf(pos, remain,
422 "Client device / client driver info for %s eartition (vbus #%u):\n",
423 partition_name, vdev->chipset_dev_no);
426 shift = visorchannel_read(channel,
428 spar_vbus_channel_protocol,
430 &dev_info, sizeof(dev_info));
432 shift = vbuschannel_devinfo_to_string(&dev_info, pos,
437 shift = visorchannel_read(channel,
439 spar_vbus_channel_protocol,
441 &dev_info, sizeof(dev_info));
443 shift = vbuschannel_devinfo_to_string(&dev_info, pos,
448 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
450 while (off + sizeof(dev_info) <=
451 visorchannel_get_nbytes(channel)) {
452 shift = visorchannel_read(channel,
456 shift = vbuschannel_devinfo_to_string
457 (&dev_info, pos, remain, i);
461 off += sizeof(dev_info);
465 return PAGE_SIZE - remain;
468 static DEVICE_ATTR_RO(partition_handle);
469 static DEVICE_ATTR_RO(partition_guid);
470 static DEVICE_ATTR_RO(partition_name);
471 static DEVICE_ATTR_RO(channel_addr);
472 static DEVICE_ATTR_RO(channel_bytes);
473 static DEVICE_ATTR_RO(channel_id);
474 static DEVICE_ATTR_RO(client_bus_info);
476 static struct attribute *dev_attrs[] = {
477 &dev_attr_partition_handle.attr,
478 &dev_attr_partition_guid.attr,
479 &dev_attr_partition_name.attr,
480 &dev_attr_channel_addr.attr,
481 &dev_attr_channel_bytes.attr,
482 &dev_attr_channel_id.attr,
483 &dev_attr_client_bus_info.attr,
487 static struct attribute_group dev_attr_grp = {
491 static const struct attribute_group *visorbus_groups[] = {
498 * define & implement display of driver attributes under
499 * /sys/bus/visorbus/drivers/<drivername>.
504 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
506 struct visor_driver *drv = to_visor_driver(xdrv);
508 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
512 register_driver_attributes(struct visor_driver *drv)
514 struct driver_attribute version =
515 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
516 drv->version_attr = version;
517 return driver_create_file(&drv->driver, &drv->version_attr);
521 unregister_driver_attributes(struct visor_driver *drv)
523 driver_remove_file(&drv->driver, &drv->version_attr);
527 dev_periodic_work(unsigned long __opaque)
529 struct visor_device *dev = (struct visor_device *)__opaque;
530 struct visor_driver *drv = to_visor_driver(dev->device.driver);
532 if (drv->channel_interrupt)
533 drv->channel_interrupt(dev);
534 mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
538 dev_start_periodic_work(struct visor_device *dev)
540 if (dev->being_removed || dev->timer_active)
542 /* now up by at least 2 */
543 get_device(&dev->device);
544 dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
545 add_timer(&dev->timer);
546 dev->timer_active = true;
550 dev_stop_periodic_work(struct visor_device *dev)
552 if (!dev->timer_active)
554 del_timer_sync(&dev->timer);
555 dev->timer_active = false;
556 put_device(&dev->device);
559 /** This is called automatically upon adding a visor_device (device_add), or
560 * adding a visor_driver (visorbus_register_visor_driver), but only after
561 * visorbus_match has returned 1 to indicate a successful match between
565 visordriver_probe_device(struct device *xdev)
568 struct visor_driver *drv;
569 struct visor_device *dev;
571 drv = to_visor_driver(xdev->driver);
572 dev = to_visor_device(xdev);
577 mutex_lock(&dev->visordriver_callback_lock);
578 dev->being_removed = false;
580 res = drv->probe(dev);
582 /* success: reference kept via unmatched get_device() */
583 get_device(&dev->device);
584 fix_vbus_dev_info(dev);
587 mutex_unlock(&dev->visordriver_callback_lock);
591 /** This is called when device_unregister() is called for each child device
592 * instance, to notify the appropriate visorbus_driver that the device is
593 * going away, and to decrease the reference count of the device.
596 visordriver_remove_device(struct device *xdev)
598 struct visor_device *dev;
599 struct visor_driver *drv;
601 dev = to_visor_device(xdev);
602 drv = to_visor_driver(xdev->driver);
603 mutex_lock(&dev->visordriver_callback_lock);
604 dev->being_removed = true;
607 mutex_unlock(&dev->visordriver_callback_lock);
608 dev_stop_periodic_work(dev);
610 put_device(&dev->device);
614 /** A particular type of visor driver calls this function to register
615 * the driver. The caller MUST fill in the following fields within the
617 * name, version, owner, channel_types, probe, remove
619 * Here's how the whole Linux bus / driver / device model works.
621 * At system start-up, the visorbus kernel module is loaded, which registers
622 * visorbus_type as a bus type, using bus_register().
624 * All kernel modules that support particular device types on a
625 * visorbus bus are loaded. Each of these kernel modules calls
626 * visorbus_register_visor_driver() in their init functions, passing a
627 * visor_driver struct. visorbus_register_visor_driver() in turn calls
628 * register_driver(&visor_driver.driver). This .driver member is
629 * initialized with generic methods (like probe), whose sole responsibility
630 * is to act as a broker for the real methods, which are within the
631 * visor_driver struct. (This is the way the subclass behavior is
632 * implemented, since visor_driver is essentially a subclass of the
633 * generic driver.) Whenever a driver_register() happens, core bus code in
634 * the kernel does (see device_attach() in drivers/base/dd.c):
636 * for each dev associated with the bus (the bus that driver is on) that
637 * does not yet have a driver
638 * if bus.match(dev,newdriver) == yes_matched ** .match specified
639 * ** during bus_register().
640 * newdriver.probe(dev) ** for visor drivers, this will call
641 * ** the generic driver.probe implemented in visorbus.c,
642 * ** which in turn calls the probe specified within the
643 * ** struct visor_driver (which was specified by the
644 * ** actual device driver as part of
645 * ** visorbus_register_visor_driver()).
647 * The above dance also happens when a new device appears.
648 * So the question is, how are devices created within the system?
649 * Basically, just call device_add(dev). See pci_bus_add_devices().
650 * pci_scan_device() shows an example of how to build a device struct. It
651 * returns the newly-created struct to pci_scan_single_device(), who adds it
652 * to the list of devices at PCIBUS.devices. That list of devices is what
653 * is traversed by pci_bus_add_devices().
656 int visorbus_register_visor_driver(struct visor_driver *drv)
661 return -ENODEV; /*can't register on a nonexistent bus*/
663 drv->driver.name = drv->name;
664 drv->driver.bus = &visorbus_type;
665 drv->driver.probe = visordriver_probe_device;
666 drv->driver.remove = visordriver_remove_device;
667 drv->driver.owner = drv->owner;
669 /* driver_register does this:
670 * bus_add_driver(drv)
671 * ->if (drv.bus) ** (bus_type) **
673 * for each dev with bus type of drv.bus
674 * if (!dev.drv) ** no driver assigned yet **
675 * if (bus.match(dev,drv)) [visorbus_match]
677 * if (!drv.probe(dev)) [visordriver_probe_device]
681 rc = driver_register(&drv->driver);
684 rc = register_driver_attributes(drv);
686 driver_unregister(&drv->driver);
689 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
691 /** A particular type of visor driver calls this function to unregister
692 * the driver, i.e., within its module_exit function.
695 visorbus_unregister_visor_driver(struct visor_driver *drv)
697 unregister_driver_attributes(drv);
698 driver_unregister(&drv->driver);
700 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
703 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
704 void *dest, unsigned long nbytes)
706 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
708 EXPORT_SYMBOL_GPL(visorbus_read_channel);
711 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
712 void *src, unsigned long nbytes)
714 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
716 EXPORT_SYMBOL_GPL(visorbus_write_channel);
718 /** We don't really have a real interrupt, so for now we just call the
719 * interrupt function periodically...
722 visorbus_enable_channel_interrupts(struct visor_device *dev)
724 dev_start_periodic_work(dev);
726 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
729 visorbus_disable_channel_interrupts(struct visor_device *dev)
731 dev_stop_periodic_work(dev);
733 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
735 /** This is how everything starts from the device end.
736 * This function is called when a channel first appears via a ControlVM
737 * message. In response, this function allocates a visor_device to
738 * correspond to the new channel, and attempts to connect it the appropriate
739 * driver. If the appropriate driver is found, the visor_driver.probe()
740 * function for that driver will be called, and will be passed the new
741 * visor_device that we just created.
743 * It's ok if the appropriate driver is not yet loaded, because in that case
744 * the new device struct will just stick around in the bus' list of devices.
745 * When the appropriate driver calls visorbus_register_visor_driver(), the
746 * visor_driver.probe() for the new driver will be called with the new
750 create_visor_device(struct visor_device *dev)
753 u32 chipset_bus_no = dev->chipset_bus_no;
754 u32 chipset_dev_no = dev->chipset_dev_no;
756 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
757 POSTCODE_SEVERITY_INFO);
759 mutex_init(&dev->visordriver_callback_lock);
760 dev->device.bus = &visorbus_type;
761 dev->device.groups = visorbus_channel_groups;
762 device_initialize(&dev->device);
763 dev->device.release = visorbus_release_device;
764 /* keep a reference just for us (now 2) */
765 get_device(&dev->device);
766 init_timer(&dev->timer);
767 dev->timer.data = (unsigned long)(dev);
768 dev->timer.function = dev_periodic_work;
770 /* bus_id must be a unique name with respect to this bus TYPE
771 * (NOT bus instance). That's why we need to include the bus
772 * number within the name.
774 dev_set_name(&dev->device, "vbus%u:dev%u",
775 chipset_bus_no, chipset_dev_no);
777 /* device_add does this:
778 * bus_add_device(dev)
779 * ->device_attach(dev)
780 * ->for each driver drv registered on the bus that dev is on
781 * if (dev.drv) ** device already has a driver **
782 * ** not sure we could ever get here... **
784 * if (bus.match(dev,drv)) [visorbus_match]
786 * if (!drv.probe(dev)) [visordriver_probe_device]
789 * Note that device_add does NOT fail if no driver failed to
790 * claim the device. The device will be linked onto
791 * bus_type.klist_devices regardless (use bus_for_each_dev).
793 err = device_add(&dev->device);
795 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
800 list_add_tail(&dev->list_all, &list_all_device_instances);
801 return 0; /* success: reference kept via unmatched get_device() */
804 put_device(&dev->device);
809 remove_visor_device(struct visor_device *dev)
811 list_del(&dev->list_all);
812 put_device(&dev->device);
813 device_unregister(&dev->device);
817 get_vbus_header_info(struct visorchannel *chan,
818 struct spar_vbus_headerinfo *hdr_info)
820 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
823 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
824 sizeof(*hdr_info)) < 0) {
827 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
830 if (hdr_info->device_info_struct_bytes <
831 sizeof(struct ultra_vbus_deviceinfo)) {
837 /* Write the contents of <info> to the struct
838 * spar_vbus_channel_protocol.chp_info.
840 * Returns void since this is debug information and not needed for
841 * device functionality.
845 write_vbus_chp_info(struct visorchannel *chan,
846 struct spar_vbus_headerinfo *hdr_info,
847 struct ultra_vbus_deviceinfo *info)
849 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
851 if (hdr_info->chp_info_offset == 0)
854 visorchannel_write(chan, off, info, sizeof(*info));
857 /* Write the contents of <info> to the struct
858 * spar_vbus_channel_protocol.bus_info.
860 * Returns void since this is debug information and not needed for
861 * device functionality.
865 write_vbus_bus_info(struct visorchannel *chan,
866 struct spar_vbus_headerinfo *hdr_info,
867 struct ultra_vbus_deviceinfo *info)
869 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
871 if (hdr_info->bus_info_offset == 0)
874 visorchannel_write(chan, off, info, sizeof(*info));
877 /* Write the contents of <info> to the
878 * struct spar_vbus_channel_protocol.dev_info[<devix>].
880 * Returns void since this is debug information and not needed for
881 * device functionality.
884 write_vbus_dev_info(struct visorchannel *chan,
885 struct spar_vbus_headerinfo *hdr_info,
886 struct ultra_vbus_deviceinfo *info, int devix)
889 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
890 (hdr_info->device_info_struct_bytes * devix);
892 if (hdr_info->dev_info_offset == 0)
895 visorchannel_write(chan, off, info, sizeof(*info));
898 /* For a child device just created on a client bus, fill in
899 * information about the driver that is controlling this device into
900 * the the appropriate slot within the vbus channel of the bus
904 fix_vbus_dev_info(struct visor_device *visordev)
907 struct visor_device *bdev;
908 struct visor_driver *visordrv;
909 int bus_no = visordev->chipset_bus_no;
910 int dev_no = visordev->chipset_dev_no;
911 struct ultra_vbus_deviceinfo dev_info;
912 const char *chan_type_name = NULL;
913 struct spar_vbus_headerinfo *hdr_info;
915 if (!visordev->device.driver)
918 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
922 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
926 visordrv = to_visor_driver(visordev->device.driver);
928 /* Within the list of device types (by GUID) that the driver
929 * says it supports, find out which one of those types matches
930 * the type of this device, so that we can include the device
933 for (i = 0; visordrv->channel_types[i].name; i++) {
934 if (memcmp(&visordrv->channel_types[i].guid,
935 &visordev->channel_type_guid,
936 sizeof(visordrv->channel_types[i].guid)) == 0) {
937 chan_type_name = visordrv->channel_types[i].name;
942 bus_device_info_init(&dev_info, chan_type_name,
943 visordrv->name, visordrv->version,
945 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
947 /* Re-write bus+chipset info, because it is possible that this
948 * was previously written by our evil counterpart, virtpci.
950 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
951 write_vbus_bus_info(bdev->visorchannel, hdr_info,
952 &clientbus_driverinfo);
955 /** Create a device instance for the visor bus itself.
958 create_bus_instance(struct visor_device *dev)
960 int id = dev->chipset_bus_no;
961 struct spar_vbus_headerinfo *hdr_info;
963 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
965 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
969 dev_set_name(&dev->device, "visorbus%d", id);
970 dev->device.bus = &visorbus_type;
971 dev->device.groups = visorbus_groups;
972 dev->device.release = visorbus_release_busdevice;
974 if (device_register(&dev->device) < 0) {
975 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
976 POSTCODE_SEVERITY_ERR);
981 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
982 dev->vbus_hdr_info = (void *)hdr_info;
983 write_vbus_chp_info(dev->visorchannel, hdr_info,
984 &chipset_driverinfo);
985 write_vbus_bus_info(dev->visorchannel, hdr_info,
986 &clientbus_driverinfo);
991 list_add_tail(&dev->list_all, &list_all_bus_instances);
992 dev_set_drvdata(&dev->device, dev);
996 /** Remove a device instance for the visor bus itself.
999 remove_bus_instance(struct visor_device *dev)
1001 /* Note that this will result in the release method for
1002 * dev->dev being called, which will call
1003 * visorbus_release_busdevice(). This has something to do with
1004 * the put_device() done in device_unregister(), but I have never
1005 * successfully been able to trace thru the code to see where/how
1006 * release() gets called. But I know it does.
1009 if (dev->visorchannel) {
1010 visorchannel_destroy(dev->visorchannel);
1011 dev->visorchannel = NULL;
1013 kfree(dev->vbus_hdr_info);
1014 list_del(&dev->list_all);
1015 device_unregister(&dev->device);
1018 /** Create and register the one-and-only one instance of
1019 * the visor bus type (visorbus_type).
1022 create_bus_type(void)
1024 busreg_rc = bus_register(&visorbus_type);
1028 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1031 remove_bus_type(void)
1033 bus_unregister(&visorbus_type);
1036 /** Remove all child visor bus device instances.
1039 remove_all_visor_devices(void)
1041 struct list_head *listentry, *listtmp;
1043 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1044 struct visor_device *dev = list_entry(listentry,
1045 struct visor_device,
1047 remove_visor_device(dev);
1052 chipset_bus_create(struct visor_device *dev)
1055 u32 bus_no = dev->chipset_bus_no;
1057 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1058 rc = create_bus_instance(dev);
1059 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1062 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1063 POSTCODE_SEVERITY_ERR);
1065 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1066 POSTCODE_SEVERITY_INFO);
1068 if (chipset_responders.bus_create)
1069 (*chipset_responders.bus_create) (dev, rc);
1073 chipset_bus_destroy(struct visor_device *dev)
1075 remove_bus_instance(dev);
1076 if (chipset_responders.bus_destroy)
1077 (*chipset_responders.bus_destroy)(dev, 0);
1081 chipset_device_create(struct visor_device *dev_info)
1084 u32 bus_no = dev_info->chipset_bus_no;
1085 u32 dev_no = dev_info->chipset_dev_no;
1087 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1088 POSTCODE_SEVERITY_INFO);
1090 rc = create_visor_device(dev_info);
1091 if (chipset_responders.device_create)
1092 chipset_responders.device_create(dev_info, rc);
1095 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1096 POSTCODE_SEVERITY_ERR);
1098 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1099 POSTCODE_SEVERITY_INFO);
1103 chipset_device_destroy(struct visor_device *dev_info)
1105 remove_visor_device(dev_info);
1107 if (chipset_responders.device_destroy)
1108 (*chipset_responders.device_destroy) (dev_info, 0);
1111 /* This is the callback function specified for a function driver, to
1112 * be called when a pending "pause device" operation has been
1116 pause_state_change_complete(struct visor_device *dev, int status)
1121 dev->pausing = false;
1122 if (!chipset_responders.device_pause) /* this can never happen! */
1125 /* Notify the chipset driver that the pause is complete, which
1126 * will presumably want to send some sort of response to the
1129 (*chipset_responders.device_pause) (dev, status);
1132 /* This is the callback function specified for a function driver, to
1133 * be called when a pending "resume device" operation has been
1137 resume_state_change_complete(struct visor_device *dev, int status)
1142 dev->resuming = false;
1143 if (!chipset_responders.device_resume) /* this can never happen! */
1146 /* Notify the chipset driver that the resume is complete,
1147 * which will presumably want to send some sort of response to
1150 (*chipset_responders.device_resume) (dev, status);
1153 /* Tell the subordinate function driver for a specific device to pause
1154 * or resume that device. Result is returned asynchronously via a
1155 * callback function.
1158 initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
1161 struct visor_driver *drv = NULL;
1162 void (*notify_func)(struct visor_device *dev, int response) = NULL;
1165 notify_func = chipset_responders.device_pause;
1167 notify_func = chipset_responders.device_resume;
1171 drv = to_visor_driver(dev->device.driver);
1173 (*notify_func)(dev, -ENODEV);
1177 if (dev->pausing || dev->resuming) {
1178 (*notify_func)(dev, -EBUSY);
1182 /* Note that even though both drv->pause() and drv->resume
1183 * specify a callback function, it is NOT necessary for us to
1184 * increment our local module usage count. Reason is, there
1185 * is already a linkage dependency between child function
1186 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1187 * visorbus while child function drivers are still running.
1191 (*notify_func)(dev, -EINVAL);
1195 dev->pausing = true;
1196 rc = drv->pause(dev, pause_state_change_complete);
1198 /* This should be done at BUS resume time, but an
1199 * existing problem prevents us from ever getting a bus
1200 * resume... This hack would fail to work should we
1201 * ever have a bus that contains NO devices, since we
1202 * would never even get here in that case.
1204 fix_vbus_dev_info(dev);
1206 (*notify_func)(dev, -EINVAL);
1210 dev->resuming = true;
1211 rc = drv->resume(dev, resume_state_change_complete);
1215 dev->pausing = false;
1217 dev->resuming = false;
1218 (*notify_func)(dev, -EINVAL);
1223 chipset_device_pause(struct visor_device *dev_info)
1225 initiate_chipset_device_pause_resume(dev_info, true);
1229 chipset_device_resume(struct visor_device *dev_info)
1231 initiate_chipset_device_pause_resume(dev_info, false);
1239 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, 0, POSTCODE_SEVERITY_INFO);
1240 bus_device_info_init(&clientbus_driverinfo,
1241 "clientbus", "visorbus",
1244 err = create_bus_type();
1246 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1250 /* This enables us to receive notifications when devices appear for
1251 * which this service partition is to be a server for.
1253 visorchipset_register_busdev(&chipset_notifiers,
1254 &chipset_responders,
1255 &chipset_driverinfo);
1260 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, err, POSTCODE_SEVERITY_ERR);
1267 struct list_head *listentry, *listtmp;
1269 visorchipset_register_busdev(NULL, NULL, NULL);
1270 remove_all_visor_devices();
1272 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1273 struct visor_device *dev = list_entry(listentry,
1274 struct visor_device,
1276 remove_bus_instance(dev);
1281 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1282 MODULE_PARM_DESC(visorbus_forcematch,
1283 "1 to force a successful dev <--> drv match");
1285 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1286 MODULE_PARM_DESC(visorbus_forcenomatch,
1287 "1 to force an UNsuccessful dev <--> drv match");