162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  IBM System z PNET ID Support
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *    Copyright IBM Corp. 2018
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/device.h>
962306a36Sopenharmony_ci#include <linux/module.h>
1062306a36Sopenharmony_ci#include <linux/pci.h>
1162306a36Sopenharmony_ci#include <linux/types.h>
1262306a36Sopenharmony_ci#include <asm/ccwgroup.h>
1362306a36Sopenharmony_ci#include <asm/ccwdev.h>
1462306a36Sopenharmony_ci#include <asm/pnet.h>
1562306a36Sopenharmony_ci#include <asm/ebcdic.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define PNETIDS_LEN		64	/* Total utility string length in bytes
1862306a36Sopenharmony_ci					 * to cover up to 4 PNETIDs of 16 bytes
1962306a36Sopenharmony_ci					 * for up to 4 device ports
2062306a36Sopenharmony_ci					 */
2162306a36Sopenharmony_ci#define MAX_PNETID_LEN		16	/* Max.length of a single port PNETID */
2262306a36Sopenharmony_ci#define MAX_PNETID_PORTS	(PNETIDS_LEN / MAX_PNETID_LEN)
2362306a36Sopenharmony_ci					/* Max. # of ports with a PNETID */
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * Get the PNETIDs from a device.
2762306a36Sopenharmony_ci * s390 hardware supports the definition of a so-called Physical Network
2862306a36Sopenharmony_ci * Identifier (short PNETID) per network device port. These PNETIDs can be
2962306a36Sopenharmony_ci * used to identify network devices that are attached to the same physical
3062306a36Sopenharmony_ci * network (broadcast domain).
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * The device can be
3362306a36Sopenharmony_ci * - a ccwgroup device with all bundled subchannels having the same PNETID
3462306a36Sopenharmony_ci * - a PCI attached network device
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * Returns:
3762306a36Sopenharmony_ci * 0:		PNETIDs extracted from device.
3862306a36Sopenharmony_ci * -ENOMEM:	No memory to extract utility string.
3962306a36Sopenharmony_ci * -EOPNOTSUPP: Device type without utility string support
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_cistatic int pnet_ids_by_device(struct device *dev, u8 *pnetids)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	memset(pnetids, 0, PNETIDS_LEN);
4462306a36Sopenharmony_ci	if (dev_is_ccwgroup(dev)) {
4562306a36Sopenharmony_ci		struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
4662306a36Sopenharmony_ci		u8 *util_str;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci		util_str = ccw_device_get_util_str(gdev->cdev[0], 0);
4962306a36Sopenharmony_ci		if (!util_str)
5062306a36Sopenharmony_ci			return -ENOMEM;
5162306a36Sopenharmony_ci		memcpy(pnetids, util_str, PNETIDS_LEN);
5262306a36Sopenharmony_ci		EBCASC(pnetids, PNETIDS_LEN);
5362306a36Sopenharmony_ci		kfree(util_str);
5462306a36Sopenharmony_ci		return 0;
5562306a36Sopenharmony_ci	}
5662306a36Sopenharmony_ci	if (dev_is_pci(dev)) {
5762306a36Sopenharmony_ci		struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci		memcpy(pnetids, zdev->util_str, sizeof(zdev->util_str));
6062306a36Sopenharmony_ci		EBCASC(pnetids, sizeof(zdev->util_str));
6162306a36Sopenharmony_ci		return 0;
6262306a36Sopenharmony_ci	}
6362306a36Sopenharmony_ci	return -EOPNOTSUPP;
6462306a36Sopenharmony_ci}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/*
6762306a36Sopenharmony_ci * Extract the pnetid for a device port.
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * Return 0 if a pnetid is found and -ENOENT otherwise.
7062306a36Sopenharmony_ci */
7162306a36Sopenharmony_ciint pnet_id_by_dev_port(struct device *dev, unsigned short port, u8 *pnetid)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	u8 pnetids[MAX_PNETID_PORTS][MAX_PNETID_LEN];
7462306a36Sopenharmony_ci	static const u8 zero[MAX_PNETID_LEN] = { 0 };
7562306a36Sopenharmony_ci	int rc = 0;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	if (!dev || port >= MAX_PNETID_PORTS)
7862306a36Sopenharmony_ci		return -ENOENT;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	if (!pnet_ids_by_device(dev, (u8 *)pnetids) &&
8162306a36Sopenharmony_ci	    memcmp(pnetids[port], zero, MAX_PNETID_LEN))
8262306a36Sopenharmony_ci		memcpy(pnetid, pnetids[port], MAX_PNETID_LEN);
8362306a36Sopenharmony_ci	else
8462306a36Sopenharmony_ci		rc = -ENOENT;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	return rc;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(pnet_id_by_dev_port);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciMODULE_DESCRIPTION("pnetid determination from utility strings");
9162306a36Sopenharmony_ciMODULE_LICENSE("GPL");
92