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"
24 #include "periodic_work.h"
25 #include "vbuschannel.h"
26 #include "guestlinuxdebug.h"
27 #include "vbusdeviceinfo.h"
29 #define MYDRVNAME "visorbus"
31 /* module parameters */
33 int visorbus_forcematch
;
34 int visorbus_forcenomatch
;
35 #define MAXDEVICETEST 4
36 int visorbus_devicetest
;
37 int visorbus_debugref
;
38 #define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
40 /** This is the private data that we store for each bus device instance.
42 struct visorbus_devdata
{
43 int devno
; /* this is the chipset busNo */
44 struct list_head list_all
;
47 struct visorchannel
*chan
; /* channel area for bus itself */
49 struct spar_vbus_headerinfo vbus_hdr_info
;
52 /* These forward declarations are required since our drivers are out-of-tree.
53 * The structures referenced are kernel-private and are not in the headers, but
54 * it is impossible to make a functioning bus driver without them.
56 struct subsys_private
{
58 struct kset
*devices_kset
;
60 struct kset
*drivers_kset
;
61 struct klist klist_devices
;
62 struct klist klist_drivers
;
63 struct blocking_notifier_head bus_notifier
;
64 unsigned int drivers_autoprobe
:1;
67 struct list_head class_interfaces
;
68 struct kset glue_dirs
;
69 struct mutex class_mutex
; /* ignore */
73 struct bus_type_private
{
75 struct kset
*drivers_kset
;
76 struct kset
*devices_kset
;
77 struct klist klist_devices
;
78 struct klist klist_drivers
;
79 struct blocking_notifier_head bus_notifier
;
80 unsigned int drivers_autoprobe
:1;
84 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
85 #define POLLJIFFIES_TESTWORK 100
86 #define POLLJIFFIES_NORMALCHANNEL 10
88 static int visorbus_uevent(struct device
*xdev
, struct kobj_uevent_env
*env
);
89 static int visorbus_match(struct device
*xdev
, struct device_driver
*xdrv
);
90 static void fix_vbus_dev_info(struct visor_device
*visordev
);
92 /** This describes the TYPE of bus.
93 * (Don't confuse this with an INSTANCE of the bus.)
95 static struct bus_type visorbus_type
= {
97 .match
= visorbus_match
,
98 .uevent
= visorbus_uevent
,
101 static struct delayed_work periodic_work
;
103 /* YES, we need 2 workqueues.
104 * The reason is, workitems on the test queue may need to cancel
105 * workitems on the other queue. You will be in for trouble if you try to
106 * do this with workitems queued on the same workqueue.
108 static struct workqueue_struct
*periodic_test_workqueue
;
109 static struct workqueue_struct
*periodic_dev_workqueue
;
110 static long long bus_count
; /** number of bus instances */
111 static long long total_devices_created
;
112 /** ever-increasing */
114 static void chipset_bus_create(u32 bus_no
);
115 static void chipset_bus_destroy(u32 bus_no
);
116 static void chipset_device_create(u32 bus_no
, u32 dev_no
);
117 static void chipset_device_destroy(u32 bus_no
, u32 dev_no
);
118 static void chipset_device_pause(u32 bus_no
, u32 dev_no
);
119 static void chipset_device_resume(u32 bus_no
, u32 dev_no
);
121 /** These functions are implemented herein, and are called by the chipset
122 * driver to notify us about specific events.
124 static struct visorchipset_busdev_notifiers chipset_notifiers
= {
125 .bus_create
= chipset_bus_create
,
126 .bus_destroy
= chipset_bus_destroy
,
127 .device_create
= chipset_device_create
,
128 .device_destroy
= chipset_device_destroy
,
129 .device_pause
= chipset_device_pause
,
130 .device_resume
= chipset_device_resume
,
133 /** These functions are implemented in the chipset driver, and we call them
134 * herein when we want to acknowledge a specific event.
136 static struct visorchipset_busdev_responders chipset_responders
;
138 /* filled in with info about parent chipset driver when we register with it */
139 static struct ultra_vbus_deviceinfo chipset_driverinfo
;
140 /* filled in with info about this driver, wrt it servicing client busses */
141 static struct ultra_vbus_deviceinfo clientbus_driverinfo
;
143 /** list of visorbus_devdata structs, linked via .list_all */
144 static LIST_HEAD(list_all_bus_instances
);
145 /** list of visor_device structs, linked via .list_all */
146 static LIST_HEAD(list_all_device_instances
);
149 visorbus_uevent(struct device
*xdev
, struct kobj_uevent_env
*env
)
151 if (add_uevent_var(env
, "VERSION=%s", VERSION
))
156 /* This is called automatically upon adding a visor_device (device_add), or
157 * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
158 * provided driver can control the specified device.
161 visorbus_match(struct device
*xdev
, struct device_driver
*xdrv
)
163 uuid_le channel_type
;
166 struct visor_device
*dev
;
167 struct visor_driver
*drv
;
169 dev
= to_visor_device(xdev
);
170 drv
= to_visor_driver(xdrv
);
171 channel_type
= visorchannel_get_uuid(dev
->visorchannel
);
172 if (visorbus_forcematch
) {
176 if (visorbus_forcenomatch
)
179 if (!drv
->channel_types
)
182 (uuid_le_cmp(drv
->channel_types
[i
].guid
, NULL_UUID_LE
) != 0) ||
183 (drv
->channel_types
[i
].name
);
185 if (uuid_le_cmp(drv
->channel_types
[i
].guid
,
186 channel_type
) == 0) {
194 /** This is called when device_unregister() is called for the bus device
195 * instance, after all other tasks involved with destroying the device
199 visorbus_release_busdevice(struct device
*xdev
)
201 struct visorbus_devdata
*devdata
= dev_get_drvdata(xdev
);
203 dev_set_drvdata(xdev
, NULL
);
208 /** This is called when device_unregister() is called for each child
212 visorbus_release_device(struct device
*xdev
)
214 struct visor_device
*dev
= to_visor_device(xdev
);
216 if (dev
->periodic_work
) {
217 visor_periodic_work_destroy(dev
->periodic_work
);
218 dev
->periodic_work
= NULL
;
220 if (dev
->visorchannel
) {
221 visorchannel_destroy(dev
->visorchannel
);
222 dev
->visorchannel
= NULL
;
227 /* Implement publishing of device node attributes under:
229 * /sys/bus/visorbus<x>/dev<y>/devmajorminor
233 #define to_devmajorminor_attr(_attr) \
234 container_of(_attr, struct devmajorminor_attribute, attr)
235 #define to_visor_device_from_kobjdevmajorminor(obj) \
236 container_of(obj, struct visor_device, kobjdevmajorminor)
238 struct devmajorminor_attribute
{
239 struct attribute attr
;
241 ssize_t (*show
)(struct visor_device
*, int slot
, char *buf
);
242 ssize_t (*store
)(struct visor_device
*, int slot
, const char *buf
,
246 static ssize_t
DEVMAJORMINOR_ATTR(struct visor_device
*dev
, int slot
, char *buf
)
248 int maxdevnodes
= ARRAY_SIZE(dev
->devnodes
) / sizeof(dev
->devnodes
[0]);
250 if (slot
< 0 || slot
>= maxdevnodes
)
252 return snprintf(buf
, PAGE_SIZE
, "%d:%d\n",
253 dev
->devnodes
[slot
].major
, dev
->devnodes
[slot
].minor
);
257 devmajorminor_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
259 struct devmajorminor_attribute
*devmajorminor_attr
=
260 to_devmajorminor_attr(attr
);
261 struct visor_device
*dev
= to_visor_device_from_kobjdevmajorminor(kobj
);
264 if (devmajorminor_attr
->show
)
265 ret
= devmajorminor_attr
->show(dev
,
266 devmajorminor_attr
->slot
, buf
);
271 devmajorminor_attr_store(struct kobject
*kobj
,
272 struct attribute
*attr
, const char *buf
, size_t count
)
274 struct devmajorminor_attribute
*devmajorminor_attr
=
275 to_devmajorminor_attr(attr
);
276 struct visor_device
*dev
= to_visor_device_from_kobjdevmajorminor(kobj
);
279 if (devmajorminor_attr
->store
)
280 ret
= devmajorminor_attr
->store(dev
,
281 devmajorminor_attr
->slot
,
286 static int register_devmajorminor_attributes(struct visor_device
*dev
);
289 devmajorminor_create_file(struct visor_device
*dev
, const char *name
,
290 int major
, int minor
)
292 int maxdevnodes
= ARRAY_SIZE(dev
->devnodes
) / sizeof(dev
->devnodes
[0]);
293 struct devmajorminor_attribute
*myattr
= NULL
;
294 int x
= -1, rc
= 0, slot
= -1;
296 register_devmajorminor_attributes(dev
);
297 for (slot
= 0; slot
< maxdevnodes
; slot
++)
298 if (!dev
->devnodes
[slot
].attr
)
300 if (slot
== maxdevnodes
) {
304 myattr
= kmalloc(sizeof(*myattr
), GFP_KERNEL
);
309 memset(myattr
, 0, sizeof(struct devmajorminor_attribute
));
310 myattr
->show
= DEVMAJORMINOR_ATTR
;
311 myattr
->store
= NULL
;
313 myattr
->attr
.name
= name
;
314 myattr
->attr
.mode
= S_IRUGO
;
315 dev
->devnodes
[slot
].attr
= myattr
;
316 dev
->devnodes
[slot
].major
= major
;
317 dev
->devnodes
[slot
].minor
= minor
;
318 x
= sysfs_create_file(&dev
->kobjdevmajorminor
, &myattr
->attr
);
323 kobject_uevent(&dev
->device
.kobj
, KOBJ_ONLINE
);
328 dev
->devnodes
[slot
].attr
= NULL
;
334 devmajorminor_remove_file(struct visor_device
*dev
, int slot
)
336 int maxdevnodes
= ARRAY_SIZE(dev
->devnodes
) / sizeof(dev
->devnodes
[0]);
337 struct devmajorminor_attribute
*myattr
= NULL
;
339 if (slot
< 0 || slot
>= maxdevnodes
)
341 myattr
= (struct devmajorminor_attribute
*)(dev
->devnodes
[slot
].attr
);
344 sysfs_remove_file(&dev
->kobjdevmajorminor
, &myattr
->attr
);
345 kobject_uevent(&dev
->device
.kobj
, KOBJ_OFFLINE
);
346 dev
->devnodes
[slot
].attr
= NULL
;
351 devmajorminor_remove_all_files(struct visor_device
*dev
)
354 int maxdevnodes
= ARRAY_SIZE(dev
->devnodes
) / sizeof(dev
->devnodes
[0]);
356 for (i
= 0; i
< maxdevnodes
; i
++)
357 devmajorminor_remove_file(dev
, i
);
360 static const struct sysfs_ops devmajorminor_sysfs_ops
= {
361 .show
= devmajorminor_attr_show
,
362 .store
= devmajorminor_attr_store
,
365 static struct kobj_type devmajorminor_kobj_type
= {
366 .sysfs_ops
= &devmajorminor_sysfs_ops
370 register_devmajorminor_attributes(struct visor_device
*dev
)
374 if (dev
->kobjdevmajorminor
.parent
)
375 goto away
; /* already registered */
376 x
= kobject_init_and_add(&dev
->kobjdevmajorminor
,
377 &devmajorminor_kobj_type
, &dev
->device
.kobj
,
384 kobject_uevent(&dev
->kobjdevmajorminor
, KOBJ_ADD
);
391 unregister_devmajorminor_attributes(struct visor_device
*dev
)
393 if (!dev
->kobjdevmajorminor
.parent
)
394 return; /* already unregistered */
395 devmajorminor_remove_all_files(dev
);
397 kobject_del(&dev
->kobjdevmajorminor
);
398 kobject_put(&dev
->kobjdevmajorminor
);
399 dev
->kobjdevmajorminor
.parent
= NULL
;
402 /* Implement publishing of channel attributes under:
404 * /sys/bus/visorbus<x>/dev<y>/channel
408 #define to_channel_attr(_attr) \
409 container_of(_attr, struct channel_attribute, attr)
410 #define to_visor_device_from_kobjchannel(obj) \
411 container_of(obj, struct visor_device, kobjchannel)
413 struct channel_attribute
{
414 struct attribute attr
;
415 ssize_t (*show
)(struct visor_device
*, char *buf
);
416 ssize_t (*store
)(struct visor_device
*, const char *buf
, size_t count
);
419 /* begin implementation of specific channel attributes to appear under
420 * /sys/bus/visorbus<x>/dev<y>/channel
422 static ssize_t
devicechannel_attr_physaddr(struct visor_device
*dev
, char *buf
)
424 if (!dev
->visorchannel
)
426 return snprintf(buf
, PAGE_SIZE
, "0x%Lx\n",
427 visorchannel_get_physaddr(dev
->visorchannel
));
430 static ssize_t
devicechannel_attr_nbytes(struct visor_device
*dev
, char *buf
)
432 if (!dev
->visorchannel
)
434 return snprintf(buf
, PAGE_SIZE
, "0x%lx\n",
435 visorchannel_get_nbytes(dev
->visorchannel
));
438 static ssize_t
devicechannel_attr_clientpartition(struct visor_device
*dev
,
440 if (!dev
->visorchannel
)
442 return snprintf(buf
, PAGE_SIZE
, "0x%Lx\n",
443 visorchannel_get_clientpartition(dev
->visorchannel
));
446 static ssize_t
devicechannel_attr_typeguid(struct visor_device
*dev
, char *buf
)
450 if (!dev
->visorchannel
)
452 return snprintf(buf
, PAGE_SIZE
, "%s\n",
453 visorchannel_id(dev
->visorchannel
, s
));
456 static ssize_t
devicechannel_attr_zoneguid(struct visor_device
*dev
, char *buf
)
460 if (!dev
->visorchannel
)
462 return snprintf(buf
, PAGE_SIZE
, "%s\n",
463 visorchannel_zoneid(dev
->visorchannel
, s
));
466 static ssize_t
devicechannel_attr_typename(struct visor_device
*dev
, char *buf
)
469 struct bus_type
*xbus
= dev
->device
.bus
;
470 struct device_driver
*xdrv
= dev
->device
.driver
;
471 struct visor_driver
*drv
= NULL
;
473 if (!dev
->visorchannel
|| !xbus
|| !xdrv
)
475 i
= xbus
->match(&dev
->device
, xdrv
);
478 drv
= to_visor_driver(xdrv
);
479 return snprintf(buf
, PAGE_SIZE
, "%s\n", drv
->channel_types
[i
- 1].name
);
482 static ssize_t
devicechannel_attr_dump(struct visor_device
*dev
, char *buf
)
485 /* TODO: replace this with debugfs code
486 struct seq_file *m = NULL;
487 if (dev->visorchannel == NULL)
489 m = visor_seq_file_new_buffer(buf, PAGE_SIZE - 1);
492 visorchannel_debug(dev->visorchannel, 1, m, 0);
494 visor_seq_file_done_buffer(m);
500 static struct channel_attribute all_channel_attrs
[] = {
501 __ATTR(physaddr
, S_IRUGO
,
502 devicechannel_attr_physaddr
, NULL
),
503 __ATTR(nbytes
, S_IRUGO
,
504 devicechannel_attr_nbytes
, NULL
),
505 __ATTR(clientpartition
, S_IRUGO
,
506 devicechannel_attr_clientpartition
, NULL
),
507 __ATTR(typeguid
, S_IRUGO
,
508 devicechannel_attr_typeguid
, NULL
),
509 __ATTR(zoneguid
, S_IRUGO
,
510 devicechannel_attr_zoneguid
, NULL
),
511 __ATTR(typename
, S_IRUGO
,
512 devicechannel_attr_typename
, NULL
),
513 __ATTR(dump
, S_IRUGO
,
514 devicechannel_attr_dump
, NULL
),
517 /* end implementation of specific channel attributes */
519 static ssize_t
channel_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
522 struct channel_attribute
*channel_attr
= to_channel_attr(attr
);
523 struct visor_device
*dev
= to_visor_device_from_kobjchannel(kobj
);
526 if (channel_attr
->show
)
527 ret
= channel_attr
->show(dev
, buf
);
531 static ssize_t
channel_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
532 const char *buf
, size_t count
)
534 struct channel_attribute
*channel_attr
= to_channel_attr(attr
);
535 struct visor_device
*dev
= to_visor_device_from_kobjchannel(kobj
);
538 if (channel_attr
->store
)
539 ret
= channel_attr
->store(dev
, buf
, count
);
543 static int channel_create_file(struct visor_device
*dev
,
544 struct channel_attribute
*attr
)
546 return sysfs_create_file(&dev
->kobjchannel
, &attr
->attr
);
549 static void channel_remove_file(struct visor_device
*dev
,
550 struct channel_attribute
*attr
)
552 sysfs_remove_file(&dev
->kobjchannel
, &attr
->attr
);
555 static const struct sysfs_ops channel_sysfs_ops
= {
556 .show
= channel_attr_show
,
557 .store
= channel_attr_store
,
560 static struct kobj_type channel_kobj_type
= {
561 .sysfs_ops
= &channel_sysfs_ops
564 int register_channel_attributes(struct visor_device
*dev
)
566 int rc
= 0, i
= 0, x
= 0;
568 if (dev
->kobjchannel
.parent
)
569 goto away
; /* already registered */
570 x
= kobject_init_and_add(&dev
->kobjchannel
, &channel_kobj_type
,
571 &dev
->device
.kobj
, "channel");
577 kobject_uevent(&dev
->kobjchannel
, KOBJ_ADD
);
580 i
< sizeof(all_channel_attrs
) / sizeof(struct channel_attribute
);
582 x
= channel_create_file(dev
, &all_channel_attrs
[i
]);
585 channel_remove_file(dev
, &all_channel_attrs
[i
]);
586 kobject_del(&dev
->kobjchannel
);
587 kobject_put(&dev
->kobjchannel
);
595 void unregister_channel_attributes(struct visor_device
*dev
)
599 if (!dev
->kobjchannel
.parent
)
600 return; /* already unregistered */
602 i
< sizeof(all_channel_attrs
) / sizeof(struct channel_attribute
);
604 channel_remove_file(dev
, &all_channel_attrs
[i
]);
606 kobject_del(&dev
->kobjchannel
);
607 kobject_put(&dev
->kobjchannel
);
608 dev
->kobjchannel
.parent
= NULL
;
610 /* This is actually something they forgot to put in the kernel.
611 * struct bus_type in the kernel SHOULD have a "busses" member, which
612 * should be treated similarly to the "devices" and "drivers" members.
614 * - a "businst_attribute" analogous to the existing "bus_attribute"
615 * - a "businst_create_file" and "businst_remove_file" analogous to the
616 * existing "bus_create_file" and "bus_remove_file".
617 * That's what I created businst.c and businst.h to do.
619 * We want to add the "busses" sub-tree in sysfs, where we will house the
620 * names and properties of each bus instance:
622 * /sys/bus/<bustypename>/
625 * <devname1> --> /sys/devices/<businstancename><devname1>
626 * <devname2> --> /sys/devices/<businstancename><devname2>
628 * <driverinstancename1>
629 * <driverinstance1property1>
630 * <driverinstance1property2>
632 * <driverinstancename2>
633 * <driverinstance2property1>
634 * <driverinstance2property2>
637 * >> <businstancename1>
638 * >> <businstance1property1>
639 * >> <businstance1property2>
641 * >> <businstancename2>
642 * >> <businstance2property1>
643 * >> <businstance2property2>
646 * I considered adding bus instance properties under
647 * /sys/devices/<businstancename>. But I thought there may be existing
648 * notions that ONLY device sub-trees should live under
649 * /sys/devices/<businstancename>. So I stayed out of there.
653 struct businst_attribute
{
654 struct attribute attr
;
655 ssize_t (*show
)(struct visorbus_devdata
*, char *buf
);
656 ssize_t (*store
)(struct visorbus_devdata
*, const char *buf
,
660 #define to_businst_attr(_attr) \
661 container_of(_attr, struct businst_attribute, attr)
662 #define to_visorbus_devdata(obj) \
663 container_of(obj, struct visorbus_devdata, kobj)
666 businst_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
669 struct businst_attribute
*businst_attr
= to_businst_attr(attr
);
670 struct visorbus_devdata
*bus
= to_visorbus_devdata(kobj
);
673 if (businst_attr
->show
)
674 ret
= businst_attr
->show(bus
, buf
);
679 businst_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
680 const char *buf
, size_t count
)
682 struct businst_attribute
*businst_attr
= to_businst_attr(attr
);
683 struct visorbus_devdata
*bus
= to_visorbus_devdata(kobj
);
686 if (businst_attr
->store
)
687 ret
= businst_attr
->store(bus
, buf
, count
);
692 businst_create_file(struct visorbus_devdata
*bus
,
693 struct businst_attribute
*attr
)
695 return sysfs_create_file(&bus
->kobj
, &attr
->attr
);
699 businst_remove_file(struct visorbus_devdata
*bus
,
700 struct businst_attribute
*attr
)
702 sysfs_remove_file(&bus
->kobj
, &attr
->attr
);
705 static const struct sysfs_ops businst_sysfs_ops
= {
706 .show
= businst_attr_show
,
707 .store
= businst_attr_store
,
710 static struct kobj_type businst_kobj_type
= {
711 .sysfs_ops
= &businst_sysfs_ops
714 static struct kset businstances
= { /* should actually be a member of
718 /* BUS type attributes
720 * define & implement display of bus attributes under
726 BUSTYPE_ATTR_version(struct bus_type
*bus
, char *buf
)
728 return snprintf(buf
, PAGE_SIZE
, "%s\n", VERSION
);
731 static struct bus_attribute bustype_attr_version
=
732 __ATTR(version
, S_IRUGO
, BUSTYPE_ATTR_version
, NULL
);
735 register_bustype_attributes(void)
739 rc
= bus_create_file(&visorbus_type
, &bustype_attr_version
);
743 /* Here we make up for the fact that bus_type does not yet have a
744 * member to keep track of multiple bus instances for a given bus
745 * type. This is useful for stashing properties for each bus
748 kobject_set_name(&businstances
.kobj
, "busses");
749 businstances
.kobj
.ktype
= &businst_kobj_type
;
750 businstances
.kobj
.parent
= &visorbus_type
.p
->subsys
.kobj
;
751 rc
= kset_register(&businstances
);
761 unregister_bustype_attributes(void)
763 bus_remove_file(&visorbus_type
, &bustype_attr_version
);
764 kset_unregister(&businstances
);
767 /* BUS instance attributes
769 * define & implement display of bus attributes under
770 * /sys/bus/visorbus/busses/visorbus<n>.
772 * This is a bit hoaky because the kernel does not yet have the infrastructure
773 * to separate bus INSTANCE attributes from bus TYPE attributes...
774 * so we roll our own. See businst.c / businst.h.
778 static ssize_t
businst_attr_partition_handle(struct visorbus_devdata
*businst
,
780 struct visorchipset_bus_info bus_info
;
783 if (businst
&& visorchipset_get_bus_info(businst
->devno
, &bus_info
))
784 len
= snprintf(buf
, PAGE_SIZE
,
786 (unsigned long long)bus_info
.partition_handle
);
790 static ssize_t
businst_attr_partition_guid(struct visorbus_devdata
*businst
,
792 struct visorchipset_bus_info bus_info
;
795 if (businst
&& visorchipset_get_bus_info(businst
->devno
, &bus_info
))
796 len
= snprintf(buf
, PAGE_SIZE
, "{%pUb}\n",
797 &bus_info
.partition_uuid
);
801 static ssize_t
businst_attr_partition_name(struct visorbus_devdata
*businst
,
803 struct visorchipset_bus_info bus_info
;
807 visorchipset_get_bus_info(businst
->devno
, &bus_info
) &&
809 len
= snprintf(buf
, PAGE_SIZE
, "%s\n", bus_info
.name
);
813 static ssize_t
businst_attr_channel_addr(struct visorbus_devdata
*businst
,
815 struct visorchipset_bus_info bus_info
;
818 if (businst
&& visorchipset_get_bus_info(businst
->devno
, &bus_info
))
819 len
= snprintf(buf
, PAGE_SIZE
, "0x%Lx\n", (unsigned long long)
820 bus_info
.chan_info
.channel_addr
);
824 static ssize_t
businst_attr_nchannel_bytes(struct visorbus_devdata
*businst
,
826 struct visorchipset_bus_info bus_info
;
829 if (businst
&& visorchipset_get_bus_info(businst
->devno
, &bus_info
))
830 len
= snprintf(buf
, PAGE_SIZE
, "0x%Lx\n", (unsigned long long)
831 bus_info
.chan_info
.n_channel_bytes
);
835 static ssize_t
businst_attr_channel_id(struct visorbus_devdata
*businst
,
839 if (businst
&& businst
->chan
) {
840 visorchannel_id(businst
->chan
, buf
);
847 static ssize_t
businst_attr_client_bus_info(struct visorbus_devdata
*businst
,
849 struct visorchipset_bus_info bus_info
;
850 int i
, x
, remain
= PAGE_SIZE
;
854 struct ultra_vbus_deviceinfo dev_info
;
857 if (businst
&& businst
->chan
) {
858 if (visorchipset_get_bus_info(businst
->devno
, &bus_info
) &&
860 partition_name
= bus_info
.name
;
861 x
= snprintf(p
, remain
,
862 "Client device / client driver info for %s partition (vbus #%d):\n",
863 partition_name
, businst
->devno
);
866 x
= visorchannel_read(businst
->chan
,
868 spar_vbus_channel_protocol
,
870 &dev_info
, sizeof(dev_info
));
872 x
= vbuschannel_devinfo_to_string(&dev_info
, p
,
877 x
= visorchannel_read(businst
->chan
,
879 spar_vbus_channel_protocol
,
881 &dev_info
, sizeof(dev_info
));
883 x
= vbuschannel_devinfo_to_string(&dev_info
, p
,
888 off
= offsetof(struct spar_vbus_channel_protocol
, dev_info
);
890 while (off
+ sizeof(dev_info
) <=
891 visorchannel_get_nbytes(businst
->chan
)) {
892 x
= visorchannel_read(businst
->chan
,
893 off
, &dev_info
, sizeof(dev_info
));
895 x
= vbuschannel_devinfo_to_string
896 (&dev_info
, p
, remain
, i
);
900 off
+= sizeof(dev_info
);
904 return PAGE_SIZE
- remain
;
907 static struct businst_attribute ba_partition_handle
=
908 __ATTR(partition_handle
, S_IRUGO
, businst_attr_partition_handle
, NULL
);
909 static struct businst_attribute ba_partition_guid
=
910 __ATTR(partition_guid
, S_IRUGO
, businst_attr_partition_guid
, NULL
);
911 static struct businst_attribute ba_partition_name
=
912 __ATTR(partition_name
, S_IRUGO
, businst_attr_partition_name
, NULL
);
913 static struct businst_attribute ba_channel_addr
=
914 __ATTR(channel_addr
, S_IRUGO
, businst_attr_channel_addr
, NULL
);
915 static struct businst_attribute ba_nchannel_bytes
=
916 __ATTR(nchannel_bytes
, S_IRUGO
, businst_attr_nchannel_bytes
, NULL
);
917 static struct businst_attribute ba_channel_id
=
918 __ATTR(channel_id
, S_IRUGO
, businst_attr_channel_id
, NULL
);
919 static struct businst_attribute ba_client_bus_info
=
920 __ATTR(client_bus_info
, S_IRUGO
, businst_attr_client_bus_info
, NULL
);
923 register_businst_attributes(struct visorbus_devdata
*businst
)
927 businst
->kobj
.kset
= &businstances
; /* identify parent sysfs dir */
928 rc
= kobject_init_and_add(&businst
->kobj
, &businst_kobj_type
,
929 NULL
, "visorbus%d", businst
->devno
);
933 rc
= businst_create_file(businst
, &ba_partition_handle
);
937 rc
= businst_create_file(businst
, &ba_partition_guid
);
941 rc
= businst_create_file(businst
, &ba_partition_name
);
945 rc
= businst_create_file(businst
, &ba_channel_addr
);
949 rc
= businst_create_file(businst
, &ba_nchannel_bytes
);
953 rc
= businst_create_file(businst
, &ba_channel_id
);
957 rc
= businst_create_file(businst
, &ba_client_bus_info
);
961 kobject_uevent(&businst
->kobj
, KOBJ_ADD
);
969 unregister_businst_attributes(struct visorbus_devdata
*businst
)
971 businst_remove_file(businst
, &ba_partition_handle
);
972 businst_remove_file(businst
, &ba_partition_guid
);
973 businst_remove_file(businst
, &ba_partition_name
);
974 businst_remove_file(businst
, &ba_channel_addr
);
975 businst_remove_file(businst
, &ba_nchannel_bytes
);
976 businst_remove_file(businst
, &ba_channel_id
);
977 businst_remove_file(businst
, &ba_client_bus_info
);
978 kobject_put(&businst
->kobj
);
983 * define & implement display of driver attributes under
984 * /sys/bus/visorbus/drivers/<drivername>.
989 DRIVER_ATTR_version(struct device_driver
*xdrv
, char *buf
)
991 struct visor_driver
*drv
= to_visor_driver(xdrv
);
993 return snprintf(buf
, PAGE_SIZE
, "%s\n", drv
->version
);
997 register_driver_attributes(struct visor_driver
*drv
)
1000 struct driver_attribute version
=
1001 __ATTR(version
, S_IRUGO
, DRIVER_ATTR_version
, NULL
);
1002 drv
->version_attr
= version
;
1003 rc
= driver_create_file(&drv
->driver
, &drv
->version_attr
);
1008 unregister_driver_attributes(struct visor_driver
*drv
)
1010 driver_remove_file(&drv
->driver
, &drv
->version_attr
);
1013 /* DEVICE attributes
1015 * define & implement display of device attributes under
1016 * /sys/bus/visorbus/devices/<devicename>.
1020 #define DEVATTR(nam, func) { \
1021 .attr = { .name = __stringify(nam), \
1023 .owner = THIS_MODULE }, \
1027 static struct device_attribute visor_device_attrs
[] = {
1028 /* DEVATTR(channel_nbytes, DEVICE_ATTR_channel_nbytes), */
1033 dev_periodic_work(void *xdev
)
1035 struct visor_device
*dev
= (struct visor_device
*)xdev
;
1036 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
1038 down(&dev
->visordriver_callback_lock
);
1039 if (drv
->channel_interrupt
)
1040 drv
->channel_interrupt(dev
);
1041 up(&dev
->visordriver_callback_lock
);
1042 if (!visor_periodic_work_nextperiod(dev
->periodic_work
))
1043 put_device(&dev
->device
);
1047 dev_start_periodic_work(struct visor_device
*dev
)
1049 if (dev
->being_removed
)
1051 /* now up by at least 2 */
1052 get_device(&dev
->device
);
1053 if (!visor_periodic_work_start(dev
->periodic_work
))
1054 put_device(&dev
->device
);
1058 dev_stop_periodic_work(struct visor_device
*dev
)
1060 if (visor_periodic_work_stop(dev
->periodic_work
))
1061 put_device(&dev
->device
);
1064 /** This is called automatically upon adding a visor_device (device_add), or
1065 * adding a visor_driver (visorbus_register_visor_driver), but only after
1066 * visorbus_match has returned 1 to indicate a successful match between
1067 * driver and device.
1070 visordriver_probe_device(struct device
*xdev
)
1073 struct visor_driver
*drv
;
1074 struct visor_device
*dev
;
1076 drv
= to_visor_driver(xdev
->driver
);
1077 dev
= to_visor_device(xdev
);
1078 down(&dev
->visordriver_callback_lock
);
1079 dev
->being_removed
= false;
1081 * ensure that the dev->being_removed flag is cleared before
1082 * we start the probe
1085 get_device(&dev
->device
);
1087 up(&dev
->visordriver_callback_lock
);
1091 rc
= drv
->probe(dev
);
1095 fix_vbus_dev_info(dev
);
1096 up(&dev
->visordriver_callback_lock
);
1100 put_device(&dev
->device
);
1101 /* We could get here more than once if the child driver module is
1102 * unloaded and re-loaded while devices are present. That's why we
1103 * need a flag to be sure that we only respond to the device_create
1104 * once. We cannot respond to the device_create prior to here,
1105 * because until we call drv->probe() above, the channel has not been
1108 if (!dev
->responded_to_device_create
) {
1109 dev
->responded_to_device_create
= true;
1110 if (chipset_responders
.device_create
)
1111 (*chipset_responders
.device_create
)(dev
->chipset_bus_no
,
1112 dev
->chipset_dev_no
,
1118 /** This is called when device_unregister() is called for each child device
1119 * instance, to notify the appropriate visorbus_driver that the device is
1120 * going away, and to decrease the reference count of the device.
1123 visordriver_remove_device(struct device
*xdev
)
1126 struct visor_device
*dev
;
1127 struct visor_driver
*drv
;
1129 dev
= to_visor_device(xdev
);
1130 drv
= to_visor_driver(xdev
->driver
);
1131 down(&dev
->visordriver_callback_lock
);
1132 dev
->being_removed
= true;
1134 * ensure that the dev->being_removed flag is set before we start the
1142 up(&dev
->visordriver_callback_lock
);
1143 dev_stop_periodic_work(dev
);
1144 devmajorminor_remove_all_files(dev
);
1146 put_device(&dev
->device
);
1151 /** A particular type of visor driver calls this function to register
1152 * the driver. The caller MUST fill in the following fields within the
1154 * name, version, owner, channel_types, probe, remove
1156 * Here's how the whole Linux bus / driver / device model works.
1158 * At system start-up, the visorbus kernel module is loaded, which registers
1159 * visorbus_type as a bus type, using bus_register().
1161 * All kernel modules that support particular device types on a
1162 * visorbus bus are loaded. Each of these kernel modules calls
1163 * visorbus_register_visor_driver() in their init functions, passing a
1164 * visor_driver struct. visorbus_register_visor_driver() in turn calls
1165 * register_driver(&visor_driver.driver). This .driver member is
1166 * initialized with generic methods (like probe), whose sole responsibility
1167 * is to act as a broker for the real methods, which are within the
1168 * visor_driver struct. (This is the way the subclass behavior is
1169 * implemented, since visor_driver is essentially a subclass of the
1170 * generic driver.) Whenever a driver_register() happens, core bus code in
1171 * the kernel does (see device_attach() in drivers/base/dd.c):
1173 * for each dev associated with the bus (the bus that driver is on) that
1174 * does not yet have a driver
1175 * if bus.match(dev,newdriver) == yes_matched ** .match specified
1176 * ** during bus_register().
1177 * newdriver.probe(dev) ** for visor drivers, this will call
1178 * ** the generic driver.probe implemented in visorbus.c,
1179 * ** which in turn calls the probe specified within the
1180 * ** struct visor_driver (which was specified by the
1181 * ** actual device driver as part of
1182 * ** visorbus_register_visor_driver()).
1184 * The above dance also happens when a new device appears.
1185 * So the question is, how are devices created within the system?
1186 * Basically, just call device_add(dev). See pci_bus_add_devices().
1187 * pci_scan_device() shows an example of how to build a device struct. It
1188 * returns the newly-created struct to pci_scan_single_device(), who adds it
1189 * to the list of devices at PCIBUS.devices. That list of devices is what
1190 * is traversed by pci_bus_add_devices().
1193 int visorbus_register_visor_driver(struct visor_driver
*drv
)
1197 drv
->driver
.name
= drv
->name
;
1198 drv
->driver
.bus
= &visorbus_type
;
1199 drv
->driver
.probe
= visordriver_probe_device
;
1200 drv
->driver
.remove
= visordriver_remove_device
;
1201 drv
->driver
.owner
= drv
->owner
;
1203 /* driver_register does this:
1204 * bus_add_driver(drv)
1205 * ->if (drv.bus) ** (bus_type) **
1206 * driver_attach(drv)
1207 * for each dev with bus type of drv.bus
1208 * if (!dev.drv) ** no driver assigned yet **
1209 * if (bus.match(dev,drv)) [visorbus_match]
1211 * if (!drv.probe(dev)) [visordriver_probe_device]
1215 rc
= driver_register(&drv
->driver
);
1218 rc
= register_driver_attributes(drv
);
1221 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver
);
1223 /** A particular type of visor driver calls this function to unregister
1224 * the driver, i.e., within its module_exit function.
1227 visorbus_unregister_visor_driver(struct visor_driver
*drv
)
1229 unregister_driver_attributes(drv
);
1230 driver_unregister(&drv
->driver
);
1232 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver
);
1235 visorbus_read_channel(struct visor_device
*dev
, unsigned long offset
,
1236 void *dest
, unsigned long nbytes
)
1238 return visorchannel_read(dev
->visorchannel
, offset
, dest
, nbytes
);
1240 EXPORT_SYMBOL_GPL(visorbus_read_channel
);
1243 visorbus_write_channel(struct visor_device
*dev
, unsigned long offset
,
1244 void *src
, unsigned long nbytes
)
1246 return visorchannel_write(dev
->visorchannel
, offset
, src
, nbytes
);
1248 EXPORT_SYMBOL_GPL(visorbus_write_channel
);
1251 visorbus_clear_channel(struct visor_device
*dev
, unsigned long offset
, u8 ch
,
1252 unsigned long nbytes
)
1254 return visorchannel_clear(dev
->visorchannel
, offset
, ch
, nbytes
);
1256 EXPORT_SYMBOL_GPL(visorbus_clear_channel
);
1259 visorbus_registerdevnode(struct visor_device
*dev
,
1260 const char *name
, int major
, int minor
)
1262 return devmajorminor_create_file(dev
, name
, major
, minor
);
1264 EXPORT_SYMBOL_GPL(visorbus_registerdevnode
);
1266 /** We don't really have a real interrupt, so for now we just call the
1267 * interrupt function periodically...
1270 visorbus_enable_channel_interrupts(struct visor_device
*dev
)
1272 dev_start_periodic_work(dev
);
1274 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts
);
1277 visorbus_disable_channel_interrupts(struct visor_device
*dev
)
1279 dev_stop_periodic_work(dev
);
1281 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts
);
1283 /** This is how everything starts from the device end.
1284 * This function is called when a channel first appears via a ControlVM
1285 * message. In response, this function allocates a visor_device to
1286 * correspond to the new channel, and attempts to connect it the appropriate
1287 * driver. If the appropriate driver is found, the visor_driver.probe()
1288 * function for that driver will be called, and will be passed the new
1289 * visor_device that we just created.
1291 * It's ok if the appropriate driver is not yet loaded, because in that case
1292 * the new device struct will just stick around in the bus' list of devices.
1293 * When the appropriate driver calls visorbus_register_visor_driver(), the
1294 * visor_driver.probe() for the new driver will be called with the new
1298 create_visor_device(struct visorbus_devdata
*devdata
,
1299 unsigned long chipset_bus_no
, unsigned long chipset_dev_no
,
1300 struct visorchipset_channel_info chan_info
,
1301 u64 partition_handle
)
1304 struct visorchannel
*visorchannel
= NULL
;
1305 struct visor_device
*dev
= NULL
;
1306 bool gotten
= false, registered1
= false, registered2
= false;
1308 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC
, chipset_dev_no
, chipset_bus_no
,
1309 POSTCODE_SEVERITY_INFO
);
1310 /* prepare chan_hdr (abstraction to read/write channel memory) */
1311 visorchannel
= visorchannel_create(chan_info
.channel_addr
,
1313 chan_info
.n_channel_bytes
,
1314 chan_info
.channel_type_uuid
);
1315 if (!visorchannel
) {
1316 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC
, chipset_dev_no
,
1320 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
1322 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC
, chipset_dev_no
,
1327 memset(dev
, 0, sizeof(struct visor_device
));
1328 dev
->visorchannel
= visorchannel
;
1329 dev
->channel_type_guid
= chan_info
.channel_type_uuid
;
1330 dev
->channel_bytes
= chan_info
.n_channel_bytes
;
1331 dev
->chipset_bus_no
= chipset_bus_no
;
1332 dev
->chipset_dev_no
= chipset_dev_no
;
1333 dev
->device
.parent
= devdata
->dev
;
1334 sema_init(&dev
->visordriver_callback_lock
, 1); /* unlocked */
1335 dev
->device
.bus
= &visorbus_type
;
1336 device_initialize(&dev
->device
);
1337 dev
->device
.release
= visorbus_release_device
;
1338 /* keep a reference just for us (now 2) */
1339 get_device(&dev
->device
);
1341 dev
->periodic_work
=
1342 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL
,
1343 periodic_dev_workqueue
,
1345 dev
, dev_name(&dev
->device
));
1346 if (!dev
->periodic_work
) {
1347 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC
, chipset_dev_no
,
1352 /* bus_id must be a unique name with respect to this bus TYPE
1353 * (NOT bus instance). That's why we need to include the bus
1354 * number within the name.
1356 dev_set_name(&dev
->device
, "vbus%lu:dev%lu",
1357 chipset_bus_no
, chipset_dev_no
);
1359 /* device_add does this:
1360 * bus_add_device(dev)
1361 * ->device_attach(dev)
1362 * ->for each driver drv registered on the bus that dev is on
1363 * if (dev.drv) ** device already has a driver **
1364 * ** not sure we could ever get here... **
1366 * if (bus.match(dev,drv)) [visorbus_match]
1368 * if (!drv.probe(dev)) [visordriver_probe_device]
1371 * Note that device_add does NOT fail if no driver failed to
1372 * claim the device. The device will be linked onto
1373 * bus_type.klist_devices regardless (use bus_for_each_dev).
1375 rc
= device_add(&dev
->device
);
1377 POSTCODE_LINUX_3(DEVICE_ADD_PC
, chipset_bus_no
,
1382 /* note: device_register is simply device_initialize + device_add */
1383 rc
= register_channel_attributes(dev
);
1385 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC
, chipset_dev_no
,
1392 rc
= register_devmajorminor_attributes(dev
);
1394 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC
, chipset_dev_no
,
1405 unregister_devmajorminor_attributes(dev
);
1407 unregister_channel_attributes(dev
);
1409 put_device(&dev
->device
);
1411 visorchannel_destroy(visorchannel
);
1414 total_devices_created
++;
1415 list_add_tail(&dev
->list_all
, &list_all_device_instances
);
1421 remove_visor_device(struct visor_device
*dev
)
1423 list_del(&dev
->list_all
);
1424 unregister_devmajorminor_attributes(dev
);
1425 unregister_channel_attributes(dev
);
1426 put_device(&dev
->device
);
1427 device_unregister(&dev
->device
);
1430 static struct visor_device
*
1431 find_visor_device_by_channel(u64 channel_physaddr
)
1433 struct list_head
*listentry
, *listtmp
;
1435 list_for_each_safe(listentry
, listtmp
, &list_all_device_instances
) {
1436 struct visor_device
*dev
= list_entry(listentry
,
1437 struct visor_device
,
1439 if (visorchannel_get_physaddr(dev
->visorchannel
) ==
1447 init_vbus_channel(struct visorchannel
*chan
)
1450 unsigned long allocated_bytes
= visorchannel_get_nbytes(chan
);
1451 struct spar_vbus_channel_protocol
*x
=
1452 kmalloc(sizeof(struct spar_vbus_channel_protocol
),
1455 POSTCODE_LINUX_3(VBUS_CHANNEL_ENTRY_PC
, rc
, POSTCODE_SEVERITY_INFO
);
1458 POSTCODE_LINUX_2(MALLOC_FAILURE_PC
, POSTCODE_SEVERITY_ERR
);
1461 if (visorchannel_clear(chan
, 0, 0, allocated_bytes
) < 0) {
1462 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC
,
1463 POSTCODE_SEVERITY_ERR
);
1466 if (visorchannel_read
1467 (chan
, 0, x
, sizeof(struct spar_vbus_channel_protocol
)) < 0) {
1468 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC
,
1469 POSTCODE_SEVERITY_ERR
);
1472 if (!SPAR_VBUS_CHANNEL_OK_SERVER(allocated_bytes
)) {
1473 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC
,
1474 POSTCODE_SEVERITY_ERR
);
1478 if (visorchannel_write
1479 (chan
, 0, x
, sizeof(struct spar_vbus_channel_protocol
)) < 0) {
1480 POSTCODE_LINUX_3(VBUS_CHANNEL_FAILURE_PC
, chan
,
1481 POSTCODE_SEVERITY_ERR
);
1485 POSTCODE_LINUX_3(VBUS_CHANNEL_EXIT_PC
, chan
, POSTCODE_SEVERITY_INFO
);
1495 get_vbus_header_info(struct visorchannel
*chan
,
1496 struct spar_vbus_headerinfo
*hdr_info
)
1500 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan
)))
1502 if (visorchannel_read(chan
, sizeof(struct channel_header
), hdr_info
,
1503 sizeof(*hdr_info
)) < 0) {
1506 if (hdr_info
->struct_bytes
< sizeof(struct spar_vbus_headerinfo
))
1508 if (hdr_info
->device_info_struct_bytes
<
1509 sizeof(struct ultra_vbus_deviceinfo
)) {
1517 /* Write the contents of <info> to the struct
1518 * spar_vbus_channel_protocol.chp_info. */
1521 write_vbus_chp_info(struct visorchannel
*chan
,
1522 struct spar_vbus_headerinfo
*hdr_info
,
1523 struct ultra_vbus_deviceinfo
*info
)
1525 int off
= sizeof(struct channel_header
) + hdr_info
->chp_info_offset
;
1527 if (hdr_info
->chp_info_offset
== 0)
1530 if (visorchannel_write(chan
, off
, info
, sizeof(*info
)) < 0)
1535 /* Write the contents of <info> to the struct
1536 * spar_vbus_channel_protocol.bus_info. */
1539 write_vbus_bus_info(struct visorchannel
*chan
,
1540 struct spar_vbus_headerinfo
*hdr_info
,
1541 struct ultra_vbus_deviceinfo
*info
)
1543 int off
= sizeof(struct channel_header
) + hdr_info
->bus_info_offset
;
1545 if (hdr_info
->bus_info_offset
== 0)
1548 if (visorchannel_write(chan
, off
, info
, sizeof(*info
)) < 0)
1553 /* Write the contents of <info> to the
1554 * struct spar_vbus_channel_protocol.dev_info[<devix>].
1557 write_vbus_dev_info(struct visorchannel
*chan
,
1558 struct spar_vbus_headerinfo
*hdr_info
,
1559 struct ultra_vbus_deviceinfo
*info
, int devix
)
1562 (sizeof(struct channel_header
) + hdr_info
->dev_info_offset
) +
1563 (hdr_info
->device_info_struct_bytes
* devix
);
1565 if (hdr_info
->dev_info_offset
== 0)
1568 if (visorchannel_write(chan
, off
, info
, sizeof(*info
)) < 0)
1573 /* For a child device just created on a client bus, fill in
1574 * information about the driver that is controlling this device into
1575 * the the appropriate slot within the vbus channel of the bus
1579 fix_vbus_dev_info(struct visor_device
*visordev
)
1582 struct visorchipset_bus_info bus_info
;
1583 struct visorbus_devdata
*devdata
= NULL
;
1584 struct visor_driver
*visordrv
;
1585 int bus_no
= visordev
->chipset_bus_no
;
1586 int dev_no
= visordev
->chipset_dev_no
;
1587 struct ultra_vbus_deviceinfo dev_info
;
1588 const char *chan_type_name
= NULL
;
1590 if (!visordev
->device
.driver
)
1593 visordrv
= to_visor_driver(visordev
->device
.driver
);
1594 if (!visorchipset_get_bus_info(bus_no
, &bus_info
))
1597 devdata
= (struct visorbus_devdata
*)(bus_info
.bus_driver_context
);
1601 if (!devdata
->vbus_valid
)
1604 /* Within the list of device types (by GUID) that the driver
1605 * says it supports, find out which one of those types matches
1606 * the type of this device, so that we can include the device
1609 for (i
= 0; visordrv
->channel_types
[i
].name
; i
++) {
1610 if (memcmp(&visordrv
->channel_types
[i
].guid
,
1611 &visordev
->channel_type_guid
,
1612 sizeof(visordrv
->channel_types
[i
].guid
)) == 0) {
1613 chan_type_name
= visordrv
->channel_types
[i
].name
;
1618 bus_device_info_init(&dev_info
, chan_type_name
,
1619 visordrv
->name
, visordrv
->version
,
1621 write_vbus_dev_info(devdata
->chan
,
1622 &devdata
->vbus_hdr_info
, &dev_info
, dev_no
);
1624 /* Re-write bus+chipset info, because it is possible that this
1625 * was previously written by our evil counterpart, virtpci.
1627 write_vbus_chp_info(devdata
->chan
, &devdata
->vbus_hdr_info
,
1628 &chipset_driverinfo
);
1629 write_vbus_bus_info(devdata
->chan
, &devdata
->vbus_hdr_info
,
1630 &clientbus_driverinfo
);
1633 /** Create a device instance for the visor bus itself.
1635 static struct visorbus_devdata
*
1636 create_bus_instance(int id
)
1638 struct visorbus_devdata
*rc
= NULL
;
1639 struct visorbus_devdata
*devdata
= NULL
;
1641 struct visorchipset_bus_info bus_info
;
1643 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC
, POSTCODE_SEVERITY_INFO
);
1644 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
1646 POSTCODE_LINUX_2(MALLOC_FAILURE_PC
, POSTCODE_SEVERITY_ERR
);
1650 memset(dev
, 0, sizeof(struct device
));
1651 dev_set_name(dev
, "visorbus%d", id
);
1652 dev
->release
= visorbus_release_busdevice
;
1653 if (device_register(dev
) < 0) {
1654 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC
, id
,
1655 POSTCODE_SEVERITY_ERR
);
1659 devdata
= kmalloc(sizeof(*devdata
), GFP_KERNEL
);
1661 POSTCODE_LINUX_2(MALLOC_FAILURE_PC
, POSTCODE_SEVERITY_ERR
);
1665 memset(devdata
, 0, sizeof(struct visorbus_devdata
));
1666 devdata
->devno
= id
;
1668 if ((visorchipset_get_bus_info(id
, &bus_info
)) &&
1669 (bus_info
.chan_info
.channel_addr
> 0) &&
1670 (bus_info
.chan_info
.n_channel_bytes
> 0)) {
1671 u64 channel_addr
= bus_info
.chan_info
.channel_addr
;
1672 unsigned long n_channel_bytes
=
1674 bus_info
.chan_info
.n_channel_bytes
;
1675 uuid_le channel_type_guid
=
1676 bus_info
.chan_info
.channel_type_uuid
;
1678 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
);