3 * Copyright � 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
18 #include <linux/uuid.h>
21 #include "visorbus_private.h"
23 #include "periodic_work.h"
24 #include "vbuschannel.h"
25 #include "guestlinuxdebug.h"
26 #include "vbusdeviceinfo.h"
28 #define MYDRVNAME "visorbus"
30 /* module parameters */
32 int visorbus_forcematch;
33 int visorbus_forcenomatch;
34 #define MAXDEVICETEST 4
35 int visorbus_devicetest;
36 int visorbus_debugref;
37 #define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
39 /** This is the private data that we store for each bus device instance.
41 struct visorbus_devdata {
42 int devno; /* this is the chipset busNo */
43 struct list_head list_all;
46 struct visorchannel *chan; /* channel area for bus itself */
48 struct spar_vbus_headerinfo vbus_hdr_info;
51 /* These forward declarations are required since our drivers are out-of-tree.
52 * The structures referenced are kernel-private and are not in the headers, but
53 * it is impossible to make a functioning bus driver without them.
55 struct subsys_private {
57 struct kset *devices_kset;
59 struct kset *drivers_kset;
60 struct klist klist_devices;
61 struct klist klist_drivers;
62 struct blocking_notifier_head bus_notifier;
63 unsigned int drivers_autoprobe:1;
66 struct list_head class_interfaces;
67 struct kset glue_dirs;
68 struct mutex class_mutex; /* ignore */
72 struct bus_type_private {
74 struct kset *drivers_kset;
75 struct kset *devices_kset;
76 struct klist klist_devices;
77 struct klist klist_drivers;
78 struct blocking_notifier_head bus_notifier;
79 unsigned int drivers_autoprobe:1;
83 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
84 #define POLLJIFFIES_TESTWORK 100
85 #define POLLJIFFIES_NORMALCHANNEL 10
87 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
88 static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
89 static void fix_vbus_dev_info(struct visor_device *visordev);
91 /** This describes the TYPE of bus.
92 * (Don't confuse this with an INSTANCE of the bus.)
94 static struct bus_type visorbus_type = {
96 .match = visorbus_match,
97 .uevent = visorbus_uevent,
100 static struct delayed_work periodic_work;
102 /* YES, we need 2 workqueues.
103 * The reason is, workitems on the test queue may need to cancel
104 * workitems on the other queue. You will be in for trouble if you try to
105 * do this with workitems queued on the same workqueue.
107 static struct workqueue_struct *periodic_test_workqueue;
108 static struct workqueue_struct *periodic_dev_workqueue;
109 static long long bus_count; /** number of bus instances */
110 static long long total_devices_created;
111 /** ever-increasing */
113 static void chipset_bus_create(u32 bus_no);
114 static void chipset_bus_destroy(u32 bus_no);
115 static void chipset_device_create(u32 bus_no, u32 dev_no);
116 static void chipset_device_destroy(u32 bus_no, u32 dev_no);
117 static void chipset_device_pause(u32 bus_no, u32 dev_no);
118 static void chipset_device_resume(u32 bus_no, u32 dev_no);
120 /** These functions are implemented herein, and are called by the chipset
121 * driver to notify us about specific events.
123 static struct visorchipset_busdev_notifiers chipset_notifiers = {
124 .bus_create = chipset_bus_create,
125 .bus_destroy = chipset_bus_destroy,
126 .device_create = chipset_device_create,
127 .device_destroy = chipset_device_destroy,
128 .device_pause = chipset_device_pause,
129 .device_resume = chipset_device_resume,
132 /** These functions are implemented in the chipset driver, and we call them
133 * herein when we want to acknowledge a specific event.
135 static struct visorchipset_busdev_responders chipset_responders;
137 /* filled in with info about parent chipset driver when we register with it */
138 static struct ultra_vbus_deviceinfo chipset_driverinfo;
139 /* filled in with info about this driver, wrt it servicing client busses */
140 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
142 /** list of visorbus_devdata structs, linked via .list_all */
143 static LIST_HEAD(list_all_bus_instances);
144 /** list of visor_device structs, linked via .list_all */
145 static LIST_HEAD(list_all_device_instances);
148 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
150 if (add_uevent_var(env, "VERSION=%s", VERSION))
155 /* This is called automatically upon adding a visor_device (device_add), or
156 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
157 * provided driver can control the specified device.
160 visorbus_match(struct device *xdev, struct device_driver *xdrv)
162 uuid_le channel_type;
165 struct visor_device *dev;
166 struct visor_driver *drv;
168 dev = to_visor_device(xdev);
169 drv = to_visor_driver(xdrv);
170 channel_type = visorchannel_get_uuid(dev->visorchannel);
171 if (visorbus_forcematch) {
175 if (visorbus_forcenomatch)
178 if (!drv->channel_types)
181 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
182 (drv->channel_types[i].name);
184 if (uuid_le_cmp(drv->channel_types[i].guid,
185 channel_type) == 0) {
193 /** This is called when device_unregister() is called for the bus device
194 * instance, after all other tasks involved with destroying the device
198 visorbus_release_busdevice(struct device *xdev)
200 struct visorbus_devdata *devdata = dev_get_drvdata(xdev);
202 dev_set_drvdata(xdev, NULL);
207 /** This is called when device_unregister() is called for each child
211 visorbus_release_device(struct device *xdev)
213 struct visor_device *dev = to_visor_device(xdev);
215 if (dev->periodic_work) {
216 visor_periodic_work_destroy(dev->periodic_work);
217 dev->periodic_work = NULL;
219 if (dev->visorchannel) {
220 visorchannel_destroy(dev->visorchannel);
221 dev->visorchannel = NULL;
226 /* Implement publishing of device node attributes under:
228 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
232 #define to_devmajorminor_attr(_attr) \
233 container_of(_attr, struct devmajorminor_attribute, attr)
234 #define to_visor_device_from_kobjdevmajorminor(obj) \
235 container_of(obj, struct visor_device, kobjdevmajorminor)
237 struct devmajorminor_attribute {
238 struct attribute attr;
240 ssize_t (*show)(struct visor_device *, int slot, char *buf);
241 ssize_t (*store)(struct visor_device *, int slot, const char *buf,
245 static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
247 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
249 if (slot < 0 || slot >= maxdevnodes)
251 return snprintf(buf, PAGE_SIZE, "%d:%d\n",
252 dev->devnodes[slot].major, dev->devnodes[slot].minor);
256 devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
258 struct devmajorminor_attribute *devmajorminor_attr =
259 to_devmajorminor_attr(attr);
260 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
263 if (devmajorminor_attr->show)
264 ret = devmajorminor_attr->show(dev,
265 devmajorminor_attr->slot, buf);
270 devmajorminor_attr_store(struct kobject *kobj,
271 struct attribute *attr, const char *buf, size_t count)
273 struct devmajorminor_attribute *devmajorminor_attr =
274 to_devmajorminor_attr(attr);
275 struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
278 if (devmajorminor_attr->store)
279 ret = devmajorminor_attr->store(dev,
280 devmajorminor_attr->slot,
285 static int register_devmajorminor_attributes(struct visor_device *dev);
288 devmajorminor_create_file(struct visor_device *dev, const char *name,
289 int major, int minor)
291 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
292 struct devmajorminor_attribute *myattr = NULL;
293 int x = -1, rc = 0, slot = -1;
295 register_devmajorminor_attributes(dev);
296 for (slot = 0; slot < maxdevnodes; slot++)
297 if (!dev->devnodes[slot].attr)
299 if (slot == maxdevnodes) {
303 myattr = kmalloc(sizeof(*myattr), GFP_KERNEL);
308 memset(myattr, 0, sizeof(struct devmajorminor_attribute));
309 myattr->show = DEVMAJORMINOR_ATTR;
310 myattr->store = NULL;
312 myattr->attr.name = name;
313 myattr->attr.mode = S_IRUGO;
314 dev->devnodes[slot].attr = myattr;
315 dev->devnodes[slot].major = major;
316 dev->devnodes[slot].minor = minor;
317 x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
322 kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
327 dev->devnodes[slot].attr = NULL;
333 devmajorminor_remove_file(struct visor_device *dev, int slot)
335 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
336 struct devmajorminor_attribute *myattr = NULL;
338 if (slot < 0 || slot >= maxdevnodes)
340 myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
343 sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
344 kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
345 dev->devnodes[slot].attr = NULL;
350 devmajorminor_remove_all_files(struct visor_device *dev)
353 int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
355 for (i = 0; i < maxdevnodes; i++)
356 devmajorminor_remove_file(dev, i);
359 static const struct sysfs_ops devmajorminor_sysfs_ops = {
360 .show = devmajorminor_attr_show,
361 .store = devmajorminor_attr_store,
364 static struct kobj_type devmajorminor_kobj_type = {
365 .sysfs_ops = &devmajorminor_sysfs_ops
369 register_devmajorminor_attributes(struct visor_device *dev)
373 if (dev->kobjdevmajorminor.parent)
374 goto away; /* already registered */
375 x = kobject_init_and_add(&dev->kobjdevmajorminor,
376 &devmajorminor_kobj_type, &dev->device.kobj,
383 kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
390 unregister_devmajorminor_attributes(struct visor_device *dev)
392 if (!dev->kobjdevmajorminor.parent)
393 return; /* already unregistered */
394 devmajorminor_remove_all_files(dev);
396 kobject_del(&dev->kobjdevmajorminor);
397 kobject_put(&dev->kobjdevmajorminor);
398 dev->kobjdevmajorminor.parent = NULL;
401 /* Implement publishing of channel attributes under:
403 * /sys/bus/visorbus<x>/dev<y>/channel
407 #define to_channel_attr(_attr) \
408 container_of(_attr, struct channel_attribute, attr)
409 #define to_visor_device_from_kobjchannel(obj) \
410 container_of(obj, struct visor_device, kobjchannel)
412 struct channel_attribute {
413 struct attribute attr;
414 ssize_t (*show)(struct visor_device*, char *buf);
415 ssize_t (*store)(struct visor_device*, const char *buf, size_t count);
418 /* begin implementation of specific channel attributes to appear under
419 * /sys/bus/visorbus<x>/dev<y>/channel
421 static ssize_t devicechannel_attr_physaddr(struct visor_device *dev, char *buf)
423 if (!dev->visorchannel)
425 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
426 visorchannel_get_physaddr(dev->visorchannel));
429 static ssize_t devicechannel_attr_nbytes(struct visor_device *dev, char *buf)
431 if (!dev->visorchannel)
433 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
434 visorchannel_get_nbytes(dev->visorchannel));
437 static ssize_t devicechannel_attr_clientpartition(struct visor_device *dev,
439 if (!dev->visorchannel)
441 return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
442 visorchannel_get_clientpartition(dev->visorchannel));
445 static ssize_t devicechannel_attr_typeguid(struct visor_device *dev, char *buf)
449 if (!dev->visorchannel)
451 return snprintf(buf, PAGE_SIZE, "%s\n",
452 visorchannel_id(dev->visorchannel, s));
455 static ssize_t devicechannel_attr_zoneguid(struct visor_device *dev, char *buf)
459 if (!dev->visorchannel)
461 return snprintf(buf, PAGE_SIZE, "%s\n",
462 visorchannel_zoneid(dev->visorchannel, s));
465 static ssize_t devicechannel_attr_typename(struct visor_device *dev, char *buf)
468 struct bus_type *xbus = dev->device.bus;
469 struct device_driver *xdrv = dev->device.driver;
470 struct visor_driver *drv = NULL;
472 if (!dev->visorchannel || !xbus || !xdrv)
474 i = xbus->match(&dev->device, xdrv);
477 drv = to_visor_driver(xdrv);
478 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
481 static ssize_t devicechannel_attr_dump(struct visor_device *dev, char *buf)
484 /* TODO: replace this with debugfs code
485 struct seq_file *m = NULL;
486 if (dev->visorchannel == NULL)
488 m = visor_seq_file_new_buffer(buf, PAGE_SIZE - 1);
491 visorchannel_debug(dev->visorchannel, 1, m, 0);
493 visor_seq_file_done_buffer(m);
499 static struct channel_attribute all_channel_attrs[] = {
500 __ATTR(physaddr, S_IRUGO,
501 devicechannel_attr_physaddr, NULL),
502 __ATTR(nbytes, S_IRUGO,
503 devicechannel_attr_nbytes, NULL),
504 __ATTR(clientpartition, S_IRUGO,
505 devicechannel_attr_clientpartition, NULL),
506 __ATTR(typeguid, S_IRUGO,
507 devicechannel_attr_typeguid, NULL),
508 __ATTR(zoneguid, S_IRUGO,
509 devicechannel_attr_zoneguid, NULL),
510 __ATTR(typename, S_IRUGO,
511 devicechannel_attr_typename, NULL),
512 __ATTR(dump, S_IRUGO,
513 devicechannel_attr_dump, NULL),
516 /* end implementation of specific channel attributes */
518 static ssize_t channel_attr_show(struct kobject *kobj, struct attribute *attr,
521 struct channel_attribute *channel_attr = to_channel_attr(attr);
522 struct visor_device *dev = to_visor_device_from_kobjchannel(kobj);
525 if (channel_attr->show)
526 ret = channel_attr->show(dev, buf);
530 static ssize_t channel_attr_store(struct kobject *kobj, struct attribute *attr,
531 const char *buf, size_t count)
533 struct channel_attribute *channel_attr = to_channel_attr(attr);
534 struct visor_device *dev = to_visor_device_from_kobjchannel(kobj);
537 if (channel_attr->store)
538 ret = channel_attr->store(dev, buf, count);
542 static int channel_create_file(struct visor_device *dev,
543 struct channel_attribute *attr)
545 return sysfs_create_file(&dev->kobjchannel, &attr->attr);
548 static void channel_remove_file(struct visor_device *dev,
549 struct channel_attribute *attr)
551 sysfs_remove_file(&dev->kobjchannel, &attr->attr);
554 static const struct sysfs_ops channel_sysfs_ops = {
555 .show = channel_attr_show,
556 .store = channel_attr_store,
559 static struct kobj_type channel_kobj_type = {
560 .sysfs_ops = &channel_sysfs_ops
563 int register_channel_attributes(struct visor_device *dev)
565 int rc = 0, i = 0, x = 0;
567 if (dev->kobjchannel.parent)
568 goto away; /* already registered */
569 x = kobject_init_and_add(&dev->kobjchannel, &channel_kobj_type,
570 &dev->device.kobj, "channel");
576 kobject_uevent(&dev->kobjchannel, KOBJ_ADD);
579 i < sizeof(all_channel_attrs) / sizeof(struct channel_attribute);
581 x = channel_create_file(dev, &all_channel_attrs[i]);
584 channel_remove_file(dev, &all_channel_attrs[i]);
585 kobject_del(&dev->kobjchannel);
586 kobject_put(&dev->kobjchannel);
594 void unregister_channel_attributes(struct visor_device *dev)
598 if (!dev->kobjchannel.parent)
599 return; /* already unregistered */
601 i < sizeof(all_channel_attrs) / sizeof(struct channel_attribute);
603 channel_remove_file(dev, &all_channel_attrs[i]);
605 kobject_del(&dev->kobjchannel);
606 kobject_put(&dev->kobjchannel);
607 dev->kobjchannel.parent = NULL;
609 /* This is actually something they forgot to put in the kernel.
610 * struct bus_type in the kernel SHOULD have a "busses" member, which
611 * should be treated similarly to the "devices" and "drivers" members.
613 * - a "businst_attribute" analogous to the existing "bus_attribute"
614 * - a "businst_create_file" and "businst_remove_file" analogous to the
615 * existing "bus_create_file" and "bus_remove_file".
616 * That's what I created businst.c and businst.h to do.
618 * We want to add the "busses" sub-tree in sysfs, where we will house the
619 * names and properties of each bus instance:
621 * /sys/bus/<bustypename>/
624 * <devname1> --> /sys/devices/<businstancename><devname1>
625 * <devname2> --> /sys/devices/<businstancename><devname2>
627 * <driverinstancename1>
628 * <driverinstance1property1>
629 * <driverinstance1property2>
631 * <driverinstancename2>
632 * <driverinstance2property1>
633 * <driverinstance2property2>
636 * >> <businstancename1>
637 * >> <businstance1property1>
638 * >> <businstance1property2>
640 * >> <businstancename2>
641 * >> <businstance2property1>
642 * >> <businstance2property2>
645 * I considered adding bus instance properties under
646 * /sys/devices/<businstancename>. But I thought there may be existing
647 * notions that ONLY device sub-trees should live under
648 * /sys/devices/<businstancename>. So I stayed out of there.
652 struct businst_attribute {
653 struct attribute attr;
654 ssize_t (*show)(struct visorbus_devdata*, char *buf);
655 ssize_t (*store)(struct visorbus_devdata*, const char *buf,
659 #define to_businst_attr(_attr) \
660 container_of(_attr, struct businst_attribute, attr)
661 #define to_visorbus_devdata(obj) \
662 container_of(obj, struct visorbus_devdata, kobj)
665 businst_attr_show(struct kobject *kobj, struct attribute *attr,
668 struct businst_attribute *businst_attr = to_businst_attr(attr);
669 struct visorbus_devdata *bus = to_visorbus_devdata(kobj);
672 if (businst_attr->show)
673 ret = businst_attr->show(bus, buf);
678 businst_attr_store(struct kobject *kobj, struct attribute *attr,
679 const char *buf, size_t count)
681 struct businst_attribute *businst_attr = to_businst_attr(attr);
682 struct visorbus_devdata *bus = to_visorbus_devdata(kobj);
685 if (businst_attr->store)
686 ret = businst_attr->store(bus, buf, count);
691 businst_create_file(struct visorbus_devdata *bus,
692 struct businst_attribute *attr)
694 return sysfs_create_file(&bus->kobj, &attr->attr);
698 businst_remove_file(struct visorbus_devdata *bus,
699 struct businst_attribute *attr)
701 sysfs_remove_file(&bus->kobj, &attr->attr);
704 static const struct sysfs_ops businst_sysfs_ops = {
705 .show = businst_attr_show,
706 .store = businst_attr_store,
709 static struct kobj_type businst_kobj_type = {
710 .sysfs_ops = &businst_sysfs_ops
713 static struct kset businstances = { /* should actually be a member of
717 /* BUS type attributes
719 * define & implement display of bus attributes under
725 BUSTYPE_ATTR_version(struct bus_type *bus, char *buf)
727 return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
730 static struct bus_attribute bustype_attr_version =
731 __ATTR(version, S_IRUGO, BUSTYPE_ATTR_version, NULL);
734 register_bustype_attributes(void)
738 rc = bus_create_file(&visorbus_type, &bustype_attr_version);
742 /* Here we make up for the fact that bus_type does not yet have a
743 * member to keep track of multiple bus instances for a given bus
744 * type. This is useful for stashing properties for each bus
747 kobject_set_name(&businstances.kobj, "busses");
748 businstances.kobj.ktype = &businst_kobj_type;
749 businstances.kobj.parent = &visorbus_type.p->subsys.kobj;
750 rc = kset_register(&businstances);
760 unregister_bustype_attributes(void)
762 bus_remove_file(&visorbus_type, &bustype_attr_version);
763 kset_unregister(&businstances);
766 /* BUS instance attributes
768 * define & implement display of bus attributes under
769 * /sys/bus/visorbus/busses/visorbus<n>.
771 * This is a bit hoaky because the kernel does not yet have the infrastructure
772 * to separate bus INSTANCE attributes from bus TYPE attributes...
773 * so we roll our own. See businst.c / businst.h.
777 static ssize_t businst_attr_partition_handle(struct visorbus_devdata *businst,
779 struct visorchipset_bus_info bus_info;
782 if (businst && visorchipset_get_bus_info(businst->devno, &bus_info))
783 len = snprintf(buf, PAGE_SIZE,
785 (unsigned long long)bus_info.partition_handle);
789 static ssize_t businst_attr_partition_guid(struct visorbus_devdata *businst,
791 struct visorchipset_bus_info bus_info;
794 if (businst && visorchipset_get_bus_info(businst->devno, &bus_info))
795 len = snprintf(buf, PAGE_SIZE, "{%pUb}\n",
796 &bus_info.partition_uuid);
800 static ssize_t businst_attr_partition_name(struct visorbus_devdata *businst,
802 struct visorchipset_bus_info bus_info;
806 visorchipset_get_bus_info(businst->devno, &bus_info) &&
808 len = snprintf(buf, PAGE_SIZE, "%s\n", bus_info.name);
812 static ssize_t businst_attr_channel_addr(struct visorbus_devdata *businst,
814 struct visorchipset_bus_info bus_info;
817 if (businst && visorchipset_get_bus_info(businst->devno, &bus_info))
818 len = snprintf(buf, PAGE_SIZE, "0x%Lx\n", (unsigned long long)
819 bus_info.chan_info.channel_addr);
823 static ssize_t businst_attr_nchannel_bytes(struct visorbus_devdata *businst,
825 struct visorchipset_bus_info bus_info;
828 if (businst && visorchipset_get_bus_info(businst->devno, &bus_info))
829 len = snprintf(buf, PAGE_SIZE, "0x%Lx\n", (unsigned long long)
830 bus_info.chan_info.n_channel_bytes);
834 static ssize_t businst_attr_channel_id(struct visorbus_devdata *businst,
838 if (businst && businst->chan) {
839 visorchannel_id(businst->chan, buf);
846 static ssize_t businst_attr_client_bus_info(struct visorbus_devdata *businst,
848 struct visorchipset_bus_info bus_info;
849 int i, x, remain = PAGE_SIZE;
853 struct ultra_vbus_deviceinfo dev_info;
856 if (businst && businst->chan) {
857 if (visorchipset_get_bus_info(businst->devno, &bus_info) &&
859 partition_name = bus_info.name;
860 x = snprintf(p, remain,
861 "Client device / client driver info for %s partition (vbus #%d):\n",
862 partition_name, businst->devno);
865 x = visorchannel_read(businst->chan,
867 spar_vbus_channel_protocol,
869 &dev_info, sizeof(dev_info));
871 x = vbuschannel_devinfo_to_string(&dev_info, p,
876 x = visorchannel_read(businst->chan,
878 spar_vbus_channel_protocol,
880 &dev_info, sizeof(dev_info));
882 x = vbuschannel_devinfo_to_string(&dev_info, p,
887 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
889 while (off + sizeof(dev_info) <=
890 visorchannel_get_nbytes(businst->chan)) {
891 x = visorchannel_read(businst->chan,
892 off, &dev_info, sizeof(dev_info));
894 x = vbuschannel_devinfo_to_string
895 (&dev_info, p, remain, i);
899 off += sizeof(dev_info);
903 return PAGE_SIZE - remain;
906 static struct businst_attribute ba_partition_handle =
907 __ATTR(partition_handle, S_IRUGO, businst_attr_partition_handle, NULL);
908 static struct businst_attribute ba_partition_guid =
909 __ATTR(partition_guid, S_IRUGO, businst_attr_partition_guid, NULL);
910 static struct businst_attribute ba_partition_name =
911 __ATTR(partition_name, S_IRUGO, businst_attr_partition_name, NULL);
912 static struct businst_attribute ba_channel_addr =
913 __ATTR(channel_addr, S_IRUGO, businst_attr_channel_addr, NULL);
914 static struct businst_attribute ba_nchannel_bytes =
915 __ATTR(nchannel_bytes, S_IRUGO, businst_attr_nchannel_bytes, NULL);
916 static struct businst_attribute ba_channel_id =
917 __ATTR(channel_id, S_IRUGO, businst_attr_channel_id, NULL);
918 static struct businst_attribute ba_client_bus_info =
919 __ATTR(client_bus_info, S_IRUGO, businst_attr_client_bus_info, NULL);
922 register_businst_attributes(struct visorbus_devdata *businst)
926 businst->kobj.kset = &businstances; /* identify parent sysfs dir */
927 rc = kobject_init_and_add(&businst->kobj, &businst_kobj_type,
928 NULL, "visorbus%d", businst->devno);
932 rc = businst_create_file(businst, &ba_partition_handle);
936 rc = businst_create_file(businst, &ba_partition_guid);
940 rc = businst_create_file(businst, &ba_partition_name);
944 rc = businst_create_file(businst, &ba_channel_addr);
948 rc = businst_create_file(businst, &ba_nchannel_bytes);
952 rc = businst_create_file(businst, &ba_channel_id);
956 rc = businst_create_file(businst, &ba_client_bus_info);
960 kobject_uevent(&businst->kobj, KOBJ_ADD);
968 unregister_businst_attributes(struct visorbus_devdata *businst)
970 businst_remove_file(businst, &ba_partition_handle);
971 businst_remove_file(businst, &ba_partition_guid);
972 businst_remove_file(businst, &ba_partition_name);
973 businst_remove_file(businst, &ba_channel_addr);
974 businst_remove_file(businst, &ba_nchannel_bytes);
975 businst_remove_file(businst, &ba_channel_id);
976 businst_remove_file(businst, &ba_client_bus_info);
977 kobject_put(&businst->kobj);
982 * define & implement display of driver attributes under
983 * /sys/bus/visorbus/drivers/<drivername>.
988 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
990 struct visor_driver *drv = to_visor_driver(xdrv);
992 return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
996 register_driver_attributes(struct visor_driver *drv)
999 struct driver_attribute version =
1000 __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
1001 drv->version_attr = version;
1002 rc = driver_create_file(&drv->driver, &drv->version_attr);
1007 unregister_driver_attributes(struct visor_driver *drv)
1009 driver_remove_file(&drv->driver, &drv->version_attr);
1012 /* DEVICE attributes
1014 * define & implement display of device attributes under
1015 * /sys/bus/visorbus/devices/<devicename>.
1019 #define DEVATTR(nam, func) { \
1020 .attr = { .name = __stringify(nam), \
1022 .owner = THIS_MODULE }, \
1026 static struct device_attribute visor_device_attrs[] = {
1027 /* DEVATTR(channel_nbytes, DEVICE_ATTR_channel_nbytes), */
1032 dev_periodic_work(void *xdev)
1034 struct visor_device *dev = (struct visor_device *)xdev;
1035 struct visor_driver *drv = to_visor_driver(dev->device.driver);
1037 down(&dev->visordriver_callback_lock);
1038 if (drv->channel_interrupt)
1039 drv->channel_interrupt(dev);
1040 up(&dev->visordriver_callback_lock);
1041 if (!visor_periodic_work_nextperiod(dev->periodic_work))
1042 put_device(&dev->device);
1046 dev_start_periodic_work(struct visor_device *dev)
1048 if (dev->being_removed)
1050 /* now up by at least 2 */
1051 get_device(&dev->device);
1052 if (!visor_periodic_work_start(dev->periodic_work))
1053 put_device(&dev->device);
1057 dev_stop_periodic_work(struct visor_device *dev)
1059 if (visor_periodic_work_stop(dev->periodic_work))
1060 put_device(&dev->device);
1063 /** This is called automatically upon adding a visor_device (device_add), or
1064 * adding a visor_driver (visorbus_register_visor_driver), but only after
1065 * visorbus_match has returned 1 to indicate a successful match between
1066 * driver and device.
1069 visordriver_probe_device(struct device *xdev)
1072 struct visor_driver *drv;
1073 struct visor_device *dev;
1075 drv = to_visor_driver(xdev->driver);
1076 dev = to_visor_device(xdev);
1077 down(&dev->visordriver_callback_lock);
1078 dev->being_removed = false;
1080 * ensure that the dev->being_removed flag is cleared before
1081 * we start the probe
1084 get_device(&dev->device);
1086 up(&dev->visordriver_callback_lock);
1090 rc = drv->probe(dev);
1094 fix_vbus_dev_info(dev);
1095 up(&dev->visordriver_callback_lock);
1099 put_device(&dev->device);
1100 /* We could get here more than once if the child driver module is
1101 * unloaded and re-loaded while devices are present. That's why we
1102 * need a flag to be sure that we only respond to the device_create
1103 * once. We cannot respond to the device_create prior to here,
1104 * because until we call drv->probe() above, the channel has not been
1107 if (!dev->responded_to_device_create) {
1108 dev->responded_to_device_create = true;
1109 if (chipset_responders.device_create)
1110 (*chipset_responders.device_create)(dev->chipset_bus_no,
1111 dev->chipset_dev_no,
1117 /** This is called when device_unregister() is called for each child device
1118 * instance, to notify the appropriate visorbus_driver that the device is
1119 * going away, and to decrease the reference count of the device.
1122 visordriver_remove_device(struct device *xdev)
1125 struct visor_device *dev;
1126 struct visor_driver *drv;
1128 dev = to_visor_device(xdev);
1129 drv = to_visor_driver(xdev->driver);
1130 down(&dev->visordriver_callback_lock);
1131 dev->being_removed = true;
1133 * ensure that the dev->being_removed flag is set before we start the
1141 up(&dev->visordriver_callback_lock);
1142 dev_stop_periodic_work(dev);
1143 devmajorminor_remove_all_files(dev);
1145 put_device(&dev->device);
1150 /** A particular type of visor driver calls this function to register
1151 * the driver. The caller MUST fill in the following fields within the
1153 * name, version, owner, channel_types, probe, remove
1155 * Here's how the whole Linux bus / driver / device model works.
1157 * At system start-up, the visorbus kernel module is loaded, which registers
1158 * visorbus_type as a bus type, using bus_register().
1160 * All kernel modules that support particular device types on a
1161 * visorbus bus are loaded. Each of these kernel modules calls
1162 * visorbus_register_visor_driver() in their init functions, passing a
1163 * visor_driver struct. visorbus_register_visor_driver() in turn calls
1164 * register_driver(&visor_driver.driver). This .driver member is
1165 * initialized with generic methods (like probe), whose sole responsibility
1166 * is to act as a broker for the real methods, which are within the
1167 * visor_driver struct. (This is the way the subclass behavior is
1168 * implemented, since visor_driver is essentially a subclass of the
1169 * generic driver.) Whenever a driver_register() happens, core bus code in
1170 * the kernel does (see device_attach() in drivers/base/dd.c):
1172 * for each dev associated with the bus (the bus that driver is on) that
1173 * does not yet have a driver
1174 * if bus.match(dev,newdriver) == yes_matched ** .match specified
1175 * ** during bus_register().
1176 * newdriver.probe(dev) ** for visor drivers, this will call
1177 * ** the generic driver.probe implemented in visorbus.c,
1178 * ** which in turn calls the probe specified within the
1179 * ** struct visor_driver (which was specified by the
1180 * ** actual device driver as part of
1181 * ** visorbus_register_visor_driver()).
1183 * The above dance also happens when a new device appears.
1184 * So the question is, how are devices created within the system?
1185 * Basically, just call device_add(dev). See pci_bus_add_devices().
1186 * pci_scan_device() shows an example of how to build a device struct. It
1187 * returns the newly-created struct to pci_scan_single_device(), who adds it
1188 * to the list of devices at PCIBUS.devices. That list of devices is what
1189 * is traversed by pci_bus_add_devices().
1192 int visorbus_register_visor_driver(struct visor_driver *drv)
1196 drv->driver.name = drv->name;
1197 drv->driver.bus = &visorbus_type;
1198 drv->driver.probe = visordriver_probe_device;
1199 drv->driver.remove = visordriver_remove_device;
1200 drv->driver.owner = drv->owner;
1202 /* driver_register does this:
1203 * bus_add_driver(drv)
1204 * ->if (drv.bus) ** (bus_type) **
1205 * driver_attach(drv)
1206 * for each dev with bus type of drv.bus
1207 * if (!dev.drv) ** no driver assigned yet **
1208 * if (bus.match(dev,drv)) [visorbus_match]
1210 * if (!drv.probe(dev)) [visordriver_probe_device]
1214 rc = driver_register(&drv->driver);
1217 rc = register_driver_attributes(drv);
1220 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1222 /** A particular type of visor driver calls this function to unregister
1223 * the driver, i.e., within its module_exit function.
1226 visorbus_unregister_visor_driver(struct visor_driver *drv)
1228 unregister_driver_attributes(drv);
1229 driver_unregister(&drv->driver);
1231 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
1234 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
1235 void *dest, unsigned long nbytes)
1237 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
1239 EXPORT_SYMBOL_GPL(visorbus_read_channel);
1242 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
1243 void *src, unsigned long nbytes)
1245 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
1247 EXPORT_SYMBOL_GPL(visorbus_write_channel);
1250 visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
1251 unsigned long nbytes)
1253 return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
1255 EXPORT_SYMBOL_GPL(visorbus_clear_channel);
1258 visorbus_registerdevnode(struct visor_device *dev,
1259 const char *name, int major, int minor)
1261 return devmajorminor_create_file(dev, name, major, minor);
1263 EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
1265 /** We don't really have a real interrupt, so for now we just call the
1266 * interrupt function periodically...
1269 visorbus_enable_channel_interrupts(struct visor_device *dev)
1271 dev_start_periodic_work(dev);
1273 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
1276 visorbus_disable_channel_interrupts(struct visor_device *dev)
1278 dev_stop_periodic_work(dev);
1280 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
1282 /** This is how everything starts from the device end.
1283 * This function is called when a channel first appears via a ControlVM
1284 * message. In response, this function allocates a visor_device to
1285 * correspond to the new channel, and attempts to connect it the appropriate
1286 * driver. If the appropriate driver is found, the visor_driver.probe()
1287 * function for that driver will be called, and will be passed the new
1288 * visor_device that we just created.
1290 * It's ok if the appropriate driver is not yet loaded, because in that case
1291 * the new device struct will just stick around in the bus' list of devices.
1292 * When the appropriate driver calls visorbus_register_visor_driver(), the
1293 * visor_driver.probe() for the new driver will be called with the new
1297 create_visor_device(struct visorbus_devdata *devdata,
1298 unsigned long chipset_bus_no, unsigned long chipset_dev_no,
1299 struct visorchipset_channel_info chan_info,
1300 u64 partition_handle)
1303 struct visorchannel *visorchannel = NULL;
1304 struct visor_device *dev = NULL;
1305 bool gotten = false, registered1 = false, registered2 = false;
1307 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
1308 POSTCODE_SEVERITY_INFO);
1309 /* prepare chan_hdr (abstraction to read/write channel memory) */
1310 visorchannel = visorchannel_create(chan_info.channel_addr,
1311 chan_info.n_channel_bytes,
1313 chan_info.channel_type_uuid);
1314 if (!visorchannel) {
1315 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
1319 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1321 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
1326 memset(dev, 0, sizeof(struct visor_device));
1327 dev->visorchannel = visorchannel;
1328 dev->channel_type_guid = chan_info.channel_type_uuid;
1329 dev->channel_bytes = chan_info.n_channel_bytes;
1330 dev->chipset_bus_no = chipset_bus_no;
1331 dev->chipset_dev_no = chipset_dev_no;
1332 dev->device.parent = devdata->dev;
1333 sema_init(&dev->visordriver_callback_lock, 1); /* unlocked */
1334 dev->device.bus = &visorbus_type;
1335 device_initialize(&dev->device);
1336 dev->device.release = visorbus_release_device;
1337 /* keep a reference just for us (now 2) */
1338 get_device(&dev->device);
1340 dev->periodic_work =
1341 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
1342 periodic_dev_workqueue,
1344 dev, dev_name(&dev->device));
1345 if (!dev->periodic_work) {
1346 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
1351 /* bus_id must be a unique name with respect to this bus TYPE
1352 * (NOT bus instance). That's why we need to include the bus
1353 * number within the name.
1355 dev_set_name(&dev->device, "vbus%lu:dev%lu",
1356 chipset_bus_no, chipset_dev_no);
1358 /* device_add does this:
1359 * bus_add_device(dev)
1360 * ->device_attach(dev)
1361 * ->for each driver drv registered on the bus that dev is on
1362 * if (dev.drv) ** device already has a driver **
1363 * ** not sure we could ever get here... **
1365 * if (bus.match(dev,drv)) [visorbus_match]
1367 * if (!drv.probe(dev)) [visordriver_probe_device]
1370 * Note that device_add does NOT fail if no driver failed to
1371 * claim the device. The device will be linked onto
1372 * bus_type.klist_devices regardless (use bus_for_each_dev).
1374 rc = device_add(&dev->device);
1376 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
1381 /* note: device_register is simply device_initialize + device_add */
1382 rc = register_channel_attributes(dev);
1384 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1391 rc = register_devmajorminor_attributes(dev);
1393 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1404 unregister_devmajorminor_attributes(dev);
1406 unregister_channel_attributes(dev);
1408 put_device(&dev->device);
1410 visorchannel_destroy(visorchannel);
1413 total_devices_created++;
1414 list_add_tail(&dev->list_all, &list_all_device_instances);
1420 remove_visor_device(struct visor_device *dev)
1422 list_del(&dev->list_all);
1423 unregister_devmajorminor_attributes(dev);
1424 unregister_channel_attributes(dev);
1425 put_device(&dev->device);
1426 device_unregister(&dev->device);
1429 static struct visor_device *
1430 find_visor_device_by_channel(u64 channel_physaddr)
1432 struct list_head *listentry, *listtmp;
1434 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1435 struct visor_device *dev = list_entry(listentry,
1436 struct visor_device,
1438 if (visorchannel_get_physaddr(dev->visorchannel) ==
1446 init_vbus_channel(struct visorchannel *chan)
1449 unsigned long allocated_bytes = visorchannel_get_nbytes(chan);
1450 struct spar_vbus_channel_protocol *x =
1451 kmalloc(sizeof(struct spar_vbus_channel_protocol),
1454 POSTCODE_LINUX_3(VBUS_CHANNEL_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1457 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1460 if (visorchannel_clear(chan, 0, 0, allocated_bytes) < 0) {
1461 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1462 POSTCODE_SEVERITY_ERR);
1465 if (visorchannel_read
1466 (chan, 0, x, sizeof(struct spar_vbus_channel_protocol)) < 0) {
1467 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1468 POSTCODE_SEVERITY_ERR);
1471 if (!SPAR_VBUS_CHANNEL_OK_SERVER(allocated_bytes)) {
1472 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1473 POSTCODE_SEVERITY_ERR);
1477 if (visorchannel_write
1478 (chan, 0, x, sizeof(struct spar_vbus_channel_protocol)) < 0) {
1479 POSTCODE_LINUX_3(VBUS_CHANNEL_FAILURE_PC, chan,
1480 POSTCODE_SEVERITY_ERR);
1484 POSTCODE_LINUX_3(VBUS_CHANNEL_EXIT_PC, chan, POSTCODE_SEVERITY_INFO);
1494 get_vbus_header_info(struct visorchannel *chan,
1495 struct spar_vbus_headerinfo *hdr_info)
1499 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
1501 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1502 sizeof(*hdr_info)) < 0) {
1505 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
1507 if (hdr_info->device_info_struct_bytes <
1508 sizeof(struct ultra_vbus_deviceinfo)) {
1516 /* Write the contents of <info> to the struct
1517 * spar_vbus_channel_protocol.chp_info. */
1520 write_vbus_chp_info(struct visorchannel *chan,
1521 struct spar_vbus_headerinfo *hdr_info,
1522 struct ultra_vbus_deviceinfo *info)
1524 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1526 if (hdr_info->chp_info_offset == 0)
1529 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1534 /* Write the contents of <info> to the struct
1535 * spar_vbus_channel_protocol.bus_info. */
1538 write_vbus_bus_info(struct visorchannel *chan,
1539 struct spar_vbus_headerinfo *hdr_info,
1540 struct ultra_vbus_deviceinfo *info)
1542 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1544 if (hdr_info->bus_info_offset == 0)
1547 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1552 /* Write the contents of <info> to the
1553 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1556 write_vbus_dev_info(struct visorchannel *chan,
1557 struct spar_vbus_headerinfo *hdr_info,
1558 struct ultra_vbus_deviceinfo *info, int devix)
1561 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1562 (hdr_info->device_info_struct_bytes * devix);
1564 if (hdr_info->dev_info_offset == 0)
1567 if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1572 /* For a child device just created on a client bus, fill in
1573 * information about the driver that is controlling this device into
1574 * the the appropriate slot within the vbus channel of the bus
1578 fix_vbus_dev_info(struct visor_device *visordev)
1581 struct visorchipset_bus_info bus_info;
1582 struct visorbus_devdata *devdata = NULL;
1583 struct visor_driver *visordrv;
1584 int bus_no = visordev->chipset_bus_no;
1585 int dev_no = visordev->chipset_dev_no;
1586 struct ultra_vbus_deviceinfo dev_info;
1587 const char *chan_type_name = NULL;
1589 if (!visordev->device.driver)
1592 visordrv = to_visor_driver(visordev->device.driver);
1593 if (!visorchipset_get_bus_info(bus_no, &bus_info))
1596 devdata = (struct visorbus_devdata *)(bus_info.bus_driver_context);
1600 if (!devdata->vbus_valid)
1603 /* Within the list of device types (by GUID) that the driver
1604 * says it supports, find out which one of those types matches
1605 * the type of this device, so that we can include the device
1608 for (i = 0; visordrv->channel_types[i].name; i++) {
1609 if (memcmp(&visordrv->channel_types[i].guid,
1610 &visordev->channel_type_guid,
1611 sizeof(visordrv->channel_types[i].guid)) == 0) {
1612 chan_type_name = visordrv->channel_types[i].name;
1617 bus_device_info_init(&dev_info, chan_type_name,
1618 visordrv->name, visordrv->version,
1620 write_vbus_dev_info(devdata->chan,
1621 &devdata->vbus_hdr_info, &dev_info, dev_no);
1623 /* Re-write bus+chipset info, because it is possible that this
1624 * was previously written by our evil counterpart, virtpci.
1626 write_vbus_chp_info(devdata->chan, &devdata->vbus_hdr_info,
1627 &chipset_driverinfo);
1628 write_vbus_bus_info(devdata->chan, &devdata->vbus_hdr_info,
1629 &clientbus_driverinfo);
1632 /** Create a device instance for the visor bus itself.
1634 static struct visorbus_devdata *
1635 create_bus_instance(int id)
1637 struct visorbus_devdata *rc = NULL;
1638 struct visorbus_devdata *devdata = NULL;
1640 struct visorchipset_bus_info bus_info;
1642 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1643 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1645 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1649 memset(dev, 0, sizeof(struct device));
1650 dev_set_name(dev, "visorbus%d", id);
1651 dev->release = visorbus_release_busdevice;
1652 if (device_register(dev) < 0) {
1653 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1654 POSTCODE_SEVERITY_ERR);
1658 devdata = kmalloc(sizeof(*devdata), GFP_KERNEL);
1660 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1664 memset(devdata, 0, sizeof(struct visorbus_devdata));
1665 devdata->devno = id;
1667 if ((visorchipset_get_bus_info(id, &bus_info)) &&
1668 (bus_info.chan_info.channel_addr > 0) &&
1669 (bus_info.chan_info.n_channel_bytes > 0)) {
1670 u64 channel_addr = bus_info.chan_info.channel_addr;
1671 unsigned long n_channel_bytes =
1673 bus_info.chan_info.n_channel_bytes;
1674 uuid_le channel_type_guid =
1675 bus_info.chan_info.channel_type_uuid;
1677 devdata->chan = visorchannel_create(channel_addr,
1681 if (!devdata->chan) {
1682 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, channel_addr,
1683 POSTCODE_SEVERITY_ERR);
1685 if (bus_info.flags.server) {
1686 init_vbus_channel(devdata->chan);
1688 if (get_vbus_header_info(devdata->chan,
1690 vbus_hdr_info) >= 0) {
1691 devdata->vbus_valid = true;
1692 write_vbus_chp_info(devdata->chan,
1697 write_vbus_bus_info(devdata->chan,
1700 &clientbus_driverinfo);
1705 register_businst_attributes(devdata);
1707 list_add_tail(&devdata->list_all, &list_all_bus_instances);
1709 devdata = devdata; /* for testing ONLY */
1710 dev_set_drvdata(dev, devdata);
1716 /** Remove a device instance for the visor bus itself.
1719 remove_bus_instance(struct visorbus_devdata *devdata)
1721 /* Note that this will result in the release method for
1722 * devdata->dev being called, which will call
1723 * visorbus_release_busdevice(). This has something to do with
1724 * the put_device() done in device_unregister(), but I have never
1725 * successfully been able to trace thru the code to see where/how
1726 * release() gets called. But I know it does.
1728 unregister_businst_attributes(devdata);
1730 if (devdata->chan) {
1731 visorchannel_destroy(devdata->chan);
1732 devdata->chan = NULL;
1734 list_del(&devdata->list_all);
1735 device_unregister(devdata->dev);
1738 /** Create and register the one-and-only one instance of
1739 * the visor bus type (visorbus_type).
1742 create_bus_type(void)
1746 visorbus_type.dev_attrs = visor_device_attrs;
1747 rc = bus_register(&visorbus_type);
1751 rc = register_bustype_attributes();
1755 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1758 remove_bus_type(void)
1760 unregister_bustype_attributes();
1761 bus_unregister(&visorbus_type);
1764 /** Remove all child visor bus device instances.
1767 remove_all_visor_devices(void)
1769 struct list_head *listentry, *listtmp;
1771 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1772 struct visor_device *dev = list_entry(listentry,
1773 struct visor_device,
1775 remove_visor_device(dev);
1779 static bool entered_testing_mode;
1780 static struct visorchipset_channel_info test_channel_infos[MAXDEVICETEST];
1781 static unsigned long test_bus_nos[MAXDEVICETEST];
1782 static unsigned long test_dev_nos[MAXDEVICETEST];
1785 chipset_bus_create(u32 bus_no)
1787 struct visorchipset_bus_info bus_info;
1788 struct visorbus_devdata *devdata;
1791 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1792 if (!visorchipset_get_bus_info(bus_no, &bus_info))
1794 devdata = create_bus_instance(bus_no);
1797 if (!visorchipset_set_bus_context(bus_no, devdata))
1799 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1803 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1804 POSTCODE_SEVERITY_ERR);
1807 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1808 POSTCODE_SEVERITY_INFO);
1809 if (chipset_responders.bus_create)
1810 (*chipset_responders.bus_create) (bus_no, rc);
1814 chipset_bus_destroy(u32 bus_no)
1816 struct visorchipset_bus_info bus_info;
1817 struct visorbus_devdata *devdata;
1820 if (!visorchipset_get_bus_info(bus_no, &bus_info))
1822 devdata = (struct visorbus_devdata *)(bus_info.bus_driver_context);
1825 remove_bus_instance(devdata);
1826 if (!visorchipset_set_bus_context(bus_no, NULL))
1832 if (chipset_responders.bus_destroy)
1833 (*chipset_responders.bus_destroy)(bus_no, rc);
1837 chipset_device_create(u32 bus_no, u32 dev_no)
1839 struct visorchipset_device_info dev_info;
1840 struct visorchipset_bus_info bus_info;
1841 struct visorbus_devdata *devdata = NULL;
1844 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1845 POSTCODE_SEVERITY_INFO);
1847 if (entered_testing_mode)
1849 if (!visorchipset_get_device_info(bus_no, dev_no, &dev_info))
1851 if (!visorchipset_get_bus_info(bus_no, &bus_info))
1853 if (visorbus_devicetest)
1854 if (total_devices_created < MAXDEVICETEST) {
1855 test_channel_infos[total_devices_created] =
1857 test_bus_nos[total_devices_created] = bus_no;
1858 test_dev_nos[total_devices_created] = dev_no;
1860 POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, dev_no, bus_no,
1861 POSTCODE_SEVERITY_INFO);
1865 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1866 POSTCODE_SEVERITY_ERR);
1869 devdata = (struct visorbus_devdata *)(bus_info.bus_driver_context);
1870 rc = create_visor_device(devdata, bus_no, dev_no,
1871 dev_info.chan_info, bus_info.partition_handle);
1872 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1873 POSTCODE_SEVERITY_INFO);
1875 if (chipset_responders.device_create)
1876 (*chipset_responders.device_create)(bus_no, dev_no, rc);
1880 chipset_device_destroy(u32 bus_no, u32 dev_no)
1882 struct visorchipset_device_info dev_info;
1883 struct visor_device *dev;
1886 if (entered_testing_mode)
1888 if (!visorchipset_get_device_info(bus_no, dev_no, &dev_info))
1890 dev = find_visor_device_by_channel(dev_info.chan_info.channel_addr);
1898 if (chipset_responders.device_destroy)
1899 (*chipset_responders.device_destroy) (bus_no, dev_no, rc);
1900 remove_visor_device(dev);
1903 /* This is the callback function specified for a function driver, to
1904 * be called when a pending "pause device" operation has been
1908 pause_state_change_complete(struct visor_device *dev, int status)
1913 dev->pausing = false;
1914 if (!chipset_responders.device_pause) /* this can never happen! */
1917 /* Notify the chipset driver that the pause is complete, which
1918 * will presumably want to send some sort of response to the
1920 (*chipset_responders.device_pause) (dev->chipset_bus_no,
1921 dev->chipset_dev_no, status);
1924 /* This is the callback function specified for a function driver, to
1925 * be called when a pending "resume device" operation has been
1929 resume_state_change_complete(struct visor_device *dev, int status)
1934 dev->resuming = false;
1935 if (!chipset_responders.device_resume) /* this can never happen! */
1938 /* Notify the chipset driver that the resume is complete,
1939 * which will presumably want to send some sort of response to
1941 (*chipset_responders.device_resume) (dev->chipset_bus_no,
1942 dev->chipset_dev_no, status);
1945 /* Tell the subordinate function driver for a specific device to pause
1946 * or resume that device. Result is returned asynchronously via a
1947 * callback function.
1950 initiate_chipset_device_pause_resume(u32 bus_no, u32 dev_no, bool is_pause)
1952 struct visorchipset_device_info dev_info;
1953 struct visor_device *dev = NULL;
1955 struct visor_driver *drv = NULL;
1956 void (*notify_func)(u32 bus_no, u32 dev_no, int response) = NULL;
1959 notify_func = chipset_responders.device_pause;
1961 notify_func = chipset_responders.device_resume;
1965 if (!visorchipset_get_device_info(bus_no, dev_no, &dev_info))
1968 dev = find_visor_device_by_channel(dev_info.chan_info.channel_addr);
1972 drv = to_visor_driver(dev->device.driver);
1976 if (dev->pausing || dev->resuming)
1979 /* Note that even though both drv->pause() and drv->resume
1980 * specify a callback function, it is NOT necessary for us to
1981 * increment our local module usage count. Reason is, there
1982 * is already a linkage dependency between child function
1983 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1984 * visorbus while child function drivers are still running.
1990 dev->pausing = true;
1991 x = drv->pause(dev, pause_state_change_complete);
1993 /* This should be done at BUS resume time, but an
1994 * existing problem prevents us from ever getting a bus
1995 * resume... This hack would fail to work should we
1996 * ever have a bus that contains NO devices, since we
1997 * would never even get here in that case. */
1998 fix_vbus_dev_info(dev);
2002 dev->resuming = true;
2003 x = drv->resume(dev, resume_state_change_complete);
2007 dev->pausing = false;
2009 dev->resuming = false;
2016 (*notify_func)(bus_no, dev_no, rc);
2021 chipset_device_pause(u32 bus_no, u32 dev_no)
2023 initiate_chipset_device_pause_resume(bus_no, dev_no, true);
2027 chipset_device_resume(u32 bus_no, u32 dev_no)
2029 initiate_chipset_device_pause_resume(bus_no, dev_no, false);
2032 struct channel_size_info {
2034 unsigned long min_size;
2035 unsigned long max_size;
2043 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
2044 bus_device_info_init(&clientbus_driverinfo,
2045 "clientbus", "visorbus",
2048 /* process module options */
2050 if (visorbus_devicetest > MAXDEVICETEST)
2051 visorbus_devicetest = MAXDEVICETEST;
2053 rc = create_bus_type();
2055 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
2059 periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
2060 if (!periodic_dev_workqueue) {
2061 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
2066 /* This enables us to receive notifications when devices appear for
2067 * which this service partition is to be a server for.
2069 visorchipset_register_busdev(&chipset_notifiers,
2070 &chipset_responders,
2071 &chipset_driverinfo);
2077 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
2078 POSTCODE_SEVERITY_ERR);
2085 struct list_head *listentry, *listtmp;
2087 visorchipset_register_busdev(NULL, NULL, NULL);
2088 remove_all_visor_devices();
2090 flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
2091 destroy_workqueue(periodic_dev_workqueue);
2092 periodic_dev_workqueue = NULL;
2094 if (periodic_test_workqueue) {
2095 cancel_delayed_work(&periodic_work);
2096 flush_workqueue(periodic_test_workqueue);
2097 destroy_workqueue(periodic_test_workqueue);
2098 periodic_test_workqueue = NULL;
2101 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
2102 struct visorbus_devdata *devdata = list_entry(listentry,
2106 remove_bus_instance(devdata);
2111 module_param_named(debug, visorbus_debug, int, S_IRUGO);
2112 MODULE_PARM_DESC(visorbus_debug, "1 to debug");
2113 int visorbus_debug = 0;
2115 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
2116 MODULE_PARM_DESC(visorbus_forcematch,
2117 "1 to force a successful dev <--> drv match");
2118 int visorbus_forcematch = 0;
2120 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
2121 MODULE_PARM_DESC(visorbus_forcenomatch,
2122 "1 to force an UNsuccessful dev <--> drv match");
2123 int visorbus_forcenomatch = 0;
2125 module_param_named(devicetest, visorbus_devicetest, int, S_IRUGO);
2126 MODULE_PARM_DESC(visorbus_devicetest,
2127 "non-0 to just test device creation and destruction");
2128 int visorbus_devicetest = 0;
2130 module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
2131 MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");
2132 int visorbus_debugref = 0;
2134 MODULE_AUTHOR("Unisys");
2135 MODULE_LICENSE("GPL");
2136 MODULE_DESCRIPTION("Supervisor bus driver for service partition: ver " VERSION);
2137 MODULE_VERSION(VERSION);