2424#define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
2525#define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
2626
27- static int vfio_ap_mdev_reset_queues (struct mdev_device * mdev );
27+ static int vfio_ap_mdev_reset_queues (struct ap_matrix_mdev * matrix_mdev );
2828static struct vfio_ap_queue * vfio_ap_find_queue (int apqn );
29+ static const struct vfio_device_ops vfio_ap_matrix_dev_ops ;
2930
3031static int match_apqn (struct device * dev , const void * data )
3132{
@@ -326,43 +327,57 @@ static void vfio_ap_matrix_init(struct ap_config_info *info,
326327 matrix -> adm_max = info -> apxa ? info -> Nd : 15 ;
327328}
328329
329- static int vfio_ap_mdev_create (struct mdev_device * mdev )
330+ static int vfio_ap_mdev_probe (struct mdev_device * mdev )
330331{
331332 struct ap_matrix_mdev * matrix_mdev ;
333+ int ret ;
332334
333335 if ((atomic_dec_if_positive (& matrix_dev -> available_instances ) < 0 ))
334336 return - EPERM ;
335337
336338 matrix_mdev = kzalloc (sizeof (* matrix_mdev ), GFP_KERNEL );
337339 if (!matrix_mdev ) {
338- atomic_inc ( & matrix_dev -> available_instances ) ;
339- return - ENOMEM ;
340+ ret = - ENOMEM ;
341+ goto err_dec_available ;
340342 }
343+ vfio_init_group_dev (& matrix_mdev -> vdev , & mdev -> dev ,
344+ & vfio_ap_matrix_dev_ops );
341345
342346 matrix_mdev -> mdev = mdev ;
343347 vfio_ap_matrix_init (& matrix_dev -> info , & matrix_mdev -> matrix );
344- mdev_set_drvdata (mdev , matrix_mdev );
345348 matrix_mdev -> pqap_hook = handle_pqap ;
346349 mutex_lock (& matrix_dev -> lock );
347350 list_add (& matrix_mdev -> node , & matrix_dev -> mdev_list );
348351 mutex_unlock (& matrix_dev -> lock );
349352
353+ ret = vfio_register_group_dev (& matrix_mdev -> vdev );
354+ if (ret )
355+ goto err_list ;
356+ dev_set_drvdata (& mdev -> dev , matrix_mdev );
350357 return 0 ;
358+
359+ err_list :
360+ mutex_lock (& matrix_dev -> lock );
361+ list_del (& matrix_mdev -> node );
362+ mutex_unlock (& matrix_dev -> lock );
363+ kfree (matrix_mdev );
364+ err_dec_available :
365+ atomic_inc (& matrix_dev -> available_instances );
366+ return ret ;
351367}
352368
353- static int vfio_ap_mdev_remove (struct mdev_device * mdev )
369+ static void vfio_ap_mdev_remove (struct mdev_device * mdev )
354370{
355- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
371+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (& mdev -> dev );
372+
373+ vfio_unregister_group_dev (& matrix_mdev -> vdev );
356374
357375 mutex_lock (& matrix_dev -> lock );
358- vfio_ap_mdev_reset_queues (mdev );
376+ vfio_ap_mdev_reset_queues (matrix_mdev );
359377 list_del (& matrix_mdev -> node );
360378 kfree (matrix_mdev );
361- mdev_set_drvdata (mdev , NULL );
362379 atomic_inc (& matrix_dev -> available_instances );
363380 mutex_unlock (& matrix_dev -> lock );
364-
365- return 0 ;
366381}
367382
368383static ssize_t name_show (struct mdev_type * mtype ,
@@ -604,8 +619,7 @@ static ssize_t assign_adapter_store(struct device *dev,
604619{
605620 int ret ;
606621 unsigned long apid ;
607- struct mdev_device * mdev = mdev_from_dev (dev );
608- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
622+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (dev );
609623
610624 mutex_lock (& matrix_dev -> lock );
611625
@@ -674,8 +688,7 @@ static ssize_t unassign_adapter_store(struct device *dev,
674688{
675689 int ret ;
676690 unsigned long apid ;
677- struct mdev_device * mdev = mdev_from_dev (dev );
678- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
691+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (dev );
679692
680693 mutex_lock (& matrix_dev -> lock );
681694
@@ -760,8 +773,7 @@ static ssize_t assign_domain_store(struct device *dev,
760773{
761774 int ret ;
762775 unsigned long apqi ;
763- struct mdev_device * mdev = mdev_from_dev (dev );
764- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
776+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (dev );
765777 unsigned long max_apqi = matrix_mdev -> matrix .aqm_max ;
766778
767779 mutex_lock (& matrix_dev -> lock );
@@ -826,8 +838,7 @@ static ssize_t unassign_domain_store(struct device *dev,
826838{
827839 int ret ;
828840 unsigned long apqi ;
829- struct mdev_device * mdev = mdev_from_dev (dev );
830- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
841+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (dev );
831842
832843 mutex_lock (& matrix_dev -> lock );
833844
@@ -877,8 +888,7 @@ static ssize_t assign_control_domain_store(struct device *dev,
877888{
878889 int ret ;
879890 unsigned long id ;
880- struct mdev_device * mdev = mdev_from_dev (dev );
881- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
891+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (dev );
882892
883893 mutex_lock (& matrix_dev -> lock );
884894
@@ -932,8 +942,7 @@ static ssize_t unassign_control_domain_store(struct device *dev,
932942{
933943 int ret ;
934944 unsigned long domid ;
935- struct mdev_device * mdev = mdev_from_dev (dev );
936- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
945+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (dev );
937946 unsigned long max_domid = matrix_mdev -> matrix .adm_max ;
938947
939948 mutex_lock (& matrix_dev -> lock );
@@ -968,8 +977,7 @@ static ssize_t control_domains_show(struct device *dev,
968977 int nchars = 0 ;
969978 int n ;
970979 char * bufpos = buf ;
971- struct mdev_device * mdev = mdev_from_dev (dev );
972- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
980+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (dev );
973981 unsigned long max_domid = matrix_mdev -> matrix .adm_max ;
974982
975983 mutex_lock (& matrix_dev -> lock );
@@ -987,8 +995,7 @@ static DEVICE_ATTR_RO(control_domains);
987995static ssize_t matrix_show (struct device * dev , struct device_attribute * attr ,
988996 char * buf )
989997{
990- struct mdev_device * mdev = mdev_from_dev (dev );
991- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
998+ struct ap_matrix_mdev * matrix_mdev = dev_get_drvdata (dev );
992999 char * bufpos = buf ;
9931000 unsigned long apid ;
9941001 unsigned long apqi ;
@@ -1165,7 +1172,7 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev,
11651172 mutex_lock (& matrix_dev -> lock );
11661173
11671174 kvm_arch_crypto_clear_masks (kvm );
1168- vfio_ap_mdev_reset_queues (matrix_mdev -> mdev );
1175+ vfio_ap_mdev_reset_queues (matrix_mdev );
11691176 kvm_put_kvm (kvm );
11701177 matrix_mdev -> kvm = NULL ;
11711178
@@ -1259,13 +1266,12 @@ int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
12591266 return ret ;
12601267}
12611268
1262- static int vfio_ap_mdev_reset_queues (struct mdev_device * mdev )
1269+ static int vfio_ap_mdev_reset_queues (struct ap_matrix_mdev * matrix_mdev )
12631270{
12641271 int ret ;
12651272 int rc = 0 ;
12661273 unsigned long apid , apqi ;
12671274 struct vfio_ap_queue * q ;
1268- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
12691275
12701276 for_each_set_bit_inv (apid , matrix_mdev -> matrix .apm ,
12711277 matrix_mdev -> matrix .apm_max + 1 ) {
@@ -1286,49 +1292,45 @@ static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev)
12861292 return rc ;
12871293}
12881294
1289- static int vfio_ap_mdev_open_device (struct mdev_device * mdev )
1295+ static int vfio_ap_mdev_open_device (struct vfio_device * vdev )
12901296{
1291- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
1297+ struct ap_matrix_mdev * matrix_mdev =
1298+ container_of (vdev , struct ap_matrix_mdev , vdev );
12921299 unsigned long events ;
12931300 int ret ;
12941301
1295-
1296- if (!try_module_get (THIS_MODULE ))
1297- return - ENODEV ;
1298-
12991302 matrix_mdev -> group_notifier .notifier_call = vfio_ap_mdev_group_notifier ;
13001303 events = VFIO_GROUP_NOTIFY_SET_KVM ;
13011304
1302- ret = vfio_register_notifier (mdev_dev ( mdev ) , VFIO_GROUP_NOTIFY ,
1305+ ret = vfio_register_notifier (vdev -> dev , VFIO_GROUP_NOTIFY ,
13031306 & events , & matrix_mdev -> group_notifier );
1304- if (ret ) {
1305- module_put (THIS_MODULE );
1307+ if (ret )
13061308 return ret ;
1307- }
13081309
13091310 matrix_mdev -> iommu_notifier .notifier_call = vfio_ap_mdev_iommu_notifier ;
13101311 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP ;
1311- ret = vfio_register_notifier (mdev_dev ( mdev ) , VFIO_IOMMU_NOTIFY ,
1312+ ret = vfio_register_notifier (vdev -> dev , VFIO_IOMMU_NOTIFY ,
13121313 & events , & matrix_mdev -> iommu_notifier );
1313- if (!ret )
1314- return ret ;
1314+ if (ret )
1315+ goto out_unregister_group ;
1316+ return 0 ;
13151317
1316- vfio_unregister_notifier (mdev_dev (mdev ), VFIO_GROUP_NOTIFY ,
1318+ out_unregister_group :
1319+ vfio_unregister_notifier (vdev -> dev , VFIO_GROUP_NOTIFY ,
13171320 & matrix_mdev -> group_notifier );
1318- module_put (THIS_MODULE );
13191321 return ret ;
13201322}
13211323
1322- static void vfio_ap_mdev_close_device (struct mdev_device * mdev )
1324+ static void vfio_ap_mdev_close_device (struct vfio_device * vdev )
13231325{
1324- struct ap_matrix_mdev * matrix_mdev = mdev_get_drvdata (mdev );
1326+ struct ap_matrix_mdev * matrix_mdev =
1327+ container_of (vdev , struct ap_matrix_mdev , vdev );
13251328
1326- vfio_unregister_notifier (mdev_dev ( mdev ) , VFIO_IOMMU_NOTIFY ,
1329+ vfio_unregister_notifier (vdev -> dev , VFIO_IOMMU_NOTIFY ,
13271330 & matrix_mdev -> iommu_notifier );
1328- vfio_unregister_notifier (mdev_dev ( mdev ) , VFIO_GROUP_NOTIFY ,
1331+ vfio_unregister_notifier (vdev -> dev , VFIO_GROUP_NOTIFY ,
13291332 & matrix_mdev -> group_notifier );
13301333 vfio_ap_mdev_unset_kvm (matrix_mdev , matrix_mdev -> kvm );
1331- module_put (THIS_MODULE );
13321334}
13331335
13341336static int vfio_ap_mdev_get_device_info (unsigned long arg )
@@ -1351,25 +1353,20 @@ static int vfio_ap_mdev_get_device_info(unsigned long arg)
13511353 return copy_to_user ((void __user * )arg , & info , minsz ) ? - EFAULT : 0 ;
13521354}
13531355
1354- static ssize_t vfio_ap_mdev_ioctl (struct mdev_device * mdev ,
1356+ static ssize_t vfio_ap_mdev_ioctl (struct vfio_device * vdev ,
13551357 unsigned int cmd , unsigned long arg )
13561358{
1359+ struct ap_matrix_mdev * matrix_mdev =
1360+ container_of (vdev , struct ap_matrix_mdev , vdev );
13571361 int ret ;
1358- struct ap_matrix_mdev * matrix_mdev ;
13591362
13601363 mutex_lock (& matrix_dev -> lock );
13611364 switch (cmd ) {
13621365 case VFIO_DEVICE_GET_INFO :
13631366 ret = vfio_ap_mdev_get_device_info (arg );
13641367 break ;
13651368 case VFIO_DEVICE_RESET :
1366- matrix_mdev = mdev_get_drvdata (mdev );
1367- if (WARN (!matrix_mdev , "Driver data missing from mdev!!" )) {
1368- ret = - EINVAL ;
1369- break ;
1370- }
1371-
1372- ret = vfio_ap_mdev_reset_queues (mdev );
1369+ ret = vfio_ap_mdev_reset_queues (matrix_mdev );
13731370 break ;
13741371 default :
13751372 ret = - EOPNOTSUPP ;
@@ -1380,25 +1377,51 @@ static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev,
13801377 return ret ;
13811378}
13821379
1380+ static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1381+ .open_device = vfio_ap_mdev_open_device ,
1382+ .close_device = vfio_ap_mdev_close_device ,
1383+ .ioctl = vfio_ap_mdev_ioctl ,
1384+ };
1385+
1386+ static struct mdev_driver vfio_ap_matrix_driver = {
1387+ .driver = {
1388+ .name = "vfio_ap_mdev" ,
1389+ .owner = THIS_MODULE ,
1390+ .mod_name = KBUILD_MODNAME ,
1391+ .dev_groups = vfio_ap_mdev_attr_groups ,
1392+ },
1393+ .probe = vfio_ap_mdev_probe ,
1394+ .remove = vfio_ap_mdev_remove ,
1395+ };
1396+
13831397static const struct mdev_parent_ops vfio_ap_matrix_ops = {
13841398 .owner = THIS_MODULE ,
1399+ .device_driver = & vfio_ap_matrix_driver ,
13851400 .supported_type_groups = vfio_ap_mdev_type_groups ,
1386- .mdev_attr_groups = vfio_ap_mdev_attr_groups ,
1387- .create = vfio_ap_mdev_create ,
1388- .remove = vfio_ap_mdev_remove ,
1389- .open_device = vfio_ap_mdev_open_device ,
1390- .close_device = vfio_ap_mdev_close_device ,
1391- .ioctl = vfio_ap_mdev_ioctl ,
13921401};
13931402
13941403int vfio_ap_mdev_register (void )
13951404{
1405+ int ret ;
1406+
13961407 atomic_set (& matrix_dev -> available_instances , MAX_ZDEV_ENTRIES_EXT );
13971408
1398- return mdev_register_device (& matrix_dev -> device , & vfio_ap_matrix_ops );
1409+ ret = mdev_register_driver (& vfio_ap_matrix_driver );
1410+ if (ret )
1411+ return ret ;
1412+
1413+ ret = mdev_register_device (& matrix_dev -> device , & vfio_ap_matrix_ops );
1414+ if (ret )
1415+ goto err_driver ;
1416+ return 0 ;
1417+
1418+ err_driver :
1419+ mdev_unregister_driver (& vfio_ap_matrix_driver );
1420+ return ret ;
13991421}
14001422
14011423void vfio_ap_mdev_unregister (void )
14021424{
14031425 mdev_unregister_device (& matrix_dev -> device );
1426+ mdev_unregister_driver (& vfio_ap_matrix_driver );
14041427}
0 commit comments