mic_virtio: robust feature array size calculation
[cascardo/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 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, 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         memset_io(out_features, 0, feature_len);
100         bits = min_t(unsigned, feature_len,
101                 sizeof(vdev->features)) * 8;
102         for (i = 0; i < bits; i++) {
103                 if (__virtio_test_bit(vdev, i))
104                         iowrite8(ioread8(&out_features[i / 8]) | (1 << (i % 8)),
105                                  &out_features[i / 8]);
106         }
107 }
108
109 /*
110  * Reading and writing elements in config space
111  */
112 static void mic_get(struct virtio_device *vdev, unsigned int offset,
113                    void *buf, unsigned len)
114 {
115         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
116
117         if (offset + len > ioread8(&desc->config_len))
118                 return;
119         memcpy_fromio(buf, mic_vq_configspace(desc) + offset, len);
120 }
121
122 static void mic_set(struct virtio_device *vdev, unsigned int offset,
123                    const void *buf, unsigned len)
124 {
125         struct mic_device_desc __iomem *desc = to_micvdev(vdev)->desc;
126
127         if (offset + len > ioread8(&desc->config_len))
128                 return;
129         memcpy_toio(mic_vq_configspace(desc) + offset, buf, len);
130 }
131
132 /*
133  * The operations to get and set the status word just access the status
134  * field of the device descriptor. set_status also interrupts the host
135  * to tell about status changes.
136  */
137 static u8 mic_get_status(struct virtio_device *vdev)
138 {
139         return ioread8(&to_micvdev(vdev)->desc->status);
140 }
141
142 static void mic_set_status(struct virtio_device *vdev, u8 status)
143 {
144         struct mic_vdev *mvdev = to_micvdev(vdev);
145         if (!status)
146                 return;
147         iowrite8(status, &mvdev->desc->status);
148         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
149 }
150
151 /* Inform host on a virtio device reset and wait for ack from host */
152 static void mic_reset_inform_host(struct virtio_device *vdev)
153 {
154         struct mic_vdev *mvdev = to_micvdev(vdev);
155         struct mic_device_ctrl __iomem *dc = mvdev->dc;
156         int retry;
157
158         iowrite8(0, &dc->host_ack);
159         iowrite8(1, &dc->vdev_reset);
160         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
161
162         /* Wait till host completes all card accesses and acks the reset */
163         for (retry = 100; retry--;) {
164                 if (ioread8(&dc->host_ack))
165                         break;
166                 msleep(100);
167         };
168
169         dev_dbg(mic_dev(mvdev), "%s: retry: %d\n", __func__, retry);
170
171         /* Reset status to 0 in case we timed out */
172         iowrite8(0, &mvdev->desc->status);
173 }
174
175 static void mic_reset(struct virtio_device *vdev)
176 {
177         struct mic_vdev *mvdev = to_micvdev(vdev);
178
179         dev_dbg(mic_dev(mvdev), "%s: virtio id %d\n",
180                 __func__, vdev->id.device);
181
182         mic_reset_inform_host(vdev);
183         complete_all(&mvdev->reset_done);
184 }
185
186 /*
187  * The virtio_ring code calls this API when it wants to notify the Host.
188  */
189 static bool mic_notify(struct virtqueue *vq)
190 {
191         struct mic_vdev *mvdev = vq->priv;
192
193         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
194         return true;
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(le16_to_cpu(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, le64_to_cpu(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, le16_to_cpu(config.num),
258                                  MIC_VIRTIO_RING_ALIGN, vdev, false,
259                                  (void __force *)va, mic_notify, callback,
260                                  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) *
276                                              le16_to_cpu(config.num));
277         used = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
278                                         get_order(mvdev->used_size[index]));
279         if (!used) {
280                 err = -ENOMEM;
281                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
282                         __func__, __LINE__, err);
283                 goto del_vq;
284         }
285         iowrite64(virt_to_phys(used), &vqconfig->used_address);
286
287         /*
288          * To reassign the used ring here we are directly accessing
289          * struct vring_virtqueue which is a private data structure
290          * in virtio_ring.c. At the minimum, a BUILD_BUG_ON() in
291          * vring_new_virtqueue() would ensure that
292          *  (&vq->vring == (struct vring *) (&vq->vq + 1));
293          */
294         vr = (struct vring *)(vq + 1);
295         vr->used = used;
296
297         vq->priv = mvdev;
298         return vq;
299 del_vq:
300         vring_del_virtqueue(vq);
301 unmap:
302         mic_card_unmap(mvdev->mdev, mvdev->vr[index]);
303         return ERR_PTR(err);
304 }
305
306 static int mic_find_vqs(struct virtio_device *vdev, unsigned nvqs,
307                         struct virtqueue *vqs[],
308                         vq_callback_t *callbacks[],
309                         const char *names[])
310 {
311         struct mic_vdev *mvdev = to_micvdev(vdev);
312         struct mic_device_ctrl __iomem *dc = mvdev->dc;
313         int i, err, retry;
314
315         /* We must have this many virtqueues. */
316         if (nvqs > ioread8(&mvdev->desc->num_vq))
317                 return -ENOENT;
318
319         for (i = 0; i < nvqs; ++i) {
320                 dev_dbg(mic_dev(mvdev), "%s: %d: %s\n",
321                         __func__, i, names[i]);
322                 vqs[i] = mic_find_vq(vdev, i, callbacks[i], names[i]);
323                 if (IS_ERR(vqs[i])) {
324                         err = PTR_ERR(vqs[i]);
325                         goto error;
326                 }
327         }
328
329         iowrite8(1, &dc->used_address_updated);
330         /*
331          * Send an interrupt to the host to inform it that used
332          * rings have been re-assigned.
333          */
334         mic_send_intr(mvdev->mdev, mvdev->c2h_vdev_db);
335         for (retry = 100; retry--;) {
336                 if (!ioread8(&dc->used_address_updated))
337                         break;
338                 msleep(100);
339         };
340
341         dev_dbg(mic_dev(mvdev), "%s: retry: %d\n", __func__, retry);
342         if (!retry) {
343                 err = -ENODEV;
344                 goto error;
345         }
346
347         return 0;
348 error:
349         mic_del_vqs(vdev);
350         return err;
351 }
352
353 /*
354  * The config ops structure as defined by virtio config
355  */
356 static struct virtio_config_ops mic_vq_config_ops = {
357         .get_features = mic_get_features,
358         .finalize_features = mic_finalize_features,
359         .get = mic_get,
360         .set = mic_set,
361         .get_status = mic_get_status,
362         .set_status = mic_set_status,
363         .reset = mic_reset,
364         .find_vqs = mic_find_vqs,
365         .del_vqs = mic_del_vqs,
366 };
367
368 static irqreturn_t
369 mic_virtio_intr_handler(int irq, void *data)
370 {
371         struct mic_vdev *mvdev = data;
372         struct virtqueue *vq;
373
374         mic_ack_interrupt(mvdev->mdev);
375         list_for_each_entry(vq, &mvdev->vdev.vqs, list)
376                 vring_interrupt(0, vq);
377
378         return IRQ_HANDLED;
379 }
380
381 static void mic_virtio_release_dev(struct device *_d)
382 {
383         /*
384          * No need for a release method similar to virtio PCI.
385          * Provide an empty one to avoid getting a warning from core.
386          */
387 }
388
389 /*
390  * adds a new device and register it with virtio
391  * appropriate drivers are loaded by the device model
392  */
393 static int mic_add_device(struct mic_device_desc __iomem *d,
394         unsigned int offset, struct mic_driver *mdrv)
395 {
396         struct mic_vdev *mvdev;
397         int ret;
398         int virtio_db;
399         u8 type = ioread8(&d->type);
400
401         mvdev = kzalloc(sizeof(*mvdev), GFP_KERNEL);
402         if (!mvdev) {
403                 dev_err(mdrv->dev, "Cannot allocate mic dev %u type %u\n",
404                         offset, type);
405                 return -ENOMEM;
406         }
407
408         mvdev->mdev = &mdrv->mdev;
409         mvdev->vdev.dev.parent = mdrv->dev;
410         mvdev->vdev.dev.release = mic_virtio_release_dev;
411         mvdev->vdev.id.device = type;
412         mvdev->vdev.config = &mic_vq_config_ops;
413         mvdev->desc = d;
414         mvdev->dc = (void __iomem *)d + mic_aligned_desc_size(d);
415         init_completion(&mvdev->reset_done);
416
417         virtio_db = mic_next_card_db();
418         mvdev->virtio_cookie = mic_request_card_irq(mic_virtio_intr_handler,
419                         NULL, "virtio intr", mvdev, virtio_db);
420         if (IS_ERR(mvdev->virtio_cookie)) {
421                 ret = PTR_ERR(mvdev->virtio_cookie);
422                 goto kfree;
423         }
424         iowrite8((u8)virtio_db, &mvdev->dc->h2c_vdev_db);
425         mvdev->c2h_vdev_db = ioread8(&mvdev->dc->c2h_vdev_db);
426
427         ret = register_virtio_device(&mvdev->vdev);
428         if (ret) {
429                 dev_err(mic_dev(mvdev),
430                         "Failed to register mic device %u type %u\n",
431                         offset, type);
432                 goto free_irq;
433         }
434         iowrite64((u64)mvdev, &mvdev->dc->vdev);
435         dev_dbg(mic_dev(mvdev), "%s: registered mic device %u type %u mvdev %p\n",
436                 __func__, offset, type, mvdev);
437
438         return 0;
439
440 free_irq:
441         mic_free_card_irq(mvdev->virtio_cookie, mvdev);
442 kfree:
443         kfree(mvdev);
444         return ret;
445 }
446
447 /*
448  * match for a mic device with a specific desc pointer
449  */
450 static int mic_match_desc(struct device *dev, void *data)
451 {
452         struct virtio_device *vdev = dev_to_virtio(dev);
453         struct mic_vdev *mvdev = to_micvdev(vdev);
454
455         return mvdev->desc == (void __iomem *)data;
456 }
457
458 static void mic_handle_config_change(struct mic_device_desc __iomem *d,
459         unsigned int offset, struct mic_driver *mdrv)
460 {
461         struct mic_device_ctrl __iomem *dc
462                 = (void __iomem *)d + mic_aligned_desc_size(d);
463         struct mic_vdev *mvdev = (struct mic_vdev *)ioread64(&dc->vdev);
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         virtio_config_changed(&mvdev->vdev);
470         iowrite8(1, &dc->guest_ack);
471 }
472
473 /*
474  * removes a virtio device if a hot remove event has been
475  * requested by the host.
476  */
477 static int mic_remove_device(struct mic_device_desc __iomem *d,
478         unsigned int offset, struct mic_driver *mdrv)
479 {
480         struct mic_device_ctrl __iomem *dc
481                 = (void __iomem *)d + mic_aligned_desc_size(d);
482         struct mic_vdev *mvdev = (struct mic_vdev *)ioread64(&dc->vdev);
483         u8 status;
484         int ret = -1;
485
486         if (ioread8(&dc->config_change) == MIC_VIRTIO_PARAM_DEV_REMOVE) {
487                 dev_dbg(mdrv->dev,
488                         "%s %d config_change %d type %d mvdev %p\n",
489                         __func__, __LINE__,
490                         ioread8(&dc->config_change), ioread8(&d->type), mvdev);
491
492                 status = ioread8(&d->status);
493                 reinit_completion(&mvdev->reset_done);
494                 unregister_virtio_device(&mvdev->vdev);
495                 mic_free_card_irq(mvdev->virtio_cookie, mvdev);
496                 if (status & VIRTIO_CONFIG_S_DRIVER_OK)
497                         wait_for_completion(&mvdev->reset_done);
498                 kfree(mvdev);
499                 iowrite8(1, &dc->guest_ack);
500                 dev_dbg(mdrv->dev, "%s %d guest_ack %d\n",
501                         __func__, __LINE__, ioread8(&dc->guest_ack));
502                 ret = 0;
503         }
504
505         return ret;
506 }
507
508 #define REMOVE_DEVICES true
509
510 static void mic_scan_devices(struct mic_driver *mdrv, bool remove)
511 {
512         s8 type;
513         unsigned int i;
514         struct mic_device_desc __iomem *d;
515         struct mic_device_ctrl __iomem *dc;
516         struct device *dev;
517         int ret;
518
519         for (i = sizeof(struct mic_bootparam); i < MIC_DP_SIZE;
520                 i += mic_total_desc_size(d)) {
521                 d = mdrv->dp + i;
522                 dc = (void __iomem *)d + mic_aligned_desc_size(d);
523                 /*
524                  * This read barrier is paired with the corresponding write
525                  * barrier on the host which is inserted before adding or
526                  * removing a virtio device descriptor, by updating the type.
527                  */
528                 rmb();
529                 type = ioread8(&d->type);
530
531                 /* end of list */
532                 if (type == 0)
533                         break;
534
535                 if (type == -1)
536                         continue;
537
538                 /* device already exists */
539                 dev = device_find_child(mdrv->dev, (void __force *)d,
540                                         mic_match_desc);
541                 if (dev) {
542                         if (remove)
543                                 iowrite8(MIC_VIRTIO_PARAM_DEV_REMOVE,
544                                          &dc->config_change);
545                         put_device(dev);
546                         mic_handle_config_change(d, i, mdrv);
547                         ret = mic_remove_device(d, i, mdrv);
548                         if (!ret && !remove)
549                                 iowrite8(-1, &d->type);
550                         if (remove) {
551                                 iowrite8(0, &dc->config_change);
552                                 iowrite8(0, &dc->guest_ack);
553                         }
554                         continue;
555                 }
556
557                 /* new device */
558                 dev_dbg(mdrv->dev, "%s %d Adding new virtio device %p\n",
559                         __func__, __LINE__, d);
560                 if (!remove)
561                         mic_add_device(d, i, mdrv);
562         }
563 }
564
565 /*
566  * mic_hotplug_device tries to find changes in the device page.
567  */
568 static void mic_hotplug_devices(struct work_struct *work)
569 {
570         struct mic_driver *mdrv = container_of(work,
571                 struct mic_driver, hotplug_work);
572
573         mic_scan_devices(mdrv, !REMOVE_DEVICES);
574 }
575
576 /*
577  * Interrupt handler for hot plug/config changes etc.
578  */
579 static irqreturn_t
580 mic_extint_handler(int irq, void *data)
581 {
582         struct mic_driver *mdrv = (struct mic_driver *)data;
583
584         dev_dbg(mdrv->dev, "%s %d hotplug work\n",
585                 __func__, __LINE__);
586         mic_ack_interrupt(&mdrv->mdev);
587         schedule_work(&mdrv->hotplug_work);
588         return IRQ_HANDLED;
589 }
590
591 /*
592  * Init function for virtio
593  */
594 int mic_devices_init(struct mic_driver *mdrv)
595 {
596         int rc;
597         struct mic_bootparam __iomem *bootparam;
598         int config_db;
599
600         INIT_WORK(&mdrv->hotplug_work, mic_hotplug_devices);
601         mic_scan_devices(mdrv, !REMOVE_DEVICES);
602
603         config_db = mic_next_card_db();
604         virtio_config_cookie = mic_request_card_irq(mic_extint_handler, NULL,
605                                                     "virtio_config_intr", mdrv,
606                                                     config_db);
607         if (IS_ERR(virtio_config_cookie)) {
608                 rc = PTR_ERR(virtio_config_cookie);
609                 goto exit;
610         }
611
612         bootparam = mdrv->dp;
613         iowrite8(config_db, &bootparam->h2c_config_db);
614         return 0;
615 exit:
616         return rc;
617 }
618
619 /*
620  * Uninit function for virtio
621  */
622 void mic_devices_uninit(struct mic_driver *mdrv)
623 {
624         struct mic_bootparam __iomem *bootparam = mdrv->dp;
625         iowrite8(-1, &bootparam->h2c_config_db);
626         mic_free_card_irq(virtio_config_cookie, mdrv);
627         flush_work(&mdrv->hotplug_work);
628         mic_scan_devices(mdrv, REMOVE_DEVICES);
629 }