linux/drivers/visorbus/visorbus_main.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright � 2010 - 2015 UNISYS CORPORATION
   4 * All rights reserved.
   5 */
   6
   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>
  13
  14#include "visorbus_private.h"
  15
  16static const guid_t visor_vbus_channel_guid = VISOR_VBUS_CHANNEL_GUID;
  17
  18/* Display string that is guaranteed to be no longer the 99 characters */
  19#define LINESIZE 99
  20#define POLLJIFFIES_NORMALCHANNEL 10
  21
  22/* stores whether bus_registration was successful */
  23static bool initialized;
  24static struct dentry *visorbus_debugfs_dir;
  25
  26/*
  27 * DEVICE type attributes
  28 *
  29 * The modalias file will contain the guid of the device.
  30 */
  31static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  32                             char *buf)
  33{
  34        struct visor_device *vdev;
  35        const guid_t *guid;
  36
  37        vdev = to_visor_device(dev);
  38        guid = visorchannel_get_guid(vdev->visorchannel);
  39        return sprintf(buf, "visorbus:%pUl\n", guid);
  40}
  41static DEVICE_ATTR_RO(modalias);
  42
  43static struct attribute *visorbus_dev_attrs[] = {
  44        &dev_attr_modalias.attr,
  45        NULL,
  46};
  47
  48ATTRIBUTE_GROUPS(visorbus_dev);
  49
  50/* filled in with info about parent chipset driver when we register with it */
  51static struct visor_vbus_deviceinfo chipset_driverinfo;
  52/* filled in with info about this driver, wrt it servicing client busses */
  53static struct visor_vbus_deviceinfo clientbus_driverinfo;
  54
  55/* list of visor_device structs, linked via .list_all */
  56static LIST_HEAD(list_all_bus_instances);
  57/* list of visor_device structs, linked via .list_all */
  58static LIST_HEAD(list_all_device_instances);
  59
  60/*
  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.
  65 */
  66int 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)
  70{
  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,
  76                                &ch->chtype);
  77                        return 0;
  78                }
  79        }
  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,
  86                                ch->size);
  87                        return 0;
  88                }
  89        }
  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,
  96                                ch->version_id);
  97                        return 0;
  98                }
  99        }
 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,
 105                                ch->signature);
 106                        return 0;
 107                }
 108        }
 109        return 1;
 110}
 111
 112static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
 113{
 114        struct visor_device *dev;
 115        const guid_t *guid;
 116
 117        dev = to_visor_device(xdev);
 118        guid = visorchannel_get_guid(dev->visorchannel);
 119        return add_uevent_var(env, "MODALIAS=visorbus:%pUl", guid);
 120}
 121
 122/*
 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
 128 *
 129 * Return: 1 iff the provided driver can control the specified device
 130 */
 131static int visorbus_match(struct device *xdev, struct device_driver *xdrv)
 132{
 133        const guid_t *channel_type;
 134        int i;
 135        struct visor_device *dev;
 136        struct visor_driver *drv;
 137        struct visorchannel *chan;
 138
 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)
 144                return 0;
 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),
 148                                        xdev,
 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))
 154                        return i + 1;
 155        return 0;
 156}
 157
 158/*
 159 * This describes the TYPE of bus.
 160 * (Don't confuse this with an INSTANCE of the bus.)
 161 */
 162static struct bus_type visorbus_type = {
 163        .name = "visorbus",
 164        .match = visorbus_match,
 165        .uevent = visorbus_uevent,
 166        .dev_groups = visorbus_dev_groups,
 167};
 168
 169struct visor_busdev {
 170        u32 bus_no;
 171        u32 dev_no;
 172};
 173
 174static int match_visorbus_dev_by_id(struct device *dev, const void *data)
 175{
 176        struct visor_device *vdev = to_visor_device(dev);
 177        const struct visor_busdev *id = data;
 178
 179        if (vdev->chipset_bus_no == id->bus_no &&
 180            vdev->chipset_dev_no == id->dev_no)
 181                return 1;
 182        return 0;
 183}
 184
 185struct visor_device *visorbus_get_device_by_id(u32 bus_no, u32 dev_no,
 186                                               struct visor_device *from)
 187{
 188        struct device *dev;
 189        struct device *dev_start = NULL;
 190        struct visor_busdev id = {
 191                .bus_no = bus_no,
 192                .dev_no = dev_no
 193        };
 194
 195        if (from)
 196                dev_start = &from->device;
 197        dev = bus_find_device(&visorbus_type, dev_start, (void *)&id,
 198                              match_visorbus_dev_by_id);
 199        if (!dev)
 200                return NULL;
 201        return to_visor_device(dev);
 202}
 203
 204/*
 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
 209 */
 210static void visorbus_release_busdevice(struct device *xdev)
 211{
 212        struct visor_device *dev = dev_get_drvdata(xdev);
 213
 214        debugfs_remove(dev->debugfs_bus_info);
 215        debugfs_remove_recursive(dev->debugfs_dir);
 216        visorchannel_destroy(dev->visorchannel);
 217        kfree(dev);
 218}
 219
 220/*
 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
 224 */
 225static void visorbus_release_device(struct device *xdev)
 226{
 227        struct visor_device *dev = to_visor_device(xdev);
 228
 229        visorchannel_destroy(dev->visorchannel);
 230        kfree(dev);
 231}
 232
 233/*
 234 * BUS specific channel attributes to appear under
 235 * /sys/bus/visorbus<x>/dev<y>/channel
 236 */
 237
 238static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
 239                             char *buf)
 240{
 241        struct visor_device *vdev = to_visor_device(dev);
 242
 243        return sprintf(buf, "0x%llx\n",
 244                       visorchannel_get_physaddr(vdev->visorchannel));
 245}
 246static DEVICE_ATTR_RO(physaddr);
 247
 248static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
 249                           char *buf)
 250{
 251        struct visor_device *vdev = to_visor_device(dev);
 252
 253        return sprintf(buf, "0x%lx\n",
 254                       visorchannel_get_nbytes(vdev->visorchannel));
 255}
 256static DEVICE_ATTR_RO(nbytes);
 257
 258static ssize_t clientpartition_show(struct device *dev,
 259                                    struct device_attribute *attr, char *buf)
 260{
 261        struct visor_device *vdev = to_visor_device(dev);
 262
 263        return sprintf(buf, "0x%llx\n",
 264                       visorchannel_get_clientpartition(vdev->visorchannel));
 265}
 266static DEVICE_ATTR_RO(clientpartition);
 267
 268static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
 269                             char *buf)
 270{
 271        struct visor_device *vdev = to_visor_device(dev);
 272        char typeid[LINESIZE];
 273
 274        return sprintf(buf, "%s\n",
 275                       visorchannel_id(vdev->visorchannel, typeid));
 276}
 277static DEVICE_ATTR_RO(typeguid);
 278
 279static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
 280                             char *buf)
 281{
 282        struct visor_device *vdev = to_visor_device(dev);
 283        char zoneid[LINESIZE];
 284
 285        return sprintf(buf, "%s\n",
 286                       visorchannel_zoneid(vdev->visorchannel, zoneid));
 287}
 288static DEVICE_ATTR_RO(zoneguid);
 289
 290static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
 291                             char *buf)
 292{
 293        int i = 0;
 294        struct bus_type *xbus = dev->bus;
 295        struct device_driver *xdrv = dev->driver;
 296        struct visor_driver *drv = NULL;
 297
 298        if (!xdrv)
 299                return 0;
 300        i = xbus->match(dev, xdrv);
 301        if (!i)
 302                return 0;
 303        drv = to_visor_driver(xdrv);
 304        return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
 305}
 306static DEVICE_ATTR_RO(typename);
 307
 308static 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,
 315        NULL
 316};
 317
 318ATTRIBUTE_GROUPS(channel);
 319
 320/*
 321 *  BUS instance attributes
 322 *
 323 *  define & implement display of bus attributes under
 324 *  /sys/bus/visorbus/devices/visorbus<n>.
 325 */
 326static ssize_t partition_handle_show(struct device *dev,
 327                                     struct device_attribute *attr, char *buf)
 328{
 329        struct visor_device *vdev = to_visor_device(dev);
 330        u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
 331
 332        return sprintf(buf, "0x%llx\n", handle);
 333}
 334static DEVICE_ATTR_RO(partition_handle);
 335
 336static ssize_t partition_guid_show(struct device *dev,
 337                                   struct device_attribute *attr, char *buf)
 338{
 339        struct visor_device *vdev = to_visor_device(dev);
 340
 341        return sprintf(buf, "{%pUb}\n", &vdev->partition_guid);
 342}
 343static DEVICE_ATTR_RO(partition_guid);
 344
 345static ssize_t partition_name_show(struct device *dev,
 346                                   struct device_attribute *attr, char *buf)
 347{
 348        struct visor_device *vdev = to_visor_device(dev);
 349
 350        return sprintf(buf, "%s\n", vdev->name);
 351}
 352static DEVICE_ATTR_RO(partition_name);
 353
 354static ssize_t channel_addr_show(struct device *dev,
 355                                 struct device_attribute *attr, char *buf)
 356{
 357        struct visor_device *vdev = to_visor_device(dev);
 358        u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
 359
 360        return sprintf(buf, "0x%llx\n", addr);
 361}
 362static DEVICE_ATTR_RO(channel_addr);
 363
 364static ssize_t channel_bytes_show(struct device *dev,
 365                                  struct device_attribute *attr, char *buf)
 366{
 367        struct visor_device *vdev = to_visor_device(dev);
 368        u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
 369
 370        return sprintf(buf, "0x%llx\n", nbytes);
 371}
 372static DEVICE_ATTR_RO(channel_bytes);
 373
 374static ssize_t channel_id_show(struct device *dev,
 375                               struct device_attribute *attr, char *buf)
 376{
 377        struct visor_device *vdev = to_visor_device(dev);
 378        int len = 0;
 379
 380        visorchannel_id(vdev->visorchannel, buf);
 381        len = strlen(buf);
 382        buf[len++] = '\n';
 383        return len;
 384}
 385static DEVICE_ATTR_RO(channel_id);
 386
 387static 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,
 394        NULL
 395};
 396
 397ATTRIBUTE_GROUPS(visorbus);
 398
 399/*
 400 *  BUS debugfs entries
 401 *
 402 *  define & implement display of debugfs attributes under
 403 *  /sys/kernel/debug/visorbus/visorbus<n>.
 404 */
 405
 406/*
 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
 413 *
 414 * Reads @devInfo, and writes it in human-readable notation to @seq.
 415 */
 416static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo,
 417                                      struct seq_file *seq, int devix)
 418{
 419        /* uninitialized vbus device entry */
 420        if (!isprint(devinfo->devtype[0]))
 421                return;
 422        if (devix >= 0)
 423                seq_printf(seq, "[%d]", devix);
 424        else
 425                /* vbus device entry is for bus or chipset */
 426                seq_puts(seq, "   ");
 427        /*
 428         * Note: because the s-Par back-end is free to scribble in this area,
 429         * we never assume '\0'-termination.
 430         */
 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),
 436                   devinfo->infostrs);
 437}
 438
 439static int bus_info_debugfs_show(struct seq_file *seq, void *v)
 440{
 441        int i = 0;
 442        unsigned long off;
 443        struct visor_vbus_deviceinfo dev_info;
 444        struct visor_device *vdev = seq->private;
 445        struct visorchannel *channel = vdev->visorchannel;
 446
 447        if (!channel)
 448                return 0;
 449
 450        seq_printf(seq,
 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);
 462
 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);
 469                i++;
 470        }
 471        return 0;
 472}
 473
 474static int bus_info_debugfs_open(struct inode *inode, struct file *file)
 475{
 476        return single_open(file, bus_info_debugfs_show, inode->i_private);
 477}
 478
 479static const struct file_operations bus_info_debugfs_fops = {
 480        .owner = THIS_MODULE,
 481        .open = bus_info_debugfs_open,
 482        .read = seq_read,
 483        .llseek = seq_lseek,
 484        .release = single_release,
 485};
 486
 487static void dev_periodic_work(struct timer_list *t)
 488{
 489        struct visor_device *dev = from_timer(dev, t, timer);
 490        struct visor_driver *drv = to_visor_driver(dev->device.driver);
 491
 492        drv->channel_interrupt(dev);
 493        mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
 494}
 495
 496static int dev_start_periodic_work(struct visor_device *dev)
 497{
 498        if (dev->being_removed || dev->timer_active)
 499                return -EINVAL;
 500
 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;
 506        return 0;
 507}
 508
 509static void dev_stop_periodic_work(struct visor_device *dev)
 510{
 511        if (!dev->timer_active)
 512                return;
 513
 514        del_timer_sync(&dev->timer);
 515        dev->timer_active = false;
 516        put_device(&dev->device);
 517}
 518
 519/*
 520 * visordriver_remove_device() - handle visor device going away
 521 * @xdev: struct device for the visor device being removed
 522 *
 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.
 526 *
 527 * Return: 0 iff successful
 528 */
 529static int visordriver_remove_device(struct device *xdev)
 530{
 531        struct visor_device *dev = to_visor_device(xdev);
 532        struct visor_driver *drv = to_visor_driver(xdev->driver);
 533
 534        mutex_lock(&dev->visordriver_callback_lock);
 535        dev->being_removed = true;
 536        drv->remove(dev);
 537        mutex_unlock(&dev->visordriver_callback_lock);
 538        dev_stop_periodic_work(dev);
 539        put_device(&dev->device);
 540        return 0;
 541}
 542
 543/*
 544 * visorbus_unregister_visor_driver() - unregisters the provided driver
 545 * @drv: the driver to unregister
 546 *
 547 * A visor function driver calls this function to unregister the driver,
 548 * i.e., within its module_exit function.
 549 */
 550void visorbus_unregister_visor_driver(struct visor_driver *drv)
 551{
 552        driver_unregister(&drv->driver);
 553}
 554EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
 555
 556/*
 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
 563 *
 564 * If receiving a message, use the visorchannel_signalremove() function instead.
 565 *
 566 * Return: integer indicating success (zero) or failure (non-zero)
 567 */
 568int visorbus_read_channel(struct visor_device *dev, unsigned long offset,
 569                          void *dest, unsigned long nbytes)
 570{
 571        return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
 572}
 573EXPORT_SYMBOL_GPL(visorbus_read_channel);
 574
 575/*
 576 * visorbus_write_channel() - writes the provided buffer into the designated
 577 *                            channel
 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
 582 *
 583 * If sending a message, use the visorchannel_signalinsert() function instead.
 584 *
 585 * Return: integer indicating success (zero) or failure (non-zero)
 586 */
 587int visorbus_write_channel(struct visor_device *dev, unsigned long offset,
 588                           void *src, unsigned long nbytes)
 589{
 590        return visorchannel_write(dev->visorchannel, offset, src, nbytes);
 591}
 592EXPORT_SYMBOL_GPL(visorbus_write_channel);
 593
 594/*
 595 * visorbus_enable_channel_interrupts() - enables interrupts on the
 596 *                                        designated device
 597 * @dev: the device on which to enable interrupts
 598 *
 599 * Currently we don't yet have a real interrupt, so for now we just call the
 600 * interrupt function periodically via a timer.
 601 */
 602int visorbus_enable_channel_interrupts(struct visor_device *dev)
 603{
 604        struct visor_driver *drv = to_visor_driver(dev->device.driver);
 605
 606        if (!drv->channel_interrupt) {
 607                dev_err(&dev->device, "%s no interrupt function!\n", __func__);
 608                return -ENOENT;
 609        }
 610
 611        return dev_start_periodic_work(dev);
 612}
 613EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
 614
 615/*
 616 * visorbus_disable_channel_interrupts() - disables interrupts on the
 617 *                                         designated device
 618 * @dev: the device on which to disable interrupts
 619 */
 620void visorbus_disable_channel_interrupts(struct visor_device *dev)
 621{
 622        dev_stop_periodic_work(dev);
 623}
 624EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
 625
 626/*
 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
 631 *
 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
 638 * just created.
 639 *
 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.
 644 *
 645 * Return: 0 if successful, otherwise the negative value returned by
 646 *         device_add() indicating the reason for failure
 647 */
 648int create_visor_device(struct visor_device *dev)
 649{
 650        int err;
 651        u32 chipset_bus_no = dev->chipset_bus_no;
 652        u32 chipset_dev_no = dev->chipset_dev_no;
 653
 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);
 662        /*
 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
 665         * name.
 666         */
 667        err = dev_set_name(&dev->device, "vbus%u:dev%u",
 668                           chipset_bus_no, chipset_dev_no);
 669        if (err)
 670                goto err_put;
 671        /*
 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... **
 678         *          else
 679         *            if (bus.match(dev,drv)) [visorbus_match]
 680         *              dev.drv = drv
 681         *              if (!drv.probe(dev))  [visordriver_probe_device]
 682         *                dev.drv = NULL
 683         *
 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).
 687         */
 688        err = device_add(&dev->device);
 689        if (err < 0)
 690                goto err_put;
 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() */
 695        return 0;
 696
 697err_put:
 698        put_device(&dev->device);
 699        dev_err(&dev->device, "Creating visor device failed. %d\n", err);
 700        return err;
 701}
 702
 703void remove_visor_device(struct visor_device *dev)
 704{
 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);
 710}
 711
 712static int get_vbus_header_info(struct visorchannel *chan,
 713                                struct device *dev,
 714                                struct visor_vbus_headerinfo *hdr_info)
 715{
 716        int err;
 717
 718        if (!visor_check_channel(visorchannel_get_header(chan),
 719                                 dev,
 720                                 &visor_vbus_channel_guid,
 721                                 "vbus",
 722                                 sizeof(struct visor_vbus_channel),
 723                                 VISOR_VBUS_CHANNEL_VERSIONID,
 724                                 VISOR_CHANNEL_SIGNATURE))
 725                return -EINVAL;
 726
 727        err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
 728                                sizeof(*hdr_info));
 729        if (err < 0)
 730                return err;
 731        if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo))
 732                return -EINVAL;
 733        if (hdr_info->device_info_struct_bytes <
 734            sizeof(struct visor_vbus_deviceinfo))
 735                return -EINVAL;
 736        return 0;
 737}
 738
 739/*
 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
 745 *
 746 * Writes chipset info into the channel memory to be used for diagnostic
 747 * purposes.
 748 *
 749 * Returns no value since this is debug information and not needed for
 750 * device functionality.
 751 */
 752static void write_vbus_chp_info(struct visorchannel *chan,
 753                                struct visor_vbus_headerinfo *hdr_info,
 754                                struct visor_vbus_deviceinfo *info)
 755{
 756        int off;
 757
 758        if (hdr_info->chp_info_offset == 0)
 759                return;
 760
 761        off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
 762        visorchannel_write(chan, off, info, sizeof(*info));
 763}
 764
 765/*
 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
 771 *
 772 * Writes bus info into the channel memory to be used for diagnostic
 773 * purposes.
 774 *
 775 * Returns no value since this is debug information and not needed for
 776 * device functionality.
 777 */
 778static void write_vbus_bus_info(struct visorchannel *chan,
 779                                struct visor_vbus_headerinfo *hdr_info,
 780                                struct visor_vbus_deviceinfo *info)
 781{
 782        int off;
 783
 784        if (hdr_info->bus_info_offset == 0)
 785                return;
 786
 787        off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
 788        visorchannel_write(chan, off, info, sizeof(*info));
 789}
 790
 791/*
 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
 798 *
 799 * Writes device info into the channel memory to be used for diagnostic
 800 * purposes.
 801 *
 802 * Returns no value since this is debug information and not needed for
 803 * device functionality.
 804 */
 805static void write_vbus_dev_info(struct visorchannel *chan,
 806                                struct visor_vbus_headerinfo *hdr_info,
 807                                struct visor_vbus_deviceinfo *info,
 808                                unsigned int devix)
 809{
 810        int off;
 811
 812        if (hdr_info->dev_info_offset == 0)
 813                return;
 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));
 817}
 818
 819static void bus_device_info_init(
 820                struct visor_vbus_deviceinfo *bus_device_info_ptr,
 821                const char *dev_type, const char *drv_name)
 822{
 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",
 832                 utsname()->release);
 833}
 834
 835/*
 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
 841 */
 842static void publish_vbus_dev_info(struct visor_device *visordev)
 843{
 844        int i;
 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;
 852
 853        if (!visordev->device.driver)
 854                return;
 855        bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
 856        if (!bdev)
 857                return;
 858        hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info;
 859        if (!hdr_info)
 860                return;
 861        visordrv = to_visor_driver(visordev->device.driver);
 862
 863        /*
 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
 867         * type name
 868         */
 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;
 873                        break;
 874                }
 875        }
 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);
 881}
 882
 883/*
 884 * visordriver_probe_device() - handle new visor device coming online
 885 * @xdev: struct device for the visor device being probed
 886 *
 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
 890 * driver and device.
 891 *
 892 * If successful, a reference to the device will be held onto via get_device().
 893 *
 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
 897 */
 898static int visordriver_probe_device(struct device *xdev)
 899{
 900        int err;
 901        struct visor_driver *drv = to_visor_driver(xdev->driver);
 902        struct visor_device *dev = to_visor_device(xdev);
 903
 904        mutex_lock(&dev->visordriver_callback_lock);
 905        dev->being_removed = false;
 906        err = drv->probe(dev);
 907        if (err) {
 908                mutex_unlock(&dev->visordriver_callback_lock);
 909                return err;
 910        }
 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);
 915        return 0;
 916}
 917
 918/*
 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
 923 *
 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
 927 *
 928 * Here's how the whole Linux bus / driver / device model works.
 929 *
 930 * At system start-up, the visorbus kernel module is loaded, which registers
 931 * visorbus_type as a bus type, using bus_register().
 932 *
 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):
 944 *
 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()).
 955 *
 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().
 963 *
 964 * Return: integer indicating success (zero) or failure (non-zero)
 965 */
 966int visorbus_register_visor_driver(struct visor_driver *drv)
 967{
 968        /* can't register on a nonexistent bus */
 969        if (!initialized)
 970                return -ENODEV;
 971        if (!drv->probe)
 972                return -EINVAL;
 973        if (!drv->remove)
 974                return -EINVAL;
 975        if (!drv->pause)
 976                return -EINVAL;
 977        if (!drv->resume)
 978                return -EINVAL;
 979
 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;
 985        /*
 986         * driver_register does this:
 987         *   bus_add_driver(drv)
 988         *   ->if (drv.bus)  ** (bus_type) **
 989         *       driver_attach(drv)
 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]
 993         *               dev.drv = drv
 994         *               if (!drv.probe(dev))   [visordriver_probe_device]
 995         *                 dev.drv = NULL
 996         */
 997        return driver_register(&drv->driver);
 998}
 999EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1000
1001/*
1002 * visorbus_create_instance() - create a device instance for the visorbus itself
1003 * @dev: struct visor_device indicating the bus instance
1004 *
1005 * Return: 0 for success, otherwise negative errno value indicating reason for
1006 *         failure
1007 */
1008int visorbus_create_instance(struct visor_device *dev)
1009{
1010        int id = dev->chipset_bus_no;
1011        int err;
1012        struct visor_vbus_headerinfo *hdr_info;
1013
1014        hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1015        if (!hdr_info)
1016                return -ENOMEM;
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);
1028        if (err < 0)
1029                goto err_debugfs_dir;
1030        err = device_register(&dev->device);
1031        if (err < 0)
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);
1039        return 0;
1040
1041err_debugfs_dir:
1042        debugfs_remove_recursive(dev->debugfs_dir);
1043        kfree(hdr_info);
1044        dev_err(&dev->device, "%s failed: %d\n", __func__, err);
1045        return err;
1046}
1047
1048/*
1049 * visorbus_remove_instance() - remove a device instance for the visorbus itself
1050 * @dev: struct visor_device indentifying the bus to remove
1051 */
1052void visorbus_remove_instance(struct visor_device *dev)
1053{
1054        /*
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.
1061         */
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);
1067}
1068
1069/*
1070 * remove_all_visor_devices() - remove all child visorbus device instances
1071 */
1072static void remove_all_visor_devices(void)
1073{
1074        struct list_head *listentry, *listtmp;
1075
1076        list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1077                struct visor_device *dev;
1078
1079                dev = list_entry(listentry, struct visor_device, list_all);
1080                remove_visor_device(dev);
1081        }
1082}
1083
1084/*
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
1088 *                                 completed
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
1092 */
1093static void pause_state_change_complete(struct visor_device *dev, int status)
1094{
1095        if (!dev->pausing)
1096                return;
1097
1098        dev->pausing = false;
1099        visorbus_device_changestate_response(dev, status,
1100                                             segment_state_standby);
1101}
1102
1103/*
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
1107 *                                  completed
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
1111 */
1112static void resume_state_change_complete(struct visor_device *dev, int status)
1113{
1114        if (!dev->resuming)
1115                return;
1116
1117        dev->resuming = false;
1118        /*
1119         * Notify the chipset driver that the resume is complete,
1120         * which will presumably want to send some sort of response to
1121         * the initiator.
1122         */
1123        visorbus_device_changestate_response(dev, status,
1124                                             segment_state_running);
1125}
1126
1127/*
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
1132 *
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().
1137 */
1138static int visorchipset_initiate_device_pause_resume(struct visor_device *dev,
1139                                                     bool is_pause)
1140{
1141        int err;
1142        struct visor_driver *drv;
1143
1144        /* If no driver associated with the device nothing to pause/resume */
1145        if (!dev->device.driver)
1146                return 0;
1147        if (dev->pausing || dev->resuming)
1148                return -EBUSY;
1149
1150        drv = to_visor_driver(dev->device.driver);
1151        if (is_pause) {
1152                dev->pausing = true;
1153                err = drv->pause(dev, pause_state_change_complete);
1154        } else {
1155                /*
1156                 * The vbus_dev_info structure in the channel was been cleared,
1157                 * make sure it is valid.
1158                 */
1159                publish_vbus_dev_info(dev);
1160                dev->resuming = true;
1161                err = drv->resume(dev, resume_state_change_complete);
1162        }
1163        return err;
1164}
1165
1166/*
1167 * visorchipset_device_pause() - start a pause operation for a visor device
1168 * @dev_info: struct visor_device identifying the device being paused
1169 *
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().
1173 */
1174int visorchipset_device_pause(struct visor_device *dev_info)
1175{
1176        int err;
1177
1178        err = visorchipset_initiate_device_pause_resume(dev_info, true);
1179        if (err < 0) {
1180                dev_info->pausing = false;
1181                return err;
1182        }
1183        return 0;
1184}
1185
1186/*
1187 * visorchipset_device_resume() - start a resume operation for a visor device
1188 * @dev_info: struct visor_device identifying the device being resumed
1189 *
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().
1193 */
1194int visorchipset_device_resume(struct visor_device *dev_info)
1195{
1196        int err;
1197
1198        err = visorchipset_initiate_device_pause_resume(dev_info, false);
1199        if (err < 0) {
1200                dev_info->resuming = false;
1201                return err;
1202        }
1203        return 0;
1204}
1205
1206int visorbus_init(void)
1207{
1208        int err;
1209
1210        visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
1211        bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
1212        err = bus_register(&visorbus_type);
1213        if (err < 0)
1214                return err;
1215        initialized = true;
1216        bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
1217        return 0;
1218}
1219
1220void visorbus_exit(void)
1221{
1222        struct list_head *listentry, *listtmp;
1223
1224        remove_all_visor_devices();
1225        list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1226                struct visor_device *dev;
1227
1228                dev = list_entry(listentry, struct visor_device, list_all);
1229                visorbus_remove_instance(dev);
1230        }
1231        bus_unregister(&visorbus_type);
1232        initialized = false;
1233        debugfs_remove_recursive(visorbus_debugfs_dir);
1234}
1235