1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * PCI Hot Plug Controller Driver for System z 4 * 5 * Copyright 2012 IBM Corp. 6 * 7 * Author(s): 8 * Jan Glauber <jang@linux.vnet.ibm.com> 9 */ 10 11 #define pr_fmt(fmt) "zpci: " fmt 12 13 #include <linux/kernel.h> 14 #include <linux/slab.h> 15 #include <linux/pci.h> 16 #include <linux/pci_hotplug.h> 17 #include <asm/pci_debug.h> 18 #include <asm/sclp.h> 19 20 #define SLOT_NAME_SIZE 10 21 22 static int enable_slot(struct hotplug_slot *hotplug_slot) 23 { 24 struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev, 25 hotplug_slot); 26 int rc; 27 28 mutex_lock(&zdev->state_lock); 29 if (zdev->state != ZPCI_FN_STATE_STANDBY) { 30 rc = -EIO; 31 goto out; 32 } 33 34 rc = sclp_pci_configure(zdev->fid); 35 zpci_dbg(3, "conf fid:%x, rc:%d\n", zdev->fid, rc); 36 if (rc) 37 goto out; 38 zdev->state = ZPCI_FN_STATE_CONFIGURED; 39 40 rc = zpci_scan_configured_device(zdev, zdev->fh); 41 out: 42 mutex_unlock(&zdev->state_lock); 43 return rc; 44 } 45 46 static int disable_slot(struct hotplug_slot *hotplug_slot) 47 { 48 struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev, 49 hotplug_slot); 50 struct pci_dev *pdev = NULL; 51 int rc; 52 53 mutex_lock(&zdev->state_lock); 54 if (zdev->state != ZPCI_FN_STATE_CONFIGURED) { 55 rc = -EIO; 56 goto out; 57 } 58 59 pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn); 60 if (pdev && pci_num_vf(pdev)) { 61 rc = -EBUSY; 62 goto out; 63 } 64 65 rc = zpci_deconfigure_device(zdev); 66 out: 67 if (pdev) 68 pci_dev_put(pdev); 69 mutex_unlock(&zdev->state_lock); 70 return rc; 71 } 72 73 static int reset_slot(struct hotplug_slot *hotplug_slot, bool probe) 74 { 75 struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev, 76 hotplug_slot); 77 int rc = -EIO; 78 79 /* 80 * If we can't get the zdev->state_lock the device state is 81 * currently undergoing a transition and we bail out - just 82 * the same as if the device's state is not configured at all. 83 */ 84 if (!mutex_trylock(&zdev->state_lock)) 85 return rc; 86 87 /* We can reset only if the function is configured */ 88 if (zdev->state != ZPCI_FN_STATE_CONFIGURED) 89 goto out; 90 91 if (probe) { 92 rc = 0; 93 goto out; 94 } 95 96 rc = zpci_hot_reset_device(zdev); 97 out: 98 mutex_unlock(&zdev->state_lock); 99 return rc; 100 } 101 102 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value) 103 { 104 struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev, 105 hotplug_slot); 106 107 *value = zpci_is_device_configured(zdev) ? 1 : 0; 108 return 0; 109 } 110 111 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value) 112 { 113 /* if the slot exists it always contains a function */ 114 *value = 1; 115 return 0; 116 } 117 118 static const struct hotplug_slot_ops s390_hotplug_slot_ops = { 119 .enable_slot = enable_slot, 120 .disable_slot = disable_slot, 121 .reset_slot = reset_slot, 122 .get_power_status = get_power_status, 123 .get_adapter_status = get_adapter_status, 124 }; 125 126 int zpci_init_slot(struct zpci_dev *zdev) 127 { 128 char name[SLOT_NAME_SIZE]; 129 struct zpci_bus *zbus = zdev->zbus; 130 131 zdev->hotplug_slot.ops = &s390_hotplug_slot_ops; 132 133 snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid); 134 return pci_hp_register(&zdev->hotplug_slot, zbus->bus, 135 zdev->devfn, name); 136 } 137 138 void zpci_exit_slot(struct zpci_dev *zdev) 139 { 140 pci_hp_deregister(&zdev->hotplug_slot); 141 } 142