summaryrefslogtreecommitdiff
path: root/arch/s390/net/pnet.c
blob: e22f1b10a6c7bea046cc88502313a9d63893a95e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-License-Identifier: GPL-2.0
/*
 *  IBM System z PNET ID Support
 *
 *    Copyright IBM Corp. 2018
 */

#include <linux/device.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/types.h>
#include <asm/ccwgroup.h>
#include <asm/ccwdev.h>
#include <asm/pnet.h>

/*
 * Get the PNETIDs from a device.
 * s390 hardware supports the definition of a so-called Physical Network
 * Identifier (short PNETID) per network device port. These PNETIDs can be
 * used to identify network devices that are attached to the same physical
 * network (broadcast domain).
 *
 * The device can be
 * - a ccwgroup device with all bundled subchannels having the same PNETID
 * - a PCI attached network device
 *
 * Returns:
 * 0:		PNETIDs extracted from device.
 * -ENOMEM:	No memory to extract utility string.
 * -EOPNOTSUPP: Device type without utility string support
 */
static int pnet_ids_by_device(struct device *dev, u8 *pnetids)
{
	memset(pnetids, 0, PNETIDS_LEN);
	if (dev_is_ccwgroup(dev)) {
		struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
		u8 *util_str;

		util_str = ccw_device_get_util_str(gdev->cdev[0], 0);
		if (!util_str)
			return -ENOMEM;
		memcpy(pnetids, util_str, PNETIDS_LEN);
		kfree(util_str);
		return 0;
	}
	if (dev_is_pci(dev)) {
		struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));

		memcpy(pnetids, zdev->util_str, sizeof(zdev->util_str));
		return 0;
	}
	return -EOPNOTSUPP;
}

/*
 * Extract the pnetid for a device port.
 *
 * Return 0 if a pnetid is found and -ENOENT otherwise.
 */
int pnet_id_by_dev_port(struct device *dev, unsigned short port, u8 *pnetid)
{
	u8 pnetids[MAX_PNETID_PORTS][MAX_PNETID_LEN];
	static const u8 zero[MAX_PNETID_LEN] = { 0 };
	int rc = 0;

	if (!dev || port >= MAX_PNETID_PORTS)
		return -ENOENT;

	if (!pnet_ids_by_device(dev, (u8 *)pnetids) &&
	    memcmp(pnetids[port], zero, MAX_PNETID_LEN))
		memcpy(pnetid, pnetids[port], MAX_PNETID_LEN);
	else
		rc = -ENOENT;

	return rc;
}
EXPORT_SYMBOL_GPL(pnet_id_by_dev_port);

MODULE_DESCRIPTION("pnetid determination from utility strings");
MODULE_LICENSE("GPL");