aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/xen
diff options
context:
space:
mode:
authorDmitry Torokhov <dmitry.torokhov@gmail.com>2012-01-09 02:38:23 -0500
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2012-01-09 02:38:23 -0500
commitda733563be5a9da26fe81d9f007262d00b846e22 (patch)
treedb28291df94a2043af2123911984c5c173da4e6f /drivers/xen
parent6ccbcf2cb41131f8d56ef0723bf3f7c1f8486076 (diff)
parentdab78d7924598ea4031663dd10db814e2e324928 (diff)
Merge branch 'next' into for-linus
Diffstat (limited to 'drivers/xen')
-rw-r--r--drivers/xen/Kconfig10
-rw-r--r--drivers/xen/Makefile4
-rw-r--r--drivers/xen/balloon.c76
-rw-r--r--drivers/xen/events.c98
-rw-r--r--drivers/xen/gntalloc.c4
-rw-r--r--drivers/xen/gntdev.c50
-rw-r--r--drivers/xen/grant-table.c8
-rw-r--r--drivers/xen/manage.c1
-rw-r--r--drivers/xen/pci.c107
-rw-r--r--drivers/xen/swiotlb-xen.c71
-rw-r--r--drivers/xen/xen-balloon.c17
-rw-r--r--drivers/xen/xen-pciback/conf_space.c2
-rw-r--r--drivers/xen/xen-pciback/conf_space_header.c5
-rw-r--r--drivers/xen/xen-pciback/conf_space_quirks.c3
-rw-r--r--drivers/xen/xen-pciback/passthrough.c34
-rw-r--r--drivers/xen/xen-pciback/pci_stub.c35
-rw-r--r--drivers/xen/xen-pciback/pciback.h32
-rw-r--r--drivers/xen/xen-pciback/pciback_ops.c1
-rw-r--r--drivers/xen/xen-pciback/vpci.c35
-rw-r--r--drivers/xen/xen-pciback/xenbus.c29
-rw-r--r--drivers/xen/xen-selfballoon.c67
-rw-r--r--drivers/xen/xenbus/xenbus_client.c16
-rw-r--r--drivers/xen/xenbus/xenbus_comms.c4
-rw-r--r--drivers/xen/xenbus/xenbus_probe.c107
-rw-r--r--drivers/xen/xenbus/xenbus_probe.h3
-rw-r--r--drivers/xen/xenbus/xenbus_probe_backend.c3
-rw-r--r--drivers/xen/xenbus/xenbus_probe_frontend.c122
-rw-r--r--drivers/xen/xenbus/xenbus_xs.c17
28 files changed, 663 insertions, 298 deletions
diff --git a/drivers/xen/Kconfig b/drivers/xen/Kconfig
index 5f7ff8e2fc14..8795480c2350 100644
--- a/drivers/xen/Kconfig
+++ b/drivers/xen/Kconfig
@@ -137,16 +137,6 @@ config XEN_GRANT_DEV_ALLOC
137 to other domains. This can be used to implement frontend drivers 137 to other domains. This can be used to implement frontend drivers
138 or as part of an inter-domain shared memory channel. 138 or as part of an inter-domain shared memory channel.
139 139
140config XEN_PLATFORM_PCI
141 tristate "xen platform pci device driver"
142 depends on XEN_PVHVM && PCI
143 default m
144 help
145 Driver for the Xen PCI Platform device: it is responsible for
146 initializing xenbus and grant_table when running in a Xen HVM
147 domain. As a consequence this driver is required to run any Xen PV
148 frontend on Xen HVM.
149
150config SWIOTLB_XEN 140config SWIOTLB_XEN
151 def_bool y 141 def_bool y
152 depends on PCI 142 depends on PCI
diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile
index 72bbb27d7a68..974fffdf22b2 100644
--- a/drivers/xen/Makefile
+++ b/drivers/xen/Makefile
@@ -14,7 +14,7 @@ obj-$(CONFIG_XEN_GNTDEV) += xen-gntdev.o
14obj-$(CONFIG_XEN_GRANT_DEV_ALLOC) += xen-gntalloc.o 14obj-$(CONFIG_XEN_GRANT_DEV_ALLOC) += xen-gntalloc.o
15obj-$(CONFIG_XENFS) += xenfs/ 15obj-$(CONFIG_XENFS) += xenfs/
16obj-$(CONFIG_XEN_SYS_HYPERVISOR) += sys-hypervisor.o 16obj-$(CONFIG_XEN_SYS_HYPERVISOR) += sys-hypervisor.o
17obj-$(CONFIG_XEN_PLATFORM_PCI) += xen-platform-pci.o 17obj-$(CONFIG_XEN_PVHVM) += platform-pci.o
18obj-$(CONFIG_XEN_TMEM) += tmem.o 18obj-$(CONFIG_XEN_TMEM) += tmem.o
19obj-$(CONFIG_SWIOTLB_XEN) += swiotlb-xen.o 19obj-$(CONFIG_SWIOTLB_XEN) += swiotlb-xen.o
20obj-$(CONFIG_XEN_DOM0) += pci.o 20obj-$(CONFIG_XEN_DOM0) += pci.o
@@ -23,5 +23,3 @@ obj-$(CONFIG_XEN_PCIDEV_BACKEND) += xen-pciback/
23xen-evtchn-y := evtchn.o 23xen-evtchn-y := evtchn.o
24xen-gntdev-y := gntdev.o 24xen-gntdev-y := gntdev.o
25xen-gntalloc-y := gntalloc.o 25xen-gntalloc-y := gntalloc.o
26
27xen-platform-pci-y := platform-pci.o
diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c
index 5dfd8f8ff07f..31ab82fda38a 100644
--- a/drivers/xen/balloon.c
+++ b/drivers/xen/balloon.c
@@ -39,6 +39,7 @@
39#include <linux/kernel.h> 39#include <linux/kernel.h>
40#include <linux/sched.h> 40#include <linux/sched.h>
41#include <linux/errno.h> 41#include <linux/errno.h>
42#include <linux/module.h>
42#include <linux/mm.h> 43#include <linux/mm.h>
43#include <linux/bootmem.h> 44#include <linux/bootmem.h>
44#include <linux/pagemap.h> 45#include <linux/pagemap.h>
@@ -93,8 +94,8 @@ static unsigned long frame_list[PAGE_SIZE / sizeof(unsigned long)];
93#define inc_totalhigh_pages() (totalhigh_pages++) 94#define inc_totalhigh_pages() (totalhigh_pages++)
94#define dec_totalhigh_pages() (totalhigh_pages--) 95#define dec_totalhigh_pages() (totalhigh_pages--)
95#else 96#else
96#define inc_totalhigh_pages() do {} while(0) 97#define inc_totalhigh_pages() do {} while (0)
97#define dec_totalhigh_pages() do {} while(0) 98#define dec_totalhigh_pages() do {} while (0)
98#endif 99#endif
99 100
100/* List of ballooned pages, threaded through the mem_map array. */ 101/* List of ballooned pages, threaded through the mem_map array. */
@@ -154,8 +155,7 @@ static struct page *balloon_retrieve(bool prefer_highmem)
154 if (PageHighMem(page)) { 155 if (PageHighMem(page)) {
155 balloon_stats.balloon_high--; 156 balloon_stats.balloon_high--;
156 inc_totalhigh_pages(); 157 inc_totalhigh_pages();
157 } 158 } else
158 else
159 balloon_stats.balloon_low--; 159 balloon_stats.balloon_low--;
160 160
161 totalram_pages++; 161 totalram_pages++;
@@ -422,7 +422,7 @@ static enum bp_state decrease_reservation(unsigned long nr_pages, gfp_t gfp)
422 (unsigned long)__va(pfn << PAGE_SHIFT), 422 (unsigned long)__va(pfn << PAGE_SHIFT),
423 __pte_ma(0), 0); 423 __pte_ma(0), 0);
424 BUG_ON(ret); 424 BUG_ON(ret);
425 } 425 }
426 426
427 } 427 }
428 428
@@ -501,20 +501,24 @@ EXPORT_SYMBOL_GPL(balloon_set_new_target);
501 * alloc_xenballooned_pages - get pages that have been ballooned out 501 * alloc_xenballooned_pages - get pages that have been ballooned out
502 * @nr_pages: Number of pages to get 502 * @nr_pages: Number of pages to get
503 * @pages: pages returned 503 * @pages: pages returned
504 * @highmem: allow highmem pages
504 * @return 0 on success, error otherwise 505 * @return 0 on success, error otherwise
505 */ 506 */
506int alloc_xenballooned_pages(int nr_pages, struct page** pages) 507int alloc_xenballooned_pages(int nr_pages, struct page **pages, bool highmem)
507{ 508{
508 int pgno = 0; 509 int pgno = 0;
509 struct page* page; 510 struct page *page;
510 mutex_lock(&balloon_mutex); 511 mutex_lock(&balloon_mutex);
511 while (pgno < nr_pages) { 512 while (pgno < nr_pages) {
512 page = balloon_retrieve(true); 513 page = balloon_retrieve(highmem);
513 if (page) { 514 if (page && (highmem || !PageHighMem(page))) {
514 pages[pgno++] = page; 515 pages[pgno++] = page;
515 } else { 516 } else {
516 enum bp_state st; 517 enum bp_state st;
517 st = decrease_reservation(nr_pages - pgno, GFP_HIGHUSER); 518 if (page)
519 balloon_append(page);
520 st = decrease_reservation(nr_pages - pgno,
521 highmem ? GFP_HIGHUSER : GFP_USER);
518 if (st != BP_DONE) 522 if (st != BP_DONE)
519 goto out_undo; 523 goto out_undo;
520 } 524 }
@@ -536,7 +540,7 @@ EXPORT_SYMBOL(alloc_xenballooned_pages);
536 * @nr_pages: Number of pages 540 * @nr_pages: Number of pages
537 * @pages: pages to return 541 * @pages: pages to return
538 */ 542 */
539void free_xenballooned_pages(int nr_pages, struct page** pages) 543void free_xenballooned_pages(int nr_pages, struct page **pages)
540{ 544{
541 int i; 545 int i;
542 546
@@ -555,17 +559,40 @@ void free_xenballooned_pages(int nr_pages, struct page** pages)
555} 559}
556EXPORT_SYMBOL(free_xenballooned_pages); 560EXPORT_SYMBOL(free_xenballooned_pages);
557 561
558static int __init balloon_init(void) 562static void __init balloon_add_region(unsigned long start_pfn,
563 unsigned long pages)
559{ 564{
560 unsigned long pfn, extra_pfn_end; 565 unsigned long pfn, extra_pfn_end;
561 struct page *page; 566 struct page *page;
562 567
568 /*
569 * If the amount of usable memory has been limited (e.g., with
570 * the 'mem' command line parameter), don't add pages beyond
571 * this limit.
572 */
573 extra_pfn_end = min(max_pfn, start_pfn + pages);
574
575 for (pfn = start_pfn; pfn < extra_pfn_end; pfn++) {
576 page = pfn_to_page(pfn);
577 /* totalram_pages and totalhigh_pages do not
578 include the boot-time balloon extension, so
579 don't subtract from it. */
580 __balloon_append(page);
581 }
582}
583
584static int __init balloon_init(void)
585{
586 int i;
587
563 if (!xen_domain()) 588 if (!xen_domain())
564 return -ENODEV; 589 return -ENODEV;
565 590
566 pr_info("xen/balloon: Initialising balloon driver.\n"); 591 pr_info("xen/balloon: Initialising balloon driver.\n");
567 592
568 balloon_stats.current_pages = xen_pv_domain() ? min(xen_start_info->nr_pages, max_pfn) : max_pfn; 593 balloon_stats.current_pages = xen_pv_domain()
594 ? min(xen_start_info->nr_pages - xen_released_pages, max_pfn)
595 : max_pfn;
569 balloon_stats.target_pages = balloon_stats.current_pages; 596 balloon_stats.target_pages = balloon_stats.current_pages;
570 balloon_stats.balloon_low = 0; 597 balloon_stats.balloon_low = 0;
571 balloon_stats.balloon_high = 0; 598 balloon_stats.balloon_high = 0;
@@ -584,24 +611,13 @@ static int __init balloon_init(void)
584#endif 611#endif
585 612
586 /* 613 /*
587 * Initialise the balloon with excess memory space. We need 614 * Initialize the balloon with pages from the extra memory
588 * to make sure we don't add memory which doesn't exist or 615 * regions (see arch/x86/xen/setup.c).
589 * logically exist. The E820 map can be trimmed to be smaller
590 * than the amount of physical memory due to the mem= command
591 * line parameter. And if this is a 32-bit non-HIGHMEM kernel
592 * on a system with memory which requires highmem to access,
593 * don't try to use it.
594 */ 616 */
595 extra_pfn_end = min(min(max_pfn, e820_end_of_ram_pfn()), 617 for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++)
596 (unsigned long)PFN_DOWN(xen_extra_mem_start + xen_extra_mem_size)); 618 if (xen_extra_mem[i].size)
597 for (pfn = PFN_UP(xen_extra_mem_start); 619 balloon_add_region(PFN_UP(xen_extra_mem[i].start),
598 pfn < extra_pfn_end; 620 PFN_DOWN(xen_extra_mem[i].size));
599 pfn++) {
600 page = pfn_to_page(pfn);
601 /* totalram_pages and totalhigh_pages do not include the boot-time
602 balloon extension, so don't subtract from it. */
603 __balloon_append(page);
604 }
605 621
606 return 0; 622 return 0;
607} 623}
diff --git a/drivers/xen/events.c b/drivers/xen/events.c
index da70f5c32eb9..6e075cdd0c6b 100644
--- a/drivers/xen/events.c
+++ b/drivers/xen/events.c
@@ -54,7 +54,7 @@
54 * This lock protects updates to the following mapping and reference-count 54 * This lock protects updates to the following mapping and reference-count
55 * arrays. The lock does not need to be acquired to read the mapping tables. 55 * arrays. The lock does not need to be acquired to read the mapping tables.
56 */ 56 */
57static DEFINE_SPINLOCK(irq_mapping_update_lock); 57static DEFINE_MUTEX(irq_mapping_update_lock);
58 58
59static LIST_HEAD(xen_irq_list_head); 59static LIST_HEAD(xen_irq_list_head);
60 60
@@ -85,8 +85,7 @@ enum xen_irq_type {
85 * IPI - IPI vector 85 * IPI - IPI vector
86 * EVTCHN - 86 * EVTCHN -
87 */ 87 */
88struct irq_info 88struct irq_info {
89{
90 struct list_head list; 89 struct list_head list;
91 enum xen_irq_type type; /* type */ 90 enum xen_irq_type type; /* type */
92 unsigned irq; 91 unsigned irq;
@@ -282,9 +281,9 @@ static inline unsigned long active_evtchns(unsigned int cpu,
282 struct shared_info *sh, 281 struct shared_info *sh,
283 unsigned int idx) 282 unsigned int idx)
284{ 283{
285 return (sh->evtchn_pending[idx] & 284 return sh->evtchn_pending[idx] &
286 per_cpu(cpu_evtchn_mask, cpu)[idx] & 285 per_cpu(cpu_evtchn_mask, cpu)[idx] &
287 ~sh->evtchn_mask[idx]); 286 ~sh->evtchn_mask[idx];
288} 287}
289 288
290static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu) 289static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
@@ -432,7 +431,8 @@ static int __must_check xen_allocate_irq_dynamic(void)
432 431
433 irq = irq_alloc_desc_from(first, -1); 432 irq = irq_alloc_desc_from(first, -1);
434 433
435 xen_irq_init(irq); 434 if (irq >= 0)
435 xen_irq_init(irq);
436 436
437 return irq; 437 return irq;
438} 438}
@@ -631,7 +631,7 @@ int xen_bind_pirq_gsi_to_irq(unsigned gsi,
631 int irq = -1; 631 int irq = -1;
632 struct physdev_irq irq_op; 632 struct physdev_irq irq_op;
633 633
634 spin_lock(&irq_mapping_update_lock); 634 mutex_lock(&irq_mapping_update_lock);
635 635
636 irq = find_irq_by_gsi(gsi); 636 irq = find_irq_by_gsi(gsi);
637 if (irq != -1) { 637 if (irq != -1) {
@@ -684,7 +684,7 @@ int xen_bind_pirq_gsi_to_irq(unsigned gsi,
684 handle_edge_irq, name); 684 handle_edge_irq, name);
685 685
686out: 686out:
687 spin_unlock(&irq_mapping_update_lock); 687 mutex_unlock(&irq_mapping_update_lock);
688 688
689 return irq; 689 return irq;
690} 690}
@@ -710,10 +710,10 @@ int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
710{ 710{
711 int irq, ret; 711 int irq, ret;
712 712
713 spin_lock(&irq_mapping_update_lock); 713 mutex_lock(&irq_mapping_update_lock);
714 714
715 irq = xen_allocate_irq_dynamic(); 715 irq = xen_allocate_irq_dynamic();
716 if (irq == -1) 716 if (irq < 0)
717 goto out; 717 goto out;
718 718
719 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq, 719 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
@@ -724,12 +724,12 @@ int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
724 if (ret < 0) 724 if (ret < 0)
725 goto error_irq; 725 goto error_irq;
726out: 726out:
727 spin_unlock(&irq_mapping_update_lock); 727 mutex_unlock(&irq_mapping_update_lock);
728 return irq; 728 return irq;
729error_irq: 729error_irq:
730 spin_unlock(&irq_mapping_update_lock); 730 mutex_unlock(&irq_mapping_update_lock);
731 xen_free_irq(irq); 731 xen_free_irq(irq);
732 return -1; 732 return ret;
733} 733}
734#endif 734#endif
735 735
@@ -740,7 +740,7 @@ int xen_destroy_irq(int irq)
740 struct irq_info *info = info_for_irq(irq); 740 struct irq_info *info = info_for_irq(irq);
741 int rc = -ENOENT; 741 int rc = -ENOENT;
742 742
743 spin_lock(&irq_mapping_update_lock); 743 mutex_lock(&irq_mapping_update_lock);
744 744
745 desc = irq_to_desc(irq); 745 desc = irq_to_desc(irq);
746 if (!desc) 746 if (!desc)
@@ -766,7 +766,7 @@ int xen_destroy_irq(int irq)
766 xen_free_irq(irq); 766 xen_free_irq(irq);
767 767
768out: 768out:
769 spin_unlock(&irq_mapping_update_lock); 769 mutex_unlock(&irq_mapping_update_lock);
770 return rc; 770 return rc;
771} 771}
772 772
@@ -776,10 +776,10 @@ int xen_irq_from_pirq(unsigned pirq)
776 776
777 struct irq_info *info; 777 struct irq_info *info;
778 778
779 spin_lock(&irq_mapping_update_lock); 779 mutex_lock(&irq_mapping_update_lock);
780 780
781 list_for_each_entry(info, &xen_irq_list_head, list) { 781 list_for_each_entry(info, &xen_irq_list_head, list) {
782 if (info == NULL || info->type != IRQT_PIRQ) 782 if (info->type != IRQT_PIRQ)
783 continue; 783 continue;
784 irq = info->irq; 784 irq = info->irq;
785 if (info->u.pirq.pirq == pirq) 785 if (info->u.pirq.pirq == pirq)
@@ -787,7 +787,7 @@ int xen_irq_from_pirq(unsigned pirq)
787 } 787 }
788 irq = -1; 788 irq = -1;
789out: 789out:
790 spin_unlock(&irq_mapping_update_lock); 790 mutex_unlock(&irq_mapping_update_lock);
791 791
792 return irq; 792 return irq;
793} 793}
@@ -802,7 +802,7 @@ int bind_evtchn_to_irq(unsigned int evtchn)
802{ 802{
803 int irq; 803 int irq;
804 804
805 spin_lock(&irq_mapping_update_lock); 805 mutex_lock(&irq_mapping_update_lock);
806 806
807 irq = evtchn_to_irq[evtchn]; 807 irq = evtchn_to_irq[evtchn];
808 808
@@ -818,7 +818,7 @@ int bind_evtchn_to_irq(unsigned int evtchn)
818 } 818 }
819 819
820out: 820out:
821 spin_unlock(&irq_mapping_update_lock); 821 mutex_unlock(&irq_mapping_update_lock);
822 822
823 return irq; 823 return irq;
824} 824}
@@ -829,7 +829,7 @@ static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
829 struct evtchn_bind_ipi bind_ipi; 829 struct evtchn_bind_ipi bind_ipi;
830 int evtchn, irq; 830 int evtchn, irq;
831 831
832 spin_lock(&irq_mapping_update_lock); 832 mutex_lock(&irq_mapping_update_lock);
833 833
834 irq = per_cpu(ipi_to_irq, cpu)[ipi]; 834 irq = per_cpu(ipi_to_irq, cpu)[ipi];
835 835
@@ -853,7 +853,7 @@ static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
853 } 853 }
854 854
855 out: 855 out:
856 spin_unlock(&irq_mapping_update_lock); 856 mutex_unlock(&irq_mapping_update_lock);
857 return irq; 857 return irq;
858} 858}
859 859
@@ -872,13 +872,34 @@ static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
872 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port); 872 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
873} 873}
874 874
875static int find_virq(unsigned int virq, unsigned int cpu)
876{
877 struct evtchn_status status;
878 int port, rc = -ENOENT;
879
880 memset(&status, 0, sizeof(status));
881 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
882 status.dom = DOMID_SELF;
883 status.port = port;
884 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
885 if (rc < 0)
886 continue;
887 if (status.status != EVTCHNSTAT_virq)
888 continue;
889 if (status.u.virq == virq && status.vcpu == cpu) {
890 rc = port;
891 break;
892 }
893 }
894 return rc;
895}
875 896
876int bind_virq_to_irq(unsigned int virq, unsigned int cpu) 897int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
877{ 898{
878 struct evtchn_bind_virq bind_virq; 899 struct evtchn_bind_virq bind_virq;
879 int evtchn, irq; 900 int evtchn, irq, ret;
880 901
881 spin_lock(&irq_mapping_update_lock); 902 mutex_lock(&irq_mapping_update_lock);
882 903
883 irq = per_cpu(virq_to_irq, cpu)[virq]; 904 irq = per_cpu(virq_to_irq, cpu)[virq];
884 905
@@ -892,10 +913,16 @@ int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
892 913
893 bind_virq.virq = virq; 914 bind_virq.virq = virq;
894 bind_virq.vcpu = cpu; 915 bind_virq.vcpu = cpu;
895 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, 916 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
896 &bind_virq) != 0) 917 &bind_virq);
897 BUG(); 918 if (ret == 0)
898 evtchn = bind_virq.port; 919 evtchn = bind_virq.port;
920 else {
921 if (ret == -EEXIST)
922 ret = find_virq(virq, cpu);
923 BUG_ON(ret < 0);
924 evtchn = ret;
925 }
899 926
900 xen_irq_info_virq_init(cpu, irq, evtchn, virq); 927 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
901 928
@@ -903,7 +930,7 @@ int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
903 } 930 }
904 931
905out: 932out:
906 spin_unlock(&irq_mapping_update_lock); 933 mutex_unlock(&irq_mapping_update_lock);
907 934
908 return irq; 935 return irq;
909} 936}
@@ -913,7 +940,7 @@ static void unbind_from_irq(unsigned int irq)
913 struct evtchn_close close; 940 struct evtchn_close close;
914 int evtchn = evtchn_from_irq(irq); 941 int evtchn = evtchn_from_irq(irq);
915 942
916 spin_lock(&irq_mapping_update_lock); 943 mutex_lock(&irq_mapping_update_lock);
917 944
918 if (VALID_EVTCHN(evtchn)) { 945 if (VALID_EVTCHN(evtchn)) {
919 close.port = evtchn; 946 close.port = evtchn;
@@ -943,7 +970,7 @@ static void unbind_from_irq(unsigned int irq)
943 970
944 xen_free_irq(irq); 971 xen_free_irq(irq);
945 972
946 spin_unlock(&irq_mapping_update_lock); 973 mutex_unlock(&irq_mapping_update_lock);
947} 974}
948 975
949int bind_evtchn_to_irqhandler(unsigned int evtchn, 976int bind_evtchn_to_irqhandler(unsigned int evtchn,
@@ -1021,7 +1048,7 @@ int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1021 if (irq < 0) 1048 if (irq < 0)
1022 return irq; 1049 return irq;
1023 1050
1024 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME; 1051 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1025 retval = request_irq(irq, handler, irqflags, devname, dev_id); 1052 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1026 if (retval != 0) { 1053 if (retval != 0) {
1027 unbind_from_irq(irq); 1054 unbind_from_irq(irq);
@@ -1152,7 +1179,7 @@ static void __xen_evtchn_do_upcall(void)
1152 int cpu = get_cpu(); 1179 int cpu = get_cpu();
1153 struct shared_info *s = HYPERVISOR_shared_info; 1180 struct shared_info *s = HYPERVISOR_shared_info;
1154 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu); 1181 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1155 unsigned count; 1182 unsigned count;
1156 1183
1157 do { 1184 do {
1158 unsigned long pending_words; 1185 unsigned long pending_words;
@@ -1279,7 +1306,7 @@ void rebind_evtchn_irq(int evtchn, int irq)
1279 will also be masked. */ 1306 will also be masked. */
1280 disable_irq(irq); 1307 disable_irq(irq);
1281 1308
1282 spin_lock(&irq_mapping_update_lock); 1309 mutex_lock(&irq_mapping_update_lock);
1283 1310
1284 /* After resume the irq<->evtchn mappings are all cleared out */ 1311 /* After resume the irq<->evtchn mappings are all cleared out */
1285 BUG_ON(evtchn_to_irq[evtchn] != -1); 1312 BUG_ON(evtchn_to_irq[evtchn] != -1);
@@ -1289,7 +1316,7 @@ void rebind_evtchn_irq(int evtchn, int irq)
1289 1316
1290 xen_irq_info_evtchn_init(irq, evtchn); 1317 xen_irq_info_evtchn_init(irq, evtchn);
1291 1318
1292 spin_unlock(&irq_mapping_update_lock); 1319 mutex_unlock(&irq_mapping_update_lock);
1293 1320
1294 /* new event channels are always bound to cpu 0 */ 1321 /* new event channels are always bound to cpu 0 */
1295 irq_set_affinity(irq, cpumask_of(0)); 1322 irq_set_affinity(irq, cpumask_of(0));
@@ -1670,6 +1697,7 @@ void __init xen_init_IRQ(void)
1670 1697
1671 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq), 1698 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1672 GFP_KERNEL); 1699 GFP_KERNEL);
1700 BUG_ON(!evtchn_to_irq);
1673 for (i = 0; i < NR_EVENT_CHANNELS; i++) 1701 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1674 evtchn_to_irq[i] = -1; 1702 evtchn_to_irq[i] = -1;
1675 1703
diff --git a/drivers/xen/gntalloc.c b/drivers/xen/gntalloc.c
index f6832f46aea4..e1c4c6e5b469 100644
--- a/drivers/xen/gntalloc.c
+++ b/drivers/xen/gntalloc.c
@@ -135,7 +135,7 @@ static int add_grefs(struct ioctl_gntalloc_alloc_gref *op,
135 /* Grant foreign access to the page. */ 135 /* Grant foreign access to the page. */
136 gref->gref_id = gnttab_grant_foreign_access(op->domid, 136 gref->gref_id = gnttab_grant_foreign_access(op->domid,
137 pfn_to_mfn(page_to_pfn(gref->page)), readonly); 137 pfn_to_mfn(page_to_pfn(gref->page)), readonly);
138 if (gref->gref_id < 0) { 138 if ((int)gref->gref_id < 0) {
139 rc = gref->gref_id; 139 rc = gref->gref_id;
140 goto undo; 140 goto undo;
141 } 141 }
@@ -280,7 +280,7 @@ static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv,
280 goto out; 280 goto out;
281 } 281 }
282 282
283 gref_ids = kzalloc(sizeof(gref_ids[0]) * op.count, GFP_TEMPORARY); 283 gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_TEMPORARY);
284 if (!gref_ids) { 284 if (!gref_ids) {
285 rc = -ENOMEM; 285 rc = -ENOMEM;
286 goto out; 286 goto out;
diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c
index f914b26cf0c2..afca14d9042e 100644
--- a/drivers/xen/gntdev.c
+++ b/drivers/xen/gntdev.c
@@ -83,6 +83,7 @@ struct grant_map {
83 struct ioctl_gntdev_grant_ref *grants; 83 struct ioctl_gntdev_grant_ref *grants;
84 struct gnttab_map_grant_ref *map_ops; 84 struct gnttab_map_grant_ref *map_ops;
85 struct gnttab_unmap_grant_ref *unmap_ops; 85 struct gnttab_unmap_grant_ref *unmap_ops;
86 struct gnttab_map_grant_ref *kmap_ops;
86 struct page **pages; 87 struct page **pages;
87}; 88};
88 89
@@ -113,22 +114,25 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
113 if (NULL == add) 114 if (NULL == add)
114 return NULL; 115 return NULL;
115 116
116 add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL); 117 add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
117 add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL); 118 add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
118 add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL); 119 add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
119 add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL); 120 add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
121 add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
120 if (NULL == add->grants || 122 if (NULL == add->grants ||
121 NULL == add->map_ops || 123 NULL == add->map_ops ||
122 NULL == add->unmap_ops || 124 NULL == add->unmap_ops ||
125 NULL == add->kmap_ops ||
123 NULL == add->pages) 126 NULL == add->pages)
124 goto err; 127 goto err;
125 128
126 if (alloc_xenballooned_pages(count, add->pages)) 129 if (alloc_xenballooned_pages(count, add->pages, false /* lowmem */))
127 goto err; 130 goto err;
128 131
129 for (i = 0; i < count; i++) { 132 for (i = 0; i < count; i++) {
130 add->map_ops[i].handle = -1; 133 add->map_ops[i].handle = -1;
131 add->unmap_ops[i].handle = -1; 134 add->unmap_ops[i].handle = -1;
135 add->kmap_ops[i].handle = -1;
132 } 136 }
133 137
134 add->index = 0; 138 add->index = 0;
@@ -142,6 +146,7 @@ err:
142 kfree(add->grants); 146 kfree(add->grants);
143 kfree(add->map_ops); 147 kfree(add->map_ops);
144 kfree(add->unmap_ops); 148 kfree(add->unmap_ops);
149 kfree(add->kmap_ops);
145 kfree(add); 150 kfree(add);
146 return NULL; 151 return NULL;
147} 152}
@@ -188,9 +193,8 @@ static void gntdev_put_map(struct grant_map *map)
188 193
189 atomic_sub(map->count, &pages_mapped); 194 atomic_sub(map->count, &pages_mapped);
190 195
191 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { 196 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
192 notify_remote_via_evtchn(map->notify.event); 197 notify_remote_via_evtchn(map->notify.event);
193 }
194 198
195 if (map->pages) { 199 if (map->pages) {
196 if (!use_ptemod) 200 if (!use_ptemod)
@@ -243,10 +247,35 @@ static int map_grant_pages(struct grant_map *map)
243 gnttab_set_unmap_op(&map->unmap_ops[i], addr, 247 gnttab_set_unmap_op(&map->unmap_ops[i], addr,
244 map->flags, -1 /* handle */); 248 map->flags, -1 /* handle */);
245 } 249 }
250 } else {
251 /*
252 * Setup the map_ops corresponding to the pte entries pointing
253 * to the kernel linear addresses of the struct pages.
254 * These ptes are completely different from the user ptes dealt
255 * with find_grant_ptes.
256 */
257 for (i = 0; i < map->count; i++) {
258 unsigned level;
259 unsigned long address = (unsigned long)
260 pfn_to_kaddr(page_to_pfn(map->pages[i]));
261 pte_t *ptep;
262 u64 pte_maddr = 0;
263 BUG_ON(PageHighMem(map->pages[i]));
264
265 ptep = lookup_address(address, &level);
266 pte_maddr = arbitrary_virt_to_machine(ptep).maddr;
267 gnttab_set_map_op(&map->kmap_ops[i], pte_maddr,
268 map->flags |
269 GNTMAP_host_map |
270 GNTMAP_contains_pte,
271 map->grants[i].ref,
272 map->grants[i].domid);
273 }
246 } 274 }
247 275
248 pr_debug("map %d+%d\n", map->index, map->count); 276 pr_debug("map %d+%d\n", map->index, map->count);
249 err = gnttab_map_refs(map->map_ops, map->pages, map->count); 277 err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
278 map->pages, map->count);
250 if (err) 279 if (err)
251 return err; 280 return err;
252 281
@@ -462,13 +491,11 @@ static int gntdev_release(struct inode *inode, struct file *flip)
462 491
463 pr_debug("priv %p\n", priv); 492 pr_debug("priv %p\n", priv);
464 493
465 spin_lock(&priv->lock);
466 while (!list_empty(&priv->maps)) { 494 while (!list_empty(&priv->maps)) {
467 map = list_entry(priv->maps.next, struct grant_map, next); 495 map = list_entry(priv->maps.next, struct grant_map, next);
468 list_del(&map->next); 496 list_del(&map->next);
469 gntdev_put_map(map); 497 gntdev_put_map(map);
470 } 498 }
471 spin_unlock(&priv->lock);
472 499
473 if (use_ptemod) 500 if (use_ptemod)
474 mmu_notifier_unregister(&priv->mn, priv->mm); 501 mmu_notifier_unregister(&priv->mn, priv->mm);
@@ -532,10 +559,11 @@ static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
532 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count); 559 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
533 if (map) { 560 if (map) {
534 list_del(&map->next); 561 list_del(&map->next);
535 gntdev_put_map(map);
536 err = 0; 562 err = 0;
537 } 563 }
538 spin_unlock(&priv->lock); 564 spin_unlock(&priv->lock);
565 if (map)
566 gntdev_put_map(map);
539 return err; 567 return err;
540} 568}
541 569
diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c
index 4f44b347b24a..bf1c094f4ebf 100644
--- a/drivers/xen/grant-table.c
+++ b/drivers/xen/grant-table.c
@@ -193,7 +193,7 @@ int gnttab_query_foreign_access(grant_ref_t ref)
193 193
194 nflags = shared[ref].flags; 194 nflags = shared[ref].flags;
195 195
196 return (nflags & (GTF_reading|GTF_writing)); 196 return nflags & (GTF_reading|GTF_writing);
197} 197}
198EXPORT_SYMBOL_GPL(gnttab_query_foreign_access); 198EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
199 199
@@ -448,7 +448,8 @@ unsigned int gnttab_max_grant_frames(void)
448EXPORT_SYMBOL_GPL(gnttab_max_grant_frames); 448EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
449 449
450int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops, 450int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
451 struct page **pages, unsigned int count) 451 struct gnttab_map_grant_ref *kmap_ops,
452 struct page **pages, unsigned int count)
452{ 453{
453 int i, ret; 454 int i, ret;
454 pte_t *pte; 455 pte_t *pte;
@@ -488,8 +489,7 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
488 */ 489 */
489 return -EOPNOTSUPP; 490 return -EOPNOTSUPP;
490 } 491 }
491 ret = m2p_add_override(mfn, pages[i], 492 ret = m2p_add_override(mfn, pages[i], &kmap_ops[i]);
492 map_ops[i].flags & GNTMAP_contains_pte);
493 if (ret) 493 if (ret)
494 return ret; 494 return ret;
495 } 495 }
diff --git a/drivers/xen/manage.c b/drivers/xen/manage.c
index 0b5366b5be20..ce4fa0831860 100644
--- a/drivers/xen/manage.c
+++ b/drivers/xen/manage.c
@@ -9,6 +9,7 @@
9#include <linux/stop_machine.h> 9#include <linux/stop_machine.h>
10#include <linux/freezer.h> 10#include <linux/freezer.h>
11#include <linux/syscore_ops.h> 11#include <linux/syscore_ops.h>
12#include <linux/export.h>
12 13
13#include <xen/xen.h> 14#include <xen/xen.h>
14#include <xen/xenbus.h> 15#include <xen/xenbus.h>
diff --git a/drivers/xen/pci.c b/drivers/xen/pci.c
index cef4bafc07dc..b84bf0b6cc34 100644
--- a/drivers/xen/pci.c
+++ b/drivers/xen/pci.c
@@ -18,6 +18,7 @@
18 */ 18 */
19 19
20#include <linux/pci.h> 20#include <linux/pci.h>
21#include <linux/acpi.h>
21#include <xen/xen.h> 22#include <xen/xen.h>
22#include <xen/interface/physdev.h> 23#include <xen/interface/physdev.h>
23#include <xen/interface/xen.h> 24#include <xen/interface/xen.h>
@@ -26,26 +27,85 @@
26#include <asm/xen/hypercall.h> 27#include <asm/xen/hypercall.h>
27#include "../pci/pci.h" 28#include "../pci/pci.h"
28 29
30static bool __read_mostly pci_seg_supported = true;
31
29static int xen_add_device(struct device *dev) 32static int xen_add_device(struct device *dev)
30{ 33{
31 int r; 34 int r;
32 struct pci_dev *pci_dev = to_pci_dev(dev); 35 struct pci_dev *pci_dev = to_pci_dev(dev);
36#ifdef CONFIG_PCI_IOV
37 struct pci_dev *physfn = pci_dev->physfn;
38#endif
39
40 if (pci_seg_supported) {
41 struct physdev_pci_device_add add = {
42 .seg = pci_domain_nr(pci_dev->bus),
43 .bus = pci_dev->bus->number,
44 .devfn = pci_dev->devfn
45 };
46#ifdef CONFIG_ACPI
47 acpi_handle handle;
48#endif
49
50#ifdef CONFIG_PCI_IOV
51 if (pci_dev->is_virtfn) {
52 add.flags = XEN_PCI_DEV_VIRTFN;
53 add.physfn.bus = physfn->bus->number;
54 add.physfn.devfn = physfn->devfn;
55 } else
56#endif
57 if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
58 add.flags = XEN_PCI_DEV_EXTFN;
59
60#ifdef CONFIG_ACPI
61 handle = DEVICE_ACPI_HANDLE(&pci_dev->dev);
62 if (!handle)
63 handle = DEVICE_ACPI_HANDLE(pci_dev->bus->bridge);
64#ifdef CONFIG_PCI_IOV
65 if (!handle && pci_dev->is_virtfn)
66 handle = DEVICE_ACPI_HANDLE(physfn->bus->bridge);
67#endif
68 if (handle) {
69 acpi_status status;
70
71 do {
72 unsigned long long pxm;
73
74 status = acpi_evaluate_integer(handle, "_PXM",
75 NULL, &pxm);
76 if (ACPI_SUCCESS(status)) {
77 add.optarr[0] = pxm;
78 add.flags |= XEN_PCI_DEV_PXM;
79 break;
80 }
81 status = acpi_get_parent(handle, &handle);
82 } while (ACPI_SUCCESS(status));
83 }
84#endif /* CONFIG_ACPI */
85
86 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, &add);
87 if (r != -ENOSYS)
88 return r;
89 pci_seg_supported = false;
90 }
33 91
92 if (pci_domain_nr(pci_dev->bus))
93 r = -ENOSYS;
34#ifdef CONFIG_PCI_IOV 94#ifdef CONFIG_PCI_IOV
35 if (pci_dev->is_virtfn) { 95 else if (pci_dev->is_virtfn) {
36 struct physdev_manage_pci_ext manage_pci_ext = { 96 struct physdev_manage_pci_ext manage_pci_ext = {
37 .bus = pci_dev->bus->number, 97 .bus = pci_dev->bus->number,
38 .devfn = pci_dev->devfn, 98 .devfn = pci_dev->devfn,
39 .is_virtfn = 1, 99 .is_virtfn = 1,
40 .physfn.bus = pci_dev->physfn->bus->number, 100 .physfn.bus = physfn->bus->number,
41 .physfn.devfn = pci_dev->physfn->devfn, 101 .physfn.devfn = physfn->devfn,
42 }; 102 };
43 103
44 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext, 104 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
45 &manage_pci_ext); 105 &manage_pci_ext);
46 } else 106 }
47#endif 107#endif
48 if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) { 108 else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
49 struct physdev_manage_pci_ext manage_pci_ext = { 109 struct physdev_manage_pci_ext manage_pci_ext = {
50 .bus = pci_dev->bus->number, 110 .bus = pci_dev->bus->number,
51 .devfn = pci_dev->devfn, 111 .devfn = pci_dev->devfn,
@@ -56,7 +116,7 @@ static int xen_add_device(struct device *dev)
56 &manage_pci_ext); 116 &manage_pci_ext);
57 } else { 117 } else {
58 struct physdev_manage_pci manage_pci = { 118 struct physdev_manage_pci manage_pci = {
59 .bus = pci_dev->bus->number, 119 .bus = pci_dev->bus->number,
60 .devfn = pci_dev->devfn, 120 .devfn = pci_dev->devfn,
61 }; 121 };
62 122
@@ -71,13 +131,27 @@ static int xen_remove_device(struct device *dev)
71{ 131{
72 int r; 132 int r;
73 struct pci_dev *pci_dev = to_pci_dev(dev); 133 struct pci_dev *pci_dev = to_pci_dev(dev);
74 struct physdev_manage_pci manage_pci;
75 134
76 manage_pci.bus = pci_dev->bus->number; 135 if (pci_seg_supported) {
77 manage_pci.devfn = pci_dev->devfn; 136 struct physdev_pci_device device = {
137 .seg = pci_domain_nr(pci_dev->bus),
138 .bus = pci_dev->bus->number,
139 .devfn = pci_dev->devfn
140 };
78 141
79 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove, 142 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
80 &manage_pci); 143 &device);
144 } else if (pci_domain_nr(pci_dev->bus))
145 r = -ENOSYS;
146 else {
147 struct physdev_manage_pci manage_pci = {
148 .bus = pci_dev->bus->number,
149 .devfn = pci_dev->devfn
150 };
151
152 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
153 &manage_pci);
154 }
81 155
82 return r; 156 return r;
83} 157}
@@ -96,13 +170,16 @@ static int xen_pci_notifier(struct notifier_block *nb,
96 r = xen_remove_device(dev); 170 r = xen_remove_device(dev);
97 break; 171 break;
98 default: 172 default:
99 break; 173 return NOTIFY_DONE;
100 } 174 }
101 175 if (r)
102 return r; 176 dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
177 action == BUS_NOTIFY_ADD_DEVICE ? "add" :
178 (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
179 return NOTIFY_OK;
103} 180}
104 181
105struct notifier_block device_nb = { 182static struct notifier_block device_nb = {
106 .notifier_call = xen_pci_notifier, 183 .notifier_call = xen_pci_notifier,
107}; 184};
108 185
diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c
index 6e8c15a23201..8e964b91c447 100644
--- a/drivers/xen/swiotlb-xen.c
+++ b/drivers/xen/swiotlb-xen.c
@@ -35,9 +35,11 @@
35 35
36#include <linux/bootmem.h> 36#include <linux/bootmem.h>
37#include <linux/dma-mapping.h> 37#include <linux/dma-mapping.h>
38#include <linux/export.h>
38#include <xen/swiotlb-xen.h> 39#include <xen/swiotlb-xen.h>
39#include <xen/page.h> 40#include <xen/page.h>
40#include <xen/xen-ops.h> 41#include <xen/xen-ops.h>
42#include <xen/hvc-console.h>
41/* 43/*
42 * Used to do a quick range check in swiotlb_tbl_unmap_single and 44 * Used to do a quick range check in swiotlb_tbl_unmap_single and
43 * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this 45 * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this
@@ -146,8 +148,10 @@ xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs)
146void __init xen_swiotlb_init(int verbose) 148void __init xen_swiotlb_init(int verbose)
147{ 149{
148 unsigned long bytes; 150 unsigned long bytes;
149 int rc; 151 int rc = -ENOMEM;
150 unsigned long nr_tbl; 152 unsigned long nr_tbl;
153 char *m = NULL;
154 unsigned int repeat = 3;
151 155
152 nr_tbl = swioltb_nr_tbl(); 156 nr_tbl = swioltb_nr_tbl();
153 if (nr_tbl) 157 if (nr_tbl)
@@ -156,16 +160,17 @@ void __init xen_swiotlb_init(int verbose)
156 xen_io_tlb_nslabs = (64 * 1024 * 1024 >> IO_TLB_SHIFT); 160 xen_io_tlb_nslabs = (64 * 1024 * 1024 >> IO_TLB_SHIFT);
157 xen_io_tlb_nslabs = ALIGN(xen_io_tlb_nslabs, IO_TLB_SEGSIZE); 161 xen_io_tlb_nslabs = ALIGN(xen_io_tlb_nslabs, IO_TLB_SEGSIZE);
158 } 162 }
159 163retry:
160 bytes = xen_io_tlb_nslabs << IO_TLB_SHIFT; 164 bytes = xen_io_tlb_nslabs << IO_TLB_SHIFT;
161 165
162 /* 166 /*
163 * Get IO TLB memory from any location. 167 * Get IO TLB memory from any location.
164 */ 168 */
165 xen_io_tlb_start = alloc_bootmem(bytes); 169 xen_io_tlb_start = alloc_bootmem(bytes);
166 if (!xen_io_tlb_start) 170 if (!xen_io_tlb_start) {
167 panic("Cannot allocate SWIOTLB buffer"); 171 m = "Cannot allocate Xen-SWIOTLB buffer!\n";
168 172 goto error;
173 }
169 xen_io_tlb_end = xen_io_tlb_start + bytes; 174 xen_io_tlb_end = xen_io_tlb_start + bytes;
170 /* 175 /*
171 * And replace that memory with pages under 4GB. 176 * And replace that memory with pages under 4GB.
@@ -173,17 +178,28 @@ void __init xen_swiotlb_init(int verbose)
173 rc = xen_swiotlb_fixup(xen_io_tlb_start, 178 rc = xen_swiotlb_fixup(xen_io_tlb_start,
174 bytes, 179 bytes,
175 xen_io_tlb_nslabs); 180 xen_io_tlb_nslabs);
176 if (rc) 181 if (rc) {
182 free_bootmem(__pa(xen_io_tlb_start), bytes);
183 m = "Failed to get contiguous memory for DMA from Xen!\n"\
184 "You either: don't have the permissions, do not have"\
185 " enough free memory under 4GB, or the hypervisor memory"\
186 "is too fragmented!";
177 goto error; 187 goto error;
178 188 }
179 start_dma_addr = xen_virt_to_bus(xen_io_tlb_start); 189 start_dma_addr = xen_virt_to_bus(xen_io_tlb_start);
180 swiotlb_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs, verbose); 190 swiotlb_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs, verbose);
181 191
182 return; 192 return;
183error: 193error:
184 panic("DMA(%d): Failed to exchange pages allocated for DMA with Xen! "\ 194 if (repeat--) {
185 "We either don't have the permission or you do not have enough"\ 195 xen_io_tlb_nslabs = max(1024UL, /* Min is 2MB */
186 "free memory under 4GB!\n", rc); 196 (xen_io_tlb_nslabs >> 1));
197 printk(KERN_INFO "Xen-SWIOTLB: Lowering to %luMB\n",
198 (xen_io_tlb_nslabs << IO_TLB_SHIFT) >> 20);
199 goto retry;
200 }
201 xen_raw_printk("%s (rc:%d)", m, rc);
202 panic("%s (rc:%d)", m, rc);
187} 203}
188 204
189void * 205void *
@@ -194,6 +210,8 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
194 int order = get_order(size); 210 int order = get_order(size);
195 u64 dma_mask = DMA_BIT_MASK(32); 211 u64 dma_mask = DMA_BIT_MASK(32);
196 unsigned long vstart; 212 unsigned long vstart;
213 phys_addr_t phys;
214 dma_addr_t dev_addr;
197 215
198 /* 216 /*
199 * Ignore region specifiers - the kernel's ideas of 217 * Ignore region specifiers - the kernel's ideas of
@@ -209,18 +227,26 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
209 vstart = __get_free_pages(flags, order); 227 vstart = __get_free_pages(flags, order);
210 ret = (void *)vstart; 228 ret = (void *)vstart;
211 229
230 if (!ret)
231 return ret;
232
212 if (hwdev && hwdev->coherent_dma_mask) 233 if (hwdev && hwdev->coherent_dma_mask)
213 dma_mask = dma_alloc_coherent_mask(hwdev, flags); 234 dma_mask = hwdev->coherent_dma_mask;
214 235
215 if (ret) { 236 phys = virt_to_phys(ret);
237 dev_addr = xen_phys_to_bus(phys);
238 if (((dev_addr + size - 1 <= dma_mask)) &&
239 !range_straddles_page_boundary(phys, size))
240 *dma_handle = dev_addr;
241 else {
216 if (xen_create_contiguous_region(vstart, order, 242 if (xen_create_contiguous_region(vstart, order,
217 fls64(dma_mask)) != 0) { 243 fls64(dma_mask)) != 0) {
218 free_pages(vstart, order); 244 free_pages(vstart, order);
219 return NULL; 245 return NULL;
220 } 246 }
221 memset(ret, 0, size);
222 *dma_handle = virt_to_machine(ret).maddr; 247 *dma_handle = virt_to_machine(ret).maddr;
223 } 248 }
249 memset(ret, 0, size);
224 return ret; 250 return ret;
225} 251}
226EXPORT_SYMBOL_GPL(xen_swiotlb_alloc_coherent); 252EXPORT_SYMBOL_GPL(xen_swiotlb_alloc_coherent);
@@ -230,11 +256,21 @@ xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
230 dma_addr_t dev_addr) 256 dma_addr_t dev_addr)
231{ 257{
232 int order = get_order(size); 258 int order = get_order(size);
259 phys_addr_t phys;
260 u64 dma_mask = DMA_BIT_MASK(32);
233 261
234 if (dma_release_from_coherent(hwdev, order, vaddr)) 262 if (dma_release_from_coherent(hwdev, order, vaddr))
235 return; 263 return;
236 264
237 xen_destroy_contiguous_region((unsigned long)vaddr, order); 265 if (hwdev && hwdev->coherent_dma_mask)
266 dma_mask = hwdev->coherent_dma_mask;
267
268 phys = virt_to_phys(vaddr);
269
270 if (((dev_addr + size - 1 > dma_mask)) ||
271 range_straddles_page_boundary(phys, size))
272 xen_destroy_contiguous_region((unsigned long)vaddr, order);
273
238 free_pages((unsigned long)vaddr, order); 274 free_pages((unsigned long)vaddr, order);
239} 275}
240EXPORT_SYMBOL_GPL(xen_swiotlb_free_coherent); 276EXPORT_SYMBOL_GPL(xen_swiotlb_free_coherent);
@@ -278,9 +314,10 @@ dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page,
278 /* 314 /*
279 * Ensure that the address returned is DMA'ble 315 * Ensure that the address returned is DMA'ble
280 */ 316 */
281 if (!dma_capable(dev, dev_addr, size)) 317 if (!dma_capable(dev, dev_addr, size)) {
282 panic("map_single: bounce buffer is not DMA'ble"); 318 swiotlb_tbl_unmap_single(dev, map, size, dir);
283 319 dev_addr = 0;
320 }
284 return dev_addr; 321 return dev_addr;
285} 322}
286EXPORT_SYMBOL_GPL(xen_swiotlb_map_page); 323EXPORT_SYMBOL_GPL(xen_swiotlb_map_page);
diff --git a/drivers/xen/xen-balloon.c b/drivers/xen/xen-balloon.c
index 5c9dc43c1e94..9cc2259c9992 100644
--- a/drivers/xen/xen-balloon.c
+++ b/drivers/xen/xen-balloon.c
@@ -50,11 +50,6 @@ static struct sys_device balloon_sysdev;
50 50
51static int register_balloon(struct sys_device *sysdev); 51static int register_balloon(struct sys_device *sysdev);
52 52
53static struct xenbus_watch target_watch =
54{
55 .node = "memory/target"
56};
57
58/* React to a change in the target key */ 53/* React to a change in the target key */
59static void watch_target(struct xenbus_watch *watch, 54static void watch_target(struct xenbus_watch *watch,
60 const char **vec, unsigned int len) 55 const char **vec, unsigned int len)
@@ -73,6 +68,11 @@ static void watch_target(struct xenbus_watch *watch,
73 */ 68 */
74 balloon_set_new_target(new_target >> (PAGE_SHIFT - 10)); 69 balloon_set_new_target(new_target >> (PAGE_SHIFT - 10));
75} 70}
71static struct xenbus_watch target_watch = {
72 .node = "memory/target",
73 .callback = watch_target,
74};
75
76 76
77static int balloon_init_watcher(struct notifier_block *notifier, 77static int balloon_init_watcher(struct notifier_block *notifier,
78 unsigned long event, 78 unsigned long event,
@@ -87,7 +87,9 @@ static int balloon_init_watcher(struct notifier_block *notifier,
87 return NOTIFY_DONE; 87 return NOTIFY_DONE;
88} 88}
89 89
90static struct notifier_block xenstore_notifier; 90static struct notifier_block xenstore_notifier = {
91 .notifier_call = balloon_init_watcher,
92};
91 93
92static int __init balloon_init(void) 94static int __init balloon_init(void)
93{ 95{
@@ -100,9 +102,6 @@ static int __init balloon_init(void)
100 102
101 register_xen_selfballooning(&balloon_sysdev); 103 register_xen_selfballooning(&balloon_sysdev);
102 104
103 target_watch.callback = watch_target;
104 xenstore_notifier.notifier_call = balloon_init_watcher;
105
106 register_xenstore_notifier(&xenstore_notifier); 105 register_xenstore_notifier(&xenstore_notifier);
107 106
108 return 0; 107 return 0;
diff --git a/drivers/xen/xen-pciback/conf_space.c b/drivers/xen/xen-pciback/conf_space.c
index a8031445d94e..52fed16d8701 100644
--- a/drivers/xen/xen-pciback/conf_space.c
+++ b/drivers/xen/xen-pciback/conf_space.c
@@ -10,12 +10,12 @@
10 */ 10 */
11 11
12#include <linux/kernel.h> 12#include <linux/kernel.h>
13#include <linux/module.h>
13#include <linux/pci.h> 14#include <linux/pci.h>
14#include "pciback.h" 15#include "pciback.h"
15#include "conf_space.h" 16#include "conf_space.h"
16#include "conf_space_quirks.h" 17#include "conf_space_quirks.h"
17 18
18#define DRV_NAME "xen-pciback"
19static int permissive; 19static int permissive;
20module_param(permissive, bool, 0644); 20module_param(permissive, bool, 0644);
21 21
diff --git a/drivers/xen/xen-pciback/conf_space_header.c b/drivers/xen/xen-pciback/conf_space_header.c
index da3cbdfcb5dc..3daf862d739d 100644
--- a/drivers/xen/xen-pciback/conf_space_header.c
+++ b/drivers/xen/xen-pciback/conf_space_header.c
@@ -15,7 +15,6 @@ struct pci_bar_info {
15 int which; 15 int which;
16}; 16};
17 17
18#define DRV_NAME "xen-pciback"
19#define is_enable_cmd(value) ((value)&(PCI_COMMAND_MEMORY|PCI_COMMAND_IO)) 18#define is_enable_cmd(value) ((value)&(PCI_COMMAND_MEMORY|PCI_COMMAND_IO))
20#define is_master_cmd(value) ((value)&PCI_COMMAND_MASTER) 19#define is_master_cmd(value) ((value)&PCI_COMMAND_MASTER)
21 20
@@ -25,7 +24,7 @@ static int command_read(struct pci_dev *dev, int offset, u16 *value, void *data)
25 int ret; 24 int ret;
26 25
27 ret = xen_pcibk_read_config_word(dev, offset, value, data); 26 ret = xen_pcibk_read_config_word(dev, offset, value, data);
28 if (!atomic_read(&dev->enable_cnt)) 27 if (!pci_is_enabled(dev))
29 return ret; 28 return ret;
30 29
31 for (i = 0; i < PCI_ROM_RESOURCE; i++) { 30 for (i = 0; i < PCI_ROM_RESOURCE; i++) {
@@ -187,7 +186,7 @@ static inline void read_dev_bar(struct pci_dev *dev,
187 186
188 bar_info->val = res[pos].start | 187 bar_info->val = res[pos].start |
189 (res[pos].flags & PCI_REGION_FLAG_MASK); 188 (res[pos].flags & PCI_REGION_FLAG_MASK);
190 bar_info->len_val = res[pos].end - res[pos].start + 1; 189 bar_info->len_val = resource_size(&res[pos]);
191} 190}
192 191
193static void *bar_init(struct pci_dev *dev, int offset) 192static void *bar_init(struct pci_dev *dev, int offset)
diff --git a/drivers/xen/xen-pciback/conf_space_quirks.c b/drivers/xen/xen-pciback/conf_space_quirks.c
index 921a889e65eb..7476791cab40 100644
--- a/drivers/xen/xen-pciback/conf_space_quirks.c
+++ b/drivers/xen/xen-pciback/conf_space_quirks.c
@@ -12,7 +12,6 @@
12#include "conf_space_quirks.h" 12#include "conf_space_quirks.h"
13 13
14LIST_HEAD(xen_pcibk_quirks); 14LIST_HEAD(xen_pcibk_quirks);
15#define DRV_NAME "xen-pciback"
16static inline const struct pci_device_id * 15static inline const struct pci_device_id *
17match_one_device(const struct pci_device_id *id, const struct pci_dev *dev) 16match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
18{ 17{
@@ -36,7 +35,7 @@ static struct xen_pcibk_config_quirk *xen_pcibk_find_quirk(struct pci_dev *dev)
36 goto out; 35 goto out;
37 tmp_quirk = NULL; 36 tmp_quirk = NULL;
38 printk(KERN_DEBUG DRV_NAME 37 printk(KERN_DEBUG DRV_NAME
39 ":quirk didn't match any device xen_pciback knows about\n"); 38 ": quirk didn't match any device known\n");
40out: 39out:
41 return tmp_quirk; 40 return tmp_quirk;
42} 41}
diff --git a/drivers/xen/xen-pciback/passthrough.c b/drivers/xen/xen-pciback/passthrough.c
index 1d32a9a42c01..828dddc360df 100644
--- a/drivers/xen/xen-pciback/passthrough.c
+++ b/drivers/xen/xen-pciback/passthrough.c
@@ -7,13 +7,13 @@
7 7
8#include <linux/list.h> 8#include <linux/list.h>
9#include <linux/pci.h> 9#include <linux/pci.h>
10#include <linux/spinlock.h> 10#include <linux/mutex.h>
11#include "pciback.h" 11#include "pciback.h"
12 12
13struct passthrough_dev_data { 13struct passthrough_dev_data {
14 /* Access to dev_list must be protected by lock */ 14 /* Access to dev_list must be protected by lock */
15 struct list_head dev_list; 15 struct list_head dev_list;
16 spinlock_t lock; 16 struct mutex lock;
17}; 17};
18 18
19static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev, 19static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
@@ -24,9 +24,8 @@ static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
24 struct passthrough_dev_data *dev_data = pdev->pci_dev_data; 24 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
25 struct pci_dev_entry *dev_entry; 25 struct pci_dev_entry *dev_entry;
26 struct pci_dev *dev = NULL; 26 struct pci_dev *dev = NULL;
27 unsigned long flags;
28 27
29 spin_lock_irqsave(&dev_data->lock, flags); 28 mutex_lock(&dev_data->lock);
30 29
31 list_for_each_entry(dev_entry, &dev_data->dev_list, list) { 30 list_for_each_entry(dev_entry, &dev_data->dev_list, list) {
32 if (domain == (unsigned int)pci_domain_nr(dev_entry->dev->bus) 31 if (domain == (unsigned int)pci_domain_nr(dev_entry->dev->bus)
@@ -37,7 +36,7 @@ static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
37 } 36 }
38 } 37 }
39 38
40 spin_unlock_irqrestore(&dev_data->lock, flags); 39 mutex_unlock(&dev_data->lock);
41 40
42 return dev; 41 return dev;
43} 42}
@@ -48,7 +47,6 @@ static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
48{ 47{
49 struct passthrough_dev_data *dev_data = pdev->pci_dev_data; 48 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
50 struct pci_dev_entry *dev_entry; 49 struct pci_dev_entry *dev_entry;
51 unsigned long flags;
52 unsigned int domain, bus, devfn; 50 unsigned int domain, bus, devfn;
53 int err; 51 int err;
54 52
@@ -57,9 +55,9 @@ static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
57 return -ENOMEM; 55 return -ENOMEM;
58 dev_entry->dev = dev; 56 dev_entry->dev = dev;
59 57
60 spin_lock_irqsave(&dev_data->lock, flags); 58 mutex_lock(&dev_data->lock);
61 list_add_tail(&dev_entry->list, &dev_data->dev_list); 59 list_add_tail(&dev_entry->list, &dev_data->dev_list);
62 spin_unlock_irqrestore(&dev_data->lock, flags); 60 mutex_unlock(&dev_data->lock);
63 61
64 /* Publish this device. */ 62 /* Publish this device. */
65 domain = (unsigned int)pci_domain_nr(dev->bus); 63 domain = (unsigned int)pci_domain_nr(dev->bus);
@@ -76,9 +74,8 @@ static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
76 struct passthrough_dev_data *dev_data = pdev->pci_dev_data; 74 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
77 struct pci_dev_entry *dev_entry, *t; 75 struct pci_dev_entry *dev_entry, *t;
78 struct pci_dev *found_dev = NULL; 76 struct pci_dev *found_dev = NULL;
79 unsigned long flags;
80 77
81 spin_lock_irqsave(&dev_data->lock, flags); 78 mutex_lock(&dev_data->lock);
82 79
83 list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) { 80 list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) {
84 if (dev_entry->dev == dev) { 81 if (dev_entry->dev == dev) {
@@ -88,7 +85,7 @@ static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
88 } 85 }
89 } 86 }
90 87
91 spin_unlock_irqrestore(&dev_data->lock, flags); 88 mutex_unlock(&dev_data->lock);
92 89
93 if (found_dev) 90 if (found_dev)
94 pcistub_put_pci_dev(found_dev); 91 pcistub_put_pci_dev(found_dev);
@@ -102,7 +99,7 @@ static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
102 if (!dev_data) 99 if (!dev_data)
103 return -ENOMEM; 100 return -ENOMEM;
104 101
105 spin_lock_init(&dev_data->lock); 102 mutex_init(&dev_data->lock);
106 103
107 INIT_LIST_HEAD(&dev_data->dev_list); 104 INIT_LIST_HEAD(&dev_data->dev_list);
108 105
@@ -116,14 +113,14 @@ static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
116{ 113{
117 int err = 0; 114 int err = 0;
118 struct passthrough_dev_data *dev_data = pdev->pci_dev_data; 115 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
119 struct pci_dev_entry *dev_entry, *e, *tmp; 116 struct pci_dev_entry *dev_entry, *e;
120 struct pci_dev *dev; 117 struct pci_dev *dev;
121 int found; 118 int found;
122 unsigned int domain, bus; 119 unsigned int domain, bus;
123 120
124 spin_lock(&dev_data->lock); 121 mutex_lock(&dev_data->lock);
125 122
126 list_for_each_entry_safe(dev_entry, tmp, &dev_data->dev_list, list) { 123 list_for_each_entry(dev_entry, &dev_data->dev_list, list) {
127 /* Only publish this device as a root if none of its 124 /* Only publish this device as a root if none of its
128 * parent bridges are exported 125 * parent bridges are exported
129 */ 126 */
@@ -142,16 +139,13 @@ static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
142 bus = (unsigned int)dev_entry->dev->bus->number; 139 bus = (unsigned int)dev_entry->dev->bus->number;
143 140
144 if (!found) { 141 if (!found) {
145 spin_unlock(&dev_data->lock);
146 err = publish_root_cb(pdev, domain, bus); 142 err = publish_root_cb(pdev, domain, bus);
147 if (err) 143 if (err)
148 break; 144 break;
149 spin_lock(&dev_data->lock);
150 } 145 }
151 } 146 }
152 147
153 if (!err) 148 mutex_unlock(&dev_data->lock);
154 spin_unlock(&dev_data->lock);
155 149
156 return err; 150 return err;
157} 151}
@@ -182,7 +176,7 @@ static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
182 return 1; 176 return 1;
183} 177}
184 178
185struct xen_pcibk_backend xen_pcibk_passthrough_backend = { 179const struct xen_pcibk_backend xen_pcibk_passthrough_backend = {
186 .name = "passthrough", 180 .name = "passthrough",
187 .init = __xen_pcibk_init_devices, 181 .init = __xen_pcibk_init_devices,
188 .free = __xen_pcibk_release_devices, 182 .free = __xen_pcibk_release_devices,
diff --git a/drivers/xen/xen-pciback/pci_stub.c b/drivers/xen/xen-pciback/pci_stub.c
index aec214ac0a14..8f06e1ed028c 100644
--- a/drivers/xen/xen-pciback/pci_stub.c
+++ b/drivers/xen/xen-pciback/pci_stub.c
@@ -21,8 +21,6 @@
21#include "conf_space.h" 21#include "conf_space.h"
22#include "conf_space_quirks.h" 22#include "conf_space_quirks.h"
23 23
24#define DRV_NAME "xen-pciback"
25
26static char *pci_devs_to_hide; 24static char *pci_devs_to_hide;
27wait_queue_head_t xen_pcibk_aer_wait_queue; 25wait_queue_head_t xen_pcibk_aer_wait_queue;
28/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops, 26/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
@@ -222,6 +220,8 @@ void pcistub_put_pci_dev(struct pci_dev *dev)
222 } 220 }
223 221
224 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 222 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
223 if (WARN_ON(!found_psdev))
224 return;
225 225
226 /*hold this lock for avoiding breaking link between 226 /*hold this lock for avoiding breaking link between
227 * pcistub and xen_pcibk when AER is in processing 227 * pcistub and xen_pcibk when AER is in processing
@@ -514,12 +514,9 @@ static void kill_domain_by_device(struct pcistub_device *psdev)
514 int err; 514 int err;
515 char nodename[PCI_NODENAME_MAX]; 515 char nodename[PCI_NODENAME_MAX];
516 516
517 if (!psdev) 517 BUG_ON(!psdev);
518 dev_err(&psdev->dev->dev,
519 "device is NULL when do AER recovery/kill_domain\n");
520 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0", 518 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
521 psdev->pdev->xdev->otherend_id); 519 psdev->pdev->xdev->otherend_id);
522 nodename[strlen(nodename)] = '\0';
523 520
524again: 521again:
525 err = xenbus_transaction_start(&xbt); 522 err = xenbus_transaction_start(&xbt);
@@ -605,7 +602,7 @@ static pci_ers_result_t common_process(struct pcistub_device *psdev,
605 if (test_bit(_XEN_PCIF_active, 602 if (test_bit(_XEN_PCIF_active,
606 (unsigned long *)&psdev->pdev->sh_info->flags)) { 603 (unsigned long *)&psdev->pdev->sh_info->flags)) {
607 dev_dbg(&psdev->dev->dev, 604 dev_dbg(&psdev->dev->dev,
608 "schedule pci_conf service in xen_pcibk\n"); 605 "schedule pci_conf service in " DRV_NAME "\n");
609 xen_pcibk_test_and_schedule_op(psdev->pdev); 606 xen_pcibk_test_and_schedule_op(psdev->pdev);
610 } 607 }
611 608
@@ -995,8 +992,7 @@ out:
995 err = count; 992 err = count;
996 return err; 993 return err;
997} 994}
998 995static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
999DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1000 996
1001static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf, 997static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1002 size_t count) 998 size_t count)
@@ -1015,8 +1011,7 @@ out:
1015 err = count; 1011 err = count;
1016 return err; 1012 return err;
1017} 1013}
1018 1014static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1019DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1020 1015
1021static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf) 1016static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1022{ 1017{
@@ -1039,8 +1034,7 @@ static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1039 1034
1040 return count; 1035 return count;
1041} 1036}
1042 1037static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1043DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1044 1038
1045static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf) 1039static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1046{ 1040{
@@ -1069,8 +1063,7 @@ static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1069 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 1063 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1070 return count; 1064 return count;
1071} 1065}
1072 1066static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1073DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1074 1067
1075static ssize_t pcistub_irq_handler_switch(struct device_driver *drv, 1068static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1076 const char *buf, 1069 const char *buf,
@@ -1106,7 +1099,8 @@ out:
1106 err = count; 1099 err = count;
1107 return err; 1100 return err;
1108} 1101}
1109DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL, pcistub_irq_handler_switch); 1102static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1103 pcistub_irq_handler_switch);
1110 1104
1111static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf, 1105static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1112 size_t count) 1106 size_t count)
@@ -1170,8 +1164,8 @@ out:
1170 1164
1171 return count; 1165 return count;
1172} 1166}
1173 1167static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1174DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, pcistub_quirk_add); 1168 pcistub_quirk_add);
1175 1169
1176static ssize_t permissive_add(struct device_driver *drv, const char *buf, 1170static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1177 size_t count) 1171 size_t count)
@@ -1236,8 +1230,8 @@ static ssize_t permissive_show(struct device_driver *drv, char *buf)
1236 spin_unlock_irqrestore(&pcistub_devices_lock, flags); 1230 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1237 return count; 1231 return count;
1238} 1232}
1239 1233static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1240DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, permissive_add); 1234 permissive_add);
1241 1235
1242static void pcistub_exit(void) 1236static void pcistub_exit(void)
1243{ 1237{
@@ -1374,3 +1368,4 @@ module_init(xen_pcibk_init);
1374module_exit(xen_pcibk_cleanup); 1368module_exit(xen_pcibk_cleanup);
1375 1369
1376MODULE_LICENSE("Dual BSD/GPL"); 1370MODULE_LICENSE("Dual BSD/GPL");
1371MODULE_ALIAS("xen-backend:pci");
diff --git a/drivers/xen/xen-pciback/pciback.h b/drivers/xen/xen-pciback/pciback.h
index a0e131a81503..e9b4011c5f9a 100644
--- a/drivers/xen/xen-pciback/pciback.h
+++ b/drivers/xen/xen-pciback/pciback.h
@@ -15,6 +15,8 @@
15#include <linux/atomic.h> 15#include <linux/atomic.h>
16#include <xen/interface/io/pciif.h> 16#include <xen/interface/io/pciif.h>
17 17
18#define DRV_NAME "xen-pciback"
19
18struct pci_dev_entry { 20struct pci_dev_entry {
19 struct list_head list; 21 struct list_head list;
20 struct pci_dev *dev; 22 struct pci_dev *dev;
@@ -27,7 +29,7 @@ struct pci_dev_entry {
27 29
28struct xen_pcibk_device { 30struct xen_pcibk_device {
29 void *pci_dev_data; 31 void *pci_dev_data;
30 spinlock_t dev_lock; 32 struct mutex dev_lock;
31 struct xenbus_device *xdev; 33 struct xenbus_device *xdev;
32 struct xenbus_watch be_watch; 34 struct xenbus_watch be_watch;
33 u8 be_watching; 35 u8 be_watching;
@@ -89,7 +91,7 @@ typedef int (*publish_pci_root_cb) (struct xen_pcibk_device *pdev,
89 * passthrough - BDFs are exactly like in the host. 91 * passthrough - BDFs are exactly like in the host.
90 */ 92 */
91struct xen_pcibk_backend { 93struct xen_pcibk_backend {
92 char *name; 94 const char *name;
93 int (*init)(struct xen_pcibk_device *pdev); 95 int (*init)(struct xen_pcibk_device *pdev);
94 void (*free)(struct xen_pcibk_device *pdev); 96 void (*free)(struct xen_pcibk_device *pdev);
95 int (*find)(struct pci_dev *pcidev, struct xen_pcibk_device *pdev, 97 int (*find)(struct pci_dev *pcidev, struct xen_pcibk_device *pdev,
@@ -104,9 +106,9 @@ struct xen_pcibk_backend {
104 unsigned int devfn); 106 unsigned int devfn);
105}; 107};
106 108
107extern struct xen_pcibk_backend xen_pcibk_vpci_backend; 109extern const struct xen_pcibk_backend xen_pcibk_vpci_backend;
108extern struct xen_pcibk_backend xen_pcibk_passthrough_backend; 110extern const struct xen_pcibk_backend xen_pcibk_passthrough_backend;
109extern struct xen_pcibk_backend *xen_pcibk_backend; 111extern const struct xen_pcibk_backend *xen_pcibk_backend;
110 112
111static inline int xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev, 113static inline int xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
112 struct pci_dev *dev, 114 struct pci_dev *dev,
@@ -116,13 +118,14 @@ static inline int xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
116 if (xen_pcibk_backend && xen_pcibk_backend->add) 118 if (xen_pcibk_backend && xen_pcibk_backend->add)
117 return xen_pcibk_backend->add(pdev, dev, devid, publish_cb); 119 return xen_pcibk_backend->add(pdev, dev, devid, publish_cb);
118 return -1; 120 return -1;
119}; 121}
122
120static inline void xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev, 123static inline void xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
121 struct pci_dev *dev) 124 struct pci_dev *dev)
122{ 125{
123 if (xen_pcibk_backend && xen_pcibk_backend->free) 126 if (xen_pcibk_backend && xen_pcibk_backend->free)
124 return xen_pcibk_backend->release(pdev, dev); 127 return xen_pcibk_backend->release(pdev, dev);
125}; 128}
126 129
127static inline struct pci_dev * 130static inline struct pci_dev *
128xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev, unsigned int domain, 131xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev, unsigned int domain,
@@ -131,7 +134,8 @@ xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev, unsigned int domain,
131 if (xen_pcibk_backend && xen_pcibk_backend->get) 134 if (xen_pcibk_backend && xen_pcibk_backend->get)
132 return xen_pcibk_backend->get(pdev, domain, bus, devfn); 135 return xen_pcibk_backend->get(pdev, domain, bus, devfn);
133 return NULL; 136 return NULL;
134}; 137}
138
135/** 139/**
136* Add for domain0 PCIE-AER handling. Get guest domain/bus/devfn in xen_pcibk 140* Add for domain0 PCIE-AER handling. Get guest domain/bus/devfn in xen_pcibk
137* before sending aer request to pcifront, so that guest could identify 141* before sending aer request to pcifront, so that guest could identify
@@ -148,25 +152,29 @@ static inline int xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
148 return xen_pcibk_backend->find(pcidev, pdev, domain, bus, 152 return xen_pcibk_backend->find(pcidev, pdev, domain, bus,
149 devfn); 153 devfn);
150 return -1; 154 return -1;
151}; 155}
156
152static inline int xen_pcibk_init_devices(struct xen_pcibk_device *pdev) 157static inline int xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
153{ 158{
154 if (xen_pcibk_backend && xen_pcibk_backend->init) 159 if (xen_pcibk_backend && xen_pcibk_backend->init)
155 return xen_pcibk_backend->init(pdev); 160 return xen_pcibk_backend->init(pdev);
156 return -1; 161 return -1;
157}; 162}
163
158static inline int xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev, 164static inline int xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
159 publish_pci_root_cb cb) 165 publish_pci_root_cb cb)
160{ 166{
161 if (xen_pcibk_backend && xen_pcibk_backend->publish) 167 if (xen_pcibk_backend && xen_pcibk_backend->publish)
162 return xen_pcibk_backend->publish(pdev, cb); 168 return xen_pcibk_backend->publish(pdev, cb);
163 return -1; 169 return -1;
164}; 170}
171
165static inline void xen_pcibk_release_devices(struct xen_pcibk_device *pdev) 172static inline void xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
166{ 173{
167 if (xen_pcibk_backend && xen_pcibk_backend->free) 174 if (xen_pcibk_backend && xen_pcibk_backend->free)
168 return xen_pcibk_backend->free(pdev); 175 return xen_pcibk_backend->free(pdev);
169}; 176}
177
170/* Handles events from front-end */ 178/* Handles events from front-end */
171irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id); 179irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id);
172void xen_pcibk_do_op(struct work_struct *data); 180void xen_pcibk_do_op(struct work_struct *data);
diff --git a/drivers/xen/xen-pciback/pciback_ops.c b/drivers/xen/xen-pciback/pciback_ops.c
index 8c95c3415b75..63616d7453e6 100644
--- a/drivers/xen/xen-pciback/pciback_ops.c
+++ b/drivers/xen/xen-pciback/pciback_ops.c
@@ -10,7 +10,6 @@
10#include <linux/sched.h> 10#include <linux/sched.h>
11#include "pciback.h" 11#include "pciback.h"
12 12
13#define DRV_NAME "xen-pciback"
14int verbose_request; 13int verbose_request;
15module_param(verbose_request, int, 0644); 14module_param(verbose_request, int, 0644);
16 15
diff --git a/drivers/xen/xen-pciback/vpci.c b/drivers/xen/xen-pciback/vpci.c
index 4a42cfb0959d..46d140baebd8 100644
--- a/drivers/xen/xen-pciback/vpci.c
+++ b/drivers/xen/xen-pciback/vpci.c
@@ -8,16 +8,15 @@
8#include <linux/list.h> 8#include <linux/list.h>
9#include <linux/slab.h> 9#include <linux/slab.h>
10#include <linux/pci.h> 10#include <linux/pci.h>
11#include <linux/spinlock.h> 11#include <linux/mutex.h>
12#include "pciback.h" 12#include "pciback.h"
13 13
14#define PCI_SLOT_MAX 32 14#define PCI_SLOT_MAX 32
15#define DRV_NAME "xen-pciback"
16 15
17struct vpci_dev_data { 16struct vpci_dev_data {
18 /* Access to dev_list must be protected by lock */ 17 /* Access to dev_list must be protected by lock */
19 struct list_head dev_list[PCI_SLOT_MAX]; 18 struct list_head dev_list[PCI_SLOT_MAX];
20 spinlock_t lock; 19 struct mutex lock;
21}; 20};
22 21
23static inline struct list_head *list_first(struct list_head *head) 22static inline struct list_head *list_first(struct list_head *head)
@@ -33,13 +32,12 @@ static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
33 struct pci_dev_entry *entry; 32 struct pci_dev_entry *entry;
34 struct pci_dev *dev = NULL; 33 struct pci_dev *dev = NULL;
35 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; 34 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
36 unsigned long flags;
37 35
38 if (domain != 0 || bus != 0) 36 if (domain != 0 || bus != 0)
39 return NULL; 37 return NULL;
40 38
41 if (PCI_SLOT(devfn) < PCI_SLOT_MAX) { 39 if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
42 spin_lock_irqsave(&vpci_dev->lock, flags); 40 mutex_lock(&vpci_dev->lock);
43 41
44 list_for_each_entry(entry, 42 list_for_each_entry(entry,
45 &vpci_dev->dev_list[PCI_SLOT(devfn)], 43 &vpci_dev->dev_list[PCI_SLOT(devfn)],
@@ -50,7 +48,7 @@ static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
50 } 48 }
51 } 49 }
52 50
53 spin_unlock_irqrestore(&vpci_dev->lock, flags); 51 mutex_unlock(&vpci_dev->lock);
54 } 52 }
55 return dev; 53 return dev;
56} 54}
@@ -71,7 +69,6 @@ static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
71 int err = 0, slot, func = -1; 69 int err = 0, slot, func = -1;
72 struct pci_dev_entry *t, *dev_entry; 70 struct pci_dev_entry *t, *dev_entry;
73 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; 71 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
74 unsigned long flags;
75 72
76 if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) { 73 if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) {
77 err = -EFAULT; 74 err = -EFAULT;
@@ -90,7 +87,7 @@ static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
90 87
91 dev_entry->dev = dev; 88 dev_entry->dev = dev;
92 89
93 spin_lock_irqsave(&vpci_dev->lock, flags); 90 mutex_lock(&vpci_dev->lock);
94 91
95 /* Keep multi-function devices together on the virtual PCI bus */ 92 /* Keep multi-function devices together on the virtual PCI bus */
96 for (slot = 0; slot < PCI_SLOT_MAX; slot++) { 93 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
@@ -129,7 +126,7 @@ static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
129 "No more space on root virtual PCI bus"); 126 "No more space on root virtual PCI bus");
130 127
131unlock: 128unlock:
132 spin_unlock_irqrestore(&vpci_dev->lock, flags); 129 mutex_unlock(&vpci_dev->lock);
133 130
134 /* Publish this device. */ 131 /* Publish this device. */
135 if (!err) 132 if (!err)
@@ -145,14 +142,13 @@ static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
145 int slot; 142 int slot;
146 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; 143 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
147 struct pci_dev *found_dev = NULL; 144 struct pci_dev *found_dev = NULL;
148 unsigned long flags;
149 145
150 spin_lock_irqsave(&vpci_dev->lock, flags); 146 mutex_lock(&vpci_dev->lock);
151 147
152 for (slot = 0; slot < PCI_SLOT_MAX; slot++) { 148 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
153 struct pci_dev_entry *e, *tmp; 149 struct pci_dev_entry *e;
154 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot], 150
155 list) { 151 list_for_each_entry(e, &vpci_dev->dev_list[slot], list) {
156 if (e->dev == dev) { 152 if (e->dev == dev) {
157 list_del(&e->list); 153 list_del(&e->list);
158 found_dev = e->dev; 154 found_dev = e->dev;
@@ -163,7 +159,7 @@ static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
163 } 159 }
164 160
165out: 161out:
166 spin_unlock_irqrestore(&vpci_dev->lock, flags); 162 mutex_unlock(&vpci_dev->lock);
167 163
168 if (found_dev) 164 if (found_dev)
169 pcistub_put_pci_dev(found_dev); 165 pcistub_put_pci_dev(found_dev);
@@ -178,7 +174,7 @@ static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
178 if (!vpci_dev) 174 if (!vpci_dev)
179 return -ENOMEM; 175 return -ENOMEM;
180 176
181 spin_lock_init(&vpci_dev->lock); 177 mutex_init(&vpci_dev->lock);
182 178
183 for (slot = 0; slot < PCI_SLOT_MAX; slot++) 179 for (slot = 0; slot < PCI_SLOT_MAX; slot++)
184 INIT_LIST_HEAD(&vpci_dev->dev_list[slot]); 180 INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
@@ -222,10 +218,9 @@ static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
222 struct pci_dev_entry *entry; 218 struct pci_dev_entry *entry;
223 struct pci_dev *dev = NULL; 219 struct pci_dev *dev = NULL;
224 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; 220 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
225 unsigned long flags;
226 int found = 0, slot; 221 int found = 0, slot;
227 222
228 spin_lock_irqsave(&vpci_dev->lock, flags); 223 mutex_lock(&vpci_dev->lock);
229 for (slot = 0; slot < PCI_SLOT_MAX; slot++) { 224 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
230 list_for_each_entry(entry, 225 list_for_each_entry(entry,
231 &vpci_dev->dev_list[slot], 226 &vpci_dev->dev_list[slot],
@@ -243,11 +238,11 @@ static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
243 } 238 }
244 } 239 }
245 } 240 }
246 spin_unlock_irqrestore(&vpci_dev->lock, flags); 241 mutex_unlock(&vpci_dev->lock);
247 return found; 242 return found;
248} 243}
249 244
250struct xen_pcibk_backend xen_pcibk_vpci_backend = { 245const struct xen_pcibk_backend xen_pcibk_vpci_backend = {
251 .name = "vpci", 246 .name = "vpci",
252 .init = __xen_pcibk_init_devices, 247 .init = __xen_pcibk_init_devices,
253 .free = __xen_pcibk_release_devices, 248 .free = __xen_pcibk_release_devices,
diff --git a/drivers/xen/xen-pciback/xenbus.c b/drivers/xen/xen-pciback/xenbus.c
index 978d2c6f5dca..075525945e36 100644
--- a/drivers/xen/xen-pciback/xenbus.c
+++ b/drivers/xen/xen-pciback/xenbus.c
@@ -13,7 +13,6 @@
13#include <asm/xen/pci.h> 13#include <asm/xen/pci.h>
14#include "pciback.h" 14#include "pciback.h"
15 15
16#define DRV_NAME "xen-pciback"
17#define INVALID_EVTCHN_IRQ (-1) 16#define INVALID_EVTCHN_IRQ (-1)
18struct workqueue_struct *xen_pcibk_wq; 17struct workqueue_struct *xen_pcibk_wq;
19 18
@@ -44,7 +43,7 @@ static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
44 pdev->xdev = xdev; 43 pdev->xdev = xdev;
45 dev_set_drvdata(&xdev->dev, pdev); 44 dev_set_drvdata(&xdev->dev, pdev);
46 45
47 spin_lock_init(&pdev->dev_lock); 46 mutex_init(&pdev->dev_lock);
48 47
49 pdev->sh_info = NULL; 48 pdev->sh_info = NULL;
50 pdev->evtchn_irq = INVALID_EVTCHN_IRQ; 49 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
@@ -62,14 +61,12 @@ out:
62 61
63static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev) 62static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
64{ 63{
65 spin_lock(&pdev->dev_lock); 64 mutex_lock(&pdev->dev_lock);
66
67 /* Ensure the guest can't trigger our handler before removing devices */ 65 /* Ensure the guest can't trigger our handler before removing devices */
68 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) { 66 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
69 unbind_from_irqhandler(pdev->evtchn_irq, pdev); 67 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
70 pdev->evtchn_irq = INVALID_EVTCHN_IRQ; 68 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
71 } 69 }
72 spin_unlock(&pdev->dev_lock);
73 70
74 /* If the driver domain started an op, make sure we complete it 71 /* If the driver domain started an op, make sure we complete it
75 * before releasing the shared memory */ 72 * before releasing the shared memory */
@@ -77,13 +74,11 @@ static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
77 /* Note, the workqueue does not use spinlocks at all.*/ 74 /* Note, the workqueue does not use spinlocks at all.*/
78 flush_workqueue(xen_pcibk_wq); 75 flush_workqueue(xen_pcibk_wq);
79 76
80 spin_lock(&pdev->dev_lock);
81 if (pdev->sh_info != NULL) { 77 if (pdev->sh_info != NULL) {
82 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info); 78 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
83 pdev->sh_info = NULL; 79 pdev->sh_info = NULL;
84 } 80 }
85 spin_unlock(&pdev->dev_lock); 81 mutex_unlock(&pdev->dev_lock);
86
87} 82}
88 83
89static void free_pdev(struct xen_pcibk_device *pdev) 84static void free_pdev(struct xen_pcibk_device *pdev)
@@ -120,9 +115,7 @@ static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
120 goto out; 115 goto out;
121 } 116 }
122 117
123 spin_lock(&pdev->dev_lock);
124 pdev->sh_info = vaddr; 118 pdev->sh_info = vaddr;
125 spin_unlock(&pdev->dev_lock);
126 119
127 err = bind_interdomain_evtchn_to_irqhandler( 120 err = bind_interdomain_evtchn_to_irqhandler(
128 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event, 121 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
@@ -132,10 +125,7 @@ static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
132 "Error binding event channel to IRQ"); 125 "Error binding event channel to IRQ");
133 goto out; 126 goto out;
134 } 127 }
135
136 spin_lock(&pdev->dev_lock);
137 pdev->evtchn_irq = err; 128 pdev->evtchn_irq = err;
138 spin_unlock(&pdev->dev_lock);
139 err = 0; 129 err = 0;
140 130
141 dev_dbg(&pdev->xdev->dev, "Attached!\n"); 131 dev_dbg(&pdev->xdev->dev, "Attached!\n");
@@ -150,6 +140,7 @@ static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
150 char *magic = NULL; 140 char *magic = NULL;
151 141
152 142
143 mutex_lock(&pdev->dev_lock);
153 /* Make sure we only do this setup once */ 144 /* Make sure we only do this setup once */
154 if (xenbus_read_driver_state(pdev->xdev->nodename) != 145 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
155 XenbusStateInitialised) 146 XenbusStateInitialised)
@@ -176,7 +167,7 @@ static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
176 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) { 167 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
177 xenbus_dev_fatal(pdev->xdev, -EFAULT, 168 xenbus_dev_fatal(pdev->xdev, -EFAULT,
178 "version mismatch (%s/%s) with pcifront - " 169 "version mismatch (%s/%s) with pcifront - "
179 "halting xen_pcibk", 170 "halting " DRV_NAME,
180 magic, XEN_PCI_MAGIC); 171 magic, XEN_PCI_MAGIC);
181 goto out; 172 goto out;
182 } 173 }
@@ -194,6 +185,7 @@ static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
194 185
195 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err); 186 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
196out: 187out:
188 mutex_unlock(&pdev->dev_lock);
197 189
198 kfree(magic); 190 kfree(magic);
199 191
@@ -249,6 +241,7 @@ static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
249 goto out; 241 goto out;
250 242
251 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id); 243 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
244 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
252 if (xen_register_device_domain_owner(dev, 245 if (xen_register_device_domain_owner(dev,
253 pdev->xdev->otherend_id) != 0) { 246 pdev->xdev->otherend_id) != 0) {
254 dev_err(&dev->dev, "device has been assigned to another " \ 247 dev_err(&dev->dev, "device has been assigned to another " \
@@ -288,6 +281,7 @@ static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
288 } 281 }
289 282
290 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id); 283 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
284 dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
291 xen_unregister_device_domain_owner(dev); 285 xen_unregister_device_domain_owner(dev);
292 286
293 xen_pcibk_release_pci_dev(pdev, dev); 287 xen_pcibk_release_pci_dev(pdev, dev);
@@ -369,6 +363,7 @@ static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
369 363
370 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n"); 364 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
371 365
366 mutex_lock(&pdev->dev_lock);
372 /* Make sure we only reconfigure once */ 367 /* Make sure we only reconfigure once */
373 if (xenbus_read_driver_state(pdev->xdev->nodename) != 368 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
374 XenbusStateReconfiguring) 369 XenbusStateReconfiguring)
@@ -506,6 +501,7 @@ static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
506 } 501 }
507 502
508out: 503out:
504 mutex_unlock(&pdev->dev_lock);
509 return 0; 505 return 0;
510} 506}
511 507
@@ -562,6 +558,7 @@ static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
562 char dev_str[64]; 558 char dev_str[64];
563 char state_str[64]; 559 char state_str[64];
564 560
561 mutex_lock(&pdev->dev_lock);
565 /* It's possible we could get the call to setup twice, so make sure 562 /* It's possible we could get the call to setup twice, so make sure
566 * we're not already connected. 563 * we're not already connected.
567 */ 564 */
@@ -642,10 +639,10 @@ static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
642 "Error switching to initialised state!"); 639 "Error switching to initialised state!");
643 640
644out: 641out:
642 mutex_unlock(&pdev->dev_lock);
645 if (!err) 643 if (!err)
646 /* see if pcifront is already configured (if not, we'll wait) */ 644 /* see if pcifront is already configured (if not, we'll wait) */
647 xen_pcibk_attach(pdev); 645 xen_pcibk_attach(pdev);
648
649 return err; 646 return err;
650} 647}
651 648
@@ -724,7 +721,7 @@ static struct xenbus_driver xenbus_xen_pcibk_driver = {
724 .otherend_changed = xen_pcibk_frontend_changed, 721 .otherend_changed = xen_pcibk_frontend_changed,
725}; 722};
726 723
727struct xen_pcibk_backend *xen_pcibk_backend; 724const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
728 725
729int __init xen_pcibk_xenbus_register(void) 726int __init xen_pcibk_xenbus_register(void)
730{ 727{
diff --git a/drivers/xen/xen-selfballoon.c b/drivers/xen/xen-selfballoon.c
index 6ea852e25162..d93c70857e03 100644
--- a/drivers/xen/xen-selfballoon.c
+++ b/drivers/xen/xen-selfballoon.c
@@ -68,6 +68,8 @@
68 */ 68 */
69 69
70#include <linux/kernel.h> 70#include <linux/kernel.h>
71#include <linux/bootmem.h>
72#include <linux/swap.h>
71#include <linux/mm.h> 73#include <linux/mm.h>
72#include <linux/mman.h> 74#include <linux/mman.h>
73#include <linux/module.h> 75#include <linux/module.h>
@@ -93,6 +95,15 @@ static unsigned int selfballoon_uphysteresis __read_mostly = 1;
93/* In HZ, controls frequency of worker invocation. */ 95/* In HZ, controls frequency of worker invocation. */
94static unsigned int selfballoon_interval __read_mostly = 5; 96static unsigned int selfballoon_interval __read_mostly = 5;
95 97
98/*
99 * Minimum usable RAM in MB for selfballooning target for balloon.
100 * If non-zero, it is added to totalreserve_pages and self-ballooning
101 * will not balloon below the sum. If zero, a piecewise linear function
102 * is calculated as a minimum and added to totalreserve_pages. Note that
103 * setting this value indiscriminately may cause OOMs and crashes.
104 */
105static unsigned int selfballoon_min_usable_mb;
106
96static void selfballoon_process(struct work_struct *work); 107static void selfballoon_process(struct work_struct *work);
97static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process); 108static DECLARE_DELAYED_WORK(selfballoon_worker, selfballoon_process);
98 109
@@ -189,20 +200,23 @@ static int __init xen_selfballooning_setup(char *s)
189__setup("selfballooning", xen_selfballooning_setup); 200__setup("selfballooning", xen_selfballooning_setup);
190#endif /* CONFIG_FRONTSWAP */ 201#endif /* CONFIG_FRONTSWAP */
191 202
203#define MB2PAGES(mb) ((mb) << (20 - PAGE_SHIFT))
204
192/* 205/*
193 * Use current balloon size, the goal (vm_committed_as), and hysteresis 206 * Use current balloon size, the goal (vm_committed_as), and hysteresis
194 * parameters to set a new target balloon size 207 * parameters to set a new target balloon size
195 */ 208 */
196static void selfballoon_process(struct work_struct *work) 209static void selfballoon_process(struct work_struct *work)
197{ 210{
198 unsigned long cur_pages, goal_pages, tgt_pages; 211 unsigned long cur_pages, goal_pages, tgt_pages, floor_pages;
212 unsigned long useful_pages;
199 bool reset_timer = false; 213 bool reset_timer = false;
200 214
201 if (xen_selfballooning_enabled) { 215 if (xen_selfballooning_enabled) {
202 cur_pages = balloon_stats.current_pages; 216 cur_pages = totalram_pages;
203 tgt_pages = cur_pages; /* default is no change */ 217 tgt_pages = cur_pages; /* default is no change */
204 goal_pages = percpu_counter_read_positive(&vm_committed_as) + 218 goal_pages = percpu_counter_read_positive(&vm_committed_as) +
205 balloon_stats.current_pages - totalram_pages; 219 totalreserve_pages;
206#ifdef CONFIG_FRONTSWAP 220#ifdef CONFIG_FRONTSWAP
207 /* allow space for frontswap pages to be repatriated */ 221 /* allow space for frontswap pages to be repatriated */
208 if (frontswap_selfshrinking && frontswap_enabled) 222 if (frontswap_selfshrinking && frontswap_enabled)
@@ -217,7 +231,26 @@ static void selfballoon_process(struct work_struct *work)
217 ((goal_pages - cur_pages) / 231 ((goal_pages - cur_pages) /
218 selfballoon_uphysteresis); 232 selfballoon_uphysteresis);
219 /* else if cur_pages == goal_pages, no change */ 233 /* else if cur_pages == goal_pages, no change */
220 balloon_set_new_target(tgt_pages); 234 useful_pages = max_pfn - totalreserve_pages;
235 if (selfballoon_min_usable_mb != 0)
236 floor_pages = totalreserve_pages +
237 MB2PAGES(selfballoon_min_usable_mb);
238 /* piecewise linear function ending in ~3% slope */
239 else if (useful_pages < MB2PAGES(16))
240 floor_pages = max_pfn; /* not worth ballooning */
241 else if (useful_pages < MB2PAGES(64))
242 floor_pages = totalreserve_pages + MB2PAGES(16) +
243 ((useful_pages - MB2PAGES(16)) >> 1);
244 else if (useful_pages < MB2PAGES(512))
245 floor_pages = totalreserve_pages + MB2PAGES(40) +
246 ((useful_pages - MB2PAGES(40)) >> 3);
247 else /* useful_pages >= MB2PAGES(512) */
248 floor_pages = totalreserve_pages + MB2PAGES(99) +
249 ((useful_pages - MB2PAGES(99)) >> 5);
250 if (tgt_pages < floor_pages)
251 tgt_pages = floor_pages;
252 balloon_set_new_target(tgt_pages +
253 balloon_stats.current_pages - totalram_pages);
221 reset_timer = true; 254 reset_timer = true;
222 } 255 }
223#ifdef CONFIG_FRONTSWAP 256#ifdef CONFIG_FRONTSWAP
@@ -340,6 +373,31 @@ static ssize_t store_selfballoon_uphys(struct sys_device *dev,
340static SYSDEV_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR, 373static SYSDEV_ATTR(selfballoon_uphysteresis, S_IRUGO | S_IWUSR,
341 show_selfballoon_uphys, store_selfballoon_uphys); 374 show_selfballoon_uphys, store_selfballoon_uphys);
342 375
376SELFBALLOON_SHOW(selfballoon_min_usable_mb, "%d\n",
377 selfballoon_min_usable_mb);
378
379static ssize_t store_selfballoon_min_usable_mb(struct sys_device *dev,
380 struct sysdev_attribute *attr,
381 const char *buf,
382 size_t count)
383{
384 unsigned long val;
385 int err;
386
387 if (!capable(CAP_SYS_ADMIN))
388 return -EPERM;
389 err = strict_strtoul(buf, 10, &val);
390 if (err || val == 0)
391 return -EINVAL;
392 selfballoon_min_usable_mb = val;
393 return count;
394}
395
396static SYSDEV_ATTR(selfballoon_min_usable_mb, S_IRUGO | S_IWUSR,
397 show_selfballoon_min_usable_mb,
398 store_selfballoon_min_usable_mb);
399
400
343#ifdef CONFIG_FRONTSWAP 401#ifdef CONFIG_FRONTSWAP
344SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking); 402SELFBALLOON_SHOW(frontswap_selfshrinking, "%d\n", frontswap_selfshrinking);
345 403
@@ -421,6 +479,7 @@ static struct attribute *selfballoon_attrs[] = {
421 &attr_selfballoon_interval.attr, 479 &attr_selfballoon_interval.attr,
422 &attr_selfballoon_downhysteresis.attr, 480 &attr_selfballoon_downhysteresis.attr,
423 &attr_selfballoon_uphysteresis.attr, 481 &attr_selfballoon_uphysteresis.attr,
482 &attr_selfballoon_min_usable_mb.attr,
424#ifdef CONFIG_FRONTSWAP 483#ifdef CONFIG_FRONTSWAP
425 &attr_frontswap_selfshrinking.attr, 484 &attr_frontswap_selfshrinking.attr,
426 &attr_frontswap_hysteresis.attr, 485 &attr_frontswap_hysteresis.attr,
diff --git a/drivers/xen/xenbus/xenbus_client.c b/drivers/xen/xenbus/xenbus_client.c
index cdacf923e073..1906125eab49 100644
--- a/drivers/xen/xenbus/xenbus_client.c
+++ b/drivers/xen/xenbus/xenbus_client.c
@@ -33,7 +33,9 @@
33#include <linux/slab.h> 33#include <linux/slab.h>
34#include <linux/types.h> 34#include <linux/types.h>
35#include <linux/vmalloc.h> 35#include <linux/vmalloc.h>
36#include <linux/export.h>
36#include <asm/xen/hypervisor.h> 37#include <asm/xen/hypervisor.h>
38#include <asm/xen/page.h>
37#include <xen/interface/xen.h> 39#include <xen/interface/xen.h>
38#include <xen/interface/event_channel.h> 40#include <xen/interface/event_channel.h>
39#include <xen/events.h> 41#include <xen/events.h>
@@ -435,25 +437,26 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
435int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr) 437int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr)
436{ 438{
437 struct gnttab_map_grant_ref op = { 439 struct gnttab_map_grant_ref op = {
438 .flags = GNTMAP_host_map, 440 .flags = GNTMAP_host_map | GNTMAP_contains_pte,
439 .ref = gnt_ref, 441 .ref = gnt_ref,
440 .dom = dev->otherend_id, 442 .dom = dev->otherend_id,
441 }; 443 };
442 struct vm_struct *area; 444 struct vm_struct *area;
445 pte_t *pte;
443 446
444 *vaddr = NULL; 447 *vaddr = NULL;
445 448
446 area = xen_alloc_vm_area(PAGE_SIZE); 449 area = alloc_vm_area(PAGE_SIZE, &pte);
447 if (!area) 450 if (!area)
448 return -ENOMEM; 451 return -ENOMEM;
449 452
450 op.host_addr = (unsigned long)area->addr; 453 op.host_addr = arbitrary_virt_to_machine(pte).maddr;
451 454
452 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1)) 455 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
453 BUG(); 456 BUG();
454 457
455 if (op.status != GNTST_okay) { 458 if (op.status != GNTST_okay) {
456 xen_free_vm_area(area); 459 free_vm_area(area);
457 xenbus_dev_fatal(dev, op.status, 460 xenbus_dev_fatal(dev, op.status,
458 "mapping in shared page %d from domain %d", 461 "mapping in shared page %d from domain %d",
459 gnt_ref, dev->otherend_id); 462 gnt_ref, dev->otherend_id);
@@ -526,6 +529,7 @@ int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
526 struct gnttab_unmap_grant_ref op = { 529 struct gnttab_unmap_grant_ref op = {
527 .host_addr = (unsigned long)vaddr, 530 .host_addr = (unsigned long)vaddr,
528 }; 531 };
532 unsigned int level;
529 533
530 /* It'd be nice if linux/vmalloc.h provided a find_vm_area(void *addr) 534 /* It'd be nice if linux/vmalloc.h provided a find_vm_area(void *addr)
531 * method so that we don't have to muck with vmalloc internals here. 535 * method so that we don't have to muck with vmalloc internals here.
@@ -547,12 +551,14 @@ int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
547 } 551 }
548 552
549 op.handle = (grant_handle_t)area->phys_addr; 553 op.handle = (grant_handle_t)area->phys_addr;
554 op.host_addr = arbitrary_virt_to_machine(
555 lookup_address((unsigned long)vaddr, &level)).maddr;
550 556
551 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1)) 557 if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
552 BUG(); 558 BUG();
553 559
554 if (op.status == GNTST_okay) 560 if (op.status == GNTST_okay)
555 xen_free_vm_area(area); 561 free_vm_area(area);
556 else 562 else
557 xenbus_dev_error(dev, op.status, 563 xenbus_dev_error(dev, op.status,
558 "unmapping page at handle %d error %d", 564 "unmapping page at handle %d error %d",
diff --git a/drivers/xen/xenbus/xenbus_comms.c b/drivers/xen/xenbus/xenbus_comms.c
index 090c61ee8fd0..2eff7a6aaa20 100644
--- a/drivers/xen/xenbus/xenbus_comms.c
+++ b/drivers/xen/xenbus/xenbus_comms.c
@@ -212,7 +212,9 @@ int xb_init_comms(void)
212 printk(KERN_WARNING "XENBUS response ring is not quiescent " 212 printk(KERN_WARNING "XENBUS response ring is not quiescent "
213 "(%08x:%08x): fixing up\n", 213 "(%08x:%08x): fixing up\n",
214 intf->rsp_cons, intf->rsp_prod); 214 intf->rsp_cons, intf->rsp_prod);
215 intf->rsp_cons = intf->rsp_prod; 215 /* breaks kdump */
216 if (!reset_devices)
217 intf->rsp_cons = intf->rsp_prod;
216 } 218 }
217 219
218 if (xenbus_irq) { 220 if (xenbus_irq) {
diff --git a/drivers/xen/xenbus/xenbus_probe.c b/drivers/xen/xenbus/xenbus_probe.c
index bd2f90c9ac8b..1b178c6e8937 100644
--- a/drivers/xen/xenbus/xenbus_probe.c
+++ b/drivers/xen/xenbus/xenbus_probe.c
@@ -46,6 +46,7 @@
46#include <linux/mutex.h> 46#include <linux/mutex.h>
47#include <linux/io.h> 47#include <linux/io.h>
48#include <linux/slab.h> 48#include <linux/slab.h>
49#include <linux/module.h>
49 50
50#include <asm/page.h> 51#include <asm/page.h>
51#include <asm/pgtable.h> 52#include <asm/pgtable.h>
@@ -309,8 +310,7 @@ void xenbus_unregister_driver(struct xenbus_driver *drv)
309} 310}
310EXPORT_SYMBOL_GPL(xenbus_unregister_driver); 311EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
311 312
312struct xb_find_info 313struct xb_find_info {
313{
314 struct xenbus_device *dev; 314 struct xenbus_device *dev;
315 const char *nodename; 315 const char *nodename;
316}; 316};
@@ -639,7 +639,7 @@ int xenbus_dev_cancel(struct device *dev)
639EXPORT_SYMBOL_GPL(xenbus_dev_cancel); 639EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
640 640
641/* A flag to determine if xenstored is 'ready' (i.e. has started) */ 641/* A flag to determine if xenstored is 'ready' (i.e. has started) */
642int xenstored_ready = 0; 642int xenstored_ready;
643 643
644 644
645int register_xenstore_notifier(struct notifier_block *nb) 645int register_xenstore_notifier(struct notifier_block *nb)
@@ -684,64 +684,74 @@ static int __init xenbus_probe_initcall(void)
684 684
685device_initcall(xenbus_probe_initcall); 685device_initcall(xenbus_probe_initcall);
686 686
687static int __init xenbus_init(void) 687/* Set up event channel for xenstored which is run as a local process
688 * (this is normally used only in dom0)
689 */
690static int __init xenstored_local_init(void)
688{ 691{
689 int err = 0; 692 int err = 0;
690 unsigned long page = 0; 693 unsigned long page = 0;
694 struct evtchn_alloc_unbound alloc_unbound;
691 695
692 DPRINTK(""); 696 /* Allocate Xenstore page */
697 page = get_zeroed_page(GFP_KERNEL);
698 if (!page)
699 goto out_err;
693 700
694 err = -ENODEV; 701 xen_store_mfn = xen_start_info->store_mfn =
695 if (!xen_domain()) 702 pfn_to_mfn(virt_to_phys((void *)page) >>
696 return err; 703 PAGE_SHIFT);
697 704
698 /* 705 /* Next allocate a local port which xenstored can bind to */
699 * Domain0 doesn't have a store_evtchn or store_mfn yet. 706 alloc_unbound.dom = DOMID_SELF;
700 */ 707 alloc_unbound.remote_dom = DOMID_SELF;
701 if (xen_initial_domain()) {
702 struct evtchn_alloc_unbound alloc_unbound;
703 708
704 /* Allocate Xenstore page */ 709 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
705 page = get_zeroed_page(GFP_KERNEL); 710 &alloc_unbound);
706 if (!page) 711 if (err == -ENOSYS)
707 goto out_error; 712 goto out_err;
708 713
709 xen_store_mfn = xen_start_info->store_mfn = 714 BUG_ON(err);
710 pfn_to_mfn(virt_to_phys((void *)page) >> 715 xen_store_evtchn = xen_start_info->store_evtchn =
711 PAGE_SHIFT); 716 alloc_unbound.port;
712 717
713 /* Next allocate a local port which xenstored can bind to */ 718 return 0;
714 alloc_unbound.dom = DOMID_SELF;
715 alloc_unbound.remote_dom = 0;
716 719
717 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, 720 out_err:
718 &alloc_unbound); 721 if (page != 0)
719 if (err == -ENOSYS) 722 free_page(page);
720 goto out_error; 723 return err;
724}
721 725
722 BUG_ON(err); 726static int __init xenbus_init(void)
723 xen_store_evtchn = xen_start_info->store_evtchn = 727{
724 alloc_unbound.port; 728 int err = 0;
725 729
726 xen_store_interface = mfn_to_virt(xen_store_mfn); 730 if (!xen_domain())
731 return -ENODEV;
732
733 if (xen_hvm_domain()) {
734 uint64_t v = 0;
735 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
736 if (err)
737 goto out_error;
738 xen_store_evtchn = (int)v;
739 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
740 if (err)
741 goto out_error;
742 xen_store_mfn = (unsigned long)v;
743 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
727 } else { 744 } else {
728 if (xen_hvm_domain()) { 745 xen_store_evtchn = xen_start_info->store_evtchn;
729 uint64_t v = 0; 746 xen_store_mfn = xen_start_info->store_mfn;
730 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v); 747 if (xen_store_evtchn)
731 if (err) 748 xenstored_ready = 1;
732 goto out_error; 749 else {
733 xen_store_evtchn = (int)v; 750 err = xenstored_local_init();
734 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
735 if (err) 751 if (err)
736 goto out_error; 752 goto out_error;
737 xen_store_mfn = (unsigned long)v;
738 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
739 } else {
740 xen_store_evtchn = xen_start_info->store_evtchn;
741 xen_store_mfn = xen_start_info->store_mfn;
742 xen_store_interface = mfn_to_virt(xen_store_mfn);
743 xenstored_ready = 1;
744 } 753 }
754 xen_store_interface = mfn_to_virt(xen_store_mfn);
745 } 755 }
746 756
747 /* Initialize the interface to xenstore. */ 757 /* Initialize the interface to xenstore. */
@@ -760,12 +770,7 @@ static int __init xenbus_init(void)
760 proc_mkdir("xen", NULL); 770 proc_mkdir("xen", NULL);
761#endif 771#endif
762 772
763 return 0; 773out_error:
764
765 out_error:
766 if (page != 0)
767 free_page(page);
768
769 return err; 774 return err;
770} 775}
771 776
diff --git a/drivers/xen/xenbus/xenbus_probe.h b/drivers/xen/xenbus/xenbus_probe.h
index b814935378c7..9b1de4e34c64 100644
--- a/drivers/xen/xenbus/xenbus_probe.h
+++ b/drivers/xen/xenbus/xenbus_probe.h
@@ -36,8 +36,7 @@
36 36
37#define XEN_BUS_ID_SIZE 20 37#define XEN_BUS_ID_SIZE 20
38 38
39struct xen_bus_type 39struct xen_bus_type {
40{
41 char *root; 40 char *root;
42 unsigned int levels; 41 unsigned int levels;
43 int (*get_bus_id)(char bus_id[XEN_BUS_ID_SIZE], const char *nodename); 42 int (*get_bus_id)(char bus_id[XEN_BUS_ID_SIZE], const char *nodename);
diff --git a/drivers/xen/xenbus/xenbus_probe_backend.c b/drivers/xen/xenbus/xenbus_probe_backend.c
index 60adf919d78d..c3c7cd195c11 100644
--- a/drivers/xen/xenbus/xenbus_probe_backend.c
+++ b/drivers/xen/xenbus/xenbus_probe_backend.c
@@ -42,6 +42,7 @@
42#include <linux/fcntl.h> 42#include <linux/fcntl.h>
43#include <linux/mm.h> 43#include <linux/mm.h>
44#include <linux/notifier.h> 44#include <linux/notifier.h>
45#include <linux/export.h>
45 46
46#include <asm/page.h> 47#include <asm/page.h>
47#include <asm/pgtable.h> 48#include <asm/pgtable.h>
@@ -104,8 +105,6 @@ static int xenbus_uevent_backend(struct device *dev,
104 105
105 xdev = to_xenbus_device(dev); 106 xdev = to_xenbus_device(dev);
106 bus = container_of(xdev->dev.bus, struct xen_bus_type, bus); 107 bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
107 if (xdev == NULL)
108 return -ENODEV;
109 108
110 if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype)) 109 if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
111 return -ENOMEM; 110 return -ENOMEM;
diff --git a/drivers/xen/xenbus/xenbus_probe_frontend.c b/drivers/xen/xenbus/xenbus_probe_frontend.c
index ed2ba474a560..2f73195512b4 100644
--- a/drivers/xen/xenbus/xenbus_probe_frontend.c
+++ b/drivers/xen/xenbus/xenbus_probe_frontend.c
@@ -13,6 +13,7 @@
13#include <linux/kthread.h> 13#include <linux/kthread.h>
14#include <linux/mutex.h> 14#include <linux/mutex.h>
15#include <linux/io.h> 15#include <linux/io.h>
16#include <linux/module.h>
16 17
17#include <asm/page.h> 18#include <asm/page.h>
18#include <asm/pgtable.h> 19#include <asm/pgtable.h>
@@ -248,10 +249,131 @@ int __xenbus_register_frontend(struct xenbus_driver *drv,
248} 249}
249EXPORT_SYMBOL_GPL(__xenbus_register_frontend); 250EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
250 251
252static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
253static int backend_state;
254
255static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
256 const char **v, unsigned int l)
257{
258 xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
259 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
260 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
261 wake_up(&backend_state_wq);
262}
263
264static void xenbus_reset_wait_for_backend(char *be, int expected)
265{
266 long timeout;
267 timeout = wait_event_interruptible_timeout(backend_state_wq,
268 backend_state == expected, 5 * HZ);
269 if (timeout <= 0)
270 printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
271}
272
273/*
274 * Reset frontend if it is in Connected or Closed state.
275 * Wait for backend to catch up.
276 * State Connected happens during kdump, Closed after kexec.
277 */
278static void xenbus_reset_frontend(char *fe, char *be, int be_state)
279{
280 struct xenbus_watch be_watch;
281
282 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
283 be, xenbus_strstate(be_state));
284
285 memset(&be_watch, 0, sizeof(be_watch));
286 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
287 if (!be_watch.node)
288 return;
289
290 be_watch.callback = xenbus_reset_backend_state_changed;
291 backend_state = XenbusStateUnknown;
292
293 printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
294 register_xenbus_watch(&be_watch);
295
296 /* fall through to forward backend to state XenbusStateInitialising */
297 switch (be_state) {
298 case XenbusStateConnected:
299 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
300 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
301
302 case XenbusStateClosing:
303 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
304 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
305
306 case XenbusStateClosed:
307 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
308 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
309 }
310
311 unregister_xenbus_watch(&be_watch);
312 printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
313 kfree(be_watch.node);
314}
315
316static void xenbus_check_frontend(char *class, char *dev)
317{
318 int be_state, fe_state, err;
319 char *backend, *frontend;
320
321 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
322 if (!frontend)
323 return;
324
325 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
326 if (err != 1)
327 goto out;
328
329 switch (fe_state) {
330 case XenbusStateConnected:
331 case XenbusStateClosed:
332 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
333 frontend, xenbus_strstate(fe_state));
334 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
335 if (!backend || IS_ERR(backend))
336 goto out;
337 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
338 if (err == 1)
339 xenbus_reset_frontend(frontend, backend, be_state);
340 kfree(backend);
341 break;
342 default:
343 break;
344 }
345out:
346 kfree(frontend);
347}
348
349static void xenbus_reset_state(void)
350{
351 char **devclass, **dev;
352 int devclass_n, dev_n;
353 int i, j;
354
355 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
356 if (IS_ERR(devclass))
357 return;
358
359 for (i = 0; i < devclass_n; i++) {
360 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
361 if (IS_ERR(dev))
362 continue;
363 for (j = 0; j < dev_n; j++)
364 xenbus_check_frontend(devclass[i], dev[j]);
365 kfree(dev);
366 }
367 kfree(devclass);
368}
369
251static int frontend_probe_and_watch(struct notifier_block *notifier, 370static int frontend_probe_and_watch(struct notifier_block *notifier,
252 unsigned long event, 371 unsigned long event,
253 void *data) 372 void *data)
254{ 373{
374 /* reset devices in Connected or Closed state */
375 if (xen_hvm_domain())
376 xenbus_reset_state();
255 /* Enumerate devices in xenstore and watch for changes. */ 377 /* Enumerate devices in xenstore and watch for changes. */
256 xenbus_probe_devices(&xenbus_frontend); 378 xenbus_probe_devices(&xenbus_frontend);
257 register_xenbus_watch(&fe_watch); 379 register_xenbus_watch(&fe_watch);
diff --git a/drivers/xen/xenbus/xenbus_xs.c b/drivers/xen/xenbus/xenbus_xs.c
index 5534690075af..b3b8f2f3ad10 100644
--- a/drivers/xen/xenbus/xenbus_xs.c
+++ b/drivers/xen/xenbus/xenbus_xs.c
@@ -45,6 +45,7 @@
45#include <linux/module.h> 45#include <linux/module.h>
46#include <linux/mutex.h> 46#include <linux/mutex.h>
47#include <xen/xenbus.h> 47#include <xen/xenbus.h>
48#include <xen/xen.h>
48#include "xenbus_comms.h" 49#include "xenbus_comms.h"
49 50
50struct xs_stored_msg { 51struct xs_stored_msg {
@@ -620,6 +621,15 @@ static struct xenbus_watch *find_watch(const char *token)
620 return NULL; 621 return NULL;
621} 622}
622 623
624static void xs_reset_watches(void)
625{
626 int err;
627
628 err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL));
629 if (err && err != -EEXIST)
630 printk(KERN_WARNING "xs_reset_watches failed: %d\n", err);
631}
632
623/* Register callback to watch this node. */ 633/* Register callback to watch this node. */
624int register_xenbus_watch(struct xenbus_watch *watch) 634int register_xenbus_watch(struct xenbus_watch *watch)
625{ 635{
@@ -638,8 +648,7 @@ int register_xenbus_watch(struct xenbus_watch *watch)
638 648
639 err = xs_watch(watch->node, token); 649 err = xs_watch(watch->node, token);
640 650
641 /* Ignore errors due to multiple registration. */ 651 if (err) {
642 if ((err != 0) && (err != -EEXIST)) {
643 spin_lock(&watches_lock); 652 spin_lock(&watches_lock);
644 list_del(&watch->list); 653 list_del(&watch->list);
645 spin_unlock(&watches_lock); 654 spin_unlock(&watches_lock);
@@ -897,5 +906,9 @@ int xs_init(void)
897 if (IS_ERR(task)) 906 if (IS_ERR(task))
898 return PTR_ERR(task); 907 return PTR_ERR(task);
899 908
909 /* shutdown watches for kexec boot */
910 if (xen_hvm_domain())
911 xs_reset_watches();
912
900 return 0; 913 return 0;
901} 914}