linux/drivers/misc/mic/vop/vop_main.c
<<
>>
Prefs
   1/*
   2 * Intel MIC Platform Software Stack (MPSS)
   3 *
   4 * Copyright(c) 2016 Intel Corporation.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License, version 2, as
   8 * published by the Free Software Foundation.
   9 *
  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. See the GNU
  13 * General Public License for more details.
  14 *
  15 * The full GNU General Public License is included in this distribution in
  16 * the file called "COPYING".
  17 *
  18 * Adapted from:
  19 *
  20 * virtio for kvm on s390
  21 *
  22 * Copyright IBM Corp. 2008
  23 *
  24 * This program is free software; you can redistribute it and/or modify
  25 * it under the terms of the GNU General Public License (version 2 only)
  26 * as published by the Free Software Foundation.
  27 *
  28 *    Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
  29 *
  30 * Intel Virtio Over PCIe (VOP) driver.
  31 *
  32 */
  33#include <linux/delay.h>
  34#include <linux/module.h>
  35#include <linux/sched.h>
  36#include <linux/dma-mapping.h>
  37
  38#include "vop_main.h"
  39
  40#define VOP_MAX_VRINGS 4
  41
  42/*
  43 * _vop_vdev - Allocated per virtio device instance injected by the peer.
  44 *
  45 * @vdev: Virtio device
  46 * @desc: Virtio device page descriptor
  47 * @dc: Virtio device control
  48 * @vpdev: VOP device which is the parent for this virtio device
  49 * @vr: Buffer for accessing the VRING
  50 * @used: Buffer for used
  51 * @used_size: Size of the used buffer
  52 * @reset_done: Track whether VOP reset is complete
  53 * @virtio_cookie: Cookie returned upon requesting a interrupt
  54 * @c2h_vdev_db: The doorbell used by the guest to interrupt the host
  55 * @h2c_vdev_db: The doorbell used by the host to interrupt the guest
  56 * @dnode: The destination node
  57 */
  58struct _vop_vdev {
  59        struct virtio_device vdev;
  60        struct mic_device_desc __iomem *desc;
  61        struct mic_device_ctrl __iomem *dc;
  62        struct vop_device *vpdev;
  63        void __iomem *vr[VOP_MAX_VRINGS];
  64        dma_addr_t used[VOP_MAX_VRINGS];
  65        int used_size[VOP_MAX_VRINGS];
  66        struct completion reset_done;
  67        struct mic_irq *virtio_cookie;
  68        int c2h_vdev_db;
  69        int h2c_vdev_db;
  70        int dnode;
  71};
  72
  73#define to_vopvdev(vd) container_of(vd, struct _vop_vdev, vdev)
  74
  75#define _vop_aligned_desc_size(d) __mic_align(_vop_desc_size(d), 8)
  76
  77/* Helper API to obtain the parent of the virtio device */
  78static inline struct device *_vop_dev(struct _vop_vdev *vdev)
  79{
  80        return vdev->vdev.dev.parent;
  81}
  82
  83static inline unsigned _vop_desc_size(struct mic_device_desc __iomem *desc)
  84{
  85        return sizeof(*desc)
  86                + ioread8(&desc->num_vq) * sizeof(struct mic_vqconfig)
  87                + ioread8(&desc->feature_len) * 2
  88                + ioread8(&desc->config_len);
  89}
  90
  91static inline struct mic_vqconfig __iomem *
  92_vop_vq_config(struct mic_device_desc __iomem *desc)
  93{
  94        return (struct mic_vqconfig __iomem *)(desc + 1);
  95}
  96
  97static inline u8 __iomem *
  98_vop_vq_features(struct mic_device_desc __iomem *desc)
  99{
 100        return (u8 __iomem *)(_vop_vq_config(desc) + ioread8(&desc->num_vq));
 101}
 102
 103static inline u8 __iomem *
 104_vop_vq_configspace(struct mic_device_desc __iomem *desc)
 105{
 106        return _vop_vq_features(desc) + ioread8(&desc->feature_len) * 2;
 107}
 108
 109static inline unsigned
 110_vop_total_desc_size(struct mic_device_desc __iomem *desc)
 111{
 112        return _vop_aligned_desc_size(desc) + sizeof(struct mic_device_ctrl);
 113}
 114
 115/* This gets the device's feature bits. */
 116static u64 vop_get_features(struct virtio_device *vdev)
 117{
 118        unsigned int i, bits;
 119        u32 features = 0;
 120        struct mic_device_desc __iomem *desc = to_vopvdev(vdev)->desc;
 121        u8 __iomem *in_features = _vop_vq_features(desc);
 122        int feature_len = ioread8(&desc->feature_len);
 123
 124        bits = min_t(unsigned, feature_len, sizeof(vdev->features)) * 8;
 125        for (i = 0; i < bits; i++)
 126                if (ioread8(&in_features[i / 8]) & (BIT(i % 8)))
 127                        features |= BIT(i);
 128
 129        return features;
 130}
 131
 132static int vop_finalize_features(struct virtio_device *vdev)
 133{
 134        unsigned int i, bits;
 135        struct mic_device_desc __iomem *desc = to_vopvdev(vdev)->desc;
 136        u8 feature_len = ioread8(&desc->feature_len);
 137        /* Second half of bitmap is features we accept. */
 138        u8 __iomem *out_features =
 139                _vop_vq_features(desc) + feature_len;
 140
 141        /* Give virtio_ring a chance to accept features. */
 142        vring_transport_features(vdev);
 143
 144        memset_io(out_features, 0, feature_len);
 145        bits = min_t(unsigned, feature_len,
 146                     sizeof(vdev->features)) * 8;
 147        for (i = 0; i < bits; i++) {
 148                if (__virtio_test_bit(vdev, i))
 149                        iowrite8(ioread8(&out_features[i / 8]) | (1 << (i % 8)),
 150                                 &out_features[i / 8]);
 151        }
 152        return 0;
 153}
 154
 155/*
 156 * Reading and writing elements in config space
 157 */
 158static void vop_get(struct virtio_device *vdev, unsigned int offset,
 159                    void *buf, unsigned len)
 160{
 161        struct mic_device_desc __iomem *desc = to_vopvdev(vdev)->desc;
 162
 163        if (offset + len > ioread8(&desc->config_len))
 164                return;
 165        memcpy_fromio(buf, _vop_vq_configspace(desc) + offset, len);
 166}
 167
 168static void vop_set(struct virtio_device *vdev, unsigned int offset,
 169                    const void *buf, unsigned len)
 170{
 171        struct mic_device_desc __iomem *desc = to_vopvdev(vdev)->desc;
 172
 173        if (offset + len > ioread8(&desc->config_len))
 174                return;
 175        memcpy_toio(_vop_vq_configspace(desc) + offset, buf, len);
 176}
 177
 178/*
 179 * The operations to get and set the status word just access the status
 180 * field of the device descriptor. set_status also interrupts the host
 181 * to tell about status changes.
 182 */
 183static u8 vop_get_status(struct virtio_device *vdev)
 184{
 185        return ioread8(&to_vopvdev(vdev)->desc->status);
 186}
 187
 188static void vop_set_status(struct virtio_device *dev, u8 status)
 189{
 190        struct _vop_vdev *vdev = to_vopvdev(dev);
 191        struct vop_device *vpdev = vdev->vpdev;
 192
 193        if (!status)
 194                return;
 195        iowrite8(status, &vdev->desc->status);
 196        vpdev->hw_ops->send_intr(vpdev, vdev->c2h_vdev_db);
 197}
 198
 199/* Inform host on a virtio device reset and wait for ack from host */
 200static void vop_reset_inform_host(struct virtio_device *dev)
 201{
 202        struct _vop_vdev *vdev = to_vopvdev(dev);
 203        struct mic_device_ctrl __iomem *dc = vdev->dc;
 204        struct vop_device *vpdev = vdev->vpdev;
 205        int retry;
 206
 207        iowrite8(0, &dc->host_ack);
 208        iowrite8(1, &dc->vdev_reset);
 209        vpdev->hw_ops->send_intr(vpdev, vdev->c2h_vdev_db);
 210
 211        /* Wait till host completes all card accesses and acks the reset */
 212        for (retry = 100; retry--;) {
 213                if (ioread8(&dc->host_ack))
 214                        break;
 215                msleep(100);
 216        };
 217
 218        dev_dbg(_vop_dev(vdev), "%s: retry: %d\n", __func__, retry);
 219
 220        /* Reset status to 0 in case we timed out */
 221        iowrite8(0, &vdev->desc->status);
 222}
 223
 224static void vop_reset(struct virtio_device *dev)
 225{
 226        struct _vop_vdev *vdev = to_vopvdev(dev);
 227
 228        dev_dbg(_vop_dev(vdev), "%s: virtio id %d\n",
 229                __func__, dev->id.device);
 230
 231        vop_reset_inform_host(dev);
 232        complete_all(&vdev->reset_done);
 233}
 234
 235/*
 236 * The virtio_ring code calls this API when it wants to notify the Host.
 237 */
 238static bool vop_notify(struct virtqueue *vq)
 239{
 240        struct _vop_vdev *vdev = vq->priv;
 241        struct vop_device *vpdev = vdev->vpdev;
 242
 243        vpdev->hw_ops->send_intr(vpdev, vdev->c2h_vdev_db);
 244        return true;
 245}
 246
 247static void vop_del_vq(struct virtqueue *vq, int n)
 248{
 249        struct _vop_vdev *vdev = to_vopvdev(vq->vdev);
 250        struct vring *vr = (struct vring *)(vq + 1);
 251        struct vop_device *vpdev = vdev->vpdev;
 252
 253        dma_unmap_single(&vpdev->dev, vdev->used[n],
 254                         vdev->used_size[n], DMA_BIDIRECTIONAL);
 255        free_pages((unsigned long)vr->used, get_order(vdev->used_size[n]));
 256        vring_del_virtqueue(vq);
 257        vpdev->hw_ops->iounmap(vpdev, vdev->vr[n]);
 258        vdev->vr[n] = NULL;
 259}
 260
 261static void vop_del_vqs(struct virtio_device *dev)
 262{
 263        struct _vop_vdev *vdev = to_vopvdev(dev);
 264        struct virtqueue *vq, *n;
 265        int idx = 0;
 266
 267        dev_dbg(_vop_dev(vdev), "%s\n", __func__);
 268
 269        list_for_each_entry_safe(vq, n, &dev->vqs, list)
 270                vop_del_vq(vq, idx++);
 271}
 272
 273/*
 274 * This routine will assign vring's allocated in host/io memory. Code in
 275 * virtio_ring.c however continues to access this io memory as if it were local
 276 * memory without io accessors.
 277 */
 278static struct virtqueue *vop_find_vq(struct virtio_device *dev,
 279                                     unsigned index,
 280                                     void (*callback)(struct virtqueue *vq),
 281                                     const char *name, bool ctx)
 282{
 283        struct _vop_vdev *vdev = to_vopvdev(dev);
 284        struct vop_device *vpdev = vdev->vpdev;
 285        struct mic_vqconfig __iomem *vqconfig;
 286        struct mic_vqconfig config;
 287        struct virtqueue *vq;
 288        void __iomem *va;
 289        struct _mic_vring_info __iomem *info;
 290        void *used;
 291        int vr_size, _vr_size, err, magic;
 292        struct vring *vr;
 293        u8 type = ioread8(&vdev->desc->type);
 294
 295        if (index >= ioread8(&vdev->desc->num_vq))
 296                return ERR_PTR(-ENOENT);
 297
 298        if (!name)
 299                return ERR_PTR(-ENOENT);
 300
 301        /* First assign the vring's allocated in host memory */
 302        vqconfig = _vop_vq_config(vdev->desc) + index;
 303        memcpy_fromio(&config, vqconfig, sizeof(config));
 304        _vr_size = vring_size(le16_to_cpu(config.num), MIC_VIRTIO_RING_ALIGN);
 305        vr_size = PAGE_ALIGN(_vr_size + sizeof(struct _mic_vring_info));
 306        va = vpdev->hw_ops->ioremap(vpdev, le64_to_cpu(config.address),
 307                        vr_size);
 308        if (!va)
 309                return ERR_PTR(-ENOMEM);
 310        vdev->vr[index] = va;
 311        memset_io(va, 0x0, _vr_size);
 312        vq = vring_new_virtqueue(
 313                                index,
 314                                le16_to_cpu(config.num), MIC_VIRTIO_RING_ALIGN,
 315                                dev,
 316                                false,
 317                                ctx,
 318                                (void __force *)va, vop_notify, callback, name);
 319        if (!vq) {
 320                err = -ENOMEM;
 321                goto unmap;
 322        }
 323        info = va + _vr_size;
 324        magic = ioread32(&info->magic);
 325
 326        if (WARN(magic != MIC_MAGIC + type + index, "magic mismatch")) {
 327                err = -EIO;
 328                goto unmap;
 329        }
 330
 331        /* Allocate and reassign used ring now */
 332        vdev->used_size[index] = PAGE_ALIGN(sizeof(__u16) * 3 +
 333                                             sizeof(struct vring_used_elem) *
 334                                             le16_to_cpu(config.num));
 335        used = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
 336                                        get_order(vdev->used_size[index]));
 337        if (!used) {
 338                err = -ENOMEM;
 339                dev_err(_vop_dev(vdev), "%s %d err %d\n",
 340                        __func__, __LINE__, err);
 341                goto del_vq;
 342        }
 343        vdev->used[index] = dma_map_single(&vpdev->dev, used,
 344                                            vdev->used_size[index],
 345                                            DMA_BIDIRECTIONAL);
 346        if (dma_mapping_error(&vpdev->dev, vdev->used[index])) {
 347                err = -ENOMEM;
 348                dev_err(_vop_dev(vdev), "%s %d err %d\n",
 349                        __func__, __LINE__, err);
 350                goto free_used;
 351        }
 352        writeq(vdev->used[index], &vqconfig->used_address);
 353        /*
 354         * To reassign the used ring here we are directly accessing
 355         * struct vring_virtqueue which is a private data structure
 356         * in virtio_ring.c. At the minimum, a BUILD_BUG_ON() in
 357         * vring_new_virtqueue() would ensure that
 358         *  (&vq->vring == (struct vring *) (&vq->vq + 1));
 359         */
 360        vr = (struct vring *)(vq + 1);
 361        vr->used = used;
 362
 363        vq->priv = vdev;
 364        return vq;
 365free_used:
 366        free_pages((unsigned long)used,
 367                   get_order(vdev->used_size[index]));
 368del_vq:
 369        vring_del_virtqueue(vq);
 370unmap:
 371        vpdev->hw_ops->iounmap(vpdev, vdev->vr[index]);
 372        return ERR_PTR(err);
 373}
 374
 375static int vop_find_vqs(struct virtio_device *dev, unsigned nvqs,
 376                        struct virtqueue *vqs[],
 377                        vq_callback_t *callbacks[],
 378                        const char * const names[], const bool *ctx,
 379                        struct irq_affinity *desc)
 380{
 381        struct _vop_vdev *vdev = to_vopvdev(dev);
 382        struct vop_device *vpdev = vdev->vpdev;
 383        struct mic_device_ctrl __iomem *dc = vdev->dc;
 384        int i, err, retry, queue_idx = 0;
 385
 386        /* We must have this many virtqueues. */
 387        if (nvqs > ioread8(&vdev->desc->num_vq))
 388                return -ENOENT;
 389
 390        for (i = 0; i < nvqs; ++i) {
 391                if (!names[i]) {
 392                        vqs[i] = NULL;
 393                        continue;
 394                }
 395
 396                dev_dbg(_vop_dev(vdev), "%s: %d: %s\n",
 397                        __func__, i, names[i]);
 398                vqs[i] = vop_find_vq(dev, queue_idx++, callbacks[i], names[i],
 399                                     ctx ? ctx[i] : false);
 400                if (IS_ERR(vqs[i])) {
 401                        err = PTR_ERR(vqs[i]);
 402                        goto error;
 403                }
 404        }
 405
 406        iowrite8(1, &dc->used_address_updated);
 407        /*
 408         * Send an interrupt to the host to inform it that used
 409         * rings have been re-assigned.
 410         */
 411        vpdev->hw_ops->send_intr(vpdev, vdev->c2h_vdev_db);
 412        for (retry = 100; --retry;) {
 413                if (!ioread8(&dc->used_address_updated))
 414                        break;
 415                msleep(100);
 416        };
 417
 418        dev_dbg(_vop_dev(vdev), "%s: retry: %d\n", __func__, retry);
 419        if (!retry) {
 420                err = -ENODEV;
 421                goto error;
 422        }
 423
 424        return 0;
 425error:
 426        vop_del_vqs(dev);
 427        return err;
 428}
 429
 430/*
 431 * The config ops structure as defined by virtio config
 432 */
 433static struct virtio_config_ops vop_vq_config_ops = {
 434        .get_features = vop_get_features,
 435        .finalize_features = vop_finalize_features,
 436        .get = vop_get,
 437        .set = vop_set,
 438        .get_status = vop_get_status,
 439        .set_status = vop_set_status,
 440        .reset = vop_reset,
 441        .find_vqs = vop_find_vqs,
 442        .del_vqs = vop_del_vqs,
 443};
 444
 445static irqreturn_t vop_virtio_intr_handler(int irq, void *data)
 446{
 447        struct _vop_vdev *vdev = data;
 448        struct vop_device *vpdev = vdev->vpdev;
 449        struct virtqueue *vq;
 450
 451        vpdev->hw_ops->ack_interrupt(vpdev, vdev->h2c_vdev_db);
 452        list_for_each_entry(vq, &vdev->vdev.vqs, list)
 453                vring_interrupt(0, vq);
 454
 455        return IRQ_HANDLED;
 456}
 457
 458static void vop_virtio_release_dev(struct device *_d)
 459{
 460        struct virtio_device *vdev =
 461                        container_of(_d, struct virtio_device, dev);
 462        struct _vop_vdev *vop_vdev =
 463                        container_of(vdev, struct _vop_vdev, vdev);
 464
 465        kfree(vop_vdev);
 466}
 467
 468/*
 469 * adds a new device and register it with virtio
 470 * appropriate drivers are loaded by the device model
 471 */
 472static int _vop_add_device(struct mic_device_desc __iomem *d,
 473                           unsigned int offset, struct vop_device *vpdev,
 474                           int dnode)
 475{
 476        struct _vop_vdev *vdev, *reg_dev = NULL;
 477        int ret;
 478        u8 type = ioread8(&d->type);
 479
 480        vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
 481        if (!vdev)
 482                return -ENOMEM;
 483
 484        vdev->vpdev = vpdev;
 485        vdev->vdev.dev.parent = &vpdev->dev;
 486        vdev->vdev.dev.release = vop_virtio_release_dev;
 487        vdev->vdev.id.device = type;
 488        vdev->vdev.config = &vop_vq_config_ops;
 489        vdev->desc = d;
 490        vdev->dc = (void __iomem *)d + _vop_aligned_desc_size(d);
 491        vdev->dnode = dnode;
 492        vdev->vdev.priv = (void *)(u64)dnode;
 493        init_completion(&vdev->reset_done);
 494
 495        vdev->h2c_vdev_db = vpdev->hw_ops->next_db(vpdev);
 496        vdev->virtio_cookie = vpdev->hw_ops->request_irq(vpdev,
 497                        vop_virtio_intr_handler, "virtio intr",
 498                        vdev, vdev->h2c_vdev_db);
 499        if (IS_ERR(vdev->virtio_cookie)) {
 500                ret = PTR_ERR(vdev->virtio_cookie);
 501                goto kfree;
 502        }
 503        iowrite8((u8)vdev->h2c_vdev_db, &vdev->dc->h2c_vdev_db);
 504        vdev->c2h_vdev_db = ioread8(&vdev->dc->c2h_vdev_db);
 505
 506        ret = register_virtio_device(&vdev->vdev);
 507        reg_dev = vdev;
 508        if (ret) {
 509                dev_err(_vop_dev(vdev),
 510                        "Failed to register vop device %u type %u\n",
 511                        offset, type);
 512                goto free_irq;
 513        }
 514        writeq((u64)vdev, &vdev->dc->vdev);
 515        dev_dbg(_vop_dev(vdev), "%s: registered vop device %u type %u vdev %p\n",
 516                __func__, offset, type, vdev);
 517
 518        return 0;
 519
 520free_irq:
 521        vpdev->hw_ops->free_irq(vpdev, vdev->virtio_cookie, vdev);
 522kfree:
 523        if (reg_dev)
 524                put_device(&vdev->vdev.dev);
 525        else
 526                kfree(vdev);
 527        return ret;
 528}
 529
 530/*
 531 * match for a vop device with a specific desc pointer
 532 */
 533static int vop_match_desc(struct device *dev, void *data)
 534{
 535        struct virtio_device *_dev = dev_to_virtio(dev);
 536        struct _vop_vdev *vdev = to_vopvdev(_dev);
 537
 538        return vdev->desc == (void __iomem *)data;
 539}
 540
 541static void _vop_handle_config_change(struct mic_device_desc __iomem *d,
 542                                      unsigned int offset,
 543                                      struct vop_device *vpdev)
 544{
 545        struct mic_device_ctrl __iomem *dc
 546                = (void __iomem *)d + _vop_aligned_desc_size(d);
 547        struct _vop_vdev *vdev = (struct _vop_vdev *)readq(&dc->vdev);
 548
 549        if (ioread8(&dc->config_change) != MIC_VIRTIO_PARAM_CONFIG_CHANGED)
 550                return;
 551
 552        dev_dbg(&vpdev->dev, "%s %d\n", __func__, __LINE__);
 553        virtio_config_changed(&vdev->vdev);
 554        iowrite8(1, &dc->guest_ack);
 555}
 556
 557/*
 558 * removes a virtio device if a hot remove event has been
 559 * requested by the host.
 560 */
 561static int _vop_remove_device(struct mic_device_desc __iomem *d,
 562                              unsigned int offset, struct vop_device *vpdev)
 563{
 564        struct mic_device_ctrl __iomem *dc
 565                = (void __iomem *)d + _vop_aligned_desc_size(d);
 566        struct _vop_vdev *vdev = (struct _vop_vdev *)readq(&dc->vdev);
 567        u8 status;
 568        int ret = -1;
 569
 570        if (ioread8(&dc->config_change) == MIC_VIRTIO_PARAM_DEV_REMOVE) {
 571                dev_dbg(&vpdev->dev,
 572                        "%s %d config_change %d type %d vdev %p\n",
 573                        __func__, __LINE__,
 574                        ioread8(&dc->config_change), ioread8(&d->type), vdev);
 575                status = ioread8(&d->status);
 576                reinit_completion(&vdev->reset_done);
 577                unregister_virtio_device(&vdev->vdev);
 578                vpdev->hw_ops->free_irq(vpdev, vdev->virtio_cookie, vdev);
 579                iowrite8(-1, &dc->h2c_vdev_db);
 580                if (status & VIRTIO_CONFIG_S_DRIVER_OK)
 581                        wait_for_completion(&vdev->reset_done);
 582                put_device(&vdev->vdev.dev);
 583                iowrite8(1, &dc->guest_ack);
 584                dev_dbg(&vpdev->dev, "%s %d guest_ack %d\n",
 585                        __func__, __LINE__, ioread8(&dc->guest_ack));
 586                iowrite8(-1, &d->type);
 587                ret = 0;
 588        }
 589        return ret;
 590}
 591
 592#define REMOVE_DEVICES true
 593
 594static void _vop_scan_devices(void __iomem *dp, struct vop_device *vpdev,
 595                              bool remove, int dnode)
 596{
 597        s8 type;
 598        unsigned int i;
 599        struct mic_device_desc __iomem *d;
 600        struct mic_device_ctrl __iomem *dc;
 601        struct device *dev;
 602        int ret;
 603
 604        for (i = sizeof(struct mic_bootparam);
 605                        i < MIC_DP_SIZE; i += _vop_total_desc_size(d)) {
 606                d = dp + i;
 607                dc = (void __iomem *)d + _vop_aligned_desc_size(d);
 608                /*
 609                 * This read barrier is paired with the corresponding write
 610                 * barrier on the host which is inserted before adding or
 611                 * removing a virtio device descriptor, by updating the type.
 612                 */
 613                rmb();
 614                type = ioread8(&d->type);
 615
 616                /* end of list */
 617                if (type == 0)
 618                        break;
 619
 620                if (type == -1)
 621                        continue;
 622
 623                /* device already exists */
 624                dev = device_find_child(&vpdev->dev, (void __force *)d,
 625                                        vop_match_desc);
 626                if (dev) {
 627                        if (remove)
 628                                iowrite8(MIC_VIRTIO_PARAM_DEV_REMOVE,
 629                                         &dc->config_change);
 630                        put_device(dev);
 631                        _vop_handle_config_change(d, i, vpdev);
 632                        ret = _vop_remove_device(d, i, vpdev);
 633                        if (remove) {
 634                                iowrite8(0, &dc->config_change);
 635                                iowrite8(0, &dc->guest_ack);
 636                        }
 637                        continue;
 638                }
 639
 640                /* new device */
 641                dev_dbg(&vpdev->dev, "%s %d Adding new virtio device %p\n",
 642                        __func__, __LINE__, d);
 643                if (!remove)
 644                        _vop_add_device(d, i, vpdev, dnode);
 645        }
 646}
 647
 648static void vop_scan_devices(struct vop_info *vi,
 649                             struct vop_device *vpdev, bool remove)
 650{
 651        void __iomem *dp = vpdev->hw_ops->get_remote_dp(vpdev);
 652
 653        if (!dp)
 654                return;
 655        mutex_lock(&vi->vop_mutex);
 656        _vop_scan_devices(dp, vpdev, remove, vpdev->dnode);
 657        mutex_unlock(&vi->vop_mutex);
 658}
 659
 660/*
 661 * vop_hotplug_device tries to find changes in the device page.
 662 */
 663static void vop_hotplug_devices(struct work_struct *work)
 664{
 665        struct vop_info *vi = container_of(work, struct vop_info,
 666                                             hotplug_work);
 667
 668        vop_scan_devices(vi, vi->vpdev, !REMOVE_DEVICES);
 669}
 670
 671/*
 672 * Interrupt handler for hot plug/config changes etc.
 673 */
 674static irqreturn_t vop_extint_handler(int irq, void *data)
 675{
 676        struct vop_info *vi = data;
 677        struct mic_bootparam __iomem *bp;
 678        struct vop_device *vpdev = vi->vpdev;
 679
 680        bp = vpdev->hw_ops->get_remote_dp(vpdev);
 681        dev_dbg(&vpdev->dev, "%s %d hotplug work\n",
 682                __func__, __LINE__);
 683        vpdev->hw_ops->ack_interrupt(vpdev, ioread8(&bp->h2c_config_db));
 684        schedule_work(&vi->hotplug_work);
 685        return IRQ_HANDLED;
 686}
 687
 688static int vop_driver_probe(struct vop_device *vpdev)
 689{
 690        struct vop_info *vi;
 691        int rc;
 692
 693        vi = kzalloc(sizeof(*vi), GFP_KERNEL);
 694        if (!vi) {
 695                rc = -ENOMEM;
 696                goto exit;
 697        }
 698        dev_set_drvdata(&vpdev->dev, vi);
 699        vi->vpdev = vpdev;
 700
 701        mutex_init(&vi->vop_mutex);
 702        INIT_WORK(&vi->hotplug_work, vop_hotplug_devices);
 703        if (vpdev->dnode) {
 704                rc = vop_host_init(vi);
 705                if (rc < 0)
 706                        goto free;
 707        } else {
 708                struct mic_bootparam __iomem *bootparam;
 709
 710                vop_scan_devices(vi, vpdev, !REMOVE_DEVICES);
 711
 712                vi->h2c_config_db = vpdev->hw_ops->next_db(vpdev);
 713                vi->cookie = vpdev->hw_ops->request_irq(vpdev,
 714                                                        vop_extint_handler,
 715                                                        "virtio_config_intr",
 716                                                        vi, vi->h2c_config_db);
 717                if (IS_ERR(vi->cookie)) {
 718                        rc = PTR_ERR(vi->cookie);
 719                        goto free;
 720                }
 721                bootparam = vpdev->hw_ops->get_remote_dp(vpdev);
 722                iowrite8(vi->h2c_config_db, &bootparam->h2c_config_db);
 723        }
 724        vop_init_debugfs(vi);
 725        return 0;
 726free:
 727        kfree(vi);
 728exit:
 729        return rc;
 730}
 731
 732static void vop_driver_remove(struct vop_device *vpdev)
 733{
 734        struct vop_info *vi = dev_get_drvdata(&vpdev->dev);
 735
 736        if (vpdev->dnode) {
 737                vop_host_uninit(vi);
 738        } else {
 739                struct mic_bootparam __iomem *bootparam =
 740                        vpdev->hw_ops->get_remote_dp(vpdev);
 741                if (bootparam)
 742                        iowrite8(-1, &bootparam->h2c_config_db);
 743                vpdev->hw_ops->free_irq(vpdev, vi->cookie, vi);
 744                flush_work(&vi->hotplug_work);
 745                vop_scan_devices(vi, vpdev, REMOVE_DEVICES);
 746        }
 747        vop_exit_debugfs(vi);
 748        kfree(vi);
 749}
 750
 751static struct vop_device_id id_table[] = {
 752        { VOP_DEV_TRNSP, VOP_DEV_ANY_ID },
 753        { 0 },
 754};
 755
 756static struct vop_driver vop_driver = {
 757        .driver.name =  KBUILD_MODNAME,
 758        .driver.owner = THIS_MODULE,
 759        .id_table = id_table,
 760        .probe = vop_driver_probe,
 761        .remove = vop_driver_remove,
 762};
 763
 764module_vop_driver(vop_driver);
 765
 766MODULE_DEVICE_TABLE(mbus, id_table);
 767MODULE_AUTHOR("Intel Corporation");
 768MODULE_DESCRIPTION("Intel(R) Virtio Over PCIe (VOP) driver");
 769MODULE_LICENSE("GPL v2");
 770