]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/misc/vmw_vmci/vmci_guest.c
VMCI: dma dg: register dummy IRQ handlers for DMA datagrams
[mirror_ubuntu-jammy-kernel.git] / drivers / misc / vmw_vmci / vmci_guest.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * VMware VMCI Driver
4 *
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
6 */
7
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/vmw_vmci_api.h>
10 #include <linux/moduleparam.h>
11 #include <linux/interrupt.h>
12 #include <linux/highmem.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/pci.h>
20 #include <linux/smp.h>
21 #include <linux/io.h>
22 #include <linux/vmalloc.h>
23
24 #include "vmci_datagram.h"
25 #include "vmci_doorbell.h"
26 #include "vmci_context.h"
27 #include "vmci_driver.h"
28 #include "vmci_event.h"
29
30 #define PCI_DEVICE_ID_VMWARE_VMCI 0x0740
31
32 #define VMCI_UTIL_NUM_RESOURCES 1
33
34 static bool vmci_disable_msi;
35 module_param_named(disable_msi, vmci_disable_msi, bool, 0);
36 MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
37
38 static bool vmci_disable_msix;
39 module_param_named(disable_msix, vmci_disable_msix, bool, 0);
40 MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
41
42 static u32 ctx_update_sub_id = VMCI_INVALID_ID;
43 static u32 vm_context_id = VMCI_INVALID_ID;
44
45 struct vmci_guest_device {
46 struct device *dev; /* PCI device we are attached to */
47 void __iomem *iobase;
48 void __iomem *mmio_base;
49
50 bool exclusive_vectors;
51
52 struct tasklet_struct datagram_tasklet;
53 struct tasklet_struct bm_tasklet;
54
55 void *data_buffer;
56 void *notification_bitmap;
57 dma_addr_t notification_base;
58 };
59
60 static bool use_ppn64;
61
62 bool vmci_use_ppn64(void)
63 {
64 return use_ppn64;
65 }
66
67 /* vmci_dev singleton device and supporting data*/
68 struct pci_dev *vmci_pdev;
69 static struct vmci_guest_device *vmci_dev_g;
70 static DEFINE_SPINLOCK(vmci_dev_spinlock);
71
72 static atomic_t vmci_num_guest_devices = ATOMIC_INIT(0);
73
74 bool vmci_guest_code_active(void)
75 {
76 return atomic_read(&vmci_num_guest_devices) != 0;
77 }
78
79 u32 vmci_get_vm_context_id(void)
80 {
81 if (vm_context_id == VMCI_INVALID_ID) {
82 struct vmci_datagram get_cid_msg;
83 get_cid_msg.dst =
84 vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
85 VMCI_GET_CONTEXT_ID);
86 get_cid_msg.src = VMCI_ANON_SRC_HANDLE;
87 get_cid_msg.payload_size = 0;
88 vm_context_id = vmci_send_datagram(&get_cid_msg);
89 }
90 return vm_context_id;
91 }
92
93 static unsigned int vmci_read_reg(struct vmci_guest_device *dev, u32 reg)
94 {
95 if (dev->mmio_base != NULL)
96 return readl(dev->mmio_base + reg);
97 return ioread32(dev->iobase + reg);
98 }
99
100 static void vmci_write_reg(struct vmci_guest_device *dev, u32 val, u32 reg)
101 {
102 if (dev->mmio_base != NULL)
103 writel(val, dev->mmio_base + reg);
104 else
105 iowrite32(val, dev->iobase + reg);
106 }
107
108 /*
109 * VM to hypervisor call mechanism. We use the standard VMware naming
110 * convention since shared code is calling this function as well.
111 */
112 int vmci_send_datagram(struct vmci_datagram *dg)
113 {
114 unsigned long flags;
115 int result;
116
117 /* Check args. */
118 if (dg == NULL)
119 return VMCI_ERROR_INVALID_ARGS;
120
121 /*
122 * Need to acquire spinlock on the device because the datagram
123 * data may be spread over multiple pages and the monitor may
124 * interleave device user rpc calls from multiple
125 * VCPUs. Acquiring the spinlock precludes that
126 * possibility. Disabling interrupts to avoid incoming
127 * datagrams during a "rep out" and possibly landing up in
128 * this function.
129 */
130 spin_lock_irqsave(&vmci_dev_spinlock, flags);
131
132 if (vmci_dev_g) {
133 iowrite8_rep(vmci_dev_g->iobase + VMCI_DATA_OUT_ADDR,
134 dg, VMCI_DG_SIZE(dg));
135 result = vmci_read_reg(vmci_dev_g, VMCI_RESULT_LOW_ADDR);
136 } else {
137 result = VMCI_ERROR_UNAVAILABLE;
138 }
139
140 spin_unlock_irqrestore(&vmci_dev_spinlock, flags);
141
142 return result;
143 }
144 EXPORT_SYMBOL_GPL(vmci_send_datagram);
145
146 /*
147 * Gets called with the new context id if updated or resumed.
148 * Context id.
149 */
150 static void vmci_guest_cid_update(u32 sub_id,
151 const struct vmci_event_data *event_data,
152 void *client_data)
153 {
154 const struct vmci_event_payld_ctx *ev_payload =
155 vmci_event_data_const_payload(event_data);
156
157 if (sub_id != ctx_update_sub_id) {
158 pr_devel("Invalid subscriber (ID=0x%x)\n", sub_id);
159 return;
160 }
161
162 if (!event_data || ev_payload->context_id == VMCI_INVALID_ID) {
163 pr_devel("Invalid event data\n");
164 return;
165 }
166
167 pr_devel("Updating context from (ID=0x%x) to (ID=0x%x) on event (type=%d)\n",
168 vm_context_id, ev_payload->context_id, event_data->event);
169
170 vm_context_id = ev_payload->context_id;
171 }
172
173 /*
174 * Verify that the host supports the hypercalls we need. If it does not,
175 * try to find fallback hypercalls and use those instead. Returns
176 * true if required hypercalls (or fallback hypercalls) are
177 * supported by the host, false otherwise.
178 */
179 static int vmci_check_host_caps(struct pci_dev *pdev)
180 {
181 bool result;
182 struct vmci_resource_query_msg *msg;
183 u32 msg_size = sizeof(struct vmci_resource_query_hdr) +
184 VMCI_UTIL_NUM_RESOURCES * sizeof(u32);
185 struct vmci_datagram *check_msg;
186
187 check_msg = kzalloc(msg_size, GFP_KERNEL);
188 if (!check_msg) {
189 dev_err(&pdev->dev, "%s: Insufficient memory\n", __func__);
190 return -ENOMEM;
191 }
192
193 check_msg->dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
194 VMCI_RESOURCES_QUERY);
195 check_msg->src = VMCI_ANON_SRC_HANDLE;
196 check_msg->payload_size = msg_size - VMCI_DG_HEADERSIZE;
197 msg = (struct vmci_resource_query_msg *)VMCI_DG_PAYLOAD(check_msg);
198
199 msg->num_resources = VMCI_UTIL_NUM_RESOURCES;
200 msg->resources[0] = VMCI_GET_CONTEXT_ID;
201
202 /* Checks that hyper calls are supported */
203 result = vmci_send_datagram(check_msg) == 0x01;
204 kfree(check_msg);
205
206 dev_dbg(&pdev->dev, "%s: Host capability check: %s\n",
207 __func__, result ? "PASSED" : "FAILED");
208
209 /* We need the vector. There are no fallbacks. */
210 return result ? 0 : -ENXIO;
211 }
212
213 /*
214 * Reads datagrams from the data in port and dispatches them. We
215 * always start reading datagrams into only the first page of the
216 * datagram buffer. If the datagrams don't fit into one page, we
217 * use the maximum datagram buffer size for the remainder of the
218 * invocation. This is a simple heuristic for not penalizing
219 * small datagrams.
220 *
221 * This function assumes that it has exclusive access to the data
222 * in port for the duration of the call.
223 */
224 static void vmci_dispatch_dgs(unsigned long data)
225 {
226 struct vmci_guest_device *vmci_dev = (struct vmci_guest_device *)data;
227 u8 *dg_in_buffer = vmci_dev->data_buffer;
228 struct vmci_datagram *dg;
229 size_t dg_in_buffer_size = VMCI_MAX_DG_SIZE;
230 size_t current_dg_in_buffer_size = PAGE_SIZE;
231 size_t remaining_bytes;
232
233 BUILD_BUG_ON(VMCI_MAX_DG_SIZE < PAGE_SIZE);
234
235 ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
236 vmci_dev->data_buffer, current_dg_in_buffer_size);
237 dg = (struct vmci_datagram *)dg_in_buffer;
238 remaining_bytes = current_dg_in_buffer_size;
239
240 while (dg->dst.resource != VMCI_INVALID_ID ||
241 remaining_bytes > PAGE_SIZE) {
242 unsigned dg_in_size;
243
244 /*
245 * When the input buffer spans multiple pages, a datagram can
246 * start on any page boundary in the buffer.
247 */
248 if (dg->dst.resource == VMCI_INVALID_ID) {
249 dg = (struct vmci_datagram *)roundup(
250 (uintptr_t)dg + 1, PAGE_SIZE);
251 remaining_bytes =
252 (size_t)(dg_in_buffer +
253 current_dg_in_buffer_size -
254 (u8 *)dg);
255 continue;
256 }
257
258 dg_in_size = VMCI_DG_SIZE_ALIGNED(dg);
259
260 if (dg_in_size <= dg_in_buffer_size) {
261 int result;
262
263 /*
264 * If the remaining bytes in the datagram
265 * buffer doesn't contain the complete
266 * datagram, we first make sure we have enough
267 * room for it and then we read the reminder
268 * of the datagram and possibly any following
269 * datagrams.
270 */
271 if (dg_in_size > remaining_bytes) {
272 if (remaining_bytes !=
273 current_dg_in_buffer_size) {
274
275 /*
276 * We move the partial
277 * datagram to the front and
278 * read the reminder of the
279 * datagram and possibly
280 * following calls into the
281 * following bytes.
282 */
283 memmove(dg_in_buffer, dg_in_buffer +
284 current_dg_in_buffer_size -
285 remaining_bytes,
286 remaining_bytes);
287 dg = (struct vmci_datagram *)
288 dg_in_buffer;
289 }
290
291 if (current_dg_in_buffer_size !=
292 dg_in_buffer_size)
293 current_dg_in_buffer_size =
294 dg_in_buffer_size;
295
296 ioread8_rep(vmci_dev->iobase +
297 VMCI_DATA_IN_ADDR,
298 vmci_dev->data_buffer +
299 remaining_bytes,
300 current_dg_in_buffer_size -
301 remaining_bytes);
302 }
303
304 /*
305 * We special case event datagrams from the
306 * hypervisor.
307 */
308 if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
309 dg->dst.resource == VMCI_EVENT_HANDLER) {
310 result = vmci_event_dispatch(dg);
311 } else {
312 result = vmci_datagram_invoke_guest_handler(dg);
313 }
314 if (result < VMCI_SUCCESS)
315 dev_dbg(vmci_dev->dev,
316 "Datagram with resource (ID=0x%x) failed (err=%d)\n",
317 dg->dst.resource, result);
318
319 /* On to the next datagram. */
320 dg = (struct vmci_datagram *)((u8 *)dg +
321 dg_in_size);
322 } else {
323 size_t bytes_to_skip;
324
325 /*
326 * Datagram doesn't fit in datagram buffer of maximal
327 * size. We drop it.
328 */
329 dev_dbg(vmci_dev->dev,
330 "Failed to receive datagram (size=%u bytes)\n",
331 dg_in_size);
332
333 bytes_to_skip = dg_in_size - remaining_bytes;
334 if (current_dg_in_buffer_size != dg_in_buffer_size)
335 current_dg_in_buffer_size = dg_in_buffer_size;
336
337 for (;;) {
338 ioread8_rep(vmci_dev->iobase +
339 VMCI_DATA_IN_ADDR,
340 vmci_dev->data_buffer,
341 current_dg_in_buffer_size);
342 if (bytes_to_skip <= current_dg_in_buffer_size)
343 break;
344
345 bytes_to_skip -= current_dg_in_buffer_size;
346 }
347 dg = (struct vmci_datagram *)(dg_in_buffer +
348 bytes_to_skip);
349 }
350
351 remaining_bytes =
352 (size_t) (dg_in_buffer + current_dg_in_buffer_size -
353 (u8 *)dg);
354
355 if (remaining_bytes < VMCI_DG_HEADERSIZE) {
356 /* Get the next batch of datagrams. */
357
358 ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
359 vmci_dev->data_buffer,
360 current_dg_in_buffer_size);
361 dg = (struct vmci_datagram *)dg_in_buffer;
362 remaining_bytes = current_dg_in_buffer_size;
363 }
364 }
365 }
366
367 /*
368 * Scans the notification bitmap for raised flags, clears them
369 * and handles the notifications.
370 */
371 static void vmci_process_bitmap(unsigned long data)
372 {
373 struct vmci_guest_device *dev = (struct vmci_guest_device *)data;
374
375 if (!dev->notification_bitmap) {
376 dev_dbg(dev->dev, "No bitmap present in %s\n", __func__);
377 return;
378 }
379
380 vmci_dbell_scan_notification_entries(dev->notification_bitmap);
381 }
382
383 /*
384 * Interrupt handler for legacy or MSI interrupt, or for first MSI-X
385 * interrupt (vector VMCI_INTR_DATAGRAM).
386 */
387 static irqreturn_t vmci_interrupt(int irq, void *_dev)
388 {
389 struct vmci_guest_device *dev = _dev;
390
391 /*
392 * If we are using MSI-X with exclusive vectors then we simply schedule
393 * the datagram tasklet, since we know the interrupt was meant for us.
394 * Otherwise we must read the ICR to determine what to do.
395 */
396
397 if (dev->exclusive_vectors) {
398 tasklet_schedule(&dev->datagram_tasklet);
399 } else {
400 unsigned int icr;
401
402 /* Acknowledge interrupt and determine what needs doing. */
403 icr = vmci_read_reg(dev, VMCI_ICR_ADDR);
404 if (icr == 0 || icr == ~0)
405 return IRQ_NONE;
406
407 if (icr & VMCI_ICR_DATAGRAM) {
408 tasklet_schedule(&dev->datagram_tasklet);
409 icr &= ~VMCI_ICR_DATAGRAM;
410 }
411
412 if (icr & VMCI_ICR_NOTIFICATION) {
413 tasklet_schedule(&dev->bm_tasklet);
414 icr &= ~VMCI_ICR_NOTIFICATION;
415 }
416
417 if (icr & VMCI_ICR_DMA_DATAGRAM)
418 icr &= ~VMCI_ICR_DMA_DATAGRAM;
419
420 if (icr != 0)
421 dev_warn(dev->dev,
422 "Ignoring unknown interrupt cause (%d)\n",
423 icr);
424 }
425
426 return IRQ_HANDLED;
427 }
428
429 /*
430 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION,
431 * which is for the notification bitmap. Will only get called if we are
432 * using MSI-X with exclusive vectors.
433 */
434 static irqreturn_t vmci_interrupt_bm(int irq, void *_dev)
435 {
436 struct vmci_guest_device *dev = _dev;
437
438 /* For MSI-X we can just assume it was meant for us. */
439 tasklet_schedule(&dev->bm_tasklet);
440
441 return IRQ_HANDLED;
442 }
443
444 /*
445 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_DMA_DATAGRAM,
446 * which is for the completion of a DMA datagram send or receive operation.
447 * Will only get called if we are using MSI-X with exclusive vectors.
448 */
449 static irqreturn_t vmci_interrupt_dma_datagram(int irq, void *_dev)
450 {
451 return IRQ_HANDLED;
452 }
453
454 /*
455 * Most of the initialization at module load time is done here.
456 */
457 static int vmci_guest_probe_device(struct pci_dev *pdev,
458 const struct pci_device_id *id)
459 {
460 struct vmci_guest_device *vmci_dev;
461 void __iomem *iobase = NULL;
462 void __iomem *mmio_base = NULL;
463 unsigned int num_irq_vectors;
464 unsigned int capabilities;
465 unsigned int caps_in_use;
466 unsigned long cmd;
467 int vmci_err;
468 int error;
469
470 dev_dbg(&pdev->dev, "Probing for vmci/PCI guest device\n");
471
472 error = pcim_enable_device(pdev);
473 if (error) {
474 dev_err(&pdev->dev,
475 "Failed to enable VMCI device: %d\n", error);
476 return error;
477 }
478
479 /*
480 * The VMCI device with mmio access to registers requests 256KB
481 * for BAR1. If present, driver will use new VMCI device
482 * functionality for register access and datagram send/recv.
483 */
484
485 if (pci_resource_len(pdev, 1) == VMCI_WITH_MMIO_ACCESS_BAR_SIZE) {
486 dev_info(&pdev->dev, "MMIO register access is available\n");
487 mmio_base = pci_iomap_range(pdev, 1, VMCI_MMIO_ACCESS_OFFSET,
488 VMCI_MMIO_ACCESS_SIZE);
489 /* If the map fails, we fall back to IOIO access. */
490 if (!mmio_base)
491 dev_warn(&pdev->dev, "Failed to map MMIO register access\n");
492 }
493
494 if (!mmio_base) {
495 error = pcim_iomap_regions(pdev, BIT(0), KBUILD_MODNAME);
496 if (error) {
497 dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
498 return error;
499 }
500 iobase = pcim_iomap_table(pdev)[0];
501 }
502
503 vmci_dev = devm_kzalloc(&pdev->dev, sizeof(*vmci_dev), GFP_KERNEL);
504 if (!vmci_dev) {
505 dev_err(&pdev->dev,
506 "Can't allocate memory for VMCI device\n");
507 return -ENOMEM;
508 }
509
510 vmci_dev->dev = &pdev->dev;
511 vmci_dev->exclusive_vectors = false;
512 vmci_dev->iobase = iobase;
513 vmci_dev->mmio_base = mmio_base;
514
515 tasklet_init(&vmci_dev->datagram_tasklet,
516 vmci_dispatch_dgs, (unsigned long)vmci_dev);
517 tasklet_init(&vmci_dev->bm_tasklet,
518 vmci_process_bitmap, (unsigned long)vmci_dev);
519
520 vmci_dev->data_buffer = vmalloc(VMCI_MAX_DG_SIZE);
521 if (!vmci_dev->data_buffer) {
522 dev_err(&pdev->dev,
523 "Can't allocate memory for datagram buffer\n");
524 return -ENOMEM;
525 }
526
527 pci_set_master(pdev); /* To enable queue_pair functionality. */
528
529 /*
530 * Verify that the VMCI Device supports the capabilities that
531 * we need. If the device is missing capabilities that we would
532 * like to use, check for fallback capabilities and use those
533 * instead (so we can run a new VM on old hosts). Fail the load if
534 * a required capability is missing and there is no fallback.
535 *
536 * Right now, we need datagrams. There are no fallbacks.
537 */
538 capabilities = vmci_read_reg(vmci_dev, VMCI_CAPS_ADDR);
539 if (!(capabilities & VMCI_CAPS_DATAGRAM)) {
540 dev_err(&pdev->dev, "Device does not support datagrams\n");
541 error = -ENXIO;
542 goto err_free_data_buffer;
543 }
544 caps_in_use = VMCI_CAPS_DATAGRAM;
545
546 /*
547 * Use 64-bit PPNs if the device supports.
548 *
549 * There is no check for the return value of dma_set_mask_and_coherent
550 * since this driver can handle the default mask values if
551 * dma_set_mask_and_coherent fails.
552 */
553 if (capabilities & VMCI_CAPS_PPN64) {
554 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
555 use_ppn64 = true;
556 caps_in_use |= VMCI_CAPS_PPN64;
557 } else {
558 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
559 use_ppn64 = false;
560 }
561
562 /*
563 * If the hardware supports notifications, we will use that as
564 * well.
565 */
566 if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
567 vmci_dev->notification_bitmap = dma_alloc_coherent(
568 &pdev->dev, PAGE_SIZE, &vmci_dev->notification_base,
569 GFP_KERNEL);
570 if (!vmci_dev->notification_bitmap) {
571 dev_warn(&pdev->dev,
572 "Unable to allocate notification bitmap\n");
573 } else {
574 memset(vmci_dev->notification_bitmap, 0, PAGE_SIZE);
575 caps_in_use |= VMCI_CAPS_NOTIFICATIONS;
576 }
577 }
578
579 if (mmio_base != NULL) {
580 if (capabilities & VMCI_CAPS_DMA_DATAGRAM) {
581 caps_in_use |= VMCI_CAPS_DMA_DATAGRAM;
582 } else {
583 dev_err(&pdev->dev,
584 "Missing capability: VMCI_CAPS_DMA_DATAGRAM\n");
585 error = -ENXIO;
586 goto err_free_data_buffer;
587 }
588 }
589
590 dev_info(&pdev->dev, "Using capabilities 0x%x\n", caps_in_use);
591
592 /* Let the host know which capabilities we intend to use. */
593 vmci_write_reg(vmci_dev, caps_in_use, VMCI_CAPS_ADDR);
594
595 /* Let the device know the size for pages passed down. */
596 if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM)
597 vmci_write_reg(vmci_dev, PAGE_SHIFT, VMCI_GUEST_PAGE_SHIFT);
598
599 /* Set up global device so that we can start sending datagrams */
600 spin_lock_irq(&vmci_dev_spinlock);
601 vmci_dev_g = vmci_dev;
602 vmci_pdev = pdev;
603 spin_unlock_irq(&vmci_dev_spinlock);
604
605 /*
606 * Register notification bitmap with device if that capability is
607 * used.
608 */
609 if (caps_in_use & VMCI_CAPS_NOTIFICATIONS) {
610 unsigned long bitmap_ppn =
611 vmci_dev->notification_base >> PAGE_SHIFT;
612 if (!vmci_dbell_register_notification_bitmap(bitmap_ppn)) {
613 dev_warn(&pdev->dev,
614 "VMCI device unable to register notification bitmap with PPN 0x%lx\n",
615 bitmap_ppn);
616 error = -ENXIO;
617 goto err_remove_vmci_dev_g;
618 }
619 }
620
621 /* Check host capabilities. */
622 error = vmci_check_host_caps(pdev);
623 if (error)
624 goto err_remove_bitmap;
625
626 /* Enable device. */
627
628 /*
629 * We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can
630 * update the internal context id when needed.
631 */
632 vmci_err = vmci_event_subscribe(VMCI_EVENT_CTX_ID_UPDATE,
633 vmci_guest_cid_update, NULL,
634 &ctx_update_sub_id);
635 if (vmci_err < VMCI_SUCCESS)
636 dev_warn(&pdev->dev,
637 "Failed to subscribe to event (type=%d): %d\n",
638 VMCI_EVENT_CTX_ID_UPDATE, vmci_err);
639
640 /*
641 * Enable interrupts. Try MSI-X first, then MSI, and then fallback on
642 * legacy interrupts.
643 */
644 if (vmci_dev->mmio_base != NULL)
645 num_irq_vectors = VMCI_MAX_INTRS;
646 else
647 num_irq_vectors = VMCI_MAX_INTRS_NOTIFICATION;
648 error = pci_alloc_irq_vectors(pdev, num_irq_vectors, num_irq_vectors,
649 PCI_IRQ_MSIX);
650 if (error < 0) {
651 error = pci_alloc_irq_vectors(pdev, 1, 1,
652 PCI_IRQ_MSIX | PCI_IRQ_MSI | PCI_IRQ_LEGACY);
653 if (error < 0)
654 goto err_remove_bitmap;
655 } else {
656 vmci_dev->exclusive_vectors = true;
657 }
658
659 /*
660 * Request IRQ for legacy or MSI interrupts, or for first
661 * MSI-X vector.
662 */
663 error = request_irq(pci_irq_vector(pdev, 0), vmci_interrupt,
664 IRQF_SHARED, KBUILD_MODNAME, vmci_dev);
665 if (error) {
666 dev_err(&pdev->dev, "Irq %u in use: %d\n",
667 pci_irq_vector(pdev, 0), error);
668 goto err_disable_msi;
669 }
670
671 /*
672 * For MSI-X with exclusive vectors we need to request an
673 * interrupt for each vector so that we get a separate
674 * interrupt handler routine. This allows us to distinguish
675 * between the vectors.
676 */
677 if (vmci_dev->exclusive_vectors) {
678 error = request_irq(pci_irq_vector(pdev, 1),
679 vmci_interrupt_bm, 0, KBUILD_MODNAME,
680 vmci_dev);
681 if (error) {
682 dev_err(&pdev->dev,
683 "Failed to allocate irq %u: %d\n",
684 pci_irq_vector(pdev, 1), error);
685 goto err_free_irq;
686 }
687 if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM) {
688 error = request_irq(pci_irq_vector(pdev, 2),
689 vmci_interrupt_dma_datagram,
690 0, KBUILD_MODNAME, vmci_dev);
691 if (error) {
692 dev_err(&pdev->dev,
693 "Failed to allocate irq %u: %d\n",
694 pci_irq_vector(pdev, 2), error);
695 goto err_free_bm_irq;
696 }
697 }
698 }
699
700 dev_dbg(&pdev->dev, "Registered device\n");
701
702 atomic_inc(&vmci_num_guest_devices);
703
704 /* Enable specific interrupt bits. */
705 cmd = VMCI_IMR_DATAGRAM;
706 if (caps_in_use & VMCI_CAPS_NOTIFICATIONS)
707 cmd |= VMCI_IMR_NOTIFICATION;
708 if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM)
709 cmd |= VMCI_IMR_DMA_DATAGRAM;
710 vmci_write_reg(vmci_dev, cmd, VMCI_IMR_ADDR);
711
712 /* Enable interrupts. */
713 vmci_write_reg(vmci_dev, VMCI_CONTROL_INT_ENABLE, VMCI_CONTROL_ADDR);
714
715 pci_set_drvdata(pdev, vmci_dev);
716
717 vmci_call_vsock_callback(false);
718 return 0;
719
720 err_free_bm_irq:
721 free_irq(pci_irq_vector(pdev, 1), vmci_dev);
722 err_free_irq:
723 free_irq(pci_irq_vector(pdev, 0), vmci_dev);
724 tasklet_kill(&vmci_dev->datagram_tasklet);
725 tasklet_kill(&vmci_dev->bm_tasklet);
726
727 err_disable_msi:
728 pci_free_irq_vectors(pdev);
729
730 vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
731 if (vmci_err < VMCI_SUCCESS)
732 dev_warn(&pdev->dev,
733 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
734 VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
735
736 err_remove_bitmap:
737 if (vmci_dev->notification_bitmap) {
738 vmci_write_reg(vmci_dev, VMCI_CONTROL_RESET, VMCI_CONTROL_ADDR);
739 dma_free_coherent(&pdev->dev, PAGE_SIZE,
740 vmci_dev->notification_bitmap,
741 vmci_dev->notification_base);
742 }
743
744 err_remove_vmci_dev_g:
745 spin_lock_irq(&vmci_dev_spinlock);
746 vmci_pdev = NULL;
747 vmci_dev_g = NULL;
748 spin_unlock_irq(&vmci_dev_spinlock);
749
750 err_free_data_buffer:
751 vfree(vmci_dev->data_buffer);
752
753 /* The rest are managed resources and will be freed by PCI core */
754 return error;
755 }
756
757 static void vmci_guest_remove_device(struct pci_dev *pdev)
758 {
759 struct vmci_guest_device *vmci_dev = pci_get_drvdata(pdev);
760 int vmci_err;
761
762 dev_dbg(&pdev->dev, "Removing device\n");
763
764 atomic_dec(&vmci_num_guest_devices);
765
766 vmci_qp_guest_endpoints_exit();
767
768 vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
769 if (vmci_err < VMCI_SUCCESS)
770 dev_warn(&pdev->dev,
771 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
772 VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
773
774 spin_lock_irq(&vmci_dev_spinlock);
775 vmci_dev_g = NULL;
776 vmci_pdev = NULL;
777 spin_unlock_irq(&vmci_dev_spinlock);
778
779 dev_dbg(&pdev->dev, "Resetting vmci device\n");
780 vmci_write_reg(vmci_dev, VMCI_CONTROL_RESET, VMCI_CONTROL_ADDR);
781
782 /*
783 * Free IRQ and then disable MSI/MSI-X as appropriate. For
784 * MSI-X, we might have multiple vectors, each with their own
785 * IRQ, which we must free too.
786 */
787 if (vmci_dev->exclusive_vectors) {
788 free_irq(pci_irq_vector(pdev, 1), vmci_dev);
789 if (vmci_dev->mmio_base != NULL)
790 free_irq(pci_irq_vector(pdev, 2), vmci_dev);
791 }
792 free_irq(pci_irq_vector(pdev, 0), vmci_dev);
793 pci_free_irq_vectors(pdev);
794
795 tasklet_kill(&vmci_dev->datagram_tasklet);
796 tasklet_kill(&vmci_dev->bm_tasklet);
797
798 if (vmci_dev->notification_bitmap) {
799 /*
800 * The device reset above cleared the bitmap state of the
801 * device, so we can safely free it here.
802 */
803
804 dma_free_coherent(&pdev->dev, PAGE_SIZE,
805 vmci_dev->notification_bitmap,
806 vmci_dev->notification_base);
807 }
808
809 vfree(vmci_dev->data_buffer);
810
811 /* The rest are managed resources and will be freed by PCI core */
812 }
813
814 static const struct pci_device_id vmci_ids[] = {
815 { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_VMCI), },
816 { 0 },
817 };
818 MODULE_DEVICE_TABLE(pci, vmci_ids);
819
820 static struct pci_driver vmci_guest_driver = {
821 .name = KBUILD_MODNAME,
822 .id_table = vmci_ids,
823 .probe = vmci_guest_probe_device,
824 .remove = vmci_guest_remove_device,
825 };
826
827 int __init vmci_guest_init(void)
828 {
829 return pci_register_driver(&vmci_guest_driver);
830 }
831
832 void __exit vmci_guest_exit(void)
833 {
834 pci_unregister_driver(&vmci_guest_driver);
835 }