1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright � 2010 - 2015 UNISYS CORPORATION
7 #include <linux/ctype.h>
8 #include <linux/debugfs.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/visorbus.h>
12 #include <linux/uuid.h>
14 #include "visorbus_private.h"
16 static const guid_t visor_vbus_channel_guid = VISOR_VBUS_CHANNEL_GUID;
18 /* Display string that is guaranteed to be no longer the 99 characters */
20 #define POLLJIFFIES_NORMALCHANNEL 10
22 /* stores whether bus_registration was successful */
23 static bool initialized;
24 static struct dentry *visorbus_debugfs_dir;
27 * DEVICE type attributes
29 * The modalias file will contain the guid of the device.
31 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
34 struct visor_device *vdev;
37 vdev = to_visor_device(dev);
38 guid = visorchannel_get_guid(vdev->visorchannel);
39 return sprintf(buf, "visorbus:%pUl\n", guid);
41 static DEVICE_ATTR_RO(modalias);
43 static struct attribute *visorbus_dev_attrs[] = {
44 &dev_attr_modalias.attr,
48 ATTRIBUTE_GROUPS(visorbus_dev);
50 /* filled in with info about parent chipset driver when we register with it */
51 static struct visor_vbus_deviceinfo chipset_driverinfo;
52 /* filled in with info about this driver, wrt it servicing client busses */
53 static struct visor_vbus_deviceinfo clientbus_driverinfo;
55 /* list of visor_device structs, linked via .list_all */
56 static LIST_HEAD(list_all_bus_instances);
57 /* list of visor_device structs, linked via .list_all */
58 static LIST_HEAD(list_all_device_instances);
61 * Generic function useful for validating any type of channel when it is
62 * received by the client that will be accessing the channel.
63 * Note that <logCtx> is only needed for callers in the EFI environment, and
64 * is used to pass the EFI_DIAG_CAPTURE_PROTOCOL needed to log messages.
66 int visor_check_channel(struct channel_header *ch, struct device *dev,
67 const guid_t *expected_guid, char *chname,
68 u64 expected_min_bytes, u32 expected_version,
69 u64 expected_signature)
71 if (!guid_is_null(expected_guid)) {
72 /* caller wants us to verify type GUID */
73 if (!guid_equal(&ch->chtype, expected_guid)) {
74 dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=type expected=%pUL actual=%pUL\n",
75 chname, expected_guid, expected_guid,
80 /* verify channel size */
81 if (expected_min_bytes > 0) {
82 if (ch->size < expected_min_bytes) {
83 dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
84 chname, expected_guid,
85 (unsigned long long)expected_min_bytes,
90 /* verify channel version */
91 if (expected_version > 0) {
92 if (ch->version_id != expected_version) {
93 dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=version expected=0x%-8.8lx actual=0x%-8.8x\n",
94 chname, expected_guid,
95 (unsigned long)expected_version,
100 /* verify channel signature */
101 if (expected_signature > 0) {
102 if (ch->signature != expected_signature) {
103 dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=signature expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
104 chname, expected_guid, expected_signature,
112 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
114 struct visor_device *dev;
117 dev = to_visor_device(xdev);
118 guid = visorchannel_get_guid(dev->visorchannel);
119 return add_uevent_var(env, "MODALIAS=visorbus:%pUl", guid);
123 * visorbus_match() - called automatically upon adding a visor_device
124 * (device_add), or adding a visor_driver
125 * (visorbus_register_visor_driver)
126 * @xdev: struct device for the device being matched
127 * @xdrv: struct device_driver for driver to match device against
129 * Return: 1 iff the provided driver can control the specified device
131 static int visorbus_match(struct device *xdev, struct device_driver *xdrv)
133 const guid_t *channel_type;
135 struct visor_device *dev;
136 struct visor_driver *drv;
137 struct visorchannel *chan;
139 dev = to_visor_device(xdev);
140 channel_type = visorchannel_get_guid(dev->visorchannel);
141 drv = to_visor_driver(xdrv);
142 chan = dev->visorchannel;
143 if (!drv->channel_types)
145 for (i = 0; !guid_is_null(&drv->channel_types[i].guid); i++)
146 if (guid_equal(&drv->channel_types[i].guid, channel_type) &&
147 visor_check_channel(visorchannel_get_header(chan),
149 &drv->channel_types[i].guid,
150 (char *)drv->channel_types[i].name,
151 drv->channel_types[i].min_bytes,
152 drv->channel_types[i].version,
153 VISOR_CHANNEL_SIGNATURE))
159 * This describes the TYPE of bus.
160 * (Don't confuse this with an INSTANCE of the bus.)
162 static struct bus_type visorbus_type = {
164 .match = visorbus_match,
165 .uevent = visorbus_uevent,
166 .dev_groups = visorbus_dev_groups,
169 struct visor_busdev {
174 static int match_visorbus_dev_by_id(struct device *dev, const void *data)
176 struct visor_device *vdev = to_visor_device(dev);
177 const struct visor_busdev *id = data;
179 if (vdev->chipset_bus_no == id->bus_no &&
180 vdev->chipset_dev_no == id->dev_no)
185 struct visor_device *visorbus_get_device_by_id(u32 bus_no, u32 dev_no,
186 struct visor_device *from)
189 struct device *dev_start = NULL;
190 struct visor_busdev id = {
196 dev_start = &from->device;
197 dev = bus_find_device(&visorbus_type, dev_start, (void *)&id,
198 match_visorbus_dev_by_id);
201 return to_visor_device(dev);
205 * visorbus_release_busdevice() - called when device_unregister() is called for
206 * the bus device instance, after all other tasks
207 * involved with destroying the dev are complete
208 * @xdev: struct device for the bus being released
210 static void visorbus_release_busdevice(struct device *xdev)
212 struct visor_device *dev = dev_get_drvdata(xdev);
214 debugfs_remove(dev->debugfs_bus_info);
215 debugfs_remove_recursive(dev->debugfs_dir);
216 visorchannel_destroy(dev->visorchannel);
221 * visorbus_release_device() - called when device_unregister() is called for
222 * each child device instance
223 * @xdev: struct device for the visor device being released
225 static void visorbus_release_device(struct device *xdev)
227 struct visor_device *dev = to_visor_device(xdev);
229 visorchannel_destroy(dev->visorchannel);
234 * BUS specific channel attributes to appear under
235 * /sys/bus/visorbus<x>/dev<y>/channel
238 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
241 struct visor_device *vdev = to_visor_device(dev);
243 return sprintf(buf, "0x%llx\n",
244 visorchannel_get_physaddr(vdev->visorchannel));
246 static DEVICE_ATTR_RO(physaddr);
248 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
251 struct visor_device *vdev = to_visor_device(dev);
253 return sprintf(buf, "0x%lx\n",
254 visorchannel_get_nbytes(vdev->visorchannel));
256 static DEVICE_ATTR_RO(nbytes);
258 static ssize_t clientpartition_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct visor_device *vdev = to_visor_device(dev);
263 return sprintf(buf, "0x%llx\n",
264 visorchannel_get_clientpartition(vdev->visorchannel));
266 static DEVICE_ATTR_RO(clientpartition);
268 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
271 struct visor_device *vdev = to_visor_device(dev);
272 char typeid[LINESIZE];
274 return sprintf(buf, "%s\n",
275 visorchannel_id(vdev->visorchannel, typeid));
277 static DEVICE_ATTR_RO(typeguid);
279 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
282 struct visor_device *vdev = to_visor_device(dev);
283 char zoneid[LINESIZE];
285 return sprintf(buf, "%s\n",
286 visorchannel_zoneid(vdev->visorchannel, zoneid));
288 static DEVICE_ATTR_RO(zoneguid);
290 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
294 struct bus_type *xbus = dev->bus;
295 struct device_driver *xdrv = dev->driver;
296 struct visor_driver *drv = NULL;
300 i = xbus->match(dev, xdrv);
303 drv = to_visor_driver(xdrv);
304 return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
306 static DEVICE_ATTR_RO(typename);
308 static struct attribute *channel_attrs[] = {
309 &dev_attr_physaddr.attr,
310 &dev_attr_nbytes.attr,
311 &dev_attr_clientpartition.attr,
312 &dev_attr_typeguid.attr,
313 &dev_attr_zoneguid.attr,
314 &dev_attr_typename.attr,
318 ATTRIBUTE_GROUPS(channel);
321 * BUS instance attributes
323 * define & implement display of bus attributes under
324 * /sys/bus/visorbus/devices/visorbus<n>.
326 static ssize_t partition_handle_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 struct visor_device *vdev = to_visor_device(dev);
330 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
332 return sprintf(buf, "0x%llx\n", handle);
334 static DEVICE_ATTR_RO(partition_handle);
336 static ssize_t partition_guid_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
339 struct visor_device *vdev = to_visor_device(dev);
341 return sprintf(buf, "{%pUb}\n", &vdev->partition_guid);
343 static DEVICE_ATTR_RO(partition_guid);
345 static ssize_t partition_name_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
348 struct visor_device *vdev = to_visor_device(dev);
350 return sprintf(buf, "%s\n", vdev->name);
352 static DEVICE_ATTR_RO(partition_name);
354 static ssize_t channel_addr_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
357 struct visor_device *vdev = to_visor_device(dev);
358 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
360 return sprintf(buf, "0x%llx\n", addr);
362 static DEVICE_ATTR_RO(channel_addr);
364 static ssize_t channel_bytes_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
367 struct visor_device *vdev = to_visor_device(dev);
368 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
370 return sprintf(buf, "0x%llx\n", nbytes);
372 static DEVICE_ATTR_RO(channel_bytes);
374 static ssize_t channel_id_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 struct visor_device *vdev = to_visor_device(dev);
380 visorchannel_id(vdev->visorchannel, buf);
385 static DEVICE_ATTR_RO(channel_id);
387 static struct attribute *visorbus_attrs[] = {
388 &dev_attr_partition_handle.attr,
389 &dev_attr_partition_guid.attr,
390 &dev_attr_partition_name.attr,
391 &dev_attr_channel_addr.attr,
392 &dev_attr_channel_bytes.attr,
393 &dev_attr_channel_id.attr,
397 ATTRIBUTE_GROUPS(visorbus);
400 * BUS debugfs entries
402 * define & implement display of debugfs attributes under
403 * /sys/kernel/debug/visorbus/visorbus<n>.
407 * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo
408 * and write it to a seq_file
409 * @devinfo: the struct visor_vbus_deviceinfo to format
410 * @seq: seq_file to write to
411 * @devix: the device index to be included in the output data, or -1 if no
412 * device index is to be included
414 * Reads @devInfo, and writes it in human-readable notation to @seq.
416 static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo,
417 struct seq_file *seq, int devix)
419 /* uninitialized vbus device entry */
420 if (!isprint(devinfo->devtype[0]))
423 seq_printf(seq, "[%d]", devix);
425 /* vbus device entry is for bus or chipset */
428 * Note: because the s-Par back-end is free to scribble in this area,
429 * we never assume '\0'-termination.
431 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype),
432 (int)sizeof(devinfo->devtype), devinfo->devtype);
433 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname),
434 (int)sizeof(devinfo->drvname), devinfo->drvname);
435 seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs),
439 static int bus_info_debugfs_show(struct seq_file *seq, void *v)
443 struct visor_vbus_deviceinfo dev_info;
444 struct visor_device *vdev = seq->private;
445 struct visorchannel *channel = vdev->visorchannel;
451 "Client device/driver info for %s partition (vbus #%u):\n",
452 ((vdev->name) ? (char *)(vdev->name) : ""),
453 vdev->chipset_bus_no);
454 if (visorchannel_read(channel,
455 offsetof(struct visor_vbus_channel, chp_info),
456 &dev_info, sizeof(dev_info)) >= 0)
457 vbuschannel_print_devinfo(&dev_info, seq, -1);
458 if (visorchannel_read(channel,
459 offsetof(struct visor_vbus_channel, bus_info),
460 &dev_info, sizeof(dev_info)) >= 0)
461 vbuschannel_print_devinfo(&dev_info, seq, -1);
463 off = offsetof(struct visor_vbus_channel, dev_info);
464 while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) {
465 if (visorchannel_read(channel, off, &dev_info,
466 sizeof(dev_info)) >= 0)
467 vbuschannel_print_devinfo(&dev_info, seq, i);
468 off += sizeof(dev_info);
474 static int bus_info_debugfs_open(struct inode *inode, struct file *file)
476 return single_open(file, bus_info_debugfs_show, inode->i_private);
479 static const struct file_operations bus_info_debugfs_fops = {
480 .owner = THIS_MODULE,
481 .open = bus_info_debugfs_open,
484 .release = single_release,
487 static void dev_periodic_work(struct timer_list *t)
489 struct visor_device *dev = from_timer(dev, t, timer);
490 struct visor_driver *drv = to_visor_driver(dev->device.driver);
492 drv->channel_interrupt(dev);
493 mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
496 static int dev_start_periodic_work(struct visor_device *dev)
498 if (dev->being_removed || dev->timer_active)
501 /* now up by at least 2 */
502 get_device(&dev->device);
503 dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
504 add_timer(&dev->timer);
505 dev->timer_active = true;
509 static void dev_stop_periodic_work(struct visor_device *dev)
511 if (!dev->timer_active)
514 del_timer_sync(&dev->timer);
515 dev->timer_active = false;
516 put_device(&dev->device);
520 * visordriver_remove_device() - handle visor device going away
521 * @xdev: struct device for the visor device being removed
523 * This is called when device_unregister() is called for each child device
524 * instance, to notify the appropriate visorbus function driver that the device
525 * is going away, and to decrease the reference count of the device.
527 * Return: 0 iff successful
529 static int visordriver_remove_device(struct device *xdev)
531 struct visor_device *dev = to_visor_device(xdev);
532 struct visor_driver *drv = to_visor_driver(xdev->driver);
534 mutex_lock(&dev->visordriver_callback_lock);
535 dev->being_removed = true;
537 mutex_unlock(&dev->visordriver_callback_lock);
538 dev_stop_periodic_work(dev);
539 put_device(&dev->device);
544 * visorbus_unregister_visor_driver() - unregisters the provided driver
545 * @drv: the driver to unregister
547 * A visor function driver calls this function to unregister the driver,
548 * i.e., within its module_exit function.
550 void visorbus_unregister_visor_driver(struct visor_driver *drv)
552 driver_unregister(&drv->driver);
554 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
557 * visorbus_read_channel() - reads from the designated channel into
558 * the provided buffer
559 * @dev: the device whose channel is read from
560 * @offset: the offset into the channel at which reading starts
561 * @dest: the destination buffer that is written into from the channel
562 * @nbytes: the number of bytes to read from the channel
564 * If receiving a message, use the visorchannel_signalremove() function instead.
566 * Return: integer indicating success (zero) or failure (non-zero)
568 int visorbus_read_channel(struct visor_device *dev, unsigned long offset,
569 void *dest, unsigned long nbytes)
571 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
573 EXPORT_SYMBOL_GPL(visorbus_read_channel);
576 * visorbus_write_channel() - writes the provided buffer into the designated
578 * @dev: the device whose channel is written to
579 * @offset: the offset into the channel at which writing starts
580 * @src: the source buffer that is written into the channel
581 * @nbytes: the number of bytes to write into the channel
583 * If sending a message, use the visorchannel_signalinsert() function instead.
585 * Return: integer indicating success (zero) or failure (non-zero)
587 int visorbus_write_channel(struct visor_device *dev, unsigned long offset,
588 void *src, unsigned long nbytes)
590 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
592 EXPORT_SYMBOL_GPL(visorbus_write_channel);
595 * visorbus_enable_channel_interrupts() - enables interrupts on the
597 * @dev: the device on which to enable interrupts
599 * Currently we don't yet have a real interrupt, so for now we just call the
600 * interrupt function periodically via a timer.
602 int visorbus_enable_channel_interrupts(struct visor_device *dev)
604 struct visor_driver *drv = to_visor_driver(dev->device.driver);
606 if (!drv->channel_interrupt) {
607 dev_err(&dev->device, "%s no interrupt function!\n", __func__);
611 return dev_start_periodic_work(dev);
613 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
616 * visorbus_disable_channel_interrupts() - disables interrupts on the
618 * @dev: the device on which to disable interrupts
620 void visorbus_disable_channel_interrupts(struct visor_device *dev)
622 dev_stop_periodic_work(dev);
624 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
627 * create_visor_device() - create visor device as a result of receiving the
628 * controlvm device_create message for a new device
629 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
630 * for chipset_bus_no and chipset_dev_no, that will be initialized
632 * This is how everything starts from the device end.
633 * This function is called when a channel first appears via a ControlVM
634 * message. In response, this function allocates a visor_device to correspond
635 * to the new channel, and attempts to connect it the appropriate * driver. If
636 * the appropriate driver is found, the visor_driver.probe() function for that
637 * driver will be called, and will be passed the new * visor_device that we
640 * It's ok if the appropriate driver is not yet loaded, because in that case
641 * the new device struct will just stick around in the bus' list of devices.
642 * When the appropriate driver calls visorbus_register_visor_driver(), the
643 * visor_driver.probe() for the new driver will be called with the new device.
645 * Return: 0 if successful, otherwise the negative value returned by
646 * device_add() indicating the reason for failure
648 int create_visor_device(struct visor_device *dev)
651 u32 chipset_bus_no = dev->chipset_bus_no;
652 u32 chipset_dev_no = dev->chipset_dev_no;
654 mutex_init(&dev->visordriver_callback_lock);
655 dev->device.bus = &visorbus_type;
656 dev->device.groups = channel_groups;
657 device_initialize(&dev->device);
658 dev->device.release = visorbus_release_device;
659 /* keep a reference just for us (now 2) */
660 get_device(&dev->device);
661 timer_setup(&dev->timer, dev_periodic_work, 0);
663 * bus_id must be a unique name with respect to this bus TYPE (NOT bus
664 * instance). That's why we need to include the bus number within the
667 err = dev_set_name(&dev->device, "vbus%u:dev%u",
668 chipset_bus_no, chipset_dev_no);
672 * device_add does this:
673 * bus_add_device(dev)
674 * ->device_attach(dev)
675 * ->for each driver drv registered on the bus that dev is on
676 * if (dev.drv) ** device already has a driver **
677 * ** not sure we could ever get here... **
679 * if (bus.match(dev,drv)) [visorbus_match]
681 * if (!drv.probe(dev)) [visordriver_probe_device]
684 * Note that device_add does NOT fail if no driver failed to claim the
685 * device. The device will be linked onto bus_type.klist_devices
686 * regardless (use bus_for_each_dev).
688 err = device_add(&dev->device);
691 list_add_tail(&dev->list_all, &list_all_device_instances);
692 dev->state.created = 1;
693 visorbus_response(dev, err, CONTROLVM_DEVICE_CREATE);
694 /* success: reference kept via unmatched get_device() */
698 put_device(&dev->device);
699 dev_err(&dev->device, "Creating visor device failed. %d\n", err);
703 void remove_visor_device(struct visor_device *dev)
705 list_del(&dev->list_all);
706 put_device(&dev->device);
707 if (dev->pending_msg_hdr)
708 visorbus_response(dev, 0, CONTROLVM_DEVICE_DESTROY);
709 device_unregister(&dev->device);
712 static int get_vbus_header_info(struct visorchannel *chan,
714 struct visor_vbus_headerinfo *hdr_info)
718 if (!visor_check_channel(visorchannel_get_header(chan),
720 &visor_vbus_channel_guid,
722 sizeof(struct visor_vbus_channel),
723 VISOR_VBUS_CHANNEL_VERSIONID,
724 VISOR_CHANNEL_SIGNATURE))
727 err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
731 if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo))
733 if (hdr_info->device_info_struct_bytes <
734 sizeof(struct visor_vbus_deviceinfo))
740 * write_vbus_chp_info() - write the contents of <info> to the struct
741 * visor_vbus_channel.chp_info
742 * @chan: indentifies the s-Par channel that will be updated
743 * @hdr_info: used to find appropriate channel offset to write data
744 * @info: contains the information to write
746 * Writes chipset info into the channel memory to be used for diagnostic
749 * Returns no value since this is debug information and not needed for
750 * device functionality.
752 static void write_vbus_chp_info(struct visorchannel *chan,
753 struct visor_vbus_headerinfo *hdr_info,
754 struct visor_vbus_deviceinfo *info)
758 if (hdr_info->chp_info_offset == 0)
761 off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
762 visorchannel_write(chan, off, info, sizeof(*info));
766 * write_vbus_bus_info() - write the contents of <info> to the struct
767 * visor_vbus_channel.bus_info
768 * @chan: indentifies the s-Par channel that will be updated
769 * @hdr_info: used to find appropriate channel offset to write data
770 * @info: contains the information to write
772 * Writes bus info into the channel memory to be used for diagnostic
775 * Returns no value since this is debug information and not needed for
776 * device functionality.
778 static void write_vbus_bus_info(struct visorchannel *chan,
779 struct visor_vbus_headerinfo *hdr_info,
780 struct visor_vbus_deviceinfo *info)
784 if (hdr_info->bus_info_offset == 0)
787 off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
788 visorchannel_write(chan, off, info, sizeof(*info));
792 * write_vbus_dev_info() - write the contents of <info> to the struct
793 * visor_vbus_channel.dev_info[<devix>]
794 * @chan: indentifies the s-Par channel that will be updated
795 * @hdr_info: used to find appropriate channel offset to write data
796 * @info: contains the information to write
797 * @devix: the relative device number (0..n-1) of the device on the bus
799 * Writes device info into the channel memory to be used for diagnostic
802 * Returns no value since this is debug information and not needed for
803 * device functionality.
805 static void write_vbus_dev_info(struct visorchannel *chan,
806 struct visor_vbus_headerinfo *hdr_info,
807 struct visor_vbus_deviceinfo *info,
812 if (hdr_info->dev_info_offset == 0)
814 off = (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
815 (hdr_info->device_info_struct_bytes * devix);
816 visorchannel_write(chan, off, info, sizeof(*info));
819 static void bus_device_info_init(
820 struct visor_vbus_deviceinfo *bus_device_info_ptr,
821 const char *dev_type, const char *drv_name)
823 memset(bus_device_info_ptr, 0, sizeof(struct visor_vbus_deviceinfo));
824 snprintf(bus_device_info_ptr->devtype,
825 sizeof(bus_device_info_ptr->devtype),
826 "%s", (dev_type) ? dev_type : "unknownType");
827 snprintf(bus_device_info_ptr->drvname,
828 sizeof(bus_device_info_ptr->drvname),
829 "%s", (drv_name) ? drv_name : "unknownDriver");
830 snprintf(bus_device_info_ptr->infostrs,
831 sizeof(bus_device_info_ptr->infostrs), "kernel ver. %s",
836 * publish_vbus_dev_info() - for a child device just created on a client bus,
837 * fill in information about the driver that is
838 * controlling this device into the appropriate slot
839 * within the vbus channel of the bus instance
840 * @visordev: struct visor_device for the desired device
842 static void publish_vbus_dev_info(struct visor_device *visordev)
845 struct visor_device *bdev;
846 struct visor_driver *visordrv;
847 u32 bus_no = visordev->chipset_bus_no;
848 u32 dev_no = visordev->chipset_dev_no;
849 struct visor_vbus_deviceinfo dev_info;
850 const char *chan_type_name = NULL;
851 struct visor_vbus_headerinfo *hdr_info;
853 if (!visordev->device.driver)
855 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
858 hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info;
861 visordrv = to_visor_driver(visordev->device.driver);
864 * Within the list of device types (by GUID) that the driver
865 * says it supports, find out which one of those types matches
866 * the type of this device, so that we can include the device
869 for (i = 0; visordrv->channel_types[i].name; i++) {
870 if (guid_equal(&visordrv->channel_types[i].guid,
871 &visordev->channel_type_guid)) {
872 chan_type_name = visordrv->channel_types[i].name;
876 bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
877 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
878 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
879 write_vbus_bus_info(bdev->visorchannel, hdr_info,
880 &clientbus_driverinfo);
884 * visordriver_probe_device() - handle new visor device coming online
885 * @xdev: struct device for the visor device being probed
887 * This is called automatically upon adding a visor_device (device_add), or
888 * adding a visor_driver (visorbus_register_visor_driver), but only after
889 * visorbus_match() has returned 1 to indicate a successful match between
892 * If successful, a reference to the device will be held onto via get_device().
894 * Return: 0 if successful, meaning the function driver's probe() function
895 * was successful with this device, otherwise a negative errno
896 * value indicating failure reason
898 static int visordriver_probe_device(struct device *xdev)
901 struct visor_driver *drv = to_visor_driver(xdev->driver);
902 struct visor_device *dev = to_visor_device(xdev);
904 mutex_lock(&dev->visordriver_callback_lock);
905 dev->being_removed = false;
906 err = drv->probe(dev);
908 mutex_unlock(&dev->visordriver_callback_lock);
911 /* success: reference kept via unmatched get_device() */
912 get_device(&dev->device);
913 publish_vbus_dev_info(dev);
914 mutex_unlock(&dev->visordriver_callback_lock);
919 * visorbus_register_visor_driver() - registers the provided visor driver for
920 * handling one or more visor device
921 * types (channel_types)
922 * @drv: the driver to register
924 * A visor function driver calls this function to register the driver. The
925 * caller MUST fill in the following fields within the #drv structure:
926 * name, version, owner, channel_types, probe, remove
928 * Here's how the whole Linux bus / driver / device model works.
930 * At system start-up, the visorbus kernel module is loaded, which registers
931 * visorbus_type as a bus type, using bus_register().
933 * All kernel modules that support particular device types on a
934 * visorbus bus are loaded. Each of these kernel modules calls
935 * visorbus_register_visor_driver() in their init functions, passing a
936 * visor_driver struct. visorbus_register_visor_driver() in turn calls
937 * register_driver(&visor_driver.driver). This .driver member is
938 * initialized with generic methods (like probe), whose sole responsibility
939 * is to act as a broker for the real methods, which are within the
940 * visor_driver struct. (This is the way the subclass behavior is
941 * implemented, since visor_driver is essentially a subclass of the
942 * generic driver.) Whenever a driver_register() happens, core bus code in
943 * the kernel does (see device_attach() in drivers/base/dd.c):
945 * for each dev associated with the bus (the bus that driver is on) that
946 * does not yet have a driver
947 * if bus.match(dev,newdriver) == yes_matched ** .match specified
948 * ** during bus_register().
949 * newdriver.probe(dev) ** for visor drivers, this will call
950 * ** the generic driver.probe implemented in visorbus.c,
951 * ** which in turn calls the probe specified within the
952 * ** struct visor_driver (which was specified by the
953 * ** actual device driver as part of
954 * ** visorbus_register_visor_driver()).
956 * The above dance also happens when a new device appears.
957 * So the question is, how are devices created within the system?
958 * Basically, just call device_add(dev). See pci_bus_add_devices().
959 * pci_scan_device() shows an example of how to build a device struct. It
960 * returns the newly-created struct to pci_scan_single_device(), who adds it
961 * to the list of devices at PCIBUS.devices. That list of devices is what
962 * is traversed by pci_bus_add_devices().
964 * Return: integer indicating success (zero) or failure (non-zero)
966 int visorbus_register_visor_driver(struct visor_driver *drv)
968 /* can't register on a nonexistent bus */
980 drv->driver.name = drv->name;
981 drv->driver.bus = &visorbus_type;
982 drv->driver.probe = visordriver_probe_device;
983 drv->driver.remove = visordriver_remove_device;
984 drv->driver.owner = drv->owner;
986 * driver_register does this:
987 * bus_add_driver(drv)
988 * ->if (drv.bus) ** (bus_type) **
990 * for each dev with bus type of drv.bus
991 * if (!dev.drv) ** no driver assigned yet **
992 * if (bus.match(dev,drv)) [visorbus_match]
994 * if (!drv.probe(dev)) [visordriver_probe_device]
997 return driver_register(&drv->driver);
999 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1002 * visorbus_create_instance() - create a device instance for the visorbus itself
1003 * @dev: struct visor_device indicating the bus instance
1005 * Return: 0 for success, otherwise negative errno value indicating reason for
1008 int visorbus_create_instance(struct visor_device *dev)
1010 int id = dev->chipset_bus_no;
1012 struct visor_vbus_headerinfo *hdr_info;
1014 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1017 dev_set_name(&dev->device, "visorbus%d", id);
1018 dev->device.bus = &visorbus_type;
1019 dev->device.groups = visorbus_groups;
1020 dev->device.release = visorbus_release_busdevice;
1021 dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device),
1022 visorbus_debugfs_dir);
1023 dev->debugfs_bus_info = debugfs_create_file("client_bus_info", 0440,
1024 dev->debugfs_dir, dev,
1025 &bus_info_debugfs_fops);
1026 dev_set_drvdata(&dev->device, dev);
1027 err = get_vbus_header_info(dev->visorchannel, &dev->device, hdr_info);
1029 goto err_debugfs_dir;
1030 err = device_register(&dev->device);
1032 goto err_debugfs_dir;
1033 list_add_tail(&dev->list_all, &list_all_bus_instances);
1034 dev->state.created = 1;
1035 dev->vbus_hdr_info = (void *)hdr_info;
1036 write_vbus_chp_info(dev->visorchannel, hdr_info, &chipset_driverinfo);
1037 write_vbus_bus_info(dev->visorchannel, hdr_info, &clientbus_driverinfo);
1038 visorbus_response(dev, err, CONTROLVM_BUS_CREATE);
1042 debugfs_remove_recursive(dev->debugfs_dir);
1044 dev_err(&dev->device, "%s failed: %d\n", __func__, err);
1049 * visorbus_remove_instance() - remove a device instance for the visorbus itself
1050 * @dev: struct visor_device indentifying the bus to remove
1052 void visorbus_remove_instance(struct visor_device *dev)
1055 * Note that this will result in the release method for
1056 * dev->dev being called, which will call
1057 * visorbus_release_busdevice(). This has something to do with
1058 * the put_device() done in device_unregister(), but I have never
1059 * successfully been able to trace thru the code to see where/how
1060 * release() gets called. But I know it does.
1062 kfree(dev->vbus_hdr_info);
1063 list_del(&dev->list_all);
1064 if (dev->pending_msg_hdr)
1065 visorbus_response(dev, 0, CONTROLVM_BUS_DESTROY);
1066 device_unregister(&dev->device);
1070 * remove_all_visor_devices() - remove all child visorbus device instances
1072 static void remove_all_visor_devices(void)
1074 struct list_head *listentry, *listtmp;
1076 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1077 struct visor_device *dev;
1079 dev = list_entry(listentry, struct visor_device, list_all);
1080 remove_visor_device(dev);
1085 * pause_state_change_complete() - the callback function to be called by a
1086 * visorbus function driver when a
1087 * pending "pause device" operation has
1089 * @dev: struct visor_device identifying the paused device
1090 * @status: 0 iff the pause state change completed successfully, otherwise
1091 * a negative errno value indicating the reason for failure
1093 static void pause_state_change_complete(struct visor_device *dev, int status)
1098 dev->pausing = false;
1099 visorbus_device_changestate_response(dev, status,
1100 segment_state_standby);
1104 * resume_state_change_complete() - the callback function to be called by a
1105 * visorbus function driver when a
1106 * pending "resume device" operation has
1108 * @dev: struct visor_device identifying the resumed device
1109 * @status: 0 iff the resume state change completed successfully, otherwise
1110 * a negative errno value indicating the reason for failure
1112 static void resume_state_change_complete(struct visor_device *dev, int status)
1117 dev->resuming = false;
1119 * Notify the chipset driver that the resume is complete,
1120 * which will presumably want to send some sort of response to
1123 visorbus_device_changestate_response(dev, status,
1124 segment_state_running);
1128 * visorchipset_initiate_device_pause_resume() - start a pause or resume
1129 * operation for a visor device
1130 * @dev: struct visor_device identifying the device being paused or resumed
1131 * @is_pause: true to indicate pause operation, false to indicate resume
1133 * Tell the subordinate function driver for a specific device to pause
1134 * or resume that device. Success/failure result is returned asynchronously
1135 * via a callback function; see pause_state_change_complete() and
1136 * resume_state_change_complete().
1138 static int visorchipset_initiate_device_pause_resume(struct visor_device *dev,
1142 struct visor_driver *drv;
1144 /* If no driver associated with the device nothing to pause/resume */
1145 if (!dev->device.driver)
1147 if (dev->pausing || dev->resuming)
1150 drv = to_visor_driver(dev->device.driver);
1152 dev->pausing = true;
1153 err = drv->pause(dev, pause_state_change_complete);
1156 * The vbus_dev_info structure in the channel was been cleared,
1157 * make sure it is valid.
1159 publish_vbus_dev_info(dev);
1160 dev->resuming = true;
1161 err = drv->resume(dev, resume_state_change_complete);
1167 * visorchipset_device_pause() - start a pause operation for a visor device
1168 * @dev_info: struct visor_device identifying the device being paused
1170 * Tell the subordinate function driver for a specific device to pause
1171 * that device. Success/failure result is returned asynchronously
1172 * via a callback function; see pause_state_change_complete().
1174 int visorchipset_device_pause(struct visor_device *dev_info)
1178 err = visorchipset_initiate_device_pause_resume(dev_info, true);
1180 dev_info->pausing = false;
1187 * visorchipset_device_resume() - start a resume operation for a visor device
1188 * @dev_info: struct visor_device identifying the device being resumed
1190 * Tell the subordinate function driver for a specific device to resume
1191 * that device. Success/failure result is returned asynchronously
1192 * via a callback function; see resume_state_change_complete().
1194 int visorchipset_device_resume(struct visor_device *dev_info)
1198 err = visorchipset_initiate_device_pause_resume(dev_info, false);
1200 dev_info->resuming = false;
1206 int visorbus_init(void)
1210 visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
1211 bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
1212 err = bus_register(&visorbus_type);
1216 bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
1220 void visorbus_exit(void)
1222 struct list_head *listentry, *listtmp;
1224 remove_all_visor_devices();
1225 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1226 struct visor_device *dev;
1228 dev = list_entry(listentry, struct visor_device, list_all);
1229 visorbus_remove_instance(dev);
1231 bus_unregister(&visorbus_type);
1232 initialized = false;
1233 debugfs_remove_recursive(visorbus_debugfs_dir);