]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/misc/mic/card/mic_virtio.c
virtio: assert 32 bit features in transports
[karo-tx-linux.git] / drivers / misc / mic / card / mic_virtio.c
1 /*
2  * Intel MIC Platform Software Stack (MPSS)
3  *
4  * Copyright(c) 2013 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  * Disclaimer: The codes contained in these modules may be specific to
19  * the Intel Software Development Platform codenamed: Knights Ferry, and
20  * the Intel product codenamed: Knights Corner, and are not backward
21  * compatible with other Intel products. Additionally, Intel will NOT
22  * support the codes or instruction set in future products.
23  *
24  * Adapted from:
25  *
26  * virtio for kvm on s390
27  *
28  * Copyright IBM Corp. 2008
29  *
30  * This program is free software; you can redistribute it and/or modify
31  * it under the terms of the GNU General Public License (version 2 only)
32  * as published by the Free Software Foundation.
33  *
34  *    Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
35  *
36  * Intel MIC Card driver.
37  *
38  */
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/virtio_config.h>
42
43 #include "../common/mic_dev.h"
44 #include "mic_virtio.h"
45
46 #define VIRTIO_SUBCODE_64 0x0D00
47
48 #define MIC_MAX_VRINGS                4
49 struct mic_vdev {
50         struct virtio_device vdev;
51         struct mic_device_desc __iomem *desc;
52         struct mic_device_ctrl __iomem *dc;
53         struct mic_device *mdev;
54         void __iomem *vr[MIC_MAX_VRINGS];
55         int used_size[MIC_MAX_VRINGS];
56         struct completion reset_done;
57         struct mic_irq *virtio_cookie;
58         int c2h_vdev_db;
59 };
60
61 static struct mic_irq *virtio_config_cookie;
62 #define to_micvdev(vd) container_of(vd, struct mic_vdev, vdev)
63
64 /* Helper API to obtain the parent of the virtio device */
65 static inline struct device *mic_dev(struct mic_vdev *mvdev)
66 {
67         return mvdev->vdev.dev.parent;
68 }
69
70 /* This gets the device's feature bits. */
71 static u64 mic_get_features(struct virtio_device *vdev)
72 {
73         unsigned int i, bits;
74         u32 features = 0;
75         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
76         u8 __iomem *in_features = mic_vq_features(desc);
77         int feature_len = ioread8(&desc->feature_len);
78
79         bits = min_t(unsigned, feature_len, sizeof(features)) * 8;
80         for (i = 0; i < bits; i++)
81                 if (ioread8(&in_features[i / 8]) & (BIT(i % 8)))
82                         features |= BIT(i);
83
84         return features;
85 }
86
87 static void mic_finalize_features(struct virtio_device *vdev)
88 {
89         unsigned int i, bits;
90         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
91         u8 feature_len = ioread8(&desc->feature_len);
92         /* Second half of bitmap is features we accept. */
93         u8 __iomem *out_features =
94                 mic_vq_features(desc) + feature_len;
95
96         /* Give virtio_ring a chance to accept features. */
97         vring_transport_features(vdev);
98
99         /* Make sure we don't have any features > 32 bits! */
100         BUG_ON((u32)vdev->features != vdev->features);
101
102         memset_io(out_features, 0, feature_len);
103         bits = min_t(unsigned, feature_len,
104                 sizeof(vdev->features)) * 8;
105         for (i = 0; i < bits; i++) {
106                 if (__virtio_test_bit(vdev, i))
107                         iowrite8(ioread8(&out_features[i / 8]) | (1 << (i % 8)),
108                                  &out_features[i / 8]);
109         }
110 }
111
112 /*
113  * Reading and writing elements in config space
114  */
115 static void mic_get(struct virtio_device *vdev, unsigned int offset,
116                    void *buf, unsigned len)
117 {
118         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
119
120         if (offset + len > ioread8(&desc->config_len))
121                 return;
122         memcpy_fromio(buf, mic_vq_configspace(desc) + offset, len);
123 }
124
125 static void mic_set(struct virtio_device *vdev, unsigned int offset,
126                    const void *buf, unsigned len)
127 {
128         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
129
130         if (offset + len > ioread8(&desc->config_len))
131                 return;
132         memcpy_toio(mic_vq_configspace(desc) + offset, buf, len);
133 }
134
135 /*
136  * The operations to get and set the status word just access the status
137  * field of the device descriptor. set_status also interrupts the host
138  * to tell about status changes.
139  */
140 static u8 mic_get_status(struct virtio_device *vdev)
141 {
142         return ioread8(&to_micvdev(vdev)->desc->status);
143 }
144
145 static void mic_set_status(struct virtio_device *vdev, u8 status)
146 {
147         struct mic_vdev *mvdev = to_micvdev(vdev);
148         if (!status)
149                 return;
150         iowrite8(status, &mvdev->desc->status);
151         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
152 }
153
154 /* Inform host on a virtio device reset and wait for ack from host */
155 static void mic_reset_inform_host(struct virtio_device *vdev)
156 {
157         struct mic_vdev *mvdev = to_micvdev(vdev);
158         struct mic_device_ctrl __iomem *dc = mvdev->dc;
159         int retry;
160
161         iowrite8(0, &dc->host_ack);
162         iowrite8(1, &dc->vdev_reset);
163         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
164
165         /* Wait till host completes all card accesses and acks the reset */
166         for (retry = 100; retry--;) {
167                 if (ioread8(&dc->host_ack))
168                         break;
169                 msleep(100);
170         };
171
172         dev_dbg(mic_dev(mvdev), "%s: retry: %d\n", __func__, retry);
173
174         /* Reset status to 0 in case we timed out */
175         iowrite8(0, &mvdev->desc->status);
176 }
177
178 static void mic_reset(struct virtio_device *vdev)
179 {
180         struct mic_vdev *mvdev = to_micvdev(vdev);
181
182         dev_dbg(mic_dev(mvdev), "%s: virtio id %d\n",
183                 __func__, vdev->id.device);
184
185         mic_reset_inform_host(vdev);
186         complete_all(&mvdev->reset_done);
187 }
188
189 /*
190  * The virtio_ring code calls this API when it wants to notify the Host.
191  */
192 static bool mic_notify(struct virtqueue *vq)
193 {
194         struct mic_vdev *mvdev = vq->priv;
195
196         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
197         return true;
198 }
199
200 static void mic_del_vq(struct virtqueue *vq, int n)
201 {
202         struct mic_vdev *mvdev = to_micvdev(vq->vdev);
203         struct vring *vr = (struct vring *)(vq + 1);
204
205         free_pages((unsigned long) vr->used, get_order(mvdev->used_size[n]));
206         vring_del_virtqueue(vq);
207         mic_card_unmap(mvdev->mdev, mvdev->vr[n]);
208         mvdev->vr[n] = NULL;
209 }
210
211 static void mic_del_vqs(struct virtio_device *vdev)
212 {
213         struct mic_vdev *mvdev = to_micvdev(vdev);
214         struct virtqueue *vq, *n;
215         int idx = 0;
216
217         dev_dbg(mic_dev(mvdev), "%s\n", __func__);
218
219         list_for_each_entry_safe(vq, n, &vdev->vqs, list)
220                 mic_del_vq(vq, idx++);
221 }
222
223 /*
224  * This routine will assign vring's allocated in host/io memory. Code in
225  * virtio_ring.c however continues to access this io memory as if it were local
226  * memory without io accessors.
227  */
228 static struct virtqueue *mic_find_vq(struct virtio_device *vdev,
229                                      unsigned index,
230                                      void (*callback)(struct virtqueue *vq),
231                                      const char *name)
232 {
233         struct mic_vdev *mvdev = to_micvdev(vdev);
234         struct mic_vqconfig __iomem *vqconfig;
235         struct mic_vqconfig config;
236         struct virtqueue *vq;
237         void __iomem *va;
238         struct _mic_vring_info __iomem *info;
239         void *used;
240         int vr_size, _vr_size, err, magic;
241         struct vring *vr;
242         u8 type = ioread8(&mvdev->desc->type);
243
244         if (index >= ioread8(&mvdev->desc->num_vq))
245                 return ERR_PTR(-ENOENT);
246
247         if (!name)
248                 return ERR_PTR(-ENOENT);
249
250         /* First assign the vring's allocated in host memory */
251         vqconfig = mic_vq_config(mvdev->desc) + index;
252         memcpy_fromio(&config, vqconfig, sizeof(config));
253         _vr_size = vring_size(le16_to_cpu(config.num), MIC_VIRTIO_RING_ALIGN);
254         vr_size = PAGE_ALIGN(_vr_size + sizeof(struct _mic_vring_info));
255         va = mic_card_map(mvdev->mdev, le64_to_cpu(config.address), vr_size);
256         if (!va)
257                 return ERR_PTR(-ENOMEM);
258         mvdev->vr[index] = va;
259         memset_io(va, 0x0, _vr_size);
260         vq = vring_new_virtqueue(index, le16_to_cpu(config.num),
261                                  MIC_VIRTIO_RING_ALIGN, vdev, false,
262                                  (void __force *)va, mic_notify, callback,
263                                  name);
264         if (!vq) {
265                 err = -ENOMEM;
266                 goto unmap;
267         }
268         info = va + _vr_size;
269         magic = ioread32(&info->magic);
270
271         if (WARN(magic != MIC_MAGIC + type + index, "magic mismatch")) {
272                 err = -EIO;
273                 goto unmap;
274         }
275
276         /* Allocate and reassign used ring now */
277         mvdev->used_size[index] = PAGE_ALIGN(sizeof(__u16) * 3 +
278                                              sizeof(struct vring_used_elem) *
279                                              le16_to_cpu(config.num));
280         used = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
281                                         get_order(mvdev->used_size[index]));
282         if (!used) {
283                 err = -ENOMEM;
284                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
285                         __func__, __LINE__, err);
286                 goto del_vq;
287         }
288         iowrite64(virt_to_phys(used), &vqconfig->used_address);
289
290         /*
291          * To reassign the used ring here we are directly accessing
292          * struct vring_virtqueue which is a private data structure
293          * in virtio_ring.c. At the minimum, a BUILD_BUG_ON() in
294          * vring_new_virtqueue() would ensure that
295          *  (&vq->vring == (struct vring *) (&vq->vq + 1));
296          */
297         vr = (struct vring *)(vq + 1);
298         vr->used = used;
299
300         vq->priv = mvdev;
301         return vq;
302 del_vq:
303         vring_del_virtqueue(vq);
304 unmap:
305         mic_card_unmap(mvdev->mdev, mvdev->vr[index]);
306         return ERR_PTR(err);
307 }
308
309 static int mic_find_vqs(struct virtio_device *vdev, unsigned nvqs,
310                         struct virtqueue *vqs[],
311                         vq_callback_t *callbacks[],
312                         const char *names[])
313 {
314         struct mic_vdev *mvdev = to_micvdev(vdev);
315         struct mic_device_ctrl __iomem *dc = mvdev->dc;
316         int i, err, retry;
317
318         /* We must have this many virtqueues. */
319         if (nvqs > ioread8(&mvdev->desc->num_vq))
320                 return -ENOENT;
321
322         for (i = 0; i < nvqs; ++i) {
323                 dev_dbg(mic_dev(mvdev), "%s: %d: %s\n",
324                         __func__, i, names[i]);
325                 vqs[i] = mic_find_vq(vdev, i, callbacks[i], names[i]);
326                 if (IS_ERR(vqs[i])) {
327                         err = PTR_ERR(vqs[i]);
328                         goto error;
329                 }
330         }
331
332         iowrite8(1, &dc->used_address_updated);
333         /*
334          * Send an interrupt to the host to inform it that used
335          * rings have been re-assigned.
336          */
337         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
338         for (retry = 100; retry--;) {
339                 if (!ioread8(&dc->used_address_updated))
340                         break;
341                 msleep(100);
342         };
343
344         dev_dbg(mic_dev(mvdev), "%s: retry: %d\n", __func__, retry);
345         if (!retry) {
346                 err = -ENODEV;
347                 goto error;
348         }
349
350         return 0;
351 error:
352         mic_del_vqs(vdev);
353         return err;
354 }
355
356 /*
357  * The config ops structure as defined by virtio config
358  */
359 static struct virtio_config_ops mic_vq_config_ops = {
360         .get_features = mic_get_features,
361         .finalize_features = mic_finalize_features,
362         .get = mic_get,
363         .set = mic_set,
364         .get_status = mic_get_status,
365         .set_status = mic_set_status,
366         .reset = mic_reset,
367         .find_vqs = mic_find_vqs,
368         .del_vqs = mic_del_vqs,
369 };
370
371 static irqreturn_t
372 mic_virtio_intr_handler(int irq, void *data)
373 {
374         struct mic_vdev *mvdev = data;
375         struct virtqueue *vq;
376
377         mic_ack_interrupt(mvdev->mdev);
378         list_for_each_entry(vq, &mvdev->vdev.vqs, list)
379                 vring_interrupt(0, vq);
380
381         return IRQ_HANDLED;
382 }
383
384 static void mic_virtio_release_dev(struct device *_d)
385 {
386         /*
387          * No need for a release method similar to virtio PCI.
388          * Provide an empty one to avoid getting a warning from core.
389          */
390 }
391
392 /*
393  * adds a new device and register it with virtio
394  * appropriate drivers are loaded by the device model
395  */
396 static int mic_add_device(struct mic_device_desc __iomem *d,
397         unsigned int offset, struct mic_driver *mdrv)
398 {
399         struct mic_vdev *mvdev;
400         int ret;
401         int virtio_db;
402         u8 type = ioread8(&d->type);
403
404         mvdev = kzalloc(sizeof(*mvdev), GFP_KERNEL);
405         if (!mvdev) {
406                 dev_err(mdrv->dev, "Cannot allocate mic dev %u type %u\n",
407                         offset, type);
408                 return -ENOMEM;
409         }
410
411         mvdev->mdev = &mdrv->mdev;
412         mvdev->vdev.dev.parent = mdrv->dev;
413         mvdev->vdev.dev.release = mic_virtio_release_dev;
414         mvdev->vdev.id.device = type;
415         mvdev->vdev.config = &mic_vq_config_ops;
416         mvdev->desc = d;
417         mvdev->dc = (void __iomem *)d + mic_aligned_desc_size(d);
418         init_completion(&mvdev->reset_done);
419
420         virtio_db = mic_next_card_db();
421         mvdev->virtio_cookie = mic_request_card_irq(mic_virtio_intr_handler,
422                         NULL, "virtio intr", mvdev, virtio_db);
423         if (IS_ERR(mvdev->virtio_cookie)) {
424                 ret = PTR_ERR(mvdev->virtio_cookie);
425                 goto kfree;
426         }
427         iowrite8((u8)virtio_db, &mvdev->dc->h2c_vdev_db);
428         mvdev->c2h_vdev_db = ioread8(&mvdev->dc->c2h_vdev_db);
429
430         ret = register_virtio_device(&mvdev->vdev);
431         if (ret) {
432                 dev_err(mic_dev(mvdev),
433                         "Failed to register mic device %u type %u\n",
434                         offset, type);
435                 goto free_irq;
436         }
437         iowrite64((u64)mvdev, &mvdev->dc->vdev);
438         dev_dbg(mic_dev(mvdev), "%s: registered mic device %u type %u mvdev %p\n",
439                 __func__, offset, type, mvdev);
440
441         return 0;
442
443 free_irq:
444         mic_free_card_irq(mvdev->virtio_cookie, mvdev);
445 kfree:
446         kfree(mvdev);
447         return ret;
448 }
449
450 /*
451  * match for a mic device with a specific desc pointer
452  */
453 static int mic_match_desc(struct device *dev, void *data)
454 {
455         struct virtio_device *vdev = dev_to_virtio(dev);
456         struct mic_vdev *mvdev = to_micvdev(vdev);
457
458         return mvdev->desc == (void __iomem *)data;
459 }
460
461 static void mic_handle_config_change(struct mic_device_desc __iomem *d,
462         unsigned int offset, struct mic_driver *mdrv)
463 {
464         struct mic_device_ctrl __iomem *dc
465                 = (void __iomem *)d + mic_aligned_desc_size(d);
466         struct mic_vdev *mvdev = (struct mic_vdev *)ioread64(&dc->vdev);
467
468         if (ioread8(&dc->config_change) != MIC_VIRTIO_PARAM_CONFIG_CHANGED)
469                 return;
470
471         dev_dbg(mdrv->dev, "%s %d\n", __func__, __LINE__);
472         virtio_config_changed(&mvdev->vdev);
473         iowrite8(1, &dc->guest_ack);
474 }
475
476 /*
477  * removes a virtio device if a hot remove event has been
478  * requested by the host.
479  */
480 static int mic_remove_device(struct mic_device_desc __iomem *d,
481         unsigned int offset, struct mic_driver *mdrv)
482 {
483         struct mic_device_ctrl __iomem *dc
484                 = (void __iomem *)d + mic_aligned_desc_size(d);
485         struct mic_vdev *mvdev = (struct mic_vdev *)ioread64(&dc->vdev);
486         u8 status;
487         int ret = -1;
488
489         if (ioread8(&dc->config_change) == MIC_VIRTIO_PARAM_DEV_REMOVE) {
490                 dev_dbg(mdrv->dev,
491                         "%s %d config_change %d type %d mvdev %p\n",
492                         __func__, __LINE__,
493                         ioread8(&dc->config_change), ioread8(&d->type), mvdev);
494
495                 status = ioread8(&d->status);
496                 reinit_completion(&mvdev->reset_done);
497                 unregister_virtio_device(&mvdev->vdev);
498                 mic_free_card_irq(mvdev->virtio_cookie, mvdev);
499                 if (status & VIRTIO_CONFIG_S_DRIVER_OK)
500                         wait_for_completion(&mvdev->reset_done);
501                 kfree(mvdev);
502                 iowrite8(1, &dc->guest_ack);
503                 dev_dbg(mdrv->dev, "%s %d guest_ack %d\n",
504                         __func__, __LINE__, ioread8(&dc->guest_ack));
505                 ret = 0;
506         }
507
508         return ret;
509 }
510
511 #define REMOVE_DEVICES true
512
513 static void mic_scan_devices(struct mic_driver *mdrv, bool remove)
514 {
515         s8 type;
516         unsigned int i;
517         struct mic_device_desc __iomem *d;
518         struct mic_device_ctrl __iomem *dc;
519         struct device *dev;
520         int ret;
521
522         for (i = sizeof(struct mic_bootparam); i < MIC_DP_SIZE;
523                 i += mic_total_desc_size(d)) {
524                 d = mdrv->dp + i;
525                 dc = (void __iomem *)d + mic_aligned_desc_size(d);
526                 /*
527                  * This read barrier is paired with the corresponding write
528                  * barrier on the host which is inserted before adding or
529                  * removing a virtio device descriptor, by updating the type.
530                  */
531                 rmb();
532                 type = ioread8(&d->type);
533
534                 /* end of list */
535                 if (type == 0)
536                         break;
537
538                 if (type == -1)
539                         continue;
540
541                 /* device already exists */
542                 dev = device_find_child(mdrv->dev, (void __force *)d,
543                                         mic_match_desc);
544                 if (dev) {
545                         if (remove)
546                                 iowrite8(MIC_VIRTIO_PARAM_DEV_REMOVE,
547                                          &dc->config_change);
548                         put_device(dev);
549                         mic_handle_config_change(d, i, mdrv);
550                         ret = mic_remove_device(d, i, mdrv);
551                         if (!ret && !remove)
552                                 iowrite8(-1, &d->type);
553                         if (remove) {
554                                 iowrite8(0, &dc->config_change);
555                                 iowrite8(0, &dc->guest_ack);
556                         }
557                         continue;
558                 }
559
560                 /* new device */
561                 dev_dbg(mdrv->dev, "%s %d Adding new virtio device %p\n",
562                         __func__, __LINE__, d);
563                 if (!remove)
564                         mic_add_device(d, i, mdrv);
565         }
566 }
567
568 /*
569  * mic_hotplug_device tries to find changes in the device page.
570  */
571 static void mic_hotplug_devices(struct work_struct *work)
572 {
573         struct mic_driver *mdrv = container_of(work,
574                 struct mic_driver, hotplug_work);
575
576         mic_scan_devices(mdrv, !REMOVE_DEVICES);
577 }
578
579 /*
580  * Interrupt handler for hot plug/config changes etc.
581  */
582 static irqreturn_t
583 mic_extint_handler(int irq, void *data)
584 {
585         struct mic_driver *mdrv = (struct mic_driver *)data;
586
587         dev_dbg(mdrv->dev, "%s %d hotplug work\n",
588                 __func__, __LINE__);
589         mic_ack_interrupt(&mdrv->mdev);
590         schedule_work(&mdrv->hotplug_work);
591         return IRQ_HANDLED;
592 }
593
594 /*
595  * Init function for virtio
596  */
597 int mic_devices_init(struct mic_driver *mdrv)
598 {
599         int rc;
600         struct mic_bootparam __iomem *bootparam;
601         int config_db;
602
603         INIT_WORK(&mdrv->hotplug_work, mic_hotplug_devices);
604         mic_scan_devices(mdrv, !REMOVE_DEVICES);
605
606         config_db = mic_next_card_db();
607         virtio_config_cookie = mic_request_card_irq(mic_extint_handler, NULL,
608                                                     "virtio_config_intr", mdrv,
609                                                     config_db);
610         if (IS_ERR(virtio_config_cookie)) {
611                 rc = PTR_ERR(virtio_config_cookie);
612                 goto exit;
613         }
614
615         bootparam = mdrv->dp;
616         iowrite8(config_db, &bootparam->h2c_config_db);
617         return 0;
618 exit:
619         return rc;
620 }
621
622 /*
623  * Uninit function for virtio
624  */
625 void mic_devices_uninit(struct mic_driver *mdrv)
626 {
627         struct mic_bootparam __iomem *bootparam = mdrv->dp;
628         iowrite8(-1, &bootparam->h2c_config_db);
629         mic_free_card_irq(virtio_config_cookie, mdrv);
630         flush_work(&mdrv->hotplug_work);
631         mic_scan_devices(mdrv, REMOVE_DEVICES);
632 }