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