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/debugfs.h>
17 #include <linux/uuid.h>
20 #include "visorbus_private.h"
22 static const guid_t visor_vbus_channel_guid = VISOR_VBUS_CHANNEL_GUID;
24 /* Display string that is guaranteed to be no longer the 99 characters */
26 #define POLLJIFFIES_NORMALCHANNEL 10
28 /* stores whether bus_registration was successful */
29 static bool initialized;
30 static struct dentry *visorbus_debugfs_dir;
33 * DEVICE type attributes
35 * The modalias file will contain the guid of the device.
37 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
40 struct visor_device *vdev;
43 vdev = to_visor_device(dev);
44 guid = visorchannel_get_guid(vdev->visorchannel);
45 return sprintf(buf, "visorbus:%pUl\n", guid);
47 static DEVICE_ATTR_RO(modalias);
49 static struct attribute *visorbus_dev_attrs[] = {
50 &dev_attr_modalias.attr,
54 ATTRIBUTE_GROUPS(visorbus_dev);
56 /* filled in with info about parent chipset driver when we register with it */
57 static struct visor_vbus_deviceinfo chipset_driverinfo;
58 /* filled in with info about this driver, wrt it servicing client busses */
59 static struct visor_vbus_deviceinfo clientbus_driverinfo;
61 /* list of visor_device structs, linked via .list_all */
62 static LIST_HEAD(list_all_bus_instances);
63 /* list of visor_device structs, linked via .list_all */
64 static LIST_HEAD(list_all_device_instances);
67 * Generic function useful for validating any type of channel when it is
68 * received by the client that will be accessing the channel.
69 * Note that <logCtx> is only needed for callers in the EFI environment, and
70 * is used to pass the EFI_DIAG_CAPTURE_PROTOCOL needed to log messages.
72 int visor_check_channel(struct channel_header *ch,
74 const guid_t *expected_guid,
76 u64 expected_min_bytes,
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);
129 return add_uevent_var(env, "MODALIAS=visorbus:%pUl", guid);
133 * visorbus_match() - called automatically upon adding a visor_device
134 * (device_add), or adding a visor_driver
135 * (visorbus_register_visor_driver)
136 * @xdev: struct device for the device being matched
137 * @xdrv: struct device_driver for driver to match device against
139 * Return: 1 iff the provided driver can control the specified device
141 static int visorbus_match(struct device *xdev, struct device_driver *xdrv)
143 const guid_t *channel_type;
145 struct visor_device *dev;
146 struct visor_driver *drv;
148 dev = to_visor_device(xdev);
149 channel_type = visorchannel_get_guid(dev->visorchannel);
150 drv = to_visor_driver(xdrv);
151 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))
162 * This describes the TYPE of bus.
163 * (Don't confuse this with an INSTANCE of the bus.)
165 struct bus_type visorbus_type = {
167 .match = visorbus_match,
168 .uevent = visorbus_uevent,
169 .dev_groups = visorbus_dev_groups,
173 * visorbus_release_busdevice() - called when device_unregister() is called for
174 * the bus device instance, after all other tasks
175 * involved with destroying the dev are complete
176 * @xdev: struct device for the bus being released
178 static void visorbus_release_busdevice(struct device *xdev)
180 struct visor_device *dev = dev_get_drvdata(xdev);
182 debugfs_remove(dev->debugfs_client_bus_info);
183 debugfs_remove_recursive(dev->debugfs_dir);
188 * visorbus_release_device() - called when device_unregister() is called for
189 * each child device instance
190 * @xdev: struct device for the visor device being released
192 static void visorbus_release_device(struct device *xdev)
194 struct visor_device *dev = to_visor_device(xdev);
196 visorchannel_destroy(dev->visorchannel);
201 * begin implementation of specific channel attributes to appear under
202 * /sys/bus/visorbus<x>/dev<y>/channel
205 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
208 struct visor_device *vdev = to_visor_device(dev);
210 return sprintf(buf, "0x%llx\n",
211 visorchannel_get_physaddr(vdev->visorchannel));
213 static DEVICE_ATTR_RO(physaddr);
215 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
218 struct visor_device *vdev = to_visor_device(dev);
220 return sprintf(buf, "0x%lx\n",
221 visorchannel_get_nbytes(vdev->visorchannel));
223 static DEVICE_ATTR_RO(nbytes);
225 static ssize_t clientpartition_show(struct device *dev,
226 struct device_attribute *attr, char *buf)
228 struct visor_device *vdev = to_visor_device(dev);
230 return sprintf(buf, "0x%llx\n",
231 visorchannel_get_clientpartition(vdev->visorchannel));
233 static DEVICE_ATTR_RO(clientpartition);
235 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
238 struct visor_device *vdev = to_visor_device(dev);
239 char typeid[LINESIZE];
241 return sprintf(buf, "%s\n",
242 visorchannel_id(vdev->visorchannel, typeid));
244 static DEVICE_ATTR_RO(typeguid);
246 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
249 struct visor_device *vdev = to_visor_device(dev);
250 char zoneid[LINESIZE];
252 return sprintf(buf, "%s\n",
253 visorchannel_zoneid(vdev->visorchannel, zoneid));
255 static DEVICE_ATTR_RO(zoneguid);
257 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
261 struct bus_type *xbus = dev->bus;
262 struct device_driver *xdrv = dev->driver;
263 struct visor_driver *drv = NULL;
267 i = xbus->match(dev, xdrv);
270 drv = to_visor_driver(xdrv);
271 return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
273 static DEVICE_ATTR_RO(typename);
275 static struct attribute *channel_attrs[] = {
276 &dev_attr_physaddr.attr,
277 &dev_attr_nbytes.attr,
278 &dev_attr_clientpartition.attr,
279 &dev_attr_typeguid.attr,
280 &dev_attr_zoneguid.attr,
281 &dev_attr_typename.attr,
285 ATTRIBUTE_GROUPS(channel);
287 /* end implementation of specific channel attributes */
290 * BUS instance attributes
292 * define & implement display of bus attributes under
293 * /sys/bus/visorbus/devices/visorbus<n>.
296 static ssize_t partition_handle_show(struct device *dev,
297 struct device_attribute *attr,
300 struct visor_device *vdev = to_visor_device(dev);
301 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
303 return sprintf(buf, "0x%llx\n", handle);
305 static DEVICE_ATTR_RO(partition_handle);
307 static ssize_t partition_guid_show(struct device *dev,
308 struct device_attribute *attr,
311 struct visor_device *vdev = to_visor_device(dev);
313 return sprintf(buf, "{%pUb}\n", &vdev->partition_guid);
315 static DEVICE_ATTR_RO(partition_guid);
317 static ssize_t partition_name_show(struct device *dev,
318 struct device_attribute *attr,
321 struct visor_device *vdev = to_visor_device(dev);
323 return sprintf(buf, "%s\n", vdev->name);
325 static DEVICE_ATTR_RO(partition_name);
327 static ssize_t channel_addr_show(struct device *dev,
328 struct device_attribute *attr,
331 struct visor_device *vdev = to_visor_device(dev);
332 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
334 return sprintf(buf, "0x%llx\n", addr);
336 static DEVICE_ATTR_RO(channel_addr);
338 static ssize_t channel_bytes_show(struct device *dev,
339 struct device_attribute *attr,
342 struct visor_device *vdev = to_visor_device(dev);
343 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
345 return sprintf(buf, "0x%llx\n", nbytes);
347 static DEVICE_ATTR_RO(channel_bytes);
349 static ssize_t channel_id_show(struct device *dev,
350 struct device_attribute *attr,
353 struct visor_device *vdev = to_visor_device(dev);
356 visorchannel_id(vdev->visorchannel, buf);
362 static DEVICE_ATTR_RO(channel_id);
364 static struct attribute *visorbus_attrs[] = {
365 &dev_attr_partition_handle.attr,
366 &dev_attr_partition_guid.attr,
367 &dev_attr_partition_name.attr,
368 &dev_attr_channel_addr.attr,
369 &dev_attr_channel_bytes.attr,
370 &dev_attr_channel_id.attr,
374 ATTRIBUTE_GROUPS(visorbus);
377 * BUS debugfs entries
379 * define & implement display of debugfs attributes under
380 * /sys/kernel/debug/visorbus/visorbus<n>.
384 * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo
385 * and write it to a seq_file
386 * @devinfo: the struct visor_vbus_deviceinfo to format
387 * @seq: seq_file to write to
388 * @devix: the device index to be included in the output data, or -1 if no
389 * device index is to be included
391 * Reads @devInfo, and writes it in human-readable notation to @seq.
393 static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo,
394 struct seq_file *seq, int devix)
396 /* uninitialized vbus device entry */
397 if (!isprint(devinfo->devtype[0]))
401 seq_printf(seq, "[%d]", devix);
403 /* vbus device entry is for bus or chipset */
407 * Note: because the s-Par back-end is free to scribble in this area,
408 * we never assume '\0'-termination.
410 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype),
411 (int)sizeof(devinfo->devtype), devinfo->devtype);
412 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname),
413 (int)sizeof(devinfo->drvname), devinfo->drvname);
414 seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs),
418 static int client_bus_info_debugfs_show(struct seq_file *seq, void *v)
422 struct visor_vbus_deviceinfo dev_info;
423 struct visor_device *vdev = seq->private;
424 struct visorchannel *channel = vdev->visorchannel;
430 "Client device / client driver info for %s partition (vbus #%u):\n",
431 ((vdev->name) ? (char *)(vdev->name) : ""),
432 vdev->chipset_bus_no);
434 if (visorchannel_read(channel,
435 offsetof(struct visor_vbus_channel, chp_info),
436 &dev_info, sizeof(dev_info)) >= 0)
437 vbuschannel_print_devinfo(&dev_info, seq, -1);
438 if (visorchannel_read(channel,
439 offsetof(struct visor_vbus_channel, bus_info),
440 &dev_info, sizeof(dev_info)) >= 0)
441 vbuschannel_print_devinfo(&dev_info, seq, -1);
443 off = offsetof(struct visor_vbus_channel, dev_info);
444 while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) {
445 if (visorchannel_read(channel, off, &dev_info,
446 sizeof(dev_info)) >= 0)
447 vbuschannel_print_devinfo(&dev_info, seq, i);
448 off += sizeof(dev_info);
455 static int client_bus_info_debugfs_open(struct inode *inode, struct file *file)
457 return single_open(file, client_bus_info_debugfs_show,
461 static const struct file_operations client_bus_info_debugfs_fops = {
462 .owner = THIS_MODULE,
463 .open = client_bus_info_debugfs_open,
466 .release = single_release,
469 static void dev_periodic_work(unsigned long __opaque)
471 struct visor_device *dev = (struct visor_device *)__opaque;
472 struct visor_driver *drv = to_visor_driver(dev->device.driver);
474 drv->channel_interrupt(dev);
475 mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
478 static int dev_start_periodic_work(struct visor_device *dev)
480 if (dev->being_removed || dev->timer_active)
482 /* now up by at least 2 */
483 get_device(&dev->device);
484 dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
485 add_timer(&dev->timer);
486 dev->timer_active = true;
490 static void dev_stop_periodic_work(struct visor_device *dev)
492 if (!dev->timer_active)
494 del_timer_sync(&dev->timer);
495 dev->timer_active = false;
496 put_device(&dev->device);
500 * visordriver_remove_device() - handle visor device going away
501 * @xdev: struct device for the visor device being removed
503 * This is called when device_unregister() is called for each child device
504 * instance, to notify the appropriate visorbus function driver that the device
505 * is going away, and to decrease the reference count of the device.
507 * Return: 0 iff successful
509 static int visordriver_remove_device(struct device *xdev)
511 struct visor_device *dev;
512 struct visor_driver *drv;
514 dev = to_visor_device(xdev);
515 drv = to_visor_driver(xdev->driver);
517 mutex_lock(&dev->visordriver_callback_lock);
518 dev->being_removed = true;
520 mutex_unlock(&dev->visordriver_callback_lock);
522 dev_stop_periodic_work(dev);
523 put_device(&dev->device);
529 * visorbus_unregister_visor_driver() - unregisters the provided driver
530 * @drv: the driver to unregister
532 * A visor function driver calls this function to unregister the driver,
533 * i.e., within its module_exit function.
535 void visorbus_unregister_visor_driver(struct visor_driver *drv)
537 driver_unregister(&drv->driver);
539 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
542 * visorbus_read_channel() - reads from the designated channel into
543 * the provided buffer
544 * @dev: the device whose channel is read from
545 * @offset: the offset into the channel at which reading starts
546 * @dest: the destination buffer that is written into from the channel
547 * @nbytes: the number of bytes to read from the channel
549 * If receiving a message, use the visorchannel_signalremove()
552 * Return: integer indicating success (zero) or failure (non-zero)
554 int visorbus_read_channel(struct visor_device *dev, unsigned long offset,
555 void *dest, unsigned long nbytes)
557 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
559 EXPORT_SYMBOL_GPL(visorbus_read_channel);
562 * visorbus_write_channel() - writes the provided buffer into the designated
564 * @dev: the device whose channel is written to
565 * @offset: the offset into the channel at which writing starts
566 * @src: the source buffer that is written into the channel
567 * @nbytes: the number of bytes to write into the channel
569 * If sending a message, use the visorchannel_signalinsert()
572 * Return: integer indicating success (zero) or failure (non-zero)
574 int visorbus_write_channel(struct visor_device *dev, unsigned long offset,
575 void *src, unsigned long nbytes)
577 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
579 EXPORT_SYMBOL_GPL(visorbus_write_channel);
582 * visorbus_enable_channel_interrupts() - enables interrupts on the
584 * @dev: the device on which to enable interrupts
586 * Currently we don't yet have a real interrupt, so for now we just call the
587 * interrupt function periodically via a timer.
589 int visorbus_enable_channel_interrupts(struct visor_device *dev)
591 struct visor_driver *drv = to_visor_driver(dev->device.driver);
593 if (!drv->channel_interrupt) {
594 dev_err(&dev->device, "%s no interrupt function!\n", __func__);
598 return dev_start_periodic_work(dev);
600 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
603 * visorbus_disable_channel_interrupts() - disables interrupts on the
605 * @dev: the device on which to disable interrupts
607 void visorbus_disable_channel_interrupts(struct visor_device *dev)
609 dev_stop_periodic_work(dev);
611 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
614 * create_visor_device() - create visor device as a result of receiving the
615 * controlvm device_create message for a new device
616 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
617 * for chipset_bus_no and chipset_dev_no, that will be initialized
619 * This is how everything starts from the device end.
620 * This function is called when a channel first appears via a ControlVM
621 * message. In response, this function allocates a visor_device to
622 * correspond to the new channel, and attempts to connect it the appropriate
623 * driver. If the appropriate driver is found, the visor_driver.probe()
624 * function for that driver will be called, and will be passed the new
625 * visor_device that we just created.
627 * It's ok if the appropriate driver is not yet loaded, because in that case
628 * the new device struct will just stick around in the bus' list of devices.
629 * When the appropriate driver calls visorbus_register_visor_driver(), the
630 * visor_driver.probe() for the new driver will be called with the new
633 * Return: 0 if successful, otherwise the negative value returned by
634 * device_add() indicating the reason for failure
636 int create_visor_device(struct visor_device *dev)
639 u32 chipset_bus_no = dev->chipset_bus_no;
640 u32 chipset_dev_no = dev->chipset_dev_no;
642 mutex_init(&dev->visordriver_callback_lock);
643 dev->device.bus = &visorbus_type;
644 dev->device.groups = channel_groups;
645 device_initialize(&dev->device);
646 dev->device.release = visorbus_release_device;
647 /* keep a reference just for us (now 2) */
648 get_device(&dev->device);
649 setup_timer(&dev->timer, dev_periodic_work, (unsigned long)dev);
652 * bus_id must be a unique name with respect to this bus TYPE
653 * (NOT bus instance). That's why we need to include the bus
654 * number within the name.
656 err = dev_set_name(&dev->device, "vbus%u:dev%u",
657 chipset_bus_no, chipset_dev_no);
662 * device_add does this:
663 * bus_add_device(dev)
664 * ->device_attach(dev)
665 * ->for each driver drv registered on the bus that dev is on
666 * if (dev.drv) ** device already has a driver **
667 * ** not sure we could ever get here... **
669 * if (bus.match(dev,drv)) [visorbus_match]
671 * if (!drv.probe(dev)) [visordriver_probe_device]
674 * Note that device_add does NOT fail if no driver failed to
675 * claim the device. The device will be linked onto
676 * bus_type.klist_devices regardless (use bus_for_each_dev).
678 err = device_add(&dev->device);
682 list_add_tail(&dev->list_all, &list_all_device_instances);
683 dev->state.created = 1;
684 visorbus_response(dev, err, CONTROLVM_DEVICE_CREATE);
685 /* success: reference kept via unmatched get_device() */
689 put_device(&dev->device);
690 dev_err(&dev->device, "Creating visor device failed. %d\n", err);
694 void remove_visor_device(struct visor_device *dev)
696 list_del(&dev->list_all);
697 put_device(&dev->device);
698 device_unregister(&dev->device);
699 visorbus_response(dev, 0, CONTROLVM_DEVICE_DESTROY);
702 static int get_vbus_header_info(struct visorchannel *chan,
704 struct visor_vbus_headerinfo *hdr_info)
708 if (!visor_check_channel(visorchannel_get_header(chan),
710 &visor_vbus_channel_guid,
712 sizeof(struct visor_vbus_channel),
713 VISOR_VBUS_CHANNEL_VERSIONID,
714 VISOR_CHANNEL_SIGNATURE))
717 err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
722 if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo))
725 if (hdr_info->device_info_struct_bytes <
726 sizeof(struct visor_vbus_deviceinfo))
733 * write_vbus_chp_info() - write the contents of <info> to the struct
734 * visor_vbus_channel.chp_info
735 * @chan: indentifies the s-Par channel that will be updated
736 * @hdr_info: used to find appropriate channel offset to write data
737 * @info: contains the information to write
739 * Writes chipset info into the channel memory to be used for diagnostic
742 * Returns no value since this is debug information and not needed for
743 * device functionality.
745 static void write_vbus_chp_info(struct visorchannel *chan,
746 struct visor_vbus_headerinfo *hdr_info,
747 struct visor_vbus_deviceinfo *info)
749 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
751 if (hdr_info->chp_info_offset == 0)
754 visorchannel_write(chan, off, info, sizeof(*info));
758 * write_vbus_bus_info() - write the contents of <info> to the struct
759 * visor_vbus_channel.bus_info
760 * @chan: indentifies the s-Par channel that will be updated
761 * @hdr_info: used to find appropriate channel offset to write data
762 * @info: contains the information to write
764 * Writes bus info into the channel memory to be used for diagnostic
767 * Returns no value since this is debug information and not needed for
768 * device functionality.
770 static void write_vbus_bus_info(struct visorchannel *chan,
771 struct visor_vbus_headerinfo *hdr_info,
772 struct visor_vbus_deviceinfo *info)
774 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
776 if (hdr_info->bus_info_offset == 0)
779 visorchannel_write(chan, off, info, sizeof(*info));
783 * write_vbus_dev_info() - write the contents of <info> to the struct
784 * visor_vbus_channel.dev_info[<devix>]
785 * @chan: indentifies the s-Par channel that will be updated
786 * @hdr_info: used to find appropriate channel offset to write data
787 * @info: contains the information to write
788 * @devix: the relative device number (0..n-1) of the device on the bus
790 * Writes device info into the channel memory to be used for diagnostic
793 * Returns no value since this is debug information and not needed for
794 * device functionality.
796 static void write_vbus_dev_info(struct visorchannel *chan,
797 struct visor_vbus_headerinfo *hdr_info,
798 struct visor_vbus_deviceinfo *info,
802 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
803 (hdr_info->device_info_struct_bytes * devix);
805 if (hdr_info->dev_info_offset == 0)
808 visorchannel_write(chan, off, info, sizeof(*info));
811 static void bus_device_info_init(
812 struct visor_vbus_deviceinfo *bus_device_info_ptr,
813 const char *dev_type, const char *drv_name)
815 memset(bus_device_info_ptr, 0, sizeof(struct visor_vbus_deviceinfo));
816 snprintf(bus_device_info_ptr->devtype,
817 sizeof(bus_device_info_ptr->devtype),
818 "%s", (dev_type) ? dev_type : "unknownType");
819 snprintf(bus_device_info_ptr->drvname,
820 sizeof(bus_device_info_ptr->drvname),
821 "%s", (drv_name) ? drv_name : "unknownDriver");
822 snprintf(bus_device_info_ptr->infostrs,
823 sizeof(bus_device_info_ptr->infostrs), "kernel ver. %s",
828 * publish_vbus_dev_info() - for a child device just created on a client bus,
829 * fill in information about the driver that is
830 * controlling this device into the appropriate slot
831 * within the vbus channel of the bus instance
832 * @visordev: struct visor_device for the desired device
834 static void publish_vbus_dev_info(struct visor_device *visordev)
837 struct visor_device *bdev;
838 struct visor_driver *visordrv;
839 u32 bus_no = visordev->chipset_bus_no;
840 u32 dev_no = visordev->chipset_dev_no;
841 struct visor_vbus_deviceinfo dev_info;
842 const char *chan_type_name = NULL;
843 struct visor_vbus_headerinfo *hdr_info;
845 if (!visordev->device.driver)
848 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
851 hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info;
854 visordrv = to_visor_driver(visordev->device.driver);
857 * Within the list of device types (by GUID) that the driver
858 * says it supports, find out which one of those types matches
859 * the type of this device, so that we can include the device
862 for (i = 0; visordrv->channel_types[i].name; i++) {
863 if (memcmp(&visordrv->channel_types[i].guid,
864 &visordev->channel_type_guid,
865 sizeof(visordrv->channel_types[i].guid)) == 0) {
866 chan_type_name = visordrv->channel_types[i].name;
871 bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
872 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
873 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
874 write_vbus_bus_info(bdev->visorchannel, hdr_info,
875 &clientbus_driverinfo);
879 * visordriver_probe_device() - handle new visor device coming online
880 * @xdev: struct device for the visor device being probed
882 * This is called automatically upon adding a visor_device (device_add), or
883 * adding a visor_driver (visorbus_register_visor_driver), but only after
884 * visorbus_match() has returned 1 to indicate a successful match between
887 * If successful, a reference to the device will be held onto via get_device().
889 * Return: 0 if successful, meaning the function driver's probe() function
890 * was successful with this device, otherwise a negative errno
891 * value indicating failure reason
893 static int visordriver_probe_device(struct device *xdev)
896 struct visor_driver *drv;
897 struct visor_device *dev;
899 dev = to_visor_device(xdev);
900 drv = to_visor_driver(xdev->driver);
902 mutex_lock(&dev->visordriver_callback_lock);
903 dev->being_removed = false;
905 res = drv->probe(dev);
907 /* success: reference kept via unmatched get_device() */
908 get_device(&dev->device);
909 publish_vbus_dev_info(dev);
912 mutex_unlock(&dev->visordriver_callback_lock);
917 * visorbus_register_visor_driver() - registers the provided visor driver
918 * for handling one or more visor device
919 * types (channel_types)
920 * @drv: the driver to register
922 * A visor function driver calls this function to register
923 * the driver. The caller MUST fill in the following fields within the
925 * name, version, owner, channel_types, probe, remove
927 * Here's how the whole Linux bus / driver / device model works.
929 * At system start-up, the visorbus kernel module is loaded, which registers
930 * visorbus_type as a bus type, using bus_register().
932 * All kernel modules that support particular device types on a
933 * visorbus bus are loaded. Each of these kernel modules calls
934 * visorbus_register_visor_driver() in their init functions, passing a
935 * visor_driver struct. visorbus_register_visor_driver() in turn calls
936 * register_driver(&visor_driver.driver). This .driver member is
937 * initialized with generic methods (like probe), whose sole responsibility
938 * is to act as a broker for the real methods, which are within the
939 * visor_driver struct. (This is the way the subclass behavior is
940 * implemented, since visor_driver is essentially a subclass of the
941 * generic driver.) Whenever a driver_register() happens, core bus code in
942 * the kernel does (see device_attach() in drivers/base/dd.c):
944 * for each dev associated with the bus (the bus that driver is on) that
945 * does not yet have a driver
946 * if bus.match(dev,newdriver) == yes_matched ** .match specified
947 * ** during bus_register().
948 * newdriver.probe(dev) ** for visor drivers, this will call
949 * ** the generic driver.probe implemented in visorbus.c,
950 * ** which in turn calls the probe specified within the
951 * ** struct visor_driver (which was specified by the
952 * ** actual device driver as part of
953 * ** visorbus_register_visor_driver()).
955 * The above dance also happens when a new device appears.
956 * So the question is, how are devices created within the system?
957 * Basically, just call device_add(dev). See pci_bus_add_devices().
958 * pci_scan_device() shows an example of how to build a device struct. It
959 * returns the newly-created struct to pci_scan_single_device(), who adds it
960 * to the list of devices at PCIBUS.devices. That list of devices is what
961 * is traversed by pci_bus_add_devices().
963 * Return: integer indicating success (zero) or failure (non-zero)
965 int visorbus_register_visor_driver(struct visor_driver *drv)
967 /* can't register on a nonexistent bus */
983 drv->driver.name = drv->name;
984 drv->driver.bus = &visorbus_type;
985 drv->driver.probe = visordriver_probe_device;
986 drv->driver.remove = visordriver_remove_device;
987 drv->driver.owner = drv->owner;
990 * driver_register does this:
991 * bus_add_driver(drv)
992 * ->if (drv.bus) ** (bus_type) **
994 * for each dev with bus type of drv.bus
995 * if (!dev.drv) ** no driver assigned yet **
996 * if (bus.match(dev,drv)) [visorbus_match]
998 * if (!drv.probe(dev)) [visordriver_probe_device]
1002 return driver_register(&drv->driver);
1004 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1007 * visorbus_create_instance() - create a device instance for the visorbus itself
1008 * @dev: struct visor_device indicating the bus instance
1010 * Return: 0 for success, otherwise negative errno value indicating reason for
1013 int visorbus_create_instance(struct visor_device *dev)
1015 int id = dev->chipset_bus_no;
1017 struct visor_vbus_headerinfo *hdr_info;
1019 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1023 dev_set_name(&dev->device, "visorbus%d", id);
1024 dev->device.bus = &visorbus_type;
1025 dev->device.groups = visorbus_groups;
1026 dev->device.release = visorbus_release_busdevice;
1028 dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device),
1029 visorbus_debugfs_dir);
1030 dev->debugfs_client_bus_info =
1031 debugfs_create_file("client_bus_info", 0440,
1032 dev->debugfs_dir, dev,
1033 &client_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;
1040 err = device_register(&dev->device);
1042 goto err_debugfs_dir;
1044 list_add_tail(&dev->list_all, &list_all_bus_instances);
1046 dev->state.created = 1;
1047 dev->vbus_hdr_info = (void *)hdr_info;
1048 write_vbus_chp_info(dev->visorchannel, hdr_info,
1049 &chipset_driverinfo);
1050 write_vbus_bus_info(dev->visorchannel, hdr_info,
1051 &clientbus_driverinfo);
1053 visorbus_response(dev, err, CONTROLVM_BUS_CREATE);
1058 debugfs_remove_recursive(dev->debugfs_dir);
1060 dev_err(&dev->device, "%s failed: %d\n", __func__, err);
1065 * visorbus_remove_instance() - remove a device instance for the visorbus itself
1066 * @dev: struct visor_device indentifying the bus to remove
1068 void visorbus_remove_instance(struct visor_device *dev)
1071 * Note that this will result in the release method for
1072 * dev->dev being called, which will call
1073 * visorbus_release_busdevice(). This has something to do with
1074 * the put_device() done in device_unregister(), but I have never
1075 * successfully been able to trace thru the code to see where/how
1076 * release() gets called. But I know it does.
1078 visorchannel_destroy(dev->visorchannel);
1079 kfree(dev->vbus_hdr_info);
1080 list_del(&dev->list_all);
1081 device_unregister(&dev->device);
1082 visorbus_response(dev, 0, CONTROLVM_BUS_DESTROY);
1086 * remove_all_visor_devices() - remove all child visorbus device instances
1088 static void remove_all_visor_devices(void)
1090 struct list_head *listentry, *listtmp;
1092 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1093 struct visor_device *dev = list_entry(listentry,
1094 struct visor_device,
1096 remove_visor_device(dev);
1101 * pause_state_change_complete() - the callback function to be called by a
1102 * visorbus function driver when a
1103 * pending "pause device" operation has
1105 * @dev: struct visor_device identifying the paused device
1106 * @status: 0 iff the pause state change completed successfully, otherwise
1107 * a negative errno value indicating the reason for failure
1109 static void pause_state_change_complete(struct visor_device *dev, int status)
1114 dev->pausing = false;
1115 visorbus_device_changestate_response(dev, status,
1116 segment_state_standby);
1120 * resume_state_change_complete() - the callback function to be called by a
1121 * visorbus function driver when a
1122 * pending "resume device" operation has
1124 * @dev: struct visor_device identifying the resumed device
1125 * @status: 0 iff the resume state change completed successfully, otherwise
1126 * a negative errno value indicating the reason for failure
1128 static void resume_state_change_complete(struct visor_device *dev, int status)
1133 dev->resuming = false;
1136 * Notify the chipset driver that the resume is complete,
1137 * which will presumably want to send some sort of response to
1140 visorbus_device_changestate_response(dev, status,
1141 segment_state_running);
1145 * visorchipset_initiate_device_pause_resume() - start a pause or resume
1146 * operation for a visor device
1147 * @dev: struct visor_device identifying the device being paused or resumed
1148 * @is_pause: true to indicate pause operation, false to indicate resume
1150 * Tell the subordinate function driver for a specific device to pause
1151 * or resume that device. Success/failure result is returned asynchronously
1152 * via a callback function; see pause_state_change_complete() and
1153 * resume_state_change_complete().
1155 static int visorchipset_initiate_device_pause_resume(struct visor_device *dev,
1159 struct visor_driver *drv = NULL;
1161 /* If no driver associated with the device nothing to pause/resume */
1162 if (!dev->device.driver)
1164 if (dev->pausing || dev->resuming)
1167 drv = to_visor_driver(dev->device.driver);
1169 dev->pausing = true;
1170 err = drv->pause(dev, pause_state_change_complete);
1173 * The vbus_dev_info structure in the channel was been cleared,
1174 * make sure it is valid.
1176 publish_vbus_dev_info(dev);
1177 dev->resuming = true;
1178 err = drv->resume(dev, resume_state_change_complete);
1185 * visorchipset_device_pause() - start a pause operation for a visor device
1186 * @dev_info: struct visor_device identifying the device being paused
1188 * Tell the subordinate function driver for a specific device to pause
1189 * that device. Success/failure result is returned asynchronously
1190 * via a callback function; see pause_state_change_complete().
1192 int visorchipset_device_pause(struct visor_device *dev_info)
1196 err = visorchipset_initiate_device_pause_resume(dev_info, true);
1198 dev_info->pausing = false;
1206 * visorchipset_device_resume() - start a resume operation for a visor device
1207 * @dev_info: struct visor_device identifying the device being resumed
1209 * Tell the subordinate function driver for a specific device to resume
1210 * that device. Success/failure result is returned asynchronously
1211 * via a callback function; see resume_state_change_complete().
1213 int visorchipset_device_resume(struct visor_device *dev_info)
1217 err = visorchipset_initiate_device_pause_resume(dev_info, false);
1219 dev_info->resuming = false;
1226 int visorbus_init(void)
1230 visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
1231 if (!visorbus_debugfs_dir)
1234 bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
1236 err = bus_register(&visorbus_type);
1241 bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
1246 void visorbus_exit(void)
1248 struct list_head *listentry, *listtmp;
1250 remove_all_visor_devices();
1252 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1253 struct visor_device *dev = list_entry(listentry,
1254 struct visor_device,
1256 visorbus_remove_instance(dev);
1259 bus_unregister(&visorbus_type);
1260 initialized = false;
1261 debugfs_remove_recursive(visorbus_debugfs_dir);