2 * Copyright � 2010 - 2015 UNISYS CORPORATION
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more
16 #include <linux/ctype.h>
17 #include <linux/debugfs.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/uuid.h>
23 #include "visorbus_private.h"
25 static const guid_t visor_vbus_channel_guid
= VISOR_VBUS_CHANNEL_GUID
;
27 /* Display string that is guaranteed to be no longer the 99 characters */
29 #define POLLJIFFIES_NORMALCHANNEL 10
31 /* stores whether bus_registration was successful */
32 static bool initialized
;
33 static struct dentry
*visorbus_debugfs_dir
;
36 * DEVICE type attributes
38 * The modalias file will contain the guid of the device.
40 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
43 struct visor_device
*vdev
;
46 vdev
= to_visor_device(dev
);
47 guid
= visorchannel_get_guid(vdev
->visorchannel
);
48 return sprintf(buf
, "visorbus:%pUl\n", guid
);
50 static DEVICE_ATTR_RO(modalias
);
52 static struct attribute
*visorbus_dev_attrs
[] = {
53 &dev_attr_modalias
.attr
,
57 ATTRIBUTE_GROUPS(visorbus_dev
);
59 /* filled in with info about parent chipset driver when we register with it */
60 static struct visor_vbus_deviceinfo chipset_driverinfo
;
61 /* filled in with info about this driver, wrt it servicing client busses */
62 static struct visor_vbus_deviceinfo clientbus_driverinfo
;
64 /* list of visor_device structs, linked via .list_all */
65 static LIST_HEAD(list_all_bus_instances
);
66 /* list of visor_device structs, linked via .list_all */
67 static LIST_HEAD(list_all_device_instances
);
70 * Generic function useful for validating any type of channel when it is
71 * received by the client that will be accessing the channel.
72 * Note that <logCtx> is only needed for callers in the EFI environment, and
73 * is used to pass the EFI_DIAG_CAPTURE_PROTOCOL needed to log messages.
75 int visor_check_channel(struct channel_header
*ch
, struct device
*dev
,
76 const guid_t
*expected_guid
, char *chname
,
77 u64 expected_min_bytes
, u32 expected_version
,
78 u64 expected_signature
)
80 if (!guid_is_null(expected_guid
)) {
81 /* caller wants us to verify type GUID */
82 if (!guid_equal(&ch
->chtype
, expected_guid
)) {
83 dev_err(dev
, "Channel mismatch on channel=%s(%pUL) field=type expected=%pUL actual=%pUL\n",
84 chname
, expected_guid
, expected_guid
,
89 /* verify channel size */
90 if (expected_min_bytes
> 0) {
91 if (ch
->size
< expected_min_bytes
) {
92 dev_err(dev
, "Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
93 chname
, expected_guid
,
94 (unsigned long long)expected_min_bytes
,
99 /* verify channel version */
100 if (expected_version
> 0) {
101 if (ch
->version_id
!= expected_version
) {
102 dev_err(dev
, "Channel mismatch on channel=%s(%pUL) field=version expected=0x%-8.8lx actual=0x%-8.8x\n",
103 chname
, expected_guid
,
104 (unsigned long)expected_version
,
109 /* verify channel signature */
110 if (expected_signature
> 0) {
111 if (ch
->signature
!= expected_signature
) {
112 dev_err(dev
, "Channel mismatch on channel=%s(%pUL) field=signature expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
113 chname
, expected_guid
, expected_signature
,
121 static int visorbus_uevent(struct device
*xdev
, struct kobj_uevent_env
*env
)
123 struct visor_device
*dev
;
126 dev
= to_visor_device(xdev
);
127 guid
= visorchannel_get_guid(dev
->visorchannel
);
128 return add_uevent_var(env
, "MODALIAS=visorbus:%pUl", guid
);
132 * visorbus_match() - called automatically upon adding a visor_device
133 * (device_add), or adding a visor_driver
134 * (visorbus_register_visor_driver)
135 * @xdev: struct device for the device being matched
136 * @xdrv: struct device_driver for driver to match device against
138 * Return: 1 iff the provided driver can control the specified device
140 static int visorbus_match(struct device
*xdev
, struct device_driver
*xdrv
)
142 const guid_t
*channel_type
;
144 struct visor_device
*dev
;
145 struct visor_driver
*drv
;
146 struct visorchannel
*chan
;
148 dev
= to_visor_device(xdev
);
149 channel_type
= visorchannel_get_guid(dev
->visorchannel
);
150 drv
= to_visor_driver(xdrv
);
151 chan
= dev
->visorchannel
;
152 if (!drv
->channel_types
)
154 for (i
= 0; !guid_is_null(&drv
->channel_types
[i
].guid
); i
++)
155 if (guid_equal(&drv
->channel_types
[i
].guid
, channel_type
) &&
156 visor_check_channel(visorchannel_get_header(chan
),
158 &drv
->channel_types
[i
].guid
,
159 (char *)drv
->channel_types
[i
].name
,
160 drv
->channel_types
[i
].min_bytes
,
161 drv
->channel_types
[i
].version
,
162 VISOR_CHANNEL_SIGNATURE
))
168 * This describes the TYPE of bus.
169 * (Don't confuse this with an INSTANCE of the bus.)
171 static struct bus_type visorbus_type
= {
173 .match
= visorbus_match
,
174 .uevent
= visorbus_uevent
,
175 .dev_groups
= visorbus_dev_groups
,
178 struct visor_busdev
{
183 static int match_visorbus_dev_by_id(struct device
*dev
, void *data
)
185 struct visor_device
*vdev
= to_visor_device(dev
);
186 struct visor_busdev
*id
= data
;
188 if (vdev
->chipset_bus_no
== id
->bus_no
&&
189 vdev
->chipset_dev_no
== id
->dev_no
)
194 struct visor_device
*visorbus_get_device_by_id(u32 bus_no
, u32 dev_no
,
195 struct visor_device
*from
)
198 struct device
*dev_start
= NULL
;
199 struct visor_busdev id
= {
205 dev_start
= &from
->device
;
206 dev
= bus_find_device(&visorbus_type
, dev_start
, (void *)&id
,
207 match_visorbus_dev_by_id
);
210 return to_visor_device(dev
);
214 * visorbus_release_busdevice() - called when device_unregister() is called for
215 * the bus device instance, after all other tasks
216 * involved with destroying the dev are complete
217 * @xdev: struct device for the bus being released
219 static void visorbus_release_busdevice(struct device
*xdev
)
221 struct visor_device
*dev
= dev_get_drvdata(xdev
);
223 debugfs_remove(dev
->debugfs_bus_info
);
224 debugfs_remove_recursive(dev
->debugfs_dir
);
225 visorchannel_destroy(dev
->visorchannel
);
230 * visorbus_release_device() - called when device_unregister() is called for
231 * each child device instance
232 * @xdev: struct device for the visor device being released
234 static void visorbus_release_device(struct device
*xdev
)
236 struct visor_device
*dev
= to_visor_device(xdev
);
238 visorchannel_destroy(dev
->visorchannel
);
243 * BUS specific channel attributes to appear under
244 * /sys/bus/visorbus<x>/dev<y>/channel
247 static ssize_t
physaddr_show(struct device
*dev
, struct device_attribute
*attr
,
250 struct visor_device
*vdev
= to_visor_device(dev
);
252 return sprintf(buf
, "0x%llx\n",
253 visorchannel_get_physaddr(vdev
->visorchannel
));
255 static DEVICE_ATTR_RO(physaddr
);
257 static ssize_t
nbytes_show(struct device
*dev
, struct device_attribute
*attr
,
260 struct visor_device
*vdev
= to_visor_device(dev
);
262 return sprintf(buf
, "0x%lx\n",
263 visorchannel_get_nbytes(vdev
->visorchannel
));
265 static DEVICE_ATTR_RO(nbytes
);
267 static ssize_t
clientpartition_show(struct device
*dev
,
268 struct device_attribute
*attr
, char *buf
)
270 struct visor_device
*vdev
= to_visor_device(dev
);
272 return sprintf(buf
, "0x%llx\n",
273 visorchannel_get_clientpartition(vdev
->visorchannel
));
275 static DEVICE_ATTR_RO(clientpartition
);
277 static ssize_t
typeguid_show(struct device
*dev
, struct device_attribute
*attr
,
280 struct visor_device
*vdev
= to_visor_device(dev
);
281 char typeid[LINESIZE
];
283 return sprintf(buf
, "%s\n",
284 visorchannel_id(vdev
->visorchannel
, typeid));
286 static DEVICE_ATTR_RO(typeguid
);
288 static ssize_t
zoneguid_show(struct device
*dev
, struct device_attribute
*attr
,
291 struct visor_device
*vdev
= to_visor_device(dev
);
292 char zoneid
[LINESIZE
];
294 return sprintf(buf
, "%s\n",
295 visorchannel_zoneid(vdev
->visorchannel
, zoneid
));
297 static DEVICE_ATTR_RO(zoneguid
);
299 static ssize_t
typename_show(struct device
*dev
, struct device_attribute
*attr
,
303 struct bus_type
*xbus
= dev
->bus
;
304 struct device_driver
*xdrv
= dev
->driver
;
305 struct visor_driver
*drv
= NULL
;
309 i
= xbus
->match(dev
, xdrv
);
312 drv
= to_visor_driver(xdrv
);
313 return sprintf(buf
, "%s\n", drv
->channel_types
[i
- 1].name
);
315 static DEVICE_ATTR_RO(typename
);
317 static struct attribute
*channel_attrs
[] = {
318 &dev_attr_physaddr
.attr
,
319 &dev_attr_nbytes
.attr
,
320 &dev_attr_clientpartition
.attr
,
321 &dev_attr_typeguid
.attr
,
322 &dev_attr_zoneguid
.attr
,
323 &dev_attr_typename
.attr
,
327 ATTRIBUTE_GROUPS(channel
);
330 * BUS instance attributes
332 * define & implement display of bus attributes under
333 * /sys/bus/visorbus/devices/visorbus<n>.
335 static ssize_t
partition_handle_show(struct device
*dev
,
336 struct device_attribute
*attr
, char *buf
)
338 struct visor_device
*vdev
= to_visor_device(dev
);
339 u64 handle
= visorchannel_get_clientpartition(vdev
->visorchannel
);
341 return sprintf(buf
, "0x%llx\n", handle
);
343 static DEVICE_ATTR_RO(partition_handle
);
345 static ssize_t
partition_guid_show(struct device
*dev
,
346 struct device_attribute
*attr
, char *buf
)
348 struct visor_device
*vdev
= to_visor_device(dev
);
350 return sprintf(buf
, "{%pUb}\n", &vdev
->partition_guid
);
352 static DEVICE_ATTR_RO(partition_guid
);
354 static ssize_t
partition_name_show(struct device
*dev
,
355 struct device_attribute
*attr
, char *buf
)
357 struct visor_device
*vdev
= to_visor_device(dev
);
359 return sprintf(buf
, "%s\n", vdev
->name
);
361 static DEVICE_ATTR_RO(partition_name
);
363 static ssize_t
channel_addr_show(struct device
*dev
,
364 struct device_attribute
*attr
, char *buf
)
366 struct visor_device
*vdev
= to_visor_device(dev
);
367 u64 addr
= visorchannel_get_physaddr(vdev
->visorchannel
);
369 return sprintf(buf
, "0x%llx\n", addr
);
371 static DEVICE_ATTR_RO(channel_addr
);
373 static ssize_t
channel_bytes_show(struct device
*dev
,
374 struct device_attribute
*attr
, char *buf
)
376 struct visor_device
*vdev
= to_visor_device(dev
);
377 u64 nbytes
= visorchannel_get_nbytes(vdev
->visorchannel
);
379 return sprintf(buf
, "0x%llx\n", nbytes
);
381 static DEVICE_ATTR_RO(channel_bytes
);
383 static ssize_t
channel_id_show(struct device
*dev
,
384 struct device_attribute
*attr
, char *buf
)
386 struct visor_device
*vdev
= to_visor_device(dev
);
389 visorchannel_id(vdev
->visorchannel
, buf
);
394 static DEVICE_ATTR_RO(channel_id
);
396 static struct attribute
*visorbus_attrs
[] = {
397 &dev_attr_partition_handle
.attr
,
398 &dev_attr_partition_guid
.attr
,
399 &dev_attr_partition_name
.attr
,
400 &dev_attr_channel_addr
.attr
,
401 &dev_attr_channel_bytes
.attr
,
402 &dev_attr_channel_id
.attr
,
406 ATTRIBUTE_GROUPS(visorbus
);
409 * BUS debugfs entries
411 * define & implement display of debugfs attributes under
412 * /sys/kernel/debug/visorbus/visorbus<n>.
416 * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo
417 * and write it to a seq_file
418 * @devinfo: the struct visor_vbus_deviceinfo to format
419 * @seq: seq_file to write to
420 * @devix: the device index to be included in the output data, or -1 if no
421 * device index is to be included
423 * Reads @devInfo, and writes it in human-readable notation to @seq.
425 static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo
*devinfo
,
426 struct seq_file
*seq
, int devix
)
428 /* uninitialized vbus device entry */
429 if (!isprint(devinfo
->devtype
[0]))
432 seq_printf(seq
, "[%d]", devix
);
434 /* vbus device entry is for bus or chipset */
437 * Note: because the s-Par back-end is free to scribble in this area,
438 * we never assume '\0'-termination.
440 seq_printf(seq
, "%-*.*s ", (int)sizeof(devinfo
->devtype
),
441 (int)sizeof(devinfo
->devtype
), devinfo
->devtype
);
442 seq_printf(seq
, "%-*.*s ", (int)sizeof(devinfo
->drvname
),
443 (int)sizeof(devinfo
->drvname
), devinfo
->drvname
);
444 seq_printf(seq
, "%.*s\n", (int)sizeof(devinfo
->infostrs
),
448 static int bus_info_debugfs_show(struct seq_file
*seq
, void *v
)
452 struct visor_vbus_deviceinfo dev_info
;
453 struct visor_device
*vdev
= seq
->private;
454 struct visorchannel
*channel
= vdev
->visorchannel
;
460 "Client device/driver info for %s partition (vbus #%u):\n",
461 ((vdev
->name
) ? (char *)(vdev
->name
) : ""),
462 vdev
->chipset_bus_no
);
463 if (visorchannel_read(channel
,
464 offsetof(struct visor_vbus_channel
, chp_info
),
465 &dev_info
, sizeof(dev_info
)) >= 0)
466 vbuschannel_print_devinfo(&dev_info
, seq
, -1);
467 if (visorchannel_read(channel
,
468 offsetof(struct visor_vbus_channel
, bus_info
),
469 &dev_info
, sizeof(dev_info
)) >= 0)
470 vbuschannel_print_devinfo(&dev_info
, seq
, -1);
472 off
= offsetof(struct visor_vbus_channel
, dev_info
);
473 while (off
+ sizeof(dev_info
) <= visorchannel_get_nbytes(channel
)) {
474 if (visorchannel_read(channel
, off
, &dev_info
,
475 sizeof(dev_info
)) >= 0)
476 vbuschannel_print_devinfo(&dev_info
, seq
, i
);
477 off
+= sizeof(dev_info
);
483 static int bus_info_debugfs_open(struct inode
*inode
, struct file
*file
)
485 return single_open(file
, bus_info_debugfs_show
, inode
->i_private
);
488 static const struct file_operations bus_info_debugfs_fops
= {
489 .owner
= THIS_MODULE
,
490 .open
= bus_info_debugfs_open
,
493 .release
= single_release
,
496 static void dev_periodic_work(struct timer_list
*t
)
498 struct visor_device
*dev
= from_timer(dev
, t
, timer
);
499 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
501 drv
->channel_interrupt(dev
);
502 mod_timer(&dev
->timer
, jiffies
+ POLLJIFFIES_NORMALCHANNEL
);
505 static int dev_start_periodic_work(struct visor_device
*dev
)
507 if (dev
->being_removed
|| dev
->timer_active
)
510 /* now up by at least 2 */
511 get_device(&dev
->device
);
512 dev
->timer
.expires
= jiffies
+ POLLJIFFIES_NORMALCHANNEL
;
513 add_timer(&dev
->timer
);
514 dev
->timer_active
= true;
518 static void dev_stop_periodic_work(struct visor_device
*dev
)
520 if (!dev
->timer_active
)
523 del_timer_sync(&dev
->timer
);
524 dev
->timer_active
= false;
525 put_device(&dev
->device
);
529 * visordriver_remove_device() - handle visor device going away
530 * @xdev: struct device for the visor device being removed
532 * This is called when device_unregister() is called for each child device
533 * instance, to notify the appropriate visorbus function driver that the device
534 * is going away, and to decrease the reference count of the device.
536 * Return: 0 iff successful
538 static int visordriver_remove_device(struct device
*xdev
)
540 struct visor_device
*dev
= to_visor_device(xdev
);
541 struct visor_driver
*drv
= to_visor_driver(xdev
->driver
);
543 mutex_lock(&dev
->visordriver_callback_lock
);
544 dev
->being_removed
= true;
546 mutex_unlock(&dev
->visordriver_callback_lock
);
547 dev_stop_periodic_work(dev
);
548 put_device(&dev
->device
);
553 * visorbus_unregister_visor_driver() - unregisters the provided driver
554 * @drv: the driver to unregister
556 * A visor function driver calls this function to unregister the driver,
557 * i.e., within its module_exit function.
559 void visorbus_unregister_visor_driver(struct visor_driver
*drv
)
561 driver_unregister(&drv
->driver
);
563 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver
);
566 * visorbus_read_channel() - reads from the designated channel into
567 * the provided buffer
568 * @dev: the device whose channel is read from
569 * @offset: the offset into the channel at which reading starts
570 * @dest: the destination buffer that is written into from the channel
571 * @nbytes: the number of bytes to read from the channel
573 * If receiving a message, use the visorchannel_signalremove() function instead.
575 * Return: integer indicating success (zero) or failure (non-zero)
577 int visorbus_read_channel(struct visor_device
*dev
, unsigned long offset
,
578 void *dest
, unsigned long nbytes
)
580 return visorchannel_read(dev
->visorchannel
, offset
, dest
, nbytes
);
582 EXPORT_SYMBOL_GPL(visorbus_read_channel
);
585 * visorbus_write_channel() - writes the provided buffer into the designated
587 * @dev: the device whose channel is written to
588 * @offset: the offset into the channel at which writing starts
589 * @src: the source buffer that is written into the channel
590 * @nbytes: the number of bytes to write into the channel
592 * If sending a message, use the visorchannel_signalinsert() function instead.
594 * Return: integer indicating success (zero) or failure (non-zero)
596 int visorbus_write_channel(struct visor_device
*dev
, unsigned long offset
,
597 void *src
, unsigned long nbytes
)
599 return visorchannel_write(dev
->visorchannel
, offset
, src
, nbytes
);
601 EXPORT_SYMBOL_GPL(visorbus_write_channel
);
604 * visorbus_enable_channel_interrupts() - enables interrupts on the
606 * @dev: the device on which to enable interrupts
608 * Currently we don't yet have a real interrupt, so for now we just call the
609 * interrupt function periodically via a timer.
611 int visorbus_enable_channel_interrupts(struct visor_device
*dev
)
613 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
615 if (!drv
->channel_interrupt
) {
616 dev_err(&dev
->device
, "%s no interrupt function!\n", __func__
);
620 return dev_start_periodic_work(dev
);
622 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts
);
625 * visorbus_disable_channel_interrupts() - disables interrupts on the
627 * @dev: the device on which to disable interrupts
629 void visorbus_disable_channel_interrupts(struct visor_device
*dev
)
631 dev_stop_periodic_work(dev
);
633 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts
);
636 * create_visor_device() - create visor device as a result of receiving the
637 * controlvm device_create message for a new device
638 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
639 * for chipset_bus_no and chipset_dev_no, that will be initialized
641 * This is how everything starts from the device end.
642 * This function is called when a channel first appears via a ControlVM
643 * message. In response, this function allocates a visor_device to correspond
644 * to the new channel, and attempts to connect it the appropriate * driver. If
645 * the appropriate driver is found, the visor_driver.probe() function for that
646 * driver will be called, and will be passed the new * visor_device that we
649 * It's ok if the appropriate driver is not yet loaded, because in that case
650 * the new device struct will just stick around in the bus' list of devices.
651 * When the appropriate driver calls visorbus_register_visor_driver(), the
652 * visor_driver.probe() for the new driver will be called with the new device.
654 * Return: 0 if successful, otherwise the negative value returned by
655 * device_add() indicating the reason for failure
657 int create_visor_device(struct visor_device
*dev
)
660 u32 chipset_bus_no
= dev
->chipset_bus_no
;
661 u32 chipset_dev_no
= dev
->chipset_dev_no
;
663 mutex_init(&dev
->visordriver_callback_lock
);
664 dev
->device
.bus
= &visorbus_type
;
665 dev
->device
.groups
= channel_groups
;
666 device_initialize(&dev
->device
);
667 dev
->device
.release
= visorbus_release_device
;
668 /* keep a reference just for us (now 2) */
669 get_device(&dev
->device
);
670 timer_setup(&dev
->timer
, dev_periodic_work
, 0);
672 * bus_id must be a unique name with respect to this bus TYPE (NOT bus
673 * instance). That's why we need to include the bus number within the
676 err
= dev_set_name(&dev
->device
, "vbus%u:dev%u",
677 chipset_bus_no
, chipset_dev_no
);
681 * device_add does this:
682 * bus_add_device(dev)
683 * ->device_attach(dev)
684 * ->for each driver drv registered on the bus that dev is on
685 * if (dev.drv) ** device already has a driver **
686 * ** not sure we could ever get here... **
688 * if (bus.match(dev,drv)) [visorbus_match]
690 * if (!drv.probe(dev)) [visordriver_probe_device]
693 * Note that device_add does NOT fail if no driver failed to claim the
694 * device. The device will be linked onto bus_type.klist_devices
695 * regardless (use bus_for_each_dev).
697 err
= device_add(&dev
->device
);
700 list_add_tail(&dev
->list_all
, &list_all_device_instances
);
701 dev
->state
.created
= 1;
702 visorbus_response(dev
, err
, CONTROLVM_DEVICE_CREATE
);
703 /* success: reference kept via unmatched get_device() */
707 put_device(&dev
->device
);
708 dev_err(&dev
->device
, "Creating visor device failed. %d\n", err
);
712 void remove_visor_device(struct visor_device
*dev
)
714 list_del(&dev
->list_all
);
715 put_device(&dev
->device
);
716 if (dev
->pending_msg_hdr
)
717 visorbus_response(dev
, 0, CONTROLVM_DEVICE_DESTROY
);
718 device_unregister(&dev
->device
);
721 static int get_vbus_header_info(struct visorchannel
*chan
,
723 struct visor_vbus_headerinfo
*hdr_info
)
727 if (!visor_check_channel(visorchannel_get_header(chan
),
729 &visor_vbus_channel_guid
,
731 sizeof(struct visor_vbus_channel
),
732 VISOR_VBUS_CHANNEL_VERSIONID
,
733 VISOR_CHANNEL_SIGNATURE
))
736 err
= visorchannel_read(chan
, sizeof(struct channel_header
), hdr_info
,
740 if (hdr_info
->struct_bytes
< sizeof(struct visor_vbus_headerinfo
))
742 if (hdr_info
->device_info_struct_bytes
<
743 sizeof(struct visor_vbus_deviceinfo
))
749 * write_vbus_chp_info() - write the contents of <info> to the struct
750 * visor_vbus_channel.chp_info
751 * @chan: indentifies the s-Par channel that will be updated
752 * @hdr_info: used to find appropriate channel offset to write data
753 * @info: contains the information to write
755 * Writes chipset info into the channel memory to be used for diagnostic
758 * Returns no value since this is debug information and not needed for
759 * device functionality.
761 static void write_vbus_chp_info(struct visorchannel
*chan
,
762 struct visor_vbus_headerinfo
*hdr_info
,
763 struct visor_vbus_deviceinfo
*info
)
767 if (hdr_info
->chp_info_offset
== 0)
770 off
= sizeof(struct channel_header
) + hdr_info
->chp_info_offset
;
771 visorchannel_write(chan
, off
, info
, sizeof(*info
));
775 * write_vbus_bus_info() - write the contents of <info> to the struct
776 * visor_vbus_channel.bus_info
777 * @chan: indentifies the s-Par channel that will be updated
778 * @hdr_info: used to find appropriate channel offset to write data
779 * @info: contains the information to write
781 * Writes bus info into the channel memory to be used for diagnostic
784 * Returns no value since this is debug information and not needed for
785 * device functionality.
787 static void write_vbus_bus_info(struct visorchannel
*chan
,
788 struct visor_vbus_headerinfo
*hdr_info
,
789 struct visor_vbus_deviceinfo
*info
)
793 if (hdr_info
->bus_info_offset
== 0)
796 off
= sizeof(struct channel_header
) + hdr_info
->bus_info_offset
;
797 visorchannel_write(chan
, off
, info
, sizeof(*info
));
801 * write_vbus_dev_info() - write the contents of <info> to the struct
802 * visor_vbus_channel.dev_info[<devix>]
803 * @chan: indentifies the s-Par channel that will be updated
804 * @hdr_info: used to find appropriate channel offset to write data
805 * @info: contains the information to write
806 * @devix: the relative device number (0..n-1) of the device on the bus
808 * Writes device info into the channel memory to be used for diagnostic
811 * Returns no value since this is debug information and not needed for
812 * device functionality.
814 static void write_vbus_dev_info(struct visorchannel
*chan
,
815 struct visor_vbus_headerinfo
*hdr_info
,
816 struct visor_vbus_deviceinfo
*info
,
821 if (hdr_info
->dev_info_offset
== 0)
823 off
= (sizeof(struct channel_header
) + hdr_info
->dev_info_offset
) +
824 (hdr_info
->device_info_struct_bytes
* devix
);
825 visorchannel_write(chan
, off
, info
, sizeof(*info
));
828 static void bus_device_info_init(
829 struct visor_vbus_deviceinfo
*bus_device_info_ptr
,
830 const char *dev_type
, const char *drv_name
)
832 memset(bus_device_info_ptr
, 0, sizeof(struct visor_vbus_deviceinfo
));
833 snprintf(bus_device_info_ptr
->devtype
,
834 sizeof(bus_device_info_ptr
->devtype
),
835 "%s", (dev_type
) ? dev_type
: "unknownType");
836 snprintf(bus_device_info_ptr
->drvname
,
837 sizeof(bus_device_info_ptr
->drvname
),
838 "%s", (drv_name
) ? drv_name
: "unknownDriver");
839 snprintf(bus_device_info_ptr
->infostrs
,
840 sizeof(bus_device_info_ptr
->infostrs
), "kernel ver. %s",
845 * publish_vbus_dev_info() - for a child device just created on a client bus,
846 * fill in information about the driver that is
847 * controlling this device into the appropriate slot
848 * within the vbus channel of the bus instance
849 * @visordev: struct visor_device for the desired device
851 static void publish_vbus_dev_info(struct visor_device
*visordev
)
854 struct visor_device
*bdev
;
855 struct visor_driver
*visordrv
;
856 u32 bus_no
= visordev
->chipset_bus_no
;
857 u32 dev_no
= visordev
->chipset_dev_no
;
858 struct visor_vbus_deviceinfo dev_info
;
859 const char *chan_type_name
= NULL
;
860 struct visor_vbus_headerinfo
*hdr_info
;
862 if (!visordev
->device
.driver
)
864 bdev
= visorbus_get_device_by_id(bus_no
, BUS_ROOT_DEVICE
, NULL
);
867 hdr_info
= (struct visor_vbus_headerinfo
*)bdev
->vbus_hdr_info
;
870 visordrv
= to_visor_driver(visordev
->device
.driver
);
873 * Within the list of device types (by GUID) that the driver
874 * says it supports, find out which one of those types matches
875 * the type of this device, so that we can include the device
878 for (i
= 0; visordrv
->channel_types
[i
].name
; i
++) {
879 if (guid_equal(&visordrv
->channel_types
[i
].guid
,
880 &visordev
->channel_type_guid
)) {
881 chan_type_name
= visordrv
->channel_types
[i
].name
;
885 bus_device_info_init(&dev_info
, chan_type_name
, visordrv
->name
);
886 write_vbus_dev_info(bdev
->visorchannel
, hdr_info
, &dev_info
, dev_no
);
887 write_vbus_chp_info(bdev
->visorchannel
, hdr_info
, &chipset_driverinfo
);
888 write_vbus_bus_info(bdev
->visorchannel
, hdr_info
,
889 &clientbus_driverinfo
);
893 * visordriver_probe_device() - handle new visor device coming online
894 * @xdev: struct device for the visor device being probed
896 * This is called automatically upon adding a visor_device (device_add), or
897 * adding a visor_driver (visorbus_register_visor_driver), but only after
898 * visorbus_match() has returned 1 to indicate a successful match between
901 * If successful, a reference to the device will be held onto via get_device().
903 * Return: 0 if successful, meaning the function driver's probe() function
904 * was successful with this device, otherwise a negative errno
905 * value indicating failure reason
907 static int visordriver_probe_device(struct device
*xdev
)
910 struct visor_driver
*drv
= to_visor_driver(xdev
->driver
);
911 struct visor_device
*dev
= to_visor_device(xdev
);
913 mutex_lock(&dev
->visordriver_callback_lock
);
914 dev
->being_removed
= false;
915 err
= drv
->probe(dev
);
917 mutex_unlock(&dev
->visordriver_callback_lock
);
920 /* success: reference kept via unmatched get_device() */
921 get_device(&dev
->device
);
922 publish_vbus_dev_info(dev
);
923 mutex_unlock(&dev
->visordriver_callback_lock
);
928 * visorbus_register_visor_driver() - registers the provided visor driver for
929 * handling one or more visor device
930 * types (channel_types)
931 * @drv: the driver to register
933 * A visor function driver calls this function to register the driver. The
934 * caller MUST fill in the following fields within the #drv structure:
935 * name, version, owner, channel_types, probe, remove
937 * Here's how the whole Linux bus / driver / device model works.
939 * At system start-up, the visorbus kernel module is loaded, which registers
940 * visorbus_type as a bus type, using bus_register().
942 * All kernel modules that support particular device types on a
943 * visorbus bus are loaded. Each of these kernel modules calls
944 * visorbus_register_visor_driver() in their init functions, passing a
945 * visor_driver struct. visorbus_register_visor_driver() in turn calls
946 * register_driver(&visor_driver.driver). This .driver member is
947 * initialized with generic methods (like probe), whose sole responsibility
948 * is to act as a broker for the real methods, which are within the
949 * visor_driver struct. (This is the way the subclass behavior is
950 * implemented, since visor_driver is essentially a subclass of the
951 * generic driver.) Whenever a driver_register() happens, core bus code in
952 * the kernel does (see device_attach() in drivers/base/dd.c):
954 * for each dev associated with the bus (the bus that driver is on) that
955 * does not yet have a driver
956 * if bus.match(dev,newdriver) == yes_matched ** .match specified
957 * ** during bus_register().
958 * newdriver.probe(dev) ** for visor drivers, this will call
959 * ** the generic driver.probe implemented in visorbus.c,
960 * ** which in turn calls the probe specified within the
961 * ** struct visor_driver (which was specified by the
962 * ** actual device driver as part of
963 * ** visorbus_register_visor_driver()).
965 * The above dance also happens when a new device appears.
966 * So the question is, how are devices created within the system?
967 * Basically, just call device_add(dev). See pci_bus_add_devices().
968 * pci_scan_device() shows an example of how to build a device struct. It
969 * returns the newly-created struct to pci_scan_single_device(), who adds it
970 * to the list of devices at PCIBUS.devices. That list of devices is what
971 * is traversed by pci_bus_add_devices().
973 * Return: integer indicating success (zero) or failure (non-zero)
975 int visorbus_register_visor_driver(struct visor_driver
*drv
)
977 /* can't register on a nonexistent bus */
989 drv
->driver
.name
= drv
->name
;
990 drv
->driver
.bus
= &visorbus_type
;
991 drv
->driver
.probe
= visordriver_probe_device
;
992 drv
->driver
.remove
= visordriver_remove_device
;
993 drv
->driver
.owner
= drv
->owner
;
995 * driver_register does this:
996 * bus_add_driver(drv)
997 * ->if (drv.bus) ** (bus_type) **
999 * for each dev with bus type of drv.bus
1000 * if (!dev.drv) ** no driver assigned yet **
1001 * if (bus.match(dev,drv)) [visorbus_match]
1003 * if (!drv.probe(dev)) [visordriver_probe_device]
1006 return driver_register(&drv
->driver
);
1008 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver
);
1011 * visorbus_create_instance() - create a device instance for the visorbus itself
1012 * @dev: struct visor_device indicating the bus instance
1014 * Return: 0 for success, otherwise negative errno value indicating reason for
1017 int visorbus_create_instance(struct visor_device
*dev
)
1019 int id
= dev
->chipset_bus_no
;
1021 struct visor_vbus_headerinfo
*hdr_info
;
1023 hdr_info
= kzalloc(sizeof(*hdr_info
), GFP_KERNEL
);
1026 dev_set_name(&dev
->device
, "visorbus%d", id
);
1027 dev
->device
.bus
= &visorbus_type
;
1028 dev
->device
.groups
= visorbus_groups
;
1029 dev
->device
.release
= visorbus_release_busdevice
;
1030 dev
->debugfs_dir
= debugfs_create_dir(dev_name(&dev
->device
),
1031 visorbus_debugfs_dir
);
1032 dev
->debugfs_bus_info
= debugfs_create_file("client_bus_info", 0440,
1033 dev
->debugfs_dir
, dev
,
1034 &bus_info_debugfs_fops
);
1035 dev_set_drvdata(&dev
->device
, dev
);
1036 err
= get_vbus_header_info(dev
->visorchannel
, &dev
->device
, hdr_info
);
1038 goto err_debugfs_dir
;
1039 err
= device_register(&dev
->device
);
1041 goto err_debugfs_dir
;
1042 list_add_tail(&dev
->list_all
, &list_all_bus_instances
);
1043 dev
->state
.created
= 1;
1044 dev
->vbus_hdr_info
= (void *)hdr_info
;
1045 write_vbus_chp_info(dev
->visorchannel
, hdr_info
, &chipset_driverinfo
);
1046 write_vbus_bus_info(dev
->visorchannel
, hdr_info
, &clientbus_driverinfo
);
1047 visorbus_response(dev
, err
, CONTROLVM_BUS_CREATE
);
1051 debugfs_remove_recursive(dev
->debugfs_dir
);
1053 dev_err(&dev
->device
, "%s failed: %d\n", __func__
, err
);
1058 * visorbus_remove_instance() - remove a device instance for the visorbus itself
1059 * @dev: struct visor_device indentifying the bus to remove
1061 void visorbus_remove_instance(struct visor_device
*dev
)
1064 * Note that this will result in the release method for
1065 * dev->dev being called, which will call
1066 * visorbus_release_busdevice(). This has something to do with
1067 * the put_device() done in device_unregister(), but I have never
1068 * successfully been able to trace thru the code to see where/how
1069 * release() gets called. But I know it does.
1071 kfree(dev
->vbus_hdr_info
);
1072 list_del(&dev
->list_all
);
1073 if (dev
->pending_msg_hdr
)
1074 visorbus_response(dev
, 0, CONTROLVM_BUS_DESTROY
);
1075 device_unregister(&dev
->device
);
1079 * remove_all_visor_devices() - remove all child visorbus device instances
1081 static void remove_all_visor_devices(void)
1083 struct list_head
*listentry
, *listtmp
;
1085 list_for_each_safe(listentry
, listtmp
, &list_all_device_instances
) {
1086 struct visor_device
*dev
;
1088 dev
= list_entry(listentry
, struct visor_device
, list_all
);
1089 remove_visor_device(dev
);
1094 * pause_state_change_complete() - the callback function to be called by a
1095 * visorbus function driver when a
1096 * pending "pause device" operation has
1098 * @dev: struct visor_device identifying the paused device
1099 * @status: 0 iff the pause state change completed successfully, otherwise
1100 * a negative errno value indicating the reason for failure
1102 static void pause_state_change_complete(struct visor_device
*dev
, int status
)
1107 dev
->pausing
= false;
1108 visorbus_device_changestate_response(dev
, status
,
1109 segment_state_standby
);
1113 * resume_state_change_complete() - the callback function to be called by a
1114 * visorbus function driver when a
1115 * pending "resume device" operation has
1117 * @dev: struct visor_device identifying the resumed device
1118 * @status: 0 iff the resume state change completed successfully, otherwise
1119 * a negative errno value indicating the reason for failure
1121 static void resume_state_change_complete(struct visor_device
*dev
, int status
)
1126 dev
->resuming
= false;
1128 * Notify the chipset driver that the resume is complete,
1129 * which will presumably want to send some sort of response to
1132 visorbus_device_changestate_response(dev
, status
,
1133 segment_state_running
);
1137 * visorchipset_initiate_device_pause_resume() - start a pause or resume
1138 * operation for a visor device
1139 * @dev: struct visor_device identifying the device being paused or resumed
1140 * @is_pause: true to indicate pause operation, false to indicate resume
1142 * Tell the subordinate function driver for a specific device to pause
1143 * or resume that device. Success/failure result is returned asynchronously
1144 * via a callback function; see pause_state_change_complete() and
1145 * resume_state_change_complete().
1147 static int visorchipset_initiate_device_pause_resume(struct visor_device
*dev
,
1151 struct visor_driver
*drv
;
1153 /* If no driver associated with the device nothing to pause/resume */
1154 if (!dev
->device
.driver
)
1156 if (dev
->pausing
|| dev
->resuming
)
1159 drv
= to_visor_driver(dev
->device
.driver
);
1161 dev
->pausing
= true;
1162 err
= drv
->pause(dev
, pause_state_change_complete
);
1165 * The vbus_dev_info structure in the channel was been cleared,
1166 * make sure it is valid.
1168 publish_vbus_dev_info(dev
);
1169 dev
->resuming
= true;
1170 err
= drv
->resume(dev
, resume_state_change_complete
);
1176 * visorchipset_device_pause() - start a pause operation for a visor device
1177 * @dev_info: struct visor_device identifying the device being paused
1179 * Tell the subordinate function driver for a specific device to pause
1180 * that device. Success/failure result is returned asynchronously
1181 * via a callback function; see pause_state_change_complete().
1183 int visorchipset_device_pause(struct visor_device
*dev_info
)
1187 err
= visorchipset_initiate_device_pause_resume(dev_info
, true);
1189 dev_info
->pausing
= false;
1196 * visorchipset_device_resume() - start a resume operation for a visor device
1197 * @dev_info: struct visor_device identifying the device being resumed
1199 * Tell the subordinate function driver for a specific device to resume
1200 * that device. Success/failure result is returned asynchronously
1201 * via a callback function; see resume_state_change_complete().
1203 int visorchipset_device_resume(struct visor_device
*dev_info
)
1207 err
= visorchipset_initiate_device_pause_resume(dev_info
, false);
1209 dev_info
->resuming
= false;
1215 int visorbus_init(void)
1219 visorbus_debugfs_dir
= debugfs_create_dir("visorbus", NULL
);
1220 bus_device_info_init(&clientbus_driverinfo
, "clientbus", "visorbus");
1221 err
= bus_register(&visorbus_type
);
1225 bus_device_info_init(&chipset_driverinfo
, "chipset", "visorchipset");
1229 void visorbus_exit(void)
1231 struct list_head
*listentry
, *listtmp
;
1233 remove_all_visor_devices();
1234 list_for_each_safe(listentry
, listtmp
, &list_all_bus_instances
) {
1235 struct visor_device
*dev
;
1237 dev
= list_entry(listentry
, struct visor_device
, list_all
);
1238 visorbus_remove_instance(dev
);
1240 bus_unregister(&visorbus_type
);
1241 initialized
= false;
1242 debugfs_remove_recursive(visorbus_debugfs_dir
);