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 /* Display string that is guaranteed to be no longer the 99 characters*/
39 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
40 #define POLLJIFFIES_TESTWORK 100
41 #define POLLJIFFIES_NORMALCHANNEL 10
43 static int busreg_rc = -ENODEV; /* stores the result from bus registration */
45 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
46 static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
47 static void fix_vbus_dev_info(struct visor_device *visordev);
49 /* BUS type attributes
51 * define & implement display of bus attributes under
56 static ssize_t version_show(struct bus_type *bus, char *buf)
58 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
61 static BUS_ATTR_RO(version);
63 static struct attribute *visorbus_bus_attrs[] = {
64 &bus_attr_version.attr,
68 static const struct attribute_group visorbus_bus_group = {
69 .attrs = visorbus_bus_attrs,
72 static const struct attribute_group *visorbus_bus_groups[] = {
78 * DEVICE type attributes
80 * The modalias file will contain the guid of the device.
82 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
85 struct visor_device *vdev;
88 vdev = to_visor_device(dev);
89 guid = visorchannel_get_uuid(vdev->visorchannel);
90 return snprintf(buf, PAGE_SIZE, "visorbus:%pUl\n", &guid);
92 static DEVICE_ATTR_RO(modalias);
94 static struct attribute *visorbus_dev_attrs[] = {
95 &dev_attr_modalias.attr,
99 /* sysfs example for bridge-only sysfs files using device_type's */
100 static const struct attribute_group visorbus_dev_group = {
101 .attrs = visorbus_dev_attrs,
104 static const struct attribute_group *visorbus_dev_groups[] = {
109 /** This describes the TYPE of bus.
110 * (Don't confuse this with an INSTANCE of the bus.)
112 struct bus_type visorbus_type = {
114 .match = visorbus_match,
115 .uevent = visorbus_uevent,
116 .dev_groups = visorbus_dev_groups,
117 .bus_groups = visorbus_bus_groups,
120 static struct delayed_work periodic_work;
122 /* YES, we need 2 workqueues.
123 * The reason is, workitems on the test queue may need to cancel
124 * workitems on the other queue. You will be in for trouble if you try to
125 * do this with workitems queued on the same workqueue.
127 static struct workqueue_struct *periodic_test_workqueue;
128 static struct workqueue_struct *periodic_dev_workqueue;
129 static long long bus_count; /** number of bus instances */
130 /** ever-increasing */
132 static void chipset_bus_create(struct visor_device *bus_info);
133 static void chipset_bus_destroy(struct visor_device *bus_info);
134 static void chipset_device_create(struct visor_device *dev_info);
135 static void chipset_device_destroy(struct visor_device *dev_info);
136 static void chipset_device_pause(struct visor_device *dev_info);
137 static void chipset_device_resume(struct visor_device *dev_info);
139 /** These functions are implemented herein, and are called by the chipset
140 * driver to notify us about specific events.
142 static struct visorchipset_busdev_notifiers chipset_notifiers = {
143 .bus_create = chipset_bus_create,
144 .bus_destroy = chipset_bus_destroy,
145 .device_create = chipset_device_create,
146 .device_destroy = chipset_device_destroy,
147 .device_pause = chipset_device_pause,
148 .device_resume = chipset_device_resume,
151 /** These functions are implemented in the chipset driver, and we call them
152 * herein when we want to acknowledge a specific event.
154 static struct visorchipset_busdev_responders chipset_responders;
156 /* filled in with info about parent chipset driver when we register with it */
157 static struct ultra_vbus_deviceinfo chipset_driverinfo;
158 /* filled in with info about this driver, wrt it servicing client busses */
159 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
161 /** list of visor_device structs, linked via .list_all */
162 static LIST_HEAD(list_all_bus_instances);
163 /** list of visor_device structs, linked via .list_all */
164 static LIST_HEAD(list_all_device_instances);
167 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
169 struct visor_device *dev;
172 dev = to_visor_device(xdev);
173 guid = visorchannel_get_uuid(dev->visorchannel);
175 if (add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid))
180 /* This is called automatically upon adding a visor_device (device_add), or
181 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
182 * provided driver can control the specified device.
185 visorbus_match(struct device *xdev, struct device_driver *xdrv)
187 uuid_le channel_type;
189 struct visor_device *dev;
190 struct visor_driver *drv;
192 dev = to_visor_device(xdev);
193 drv = to_visor_driver(xdrv);
194 channel_type = visorchannel_get_uuid(dev->visorchannel);
196 if (visorbus_forcematch)
198 if (visorbus_forcenomatch)
200 if (!drv->channel_types)
204 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
205 (drv->channel_types[i].name);
207 if (uuid_le_cmp(drv->channel_types[i].guid,
214 /** This is called when device_unregister() is called for the bus device
215 * instance, after all other tasks involved with destroying the device
219 visorbus_release_busdevice(struct device *xdev)
221 struct visor_device *dev = dev_get_drvdata(xdev);
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 /* begin implementation of specific channel attributes to appear under
246 * /sys/bus/visorbus<x>/dev<y>/channel
248 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
251 struct visor_device *vdev = to_visor_device(dev);
253 if (!vdev->visorchannel)
255 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
256 visorchannel_get_physaddr(vdev->visorchannel));
259 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
262 struct visor_device *vdev = to_visor_device(dev);
264 if (!vdev->visorchannel)
266 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
267 visorchannel_get_nbytes(vdev->visorchannel));
270 static ssize_t clientpartition_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 struct visor_device *vdev = to_visor_device(dev);
275 if (!vdev->visorchannel)
277 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
278 visorchannel_get_clientpartition(vdev->visorchannel));
281 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
284 struct visor_device *vdev = to_visor_device(dev);
285 char typeid[LINESIZE];
287 if (!vdev->visorchannel)
289 return snprintf(buf, PAGE_SIZE, "%s\n",
290 visorchannel_id(vdev->visorchannel, typeid));
293 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
296 struct visor_device *vdev = to_visor_device(dev);
297 char zoneid[LINESIZE];
299 if (!vdev->visorchannel)
301 return snprintf(buf, PAGE_SIZE, "%s\n",
302 visorchannel_zoneid(vdev->visorchannel, zoneid));
305 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
308 struct visor_device *vdev = to_visor_device(dev);
310 struct bus_type *xbus = dev->bus;
311 struct device_driver *xdrv = dev->driver;
312 struct visor_driver *drv = NULL;
314 if (!vdev->visorchannel || !xbus || !xdrv)
316 i = xbus->match(dev, xdrv);
319 drv = to_visor_driver(xdrv);
320 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
323 static DEVICE_ATTR_RO(physaddr);
324 static DEVICE_ATTR_RO(nbytes);
325 static DEVICE_ATTR_RO(clientpartition);
326 static DEVICE_ATTR_RO(typeguid);
327 static DEVICE_ATTR_RO(zoneguid);
328 static DEVICE_ATTR_RO(typename);
330 static struct attribute *channel_attrs[] = {
331 &dev_attr_physaddr.attr,
332 &dev_attr_nbytes.attr,
333 &dev_attr_clientpartition.attr,
334 &dev_attr_typeguid.attr,
335 &dev_attr_zoneguid.attr,
336 &dev_attr_typename.attr,
340 static struct attribute_group channel_attr_grp = {
342 .attrs = channel_attrs,
345 static const struct attribute_group *visorbus_channel_groups[] = {
350 /* end implementation of specific channel attributes */
352 /* BUS instance attributes
354 * define & implement display of bus attributes under
355 * /sys/bus/visorbus/busses/visorbus<n>.
357 * This is a bit hoaky because the kernel does not yet have the infrastructure
358 * to separate bus INSTANCE attributes from bus TYPE attributes...
359 * so we roll our own. See businst.c / businst.h.
363 static ssize_t partition_handle_show(struct device *dev,
364 struct device_attribute *attr,
366 struct visor_device *vdev = to_visor_device(dev);
367 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
369 return snprintf(buf, PAGE_SIZE, "0x%llx\n", handle);
372 static ssize_t partition_guid_show(struct device *dev,
373 struct device_attribute *attr,
375 struct visor_device *vdev = to_visor_device(dev);
377 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
380 static ssize_t partition_name_show(struct device *dev,
381 struct device_attribute *attr,
383 struct visor_device *vdev = to_visor_device(dev);
385 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
388 static ssize_t channel_addr_show(struct device *dev,
389 struct device_attribute *attr,
391 struct visor_device *vdev = to_visor_device(dev);
392 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
394 return snprintf(buf, PAGE_SIZE, "0x%llx\n", addr);
397 static ssize_t channel_bytes_show(struct device *dev,
398 struct device_attribute *attr,
400 struct visor_device *vdev = to_visor_device(dev);
401 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
403 return snprintf(buf, PAGE_SIZE, "0x%llx\n", nbytes);
406 static ssize_t channel_id_show(struct device *dev,
407 struct device_attribute *attr,
409 struct visor_device *vdev = to_visor_device(dev);
412 if (vdev->visorchannel) {
413 visorchannel_id(vdev->visorchannel, buf);
420 static ssize_t client_bus_info_show(struct device *dev,
421 struct device_attribute *attr,
423 struct visor_device *vdev = to_visor_device(dev);
424 struct visorchannel *channel = vdev->visorchannel;
426 int i, shift, remain = PAGE_SIZE;
430 struct ultra_vbus_deviceinfo dev_info;
435 partition_name = vdev->name;
436 shift = snprintf(pos, remain,
437 "Client device / client driver info for %s eartition (vbus #%d):\n",
438 partition_name, vdev->chipset_dev_no);
441 shift = visorchannel_read(channel,
443 spar_vbus_channel_protocol,
445 &dev_info, sizeof(dev_info));
447 shift = vbuschannel_devinfo_to_string(&dev_info, pos,
452 shift = visorchannel_read(channel,
454 spar_vbus_channel_protocol,
456 &dev_info, sizeof(dev_info));
458 shift = vbuschannel_devinfo_to_string(&dev_info, pos,
463 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
465 while (off + sizeof(dev_info) <=
466 visorchannel_get_nbytes(channel)) {
467 shift = visorchannel_read(channel,
471 shift = vbuschannel_devinfo_to_string
472 (&dev_info, pos, remain, i);
476 off += sizeof(dev_info);
480 return PAGE_SIZE - remain;
483 static DEVICE_ATTR_RO(partition_handle);
484 static DEVICE_ATTR_RO(partition_guid);
485 static DEVICE_ATTR_RO(partition_name);
486 static DEVICE_ATTR_RO(channel_addr);
487 static DEVICE_ATTR_RO(channel_bytes);
488 static DEVICE_ATTR_RO(channel_id);
489 static DEVICE_ATTR_RO(client_bus_info);
491 static struct attribute *dev_attrs[] = {
492 &dev_attr_partition_handle.attr,
493 &dev_attr_partition_guid.attr,
494 &dev_attr_partition_name.attr,
495 &dev_attr_channel_addr.attr,
496 &dev_attr_channel_bytes.attr,
497 &dev_attr_channel_id.attr,
498 &dev_attr_client_bus_info.attr,
502 static struct attribute_group dev_attr_grp = {
506 static const struct attribute_group *visorbus_groups[] = {
513 * define & implement display of driver attributes under
514 * /sys/bus/visorbus/drivers/<drivername>.
519 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
521 struct visor_driver *drv = to_visor_driver(xdrv);
523 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
527 register_driver_attributes(struct visor_driver *drv)
529 struct driver_attribute version =
530 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
531 drv->version_attr = version;
532 return driver_create_file(&drv->driver, &drv->version_attr);
536 unregister_driver_attributes(struct visor_driver *drv)
538 driver_remove_file(&drv->driver, &drv->version_attr);
542 dev_periodic_work(void *xdev)
544 struct visor_device *dev = xdev;
545 struct visor_driver *drv = to_visor_driver(dev->device.driver);
547 down(&dev->visordriver_callback_lock);
548 if (drv->channel_interrupt)
549 drv->channel_interrupt(dev);
550 up(&dev->visordriver_callback_lock);
551 if (!visor_periodic_work_nextperiod(dev->periodic_work))
552 put_device(&dev->device);
556 dev_start_periodic_work(struct visor_device *dev)
558 if (dev->being_removed)
560 /* now up by at least 2 */
561 get_device(&dev->device);
562 if (!visor_periodic_work_start(dev->periodic_work))
563 put_device(&dev->device);
567 dev_stop_periodic_work(struct visor_device *dev)
569 if (visor_periodic_work_stop(dev->periodic_work))
570 put_device(&dev->device);
573 /** This is called automatically upon adding a visor_device (device_add), or
574 * adding a visor_driver (visorbus_register_visor_driver), but only after
575 * visorbus_match has returned 1 to indicate a successful match between
579 visordriver_probe_device(struct device *xdev)
582 struct visor_driver *drv;
583 struct visor_device *dev;
585 drv = to_visor_driver(xdev->driver);
586 dev = to_visor_device(xdev);
591 down(&dev->visordriver_callback_lock);
592 dev->being_removed = false;
594 res = drv->probe(dev);
596 /* success: reference kept via unmatched get_device() */
597 get_device(&dev->device);
598 fix_vbus_dev_info(dev);
601 up(&dev->visordriver_callback_lock);
605 /** This is called when device_unregister() is called for each child device
606 * instance, to notify the appropriate visorbus_driver that the device is
607 * going away, and to decrease the reference count of the device.
610 visordriver_remove_device(struct device *xdev)
612 struct visor_device *dev;
613 struct visor_driver *drv;
615 dev = to_visor_device(xdev);
616 drv = to_visor_driver(xdev->driver);
617 down(&dev->visordriver_callback_lock);
618 dev->being_removed = true;
621 up(&dev->visordriver_callback_lock);
622 dev_stop_periodic_work(dev);
624 put_device(&dev->device);
628 /** A particular type of visor driver calls this function to register
629 * the driver. The caller MUST fill in the following fields within the
631 * name, version, owner, channel_types, probe, remove
633 * Here's how the whole Linux bus / driver / device model works.
635 * At system start-up, the visorbus kernel module is loaded, which registers
636 * visorbus_type as a bus type, using bus_register().
638 * All kernel modules that support particular device types on a
639 * visorbus bus are loaded. Each of these kernel modules calls
640 * visorbus_register_visor_driver() in their init functions, passing a
641 * visor_driver struct. visorbus_register_visor_driver() in turn calls
642 * register_driver(&visor_driver.driver). This .driver member is
643 * initialized with generic methods (like probe), whose sole responsibility
644 * is to act as a broker for the real methods, which are within the
645 * visor_driver struct. (This is the way the subclass behavior is
646 * implemented, since visor_driver is essentially a subclass of the
647 * generic driver.) Whenever a driver_register() happens, core bus code in
648 * the kernel does (see device_attach() in drivers/base/dd.c):
650 * for each dev associated with the bus (the bus that driver is on) that
651 * does not yet have a driver
652 * if bus.match(dev,newdriver) == yes_matched ** .match specified
653 * ** during bus_register().
654 * newdriver.probe(dev) ** for visor drivers, this will call
655 * ** the generic driver.probe implemented in visorbus.c,
656 * ** which in turn calls the probe specified within the
657 * ** struct visor_driver (which was specified by the
658 * ** actual device driver as part of
659 * ** visorbus_register_visor_driver()).
661 * The above dance also happens when a new device appears.
662 * So the question is, how are devices created within the system?
663 * Basically, just call device_add(dev). See pci_bus_add_devices().
664 * pci_scan_device() shows an example of how to build a device struct. It
665 * returns the newly-created struct to pci_scan_single_device(), who adds it
666 * to the list of devices at PCIBUS.devices. That list of devices is what
667 * is traversed by pci_bus_add_devices().
670 int visorbus_register_visor_driver(struct visor_driver *drv)
675 return -ENODEV; /*can't register on a nonexistent bus*/
677 drv->driver.name = drv->name;
678 drv->driver.bus = &visorbus_type;
679 drv->driver.probe = visordriver_probe_device;
680 drv->driver.remove = visordriver_remove_device;
681 drv->driver.owner = drv->owner;
683 /* driver_register does this:
684 * bus_add_driver(drv)
685 * ->if (drv.bus) ** (bus_type) **
687 * for each dev with bus type of drv.bus
688 * if (!dev.drv) ** no driver assigned yet **
689 * if (bus.match(dev,drv)) [visorbus_match]
691 * if (!drv.probe(dev)) [visordriver_probe_device]
695 rc = driver_register(&drv->driver);
698 rc = register_driver_attributes(drv);
700 driver_unregister(&drv->driver);
703 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
705 /** A particular type of visor driver calls this function to unregister
706 * the driver, i.e., within its module_exit function.
709 visorbus_unregister_visor_driver(struct visor_driver *drv)
711 unregister_driver_attributes(drv);
712 driver_unregister(&drv->driver);
714 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
717 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
718 void *dest, unsigned long nbytes)
720 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
722 EXPORT_SYMBOL_GPL(visorbus_read_channel);
725 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
726 void *src, unsigned long nbytes)
728 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
730 EXPORT_SYMBOL_GPL(visorbus_write_channel);
733 visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
734 unsigned long nbytes)
736 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
738 EXPORT_SYMBOL_GPL(visorbus_clear_channel);
740 /** We don't really have a real interrupt, so for now we just call the
741 * interrupt function periodically...
744 visorbus_enable_channel_interrupts(struct visor_device *dev)
746 dev_start_periodic_work(dev);
748 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
751 visorbus_disable_channel_interrupts(struct visor_device *dev)
753 dev_stop_periodic_work(dev);
755 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
757 /** This is how everything starts from the device end.
758 * This function is called when a channel first appears via a ControlVM
759 * message. In response, this function allocates a visor_device to
760 * correspond to the new channel, and attempts to connect it the appropriate
761 * driver. If the appropriate driver is found, the visor_driver.probe()
762 * function for that driver will be called, and will be passed the new
763 * visor_device that we just created.
765 * It's ok if the appropriate driver is not yet loaded, because in that case
766 * the new device struct will just stick around in the bus' list of devices.
767 * When the appropriate driver calls visorbus_register_visor_driver(), the
768 * visor_driver.probe() for the new driver will be called with the new
772 create_visor_device(struct visor_device *dev)
775 u32 chipset_bus_no = dev->chipset_bus_no;
776 u32 chipset_dev_no = dev->chipset_dev_no;
778 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
779 POSTCODE_SEVERITY_INFO);
781 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
782 dev->device.bus = &visorbus_type;
783 dev->device.groups = visorbus_channel_groups;
784 device_initialize(&dev->device);
785 dev->device.release = visorbus_release_device;
786 /* keep a reference just for us (now 2) */
787 get_device(&dev->device);
789 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
790 periodic_dev_workqueue,
792 dev, dev_name(&dev->device));
793 if (!dev->periodic_work) {
794 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
800 /* bus_id must be a unique name with respect to this bus TYPE
801 * (NOT bus instance). That's why we need to include the bus
802 * number within the name.
804 dev_set_name(&dev->device, "vbus%u:dev%u",
805 chipset_bus_no, chipset_dev_no);
807 /* device_add does this:
808 * bus_add_device(dev)
809 * ->device_attach(dev)
810 * ->for each driver drv registered on the bus that dev is on
811 * if (dev.drv) ** device already has a driver **
812 * ** not sure we could ever get here... **
814 * if (bus.match(dev,drv)) [visorbus_match]
816 * if (!drv.probe(dev)) [visordriver_probe_device]
819 * Note that device_add does NOT fail if no driver failed to
820 * claim the device. The device will be linked onto
821 * bus_type.klist_devices regardless (use bus_for_each_dev).
823 err = device_add(&dev->device);
825 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
830 list_add_tail(&dev->list_all, &list_all_device_instances);
831 return 0; /* success: reference kept via unmatched get_device() */
834 put_device(&dev->device);
839 remove_visor_device(struct visor_device *dev)
841 list_del(&dev->list_all);
842 put_device(&dev->device);
843 device_unregister(&dev->device);
847 get_vbus_header_info(struct visorchannel *chan,
848 struct spar_vbus_headerinfo *hdr_info)
850 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
853 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
854 sizeof(*hdr_info)) < 0) {
857 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
860 if (hdr_info->device_info_struct_bytes <
861 sizeof(struct ultra_vbus_deviceinfo)) {
867 /* Write the contents of <info> to the struct
868 * spar_vbus_channel_protocol.chp_info.
872 write_vbus_chp_info(struct visorchannel *chan,
873 struct spar_vbus_headerinfo *hdr_info,
874 struct ultra_vbus_deviceinfo *info)
876 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
878 if (hdr_info->chp_info_offset == 0)
881 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
886 /* Write the contents of <info> to the struct
887 * spar_vbus_channel_protocol.bus_info.
891 write_vbus_bus_info(struct visorchannel *chan,
892 struct spar_vbus_headerinfo *hdr_info,
893 struct ultra_vbus_deviceinfo *info)
895 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
897 if (hdr_info->bus_info_offset == 0)
900 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
905 /* Write the contents of <info> to the
906 * struct spar_vbus_channel_protocol.dev_info[<devix>].
909 write_vbus_dev_info(struct visorchannel *chan,
910 struct spar_vbus_headerinfo *hdr_info,
911 struct ultra_vbus_deviceinfo *info, int devix)
914 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
915 (hdr_info->device_info_struct_bytes * devix);
917 if (hdr_info->dev_info_offset == 0)
920 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
925 /* For a child device just created on a client bus, fill in
926 * information about the driver that is controlling this device into
927 * the the appropriate slot within the vbus channel of the bus
931 fix_vbus_dev_info(struct visor_device *visordev)
934 struct visor_device *bdev;
935 struct visor_driver *visordrv;
936 int bus_no = visordev->chipset_bus_no;
937 int dev_no = visordev->chipset_dev_no;
938 struct ultra_vbus_deviceinfo dev_info;
939 const char *chan_type_name = NULL;
940 struct spar_vbus_headerinfo *hdr_info;
942 if (!visordev->device.driver)
945 hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
949 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
953 visordrv = to_visor_driver(visordev->device.driver);
955 /* Within the list of device types (by GUID) that the driver
956 * says it supports, find out which one of those types matches
957 * the type of this device, so that we can include the device
960 for (i = 0; visordrv->channel_types[i].name; i++) {
961 if (memcmp(&visordrv->channel_types[i].guid,
962 &visordev->channel_type_guid,
963 sizeof(visordrv->channel_types[i].guid)) == 0) {
964 chan_type_name = visordrv->channel_types[i].name;
969 bus_device_info_init(&dev_info, chan_type_name,
970 visordrv->name, visordrv->version,
972 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
974 /* Re-write bus+chipset info, because it is possible that this
975 * was previously written by our evil counterpart, virtpci.
977 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
978 write_vbus_bus_info(bdev->visorchannel, hdr_info,
979 &clientbus_driverinfo);
982 /** Create a device instance for the visor bus itself.
985 create_bus_instance(struct visor_device *dev)
987 int id = dev->chipset_bus_no;
988 struct spar_vbus_headerinfo *hdr_info;
990 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
992 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
996 dev_set_name(&dev->device, "visorbus%d", id);
997 dev->device.bus = &visorbus_type;
998 dev->device.groups = visorbus_groups;
999 dev->device.release = visorbus_release_busdevice;
1001 if (device_register(&dev->device) < 0) {
1002 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1003 POSTCODE_SEVERITY_ERR);
1008 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1009 dev->vbus_hdr_info = (void *)hdr_info;
1010 write_vbus_chp_info(dev->visorchannel, hdr_info,
1011 &chipset_driverinfo);
1012 write_vbus_bus_info(dev->visorchannel, hdr_info,
1013 &clientbus_driverinfo);
1018 list_add_tail(&dev->list_all, &list_all_bus_instances);
1019 dev_set_drvdata(&dev->device, dev);
1023 /** Remove a device instance for the visor bus itself.
1026 remove_bus_instance(struct visor_device *dev)
1028 /* Note that this will result in the release method for
1029 * dev->dev being called, which will call
1030 * visorbus_release_busdevice(). This has something to do with
1031 * the put_device() done in device_unregister(), but I have never
1032 * successfully been able to trace thru the code to see where/how
1033 * release() gets called. But I know it does.
1036 if (dev->visorchannel) {
1037 visorchannel_destroy(dev->visorchannel);
1038 dev->visorchannel = NULL;
1040 kfree(dev->vbus_hdr_info);
1041 list_del(&dev->list_all);
1042 device_unregister(&dev->device);
1045 /** Create and register the one-and-only one instance of
1046 * the visor bus type (visorbus_type).
1049 create_bus_type(void)
1051 busreg_rc = bus_register(&visorbus_type);
1055 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1058 remove_bus_type(void)
1060 bus_unregister(&visorbus_type);
1063 /** Remove all child visor bus device instances.
1066 remove_all_visor_devices(void)
1068 struct list_head *listentry, *listtmp;
1070 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1071 struct visor_device *dev = list_entry(listentry,
1072 struct visor_device,
1074 remove_visor_device(dev);
1079 chipset_bus_create(struct visor_device *dev)
1082 u32 bus_no = dev->chipset_bus_no;
1084 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1085 rc = create_bus_instance(dev);
1086 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1089 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1090 POSTCODE_SEVERITY_ERR);
1092 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1093 POSTCODE_SEVERITY_INFO);
1095 if (chipset_responders.bus_create)
1096 (*chipset_responders.bus_create) (dev, rc);
1100 chipset_bus_destroy(struct visor_device *dev)
1102 remove_bus_instance(dev);
1103 if (chipset_responders.bus_destroy)
1104 (*chipset_responders.bus_destroy)(dev, 0);
1108 chipset_device_create(struct visor_device *dev_info)
1111 u32 bus_no = dev_info->chipset_bus_no;
1112 u32 dev_no = dev_info->chipset_dev_no;
1114 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1115 POSTCODE_SEVERITY_INFO);
1117 rc = create_visor_device(dev_info);
1118 if (chipset_responders.device_create)
1119 chipset_responders.device_create(dev_info, rc);
1122 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1123 POSTCODE_SEVERITY_ERR);
1125 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1126 POSTCODE_SEVERITY_INFO);
1130 chipset_device_destroy(struct visor_device *dev_info)
1132 remove_visor_device(dev_info);
1134 if (chipset_responders.device_destroy)
1135 (*chipset_responders.device_destroy) (dev_info, 0);
1138 /* This is the callback function specified for a function driver, to
1139 * be called when a pending "pause device" operation has been
1143 pause_state_change_complete(struct visor_device *dev, int status)
1148 dev->pausing = false;
1149 if (!chipset_responders.device_pause) /* this can never happen! */
1152 /* Notify the chipset driver that the pause is complete, which
1153 * will presumably want to send some sort of response to the
1156 (*chipset_responders.device_pause) (dev, status);
1159 /* This is the callback function specified for a function driver, to
1160 * be called when a pending "resume device" operation has been
1164 resume_state_change_complete(struct visor_device *dev, int status)
1169 dev->resuming = false;
1170 if (!chipset_responders.device_resume) /* this can never happen! */
1173 /* Notify the chipset driver that the resume is complete,
1174 * which will presumably want to send some sort of response to
1177 (*chipset_responders.device_resume) (dev, status);
1180 /* Tell the subordinate function driver for a specific device to pause
1181 * or resume that device. Result is returned asynchronously via a
1182 * callback function.
1185 initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
1188 struct visor_driver *drv = NULL;
1189 void (*notify_func)(struct visor_device *dev, int response) = NULL;
1192 notify_func = chipset_responders.device_pause;
1194 notify_func = chipset_responders.device_resume;
1198 drv = to_visor_driver(dev->device.driver);
1200 (*notify_func)(dev, -ENODEV);
1204 if (dev->pausing || dev->resuming) {
1205 (*notify_func)(dev, -EBUSY);
1209 /* Note that even though both drv->pause() and drv->resume
1210 * specify a callback function, it is NOT necessary for us to
1211 * increment our local module usage count. Reason is, there
1212 * is already a linkage dependency between child function
1213 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1214 * visorbus while child function drivers are still running.
1218 (*notify_func)(dev, -EINVAL);
1222 dev->pausing = true;
1223 rc = drv->pause(dev, pause_state_change_complete);
1225 /* This should be done at BUS resume time, but an
1226 * existing problem prevents us from ever getting a bus
1227 * resume... This hack would fail to work should we
1228 * ever have a bus that contains NO devices, since we
1229 * would never even get here in that case.
1231 fix_vbus_dev_info(dev);
1233 (*notify_func)(dev, -EINVAL);
1237 dev->resuming = true;
1238 rc = drv->resume(dev, resume_state_change_complete);
1242 dev->pausing = false;
1244 dev->resuming = false;
1245 (*notify_func)(dev, -EINVAL);
1250 chipset_device_pause(struct visor_device *dev_info)
1252 initiate_chipset_device_pause_resume(dev_info, true);
1256 chipset_device_resume(struct visor_device *dev_info)
1258 initiate_chipset_device_pause_resume(dev_info, false);
1261 struct channel_size_info {
1263 unsigned long min_size;
1264 unsigned long max_size;
1272 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, 0, POSTCODE_SEVERITY_INFO);
1273 bus_device_info_init(&clientbus_driverinfo,
1274 "clientbus", "visorbus",
1277 err = create_bus_type();
1279 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1283 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1284 if (!periodic_dev_workqueue) {
1285 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
1290 /* This enables us to receive notifications when devices appear for
1291 * which this service partition is to be a server for.
1293 visorchipset_register_busdev(&chipset_notifiers,
1294 &chipset_responders,
1295 &chipset_driverinfo);
1300 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, err, POSTCODE_SEVERITY_ERR);
1307 struct list_head *listentry, *listtmp;
1309 visorchipset_register_busdev(NULL, NULL, NULL);
1310 remove_all_visor_devices();
1312 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1313 destroy_workqueue(periodic_dev_workqueue);
1314 periodic_dev_workqueue = NULL;
1316 if (periodic_test_workqueue) {
1317 cancel_delayed_work(&periodic_work);
1318 flush_workqueue(periodic_test_workqueue);
1319 destroy_workqueue(periodic_test_workqueue);
1320 periodic_test_workqueue = NULL;
1323 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1324 struct visor_device *dev = list_entry(listentry,
1325 struct visor_device,
1327 remove_bus_instance(dev);
1332 module_param_named(debug, visorbus_debug, int, S_IRUGO);
1333 MODULE_PARM_DESC(visorbus_debug, "1 to debug");
1335 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1336 MODULE_PARM_DESC(visorbus_forcematch,
1337 "1 to force a successful dev <--> drv match");
1339 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1340 MODULE_PARM_DESC(visorbus_forcenomatch,
1341 "1 to force an UNsuccessful dev <--> drv match");
1343 module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1344 MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");