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
,
73 const guid_t
*expected_guid
,
75 u64 expected_min_bytes
,
77 u64 expected_signature
)
79 if (!guid_is_null(expected_guid
)) {
80 /* caller wants us to verify type GUID */
81 if (!guid_equal(&ch
->chtype
, expected_guid
)) {
82 pr_err("Channel mismatch on channel=%s(%pUL) field=type expected=%pUL actual=%pUL\n",
83 chname
, expected_guid
,
84 expected_guid
, &ch
->chtype
);
88 /* verify channel size */
89 if (expected_min_bytes
> 0) {
90 if (ch
->size
< expected_min_bytes
) {
91 pr_err("Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
92 chname
, expected_guid
,
93 (unsigned long long)expected_min_bytes
,
98 /* verify channel version */
99 if (expected_version
> 0) {
100 if (ch
->version_id
!= expected_version
) {
101 pr_err("Channel mismatch on channel=%s(%pUL) field=version expected=0x%-8.8lx actual=0x%-8.8x\n",
102 chname
, expected_guid
,
103 (unsigned long)expected_version
,
108 /* verify channel signature */
109 if (expected_signature
> 0) {
110 if (ch
->signature
!= expected_signature
) {
111 pr_err("Channel mismatch on channel=%s(%pUL) field=signature expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
112 chname
, expected_guid
,
113 expected_signature
, ch
->signature
);
119 EXPORT_SYMBOL_GPL(visor_check_channel
);
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
)
155 !guid_is_null(&drv
->channel_types
[i
].guid
) || drv
->channel_types
[i
].name
;
157 if (guid_equal(&drv
->channel_types
[i
].guid
, channel_type
))
164 * This describes the TYPE of bus.
165 * (Don't confuse this with an INSTANCE of the bus.)
167 struct bus_type visorbus_type
= {
169 .match
= visorbus_match
,
170 .uevent
= visorbus_uevent
,
171 .dev_groups
= visorbus_dev_groups
,
175 * visorbus_release_busdevice() - called when device_unregister() is called for
176 * the bus device instance, after all other tasks
177 * involved with destroying the dev are complete
178 * @xdev: struct device for the bus being released
180 static void visorbus_release_busdevice(struct device
*xdev
)
182 struct visor_device
*dev
= dev_get_drvdata(xdev
);
184 debugfs_remove(dev
->debugfs_client_bus_info
);
185 debugfs_remove_recursive(dev
->debugfs_dir
);
190 * visorbus_release_device() - called when device_unregister() is called for
191 * each child device instance
192 * @xdev: struct device for the visor device being released
194 static void visorbus_release_device(struct device
*xdev
)
196 struct visor_device
*dev
= to_visor_device(xdev
);
198 visorchannel_destroy(dev
->visorchannel
);
203 * begin implementation of specific channel attributes to appear under
204 * /sys/bus/visorbus<x>/dev<y>/channel
207 static ssize_t
physaddr_show(struct device
*dev
, struct device_attribute
*attr
,
210 struct visor_device
*vdev
= to_visor_device(dev
);
212 return sprintf(buf
, "0x%llx\n",
213 visorchannel_get_physaddr(vdev
->visorchannel
));
215 static DEVICE_ATTR_RO(physaddr
);
217 static ssize_t
nbytes_show(struct device
*dev
, struct device_attribute
*attr
,
220 struct visor_device
*vdev
= to_visor_device(dev
);
222 return sprintf(buf
, "0x%lx\n",
223 visorchannel_get_nbytes(vdev
->visorchannel
));
225 static DEVICE_ATTR_RO(nbytes
);
227 static ssize_t
clientpartition_show(struct device
*dev
,
228 struct device_attribute
*attr
, char *buf
)
230 struct visor_device
*vdev
= to_visor_device(dev
);
232 return sprintf(buf
, "0x%llx\n",
233 visorchannel_get_clientpartition(vdev
->visorchannel
));
235 static DEVICE_ATTR_RO(clientpartition
);
237 static ssize_t
typeguid_show(struct device
*dev
, struct device_attribute
*attr
,
240 struct visor_device
*vdev
= to_visor_device(dev
);
241 char typeid[LINESIZE
];
243 return sprintf(buf
, "%s\n",
244 visorchannel_id(vdev
->visorchannel
, typeid));
246 static DEVICE_ATTR_RO(typeguid
);
248 static ssize_t
zoneguid_show(struct device
*dev
, struct device_attribute
*attr
,
251 struct visor_device
*vdev
= to_visor_device(dev
);
252 char zoneid
[LINESIZE
];
254 return sprintf(buf
, "%s\n",
255 visorchannel_zoneid(vdev
->visorchannel
, zoneid
));
257 static DEVICE_ATTR_RO(zoneguid
);
259 static ssize_t
typename_show(struct device
*dev
, struct device_attribute
*attr
,
263 struct bus_type
*xbus
= dev
->bus
;
264 struct device_driver
*xdrv
= dev
->driver
;
265 struct visor_driver
*drv
= NULL
;
269 i
= xbus
->match(dev
, xdrv
);
272 drv
= to_visor_driver(xdrv
);
273 return sprintf(buf
, "%s\n", drv
->channel_types
[i
- 1].name
);
275 static DEVICE_ATTR_RO(typename
);
277 static struct attribute
*channel_attrs
[] = {
278 &dev_attr_physaddr
.attr
,
279 &dev_attr_nbytes
.attr
,
280 &dev_attr_clientpartition
.attr
,
281 &dev_attr_typeguid
.attr
,
282 &dev_attr_zoneguid
.attr
,
283 &dev_attr_typename
.attr
,
287 ATTRIBUTE_GROUPS(channel
);
289 /* end implementation of specific channel attributes */
292 * BUS instance attributes
294 * define & implement display of bus attributes under
295 * /sys/bus/visorbus/devices/visorbus<n>.
298 static ssize_t
partition_handle_show(struct device
*dev
,
299 struct device_attribute
*attr
,
302 struct visor_device
*vdev
= to_visor_device(dev
);
303 u64 handle
= visorchannel_get_clientpartition(vdev
->visorchannel
);
305 return sprintf(buf
, "0x%llx\n", handle
);
307 static DEVICE_ATTR_RO(partition_handle
);
309 static ssize_t
partition_guid_show(struct device
*dev
,
310 struct device_attribute
*attr
,
313 struct visor_device
*vdev
= to_visor_device(dev
);
315 return sprintf(buf
, "{%pUb}\n", &vdev
->partition_guid
);
317 static DEVICE_ATTR_RO(partition_guid
);
319 static ssize_t
partition_name_show(struct device
*dev
,
320 struct device_attribute
*attr
,
323 struct visor_device
*vdev
= to_visor_device(dev
);
325 return sprintf(buf
, "%s\n", vdev
->name
);
327 static DEVICE_ATTR_RO(partition_name
);
329 static ssize_t
channel_addr_show(struct device
*dev
,
330 struct device_attribute
*attr
,
333 struct visor_device
*vdev
= to_visor_device(dev
);
334 u64 addr
= visorchannel_get_physaddr(vdev
->visorchannel
);
336 return sprintf(buf
, "0x%llx\n", addr
);
338 static DEVICE_ATTR_RO(channel_addr
);
340 static ssize_t
channel_bytes_show(struct device
*dev
,
341 struct device_attribute
*attr
,
344 struct visor_device
*vdev
= to_visor_device(dev
);
345 u64 nbytes
= visorchannel_get_nbytes(vdev
->visorchannel
);
347 return sprintf(buf
, "0x%llx\n", nbytes
);
349 static DEVICE_ATTR_RO(channel_bytes
);
351 static ssize_t
channel_id_show(struct device
*dev
,
352 struct device_attribute
*attr
,
355 struct visor_device
*vdev
= to_visor_device(dev
);
358 visorchannel_id(vdev
->visorchannel
, buf
);
364 static DEVICE_ATTR_RO(channel_id
);
366 static struct attribute
*visorbus_attrs
[] = {
367 &dev_attr_partition_handle
.attr
,
368 &dev_attr_partition_guid
.attr
,
369 &dev_attr_partition_name
.attr
,
370 &dev_attr_channel_addr
.attr
,
371 &dev_attr_channel_bytes
.attr
,
372 &dev_attr_channel_id
.attr
,
376 ATTRIBUTE_GROUPS(visorbus
);
379 * BUS debugfs entries
381 * define & implement display of debugfs attributes under
382 * /sys/kernel/debug/visorbus/visorbus<n>.
386 * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo
387 * and write it to a seq_file
388 * @devinfo: the struct visor_vbus_deviceinfo to format
389 * @seq: seq_file to write to
390 * @devix: the device index to be included in the output data, or -1 if no
391 * device index is to be included
393 * Reads @devInfo, and writes it in human-readable notation to @seq.
395 static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo
*devinfo
,
396 struct seq_file
*seq
, int devix
)
398 /* uninitialized vbus device entry */
399 if (!isprint(devinfo
->devtype
[0]))
403 seq_printf(seq
, "[%d]", devix
);
405 /* vbus device entry is for bus or chipset */
409 * Note: because the s-Par back-end is free to scribble in this area,
410 * we never assume '\0'-termination.
412 seq_printf(seq
, "%-*.*s ", (int)sizeof(devinfo
->devtype
),
413 (int)sizeof(devinfo
->devtype
), devinfo
->devtype
);
414 seq_printf(seq
, "%-*.*s ", (int)sizeof(devinfo
->drvname
),
415 (int)sizeof(devinfo
->drvname
), devinfo
->drvname
);
416 seq_printf(seq
, "%.*s\n", (int)sizeof(devinfo
->infostrs
),
420 static int client_bus_info_debugfs_show(struct seq_file
*seq
, void *v
)
424 struct visor_vbus_deviceinfo dev_info
;
425 struct visor_device
*vdev
= seq
->private;
426 struct visorchannel
*channel
= vdev
->visorchannel
;
432 "Client device / client driver info for %s partition (vbus #%u):\n",
433 ((vdev
->name
) ? (char *)(vdev
->name
) : ""),
434 vdev
->chipset_bus_no
);
436 if (visorchannel_read(channel
,
437 offsetof(struct visor_vbus_channel
, chp_info
),
438 &dev_info
, sizeof(dev_info
)) >= 0)
439 vbuschannel_print_devinfo(&dev_info
, seq
, -1);
440 if (visorchannel_read(channel
,
441 offsetof(struct visor_vbus_channel
, bus_info
),
442 &dev_info
, sizeof(dev_info
)) >= 0)
443 vbuschannel_print_devinfo(&dev_info
, seq
, -1);
445 off
= offsetof(struct visor_vbus_channel
, dev_info
);
446 while (off
+ sizeof(dev_info
) <= visorchannel_get_nbytes(channel
)) {
447 if (visorchannel_read(channel
, off
, &dev_info
,
448 sizeof(dev_info
)) >= 0)
449 vbuschannel_print_devinfo(&dev_info
, seq
, i
);
450 off
+= sizeof(dev_info
);
457 static int client_bus_info_debugfs_open(struct inode
*inode
, struct file
*file
)
459 return single_open(file
, client_bus_info_debugfs_show
,
463 static const struct file_operations client_bus_info_debugfs_fops
= {
464 .owner
= THIS_MODULE
,
465 .open
= client_bus_info_debugfs_open
,
468 .release
= single_release
,
471 static void dev_periodic_work(unsigned long __opaque
)
473 struct visor_device
*dev
= (struct visor_device
*)__opaque
;
474 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
476 drv
->channel_interrupt(dev
);
477 mod_timer(&dev
->timer
, jiffies
+ POLLJIFFIES_NORMALCHANNEL
);
480 static int dev_start_periodic_work(struct visor_device
*dev
)
482 if (dev
->being_removed
|| dev
->timer_active
)
484 /* now up by at least 2 */
485 get_device(&dev
->device
);
486 dev
->timer
.expires
= jiffies
+ POLLJIFFIES_NORMALCHANNEL
;
487 add_timer(&dev
->timer
);
488 dev
->timer_active
= true;
492 static void dev_stop_periodic_work(struct visor_device
*dev
)
494 if (!dev
->timer_active
)
496 del_timer_sync(&dev
->timer
);
497 dev
->timer_active
= false;
498 put_device(&dev
->device
);
502 * visordriver_remove_device() - handle visor device going away
503 * @xdev: struct device for the visor device being removed
505 * This is called when device_unregister() is called for each child device
506 * instance, to notify the appropriate visorbus function driver that the device
507 * is going away, and to decrease the reference count of the device.
509 * Return: 0 iff successful
511 static int visordriver_remove_device(struct device
*xdev
)
513 struct visor_device
*dev
;
514 struct visor_driver
*drv
;
516 dev
= to_visor_device(xdev
);
517 drv
= to_visor_driver(xdev
->driver
);
519 mutex_lock(&dev
->visordriver_callback_lock
);
520 dev
->being_removed
= true;
522 mutex_unlock(&dev
->visordriver_callback_lock
);
524 dev_stop_periodic_work(dev
);
525 put_device(&dev
->device
);
531 * visorbus_unregister_visor_driver() - unregisters the provided driver
532 * @drv: the driver to unregister
534 * A visor function driver calls this function to unregister the driver,
535 * i.e., within its module_exit function.
537 void visorbus_unregister_visor_driver(struct visor_driver
*drv
)
539 driver_unregister(&drv
->driver
);
541 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver
);
544 * visorbus_read_channel() - reads from the designated channel into
545 * the provided buffer
546 * @dev: the device whose channel is read from
547 * @offset: the offset into the channel at which reading starts
548 * @dest: the destination buffer that is written into from the channel
549 * @nbytes: the number of bytes to read from the channel
551 * If receiving a message, use the visorchannel_signalremove()
554 * Return: integer indicating success (zero) or failure (non-zero)
556 int visorbus_read_channel(struct visor_device
*dev
, unsigned long offset
,
557 void *dest
, unsigned long nbytes
)
559 return visorchannel_read(dev
->visorchannel
, offset
, dest
, nbytes
);
561 EXPORT_SYMBOL_GPL(visorbus_read_channel
);
564 * visorbus_write_channel() - writes the provided buffer into the designated
566 * @dev: the device whose channel is written to
567 * @offset: the offset into the channel at which writing starts
568 * @src: the source buffer that is written into the channel
569 * @nbytes: the number of bytes to write into the channel
571 * If sending a message, use the visorchannel_signalinsert()
574 * Return: integer indicating success (zero) or failure (non-zero)
576 int visorbus_write_channel(struct visor_device
*dev
, unsigned long offset
,
577 void *src
, unsigned long nbytes
)
579 return visorchannel_write(dev
->visorchannel
, offset
, src
, nbytes
);
581 EXPORT_SYMBOL_GPL(visorbus_write_channel
);
584 * visorbus_enable_channel_interrupts() - enables interrupts on the
586 * @dev: the device on which to enable interrupts
588 * Currently we don't yet have a real interrupt, so for now we just call the
589 * interrupt function periodically via a timer.
591 int visorbus_enable_channel_interrupts(struct visor_device
*dev
)
593 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
595 if (!drv
->channel_interrupt
) {
596 dev_err(&dev
->device
, "%s no interrupt function!\n", __func__
);
600 return dev_start_periodic_work(dev
);
602 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts
);
605 * visorbus_disable_channel_interrupts() - disables interrupts on the
607 * @dev: the device on which to disable interrupts
609 void visorbus_disable_channel_interrupts(struct visor_device
*dev
)
611 dev_stop_periodic_work(dev
);
613 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts
);
616 * create_visor_device() - create visor device as a result of receiving the
617 * controlvm device_create message for a new device
618 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
619 * for chipset_bus_no and chipset_dev_no, that will be initialized
621 * This is how everything starts from the device end.
622 * This function is called when a channel first appears via a ControlVM
623 * message. In response, this function allocates a visor_device to
624 * correspond to the new channel, and attempts to connect it the appropriate
625 * driver. If the appropriate driver is found, the visor_driver.probe()
626 * function for that driver will be called, and will be passed the new
627 * visor_device that we just created.
629 * It's ok if the appropriate driver is not yet loaded, because in that case
630 * the new device struct will just stick around in the bus' list of devices.
631 * When the appropriate driver calls visorbus_register_visor_driver(), the
632 * visor_driver.probe() for the new driver will be called with the new
635 * Return: 0 if successful, otherwise the negative value returned by
636 * device_add() indicating the reason for failure
638 int create_visor_device(struct visor_device
*dev
)
641 u32 chipset_bus_no
= dev
->chipset_bus_no
;
642 u32 chipset_dev_no
= dev
->chipset_dev_no
;
644 mutex_init(&dev
->visordriver_callback_lock
);
645 dev
->device
.bus
= &visorbus_type
;
646 dev
->device
.groups
= channel_groups
;
647 device_initialize(&dev
->device
);
648 dev
->device
.release
= visorbus_release_device
;
649 /* keep a reference just for us (now 2) */
650 get_device(&dev
->device
);
651 setup_timer(&dev
->timer
, dev_periodic_work
, (unsigned long)dev
);
654 * bus_id must be a unique name with respect to this bus TYPE
655 * (NOT bus instance). That's why we need to include the bus
656 * number within the name.
658 err
= dev_set_name(&dev
->device
, "vbus%u:dev%u",
659 chipset_bus_no
, chipset_dev_no
);
664 * device_add does this:
665 * bus_add_device(dev)
666 * ->device_attach(dev)
667 * ->for each driver drv registered on the bus that dev is on
668 * if (dev.drv) ** device already has a driver **
669 * ** not sure we could ever get here... **
671 * if (bus.match(dev,drv)) [visorbus_match]
673 * if (!drv.probe(dev)) [visordriver_probe_device]
676 * Note that device_add does NOT fail if no driver failed to
677 * claim the device. The device will be linked onto
678 * bus_type.klist_devices regardless (use bus_for_each_dev).
680 err
= device_add(&dev
->device
);
684 list_add_tail(&dev
->list_all
, &list_all_device_instances
);
685 dev
->state
.created
= 1;
686 visorbus_response(dev
, err
, CONTROLVM_DEVICE_CREATE
);
687 /* success: reference kept via unmatched get_device() */
691 put_device(&dev
->device
);
692 dev_err(&dev
->device
, "Creating visor device failed. %d\n", err
);
696 void remove_visor_device(struct visor_device
*dev
)
698 list_del(&dev
->list_all
);
699 put_device(&dev
->device
);
700 device_unregister(&dev
->device
);
701 visorbus_response(dev
, 0, CONTROLVM_DEVICE_DESTROY
);
704 static int get_vbus_header_info(struct visorchannel
*chan
,
705 struct visor_vbus_headerinfo
*hdr_info
)
709 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
, 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 drv
= to_visor_driver(dev
->device
.driver
);
1165 if (dev
->pausing
|| dev
->resuming
)
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
);