3 * Copyright � 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
18 #include <linux/uuid.h>
21 #include "visorbus_private.h"
23 #include "periodic_work.h"
24 #include "vbuschannel.h"
25 #include "guestlinuxdebug.h"
26 #include "vmcallinterface.h"
28 #define MYDRVNAME "visorbus"
30 /* module parameters */
31 static int visorbus_debug
;
32 static int visorbus_forcematch
;
33 static int visorbus_forcenomatch
;
34 static int visorbus_debugref
;
35 #define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
37 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
38 #define POLLJIFFIES_TESTWORK 100
39 #define POLLJIFFIES_NORMALCHANNEL 10
41 static int visorbus_uevent(struct device
*xdev
, struct kobj_uevent_env
*env
);
42 static int visorbus_match(struct device
*xdev
, struct device_driver
*xdrv
);
43 static void fix_vbus_dev_info(struct visor_device
*visordev
);
45 /* BUS type attributes
47 * define & implement display of bus attributes under
52 static ssize_t
version_show(struct bus_type
*bus
, char *buf
)
54 return snprintf(buf
, PAGE_SIZE
, "%s\n", VERSION
);
57 static BUS_ATTR_RO(version
);
59 static struct attribute
*visorbus_bus_attrs
[] = {
60 &bus_attr_version
.attr
,
64 static const struct attribute_group visorbus_bus_group
= {
65 .attrs
= visorbus_bus_attrs
,
68 static const struct attribute_group
*visorbus_bus_groups
[] = {
73 /** This describes the TYPE of bus.
74 * (Don't confuse this with an INSTANCE of the bus.)
76 struct bus_type visorbus_type
= {
78 .match
= visorbus_match
,
79 .uevent
= visorbus_uevent
,
80 .bus_groups
= visorbus_bus_groups
,
83 static struct delayed_work periodic_work
;
85 /* YES, we need 2 workqueues.
86 * The reason is, workitems on the test queue may need to cancel
87 * workitems on the other queue. You will be in for trouble if you try to
88 * do this with workitems queued on the same workqueue.
90 static struct workqueue_struct
*periodic_test_workqueue
;
91 static struct workqueue_struct
*periodic_dev_workqueue
;
92 static long long bus_count
; /** number of bus instances */
93 /** ever-increasing */
95 static void chipset_bus_create(struct visor_device
*bus_info
);
96 static void chipset_bus_destroy(struct visor_device
*bus_info
);
97 static void chipset_device_create(struct visor_device
*dev_info
);
98 static void chipset_device_destroy(struct visor_device
*dev_info
);
99 static void chipset_device_pause(struct visor_device
*dev_info
);
100 static void chipset_device_resume(struct visor_device
*dev_info
);
102 /** These functions are implemented herein, and are called by the chipset
103 * driver to notify us about specific events.
105 static struct visorchipset_busdev_notifiers chipset_notifiers
= {
106 .bus_create
= chipset_bus_create
,
107 .bus_destroy
= chipset_bus_destroy
,
108 .device_create
= chipset_device_create
,
109 .device_destroy
= chipset_device_destroy
,
110 .device_pause
= chipset_device_pause
,
111 .device_resume
= chipset_device_resume
,
114 /** These functions are implemented in the chipset driver, and we call them
115 * herein when we want to acknowledge a specific event.
117 static struct visorchipset_busdev_responders chipset_responders
;
119 /* filled in with info about parent chipset driver when we register with it */
120 static struct ultra_vbus_deviceinfo chipset_driverinfo
;
121 /* filled in with info about this driver, wrt it servicing client busses */
122 static struct ultra_vbus_deviceinfo clientbus_driverinfo
;
124 /** list of visor_device structs, linked via .list_all */
125 static LIST_HEAD(list_all_bus_instances
);
126 /** list of visor_device structs, linked via .list_all */
127 static LIST_HEAD(list_all_device_instances
);
130 visorbus_uevent(struct device
*xdev
, struct kobj_uevent_env
*env
)
132 if (add_uevent_var(env
, "VERSION=%s", VERSION
))
137 /* This is called automatically upon adding a visor_device (device_add), or
138 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
139 * provided driver can control the specified device.
142 visorbus_match(struct device
*xdev
, struct device_driver
*xdrv
)
144 uuid_le channel_type
;
147 struct visor_device
*dev
;
148 struct visor_driver
*drv
;
150 dev
= to_visor_device(xdev
);
151 drv
= to_visor_driver(xdrv
);
152 channel_type
= visorchannel_get_uuid(dev
->visorchannel
);
153 if (visorbus_forcematch
) {
157 if (visorbus_forcenomatch
)
160 if (!drv
->channel_types
)
163 (uuid_le_cmp(drv
->channel_types
[i
].guid
, NULL_UUID_LE
) != 0) ||
164 (drv
->channel_types
[i
].name
);
166 if (uuid_le_cmp(drv
->channel_types
[i
].guid
,
167 channel_type
) == 0) {
175 /** This is called when device_unregister() is called for the bus device
176 * instance, after all other tasks involved with destroying the device
180 visorbus_release_busdevice(struct device
*xdev
)
182 struct visor_device
*dev
= dev_get_drvdata(xdev
);
184 dev_set_drvdata(xdev
, NULL
);
188 /** This is called when device_unregister() is called for each child
192 visorbus_release_device(struct device
*xdev
)
194 struct visor_device
*dev
= to_visor_device(xdev
);
196 if (dev
->periodic_work
) {
197 visor_periodic_work_destroy(dev
->periodic_work
);
198 dev
->periodic_work
= NULL
;
200 if (dev
->visorchannel
) {
201 visorchannel_destroy(dev
->visorchannel
);
202 dev
->visorchannel
= NULL
;
207 /* Implement publishing of device node attributes under:
209 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
213 #define to_devmajorminor_attr(_attr) \
214 container_of(_attr, struct devmajorminor_attribute, attr)
215 #define to_visor_device_from_kobjdevmajorminor(obj) \
216 container_of(obj, struct visor_device, kobjdevmajorminor)
218 struct devmajorminor_attribute
{
219 struct attribute attr
;
221 ssize_t (*show
)(struct visor_device
*, int slot
, char *buf
);
222 ssize_t (*store
)(struct visor_device
*, int slot
, const char *buf
,
226 static ssize_t
DEVMAJORMINOR_ATTR(struct visor_device
*dev
, int slot
, char *buf
)
228 int maxdevnodes
= ARRAY_SIZE(dev
->devnodes
) / sizeof(dev
->devnodes
[0]);
230 if (slot
< 0 || slot
>= maxdevnodes
)
232 return snprintf(buf
, PAGE_SIZE
, "%d:%d\n",
233 dev
->devnodes
[slot
].major
, dev
->devnodes
[slot
].minor
);
237 devmajorminor_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
239 struct devmajorminor_attribute
*devmajorminor_attr
=
240 to_devmajorminor_attr(attr
);
241 struct visor_device
*dev
= to_visor_device_from_kobjdevmajorminor(kobj
);
244 if (devmajorminor_attr
->show
)
245 ret
= devmajorminor_attr
->show(dev
,
246 devmajorminor_attr
->slot
, buf
);
251 devmajorminor_attr_store(struct kobject
*kobj
,
252 struct attribute
*attr
, const char *buf
, size_t count
)
254 struct devmajorminor_attribute
*devmajorminor_attr
=
255 to_devmajorminor_attr(attr
);
256 struct visor_device
*dev
= to_visor_device_from_kobjdevmajorminor(kobj
);
259 if (devmajorminor_attr
->store
)
260 ret
= devmajorminor_attr
->store(dev
,
261 devmajorminor_attr
->slot
,
266 static int register_devmajorminor_attributes(struct visor_device
*dev
);
269 devmajorminor_create_file(struct visor_device
*dev
, const char *name
,
270 int major
, int minor
)
272 int maxdevnodes
= ARRAY_SIZE(dev
->devnodes
) / sizeof(dev
->devnodes
[0]);
273 struct devmajorminor_attribute
*myattr
= NULL
;
274 int x
= -1, rc
= 0, slot
= -1;
276 register_devmajorminor_attributes(dev
);
277 for (slot
= 0; slot
< maxdevnodes
; slot
++)
278 if (!dev
->devnodes
[slot
].attr
)
280 if (slot
== maxdevnodes
) {
284 myattr
= kmalloc(sizeof(*myattr
), GFP_KERNEL
);
289 memset(myattr
, 0, sizeof(struct devmajorminor_attribute
));
290 myattr
->show
= DEVMAJORMINOR_ATTR
;
291 myattr
->store
= NULL
;
293 myattr
->attr
.name
= name
;
294 myattr
->attr
.mode
= S_IRUGO
;
295 dev
->devnodes
[slot
].attr
= myattr
;
296 dev
->devnodes
[slot
].major
= major
;
297 dev
->devnodes
[slot
].minor
= minor
;
298 x
= sysfs_create_file(&dev
->kobjdevmajorminor
, &myattr
->attr
);
303 kobject_uevent(&dev
->device
.kobj
, KOBJ_ONLINE
);
308 dev
->devnodes
[slot
].attr
= NULL
;
314 devmajorminor_remove_file(struct visor_device
*dev
, int slot
)
316 int maxdevnodes
= ARRAY_SIZE(dev
->devnodes
) / sizeof(dev
->devnodes
[0]);
317 struct devmajorminor_attribute
*myattr
= NULL
;
319 if (slot
< 0 || slot
>= maxdevnodes
)
321 myattr
= (struct devmajorminor_attribute
*)(dev
->devnodes
[slot
].attr
);
324 sysfs_remove_file(&dev
->kobjdevmajorminor
, &myattr
->attr
);
325 kobject_uevent(&dev
->device
.kobj
, KOBJ_OFFLINE
);
326 dev
->devnodes
[slot
].attr
= NULL
;
331 devmajorminor_remove_all_files(struct visor_device
*dev
)
334 int maxdevnodes
= ARRAY_SIZE(dev
->devnodes
) / sizeof(dev
->devnodes
[0]);
336 for (i
= 0; i
< maxdevnodes
; i
++)
337 devmajorminor_remove_file(dev
, i
);
340 static const struct sysfs_ops devmajorminor_sysfs_ops
= {
341 .show
= devmajorminor_attr_show
,
342 .store
= devmajorminor_attr_store
,
345 static struct kobj_type devmajorminor_kobj_type
= {
346 .sysfs_ops
= &devmajorminor_sysfs_ops
350 register_devmajorminor_attributes(struct visor_device
*dev
)
354 if (dev
->kobjdevmajorminor
.parent
)
355 goto away
; /* already registered */
356 x
= kobject_init_and_add(&dev
->kobjdevmajorminor
,
357 &devmajorminor_kobj_type
, &dev
->device
.kobj
,
364 kobject_uevent(&dev
->kobjdevmajorminor
, KOBJ_ADD
);
371 unregister_devmajorminor_attributes(struct visor_device
*dev
)
373 if (!dev
->kobjdevmajorminor
.parent
)
374 return; /* already unregistered */
375 devmajorminor_remove_all_files(dev
);
377 kobject_del(&dev
->kobjdevmajorminor
);
378 kobject_put(&dev
->kobjdevmajorminor
);
379 dev
->kobjdevmajorminor
.parent
= NULL
;
382 /* begin implementation of specific channel attributes to appear under
383 * /sys/bus/visorbus<x>/dev<y>/channel
385 static ssize_t
physaddr_show(struct device
*dev
, struct device_attribute
*attr
,
388 struct visor_device
*vdev
= to_visor_device(dev
);
390 if (!vdev
->visorchannel
)
392 return snprintf(buf
, PAGE_SIZE
, "0x%Lx\n",
393 visorchannel_get_physaddr(vdev
->visorchannel
));
396 static ssize_t
nbytes_show(struct device
*dev
, struct device_attribute
*attr
,
399 struct visor_device
*vdev
= to_visor_device(dev
);
401 if (!vdev
->visorchannel
)
403 return snprintf(buf
, PAGE_SIZE
, "0x%lx\n",
404 visorchannel_get_nbytes(vdev
->visorchannel
));
407 static ssize_t
clientpartition_show(struct device
*dev
,
408 struct device_attribute
*attr
, char *buf
)
410 struct visor_device
*vdev
= to_visor_device(dev
);
412 if (!vdev
->visorchannel
)
414 return snprintf(buf
, PAGE_SIZE
, "0x%Lx\n",
415 visorchannel_get_clientpartition(vdev
->visorchannel
));
418 static ssize_t
typeguid_show(struct device
*dev
, struct device_attribute
*attr
,
421 struct visor_device
*vdev
= to_visor_device(dev
);
424 if (!vdev
->visorchannel
)
426 return snprintf(buf
, PAGE_SIZE
, "%s\n",
427 visorchannel_id(vdev
->visorchannel
, s
));
430 static ssize_t
zoneguid_show(struct device
*dev
, struct device_attribute
*attr
,
433 struct visor_device
*vdev
= to_visor_device(dev
);
436 if (!vdev
->visorchannel
)
438 return snprintf(buf
, PAGE_SIZE
, "%s\n",
439 visorchannel_zoneid(vdev
->visorchannel
, s
));
442 static ssize_t
typename_show(struct device
*dev
, struct device_attribute
*attr
,
445 struct visor_device
*vdev
= to_visor_device(dev
);
447 struct bus_type
*xbus
= dev
->bus
;
448 struct device_driver
*xdrv
= dev
->driver
;
449 struct visor_driver
*drv
= NULL
;
451 if (!vdev
->visorchannel
|| !xbus
|| !xdrv
)
453 i
= xbus
->match(dev
, xdrv
);
456 drv
= to_visor_driver(xdrv
);
457 return snprintf(buf
, PAGE_SIZE
, "%s\n", drv
->channel_types
[i
- 1].name
);
460 static DEVICE_ATTR_RO(physaddr
);
461 static DEVICE_ATTR_RO(nbytes
);
462 static DEVICE_ATTR_RO(clientpartition
);
463 static DEVICE_ATTR_RO(typeguid
);
464 static DEVICE_ATTR_RO(zoneguid
);
465 static DEVICE_ATTR_RO(typename
);
467 static struct attribute
*channel_attrs
[] = {
468 &dev_attr_physaddr
.attr
,
469 &dev_attr_nbytes
.attr
,
470 &dev_attr_clientpartition
.attr
,
471 &dev_attr_typeguid
.attr
,
472 &dev_attr_zoneguid
.attr
,
473 &dev_attr_typename
.attr
,
476 static struct attribute_group channel_attr_grp
= {
478 .attrs
= channel_attrs
,
481 static const struct attribute_group
*visorbus_dev_groups
[] = {
486 /* end implementation of specific channel attributes */
488 /* BUS instance attributes
490 * define & implement display of bus attributes under
491 * /sys/bus/visorbus/busses/visorbus<n>.
493 * This is a bit hoaky because the kernel does not yet have the infrastructure
494 * to separate bus INSTANCE attributes from bus TYPE attributes...
495 * so we roll our own. See businst.c / businst.h.
499 static ssize_t
partition_handle_show(struct device
*dev
,
500 struct device_attribute
*attr
,
502 struct visor_device
*vdev
= to_visor_device(dev
);
503 u64 handle
= visorchannel_get_clientpartition(vdev
->visorchannel
);
505 return snprintf(buf
, PAGE_SIZE
, "0x%Lx\n", handle
);
508 static ssize_t
partition_guid_show(struct device
*dev
,
509 struct device_attribute
*attr
,
511 struct visor_device
*vdev
= to_visor_device(dev
);
513 return snprintf(buf
, PAGE_SIZE
, "{%pUb}\n", &vdev
->partition_uuid
);
516 static ssize_t
partition_name_show(struct device
*dev
,
517 struct device_attribute
*attr
,
519 struct visor_device
*vdev
= to_visor_device(dev
);
521 return snprintf(buf
, PAGE_SIZE
, "%s\n", vdev
->name
);
524 static ssize_t
channel_addr_show(struct device
*dev
,
525 struct device_attribute
*attr
,
527 struct visor_device
*vdev
= to_visor_device(dev
);
528 u64 addr
= visorchannel_get_physaddr(vdev
->visorchannel
);
530 return snprintf(buf
, PAGE_SIZE
, "0x%Lx\n", addr
);
533 static ssize_t
channel_bytes_show(struct device
*dev
,
534 struct device_attribute
*attr
,
536 struct visor_device
*vdev
= to_visor_device(dev
);
537 u64 nbytes
= visorchannel_get_nbytes(vdev
->visorchannel
);
539 return snprintf(buf
, PAGE_SIZE
, "0x%Lx\n", nbytes
);
542 static ssize_t
channel_id_show(struct device
*dev
,
543 struct device_attribute
*attr
,
545 struct visor_device
*vdev
= to_visor_device(dev
);
548 if (vdev
->visorchannel
) {
549 visorchannel_id(vdev
->visorchannel
, buf
);
556 static ssize_t
client_bus_info_show(struct device
*dev
,
557 struct device_attribute
*attr
,
559 struct visor_device
*vdev
= to_visor_device(dev
);
560 struct visorchannel
*channel
= vdev
->visorchannel
;
562 int i
, x
, remain
= PAGE_SIZE
;
566 struct ultra_vbus_deviceinfo dev_info
;
571 partition_name
= vdev
->name
;
572 x
= snprintf(p
, remain
,
573 "Client device / client driver info for %s partition (vbus #%d):\n",
574 partition_name
, vdev
->chipset_dev_no
);
577 x
= visorchannel_read(channel
,
579 spar_vbus_channel_protocol
,
581 &dev_info
, sizeof(dev_info
));
583 x
= vbuschannel_devinfo_to_string(&dev_info
, p
,
588 x
= visorchannel_read(channel
,
590 spar_vbus_channel_protocol
,
592 &dev_info
, sizeof(dev_info
));
594 x
= vbuschannel_devinfo_to_string(&dev_info
, p
,
599 off
= offsetof(struct spar_vbus_channel_protocol
, dev_info
);
601 while (off
+ sizeof(dev_info
) <=
602 visorchannel_get_nbytes(channel
)) {
603 x
= visorchannel_read(channel
,
604 off
, &dev_info
, sizeof(dev_info
));
606 x
= vbuschannel_devinfo_to_string
607 (&dev_info
, p
, remain
, i
);
611 off
+= sizeof(dev_info
);
615 return PAGE_SIZE
- remain
;
618 static DEVICE_ATTR_RO(partition_handle
);
619 static DEVICE_ATTR_RO(partition_guid
);
620 static DEVICE_ATTR_RO(partition_name
);
621 static DEVICE_ATTR_RO(channel_addr
);
622 static DEVICE_ATTR_RO(channel_bytes
);
623 static DEVICE_ATTR_RO(channel_id
);
624 static DEVICE_ATTR_RO(client_bus_info
);
626 static struct attribute
*dev_attrs
[] = {
627 &dev_attr_partition_handle
.attr
,
628 &dev_attr_partition_guid
.attr
,
629 &dev_attr_partition_name
.attr
,
630 &dev_attr_channel_addr
.attr
,
631 &dev_attr_channel_bytes
.attr
,
632 &dev_attr_channel_id
.attr
,
633 &dev_attr_client_bus_info
.attr
,
637 static struct attribute_group dev_attr_grp
= {
641 static const struct attribute_group
*visorbus_groups
[] = {
648 * define & implement display of driver attributes under
649 * /sys/bus/visorbus/drivers/<drivername>.
654 DRIVER_ATTR_version(struct device_driver
*xdrv
, char *buf
)
656 struct visor_driver
*drv
= to_visor_driver(xdrv
);
658 return snprintf(buf
, PAGE_SIZE
, "%s\n", drv
->version
);
662 register_driver_attributes(struct visor_driver
*drv
)
665 struct driver_attribute version
=
666 __ATTR(version
, S_IRUGO
, DRIVER_ATTR_version
, NULL
);
667 drv
->version_attr
= version
;
668 rc
= driver_create_file(&drv
->driver
, &drv
->version_attr
);
673 unregister_driver_attributes(struct visor_driver
*drv
)
675 driver_remove_file(&drv
->driver
, &drv
->version_attr
);
679 dev_periodic_work(void *xdev
)
681 struct visor_device
*dev
= (struct visor_device
*)xdev
;
682 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
684 down(&dev
->visordriver_callback_lock
);
685 if (drv
->channel_interrupt
)
686 drv
->channel_interrupt(dev
);
687 up(&dev
->visordriver_callback_lock
);
688 if (!visor_periodic_work_nextperiod(dev
->periodic_work
))
689 put_device(&dev
->device
);
693 dev_start_periodic_work(struct visor_device
*dev
)
695 if (dev
->being_removed
)
697 /* now up by at least 2 */
698 get_device(&dev
->device
);
699 if (!visor_periodic_work_start(dev
->periodic_work
))
700 put_device(&dev
->device
);
704 dev_stop_periodic_work(struct visor_device
*dev
)
706 if (visor_periodic_work_stop(dev
->periodic_work
))
707 put_device(&dev
->device
);
710 /** This is called automatically upon adding a visor_device (device_add), or
711 * adding a visor_driver (visorbus_register_visor_driver), but only after
712 * visorbus_match has returned 1 to indicate a successful match between
716 visordriver_probe_device(struct device
*xdev
)
719 struct visor_driver
*drv
;
720 struct visor_device
*dev
;
722 drv
= to_visor_driver(xdev
->driver
);
723 dev
= to_visor_device(xdev
);
724 down(&dev
->visordriver_callback_lock
);
725 dev
->being_removed
= false;
727 * ensure that the dev->being_removed flag is cleared before
731 get_device(&dev
->device
);
733 up(&dev
->visordriver_callback_lock
);
737 rc
= drv
->probe(dev
);
741 fix_vbus_dev_info(dev
);
742 up(&dev
->visordriver_callback_lock
);
746 put_device(&dev
->device
);
750 /** This is called when device_unregister() is called for each child device
751 * instance, to notify the appropriate visorbus_driver that the device is
752 * going away, and to decrease the reference count of the device.
755 visordriver_remove_device(struct device
*xdev
)
757 struct visor_device
*dev
;
758 struct visor_driver
*drv
;
760 dev
= to_visor_device(xdev
);
761 drv
= to_visor_driver(xdev
->driver
);
762 down(&dev
->visordriver_callback_lock
);
763 dev
->being_removed
= true;
765 * ensure that the dev->being_removed flag is set before we start the
773 up(&dev
->visordriver_callback_lock
);
774 dev_stop_periodic_work(dev
);
775 devmajorminor_remove_all_files(dev
);
777 put_device(&dev
->device
);
782 /** A particular type of visor driver calls this function to register
783 * the driver. The caller MUST fill in the following fields within the
785 * name, version, owner, channel_types, probe, remove
787 * Here's how the whole Linux bus / driver / device model works.
789 * At system start-up, the visorbus kernel module is loaded, which registers
790 * visorbus_type as a bus type, using bus_register().
792 * All kernel modules that support particular device types on a
793 * visorbus bus are loaded. Each of these kernel modules calls
794 * visorbus_register_visor_driver() in their init functions, passing a
795 * visor_driver struct. visorbus_register_visor_driver() in turn calls
796 * register_driver(&visor_driver.driver). This .driver member is
797 * initialized with generic methods (like probe), whose sole responsibility
798 * is to act as a broker for the real methods, which are within the
799 * visor_driver struct. (This is the way the subclass behavior is
800 * implemented, since visor_driver is essentially a subclass of the
801 * generic driver.) Whenever a driver_register() happens, core bus code in
802 * the kernel does (see device_attach() in drivers/base/dd.c):
804 * for each dev associated with the bus (the bus that driver is on) that
805 * does not yet have a driver
806 * if bus.match(dev,newdriver) == yes_matched ** .match specified
807 * ** during bus_register().
808 * newdriver.probe(dev) ** for visor drivers, this will call
809 * ** the generic driver.probe implemented in visorbus.c,
810 * ** which in turn calls the probe specified within the
811 * ** struct visor_driver (which was specified by the
812 * ** actual device driver as part of
813 * ** visorbus_register_visor_driver()).
815 * The above dance also happens when a new device appears.
816 * So the question is, how are devices created within the system?
817 * Basically, just call device_add(dev). See pci_bus_add_devices().
818 * pci_scan_device() shows an example of how to build a device struct. It
819 * returns the newly-created struct to pci_scan_single_device(), who adds it
820 * to the list of devices at PCIBUS.devices. That list of devices is what
821 * is traversed by pci_bus_add_devices().
824 int visorbus_register_visor_driver(struct visor_driver
*drv
)
828 drv
->driver
.name
= drv
->name
;
829 drv
->driver
.bus
= &visorbus_type
;
830 drv
->driver
.probe
= visordriver_probe_device
;
831 drv
->driver
.remove
= visordriver_remove_device
;
832 drv
->driver
.owner
= drv
->owner
;
834 /* driver_register does this:
835 * bus_add_driver(drv)
836 * ->if (drv.bus) ** (bus_type) **
838 * for each dev with bus type of drv.bus
839 * if (!dev.drv) ** no driver assigned yet **
840 * if (bus.match(dev,drv)) [visorbus_match]
842 * if (!drv.probe(dev)) [visordriver_probe_device]
846 rc
= driver_register(&drv
->driver
);
849 rc
= register_driver_attributes(drv
);
852 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver
);
854 /** A particular type of visor driver calls this function to unregister
855 * the driver, i.e., within its module_exit function.
858 visorbus_unregister_visor_driver(struct visor_driver
*drv
)
860 unregister_driver_attributes(drv
);
861 driver_unregister(&drv
->driver
);
863 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver
);
866 visorbus_read_channel(struct visor_device
*dev
, unsigned long offset
,
867 void *dest
, unsigned long nbytes
)
869 return visorchannel_read(dev
->visorchannel
, offset
, dest
, nbytes
);
871 EXPORT_SYMBOL_GPL(visorbus_read_channel
);
874 visorbus_write_channel(struct visor_device
*dev
, unsigned long offset
,
875 void *src
, unsigned long nbytes
)
877 return visorchannel_write(dev
->visorchannel
, offset
, src
, nbytes
);
879 EXPORT_SYMBOL_GPL(visorbus_write_channel
);
882 visorbus_clear_channel(struct visor_device
*dev
, unsigned long offset
, u8 ch
,
883 unsigned long nbytes
)
885 return visorchannel_clear(dev
->visorchannel
, offset
, ch
, nbytes
);
887 EXPORT_SYMBOL_GPL(visorbus_clear_channel
);
890 visorbus_registerdevnode(struct visor_device
*dev
,
891 const char *name
, int major
, int minor
)
893 return devmajorminor_create_file(dev
, name
, major
, minor
);
895 EXPORT_SYMBOL_GPL(visorbus_registerdevnode
);
897 /** We don't really have a real interrupt, so for now we just call the
898 * interrupt function periodically...
901 visorbus_enable_channel_interrupts(struct visor_device
*dev
)
903 dev_start_periodic_work(dev
);
905 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts
);
908 visorbus_disable_channel_interrupts(struct visor_device
*dev
)
910 dev_stop_periodic_work(dev
);
912 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts
);
914 /** This is how everything starts from the device end.
915 * This function is called when a channel first appears via a ControlVM
916 * message. In response, this function allocates a visor_device to
917 * correspond to the new channel, and attempts to connect it the appropriate
918 * driver. If the appropriate driver is found, the visor_driver.probe()
919 * function for that driver will be called, and will be passed the new
920 * visor_device that we just created.
922 * It's ok if the appropriate driver is not yet loaded, because in that case
923 * the new device struct will just stick around in the bus' list of devices.
924 * When the appropriate driver calls visorbus_register_visor_driver(), the
925 * visor_driver.probe() for the new driver will be called with the new
929 create_visor_device(struct visor_device
*dev
)
932 u32 chipset_bus_no
= dev
->chipset_bus_no
;
933 u32 chipset_dev_no
= dev
->chipset_dev_no
;
935 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC
, chipset_dev_no
, chipset_bus_no
,
936 POSTCODE_SEVERITY_INFO
);
938 sema_init(&dev
->visordriver_callback_lock
, 1); /* unlocked */
939 dev
->device
.bus
= &visorbus_type
;
940 dev
->device
.groups
= visorbus_dev_groups
;
941 device_initialize(&dev
->device
);
942 dev
->device
.release
= visorbus_release_device
;
943 /* keep a reference just for us (now 2) */
944 get_device(&dev
->device
);
946 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL
,
947 periodic_dev_workqueue
,
949 dev
, dev_name(&dev
->device
));
950 if (!dev
->periodic_work
) {
951 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC
, chipset_dev_no
,
956 /* bus_id must be a unique name with respect to this bus TYPE
957 * (NOT bus instance). That's why we need to include the bus
958 * number within the name.
960 dev_set_name(&dev
->device
, "vbus%u:dev%u",
961 chipset_bus_no
, chipset_dev_no
);
963 /* device_add does this:
964 * bus_add_device(dev)
965 * ->device_attach(dev)
966 * ->for each driver drv registered on the bus that dev is on
967 * if (dev.drv) ** device already has a driver **
968 * ** not sure we could ever get here... **
970 * if (bus.match(dev,drv)) [visorbus_match]
972 * if (!drv.probe(dev)) [visordriver_probe_device]
975 * Note that device_add does NOT fail if no driver failed to
976 * claim the device. The device will be linked onto
977 * bus_type.klist_devices regardless (use bus_for_each_dev).
979 rc
= device_add(&dev
->device
);
981 POSTCODE_LINUX_3(DEVICE_ADD_PC
, chipset_bus_no
,
986 rc
= register_devmajorminor_attributes(dev
);
988 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC
, chipset_dev_no
,
993 list_add_tail(&dev
->list_all
, &list_all_device_instances
);
997 device_unregister(&dev
->device
);
999 put_device(&dev
->device
);
1004 remove_visor_device(struct visor_device
*dev
)
1006 list_del(&dev
->list_all
);
1007 unregister_devmajorminor_attributes(dev
);
1008 put_device(&dev
->device
);
1009 device_unregister(&dev
->device
);
1013 get_vbus_header_info(struct visorchannel
*chan
,
1014 struct spar_vbus_headerinfo
*hdr_info
)
1018 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan
)))
1020 if (visorchannel_read(chan
, sizeof(struct channel_header
), hdr_info
,
1021 sizeof(*hdr_info
)) < 0) {
1024 if (hdr_info
->struct_bytes
< sizeof(struct spar_vbus_headerinfo
))
1026 if (hdr_info
->device_info_struct_bytes
<
1027 sizeof(struct ultra_vbus_deviceinfo
)) {
1035 /* Write the contents of <info> to the struct
1036 * spar_vbus_channel_protocol.chp_info. */
1039 write_vbus_chp_info(struct visorchannel
*chan
,
1040 struct spar_vbus_headerinfo
*hdr_info
,
1041 struct ultra_vbus_deviceinfo
*info
)
1043 int off
= sizeof(struct channel_header
) + hdr_info
->chp_info_offset
;
1045 if (hdr_info
->chp_info_offset
== 0)
1048 if (visorchannel_write(chan
, off
, info
, sizeof(*info
)) < 0)
1053 /* Write the contents of <info> to the struct
1054 * spar_vbus_channel_protocol.bus_info. */
1057 write_vbus_bus_info(struct visorchannel
*chan
,
1058 struct spar_vbus_headerinfo
*hdr_info
,
1059 struct ultra_vbus_deviceinfo
*info
)
1061 int off
= sizeof(struct channel_header
) + hdr_info
->bus_info_offset
;
1063 if (hdr_info
->bus_info_offset
== 0)
1066 if (visorchannel_write(chan
, off
, info
, sizeof(*info
)) < 0)
1071 /* Write the contents of <info> to the
1072 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1075 write_vbus_dev_info(struct visorchannel
*chan
,
1076 struct spar_vbus_headerinfo
*hdr_info
,
1077 struct ultra_vbus_deviceinfo
*info
, int devix
)
1080 (sizeof(struct channel_header
) + hdr_info
->dev_info_offset
) +
1081 (hdr_info
->device_info_struct_bytes
* devix
);
1083 if (hdr_info
->dev_info_offset
== 0)
1086 if (visorchannel_write(chan
, off
, info
, sizeof(*info
)) < 0)
1091 /* For a child device just created on a client bus, fill in
1092 * information about the driver that is controlling this device into
1093 * the the appropriate slot within the vbus channel of the bus
1097 fix_vbus_dev_info(struct visor_device
*visordev
)
1100 struct visor_device
*bdev
;
1101 struct visor_driver
*visordrv
;
1102 int bus_no
= visordev
->chipset_bus_no
;
1103 int dev_no
= visordev
->chipset_dev_no
;
1104 struct ultra_vbus_deviceinfo dev_info
;
1105 const char *chan_type_name
= NULL
;
1106 struct spar_vbus_headerinfo
*hdr_info
;
1108 if (!visordev
->device
.driver
)
1111 hdr_info
= (struct spar_vbus_headerinfo
*)visordev
->vbus_hdr_info
;
1115 bdev
= visorbus_get_device_by_id(bus_no
, BUS_ROOT_DEVICE
, NULL
);
1119 visordrv
= to_visor_driver(visordev
->device
.driver
);
1121 /* Within the list of device types (by GUID) that the driver
1122 * says it supports, find out which one of those types matches
1123 * the type of this device, so that we can include the device
1126 for (i
= 0; visordrv
->channel_types
[i
].name
; i
++) {
1127 if (memcmp(&visordrv
->channel_types
[i
].guid
,
1128 &visordev
->channel_type_guid
,
1129 sizeof(visordrv
->channel_types
[i
].guid
)) == 0) {
1130 chan_type_name
= visordrv
->channel_types
[i
].name
;
1135 bus_device_info_init(&dev_info
, chan_type_name
,
1136 visordrv
->name
, visordrv
->version
,
1138 write_vbus_dev_info(bdev
->visorchannel
, hdr_info
, &dev_info
, dev_no
);
1140 /* Re-write bus+chipset info, because it is possible that this
1141 * was previously written by our evil counterpart, virtpci.
1143 write_vbus_chp_info(bdev
->visorchannel
, hdr_info
, &chipset_driverinfo
);
1144 write_vbus_bus_info(bdev
->visorchannel
, hdr_info
,
1145 &clientbus_driverinfo
);
1148 /** Create a device instance for the visor bus itself.
1151 create_bus_instance(struct visor_device
*dev
)
1154 int id
= dev
->chipset_bus_no
;
1155 struct spar_vbus_headerinfo
*hdr_info
;
1157 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC
, POSTCODE_SEVERITY_INFO
);
1159 hdr_info
= kzalloc(sizeof(*hdr_info
), GFP_KERNEL
);
1165 dev_set_name(&dev
->device
, "visorbus%d", id
);
1166 dev
->device
.bus
= &visorbus_type
;
1167 dev
->device
.groups
= visorbus_groups
;
1168 dev
->device
.release
= visorbus_release_busdevice
;
1170 if (device_register(&dev
->device
) < 0) {
1171 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC
, id
,
1172 POSTCODE_SEVERITY_ERR
);
1177 if (get_vbus_header_info(dev
->visorchannel
, hdr_info
) >= 0) {
1178 dev
->vbus_hdr_info
= (void *)hdr_info
;
1179 write_vbus_chp_info(dev
->visorchannel
, hdr_info
,
1180 &chipset_driverinfo
);
1181 write_vbus_bus_info(dev
->visorchannel
, hdr_info
,
1182 &clientbus_driverinfo
);
1187 list_add_tail(&dev
->list_all
, &list_all_bus_instances
);
1188 dev_set_drvdata(&dev
->device
, dev
);
1197 /** Remove a device instance for the visor bus itself.
1200 remove_bus_instance(struct visor_device
*dev
)
1202 /* Note that this will result in the release method for
1203 * dev->dev being called, which will call
1204 * visorbus_release_busdevice(). This has something to do with
1205 * the put_device() done in device_unregister(), but I have never
1206 * successfully been able to trace thru the code to see where/how
1207 * release() gets called. But I know it does.
1210 if (dev
->visorchannel
) {
1211 visorchannel_destroy(dev
->visorchannel
);
1212 dev
->visorchannel
= NULL
;
1214 kfree(dev
->vbus_hdr_info
);
1215 list_del(&dev
->list_all
);
1216 device_unregister(&dev
->device
);
1219 /** Create and register the one-and-only one instance of
1220 * the visor bus type (visorbus_type).
1223 create_bus_type(void)
1227 rc
= bus_register(&visorbus_type
);
1231 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1234 remove_bus_type(void)
1236 bus_unregister(&visorbus_type
);
1239 /** Remove all child visor bus device instances.
1242 remove_all_visor_devices(void)
1244 struct list_head
*listentry
, *listtmp
;
1246 list_for_each_safe(listentry
, listtmp
, &list_all_device_instances
) {
1247 struct visor_device
*dev
= list_entry(listentry
,
1248 struct visor_device
,
1250 remove_visor_device(dev
);
1255 chipset_bus_create(struct visor_device
*dev
)
1258 u32 bus_no
= dev
->chipset_bus_no
;
1260 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC
, bus_no
, POSTCODE_SEVERITY_INFO
);
1261 rc
= create_bus_instance(dev
);
1262 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC
, bus_no
, POSTCODE_SEVERITY_INFO
);
1265 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC
, bus_no
,
1266 POSTCODE_SEVERITY_ERR
);
1268 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC
, bus_no
,
1269 POSTCODE_SEVERITY_INFO
);
1271 if (chipset_responders
.bus_create
)
1272 (*chipset_responders
.bus_create
) (dev
, rc
);
1276 chipset_bus_destroy(struct visor_device
*dev
)
1278 remove_bus_instance(dev
);
1279 if (chipset_responders
.bus_destroy
)
1280 (*chipset_responders
.bus_destroy
)(dev
, 0);
1284 chipset_device_create(struct visor_device
*dev_info
)
1287 u32 bus_no
= dev_info
->chipset_bus_no
;
1288 u32 dev_no
= dev_info
->chipset_dev_no
;
1290 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC
, dev_no
, bus_no
,
1291 POSTCODE_SEVERITY_INFO
);
1293 rc
= create_visor_device(dev_info
);
1294 if (chipset_responders
.device_create
)
1295 chipset_responders
.device_create(dev_info
, rc
);
1298 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC
, dev_no
, bus_no
,
1299 POSTCODE_SEVERITY_ERR
);
1301 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC
, dev_no
, bus_no
,
1302 POSTCODE_SEVERITY_INFO
);
1306 chipset_device_destroy(struct visor_device
*dev_info
)
1308 remove_visor_device(dev_info
);
1310 if (chipset_responders
.device_destroy
)
1311 (*chipset_responders
.device_destroy
) (dev_info
, 0);
1314 /* This is the callback function specified for a function driver, to
1315 * be called when a pending "pause device" operation has been
1319 pause_state_change_complete(struct visor_device
*dev
, int status
)
1324 dev
->pausing
= false;
1325 if (!chipset_responders
.device_pause
) /* this can never happen! */
1328 /* Notify the chipset driver that the pause is complete, which
1329 * will presumably want to send some sort of response to the
1331 (*chipset_responders
.device_pause
) (dev
, status
);
1334 /* This is the callback function specified for a function driver, to
1335 * be called when a pending "resume device" operation has been
1339 resume_state_change_complete(struct visor_device
*dev
, int status
)
1344 dev
->resuming
= false;
1345 if (!chipset_responders
.device_resume
) /* this can never happen! */
1348 /* Notify the chipset driver that the resume is complete,
1349 * which will presumably want to send some sort of response to
1351 (*chipset_responders
.device_resume
) (dev
, status
);
1354 /* Tell the subordinate function driver for a specific device to pause
1355 * or resume that device. Result is returned asynchronously via a
1356 * callback function.
1359 initiate_chipset_device_pause_resume(struct visor_device
*dev
, bool is_pause
)
1362 struct visor_driver
*drv
= NULL
;
1363 void (*notify_func
)(struct visor_device
*dev
, int response
) = NULL
;
1366 notify_func
= chipset_responders
.device_pause
;
1368 notify_func
= chipset_responders
.device_resume
;
1372 drv
= to_visor_driver(dev
->device
.driver
);
1376 if (dev
->pausing
|| dev
->resuming
)
1379 /* Note that even though both drv->pause() and drv->resume
1380 * specify a callback function, it is NOT necessary for us to
1381 * increment our local module usage count. Reason is, there
1382 * is already a linkage dependency between child function
1383 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1384 * visorbus while child function drivers are still running.
1390 dev
->pausing
= true;
1391 x
= drv
->pause(dev
, pause_state_change_complete
);
1393 /* This should be done at BUS resume time, but an
1394 * existing problem prevents us from ever getting a bus
1395 * resume... This hack would fail to work should we
1396 * ever have a bus that contains NO devices, since we
1397 * would never even get here in that case. */
1398 fix_vbus_dev_info(dev
);
1402 dev
->resuming
= true;
1403 x
= drv
->resume(dev
, resume_state_change_complete
);
1407 dev
->pausing
= false;
1409 dev
->resuming
= false;
1416 (*notify_func
)(dev
, rc
);
1421 chipset_device_pause(struct visor_device
*dev_info
)
1423 initiate_chipset_device_pause_resume(dev_info
, true);
1427 chipset_device_resume(struct visor_device
*dev_info
)
1429 initiate_chipset_device_pause_resume(dev_info
, false);
1432 struct channel_size_info
{
1434 unsigned long min_size
;
1435 unsigned long max_size
;
1443 POSTCODE_LINUX_3(DRIVER_ENTRY_PC
, rc
, POSTCODE_SEVERITY_INFO
);
1444 bus_device_info_init(&clientbus_driverinfo
,
1445 "clientbus", "visorbus",
1448 rc
= create_bus_type();
1450 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC
, DIAG_SEVERITY_ERR
);
1454 periodic_dev_workqueue
= create_singlethread_workqueue("visorbus_dev");
1455 if (!periodic_dev_workqueue
) {
1456 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC
, DIAG_SEVERITY_ERR
);
1461 /* This enables us to receive notifications when devices appear for
1462 * which this service partition is to be a server for.
1464 visorchipset_register_busdev(&chipset_notifiers
,
1465 &chipset_responders
,
1466 &chipset_driverinfo
);
1472 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC
, rc
,
1473 POSTCODE_SEVERITY_ERR
);
1480 struct list_head
*listentry
, *listtmp
;
1482 visorchipset_register_busdev(NULL
, NULL
, NULL
);
1483 remove_all_visor_devices();
1485 flush_workqueue(periodic_dev_workqueue
); /* better not be any work! */
1486 destroy_workqueue(periodic_dev_workqueue
);
1487 periodic_dev_workqueue
= NULL
;
1489 if (periodic_test_workqueue
) {
1490 cancel_delayed_work(&periodic_work
);
1491 flush_workqueue(periodic_test_workqueue
);
1492 destroy_workqueue(periodic_test_workqueue
);
1493 periodic_test_workqueue
= NULL
;
1496 list_for_each_safe(listentry
, listtmp
, &list_all_bus_instances
) {
1497 struct visor_device
*dev
= list_entry(listentry
,
1501 remove_bus_instance(dev
);
1506 module_param_named(debug
, visorbus_debug
, int, S_IRUGO
);
1507 MODULE_PARM_DESC(visorbus_debug
, "1 to debug");
1509 module_param_named(forcematch
, visorbus_forcematch
, int, S_IRUGO
);
1510 MODULE_PARM_DESC(visorbus_forcematch
,
1511 "1 to force a successful dev <--> drv match");
1513 module_param_named(forcenomatch
, visorbus_forcenomatch
, int, S_IRUGO
);
1514 MODULE_PARM_DESC(visorbus_forcenomatch
,
1515 "1 to force an UNsuccessful dev <--> drv match");
1517 module_param_named(debugref
, visorbus_debugref
, int, S_IRUGO
);
1518 MODULE_PARM_DESC(visorbus_debugref
, "1 to debug reference counting");