]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/misc/mic/host/mic_virtio.c
9e0456fb1ea872d40bf633890151d17abc3f6929
[karo-tx-linux.git] / drivers / misc / mic / host / 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  * Intel MIC Host driver.
19  *
20  */
21 #include <linux/pci.h>
22 #include <linux/sched.h>
23 #include <linux/uaccess.h>
24
25 #include <linux/mic_common.h>
26 #include "../common/mic_dev.h"
27 #include "mic_device.h"
28 #include "mic_smpt.h"
29 #include "mic_virtio.h"
30
31 /*
32  * Initiates the copies across the PCIe bus from card memory to
33  * a user space buffer.
34  */
35 static int mic_virtio_copy_to_user(struct mic_vdev *mvdev,
36                 void __user *ubuf, size_t len, u64 addr)
37 {
38         int err;
39         void __iomem *dbuf = mvdev->mdev->aper.va + addr;
40         /*
41          * We are copying from IO below an should ideally use something
42          * like copy_to_user_fromio(..) if it existed.
43          */
44         if (copy_to_user(ubuf, dbuf, len)) {
45                 err = -EFAULT;
46                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
47                         __func__, __LINE__, err);
48                 goto err;
49         }
50         mvdev->in_bytes += len;
51         err = 0;
52 err:
53         return err;
54 }
55
56 /*
57  * Initiates copies across the PCIe bus from a user space
58  * buffer to card memory.
59  */
60 static int mic_virtio_copy_from_user(struct mic_vdev *mvdev,
61                 void __user *ubuf, size_t len, u64 addr)
62 {
63         int err;
64         void __iomem *dbuf = mvdev->mdev->aper.va + addr;
65         /*
66          * We are copying to IO below and should ideally use something
67          * like copy_from_user_toio(..) if it existed.
68          */
69         if (copy_from_user(dbuf, ubuf, len)) {
70                 err = -EFAULT;
71                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
72                         __func__, __LINE__, err);
73                 goto err;
74         }
75         mvdev->out_bytes += len;
76         err = 0;
77 err:
78         return err;
79 }
80
81 #define MIC_VRINGH_READ true
82
83 /* The function to call to notify the card about added buffers */
84 static void mic_notify(struct vringh *vrh)
85 {
86         struct mic_vringh *mvrh = container_of(vrh, struct mic_vringh, vrh);
87         struct mic_vdev *mvdev = mvrh->mvdev;
88         s8 db = mvdev->dc->h2c_vdev_db;
89
90         if (db != -1)
91                 mvdev->mdev->ops->send_intr(mvdev->mdev, db);
92 }
93
94 /* Determine the total number of bytes consumed in a VRINGH KIOV */
95 static inline u32 mic_vringh_iov_consumed(struct vringh_kiov *iov)
96 {
97         int i;
98         u32 total = iov->consumed;
99
100         for (i = 0; i < iov->i; i++)
101                 total += iov->iov[i].iov_len;
102         return total;
103 }
104
105 /*
106  * Traverse the VRINGH KIOV and issue the APIs to trigger the copies.
107  * This API is heavily based on the vringh_iov_xfer(..) implementation
108  * in vringh.c. The reason we cannot reuse vringh_iov_pull_kern(..)
109  * and vringh_iov_push_kern(..) directly is because there is no
110  * way to override the VRINGH xfer(..) routines as of v3.10.
111  */
112 static int mic_vringh_copy(struct mic_vdev *mvdev, struct vringh_kiov *iov,
113         void __user *ubuf, size_t len, bool read, size_t *out_len)
114 {
115         int ret = 0;
116         size_t partlen, tot_len = 0;
117
118         while (len && iov->i < iov->used) {
119                 partlen = min(iov->iov[iov->i].iov_len, len);
120                 if (read)
121                         ret = mic_virtio_copy_to_user(mvdev,
122                                 ubuf, partlen,
123                                 (u64)iov->iov[iov->i].iov_base);
124                 else
125                         ret = mic_virtio_copy_from_user(mvdev,
126                                 ubuf, partlen,
127                                 (u64)iov->iov[iov->i].iov_base);
128                 if (ret) {
129                         dev_err(mic_dev(mvdev), "%s %d err %d\n",
130                                 __func__, __LINE__, ret);
131                         break;
132                 }
133                 len -= partlen;
134                 ubuf += partlen;
135                 tot_len += partlen;
136                 iov->consumed += partlen;
137                 iov->iov[iov->i].iov_len -= partlen;
138                 iov->iov[iov->i].iov_base += partlen;
139                 if (!iov->iov[iov->i].iov_len) {
140                         /* Fix up old iov element then increment. */
141                         iov->iov[iov->i].iov_len = iov->consumed;
142                         iov->iov[iov->i].iov_base -= iov->consumed;
143
144                         iov->consumed = 0;
145                         iov->i++;
146                 }
147         }
148         *out_len = tot_len;
149         return ret;
150 }
151
152 /*
153  * Use the standard VRINGH infrastructure in the kernel to fetch new
154  * descriptors, initiate the copies and update the used ring.
155  */
156 static int _mic_virtio_copy(struct mic_vdev *mvdev,
157         struct mic_copy_desc *copy)
158 {
159         int ret = 0, iovcnt = copy->iovcnt;
160         struct iovec iov;
161         struct iovec __user *u_iov = copy->iov;
162         void __user *ubuf = NULL;
163         struct mic_vringh *mvr = &mvdev->mvr[copy->vr_idx];
164         struct vringh_kiov *riov = &mvr->riov;
165         struct vringh_kiov *wiov = &mvr->wiov;
166         struct vringh *vrh = &mvr->vrh;
167         u16 *head = &mvr->head;
168         struct mic_vring *vr = &mvr->vring;
169         size_t len = 0, out_len;
170
171         copy->out_len = 0;
172         /* Fetch a new IOVEC if all previous elements have been processed */
173         if (riov->i == riov->used && wiov->i == wiov->used) {
174                 ret = vringh_getdesc_kern(vrh, riov, wiov,
175                                 head, GFP_KERNEL);
176                 /* Check if there are available descriptors */
177                 if (ret <= 0)
178                         return ret;
179         }
180         while (iovcnt) {
181                 if (!len) {
182                         /* Copy over a new iovec from user space. */
183                         ret = copy_from_user(&iov, u_iov, sizeof(*u_iov));
184                         if (ret) {
185                                 ret = -EINVAL;
186                                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
187                                         __func__, __LINE__, ret);
188                                 break;
189                         }
190                         len = iov.iov_len;
191                         ubuf = iov.iov_base;
192                 }
193                 /* Issue all the read descriptors first */
194                 ret = mic_vringh_copy(mvdev, riov, ubuf, len,
195                         MIC_VRINGH_READ, &out_len);
196                 if (ret) {
197                         dev_err(mic_dev(mvdev), "%s %d err %d\n",
198                                         __func__, __LINE__, ret);
199                         break;
200                 }
201                 len -= out_len;
202                 ubuf += out_len;
203                 copy->out_len += out_len;
204                 /* Issue the write descriptors next */
205                 ret = mic_vringh_copy(mvdev, wiov, ubuf, len,
206                         !MIC_VRINGH_READ, &out_len);
207                 if (ret) {
208                         dev_err(mic_dev(mvdev), "%s %d err %d\n",
209                                         __func__, __LINE__, ret);
210                         break;
211                 }
212                 len -= out_len;
213                 ubuf += out_len;
214                 copy->out_len += out_len;
215                 if (!len) {
216                         /* One user space iovec is now completed */
217                         iovcnt--;
218                         u_iov++;
219                 }
220                 /* Exit loop if all elements in KIOVs have been processed. */
221                 if (riov->i == riov->used && wiov->i == wiov->used)
222                         break;
223         }
224         /*
225          * Update the used ring if a descriptor was available and some data was
226          * copied in/out and the user asked for a used ring update.
227          */
228         if (*head != USHRT_MAX && copy->out_len &&
229                 copy->update_used) {
230                 u32 total = 0;
231
232                 /* Determine the total data consumed */
233                 total += mic_vringh_iov_consumed(riov);
234                 total += mic_vringh_iov_consumed(wiov);
235                 vringh_complete_kern(vrh, *head, total);
236                 *head = USHRT_MAX;
237                 if (vringh_need_notify_kern(vrh) > 0)
238                         vringh_notify(vrh);
239                 vringh_kiov_cleanup(riov);
240                 vringh_kiov_cleanup(wiov);
241                 /* Update avail idx for user space */
242                 vr->info->avail_idx = vrh->last_avail_idx;
243         }
244         return ret;
245 }
246
247 static inline int mic_verify_copy_args(struct mic_vdev *mvdev,
248                 struct mic_copy_desc *copy)
249 {
250         if (copy->vr_idx >= mvdev->dd->num_vq) {
251                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
252                         __func__, __LINE__, -EINVAL);
253                 return -EINVAL;
254         }
255         return 0;
256 }
257
258 /* Copy a specified number of virtio descriptors in a chain */
259 int mic_virtio_copy_desc(struct mic_vdev *mvdev,
260                 struct mic_copy_desc *copy)
261 {
262         int err;
263         struct mic_vringh *mvr = &mvdev->mvr[copy->vr_idx];
264
265         err = mic_verify_copy_args(mvdev, copy);
266         if (err)
267                 return err;
268
269         mutex_lock(&mvr->vr_mutex);
270         if (!mic_vdevup(mvdev)) {
271                 err = -ENODEV;
272                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
273                         __func__, __LINE__, err);
274                 goto err;
275         }
276         err = _mic_virtio_copy(mvdev, copy);
277         if (err) {
278                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
279                         __func__, __LINE__, err);
280         }
281 err:
282         mutex_unlock(&mvr->vr_mutex);
283         return err;
284 }
285
286 static void mic_virtio_init_post(struct mic_vdev *mvdev)
287 {
288         struct mic_vqconfig *vqconfig = mic_vq_config(mvdev->dd);
289         int i;
290
291         for (i = 0; i < mvdev->dd->num_vq; i++) {
292                 if (!le64_to_cpu(vqconfig[i].used_address)) {
293                         dev_warn(mic_dev(mvdev), "used_address zero??\n");
294                         continue;
295                 }
296                 mvdev->mvr[i].vrh.vring.used =
297                         mvdev->mdev->aper.va +
298                         le64_to_cpu(vqconfig[i].used_address);
299         }
300
301         mvdev->dc->used_address_updated = 0;
302
303         dev_dbg(mic_dev(mvdev), "%s: device type %d LINKUP\n",
304                 __func__, mvdev->virtio_id);
305 }
306
307 static inline void mic_virtio_device_reset(struct mic_vdev *mvdev)
308 {
309         int i;
310
311         dev_dbg(mic_dev(mvdev), "%s: status %d device type %d RESET\n",
312                 __func__, mvdev->dd->status, mvdev->virtio_id);
313
314         for (i = 0; i < mvdev->dd->num_vq; i++)
315                 /*
316                  * Avoid lockdep false positive. The + 1 is for the mic
317                  * mutex which is held in the reset devices code path.
318                  */
319                 mutex_lock_nested(&mvdev->mvr[i].vr_mutex, i + 1);
320
321         /* 0 status means "reset" */
322         mvdev->dd->status = 0;
323         mvdev->dc->vdev_reset = 0;
324         mvdev->dc->host_ack = 1;
325
326         for (i = 0; i < mvdev->dd->num_vq; i++) {
327                 struct vringh *vrh = &mvdev->mvr[i].vrh;
328                 mvdev->mvr[i].vring.info->avail_idx = 0;
329                 vrh->completed = 0;
330                 vrh->last_avail_idx = 0;
331                 vrh->last_used_idx = 0;
332         }
333
334         for (i = 0; i < mvdev->dd->num_vq; i++)
335                 mutex_unlock(&mvdev->mvr[i].vr_mutex);
336 }
337
338 void mic_virtio_reset_devices(struct mic_device *mdev)
339 {
340         struct list_head *pos, *tmp;
341         struct mic_vdev *mvdev;
342
343         dev_dbg(mdev->sdev->parent, "%s\n",  __func__);
344
345         list_for_each_safe(pos, tmp, &mdev->vdev_list) {
346                 mvdev = list_entry(pos, struct mic_vdev, list);
347                 mic_virtio_device_reset(mvdev);
348                 mvdev->poll_wake = 1;
349                 wake_up(&mvdev->waitq);
350         }
351 }
352
353 void mic_bh_handler(struct work_struct *work)
354 {
355         struct mic_vdev *mvdev = container_of(work, struct mic_vdev,
356                         virtio_bh_work);
357
358         if (mvdev->dc->used_address_updated)
359                 mic_virtio_init_post(mvdev);
360
361         if (mvdev->dc->vdev_reset)
362                 mic_virtio_device_reset(mvdev);
363
364         mvdev->poll_wake = 1;
365         wake_up(&mvdev->waitq);
366 }
367
368 static irqreturn_t mic_virtio_intr_handler(int irq, void *data)
369 {
370
371         struct mic_vdev *mvdev = data;
372         struct mic_device *mdev = mvdev->mdev;
373
374         mdev->ops->ack_interrupt(mdev);
375         schedule_work(&mvdev->virtio_bh_work);
376         return IRQ_HANDLED;
377 }
378
379 int mic_virtio_config_change(struct mic_vdev *mvdev,
380                         void __user *argp)
381 {
382         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
383         int ret = 0, retry = 100, i;
384         struct mic_bootparam *bootparam = mvdev->mdev->dp;
385         s8 db = bootparam->h2c_config_db;
386
387         mutex_lock(&mvdev->mdev->mic_mutex);
388         for (i = 0; i < mvdev->dd->num_vq; i++)
389                 mutex_lock_nested(&mvdev->mvr[i].vr_mutex, i + 1);
390
391         if (db == -1 || mvdev->dd->type == -1) {
392                 ret = -EIO;
393                 goto exit;
394         }
395
396         if (copy_from_user(mic_vq_configspace(mvdev->dd),
397                                 argp, mvdev->dd->config_len)) {
398                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
399                         __func__, __LINE__, -EFAULT);
400                 ret = -EFAULT;
401                 goto exit;
402         }
403         mvdev->dc->config_change = MIC_VIRTIO_PARAM_CONFIG_CHANGED;
404         mvdev->mdev->ops->send_intr(mvdev->mdev, db);
405
406         for (i = retry; i--;) {
407                 ret = wait_event_timeout(wake,
408                         mvdev->dc->guest_ack, msecs_to_jiffies(100));
409                 if (ret)
410                         break;
411         }
412
413         dev_dbg(mic_dev(mvdev),
414                 "%s %d retry: %d\n", __func__, __LINE__, retry);
415         mvdev->dc->config_change = 0;
416         mvdev->dc->guest_ack = 0;
417 exit:
418         for (i = 0; i < mvdev->dd->num_vq; i++)
419                 mutex_unlock(&mvdev->mvr[i].vr_mutex);
420         mutex_unlock(&mvdev->mdev->mic_mutex);
421         return ret;
422 }
423
424 static int mic_copy_dp_entry(struct mic_vdev *mvdev,
425                                         void __user *argp,
426                                         __u8 *type,
427                                         struct mic_device_desc **devpage)
428 {
429         struct mic_device *mdev = mvdev->mdev;
430         struct mic_device_desc dd, *dd_config, *devp;
431         struct mic_vqconfig *vqconfig;
432         int ret = 0, i;
433         bool slot_found = false;
434
435         if (copy_from_user(&dd, argp, sizeof(dd))) {
436                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
437                         __func__, __LINE__, -EFAULT);
438                 return -EFAULT;
439         }
440
441         if (mic_aligned_desc_size(&dd) > MIC_MAX_DESC_BLK_SIZE
442                 || dd.num_vq > MIC_MAX_VRINGS) {
443                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
444                         __func__, __LINE__, -EINVAL);
445                 return -EINVAL;
446         }
447
448         dd_config = kmalloc(mic_desc_size(&dd), GFP_KERNEL);
449         if (dd_config == NULL) {
450                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
451                         __func__, __LINE__, -ENOMEM);
452                 return -ENOMEM;
453         }
454         if (copy_from_user(dd_config, argp, mic_desc_size(&dd))) {
455                 ret = -EFAULT;
456                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
457                         __func__, __LINE__, ret);
458                 goto exit;
459         }
460
461         vqconfig = mic_vq_config(dd_config);
462         for (i = 0; i < dd.num_vq; i++) {
463                 if (le16_to_cpu(vqconfig[i].num) > MIC_MAX_VRING_ENTRIES) {
464                         ret =  -EINVAL;
465                         dev_err(mic_dev(mvdev), "%s %d err %d\n",
466                                 __func__, __LINE__, ret);
467                         goto exit;
468                 }
469         }
470
471         /* Find the first free device page entry */
472         for (i = mic_aligned_size(struct mic_bootparam);
473                 i < MIC_DP_SIZE - mic_total_desc_size(dd_config);
474                 i += mic_total_desc_size(devp)) {
475                 devp = mdev->dp + i;
476                 if (devp->type == 0 || devp->type == -1) {
477                         slot_found = true;
478                         break;
479                 }
480         }
481         if (!slot_found) {
482                 ret =  -EINVAL;
483                 dev_err(mic_dev(mvdev), "%s %d err %d\n",
484                         __func__, __LINE__, ret);
485                 goto exit;
486         }
487         /*
488          * Save off the type before doing the memcpy. Type will be set in the
489          * end after completing all initialization for the new device.
490          */
491         *type = dd_config->type;
492         dd_config->type = 0;
493         memcpy(devp, dd_config, mic_desc_size(dd_config));
494
495         *devpage = devp;
496 exit:
497         kfree(dd_config);
498         return ret;
499 }
500
501 static void mic_init_device_ctrl(struct mic_vdev *mvdev,
502                                 struct mic_device_desc *devpage)
503 {
504         struct mic_device_ctrl *dc;
505
506         dc = mvdev->dc = (void *)devpage + mic_aligned_desc_size(devpage);
507
508         dc->config_change = 0;
509         dc->guest_ack = 0;
510         dc->vdev_reset = 0;
511         dc->host_ack = 0;
512         dc->used_address_updated = 0;
513         dc->c2h_vdev_db = -1;
514         dc->h2c_vdev_db = -1;
515 }
516
517 int mic_virtio_add_device(struct mic_vdev *mvdev,
518                         void __user *argp)
519 {
520         struct mic_device *mdev = mvdev->mdev;
521         struct mic_device_desc *dd;
522         struct mic_vqconfig *vqconfig;
523         int vr_size, i, j, ret;
524         u8 type;
525         s8 db;
526         char irqname[10];
527         struct mic_bootparam *bootparam = mdev->dp;
528         u16 num;
529
530         mutex_lock(&mdev->mic_mutex);
531
532         ret = mic_copy_dp_entry(mvdev, argp, &type, &dd);
533         if (ret) {
534                 mutex_unlock(&mdev->mic_mutex);
535                 return ret;
536         }
537
538         mic_init_device_ctrl(mvdev, dd);
539
540         mvdev->dd = dd;
541         mvdev->virtio_id = type;
542         vqconfig = mic_vq_config(dd);
543         INIT_WORK(&mvdev->virtio_bh_work, mic_bh_handler);
544
545         for (i = 0; i < dd->num_vq; i++) {
546                 struct mic_vringh *mvr = &mvdev->mvr[i];
547                 struct mic_vring *vr = &mvdev->mvr[i].vring;
548                 num = le16_to_cpu(vqconfig[i].num);
549                 mutex_init(&mvr->vr_mutex);
550                 vr_size = PAGE_ALIGN(vring_size(num, MIC_VIRTIO_RING_ALIGN) +
551                         sizeof(struct _mic_vring_info));
552                 vr->va = (void *)
553                         __get_free_pages(GFP_KERNEL | __GFP_ZERO,
554                         get_order(vr_size));
555                 if (!vr->va) {
556                         ret = -ENOMEM;
557                         dev_err(mic_dev(mvdev), "%s %d err %d\n",
558                                 __func__, __LINE__, ret);
559                         goto err;
560                 }
561                 vr->len = vr_size;
562                 vr->info = vr->va + vring_size(num, MIC_VIRTIO_RING_ALIGN);
563                 vr->info->magic = MIC_MAGIC + mvdev->virtio_id + i;
564                 vqconfig[i].address = mic_map_single(mdev,
565                         vr->va, vr_size);
566                 if (mic_map_error(vqconfig[i].address)) {
567                         free_pages((unsigned long)vr->va,
568                                 get_order(vr_size));
569                         ret = -ENOMEM;
570                         dev_err(mic_dev(mvdev), "%s %d err %d\n",
571                                 __func__, __LINE__, ret);
572                         goto err;
573                 }
574                 vqconfig[i].address = cpu_to_le64(vqconfig[i].address);
575
576                 vring_init(&vr->vr, num,
577                         vr->va, MIC_VIRTIO_RING_ALIGN);
578                 ret = vringh_init_kern(&mvr->vrh,
579                         *(u32 *)mic_vq_features(mvdev->dd), num, false,
580                         vr->vr.desc, vr->vr.avail, vr->vr.used);
581                 if (ret) {
582                         dev_err(mic_dev(mvdev), "%s %d err %d\n",
583                                 __func__, __LINE__, ret);
584                         goto err;
585                 }
586                 vringh_kiov_init(&mvr->riov, NULL, 0);
587                 vringh_kiov_init(&mvr->wiov, NULL, 0);
588                 mvr->head = USHRT_MAX;
589                 mvr->mvdev = mvdev;
590                 mvr->vrh.notify = mic_notify;
591                 dev_dbg(mdev->sdev->parent,
592                         "%s %d index %d va %p info %p vr_size 0x%x\n",
593                         __func__, __LINE__, i, vr->va, vr->info, vr_size);
594         }
595
596         snprintf(irqname, sizeof(irqname),
597                 "mic%dvirtio%d", mdev->id, mvdev->virtio_id);
598         mvdev->virtio_db = mic_next_db(mdev);
599         mvdev->virtio_cookie = mic_request_irq(mdev, mic_virtio_intr_handler,
600                         irqname, mvdev, mvdev->virtio_db, MIC_INTR_DB);
601         if (IS_ERR(mvdev->virtio_cookie)) {
602                 ret = PTR_ERR(mvdev->virtio_cookie);
603                 dev_dbg(mdev->sdev->parent, "request irq failed\n");
604                 goto err;
605         }
606
607         mvdev->dc->c2h_vdev_db = mvdev->virtio_db;
608
609         list_add_tail(&mvdev->list, &mdev->vdev_list);
610         /*
611          * Order the type update with previous stores. This write barrier
612          * is paired with the corresponding read barrier before the uncached
613          * system memory read of the type, on the card while scanning the
614          * device page.
615          */
616         smp_wmb();
617         dd->type = type;
618
619         dev_dbg(mdev->sdev->parent, "Added virtio device id %d\n", dd->type);
620
621         db = bootparam->h2c_config_db;
622         if (db != -1)
623                 mdev->ops->send_intr(mdev, db);
624         mutex_unlock(&mdev->mic_mutex);
625         return 0;
626 err:
627         vqconfig = mic_vq_config(dd);
628         for (j = 0; j < i; j++) {
629                 struct mic_vringh *mvr = &mvdev->mvr[j];
630                 mic_unmap_single(mdev, le64_to_cpu(vqconfig[j].address),
631                                 mvr->vring.len);
632                 free_pages((unsigned long)mvr->vring.va,
633                         get_order(mvr->vring.len));
634         }
635         mutex_unlock(&mdev->mic_mutex);
636         return ret;
637 }
638
639 void mic_virtio_del_device(struct mic_vdev *mvdev)
640 {
641         struct list_head *pos, *tmp;
642         struct mic_vdev *tmp_mvdev;
643         struct mic_device *mdev = mvdev->mdev;
644         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wake);
645         int i, ret, retry = 100;
646         struct mic_vqconfig *vqconfig;
647         struct mic_bootparam *bootparam = mdev->dp;
648         s8 db;
649
650         mutex_lock(&mdev->mic_mutex);
651         db = bootparam->h2c_config_db;
652         if (db == -1)
653                 goto skip_hot_remove;
654         dev_dbg(mdev->sdev->parent,
655                 "Requesting hot remove id %d\n", mvdev->virtio_id);
656         mvdev->dc->config_change = MIC_VIRTIO_PARAM_DEV_REMOVE;
657         mdev->ops->send_intr(mdev, db);
658         for (i = retry; i--;) {
659                 ret = wait_event_timeout(wake,
660                         mvdev->dc->guest_ack, msecs_to_jiffies(100));
661                 if (ret)
662                         break;
663         }
664         dev_dbg(mdev->sdev->parent,
665                 "Device id %d config_change %d guest_ack %d\n",
666                 mvdev->virtio_id, mvdev->dc->config_change,
667                 mvdev->dc->guest_ack);
668         mvdev->dc->config_change = 0;
669         mvdev->dc->guest_ack = 0;
670 skip_hot_remove:
671         mic_free_irq(mdev, mvdev->virtio_cookie, mvdev);
672         flush_work(&mvdev->virtio_bh_work);
673         vqconfig = mic_vq_config(mvdev->dd);
674         for (i = 0; i < mvdev->dd->num_vq; i++) {
675                 struct mic_vringh *mvr = &mvdev->mvr[i];
676                 vringh_kiov_cleanup(&mvr->riov);
677                 vringh_kiov_cleanup(&mvr->wiov);
678                 mic_unmap_single(mdev, le64_to_cpu(vqconfig[i].address),
679                                 mvr->vring.len);
680                 free_pages((unsigned long)mvr->vring.va,
681                         get_order(mvr->vring.len));
682         }
683
684         list_for_each_safe(pos, tmp, &mdev->vdev_list) {
685                 tmp_mvdev = list_entry(pos, struct mic_vdev, list);
686                 if (tmp_mvdev == mvdev) {
687                         list_del(pos);
688                         dev_dbg(mdev->sdev->parent,
689                                 "Removing virtio device id %d\n",
690                                 mvdev->virtio_id);
691                         break;
692                 }
693         }
694         /*
695          * Order the type update with previous stores. This write barrier
696          * is paired with the corresponding read barrier before the uncached
697          * system memory read of the type, on the card while scanning the
698          * device page.
699          */
700         smp_wmb();
701         mvdev->dd->type = -1;
702         mutex_unlock(&mdev->mic_mutex);
703 }