hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/samples/vfio-mdev/mtty.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Mediated virtual PCI serial host device driver
34 *
....@@ -5,13 +6,8 @@
56 * Author: Neo Jia <cjia@nvidia.com>
67 * Kirti Wankhede <kwankhede@nvidia.com>
78 *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License version 2 as
10
- * published by the Free Software Foundation.
11
- *
129 * Sample driver that creates mdev device that simulates serial port over PCI
1310 * card.
14
- *
1511 */
1612
1713 #include <linux/init.h>
....@@ -72,7 +68,7 @@
7268 * Global Structures
7369 */
7470
75
-struct mtty_dev {
71
+static struct mtty_dev {
7672 dev_t vd_devt;
7773 struct class *vd_class;
7874 struct cdev vd_cdev;
....@@ -88,7 +84,7 @@
8884 };
8985
9086 #if defined(DEBUG_REGS)
91
-const char *wr_reg[] = {
87
+static const char *wr_reg[] = {
9288 "TX",
9389 "IER",
9490 "FCR",
....@@ -99,7 +95,7 @@
9995 "SCR"
10096 };
10197
102
-const char *rd_reg[] = {
98
+static const char *rd_reg[] = {
10399 "RX",
104100 "IER",
105101 "IIR",
....@@ -147,8 +143,8 @@
147143 int nr_ports;
148144 };
149145
150
-struct mutex mdev_list_lock;
151
-struct list_head mdev_devices_list;
146
+static struct mutex mdev_list_lock;
147
+static struct list_head mdev_devices_list;
152148
153149 static const struct file_operations vd_fops = {
154150 .owner = THIS_MODULE,
....@@ -156,22 +152,11 @@
156152
157153 /* function prototypes */
158154
159
-static int mtty_trigger_interrupt(uuid_le uuid);
155
+static int mtty_trigger_interrupt(struct mdev_state *mdev_state);
160156
161157 /* Helper functions */
162
-static struct mdev_state *find_mdev_state_by_uuid(uuid_le uuid)
163
-{
164
- struct mdev_state *mds;
165158
166
- list_for_each_entry(mds, &mdev_devices_list, next) {
167
- if (uuid_le_cmp(mdev_uuid(mds->mdev), uuid) == 0)
168
- return mds;
169
- }
170
-
171
- return NULL;
172
-}
173
-
174
-void dump_buffer(u8 *buf, uint32_t count)
159
+static void dump_buffer(u8 *buf, uint32_t count)
175160 {
176161 #if defined(DEBUG)
177162 int i;
....@@ -341,8 +326,7 @@
341326 pr_err("Serial port %d: Fifo level trigger\n",
342327 index);
343328 #endif
344
- mtty_trigger_interrupt(
345
- mdev_uuid(mdev_state->mdev));
329
+ mtty_trigger_interrupt(mdev_state);
346330 }
347331 } else {
348332 #if defined(DEBUG_INTR)
....@@ -356,8 +340,7 @@
356340 */
357341 if (mdev_state->s[index].uart_reg[UART_IER] &
358342 UART_IER_RLSI)
359
- mtty_trigger_interrupt(
360
- mdev_uuid(mdev_state->mdev));
343
+ mtty_trigger_interrupt(mdev_state);
361344 }
362345 mutex_unlock(&mdev_state->rxtx_lock);
363346 break;
....@@ -376,8 +359,7 @@
376359 pr_err("Serial port %d: IER_THRI write\n",
377360 index);
378361 #endif
379
- mtty_trigger_interrupt(
380
- mdev_uuid(mdev_state->mdev));
362
+ mtty_trigger_interrupt(mdev_state);
381363 }
382364
383365 mutex_unlock(&mdev_state->rxtx_lock);
....@@ -448,7 +430,7 @@
448430 #if defined(DEBUG_INTR)
449431 pr_err("Serial port %d: MCR_OUT2 write\n", index);
450432 #endif
451
- mtty_trigger_interrupt(mdev_uuid(mdev_state->mdev));
433
+ mtty_trigger_interrupt(mdev_state);
452434 }
453435
454436 if ((mdev_state->s[index].uart_reg[UART_IER] & UART_IER_MSI) &&
....@@ -456,7 +438,7 @@
456438 #if defined(DEBUG_INTR)
457439 pr_err("Serial port %d: MCR RTS/DTR write\n", index);
458440 #endif
459
- mtty_trigger_interrupt(mdev_uuid(mdev_state->mdev));
441
+ mtty_trigger_interrupt(mdev_state);
460442 }
461443 break;
462444
....@@ -507,8 +489,7 @@
507489 #endif
508490 if (mdev_state->s[index].uart_reg[UART_IER] &
509491 UART_IER_THRI)
510
- mtty_trigger_interrupt(
511
- mdev_uuid(mdev_state->mdev));
492
+ mtty_trigger_interrupt(mdev_state);
512493 }
513494 mutex_unlock(&mdev_state->rxtx_lock);
514495
....@@ -727,7 +708,7 @@
727708 return ret;
728709 }
729710
730
-int mtty_create(struct kobject *kobj, struct mdev_device *mdev)
711
+static int mtty_create(struct kobject *kobj, struct mdev_device *mdev)
731712 {
732713 struct mdev_state *mdev_state;
733714 char name[MTTY_STRING_LEN];
....@@ -777,7 +758,7 @@
777758 return 0;
778759 }
779760
780
-int mtty_remove(struct mdev_device *mdev)
761
+static int mtty_remove(struct mdev_device *mdev)
781762 {
782763 struct mdev_state *mds, *tmp_mds;
783764 struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
....@@ -799,7 +780,7 @@
799780 return ret;
800781 }
801782
802
-int mtty_reset(struct mdev_device *mdev)
783
+static int mtty_reset(struct mdev_device *mdev)
803784 {
804785 struct mdev_state *mdev_state;
805786
....@@ -815,8 +796,8 @@
815796 return 0;
816797 }
817798
818
-ssize_t mtty_read(struct mdev_device *mdev, char __user *buf, size_t count,
819
- loff_t *ppos)
799
+static ssize_t mtty_read(struct mdev_device *mdev, char __user *buf,
800
+ size_t count, loff_t *ppos)
820801 {
821802 unsigned int done = 0;
822803 int ret;
....@@ -874,7 +855,7 @@
874855 return -EFAULT;
875856 }
876857
877
-ssize_t mtty_write(struct mdev_device *mdev, const char __user *buf,
858
+static ssize_t mtty_write(struct mdev_device *mdev, const char __user *buf,
878859 size_t count, loff_t *ppos)
879860 {
880861 unsigned int done = 0;
....@@ -1032,17 +1013,9 @@
10321013 return ret;
10331014 }
10341015
1035
-static int mtty_trigger_interrupt(uuid_le uuid)
1016
+static int mtty_trigger_interrupt(struct mdev_state *mdev_state)
10361017 {
10371018 int ret = -1;
1038
- struct mdev_state *mdev_state;
1039
-
1040
- mdev_state = find_mdev_state_by_uuid(uuid);
1041
-
1042
- if (!mdev_state) {
1043
- pr_info("%s: mdev not found\n", __func__);
1044
- return -EINVAL;
1045
- }
10461019
10471020 if ((mdev_state->irq_index == VFIO_PCI_MSI_IRQ_INDEX) &&
10481021 (!mdev_state->msi_evtfd))
....@@ -1067,7 +1040,7 @@
10671040 return ret;
10681041 }
10691042
1070
-int mtty_get_region_info(struct mdev_device *mdev,
1043
+static int mtty_get_region_info(struct mdev_device *mdev,
10711044 struct vfio_region_info *region_info,
10721045 u16 *cap_type_id, void **cap_type)
10731046 {
....@@ -1116,7 +1089,8 @@
11161089 return 0;
11171090 }
11181091
1119
-int mtty_get_irq_info(struct mdev_device *mdev, struct vfio_irq_info *irq_info)
1092
+static int mtty_get_irq_info(struct mdev_device *mdev,
1093
+ struct vfio_irq_info *irq_info)
11201094 {
11211095 switch (irq_info->index) {
11221096 case VFIO_PCI_INTX_IRQ_INDEX:
....@@ -1140,7 +1114,7 @@
11401114 return 0;
11411115 }
11421116
1143
-int mtty_get_device_info(struct mdev_device *mdev,
1117
+static int mtty_get_device_info(struct mdev_device *mdev,
11441118 struct vfio_device_info *dev_info)
11451119 {
11461120 dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
....@@ -1272,13 +1246,13 @@
12721246 return -ENOTTY;
12731247 }
12741248
1275
-int mtty_open(struct mdev_device *mdev)
1249
+static int mtty_open(struct mdev_device *mdev)
12761250 {
12771251 pr_info("%s\n", __func__);
12781252 return 0;
12791253 }
12801254
1281
-void mtty_close(struct mdev_device *mdev)
1255
+static void mtty_close(struct mdev_device *mdev)
12821256 {
12831257 pr_info("%s\n", __func__);
12841258 }
....@@ -1302,7 +1276,7 @@
13021276 .attrs = mtty_dev_attrs,
13031277 };
13041278
1305
-const struct attribute_group *mtty_dev_groups[] = {
1279
+static const struct attribute_group *mtty_dev_groups[] = {
13061280 &mtty_dev_group,
13071281 NULL,
13081282 };
....@@ -1329,7 +1303,7 @@
13291303 .attrs = mdev_dev_attrs,
13301304 };
13311305
1332
-const struct attribute_group *mdev_dev_groups[] = {
1306
+static const struct attribute_group *mdev_dev_groups[] = {
13331307 &mdev_dev_group,
13341308 NULL,
13351309 };
....@@ -1351,7 +1325,7 @@
13511325 return -EINVAL;
13521326 }
13531327
1354
-MDEV_TYPE_ATTR_RO(name);
1328
+static MDEV_TYPE_ATTR_RO(name);
13551329
13561330 static ssize_t
13571331 available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
....@@ -1379,7 +1353,7 @@
13791353 return sprintf(buf, "%d\n", (MAX_MTTYS - used)/ports);
13801354 }
13811355
1382
-MDEV_TYPE_ATTR_RO(available_instances);
1356
+static MDEV_TYPE_ATTR_RO(available_instances);
13831357
13841358
13851359 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
....@@ -1388,7 +1362,7 @@
13881362 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
13891363 }
13901364
1391
-MDEV_TYPE_ATTR_RO(device_api);
1365
+static MDEV_TYPE_ATTR_RO(device_api);
13921366
13931367 static struct attribute *mdev_types_attrs[] = {
13941368 &mdev_type_attr_name.attr,
....@@ -1407,7 +1381,7 @@
14071381 .attrs = mdev_types_attrs,
14081382 };
14091383
1410
-struct attribute_group *mdev_type_groups[] = {
1384
+static struct attribute_group *mdev_type_groups[] = {
14111385 &mdev_type_group1,
14121386 &mdev_type_group2,
14131387 NULL,
....@@ -1442,7 +1416,8 @@
14421416
14431417 idr_init(&mtty_dev.vd_idr);
14441418
1445
- ret = alloc_chrdev_region(&mtty_dev.vd_devt, 0, MINORMASK, MTTY_NAME);
1419
+ ret = alloc_chrdev_region(&mtty_dev.vd_devt, 0, MINORMASK + 1,
1420
+ MTTY_NAME);
14461421
14471422 if (ret < 0) {
14481423 pr_err("Error: failed to register mtty_dev, err:%d\n", ret);
....@@ -1450,7 +1425,7 @@
14501425 }
14511426
14521427 cdev_init(&mtty_dev.vd_cdev, &vd_fops);
1453
- cdev_add(&mtty_dev.vd_cdev, mtty_dev.vd_devt, MINORMASK);
1428
+ cdev_add(&mtty_dev.vd_cdev, mtty_dev.vd_devt, MINORMASK + 1);
14541429
14551430 pr_info("major_number:%d\n", MAJOR(mtty_dev.vd_devt));
14561431
....@@ -1487,7 +1462,7 @@
14871462
14881463 failed1:
14891464 cdev_del(&mtty_dev.vd_cdev);
1490
- unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK);
1465
+ unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1);
14911466
14921467 all_done:
14931468 return ret;
....@@ -1501,7 +1476,7 @@
15011476 device_unregister(&mtty_dev.dev);
15021477 idr_destroy(&mtty_dev.vd_idr);
15031478 cdev_del(&mtty_dev.vd_cdev);
1504
- unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK);
1479
+ unregister_chrdev_region(mtty_dev.vd_devt, MINORMASK + 1);
15051480 class_destroy(mtty_dev.vd_class);
15061481 mtty_dev.vd_class = NULL;
15071482 pr_info("mtty_dev: Unloaded!\n");