xref: /openbmc/linux/arch/s390/net/pnet.c (revision a2818ee4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  IBM System z PNET ID Support
4  *
5  *    Copyright IBM Corp. 2018
6  */
7 
8 #include <linux/device.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/types.h>
12 #include <asm/ccwgroup.h>
13 #include <asm/ccwdev.h>
14 #include <asm/pnet.h>
15 
16 /*
17  * Get the PNETIDs from a device.
18  * s390 hardware supports the definition of a so-called Physical Network
19  * Identifier (short PNETID) per network device port. These PNETIDs can be
20  * used to identify network devices that are attached to the same physical
21  * network (broadcast domain).
22  *
23  * The device can be
24  * - a ccwgroup device with all bundled subchannels having the same PNETID
25  * - a PCI attached network device
26  *
27  * Returns:
28  * 0:		PNETIDs extracted from device.
29  * -ENOMEM:	No memory to extract utility string.
30  * -EOPNOTSUPP: Device type without utility string support
31  */
32 static int pnet_ids_by_device(struct device *dev, u8 *pnetids)
33 {
34 	memset(pnetids, 0, PNETIDS_LEN);
35 	if (dev_is_ccwgroup(dev)) {
36 		struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
37 		u8 *util_str;
38 
39 		util_str = ccw_device_get_util_str(gdev->cdev[0], 0);
40 		if (!util_str)
41 			return -ENOMEM;
42 		memcpy(pnetids, util_str, PNETIDS_LEN);
43 		kfree(util_str);
44 		return 0;
45 	}
46 	if (dev_is_pci(dev)) {
47 		struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
48 
49 		memcpy(pnetids, zdev->util_str, sizeof(zdev->util_str));
50 		return 0;
51 	}
52 	return -EOPNOTSUPP;
53 }
54 
55 /*
56  * Extract the pnetid for a device port.
57  *
58  * Return 0 if a pnetid is found and -ENOENT otherwise.
59  */
60 int pnet_id_by_dev_port(struct device *dev, unsigned short port, u8 *pnetid)
61 {
62 	u8 pnetids[MAX_PNETID_PORTS][MAX_PNETID_LEN];
63 	static const u8 zero[MAX_PNETID_LEN] = { 0 };
64 	int rc = 0;
65 
66 	if (!dev || port >= MAX_PNETID_PORTS)
67 		return -ENOENT;
68 
69 	if (!pnet_ids_by_device(dev, (u8 *)pnetids) &&
70 	    memcmp(pnetids[port], zero, MAX_PNETID_LEN))
71 		memcpy(pnetid, pnetids[port], MAX_PNETID_LEN);
72 	else
73 		rc = -ENOENT;
74 
75 	return rc;
76 }
77 EXPORT_SYMBOL_GPL(pnet_id_by_dev_port);
78 
79 MODULE_DESCRIPTION("pnetid determination from utility strings");
80 MODULE_LICENSE("GPL");
81