Bjorn Helgaas | 736759e | 2018-01-26 14:22:04 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Standard Hot Plug Controller Driver |
| 4 | * |
| 5 | * Copyright (C) 1995,2001 Compaq Computer Corporation |
| 6 | * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com) |
| 7 | * Copyright (C) 2001 IBM Corp. |
| 8 | * Copyright (C) 2003-2004 Intel Corporation |
| 9 | * |
| 10 | * All rights reserved. |
| 11 | * |
Kristen Accardi | 8cf4c19 | 2005-08-16 15:16:10 -0700 | [diff] [blame] | 12 | * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
| 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "shpchp.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /* Global variables */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 25 | bool shpchp_debug; |
| 26 | bool shpchp_poll_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | int shpchp_poll_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #define DRIVER_VERSION "0.4" |
| 30 | #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>" |
| 31 | #define DRIVER_DESC "Standard Hot Plug PCI Controller Driver" |
| 32 | |
| 33 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 34 | MODULE_DESCRIPTION(DRIVER_DESC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | module_param(shpchp_debug, bool, 0644); |
| 37 | module_param(shpchp_poll_mode, bool, 0644); |
| 38 | module_param(shpchp_poll_time, int, 0644); |
| 39 | MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not"); |
| 40 | MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not"); |
| 41 | MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds"); |
| 42 | |
| 43 | #define SHPC_MODULE_NAME "shpchp" |
| 44 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 45 | static int set_attention_status(struct hotplug_slot *slot, u8 value); |
| 46 | static int enable_slot(struct hotplug_slot *slot); |
| 47 | static int disable_slot(struct hotplug_slot *slot); |
| 48 | static int get_power_status(struct hotplug_slot *slot, u8 *value); |
| 49 | static int get_attention_status(struct hotplug_slot *slot, u8 *value); |
| 50 | static int get_latch_status(struct hotplug_slot *slot, u8 *value); |
| 51 | static int get_adapter_status(struct hotplug_slot *slot, u8 *value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Lukas Wunner | 81c4b5b | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 53 | static const struct hotplug_slot_ops shpchp_hotplug_slot_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | .set_attention_status = set_attention_status, |
| 55 | .enable_slot = enable_slot, |
| 56 | .disable_slot = disable_slot, |
| 57 | .get_power_status = get_power_status, |
| 58 | .get_attention_status = get_attention_status, |
| 59 | .get_latch_status = get_latch_status, |
| 60 | .get_adapter_status = get_adapter_status, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static int init_slots(struct controller *ctrl) |
| 64 | { |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 65 | struct slot *slot; |
| 66 | struct hotplug_slot *hotplug_slot; |
Alex Chiang | 66f1705 | 2008-10-20 17:41:53 -0600 | [diff] [blame] | 67 | char name[SLOT_NAME_SIZE]; |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame] | 68 | int retval; |
Alex Chiang | 5fe6cc6 | 2008-10-20 17:41:02 -0600 | [diff] [blame] | 69 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 71 | for (i = 0; i < ctrl->num_slots; i++) { |
Kenji Kaneshige | 57c95c0 | 2006-01-26 10:02:41 +0900 | [diff] [blame] | 72 | slot = kzalloc(sizeof(*slot), GFP_KERNEL); |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame] | 73 | if (!slot) { |
| 74 | retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | goto error; |
Julia Lawall | 83d0571 | 2012-07-16 09:25:56 -0600 | [diff] [blame] | 76 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Lukas Wunner | 125450f | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 78 | hotplug_slot = &slot->hotplug_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 80 | slot->hp_slot = i; |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 81 | slot->ctrl = ctrl; |
Kenji Kaneshige | 227b84c | 2006-12-16 15:25:42 -0800 | [diff] [blame] | 82 | slot->bus = ctrl->pci_dev->subordinate->number; |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 83 | slot->device = ctrl->slot_device_offset + i; |
| 84 | slot->hpc_ops = ctrl->hpc_ops; |
Kenji Kaneshige | 6f39be2 | 2006-12-16 15:25:49 -0800 | [diff] [blame] | 85 | slot->number = ctrl->first_slot + (ctrl->slot_num_inc * i); |
Bjorn Helgaas | f652e7d | 2013-01-11 12:21:15 -0700 | [diff] [blame] | 86 | |
Kees Cook | d853754 | 2013-07-03 15:04:57 -0700 | [diff] [blame] | 87 | slot->wq = alloc_workqueue("shpchp-%d", 0, 0, slot->number); |
Bjorn Helgaas | f652e7d | 2013-01-11 12:21:15 -0700 | [diff] [blame] | 88 | if (!slot->wq) { |
| 89 | retval = -ENOMEM; |
Lukas Wunner | 125450f | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 90 | goto error_slot; |
Bjorn Helgaas | f652e7d | 2013-01-11 12:21:15 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Kenji Kaneshige | a246fa4 | 2006-02-21 15:45:48 -0800 | [diff] [blame] | 93 | mutex_init(&slot->lock); |
Kristen Carlson Accardi | e325e1f | 2007-03-21 11:45:31 -0700 | [diff] [blame] | 94 | INIT_DELAYED_WORK(&slot->work, shpchp_queue_pushbutton_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* register this slot with the hotplug pci core */ |
Alex Chiang | 66f1705 | 2008-10-20 17:41:53 -0600 | [diff] [blame] | 97 | snprintf(name, SLOT_NAME_SIZE, "%d", slot->number); |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 98 | hotplug_slot->ops = &shpchp_hotplug_slot_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Ryan Desfosses | 227f064 | 2014-04-18 20:13:50 -0400 | [diff] [blame] | 100 | ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x hp_slot=%x sun=%x slot_device_offset=%x\n", |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 101 | pci_domain_nr(ctrl->pci_dev->subordinate), |
| 102 | slot->bus, slot->device, slot->hp_slot, slot->number, |
| 103 | ctrl->slot_device_offset); |
Lukas Wunner | 125450f | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 104 | retval = pci_hp_register(hotplug_slot, |
Alex Chiang | 66f1705 | 2008-10-20 17:41:53 -0600 | [diff] [blame] | 105 | ctrl->pci_dev->subordinate, slot->device, name); |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 106 | if (retval) { |
Taku Izumi | f98ca31 | 2008-10-23 11:52:12 +0900 | [diff] [blame] | 107 | ctrl_err(ctrl, "pci_hp_register failed with error %d\n", |
| 108 | retval); |
Bjorn Helgaas | f652e7d | 2013-01-11 12:21:15 -0700 | [diff] [blame] | 109 | goto error_slotwq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Lukas Wunner | a7da216 | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 112 | get_power_status(hotplug_slot, &slot->pwr_save); |
| 113 | get_attention_status(hotplug_slot, &slot->attention_save); |
| 114 | get_latch_status(hotplug_slot, &slot->latch_save); |
| 115 | get_adapter_status(hotplug_slot, &slot->presence_save); |
Alex Chiang | 66f1705 | 2008-10-20 17:41:53 -0600 | [diff] [blame] | 116 | |
Kenji Kaneshige | 5b1a960 | 2006-01-26 09:57:40 +0900 | [diff] [blame] | 117 | list_add(&slot->slot_list, &ctrl->slot_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return 0; |
Bjorn Helgaas | f652e7d | 2013-01-11 12:21:15 -0700 | [diff] [blame] | 121 | error_slotwq: |
| 122 | destroy_workqueue(slot->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | error_slot: |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 124 | kfree(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | error: |
Kenji Kaneshige | 926030f | 2006-01-26 09:55:35 +0900 | [diff] [blame] | 126 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Kenji Kaneshige | f7391f5 | 2006-02-21 15:45:45 -0800 | [diff] [blame] | 129 | void cleanup_slots(struct controller *ctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Geliang Tang | 2ac83cc | 2015-12-12 21:36:57 +0800 | [diff] [blame] | 131 | struct slot *slot, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Geliang Tang | 2ac83cc | 2015-12-12 21:36:57 +0800 | [diff] [blame] | 133 | list_for_each_entry_safe(slot, next, &ctrl->slot_list, slot_list) { |
Kenji Kaneshige | 5b1a960 | 2006-01-26 09:57:40 +0900 | [diff] [blame] | 134 | list_del(&slot->slot_list); |
Kenji Kaneshige | f7391f5 | 2006-02-21 15:45:45 -0800 | [diff] [blame] | 135 | cancel_delayed_work(&slot->work); |
Bjorn Helgaas | f652e7d | 2013-01-11 12:21:15 -0700 | [diff] [blame] | 136 | destroy_workqueue(slot->wq); |
Lukas Wunner | 125450f | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 137 | pci_hp_deregister(&slot->hotplug_slot); |
Lukas Wunner | 51bbf9b | 2018-07-19 17:27:43 -0500 | [diff] [blame] | 138 | kfree(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* |
| 143 | * set_attention_status - Turns the Amber LED for a slot on, off or blink |
| 144 | */ |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 145 | static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
Kenji Kaneshige | 8352e04 | 2006-12-16 15:25:57 -0800 | [diff] [blame] | 147 | struct slot *slot = get_slot(hotplug_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 149 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
Taku Izumi | f98ca31 | 2008-10-23 11:52:12 +0900 | [diff] [blame] | 150 | __func__, slot_name(slot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Lukas Wunner | a7da216 | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 152 | slot->attention_save = status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | slot->hpc_ops->set_attention_status(slot, status); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 158 | static int enable_slot(struct hotplug_slot *hotplug_slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Kenji Kaneshige | 8352e04 | 2006-12-16 15:25:57 -0800 | [diff] [blame] | 160 | struct slot *slot = get_slot(hotplug_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 162 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
Taku Izumi | f98ca31 | 2008-10-23 11:52:12 +0900 | [diff] [blame] | 163 | __func__, slot_name(slot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Kenji Kaneshige | a246fa4 | 2006-02-21 15:45:48 -0800 | [diff] [blame] | 165 | return shpchp_sysfs_enable_slot(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 168 | static int disable_slot(struct hotplug_slot *hotplug_slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Kenji Kaneshige | 8352e04 | 2006-12-16 15:25:57 -0800 | [diff] [blame] | 170 | struct slot *slot = get_slot(hotplug_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 172 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
Taku Izumi | f98ca31 | 2008-10-23 11:52:12 +0900 | [diff] [blame] | 173 | __func__, slot_name(slot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Kenji Kaneshige | a246fa4 | 2006-02-21 15:45:48 -0800 | [diff] [blame] | 175 | return shpchp_sysfs_disable_slot(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 178 | static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Kenji Kaneshige | 8352e04 | 2006-12-16 15:25:57 -0800 | [diff] [blame] | 180 | struct slot *slot = get_slot(hotplug_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | int retval; |
| 182 | |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 183 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
Taku Izumi | f98ca31 | 2008-10-23 11:52:12 +0900 | [diff] [blame] | 184 | __func__, slot_name(slot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | retval = slot->hpc_ops->get_power_status(slot, value); |
| 187 | if (retval < 0) |
Lukas Wunner | a7da216 | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 188 | *value = slot->pwr_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 193 | static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Kenji Kaneshige | 8352e04 | 2006-12-16 15:25:57 -0800 | [diff] [blame] | 195 | struct slot *slot = get_slot(hotplug_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | int retval; |
| 197 | |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 198 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
Taku Izumi | f98ca31 | 2008-10-23 11:52:12 +0900 | [diff] [blame] | 199 | __func__, slot_name(slot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | retval = slot->hpc_ops->get_attention_status(slot, value); |
| 202 | if (retval < 0) |
Lukas Wunner | a7da216 | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 203 | *value = slot->attention_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 208 | static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Kenji Kaneshige | 8352e04 | 2006-12-16 15:25:57 -0800 | [diff] [blame] | 210 | struct slot *slot = get_slot(hotplug_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | int retval; |
| 212 | |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 213 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
Taku Izumi | f98ca31 | 2008-10-23 11:52:12 +0900 | [diff] [blame] | 214 | __func__, slot_name(slot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | retval = slot->hpc_ops->get_latch_status(slot, value); |
| 217 | if (retval < 0) |
Lukas Wunner | a7da216 | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 218 | *value = slot->latch_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 223 | static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
Kenji Kaneshige | 8352e04 | 2006-12-16 15:25:57 -0800 | [diff] [blame] | 225 | struct slot *slot = get_slot(hotplug_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | int retval; |
| 227 | |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 228 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
Taku Izumi | f98ca31 | 2008-10-23 11:52:12 +0900 | [diff] [blame] | 229 | __func__, slot_name(slot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | retval = slot->hpc_ops->get_adapter_status(slot, value); |
| 232 | if (retval < 0) |
Lukas Wunner | a7da216 | 2018-09-08 09:59:01 +0200 | [diff] [blame] | 233 | *value = slot->presence_save; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Bjorn Helgaas | b03799b | 2018-06-25 16:49:06 -0500 | [diff] [blame] | 238 | static bool shpc_capable(struct pci_dev *bridge) |
| 239 | { |
| 240 | /* |
| 241 | * It is assumed that AMD GOLAM chips support SHPC but they do not |
| 242 | * have SHPC capability. |
| 243 | */ |
| 244 | if (bridge->vendor == PCI_VENDOR_ID_AMD && |
| 245 | bridge->device == PCI_DEVICE_ID_AMD_GOLAM_7450) |
| 246 | return true; |
| 247 | |
| 248 | if (pci_find_capability(bridge, PCI_CAP_ID_SHPC)) |
| 249 | return true; |
| 250 | |
| 251 | return false; |
| 252 | } |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 255 | { |
| 256 | int rc; |
| 257 | struct controller *ctrl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Bjorn Helgaas | b03799b | 2018-06-25 16:49:06 -0500 | [diff] [blame] | 259 | if (!shpc_capable(pdev)) |
| 260 | return -ENODEV; |
| 261 | |
Mika Westerberg | 90cc0c3 | 2018-05-31 11:42:11 -0500 | [diff] [blame] | 262 | if (acpi_get_hp_hw_control_from_firmware(pdev)) |
rajesh.shah@intel.com | 1410dc1 | 2005-10-13 12:05:39 -0700 | [diff] [blame] | 263 | return -ENODEV; |
| 264 | |
Kenji Kaneshige | 57c95c0 | 2006-01-26 10:02:41 +0900 | [diff] [blame] | 265 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
Markus Elfring | c7abb23 | 2017-12-29 12:15:16 +0100 | [diff] [blame] | 266 | if (!ctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | goto err_out_none; |
Markus Elfring | c7abb23 | 2017-12-29 12:15:16 +0100 | [diff] [blame] | 268 | |
Kenji Kaneshige | 5b1a960 | 2006-01-26 09:57:40 +0900 | [diff] [blame] | 269 | INIT_LIST_HEAD(&ctrl->slot_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
rajesh.shah@intel.com | ee13833 | 2005-10-13 12:05:42 -0700 | [diff] [blame] | 271 | rc = shpc_init(ctrl, pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (rc) { |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 273 | ctrl_dbg(ctrl, "Controller initialization failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | goto err_out_free_ctrl; |
| 275 | } |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | pci_set_drvdata(pdev, ctrl); |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | /* Setup the slot information structures */ |
| 280 | rc = init_slots(ctrl); |
| 281 | if (rc) { |
Taku Izumi | be7bce2 | 2008-10-23 11:54:39 +0900 | [diff] [blame] | 282 | ctrl_err(ctrl, "Slot initialization failed\n"); |
Kenji Kaneshige | f7391f5 | 2006-02-21 15:45:45 -0800 | [diff] [blame] | 283 | goto err_out_release_ctlr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Greg Kroah-Hartman | e1b95dc | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 286 | rc = shpchp_create_ctrl_files(ctrl); |
| 287 | if (rc) |
| 288 | goto err_cleanup_slots; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Bjorn Helgaas | b03799b | 2018-06-25 16:49:06 -0500 | [diff] [blame] | 290 | pdev->shpc_managed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return 0; |
| 292 | |
Greg Kroah-Hartman | e1b95dc | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 293 | err_cleanup_slots: |
| 294 | cleanup_slots(ctrl); |
Kenji Kaneshige | f7391f5 | 2006-02-21 15:45:45 -0800 | [diff] [blame] | 295 | err_out_release_ctlr: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | ctrl->hpc_ops->release_ctlr(ctrl); |
| 297 | err_out_free_ctrl: |
| 298 | kfree(ctrl); |
| 299 | err_out_none: |
| 300 | return -ENODEV; |
| 301 | } |
| 302 | |
Kenji Kaneshige | e6b82b13 | 2006-02-21 15:45:50 -0800 | [diff] [blame] | 303 | static void shpc_remove(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Kenji Kaneshige | e6b82b13 | 2006-02-21 15:45:50 -0800 | [diff] [blame] | 305 | struct controller *ctrl = pci_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Bjorn Helgaas | b03799b | 2018-06-25 16:49:06 -0500 | [diff] [blame] | 307 | dev->shpc_managed = 0; |
Kenji Kaneshige | e6b82b13 | 2006-02-21 15:45:50 -0800 | [diff] [blame] | 308 | shpchp_remove_ctrl_files(ctrl); |
| 309 | ctrl->hpc_ops->release_ctlr(ctrl); |
| 310 | kfree(ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Arvind Yadav | 8394264 | 2017-08-03 18:20:17 -0500 | [diff] [blame] | 313 | static const struct pci_device_id shpcd_pci_tbl[] = { |
Pali Rohár | 904b10f | 2022-02-14 12:41:08 +0100 | [diff] [blame] | 314 | {PCI_DEVICE_CLASS(PCI_CLASS_BRIDGE_PCI_NORMAL, ~0)}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { /* end: all zeroes */ } |
| 316 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl); |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | static struct pci_driver shpc_driver = { |
| 320 | .name = SHPC_MODULE_NAME, |
| 321 | .id_table = shpcd_pci_tbl, |
| 322 | .probe = shpc_probe, |
Kenji Kaneshige | e6b82b13 | 2006-02-21 15:45:50 -0800 | [diff] [blame] | 323 | .remove = shpc_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | }; |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | static int __init shpcd_init(void) |
| 327 | { |
Bjorn Helgaas | f652e7d | 2013-01-11 12:21:15 -0700 | [diff] [blame] | 328 | int retval; |
Tejun Heo | e24dcbe | 2010-10-18 08:33:02 +0200 | [diff] [blame] | 329 | |
rajesh.shah@intel.com | 424600f | 2005-10-13 12:05:38 -0700 | [diff] [blame] | 330 | retval = pci_register_driver(&shpc_driver); |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 331 | dbg("%s: pci_register_driver = %d\n", __func__, retval); |
rajesh.shah@intel.com | 424600f | 2005-10-13 12:05:38 -0700 | [diff] [blame] | 332 | info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); |
Bjorn Helgaas | f652e7d | 2013-01-11 12:21:15 -0700 | [diff] [blame] | 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return retval; |
| 335 | } |
| 336 | |
| 337 | static void __exit shpcd_cleanup(void) |
| 338 | { |
| 339 | dbg("unload_shpchpd()\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | pci_unregister_driver(&shpc_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n"); |
| 342 | } |
| 343 | |
| 344 | module_init(shpcd_init); |
| 345 | module_exit(shpcd_cleanup); |