2525#define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
2626
2727static int vfio_ap_mdev_reset_queues (struct mdev_device * mdev );
28+ static struct vfio_ap_queue * vfio_ap_find_queue (int apqn );
2829
2930static int match_apqn (struct device * dev , const void * data )
3031{
@@ -49,20 +50,15 @@ static struct vfio_ap_queue *vfio_ap_get_queue(
4950 int apqn )
5051{
5152 struct vfio_ap_queue * q ;
52- struct device * dev ;
5353
5454 if (!test_bit_inv (AP_QID_CARD (apqn ), matrix_mdev -> matrix .apm ))
5555 return NULL ;
5656 if (!test_bit_inv (AP_QID_QUEUE (apqn ), matrix_mdev -> matrix .aqm ))
5757 return NULL ;
5858
59- dev = driver_find_device (& matrix_dev -> vfio_ap_drv -> driver , NULL ,
60- & apqn , match_apqn );
61- if (!dev )
62- return NULL ;
63- q = dev_get_drvdata (dev );
64- q -> matrix_mdev = matrix_mdev ;
65- put_device (dev );
59+ q = vfio_ap_find_queue (apqn );
60+ if (q )
61+ q -> matrix_mdev = matrix_mdev ;
6662
6763 return q ;
6864}
@@ -119,13 +115,18 @@ static void vfio_ap_wait_for_irqclear(int apqn)
119115 */
120116static void vfio_ap_free_aqic_resources (struct vfio_ap_queue * q )
121117{
122- if (q -> saved_isc != VFIO_AP_ISC_INVALID && q -> matrix_mdev )
118+ if (!q )
119+ return ;
120+ if (q -> saved_isc != VFIO_AP_ISC_INVALID &&
121+ !WARN_ON (!(q -> matrix_mdev && q -> matrix_mdev -> kvm ))) {
123122 kvm_s390_gisc_unregister (q -> matrix_mdev -> kvm , q -> saved_isc );
124- if (q -> saved_pfn && q -> matrix_mdev )
123+ q -> saved_isc = VFIO_AP_ISC_INVALID ;
124+ }
125+ if (q -> saved_pfn && !WARN_ON (!q -> matrix_mdev )) {
125126 vfio_unpin_pages (mdev_dev (q -> matrix_mdev -> mdev ),
126127 & q -> saved_pfn , 1 );
127- q -> saved_pfn = 0 ;
128- q -> saved_isc = VFIO_AP_ISC_INVALID ;
128+ q -> saved_pfn = 0 ;
129+ }
129130}
130131
131132/**
@@ -144,7 +145,7 @@ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
144145 * Returns if ap_aqic function failed with invalid, deconfigured or
145146 * checkstopped AP.
146147 */
147- struct ap_queue_status vfio_ap_irq_disable (struct vfio_ap_queue * q )
148+ static struct ap_queue_status vfio_ap_irq_disable (struct vfio_ap_queue * q )
148149{
149150 struct ap_qirq_ctrl aqic_gisa = {};
150151 struct ap_queue_status status ;
@@ -1126,70 +1127,93 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
11261127 return notify_rc ;
11271128}
11281129
1129- static void vfio_ap_irq_disable_apqn (int apqn )
1130+ static struct vfio_ap_queue * vfio_ap_find_queue (int apqn )
11301131{
11311132 struct device * dev ;
1132- struct vfio_ap_queue * q ;
1133+ struct vfio_ap_queue * q = NULL ;
11331134
11341135 dev = driver_find_device (& matrix_dev -> vfio_ap_drv -> driver , NULL ,
11351136 & apqn , match_apqn );
11361137 if (dev ) {
11371138 q = dev_get_drvdata (dev );
1138- vfio_ap_irq_disable (q );
11391139 put_device (dev );
11401140 }
1141+
1142+ return q ;
11411143}
11421144
1143- int vfio_ap_mdev_reset_queue (unsigned int apid , unsigned int apqi ,
1145+ int vfio_ap_mdev_reset_queue (struct vfio_ap_queue * q ,
11441146 unsigned int retry )
11451147{
11461148 struct ap_queue_status status ;
1149+ int ret ;
11471150 int retry2 = 2 ;
1148- int apqn = AP_MKQID (apid , apqi );
11491151
1150- do {
1151- status = ap_zapq (apqn );
1152- switch (status .response_code ) {
1153- case AP_RESPONSE_NORMAL :
1154- while (!status .queue_empty && retry2 -- ) {
1155- msleep (20 );
1156- status = ap_tapq (apqn , NULL );
1157- }
1158- WARN_ON_ONCE (retry2 <= 0 );
1159- return 0 ;
1160- case AP_RESPONSE_RESET_IN_PROGRESS :
1161- case AP_RESPONSE_BUSY :
1152+ if (!q )
1153+ return 0 ;
1154+
1155+ retry_zapq :
1156+ status = ap_zapq (q -> apqn );
1157+ switch (status .response_code ) {
1158+ case AP_RESPONSE_NORMAL :
1159+ ret = 0 ;
1160+ break ;
1161+ case AP_RESPONSE_RESET_IN_PROGRESS :
1162+ if (retry -- ) {
11621163 msleep (20 );
1163- break ;
1164- default :
1165- /* things are really broken, give up */
1166- return - EIO ;
1164+ goto retry_zapq ;
11671165 }
1168- } while (retry -- );
1166+ ret = - EBUSY ;
1167+ break ;
1168+ case AP_RESPONSE_Q_NOT_AVAIL :
1169+ case AP_RESPONSE_DECONFIGURED :
1170+ case AP_RESPONSE_CHECKSTOPPED :
1171+ WARN_ON_ONCE (status .irq_enabled );
1172+ ret = - EBUSY ;
1173+ goto free_resources ;
1174+ default :
1175+ /* things are really broken, give up */
1176+ WARN (true, "PQAP/ZAPQ completed with invalid rc (%x)\n" ,
1177+ status .response_code );
1178+ return - EIO ;
1179+ }
1180+
1181+ /* wait for the reset to take effect */
1182+ while (retry2 -- ) {
1183+ if (status .queue_empty && !status .irq_enabled )
1184+ break ;
1185+ msleep (20 );
1186+ status = ap_tapq (q -> apqn , NULL );
1187+ }
1188+ WARN_ON_ONCE (retry2 <= 0 );
11691189
1170- return - EBUSY ;
1190+ free_resources :
1191+ vfio_ap_free_aqic_resources (q );
1192+
1193+ return ret ;
11711194}
11721195
11731196static int vfio_ap_mdev_reset_queues (struct mdev_device * mdev )
11741197{
11751198 int ret ;
11761199 int rc = 0 ;
11771200 unsigned long apid , apqi ;
1201+ struct vfio_ap_queue * q ;
11781202 struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
11791203
11801204 for_each_set_bit_inv (apid , matrix_mdev -> matrix .apm ,
11811205 matrix_mdev -> matrix .apm_max + 1 ) {
11821206 for_each_set_bit_inv (apqi , matrix_mdev -> matrix .aqm ,
11831207 matrix_mdev -> matrix .aqm_max + 1 ) {
1184- ret = vfio_ap_mdev_reset_queue (apid , apqi , 1 );
1208+ q = vfio_ap_find_queue (AP_MKQID (apid , apqi ));
1209+ ret = vfio_ap_mdev_reset_queue (q , 1 );
11851210 /*
11861211 * Regardless whether a queue turns out to be busy, or
11871212 * is not operational, we need to continue resetting
11881213 * the remaining queues.
11891214 */
11901215 if (ret )
11911216 rc = ret ;
1192- vfio_ap_irq_disable_apqn (AP_MKQID (apid , apqi ));
11931217 }
11941218 }
11951219
0 commit comments