30
30
#define AP_QUEUE_UNASSIGNED "unassigned"
31
31
#define AP_QUEUE_IN_USE "in use"
32
32
33
- #define MAX_RESET_CHECK_WAIT 200 /* Sleep max 200ms for reset check */
34
33
#define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */
35
34
36
35
static int vfio_ap_mdev_reset_queues (struct ap_queue_table * qtable );
37
36
static struct vfio_ap_queue * vfio_ap_find_queue (int apqn );
38
37
static const struct vfio_device_ops vfio_ap_matrix_dev_ops ;
39
- static int vfio_ap_mdev_reset_queue (struct vfio_ap_queue * q );
38
+ static void vfio_ap_mdev_reset_queue (struct vfio_ap_queue * q );
40
39
41
40
/**
42
41
* get_update_locks_for_kvm: Acquire the locks required to dynamically update a
@@ -360,6 +359,28 @@ static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
360
359
return 0 ;
361
360
}
362
361
362
+ static int ensure_nib_shared (unsigned long addr , struct gmap * gmap )
363
+ {
364
+ int ret ;
365
+
366
+ /*
367
+ * The nib has to be located in shared storage since guest and
368
+ * host access it. vfio_pin_pages() will do a pin shared and
369
+ * if that fails (possibly because it's not a shared page) it
370
+ * calls export. We try to do a second pin shared here so that
371
+ * the UV gives us an error code if we try to pin a non-shared
372
+ * page.
373
+ *
374
+ * If the page is already pinned shared the UV will return a success.
375
+ */
376
+ ret = uv_pin_shared (addr );
377
+ if (ret ) {
378
+ /* vfio_pin_pages() likely exported the page so let's re-import */
379
+ gmap_convert_to_secure (gmap , addr );
380
+ }
381
+ return ret ;
382
+ }
383
+
363
384
/**
364
385
* vfio_ap_irq_enable - Enable Interruption for a APQN
365
386
*
@@ -423,6 +444,14 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
423
444
h_nib = page_to_phys (h_page ) | (nib & ~PAGE_MASK );
424
445
aqic_gisa .gisc = isc ;
425
446
447
+ /* NIB in non-shared storage is a rc 6 for PV guests */
448
+ if (kvm_s390_pv_cpu_is_protected (vcpu ) &&
449
+ ensure_nib_shared (h_nib & PAGE_MASK , kvm -> arch .gmap )) {
450
+ vfio_unpin_pages (& q -> matrix_mdev -> vdev , nib , 1 );
451
+ status .response_code = AP_RESPONSE_INVALID_ADDRESS ;
452
+ return status ;
453
+ }
454
+
426
455
nisc = kvm_s390_gisc_register (kvm , isc );
427
456
if (nisc < 0 ) {
428
457
VFIO_AP_DBF_WARN ("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n" ,
@@ -675,7 +704,7 @@ static bool vfio_ap_mdev_filter_matrix(unsigned long *apm, unsigned long *aqm,
675
704
*/
676
705
apqn = AP_MKQID (apid , apqi );
677
706
q = vfio_ap_mdev_get_queue (matrix_mdev , apqn );
678
- if (!q || q -> reset_rc ) {
707
+ if (!q || q -> reset_status . response_code ) {
679
708
clear_bit_inv (apid ,
680
709
matrix_mdev -> shadow_apcb .apm );
681
710
break ;
@@ -1608,19 +1637,21 @@ static int apq_status_check(int apqn, struct ap_queue_status *status)
1608
1637
{
1609
1638
switch (status -> response_code ) {
1610
1639
case AP_RESPONSE_NORMAL :
1640
+ case AP_RESPONSE_DECONFIGURED :
1641
+ return 0 ;
1611
1642
case AP_RESPONSE_RESET_IN_PROGRESS :
1612
- if (status -> queue_empty && !status -> irq_enabled )
1613
- return 0 ;
1643
+ case AP_RESPONSE_BUSY :
1614
1644
return - EBUSY ;
1615
- case AP_RESPONSE_DECONFIGURED :
1645
+ case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE :
1646
+ case AP_RESPONSE_ASSOC_FAILED :
1616
1647
/*
1617
- * If the AP queue is deconfigured, any subsequent AP command
1618
- * targeting the queue will fail with the same response code. On the
1619
- * other hand, when an AP adapter is deconfigured, the associated
1620
- * queues are reset, so let's return a value indicating the reset
1621
- * for which we're waiting completed successfully .
1648
+ * These asynchronous response codes indicate a PQAP(AAPQ)
1649
+ * instruction to associate a secret with the guest failed. All
1650
+ * subsequent AP instructions will end with the asynchronous
1651
+ * response code until the AP queue is reset; so, let's return
1652
+ * a value indicating a reset needs to be performed again .
1622
1653
*/
1623
- return 0 ;
1654
+ return - EAGAIN ;
1624
1655
default :
1625
1656
WARN (true,
1626
1657
"failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n" ,
@@ -1630,91 +1661,105 @@ static int apq_status_check(int apqn, struct ap_queue_status *status)
1630
1661
}
1631
1662
}
1632
1663
1633
- static int apq_reset_check (struct vfio_ap_queue * q )
1664
+ #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
1665
+
1666
+ static void apq_reset_check (struct work_struct * reset_work )
1634
1667
{
1635
- int ret ;
1636
- int iters = MAX_RESET_CHECK_WAIT / AP_RESET_INTERVAL ;
1668
+ int ret = - EBUSY , elapsed = 0 ;
1637
1669
struct ap_queue_status status ;
1670
+ struct vfio_ap_queue * q ;
1638
1671
1639
- for (; iters > 0 ; iters -- ) {
1672
+ q = container_of (reset_work , struct vfio_ap_queue , reset_work );
1673
+ memcpy (& status , & q -> reset_status , sizeof (status ));
1674
+ while (true) {
1640
1675
msleep (AP_RESET_INTERVAL );
1676
+ elapsed += AP_RESET_INTERVAL ;
1641
1677
status = ap_tapq (q -> apqn , NULL );
1642
1678
ret = apq_status_check (q -> apqn , & status );
1643
- if (ret != - EBUSY )
1644
- return ret ;
1679
+ if (ret == - EIO )
1680
+ return ;
1681
+ if (ret == - EBUSY ) {
1682
+ pr_notice_ratelimited (WAIT_MSG , elapsed ,
1683
+ AP_QID_CARD (q -> apqn ),
1684
+ AP_QID_QUEUE (q -> apqn ),
1685
+ status .response_code ,
1686
+ status .queue_empty ,
1687
+ status .irq_enabled );
1688
+ } else {
1689
+ if (q -> reset_status .response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
1690
+ q -> reset_status .response_code == AP_RESPONSE_BUSY ||
1691
+ q -> reset_status .response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
1692
+ ret == - EAGAIN ) {
1693
+ status = ap_zapq (q -> apqn , 0 );
1694
+ memcpy (& q -> reset_status , & status , sizeof (status ));
1695
+ continue ;
1696
+ }
1697
+ /*
1698
+ * When an AP adapter is deconfigured, the
1699
+ * associated queues are reset, so let's set the
1700
+ * status response code to 0 so the queue may be
1701
+ * passed through (i.e., not filtered)
1702
+ */
1703
+ if (status .response_code == AP_RESPONSE_DECONFIGURED )
1704
+ q -> reset_status .response_code = 0 ;
1705
+ if (q -> saved_isc != VFIO_AP_ISC_INVALID )
1706
+ vfio_ap_free_aqic_resources (q );
1707
+ break ;
1708
+ }
1645
1709
}
1646
- WARN_ONCE (iters <= 0 ,
1647
- "timeout verifying reset of queue %02x.%04x (%u, %u, %u)" ,
1648
- AP_QID_CARD (q -> apqn ), AP_QID_QUEUE (q -> apqn ),
1649
- status .queue_empty , status .irq_enabled , status .response_code );
1650
- return ret ;
1651
1710
}
1652
1711
1653
- static int vfio_ap_mdev_reset_queue (struct vfio_ap_queue * q )
1712
+ static void vfio_ap_mdev_reset_queue (struct vfio_ap_queue * q )
1654
1713
{
1655
1714
struct ap_queue_status status ;
1656
- int ret ;
1657
1715
1658
1716
if (!q )
1659
- return 0 ;
1660
- retry_zapq :
1717
+ return ;
1661
1718
status = ap_zapq (q -> apqn , 0 );
1662
- q -> reset_rc = status . response_code ;
1719
+ memcpy ( & q -> reset_status , & status , sizeof ( status )) ;
1663
1720
switch (status .response_code ) {
1664
1721
case AP_RESPONSE_NORMAL :
1665
- ret = 0 ;
1666
- /* if the reset has not completed, wait for it to take effect */
1667
- if (!status .queue_empty || status .irq_enabled )
1668
- ret = apq_reset_check (q );
1669
- break ;
1670
1722
case AP_RESPONSE_RESET_IN_PROGRESS :
1723
+ case AP_RESPONSE_BUSY :
1724
+ case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS :
1671
1725
/*
1672
- * There is a reset issued by another process in progress. Let's wait
1673
- * for that to complete. Since we have no idea whether it was a RAPQ or
1674
- * ZAPQ, then if it completes successfully, let's issue the ZAPQ.
1726
+ * Let's verify whether the ZAPQ completed successfully on a work queue.
1675
1727
*/
1676
- ret = apq_reset_check (q );
1677
- if (ret )
1678
- break ;
1679
- goto retry_zapq ;
1728
+ queue_work (system_long_wq , & q -> reset_work );
1729
+ break ;
1680
1730
case AP_RESPONSE_DECONFIGURED :
1681
1731
/*
1682
1732
* When an AP adapter is deconfigured, the associated
1683
- * queues are reset, so let's return a value indicating the reset
1684
- * completed successfully .
1733
+ * queues are reset, so let's set the status response code to 0
1734
+ * so the queue may be passed through (i.e., not filtered) .
1685
1735
*/
1686
- ret = 0 ;
1736
+ q -> reset_status .response_code = 0 ;
1737
+ vfio_ap_free_aqic_resources (q );
1687
1738
break ;
1688
1739
default :
1689
1740
WARN (true,
1690
1741
"PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n" ,
1691
1742
AP_QID_CARD (q -> apqn ), AP_QID_QUEUE (q -> apqn ),
1692
1743
status .response_code );
1693
- return - EIO ;
1694
1744
}
1695
-
1696
- vfio_ap_free_aqic_resources (q );
1697
-
1698
- return ret ;
1699
1745
}
1700
1746
1701
1747
static int vfio_ap_mdev_reset_queues (struct ap_queue_table * qtable )
1702
1748
{
1703
- int ret , loop_cursor , rc = 0 ;
1749
+ int ret = 0 , loop_cursor ;
1704
1750
struct vfio_ap_queue * q ;
1705
1751
1752
+ hash_for_each (qtable -> queues , loop_cursor , q , mdev_qnode )
1753
+ vfio_ap_mdev_reset_queue (q );
1754
+
1706
1755
hash_for_each (qtable -> queues , loop_cursor , q , mdev_qnode ) {
1707
- ret = vfio_ap_mdev_reset_queue (q );
1708
- /*
1709
- * Regardless whether a queue turns out to be busy, or
1710
- * is not operational, we need to continue resetting
1711
- * the remaining queues.
1712
- */
1713
- if (ret )
1714
- rc = ret ;
1756
+ flush_work (& q -> reset_work );
1757
+
1758
+ if (q -> reset_status .response_code )
1759
+ ret = - EIO ;
1715
1760
}
1716
1761
1717
- return rc ;
1762
+ return ret ;
1718
1763
}
1719
1764
1720
1765
static int vfio_ap_mdev_open_device (struct vfio_device * vdev )
@@ -2038,6 +2083,8 @@ int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
2038
2083
2039
2084
q -> apqn = to_ap_queue (& apdev -> device )-> qid ;
2040
2085
q -> saved_isc = VFIO_AP_ISC_INVALID ;
2086
+ memset (& q -> reset_status , 0 , sizeof (q -> reset_status ));
2087
+ INIT_WORK (& q -> reset_work , apq_reset_check );
2041
2088
matrix_mdev = get_update_locks_by_apqn (q -> apqn );
2042
2089
2043
2090
if (matrix_mdev ) {
@@ -2087,6 +2134,7 @@ void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
2087
2134
}
2088
2135
2089
2136
vfio_ap_mdev_reset_queue (q );
2137
+ flush_work (& q -> reset_work );
2090
2138
dev_set_drvdata (& apdev -> device , NULL );
2091
2139
kfree (q );
2092
2140
release_update_locks_for_mdev (matrix_mdev );
0 commit comments