<?xml version="1.0"?>
<!-- Copyright (C) 2022 Intel Corporation. -->
<!-- SPDX-License-Identifier: BSD-3-Clause -->
<xs:schema xml:id="root"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:acrn="https://projectacrn.org">

  <xs:assert test="every $root in /acrn-config satisfies
                   every $vm in $root/vm satisfies
		   every $vbdf in $root/hv//vuart_connection[type='pci']/endpoint[vm_name=$vm/name]/vbdf/text() | $root/hv//IVSHMEM_VM[VM_NAME=$vm/name]/VBDF/text() satisfies
		   count($root/hv//vuart_connection[type='pci']/endpoint[vm_name=$vm/name and vbdf=$vbdf] | $root/hv//IVSHMEM_VM[VM_NAME=$vm/name and VBDF=$vbdf]) = 1">
    <xs:annotation acrn:severity="error" acrn:report-on="$root/hv//vuart_connection[type='pci']/endpoint[vm_name=$vm/name and vbdf=$vbdf] | $root/hv//IVSHMEM_VM[VM_NAME=$vm/name and VBDF=$vbdf]">
      <xs:documentation>VM "{$vm/name}" contains multiple virtual UART controllers or IVSHMEM interfaces using BDF {$vbdf}. Adjust the BDF of those devices to be unique.</xs:documentation>
    </xs:annotation>
  </xs:assert>


  <xs:assert test="every $root in /acrn-config satisfies
                   every $vm in $root/vm[./load_order/text()='SERVICE_VM'] satisfies
                   every $vbdf in $root/hv//vuart_connection[type='pci']/endpoint[vm_name=$vm/name]/vbdf/text() | $root/hv//IVSHMEM_VM[VM_NAME=$vm/name]/VBDF/text() satisfies
                   contains(/acrn-config//PCI_DEVICE/text(), $vbdf) = false()">
    <xs:annotation acrn:severity="error" acrn:report-on="$root/hv//vuart_connection[type='pci']/endpoint[vm_name=$vm/name and vbdf=$vbdf] | $root/hv//IVSHMEM_VM[VM_NAME=$vm/name and VBDF=$vbdf]">
      <xs:documentation>VM "{$vm/name}" virtual UART controllers or IVSHMEM interfaces using BDF {$vbdf} conflict with native PCI device. Adjust the BDF of those devices to be unique.</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $vm in /acrn-config//vm[./load_order/text() = 'PRE_LAUNCHED_VM' and ./mmio_resources/p2sb/text() = 'y'] satisfies
                   count(/acrn-config//HIDDEN_PDEV[./text() = '00:1f.1']) = 0">
    <xs:annotation acrn:severity="error" acrn:report-on="/acrn-config//p2sb[./text() = 'y']">
      <xs:documentation>Can't passthrough P2SB to VM {/acrn-config//vm[./load_order/text() = 'PRE_LAUNCHED_VM' and .//p2sb/text() = 'y']/name/text()} because '00:1f.1' is set as a hidden device".</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $ivshmem in /acrn-config//IVSHMEM_VM satisfies
                   if (/acrn-config//vm[./name/text() = $ivshmem/VM_NAME/text()]/load_order/text() = 'POST_LAUNCHED_VM')
                   then (substring-before(substring-after($ivshmem/VBDF/text(), ':'), '.') &gt; '02') and (substring-before(substring-after($ivshmem/VBDF/text(), ':'), '.') &lt; '1f')
	           else true()">
    <xs:annotation acrn:severity="error" acrn:report-on="$ivshmem">
      <xs:documentation>For post launched VM, the "device" value of a virtual BDF, which is used by {$ivshmem/VM_NAME/text()}, shall not be '00', '01', '02' or '1f', i.e., the allowed values are between 03 and 1e, inclusive.</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $endpoint in /acrn-config//endpoint satisfies
                   if (/acrn-config//vm[./name/text() = $endpoint/vm_name/text()]/load_order/text() = 'POST_LAUNCHED_VM')
                   then (substring-before(substring-after($endpoint/vbdf/text(), ':'), '.') &gt; '02') and (substring-before(substring-after($endpoint/VBDF/text(), ':'), '.') &lt; '1f')
	           else true()">
    <xs:annotation acrn:severity="error" acrn:report-on="$endpoint">
      <xs:documentation>For post launched VM, the "device" value of a virtual BDF, which is used by {$endpoint/vm_name/text()}, shall not be '00', '01', '02' or '1f', i.e., the allowed values are between 03 and 1e, inclusive.</xs:documentation>
    </xs:annotation>
  </xs:assert>

</xs:schema>
