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 #define PNETIDS_LEN 64 /* Total utility string length in bytes 17 * to cover up to 4 PNETIDs of 16 bytes 18 * for up to 4 device ports 19 */ 20 #define MAX_PNETID_LEN 16 /* Max.length of a single port PNETID */ 21 #define MAX_PNETID_PORTS (PNETIDS_LEN / MAX_PNETID_LEN) 22 /* Max. # of ports with a PNETID */ 23 24 /* 25 * Get the PNETIDs from a device. 26 * s390 hardware supports the definition of a so-called Physical Network 27 * Identifier (short PNETID) per network device port. These PNETIDs can be 28 * used to identify network devices that are attached to the same physical 29 * network (broadcast domain). 30 * 31 * The device can be 32 * - a ccwgroup device with all bundled subchannels having the same PNETID 33 * - a PCI attached network device 34 * 35 * Returns: 36 * 0: PNETIDs extracted from device. 37 * -ENOMEM: No memory to extract utility string. 38 * -EOPNOTSUPP: Device type without utility string support 39 */ 40 static int pnet_ids_by_device(struct device *dev, u8 *pnetids) 41 { 42 memset(pnetids, 0, PNETIDS_LEN); 43 if (dev_is_ccwgroup(dev)) { 44 struct ccwgroup_device *gdev = to_ccwgroupdev(dev); 45 u8 *util_str; 46 47 util_str = ccw_device_get_util_str(gdev->cdev[0], 0); 48 if (!util_str) 49 return -ENOMEM; 50 memcpy(pnetids, util_str, PNETIDS_LEN); 51 kfree(util_str); 52 return 0; 53 } 54 if (dev_is_pci(dev)) { 55 struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); 56 57 memcpy(pnetids, zdev->util_str, sizeof(zdev->util_str)); 58 return 0; 59 } 60 return -EOPNOTSUPP; 61 } 62 63 /* 64 * Extract the pnetid for a device port. 65 * 66 * Return 0 if a pnetid is found and -ENOENT otherwise. 67 */ 68 int pnet_id_by_dev_port(struct device *dev, unsigned short port, u8 *pnetid) 69 { 70 u8 pnetids[MAX_PNETID_PORTS][MAX_PNETID_LEN]; 71 static const u8 zero[MAX_PNETID_LEN] = { 0 }; 72 int rc = 0; 73 74 if (!dev || port >= MAX_PNETID_PORTS) 75 return -ENOENT; 76 77 if (!pnet_ids_by_device(dev, (u8 *)pnetids) && 78 memcmp(pnetids[port], zero, MAX_PNETID_LEN)) 79 memcpy(pnetid, pnetids[port], MAX_PNETID_LEN); 80 else 81 rc = -ENOENT; 82 83 return rc; 84 } 85 EXPORT_SYMBOL_GPL(pnet_id_by_dev_port); 86 87 MODULE_DESCRIPTION("pnetid determination from utility strings"); 88 MODULE_LICENSE("GPL"); 89