Merge tag 'arc-v3.13-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-drm-fsl-dcu.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 u32 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,
80                 sizeof(vdev->features)) * 8;
81         for (i = 0; i < bits; i++)
82                 if (ioread8(&in_features[i / 8]) & (BIT(i % 8)))
83                         features |= BIT(i);
84
85         return features;
86 }
87
88 static void mic_finalize_features(struct virtio_device *vdev)
89 {
90         unsigned int i, bits;
91         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
92         u8 feature_len = ioread8(&desc->feature_len);
93         /* Second half of bitmap is features we accept. */
94         u8 __iomem *out_features =
95                 mic_vq_features(desc) + feature_len;
96
97         /* Give virtio_ring a chance to accept features. */
98         vring_transport_features(vdev);
99
100         memset_io(out_features, 0, feature_len);
101         bits = min_t(unsigned, feature_len,
102                 sizeof(vdev->features)) * 8;
103         for (i = 0; i < bits; i++) {
104                 if (test_bit(i, vdev->features))
105                         iowrite8(ioread8(&out_features[i / 8]) | (1 << (i % 8)),
106                                  &out_features[i / 8]);
107         }
108 }
109
110 /*
111  * Reading and writing elements in config space
112  */
113 static void mic_get(struct virtio_device *vdev, unsigned int offset,
114                    void *buf, unsigned len)
115 {
116         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
117
118         if (offset + len > ioread8(&desc->config_len))
119                 return;
120         memcpy_fromio(buf, mic_vq_configspace(desc) + offset, len);
121 }
122
123 static void mic_set(struct virtio_device *vdev, unsigned int offset,
124                    const void *buf, unsigned len)
125 {
126         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
127
128         if (offset + len > ioread8(&desc->config_len))
129                 return;
130         memcpy_toio(mic_vq_configspace(desc) + offset, buf, len);
131 }
132
133 /*
134  * The operations to get and set the status word just access the status
135  * field of the device descriptor. set_status also interrupts the host
136  * to tell about status changes.
137  */
138 static u8 mic_get_status(struct virtio_device *vdev)
139 {
140         return ioread8(&to_micvdev(vdev)->desc->status);
141 }
142
143 static void mic_set_status(struct virtio_device *vdev, u8 status)
144 {
145         struct mic_vdev *mvdev = to_micvdev(vdev);
146         if (!status)
147                 return;
148         iowrite8(status, &mvdev->desc->status);
149         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
150 }
151
152 /* Inform host on a virtio device reset and wait for ack from host */
153 static void mic_reset_inform_host(struct virtio_device *vdev)
154 {
155         struct mic_vdev *mvdev = to_micvdev(vdev);
156         struct mic_device_ctrl __iomem *dc = mvdev->dc;
157         int retry = 100, i;
158
159         iowrite8(0, &dc->host_ack);
160         iowrite8(1, &dc->vdev_reset);
161         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
162
163         /* Wait till host completes all card accesses and acks the reset */
164         for (i = retry; i--;) {
165                 if (ioread8(&dc->host_ack))
166                         break;
167                 msleep(100);
168         };
169
170         dev_dbg(mic_dev(mvdev), "%s: retry: %d\n", __func__, retry);
171
172         /* Reset status to 0 in case we timed out */
173         iowrite8(0, &mvdev->desc->status);
174 }
175
176 static void mic_reset(struct virtio_device *vdev)
177 {
178         struct mic_vdev *mvdev = to_micvdev(vdev);
179
180         dev_dbg(mic_dev(mvdev), "%s: virtio id %d\n",
181                 __func__, vdev->id.device);
182
183         mic_reset_inform_host(vdev);
184         complete_all(&mvdev->reset_done);
185 }
186
187 /*
188  * The virtio_ring code calls this API when it wants to notify the Host.
189  */
190 static void mic_notify(struct virtqueue *vq)
191 {
192         struct mic_vdev *mvdev = vq->priv;
193
194         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
195 }
196
197 static void mic_del_vq(struct virtqueue *vq, int n)
198 {
199         struct mic_vdev *mvdev = to_micvdev(vq->vdev);
200         struct vring *vr = (struct vring *)(vq + 1);
201
202         free_pages((unsigned long) vr->used, get_order(mvdev->used_size[n]));
203         vring_del_virtqueue(vq);
204         mic_card_unmap(mvdev->mdev, mvdev->vr[n]);
205         mvdev->vr[n] = NULL;
206 }
207
208 static void mic_del_vqs(struct virtio_device *vdev)
209 {
210         struct mic_vdev *mvdev = to_micvdev(vdev);
211         struct virtqueue *vq, *n;
212         int idx = 0;
213
214         dev_dbg(mic_dev(mvdev), "%s\n", __func__);
215
216         list_for_each_entry_safe(vq, n, &vdev->vqs, list)
217                 mic_del_vq(vq, idx++);
218 }
219
220 /*
221  * This routine will assign vring's allocated in host/io memory. Code in
222  * virtio_ring.c however continues to access this io memory as if it were local
223  * memory without io accessors.
224  */
225 static struct virtqueue *mic_find_vq(struct virtio_device *vdev,
226                                      unsigned index,
227                                      void (*callback)(struct virtqueue *vq),
228                                      const char *name)
229 {
230         struct mic_vdev *mvdev = to_micvdev(vdev);
231         struct mic_vqconfig __iomem *vqconfig;
232         struct mic_vqconfig config;
233         struct virtqueue *vq;
234         void __iomem *va;
235         struct _mic_vring_info __iomem *info;
236         void *used;
237         int vr_size, _vr_size, err, magic;
238         struct vring *vr;
239         u8 type = ioread8(&mvdev->desc->type);
240
241         if (index >= ioread8(&mvdev->desc->num_vq))
242                 return ERR_PTR(-ENOENT);
243
244         if (!name)
245                 return ERR_PTR(-ENOENT);
246
247         /* First assign the vring's allocated in host memory */
248         vqconfig = mic_vq_config(mvdev->desc) + index;
249         memcpy_fromio(&config, vqconfig, sizeof(config));
250         _vr_size = vring_size(config.num, MIC_VIRTIO_RING_ALIGN);
251         vr_size = PAGE_ALIGN(_vr_size + sizeof(struct _mic_vring_info));
252         va = mic_card_map(mvdev->mdev, config.address, vr_size);
253         if (!va)
254                 return ERR_PTR(-ENOMEM);
255         mvdev->vr[index] = va;
256         memset_io(va, 0x0, _vr_size);
257         vq = vring_new_virtqueue(index,
258                                 config.num, MIC_VIRTIO_RING_ALIGN, vdev,
259                                 false,
260                                 va, mic_notify, callback, name);
261         if (!vq) {
262                 err = -ENOMEM;
263                 goto unmap;
264         }
265         info = va + _vr_size;
266         magic = ioread32(&info->magic);
267
268         if (WARN(magic != MIC_MAGIC + type + index, "magic mismatch")) {
269                 err = -EIO;
270                 goto unmap;
271         }
272
273         /* Allocate and reassign used ring now */
274         mvdev->used_size[index] = PAGE_ALIGN(sizeof(__u16) * 3 +
275                         sizeof(struct vring_used_elem) * config.num);
276         used = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
277                                         get_order(mvdev->used_size[index]));
278         if (!used) {
279                 err = -ENOMEM;
280                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
281                         __func__, __LINE__, err);
282                 goto del_vq;
283         }
284         iowrite64(virt_to_phys(used), &vqconfig->used_address);
285
286         /*
287          * To reassign the used ring here we are directly accessing
288          * struct vring_virtqueue which is a private data structure
289          * in virtio_ring.c. At the minimum, a BUILD_BUG_ON() in
290          * vring_new_virtqueue() would ensure that
291          *  (&vq->vring == (struct vring *) (&vq->vq + 1));
292          */
293         vr = (struct vring *)(vq + 1);
294         vr->used = used;
295
296         vq->priv = mvdev;
297         return vq;
298 del_vq:
299         vring_del_virtqueue(vq);
300 unmap:
301         mic_card_unmap(mvdev->mdev, mvdev->vr[index]);
302         return ERR_PTR(err);
303 }
304
305 static int mic_find_vqs(struct virtio_device *vdev, unsigned nvqs,
306                         struct virtqueue *vqs[],
307                         vq_callback_t *callbacks[],
308                         const char *names[])
309 {
310         struct mic_vdev *mvdev = to_micvdev(vdev);
311         struct mic_device_ctrl __iomem *dc = mvdev->dc;
312         int i, err, retry = 100;
313
314         /* We must have this many virtqueues. */
315         if (nvqs > ioread8(&mvdev->desc->num_vq))
316                 return -ENOENT;
317
318         for (i = 0; i < nvqs; ++i) {
319                 dev_dbg(mic_dev(mvdev), "%s: %d: %s\n",
320                         __func__, i, names[i]);
321                 vqs[i] = mic_find_vq(vdev, i, callbacks[i], names[i]);
322                 if (IS_ERR(vqs[i])) {
323                         err = PTR_ERR(vqs[i]);
324                         goto error;
325                 }
326         }
327
328         iowrite8(1, &dc->used_address_updated);
329         /*
330          * Send an interrupt to the host to inform it that used
331          * rings have been re-assigned.
332          */
333         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
334         for (i = retry; i--;) {
335                 if (!ioread8(&dc->used_address_updated))
336                         break;
337                 msleep(100);
338         };
339
340         dev_dbg(mic_dev(mvdev), "%s: retry: %d\n", __func__, retry);
341         if (!retry) {
342                 err = -ENODEV;
343                 goto error;
344         }
345
346         return 0;
347 error:
348         mic_del_vqs(vdev);
349         return err;
350 }
351
352 /*
353  * The config ops structure as defined by virtio config
354  */
355 static struct virtio_config_ops mic_vq_config_ops = {
356         .get_features = mic_get_features,
357         .finalize_features = mic_finalize_features,
358         .get = mic_get,
359         .set = mic_set,
360         .get_status = mic_get_status,
361         .set_status = mic_set_status,
362         .reset = mic_reset,
363         .find_vqs = mic_find_vqs,
364         .del_vqs = mic_del_vqs,
365 };
366
367 static irqreturn_t
368 mic_virtio_intr_handler(int irq, void *data)
369 {
370         struct mic_vdev *mvdev = data;
371         struct virtqueue *vq;
372
373         mic_ack_interrupt(mvdev->mdev);
374         list_for_each_entry(vq, &mvdev->vdev.vqs, list)
375                 vring_interrupt(0, vq);
376
377         return IRQ_HANDLED;
378 }
379
380 static void mic_virtio_release_dev(struct device *_d)
381 {
382         /*
383          * No need for a release method similar to virtio PCI.
384          * Provide an empty one to avoid getting a warning from core.
385          */
386 }
387
388 /*
389  * adds a new device and register it with virtio
390  * appropriate drivers are loaded by the device model
391  */
392 static int mic_add_device(struct mic_device_desc __iomem *d,
393         unsigned int offset, struct mic_driver *mdrv)
394 {
395         struct mic_vdev *mvdev;
396         int ret;
397         int virtio_db;
398         u8 type = ioread8(&d->type);
399
400         mvdev = kzalloc(sizeof(*mvdev), GFP_KERNEL);
401         if (!mvdev) {
402                 dev_err(mdrv->dev, "Cannot allocate mic dev %u type %u\n",
403                         offset, type);
404                 return -ENOMEM;
405         }
406
407         mvdev->mdev = &mdrv->mdev;
408         mvdev->vdev.dev.parent = mdrv->dev;
409         mvdev->vdev.dev.release = mic_virtio_release_dev;
410         mvdev->vdev.id.device = type;
411         mvdev->vdev.config = &mic_vq_config_ops;
412         mvdev->desc = d;
413         mvdev->dc = (void __iomem *)d + mic_aligned_desc_size(d);
414         init_completion(&mvdev->reset_done);
415
416         virtio_db = mic_next_card_db();
417         mvdev->virtio_cookie = mic_request_card_irq(mic_virtio_intr_handler,
418                         "virtio intr", mvdev, virtio_db);
419         if (IS_ERR(mvdev->virtio_cookie)) {
420                 ret = PTR_ERR(mvdev->virtio_cookie);
421                 goto kfree;
422         }
423         iowrite8((u8)virtio_db, &mvdev->dc->h2c_vdev_db);
424         mvdev->c2h_vdev_db = ioread8(&mvdev->dc->c2h_vdev_db);
425
426         ret = register_virtio_device(&mvdev->vdev);
427         if (ret) {
428                 dev_err(mic_dev(mvdev),
429                         "Failed to register mic device %u type %u\n",
430                         offset, type);
431                 goto free_irq;
432         }
433         iowrite64((u64)mvdev, &mvdev->dc->vdev);
434         dev_dbg(mic_dev(mvdev), "%s: registered mic device %u type %u mvdev %p\n",
435                 __func__, offset, type, mvdev);
436
437         return 0;
438
439 free_irq:
440         mic_free_card_irq(mvdev->virtio_cookie, mvdev);
441 kfree:
442         kfree(mvdev);
443         return ret;
444 }
445
446 /*
447  * match for a mic device with a specific desc pointer
448  */
449 static int mic_match_desc(struct device *dev, void *data)
450 {
451         struct virtio_device *vdev = dev_to_virtio(dev);
452         struct mic_vdev *mvdev = to_micvdev(vdev);
453
454         return mvdev->desc == (void __iomem *)data;
455 }
456
457 static void mic_handle_config_change(struct mic_device_desc __iomem *d,
458         unsigned int offset, struct mic_driver *mdrv)
459 {
460         struct mic_device_ctrl __iomem *dc
461                 = (void __iomem *)d + mic_aligned_desc_size(d);
462         struct mic_vdev *mvdev = (struct mic_vdev *)ioread64(&dc->vdev);
463         struct virtio_driver *drv;
464
465         if (ioread8(&dc->config_change) != MIC_VIRTIO_PARAM_CONFIG_CHANGED)
466                 return;
467
468         dev_dbg(mdrv->dev, "%s %d\n", __func__, __LINE__);
469         drv = container_of(mvdev->vdev.dev.driver,
470                                 struct virtio_driver, driver);
471         if (drv->config_changed)
472                 drv->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 = mic_aligned_size(struct mic_bootparam);
523                 i < MIC_DP_SIZE; 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, d, mic_match_desc);
543                 if (dev) {
544                         if (remove)
545                                 iowrite8(MIC_VIRTIO_PARAM_DEV_REMOVE,
546                                          &dc->config_change);
547                         put_device(dev);
548                         mic_handle_config_change(d, i, mdrv);
549                         ret = mic_remove_device(d, i, mdrv);
550                         if (!ret && !remove)
551                                 iowrite8(-1, &d->type);
552                         if (remove) {
553                                 iowrite8(0, &dc->config_change);
554                                 iowrite8(0, &dc->guest_ack);
555                         }
556                         continue;
557                 }
558
559                 /* new device */
560                 dev_dbg(mdrv->dev, "%s %d Adding new virtio device %p\n",
561                         __func__, __LINE__, d);
562                 if (!remove)
563                         mic_add_device(d, i, mdrv);
564         }
565 }
566
567 /*
568  * mic_hotplug_device tries to find changes in the device page.
569  */
570 static void mic_hotplug_devices(struct work_struct *work)
571 {
572         struct mic_driver *mdrv = container_of(work,
573                 struct mic_driver, hotplug_work);
574
575         mic_scan_devices(mdrv, !REMOVE_DEVICES);
576 }
577
578 /*
579  * Interrupt handler for hot plug/config changes etc.
580  */
581 static irqreturn_t
582 mic_extint_handler(int irq, void *data)
583 {
584         struct mic_driver *mdrv = (struct mic_driver *)data;
585
586         dev_dbg(mdrv->dev, "%s %d hotplug work\n",
587                 __func__, __LINE__);
588         mic_ack_interrupt(&mdrv->mdev);
589         schedule_work(&mdrv->hotplug_work);
590         return IRQ_HANDLED;
591 }
592
593 /*
594  * Init function for virtio
595  */
596 int mic_devices_init(struct mic_driver *mdrv)
597 {
598         int rc;
599         struct mic_bootparam __iomem *bootparam;
600         int config_db;
601
602         INIT_WORK(&mdrv->hotplug_work, mic_hotplug_devices);
603         mic_scan_devices(mdrv, !REMOVE_DEVICES);
604
605         config_db = mic_next_card_db();
606         virtio_config_cookie = mic_request_card_irq(mic_extint_handler,
607                         "virtio_config_intr", mdrv, config_db);
608         if (IS_ERR(virtio_config_cookie)) {
609                 rc = PTR_ERR(virtio_config_cookie);
610                 goto exit;
611         }
612
613         bootparam = mdrv->dp;
614         iowrite8(config_db, &bootparam->h2c_config_db);
615         return 0;
616 exit:
617         return rc;
618 }
619
620 /*
621  * Uninit function for virtio
622  */
623 void mic_devices_uninit(struct mic_driver *mdrv)
624 {
625         struct mic_bootparam __iomem *bootparam = mdrv->dp;
626         iowrite8(-1, &bootparam->h2c_config_db);
627         mic_free_card_irq(virtio_config_cookie, mdrv);
628         flush_work(&mdrv->hotplug_work);
629         mic_scan_devices(mdrv, REMOVE_DEVICES);
630 }