3 * Copyright � 2010 - 2015 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
17 #include <linux/debugfs.h>
18 #include <linux/uuid.h>
21 #include "visorbus_private.h"
22 #include "vmcallinterface.h"
24 #define MYDRVNAME "visorbus"
26 /* module parameters */
27 static int visorbus_forcematch
;
28 static int visorbus_forcenomatch
;
30 /* Display string that is guaranteed to be no longer the 99 characters*/
33 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
34 #define POLLJIFFIES_NORMALCHANNEL 10
36 static int busreg_rc
= -ENODEV
; /* stores the result from bus registration */
37 static struct dentry
*visorbus_debugfs_dir
;
40 * DEVICE type attributes
42 * The modalias file will contain the guid of the device.
44 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
47 struct visor_device
*vdev
;
50 vdev
= to_visor_device(dev
);
51 guid
= visorchannel_get_uuid(vdev
->visorchannel
);
52 return sprintf(buf
, "visorbus:%pUl\n", &guid
);
54 static DEVICE_ATTR_RO(modalias
);
56 static struct attribute
*visorbus_dev_attrs
[] = {
57 &dev_attr_modalias
.attr
,
61 /* sysfs example for bridge-only sysfs files using device_type's */
62 static const struct attribute_group visorbus_dev_group
= {
63 .attrs
= visorbus_dev_attrs
,
66 static const struct attribute_group
*visorbus_dev_groups
[] = {
71 /* filled in with info about parent chipset driver when we register with it */
72 static struct ultra_vbus_deviceinfo chipset_driverinfo
;
73 /* filled in with info about this driver, wrt it servicing client busses */
74 static struct ultra_vbus_deviceinfo clientbus_driverinfo
;
76 /* list of visor_device structs, linked via .list_all */
77 static LIST_HEAD(list_all_bus_instances
);
78 /* list of visor_device structs, linked via .list_all */
79 static LIST_HEAD(list_all_device_instances
);
82 visorbus_uevent(struct device
*xdev
, struct kobj_uevent_env
*env
)
84 struct visor_device
*dev
;
87 dev
= to_visor_device(xdev
);
88 guid
= visorchannel_get_uuid(dev
->visorchannel
);
90 if (add_uevent_var(env
, "MODALIAS=visorbus:%pUl", &guid
))
96 * visorbus_match() - called automatically upon adding a visor_device
97 * (device_add), or adding a visor_driver
98 * (visorbus_register_visor_driver)
99 * @xdev: struct device for the device being matched
100 * @xdrv: struct device_driver for driver to match device against
102 * Return: 1 iff the provided driver can control the specified device
105 visorbus_match(struct device
*xdev
, struct device_driver
*xdrv
)
107 uuid_le channel_type
;
109 struct visor_device
*dev
;
110 struct visor_driver
*drv
;
112 dev
= to_visor_device(xdev
);
113 drv
= to_visor_driver(xdrv
);
114 channel_type
= visorchannel_get_uuid(dev
->visorchannel
);
116 if (visorbus_forcematch
)
118 if (visorbus_forcenomatch
)
120 if (!drv
->channel_types
)
124 (uuid_le_cmp(drv
->channel_types
[i
].guid
, NULL_UUID_LE
) != 0) ||
125 (drv
->channel_types
[i
].name
);
127 if (uuid_le_cmp(drv
->channel_types
[i
].guid
,
135 * This describes the TYPE of bus.
136 * (Don't confuse this with an INSTANCE of the bus.)
138 struct bus_type visorbus_type
= {
140 .match
= visorbus_match
,
141 .uevent
= visorbus_uevent
,
142 .dev_groups
= visorbus_dev_groups
,
146 * visorbus_releae_busdevice() - called when device_unregister() is called for
147 * the bus device instance, after all other tasks
148 * involved with destroying the dev are complete
149 * @xdev: struct device for the bus being released
152 visorbus_release_busdevice(struct device
*xdev
)
154 struct visor_device
*dev
= dev_get_drvdata(xdev
);
156 debugfs_remove(dev
->debugfs_client_bus_info
);
157 debugfs_remove_recursive(dev
->debugfs_dir
);
162 * visorbus_release_device() - called when device_unregister() is called for
163 * each child device instance
164 * @xdev: struct device for the visor device being released
167 visorbus_release_device(struct device
*xdev
)
169 struct visor_device
*dev
= to_visor_device(xdev
);
171 if (dev
->visorchannel
) {
172 visorchannel_destroy(dev
->visorchannel
);
173 dev
->visorchannel
= NULL
;
179 * begin implementation of specific channel attributes to appear under
180 * /sys/bus/visorbus<x>/dev<y>/channel
183 static ssize_t
physaddr_show(struct device
*dev
, struct device_attribute
*attr
,
186 struct visor_device
*vdev
= to_visor_device(dev
);
188 if (!vdev
->visorchannel
)
190 return sprintf(buf
, "0x%llx\n",
191 visorchannel_get_physaddr(vdev
->visorchannel
));
193 static DEVICE_ATTR_RO(physaddr
);
195 static ssize_t
nbytes_show(struct device
*dev
, struct device_attribute
*attr
,
198 struct visor_device
*vdev
= to_visor_device(dev
);
200 if (!vdev
->visorchannel
)
202 return sprintf(buf
, "0x%lx\n",
203 visorchannel_get_nbytes(vdev
->visorchannel
));
205 static DEVICE_ATTR_RO(nbytes
);
207 static ssize_t
clientpartition_show(struct device
*dev
,
208 struct device_attribute
*attr
, char *buf
)
210 struct visor_device
*vdev
= to_visor_device(dev
);
212 if (!vdev
->visorchannel
)
214 return sprintf(buf
, "0x%llx\n",
215 visorchannel_get_clientpartition(vdev
->visorchannel
));
217 static DEVICE_ATTR_RO(clientpartition
);
219 static ssize_t
typeguid_show(struct device
*dev
, struct device_attribute
*attr
,
222 struct visor_device
*vdev
= to_visor_device(dev
);
223 char typeid[LINESIZE
];
225 if (!vdev
->visorchannel
)
227 return sprintf(buf
, "%s\n",
228 visorchannel_id(vdev
->visorchannel
, typeid));
230 static DEVICE_ATTR_RO(typeguid
);
232 static ssize_t
zoneguid_show(struct device
*dev
, struct device_attribute
*attr
,
235 struct visor_device
*vdev
= to_visor_device(dev
);
236 char zoneid
[LINESIZE
];
238 if (!vdev
->visorchannel
)
240 return sprintf(buf
, "%s\n",
241 visorchannel_zoneid(vdev
->visorchannel
, zoneid
));
243 static DEVICE_ATTR_RO(zoneguid
);
245 static ssize_t
typename_show(struct device
*dev
, struct device_attribute
*attr
,
248 struct visor_device
*vdev
= to_visor_device(dev
);
250 struct bus_type
*xbus
= dev
->bus
;
251 struct device_driver
*xdrv
= dev
->driver
;
252 struct visor_driver
*drv
= NULL
;
254 if (!vdev
->visorchannel
|| !xbus
|| !xdrv
)
256 i
= xbus
->match(dev
, xdrv
);
259 drv
= to_visor_driver(xdrv
);
260 return sprintf(buf
, "%s\n", drv
->channel_types
[i
- 1].name
);
262 static DEVICE_ATTR_RO(typename
);
264 static struct attribute
*channel_attrs
[] = {
265 &dev_attr_physaddr
.attr
,
266 &dev_attr_nbytes
.attr
,
267 &dev_attr_clientpartition
.attr
,
268 &dev_attr_typeguid
.attr
,
269 &dev_attr_zoneguid
.attr
,
270 &dev_attr_typename
.attr
,
274 static struct attribute_group channel_attr_grp
= {
276 .attrs
= channel_attrs
,
279 static const struct attribute_group
*visorbus_channel_groups
[] = {
284 /* end implementation of specific channel attributes */
287 * BUS instance attributes
289 * define & implement display of bus attributes under
290 * /sys/bus/visorbus/devices/visorbus<n>.
293 static ssize_t
partition_handle_show(struct device
*dev
,
294 struct device_attribute
*attr
,
296 struct visor_device
*vdev
= to_visor_device(dev
);
297 u64 handle
= visorchannel_get_clientpartition(vdev
->visorchannel
);
299 return sprintf(buf
, "0x%llx\n", handle
);
301 static DEVICE_ATTR_RO(partition_handle
);
303 static ssize_t
partition_guid_show(struct device
*dev
,
304 struct device_attribute
*attr
,
306 struct visor_device
*vdev
= to_visor_device(dev
);
308 return sprintf(buf
, "{%pUb}\n", &vdev
->partition_uuid
);
310 static DEVICE_ATTR_RO(partition_guid
);
312 static ssize_t
partition_name_show(struct device
*dev
,
313 struct device_attribute
*attr
,
315 struct visor_device
*vdev
= to_visor_device(dev
);
317 return sprintf(buf
, "%s\n", vdev
->name
);
319 static DEVICE_ATTR_RO(partition_name
);
321 static ssize_t
channel_addr_show(struct device
*dev
,
322 struct device_attribute
*attr
,
324 struct visor_device
*vdev
= to_visor_device(dev
);
325 u64 addr
= visorchannel_get_physaddr(vdev
->visorchannel
);
327 return sprintf(buf
, "0x%llx\n", addr
);
329 static DEVICE_ATTR_RO(channel_addr
);
331 static ssize_t
channel_bytes_show(struct device
*dev
,
332 struct device_attribute
*attr
,
334 struct visor_device
*vdev
= to_visor_device(dev
);
335 u64 nbytes
= visorchannel_get_nbytes(vdev
->visorchannel
);
337 return sprintf(buf
, "0x%llx\n", nbytes
);
339 static DEVICE_ATTR_RO(channel_bytes
);
341 static ssize_t
channel_id_show(struct device
*dev
,
342 struct device_attribute
*attr
,
344 struct visor_device
*vdev
= to_visor_device(dev
);
347 if (vdev
->visorchannel
) {
348 visorchannel_id(vdev
->visorchannel
, buf
);
354 static DEVICE_ATTR_RO(channel_id
);
356 static struct attribute
*dev_attrs
[] = {
357 &dev_attr_partition_handle
.attr
,
358 &dev_attr_partition_guid
.attr
,
359 &dev_attr_partition_name
.attr
,
360 &dev_attr_channel_addr
.attr
,
361 &dev_attr_channel_bytes
.attr
,
362 &dev_attr_channel_id
.attr
,
366 static struct attribute_group dev_attr_grp
= {
370 static const struct attribute_group
*visorbus_groups
[] = {
376 * BUS debugfs entries
378 * define & implement display of debugfs attributes under
379 * /sys/kernel/debug/visorbus/visorbus<n>.
382 static int client_bus_info_debugfs_show(struct seq_file
*seq
, void *v
)
384 struct visor_device
*vdev
= seq
->private;
385 struct visorchannel
*channel
= vdev
->visorchannel
;
389 struct ultra_vbus_deviceinfo dev_info
;
395 "Client device / client driver info for %s partition (vbus #%u):\n",
396 ((vdev
->name
) ? (char *)(vdev
->name
) : ""),
397 vdev
->chipset_bus_no
);
398 if (visorchannel_read(channel
,
399 offsetof(struct spar_vbus_channel_protocol
,
401 &dev_info
, sizeof(dev_info
)) >= 0)
402 vbuschannel_print_devinfo(&dev_info
, seq
, -1);
403 if (visorchannel_read(channel
,
404 offsetof(struct spar_vbus_channel_protocol
,
406 &dev_info
, sizeof(dev_info
)) >= 0)
407 vbuschannel_print_devinfo(&dev_info
, seq
, -1);
408 off
= offsetof(struct spar_vbus_channel_protocol
, dev_info
);
410 while (off
+ sizeof(dev_info
) <= visorchannel_get_nbytes(channel
)) {
411 if (visorchannel_read(channel
, off
, &dev_info
,
412 sizeof(dev_info
)) >= 0)
413 vbuschannel_print_devinfo(&dev_info
, seq
, i
);
414 off
+= sizeof(dev_info
);
421 static int client_bus_info_debugfs_open(struct inode
*inode
, struct file
*file
)
423 return single_open(file
, client_bus_info_debugfs_show
,
427 static const struct file_operations client_bus_info_debugfs_fops
= {
428 .owner
= THIS_MODULE
,
429 .open
= client_bus_info_debugfs_open
,
432 .release
= single_release
,
436 dev_periodic_work(unsigned long __opaque
)
438 struct visor_device
*dev
= (struct visor_device
*)__opaque
;
439 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
441 drv
->channel_interrupt(dev
);
442 mod_timer(&dev
->timer
, jiffies
+ POLLJIFFIES_NORMALCHANNEL
);
446 dev_start_periodic_work(struct visor_device
*dev
)
448 if (dev
->being_removed
|| dev
->timer_active
)
450 /* now up by at least 2 */
451 get_device(&dev
->device
);
452 dev
->timer
.expires
= jiffies
+ POLLJIFFIES_NORMALCHANNEL
;
453 add_timer(&dev
->timer
);
454 dev
->timer_active
= true;
458 dev_stop_periodic_work(struct visor_device
*dev
)
460 if (!dev
->timer_active
)
462 del_timer_sync(&dev
->timer
);
463 dev
->timer_active
= false;
464 put_device(&dev
->device
);
468 * visordriver_remove_device() - handle visor device going away
469 * @xdev: struct device for the visor device being removed
471 * This is called when device_unregister() is called for each child device
472 * instance, to notify the appropriate visorbus function driver that the device
473 * is going away, and to decrease the reference count of the device.
475 * Return: 0 iff successful
478 visordriver_remove_device(struct device
*xdev
)
480 struct visor_device
*dev
;
481 struct visor_driver
*drv
;
483 dev
= to_visor_device(xdev
);
484 drv
= to_visor_driver(xdev
->driver
);
485 mutex_lock(&dev
->visordriver_callback_lock
);
486 dev
->being_removed
= true;
489 mutex_unlock(&dev
->visordriver_callback_lock
);
490 dev_stop_periodic_work(dev
);
492 put_device(&dev
->device
);
497 * visorbus_unregister_visor_driver() - unregisters the provided driver
498 * @drv: the driver to unregister
500 * A visor function driver calls this function to unregister the driver,
501 * i.e., within its module_exit function.
504 visorbus_unregister_visor_driver(struct visor_driver
*drv
)
506 driver_unregister(&drv
->driver
);
508 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver
);
511 * visorbus_read_channel() - reads from the designated channel into
512 * the provided buffer
513 * @dev: the device whose channel is read from
514 * @offset: the offset into the channel at which reading starts
515 * @dest: the destination buffer that is written into from the channel
516 * @nbytes: the number of bytes to read from the channel
518 * If receiving a message, use the visorchannel_signalremove()
521 * Return: integer indicating success (zero) or failure (non-zero)
524 visorbus_read_channel(struct visor_device
*dev
, unsigned long offset
,
525 void *dest
, unsigned long nbytes
)
527 return visorchannel_read(dev
->visorchannel
, offset
, dest
, nbytes
);
529 EXPORT_SYMBOL_GPL(visorbus_read_channel
);
532 * visorbus_write_channel() - writes the provided buffer into the designated
534 * @dev: the device whose channel is written to
535 * @offset: the offset into the channel at which writing starts
536 * @src: the source buffer that is written into the channel
537 * @nbytes: the number of bytes to write into the channel
539 * If sending a message, use the visorchannel_signalinsert()
542 * Return: integer indicating success (zero) or failure (non-zero)
545 visorbus_write_channel(struct visor_device
*dev
, unsigned long offset
,
546 void *src
, unsigned long nbytes
)
548 return visorchannel_write(dev
->visorchannel
, offset
, src
, nbytes
);
550 EXPORT_SYMBOL_GPL(visorbus_write_channel
);
553 * visorbus_enable_channel_interrupts() - enables interrupts on the
555 * @dev: the device on which to enable interrupts
557 * Currently we don't yet have a real interrupt, so for now we just call the
558 * interrupt function periodically via a timer.
561 visorbus_enable_channel_interrupts(struct visor_device
*dev
)
563 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
565 if (!drv
->channel_interrupt
) {
566 dev_err(&dev
->device
, "%s no interrupt function!\n", __func__
);
570 dev_start_periodic_work(dev
);
572 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts
);
575 * visorbus_disable_channel_interrupts() - disables interrupts on the
577 * @dev: the device on which to disable interrupts
580 visorbus_disable_channel_interrupts(struct visor_device
*dev
)
582 dev_stop_periodic_work(dev
);
584 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts
);
587 * create_visor_device() - create visor device as a result of receiving the
588 * controlvm device_create message for a new device
589 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
590 * for chipset_bus_no and chipset_dev_no, that will be initialized
592 * This is how everything starts from the device end.
593 * This function is called when a channel first appears via a ControlVM
594 * message. In response, this function allocates a visor_device to
595 * correspond to the new channel, and attempts to connect it the appropriate
596 * driver. If the appropriate driver is found, the visor_driver.probe()
597 * function for that driver will be called, and will be passed the new
598 * visor_device that we just created.
600 * It's ok if the appropriate driver is not yet loaded, because in that case
601 * the new device struct will just stick around in the bus' list of devices.
602 * When the appropriate driver calls visorbus_register_visor_driver(), the
603 * visor_driver.probe() for the new driver will be called with the new
606 * Return: 0 if successful, otherwise the negative value returned by
607 * device_add() indicating the reason for failure
610 create_visor_device(struct visor_device
*dev
)
613 u32 chipset_bus_no
= dev
->chipset_bus_no
;
614 u32 chipset_dev_no
= dev
->chipset_dev_no
;
616 POSTCODE_LINUX(DEVICE_CREATE_ENTRY_PC
, chipset_dev_no
, chipset_bus_no
,
617 DIAG_SEVERITY_PRINT
);
619 mutex_init(&dev
->visordriver_callback_lock
);
620 dev
->device
.bus
= &visorbus_type
;
621 dev
->device
.groups
= visorbus_channel_groups
;
622 device_initialize(&dev
->device
);
623 dev
->device
.release
= visorbus_release_device
;
624 /* keep a reference just for us (now 2) */
625 get_device(&dev
->device
);
626 setup_timer(&dev
->timer
, dev_periodic_work
, (unsigned long)dev
);
629 * bus_id must be a unique name with respect to this bus TYPE
630 * (NOT bus instance). That's why we need to include the bus
631 * number within the name.
633 dev_set_name(&dev
->device
, "vbus%u:dev%u",
634 chipset_bus_no
, chipset_dev_no
);
637 * device_add does this:
638 * bus_add_device(dev)
639 * ->device_attach(dev)
640 * ->for each driver drv registered on the bus that dev is on
641 * if (dev.drv) ** device already has a driver **
642 * ** not sure we could ever get here... **
644 * if (bus.match(dev,drv)) [visorbus_match]
646 * if (!drv.probe(dev)) [visordriver_probe_device]
649 * Note that device_add does NOT fail if no driver failed to
650 * claim the device. The device will be linked onto
651 * bus_type.klist_devices regardless (use bus_for_each_dev).
653 err
= device_add(&dev
->device
);
655 POSTCODE_LINUX(DEVICE_ADD_PC
, 0, chipset_bus_no
,
660 list_add_tail(&dev
->list_all
, &list_all_device_instances
);
661 return 0; /* success: reference kept via unmatched get_device() */
664 put_device(&dev
->device
);
669 remove_visor_device(struct visor_device
*dev
)
671 list_del(&dev
->list_all
);
672 put_device(&dev
->device
);
673 device_unregister(&dev
->device
);
677 get_vbus_header_info(struct visorchannel
*chan
,
678 struct spar_vbus_headerinfo
*hdr_info
)
680 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan
)))
683 if (visorchannel_read(chan
, sizeof(struct channel_header
), hdr_info
,
684 sizeof(*hdr_info
)) < 0) {
687 if (hdr_info
->struct_bytes
< sizeof(struct spar_vbus_headerinfo
))
690 if (hdr_info
->device_info_struct_bytes
<
691 sizeof(struct ultra_vbus_deviceinfo
)) {
698 * write_vbus_chp_info() - write the contents of <info> to the struct
699 * spar_vbus_channel_protocol.chp_info
700 * @chan: indentifies the s-Par channel that will be updated
701 * @hdr_info: used to find appropriate channel offset to write data
702 * @info: contains the information to write
704 * Writes chipset info into the channel memory to be used for diagnostic
707 * Returns no value since this is debug information and not needed for
708 * device functionality.
711 write_vbus_chp_info(struct visorchannel
*chan
,
712 struct spar_vbus_headerinfo
*hdr_info
,
713 struct ultra_vbus_deviceinfo
*info
)
715 int off
= sizeof(struct channel_header
) + hdr_info
->chp_info_offset
;
717 if (hdr_info
->chp_info_offset
== 0)
720 visorchannel_write(chan
, off
, info
, sizeof(*info
));
724 * write_vbus_bus_info() - write the contents of <info> to the struct
725 * spar_vbus_channel_protocol.bus_info
726 * @chan: indentifies the s-Par channel that will be updated
727 * @hdr_info: used to find appropriate channel offset to write data
728 * @info: contains the information to write
730 * Writes bus info into the channel memory to be used for diagnostic
733 * Returns no value since this is debug information and not needed for
734 * device functionality.
737 write_vbus_bus_info(struct visorchannel
*chan
,
738 struct spar_vbus_headerinfo
*hdr_info
,
739 struct ultra_vbus_deviceinfo
*info
)
741 int off
= sizeof(struct channel_header
) + hdr_info
->bus_info_offset
;
743 if (hdr_info
->bus_info_offset
== 0)
746 visorchannel_write(chan
, off
, info
, sizeof(*info
));
750 * write_vbus_dev_info() - write the contents of <info> to the struct
751 * spar_vbus_channel_protocol.dev_info[<devix>]
752 * @chan: indentifies the s-Par channel that will be updated
753 * @hdr_info: used to find appropriate channel offset to write data
754 * @info: contains the information to write
755 * @devix: the relative device number (0..n-1) of the device on the bus
757 * Writes device info into the channel memory to be used for diagnostic
760 * Returns no value since this is debug information and not needed for
761 * device functionality.
764 write_vbus_dev_info(struct visorchannel
*chan
,
765 struct spar_vbus_headerinfo
*hdr_info
,
766 struct ultra_vbus_deviceinfo
*info
, unsigned int devix
)
769 (sizeof(struct channel_header
) + hdr_info
->dev_info_offset
) +
770 (hdr_info
->device_info_struct_bytes
* devix
);
772 if (hdr_info
->dev_info_offset
== 0)
775 visorchannel_write(chan
, off
, info
, sizeof(*info
));
779 * fix_vbus_dev_info() - for a child device just created on a client bus, fill
780 * in information about the driver that is controlling
781 * this device into the the appropriate slot within the
782 * vbus channel of the bus instance
783 * @visordev: struct visor_device for the desired device
786 fix_vbus_dev_info(struct visor_device
*visordev
)
789 struct visor_device
*bdev
;
790 struct visor_driver
*visordrv
;
791 u32 bus_no
= visordev
->chipset_bus_no
;
792 u32 dev_no
= visordev
->chipset_dev_no
;
793 struct ultra_vbus_deviceinfo dev_info
;
794 const char *chan_type_name
= NULL
;
795 struct spar_vbus_headerinfo
*hdr_info
;
797 if (!visordev
->device
.driver
)
800 bdev
= visorbus_get_device_by_id(bus_no
, BUS_ROOT_DEVICE
, NULL
);
803 hdr_info
= (struct spar_vbus_headerinfo
*)bdev
->vbus_hdr_info
;
806 visordrv
= to_visor_driver(visordev
->device
.driver
);
809 * Within the list of device types (by GUID) that the driver
810 * says it supports, find out which one of those types matches
811 * the type of this device, so that we can include the device
814 for (i
= 0; visordrv
->channel_types
[i
].name
; i
++) {
815 if (memcmp(&visordrv
->channel_types
[i
].guid
,
816 &visordev
->channel_type_guid
,
817 sizeof(visordrv
->channel_types
[i
].guid
)) == 0) {
818 chan_type_name
= visordrv
->channel_types
[i
].name
;
823 bus_device_info_init(&dev_info
, chan_type_name
, visordrv
->name
);
824 write_vbus_dev_info(bdev
->visorchannel
, hdr_info
, &dev_info
, dev_no
);
827 * Re-write bus+chipset info, because it is possible that this
828 * was previously written by our evil counterpart, virtpci.
830 write_vbus_chp_info(bdev
->visorchannel
, hdr_info
, &chipset_driverinfo
);
831 write_vbus_bus_info(bdev
->visorchannel
, hdr_info
,
832 &clientbus_driverinfo
);
836 * visordriver_probe_device() - handle new visor device coming online
837 * @xdev: struct device for the visor device being probed
839 * This is called automatically upon adding a visor_device (device_add), or
840 * adding a visor_driver (visorbus_register_visor_driver), but only after
841 * visorbus_match() has returned 1 to indicate a successful match between
844 * If successful, a reference to the device will be held onto via get_device().
846 * Return: 0 if successful, meaning the function driver's probe() function
847 * was successful with this device, otherwise a negative errno
848 * value indicating failure reason
851 visordriver_probe_device(struct device
*xdev
)
854 struct visor_driver
*drv
;
855 struct visor_device
*dev
;
857 drv
= to_visor_driver(xdev
->driver
);
858 dev
= to_visor_device(xdev
);
863 mutex_lock(&dev
->visordriver_callback_lock
);
864 dev
->being_removed
= false;
866 res
= drv
->probe(dev
);
868 /* success: reference kept via unmatched get_device() */
869 get_device(&dev
->device
);
870 fix_vbus_dev_info(dev
);
873 mutex_unlock(&dev
->visordriver_callback_lock
);
878 * visorbus_register_visor_driver() - registers the provided visor driver
879 * for handling one or more visor device
880 * types (channel_types)
881 * @drv: the driver to register
883 * A visor function driver calls this function to register
884 * the driver. The caller MUST fill in the following fields within the
886 * name, version, owner, channel_types, probe, remove
888 * Here's how the whole Linux bus / driver / device model works.
890 * At system start-up, the visorbus kernel module is loaded, which registers
891 * visorbus_type as a bus type, using bus_register().
893 * All kernel modules that support particular device types on a
894 * visorbus bus are loaded. Each of these kernel modules calls
895 * visorbus_register_visor_driver() in their init functions, passing a
896 * visor_driver struct. visorbus_register_visor_driver() in turn calls
897 * register_driver(&visor_driver.driver). This .driver member is
898 * initialized with generic methods (like probe), whose sole responsibility
899 * is to act as a broker for the real methods, which are within the
900 * visor_driver struct. (This is the way the subclass behavior is
901 * implemented, since visor_driver is essentially a subclass of the
902 * generic driver.) Whenever a driver_register() happens, core bus code in
903 * the kernel does (see device_attach() in drivers/base/dd.c):
905 * for each dev associated with the bus (the bus that driver is on) that
906 * does not yet have a driver
907 * if bus.match(dev,newdriver) == yes_matched ** .match specified
908 * ** during bus_register().
909 * newdriver.probe(dev) ** for visor drivers, this will call
910 * ** the generic driver.probe implemented in visorbus.c,
911 * ** which in turn calls the probe specified within the
912 * ** struct visor_driver (which was specified by the
913 * ** actual device driver as part of
914 * ** visorbus_register_visor_driver()).
916 * The above dance also happens when a new device appears.
917 * So the question is, how are devices created within the system?
918 * Basically, just call device_add(dev). See pci_bus_add_devices().
919 * pci_scan_device() shows an example of how to build a device struct. It
920 * returns the newly-created struct to pci_scan_single_device(), who adds it
921 * to the list of devices at PCIBUS.devices. That list of devices is what
922 * is traversed by pci_bus_add_devices().
924 * Return: integer indicating success (zero) or failure (non-zero)
926 int visorbus_register_visor_driver(struct visor_driver
*drv
)
931 return -ENODEV
; /*can't register on a nonexistent bus*/
933 drv
->driver
.name
= drv
->name
;
934 drv
->driver
.bus
= &visorbus_type
;
935 drv
->driver
.probe
= visordriver_probe_device
;
936 drv
->driver
.remove
= visordriver_remove_device
;
937 drv
->driver
.owner
= drv
->owner
;
940 * driver_register does this:
941 * bus_add_driver(drv)
942 * ->if (drv.bus) ** (bus_type) **
944 * for each dev with bus type of drv.bus
945 * if (!dev.drv) ** no driver assigned yet **
946 * if (bus.match(dev,drv)) [visorbus_match]
948 * if (!drv.probe(dev)) [visordriver_probe_device]
952 rc
= driver_register(&drv
->driver
);
954 driver_unregister(&drv
->driver
);
957 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver
);
960 * create_bus_instance() - create a device instance for the visor bus itself
961 * @dev: struct visor_device indicating the bus instance
963 * Return: 0 for success, otherwise negative errno value indicating reason for
967 create_bus_instance(struct visor_device
*dev
)
969 int id
= dev
->chipset_bus_no
;
971 struct spar_vbus_headerinfo
*hdr_info
;
973 POSTCODE_LINUX(BUS_CREATE_ENTRY_PC
, 0, 0, DIAG_SEVERITY_PRINT
);
975 hdr_info
= kzalloc(sizeof(*hdr_info
), GFP_KERNEL
);
979 dev_set_name(&dev
->device
, "visorbus%d", id
);
980 dev
->device
.bus
= &visorbus_type
;
981 dev
->device
.groups
= visorbus_groups
;
982 dev
->device
.release
= visorbus_release_busdevice
;
984 dev
->debugfs_dir
= debugfs_create_dir(dev_name(&dev
->device
),
985 visorbus_debugfs_dir
);
986 if (!dev
->debugfs_dir
) {
990 dev
->debugfs_client_bus_info
=
991 debugfs_create_file("client_bus_info", 0440,
992 dev
->debugfs_dir
, dev
,
993 &client_bus_info_debugfs_fops
);
994 if (!dev
->debugfs_client_bus_info
) {
996 goto err_debugfs_dir
;
999 if (device_register(&dev
->device
) < 0) {
1000 POSTCODE_LINUX(DEVICE_CREATE_FAILURE_PC
, 0, id
,
1003 goto err_debugfs_created
;
1006 if (get_vbus_header_info(dev
->visorchannel
, hdr_info
) >= 0) {
1007 dev
->vbus_hdr_info
= (void *)hdr_info
;
1008 write_vbus_chp_info(dev
->visorchannel
, hdr_info
,
1009 &chipset_driverinfo
);
1010 write_vbus_bus_info(dev
->visorchannel
, hdr_info
,
1011 &clientbus_driverinfo
);
1015 list_add_tail(&dev
->list_all
, &list_all_bus_instances
);
1016 dev_set_drvdata(&dev
->device
, dev
);
1019 err_debugfs_created
:
1020 debugfs_remove(dev
->debugfs_client_bus_info
);
1023 debugfs_remove_recursive(dev
->debugfs_dir
);
1031 * remove_bus_instance() - remove a device instance for the visor bus itself
1032 * @dev: struct visor_device indentifying the bus to remove
1035 remove_bus_instance(struct visor_device
*dev
)
1038 * Note that this will result in the release method for
1039 * dev->dev being called, which will call
1040 * visorbus_release_busdevice(). This has something to do with
1041 * the put_device() done in device_unregister(), but I have never
1042 * successfully been able to trace thru the code to see where/how
1043 * release() gets called. But I know it does.
1045 if (dev
->visorchannel
) {
1046 visorchannel_destroy(dev
->visorchannel
);
1047 dev
->visorchannel
= NULL
;
1049 kfree(dev
->vbus_hdr_info
);
1050 list_del(&dev
->list_all
);
1051 device_unregister(&dev
->device
);
1055 * create_bus_type() - create and register the one-and-only one instance of
1056 * the visor bus type (visorbus_type)
1057 * Return: 0 for success, otherwise negative errno value returned by
1058 * bus_register() indicating the reason for failure
1061 create_bus_type(void)
1063 busreg_rc
= bus_register(&visorbus_type
);
1068 * remove_bus_type() - remove the one-and-only one instance of the visor bus
1069 * type (visorbus_type)
1072 remove_bus_type(void)
1074 bus_unregister(&visorbus_type
);
1078 * remove_all_visor_devices() - remove all child visor bus device instances
1081 remove_all_visor_devices(void)
1083 struct list_head
*listentry
, *listtmp
;
1085 list_for_each_safe(listentry
, listtmp
, &list_all_device_instances
) {
1086 struct visor_device
*dev
= list_entry(listentry
,
1087 struct visor_device
,
1089 remove_visor_device(dev
);
1094 chipset_bus_create(struct visor_device
*dev
)
1097 u32 bus_no
= dev
->chipset_bus_no
;
1099 POSTCODE_LINUX(BUS_CREATE_ENTRY_PC
, 0, bus_no
, DIAG_SEVERITY_PRINT
);
1100 rc
= create_bus_instance(dev
);
1101 POSTCODE_LINUX(BUS_CREATE_EXIT_PC
, 0, bus_no
, DIAG_SEVERITY_PRINT
);
1104 POSTCODE_LINUX(BUS_CREATE_FAILURE_PC
, 0, bus_no
,
1107 POSTCODE_LINUX(CHIPSET_INIT_SUCCESS_PC
, 0, bus_no
,
1108 DIAG_SEVERITY_PRINT
);
1110 bus_create_response(dev
, rc
);
1114 chipset_bus_destroy(struct visor_device
*dev
)
1116 remove_bus_instance(dev
);
1117 bus_destroy_response(dev
, 0);
1121 chipset_device_create(struct visor_device
*dev_info
)
1124 u32 bus_no
= dev_info
->chipset_bus_no
;
1125 u32 dev_no
= dev_info
->chipset_dev_no
;
1127 POSTCODE_LINUX(DEVICE_CREATE_ENTRY_PC
, dev_no
, bus_no
,
1128 DIAG_SEVERITY_PRINT
);
1130 rc
= create_visor_device(dev_info
);
1131 device_create_response(dev_info
, rc
);
1134 POSTCODE_LINUX(DEVICE_CREATE_FAILURE_PC
, dev_no
, bus_no
,
1137 POSTCODE_LINUX(DEVICE_CREATE_SUCCESS_PC
, dev_no
, bus_no
,
1138 DIAG_SEVERITY_PRINT
);
1142 chipset_device_destroy(struct visor_device
*dev_info
)
1144 remove_visor_device(dev_info
);
1146 device_destroy_response(dev_info
, 0);
1150 * pause_state_change_complete() - the callback function to be called by a
1151 * visorbus function driver when a
1152 * pending "pause device" operation has
1154 * @dev: struct visor_device identifying the paused device
1155 * @status: 0 iff the pause state change completed successfully, otherwise
1156 * a negative errno value indicating the reason for failure
1159 pause_state_change_complete(struct visor_device
*dev
, int status
)
1164 dev
->pausing
= false;
1166 device_pause_response(dev
, status
);
1170 * resume_state_change_complete() - the callback function to be called by a
1171 * visorbus function driver when a
1172 * pending "resume device" operation has
1174 * @dev: struct visor_device identifying the resumed device
1175 * @status: 0 iff the resume state change completed successfully, otherwise
1176 * a negative errno value indicating the reason for failure
1179 resume_state_change_complete(struct visor_device
*dev
, int status
)
1184 dev
->resuming
= false;
1187 * Notify the chipset driver that the resume is complete,
1188 * which will presumably want to send some sort of response to
1191 device_resume_response(dev
, status
);
1195 * initiate_chipset_device_pause_resume() - start a pause or resume operation
1196 * for a visor device
1197 * @dev: struct visor_device identifying the device being paused or resumed
1198 * @is_pause: true to indicate pause operation, false to indicate resume
1200 * Tell the subordinate function driver for a specific device to pause
1201 * or resume that device. Success/failure result is returned asynchronously
1202 * via a callback function; see pause_state_change_complete() and
1203 * resume_state_change_complete().
1206 initiate_chipset_device_pause_resume(struct visor_device
*dev
, bool is_pause
)
1209 struct visor_driver
*drv
= NULL
;
1210 void (*notify_func
)(struct visor_device
*dev
, int response
) = NULL
;
1213 notify_func
= device_pause_response
;
1215 notify_func
= device_resume_response
;
1219 drv
= to_visor_driver(dev
->device
.driver
);
1221 (*notify_func
)(dev
, -ENODEV
);
1225 if (dev
->pausing
|| dev
->resuming
) {
1226 (*notify_func
)(dev
, -EBUSY
);
1231 * Note that even though both drv->pause() and drv->resume
1232 * specify a callback function, it is NOT necessary for us to
1233 * increment our local module usage count. Reason is, there
1234 * is already a linkage dependency between child function
1235 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1236 * visorbus while child function drivers are still running.
1240 (*notify_func
)(dev
, -EINVAL
);
1244 dev
->pausing
= true;
1245 rc
= drv
->pause(dev
, pause_state_change_complete
);
1247 /* This should be done at BUS resume time, but an
1248 * existing problem prevents us from ever getting a bus
1249 * resume... This hack would fail to work should we
1250 * ever have a bus that contains NO devices, since we
1251 * would never even get here in that case.
1253 fix_vbus_dev_info(dev
);
1255 (*notify_func
)(dev
, -EINVAL
);
1259 dev
->resuming
= true;
1260 rc
= drv
->resume(dev
, resume_state_change_complete
);
1264 dev
->pausing
= false;
1266 dev
->resuming
= false;
1267 (*notify_func
)(dev
, -EINVAL
);
1272 * chipset_device_pause() - start a pause operation for a visor device
1273 * @dev_info: struct visor_device identifying the device being paused
1275 * Tell the subordinate function driver for a specific device to pause
1276 * that device. Success/failure result is returned asynchronously
1277 * via a callback function; see pause_state_change_complete().
1280 chipset_device_pause(struct visor_device
*dev_info
)
1282 initiate_chipset_device_pause_resume(dev_info
, true);
1286 * chipset_device_resume() - start a resume operation for a visor device
1287 * @dev_info: struct visor_device identifying the device being resumed
1289 * Tell the subordinate function driver for a specific device to resume
1290 * that device. Success/failure result is returned asynchronously
1291 * via a callback function; see resume_state_change_complete().
1294 chipset_device_resume(struct visor_device
*dev_info
)
1296 initiate_chipset_device_pause_resume(dev_info
, false);
1304 POSTCODE_LINUX(DRIVER_ENTRY_PC
, 0, 0, DIAG_SEVERITY_PRINT
);
1306 visorbus_debugfs_dir
= debugfs_create_dir("visorbus", NULL
);
1307 if (!visorbus_debugfs_dir
)
1310 bus_device_info_init(&clientbus_driverinfo
, "clientbus", "visorbus");
1312 err
= create_bus_type();
1314 POSTCODE_LINUX(BUS_CREATE_ENTRY_PC
, 0, 0, DIAG_SEVERITY_ERR
);
1318 bus_device_info_init(&chipset_driverinfo
, "chipset", "visorchipset");
1323 POSTCODE_LINUX(CHIPSET_INIT_FAILURE_PC
, 0, err
, DIAG_SEVERITY_ERR
);
1330 struct list_head
*listentry
, *listtmp
;
1332 remove_all_visor_devices();
1334 list_for_each_safe(listentry
, listtmp
, &list_all_bus_instances
) {
1335 struct visor_device
*dev
= list_entry(listentry
,
1336 struct visor_device
,
1338 remove_bus_instance(dev
);
1341 debugfs_remove_recursive(visorbus_debugfs_dir
);
1344 module_param_named(forcematch
, visorbus_forcematch
, int, 0444);
1345 MODULE_PARM_DESC(visorbus_forcematch
,
1346 "1 to force a successful dev <--> drv match");
1348 module_param_named(forcenomatch
, visorbus_forcenomatch
, int, 0444);
1349 MODULE_PARM_DESC(visorbus_forcenomatch
,
1350 "1 to force an UNsuccessful dev <--> drv match");