blob: 7ec8a8f72c698ae0862b3c46052953e0bc26df4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * CompactPCI Hot Plug Driver
3 *
Scott Murraybcc488a2005-05-27 16:48:52 -04004 * Copyright (C) 2002,2005 SOMA Networks, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
7 *
8 * All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
19 * details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * Send feedback to <scottm@somanetworks.com>
26 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/slab.h>
31#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070032#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
34#include <linux/interrupt.h>
Arun Sharma600634972011-07-26 16:09:06 -070035#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
Scott Murray0bec2c82007-07-09 11:55:57 -070037#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "cpci_hotplug.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
41#define DRIVER_DESC "CompactPCI Hot Plug Core"
42
43#define MY_NAME "cpci_hotplug"
44
45#define dbg(format, arg...) \
46 do { \
Scott Murraybcc488a2005-05-27 16:48:52 -040047 if (cpci_debug) \
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080048 printk(KERN_DEBUG "%s: " format "\n", \
49 MY_NAME, ## arg); \
Scott Murraybcc488a2005-05-27 16:48:52 -040050 } while (0)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080051#define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
52#define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
53#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* local variables */
Scott Murray43b7d7c2005-05-09 17:31:50 -040056static DECLARE_RWSEM(list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static LIST_HEAD(slot_list);
58static int slots;
Scott Murray43b7d7c2005-05-09 17:31:50 -040059static atomic_t extracting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060int cpci_debug;
61static struct cpci_hp_controller *controller;
Scott Murray0bec2c82007-07-09 11:55:57 -070062static struct task_struct *cpci_thread;
63static int thread_finished;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static int enable_slot(struct hotplug_slot *slot);
66static int disable_slot(struct hotplug_slot *slot);
67static int set_attention_status(struct hotplug_slot *slot, u8 value);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040068static int get_power_status(struct hotplug_slot *slot, u8 *value);
69static int get_attention_status(struct hotplug_slot *slot, u8 *value);
70static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
71static int get_latch_status(struct hotplug_slot *slot, u8 *value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static struct hotplug_slot_ops cpci_hotplug_slot_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .enable_slot = enable_slot,
75 .disable_slot = disable_slot,
76 .set_attention_status = set_attention_status,
77 .get_power_status = get_power_status,
78 .get_attention_status = get_attention_status,
Scott Murray43b7d7c2005-05-09 17:31:50 -040079 .get_adapter_status = get_adapter_status,
80 .get_latch_status = get_latch_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83static int
84update_latch_status(struct hotplug_slot *hotplug_slot, u8 value)
85{
86 struct hotplug_slot_info info;
87
88 memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
89 info.latch_status = value;
90 return pci_hp_change_slot_info(hotplug_slot, &info);
91}
92
93static int
94update_adapter_status(struct hotplug_slot *hotplug_slot, u8 value)
95{
96 struct hotplug_slot_info info;
97
98 memcpy(&info, hotplug_slot->info, sizeof(struct hotplug_slot_info));
99 info.adapter_status = value;
100 return pci_hp_change_slot_info(hotplug_slot, &info);
101}
102
103static int
104enable_slot(struct hotplug_slot *hotplug_slot)
105{
106 struct slot *slot = hotplug_slot->private;
107 int retval = 0;
108
Alex Chiangd6c479e2008-10-20 17:41:17 -0600109 dbg("%s - physical_slot = %s", __func__, slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Scott Murraybcc488a2005-05-27 16:48:52 -0400111 if (controller->ops->set_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 retval = controller->ops->set_power(slot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return retval;
114}
115
116static int
117disable_slot(struct hotplug_slot *hotplug_slot)
118{
119 struct slot *slot = hotplug_slot->private;
120 int retval = 0;
121
Alex Chiangd6c479e2008-10-20 17:41:17 -0600122 dbg("%s - physical_slot = %s", __func__, slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Scott Murraybcc488a2005-05-27 16:48:52 -0400124 down_write(&list_rwsem);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /* Unconfigure device */
Alex Chiangd6c479e2008-10-20 17:41:17 -0600127 dbg("%s - unconfiguring slot %s", __func__, slot_name(slot));
Quentin Lambert79e50e72014-09-07 20:03:32 +0200128 retval = cpci_unconfigure_slot(slot);
129 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 err("%s - could not unconfigure slot %s",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600131 __func__, slot_name(slot));
Scott Murraybcc488a2005-05-27 16:48:52 -0400132 goto disable_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Alex Chiangd6c479e2008-10-20 17:41:17 -0600134 dbg("%s - finished unconfiguring slot %s", __func__, slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /* Clear EXT (by setting it) */
Scott Murraybcc488a2005-05-27 16:48:52 -0400137 if (cpci_clear_ext(slot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 err("%s - could not clear EXT for slot %s",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600139 __func__, slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 retval = -ENODEV;
Scott Murraybcc488a2005-05-27 16:48:52 -0400141 goto disable_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 cpci_led_on(slot);
144
Quentin Lambert79e50e72014-09-07 20:03:32 +0200145 if (controller->ops->set_power) {
146 retval = controller->ops->set_power(slot, 0);
147 if (retval)
Scott Murraybcc488a2005-05-27 16:48:52 -0400148 goto disable_error;
Quentin Lambert79e50e72014-09-07 20:03:32 +0200149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Scott Murraybcc488a2005-05-27 16:48:52 -0400151 if (update_adapter_status(slot->hotplug_slot, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 warn("failure to update adapter file");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Scott Murraybcc488a2005-05-27 16:48:52 -0400154 if (slot->extracting) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400155 slot->extracting = 0;
156 atomic_dec(&extracting);
157 }
Scott Murraybcc488a2005-05-27 16:48:52 -0400158disable_error:
159 up_write(&list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return retval;
161}
162
163static u8
164cpci_get_power_status(struct slot *slot)
165{
166 u8 power = 1;
167
Scott Murraybcc488a2005-05-27 16:48:52 -0400168 if (controller->ops->get_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 power = controller->ops->get_power(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return power;
171}
172
173static int
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400174get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct slot *slot = hotplug_slot->private;
177
178 *value = cpci_get_power_status(slot);
179 return 0;
180}
181
182static int
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400183get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct slot *slot = hotplug_slot->private;
186
187 *value = cpci_get_attention_status(slot);
188 return 0;
189}
190
191static int
192set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
193{
194 return cpci_set_attention_status(hotplug_slot->private, status);
195}
196
Scott Murray43b7d7c2005-05-09 17:31:50 -0400197static int
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400198get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
Scott Murray43b7d7c2005-05-09 17:31:50 -0400199{
200 *value = hotplug_slot->info->adapter_status;
201 return 0;
202}
203
204static int
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400205get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
Scott Murray43b7d7c2005-05-09 17:31:50 -0400206{
207 *value = hotplug_slot->info->latch_status;
208 return 0;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static void release_slot(struct hotplug_slot *hotplug_slot)
212{
213 struct slot *slot = hotplug_slot->private;
214
215 kfree(slot->hotplug_slot->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 kfree(slot->hotplug_slot);
Markus Elfring17f14b52014-12-26 16:28:08 -0700217 pci_dev_put(slot->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 kfree(slot);
219}
220
221#define SLOT_NAME_SIZE 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223int
224cpci_hp_register_bus(struct pci_bus *bus, u8 first, u8 last)
225{
226 struct slot *slot;
227 struct hotplug_slot *hotplug_slot;
228 struct hotplug_slot_info *info;
Alex Chiangd6c479e2008-10-20 17:41:17 -0600229 char name[SLOT_NAME_SIZE];
Julia Lawall83d05712012-07-16 09:25:56 -0600230 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int i;
232
Scott Murraybcc488a2005-05-27 16:48:52 -0400233 if (!(controller && bus))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * Create a structure for each slot, and register that slot
238 * with the pci_hotplug subsystem.
239 */
240 for (i = first; i <= last; ++i) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800241 slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
Julia Lawall83d05712012-07-16 09:25:56 -0600242 if (!slot) {
243 status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto error;
Julia Lawall83d05712012-07-16 09:25:56 -0600245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 hotplug_slot =
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800248 kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
Julia Lawall83d05712012-07-16 09:25:56 -0600249 if (!hotplug_slot) {
250 status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto error_slot;
Julia Lawall83d05712012-07-16 09:25:56 -0600252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 slot->hotplug_slot = hotplug_slot;
254
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800255 info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
Julia Lawall83d05712012-07-16 09:25:56 -0600256 if (!info) {
257 status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto error_hpslot;
Julia Lawall83d05712012-07-16 09:25:56 -0600259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 hotplug_slot->info = info;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 slot->bus = bus;
263 slot->number = i;
264 slot->devfn = PCI_DEVFN(i, 0);
265
Alex Chiangd6c479e2008-10-20 17:41:17 -0600266 snprintf(name, SLOT_NAME_SIZE, "%02x:%02x", bus->number, i);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 hotplug_slot->private = slot;
269 hotplug_slot->release = &release_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 hotplug_slot->ops = &cpci_hotplug_slot_ops;
271
272 /*
273 * Initialize the slot info structure with some known
274 * good values.
275 */
Alex Chiangd6c479e2008-10-20 17:41:17 -0600276 dbg("initializing slot %s", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 info->power_status = cpci_get_power_status(slot);
278 info->attention_status = cpci_get_attention_status(slot);
279
Alex Chiangd6c479e2008-10-20 17:41:17 -0600280 dbg("registering slot %s", name);
281 status = pci_hp_register(slot->hotplug_slot, bus, i, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (status) {
283 err("pci_hp_register failed with error %d", status);
Alex Chiangd6c479e2008-10-20 17:41:17 -0600284 goto error_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Alex Chiangd6c479e2008-10-20 17:41:17 -0600286 dbg("slot registered with name: %s", slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /* Add slot to our internal list */
Scott Murray43b7d7c2005-05-09 17:31:50 -0400289 down_write(&list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 list_add(&slot->slot_list, &slot_list);
291 slots++;
Scott Murray43b7d7c2005-05-09 17:31:50 -0400292 up_write(&list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295error_info:
296 kfree(info);
297error_hpslot:
298 kfree(hotplug_slot);
299error_slot:
300 kfree(slot);
301error:
302 return status;
303}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600304EXPORT_SYMBOL_GPL(cpci_hp_register_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306int
307cpci_hp_unregister_bus(struct pci_bus *bus)
308{
309 struct slot *slot;
Scott Murraybcc488a2005-05-27 16:48:52 -0400310 struct slot *tmp;
311 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Scott Murray43b7d7c2005-05-09 17:31:50 -0400313 down_write(&list_rwsem);
Scott Murraybcc488a2005-05-27 16:48:52 -0400314 if (!slots) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400315 up_write(&list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -1;
317 }
Scott Murraybcc488a2005-05-27 16:48:52 -0400318 list_for_each_entry_safe(slot, tmp, &slot_list, slot_list) {
319 if (slot->bus == bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 list_del(&slot->slot_list);
321 slots--;
Scott Murraybcc488a2005-05-27 16:48:52 -0400322
Alex Chiangd6c479e2008-10-20 17:41:17 -0600323 dbg("deregistering slot %s", slot_name(slot));
Scott Murraybcc488a2005-05-27 16:48:52 -0400324 status = pci_hp_deregister(slot->hotplug_slot);
325 if (status) {
326 err("pci_hp_deregister failed with error %d",
327 status);
328 break;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 }
Scott Murray43b7d7c2005-05-09 17:31:50 -0400332 up_write(&list_rwsem);
Scott Murraybcc488a2005-05-27 16:48:52 -0400333 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600335EXPORT_SYMBOL_GPL(cpci_hp_unregister_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337/* This is the interrupt mode interrupt handler */
338static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100339cpci_hp_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 dbg("entered cpci_hp_intr");
342
343 /* Check to see if it was our interrupt */
Thomas Gleixner6b4486e2006-07-01 19:29:41 -0700344 if ((controller->irq_flags & IRQF_SHARED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 !controller->ops->check_irq(controller->dev_id)) {
346 dbg("exited cpci_hp_intr, not our interrupt");
347 return IRQ_NONE;
348 }
349
350 /* Disable ENUM interrupt */
351 controller->ops->disable_irq();
352
353 /* Trigger processing by the event thread */
Scott Murray0bec2c82007-07-09 11:55:57 -0700354 wake_up_process(cpci_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return IRQ_HANDLED;
356}
357
358/*
Scott Murray43b7d7c2005-05-09 17:31:50 -0400359 * According to PICMG 2.1 R2.0, section 6.3.2, upon
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * initialization, the system driver shall clear the
361 * INS bits of the cold-inserted devices.
362 */
363static int
Scott Murraybcc488a2005-05-27 16:48:52 -0400364init_slots(int clear_ins)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct slot *slot;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400367 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800369 dbg("%s - enter", __func__);
Scott Murray43b7d7c2005-05-09 17:31:50 -0400370 down_read(&list_rwsem);
Scott Murraybcc488a2005-05-27 16:48:52 -0400371 if (!slots) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400372 up_read(&list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -1;
374 }
Scott Murraybcc488a2005-05-27 16:48:52 -0400375 list_for_each_entry(slot, &slot_list, slot_list) {
Alex Chiangd6c479e2008-10-20 17:41:17 -0600376 dbg("%s - looking at slot %s", __func__, slot_name(slot));
Scott Murraybcc488a2005-05-27 16:48:52 -0400377 if (clear_ins && cpci_check_and_clear_ins(slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 dbg("%s - cleared INS for slot %s",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600379 __func__, slot_name(slot));
Scott Murraybcc488a2005-05-27 16:48:52 -0400380 dev = pci_get_slot(slot->bus, PCI_DEVFN(slot->number, 0));
381 if (dev) {
382 if (update_adapter_status(slot->hotplug_slot, 1))
383 warn("failure to update adapter file");
384 if (update_latch_status(slot->hotplug_slot, 1))
385 warn("failure to update latch file");
386 slot->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 }
Scott Murray43b7d7c2005-05-09 17:31:50 -0400389 up_read(&list_rwsem);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800390 dbg("%s - exit", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
394static int
395check_slots(void)
396{
397 struct slot *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int extracted;
399 int inserted;
Scott Murray43b7d7c2005-05-09 17:31:50 -0400400 u16 hs_csr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Scott Murray43b7d7c2005-05-09 17:31:50 -0400402 down_read(&list_rwsem);
Scott Murraybcc488a2005-05-27 16:48:52 -0400403 if (!slots) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400404 up_read(&list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 err("no slots registered, shutting down");
406 return -1;
407 }
408 extracted = inserted = 0;
Scott Murraybcc488a2005-05-27 16:48:52 -0400409 list_for_each_entry(slot, &slot_list, slot_list) {
Alex Chiangd6c479e2008-10-20 17:41:17 -0600410 dbg("%s - looking at slot %s", __func__, slot_name(slot));
Scott Murraybcc488a2005-05-27 16:48:52 -0400411 if (cpci_check_and_clear_ins(slot)) {
412 /*
413 * Some broken hardware (e.g. PLX 9054AB) asserts
414 * ENUM# twice...
415 */
416 if (slot->dev) {
417 warn("slot %s already inserted",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600418 slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 inserted++;
420 continue;
421 }
422
423 /* Process insertion */
Alex Chiangd6c479e2008-10-20 17:41:17 -0600424 dbg("%s - slot %s inserted", __func__, slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* GSM, debug */
427 hs_csr = cpci_get_hs_csr(slot);
428 dbg("%s - slot %s HS_CSR (1) = %04x",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600429 __func__, slot_name(slot), hs_csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Configure device */
432 dbg("%s - configuring slot %s",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600433 __func__, slot_name(slot));
Scott Murraybcc488a2005-05-27 16:48:52 -0400434 if (cpci_configure_slot(slot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 err("%s - could not configure slot %s",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600436 __func__, slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 continue;
438 }
439 dbg("%s - finished configuring slot %s",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600440 __func__, slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* GSM, debug */
443 hs_csr = cpci_get_hs_csr(slot);
444 dbg("%s - slot %s HS_CSR (2) = %04x",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600445 __func__, slot_name(slot), hs_csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Scott Murraybcc488a2005-05-27 16:48:52 -0400447 if (update_latch_status(slot->hotplug_slot, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 warn("failure to update latch file");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Scott Murraybcc488a2005-05-27 16:48:52 -0400450 if (update_adapter_status(slot->hotplug_slot, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 warn("failure to update adapter file");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 cpci_led_off(slot);
454
455 /* GSM, debug */
456 hs_csr = cpci_get_hs_csr(slot);
457 dbg("%s - slot %s HS_CSR (3) = %04x",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600458 __func__, slot_name(slot), hs_csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 inserted++;
Scott Murraybcc488a2005-05-27 16:48:52 -0400461 } else if (cpci_check_ext(slot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* Process extraction request */
463 dbg("%s - slot %s extracted",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600464 __func__, slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* GSM, debug */
467 hs_csr = cpci_get_hs_csr(slot);
468 dbg("%s - slot %s HS_CSR = %04x",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600469 __func__, slot_name(slot), hs_csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Scott Murraybcc488a2005-05-27 16:48:52 -0400471 if (!slot->extracting) {
Quentin Lambert656f9782014-09-07 20:02:47 +0200472 if (update_latch_status(slot->hotplug_slot, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 warn("failure to update latch file");
Quentin Lambert656f9782014-09-07 20:02:47 +0200474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 slot->extracting = 1;
Scott Murraybcc488a2005-05-27 16:48:52 -0400476 atomic_inc(&extracting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 extracted++;
Scott Murraybcc488a2005-05-27 16:48:52 -0400479 } else if (slot->extracting) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400480 hs_csr = cpci_get_hs_csr(slot);
Scott Murraybcc488a2005-05-27 16:48:52 -0400481 if (hs_csr == 0xffff) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400482 /*
483 * Hmmm, we're likely hosed at this point, should we
484 * bother trying to tell the driver or not?
485 */
486 err("card in slot %s was improperly removed",
Alex Chiangd6c479e2008-10-20 17:41:17 -0600487 slot_name(slot));
Scott Murraybcc488a2005-05-27 16:48:52 -0400488 if (update_adapter_status(slot->hotplug_slot, 0))
Scott Murray43b7d7c2005-05-09 17:31:50 -0400489 warn("failure to update adapter file");
Scott Murray43b7d7c2005-05-09 17:31:50 -0400490 slot->extracting = 0;
491 atomic_dec(&extracting);
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 }
Scott Murray43b7d7c2005-05-09 17:31:50 -0400495 up_read(&list_rwsem);
496 dbg("inserted=%d, extracted=%d, extracting=%d",
497 inserted, extracted, atomic_read(&extracting));
Scott Murraybcc488a2005-05-27 16:48:52 -0400498 if (inserted || extracted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return extracted;
Scott Murraybcc488a2005-05-27 16:48:52 -0400500 else if (!atomic_read(&extracting)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 err("cannot find ENUM# source, shutting down");
502 return -1;
503 }
Scott Murray43b7d7c2005-05-09 17:31:50 -0400504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/* This is the interrupt mode worker thread body */
508static int
509event_thread(void *data)
510{
511 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800513 dbg("%s - event thread started", __func__);
Scott Murraybcc488a2005-05-27 16:48:52 -0400514 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 dbg("event thread sleeping");
Scott Murray0bec2c82007-07-09 11:55:57 -0700516 set_current_state(TASK_INTERRUPTIBLE);
517 schedule();
518 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
Scott Murray43b7d7c2005-05-09 17:31:50 -0400520 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 rc = check_slots();
Scott Murray43b7d7c2005-05-09 17:31:50 -0400522 if (rc > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Give userspace a chance to handle extraction */
524 msleep(500);
Scott Murray43b7d7c2005-05-09 17:31:50 -0400525 } else if (rc < 0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800526 dbg("%s - error checking slots", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 thread_finished = 1;
Scott Murray0bec2c82007-07-09 11:55:57 -0700528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Scott Murray0bec2c82007-07-09 11:55:57 -0700530 } while (atomic_read(&extracting) && !kthread_should_stop());
531 if (kthread_should_stop())
Scott Murraybcc488a2005-05-27 16:48:52 -0400532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Re-enable ENUM# interrupt */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800535 dbg("%s - re-enabling irq", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 controller->ops->enable_irq();
537 }
Scott Murray0bec2c82007-07-09 11:55:57 -0700538 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0;
540}
541
542/* This is the polling mode worker thread body */
543static int
544poll_thread(void *data)
545{
546 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Scott Murraybcc488a2005-05-27 16:48:52 -0400548 while (1) {
Scott Murray0bec2c82007-07-09 11:55:57 -0700549 if (kthread_should_stop() || signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
Scott Murraybcc488a2005-05-27 16:48:52 -0400551 if (controller->ops->query_enum()) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400552 do {
553 rc = check_slots();
Scott Murraybcc488a2005-05-27 16:48:52 -0400554 if (rc > 0) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400555 /* Give userspace a chance to handle extraction */
556 msleep(500);
Scott Murraybcc488a2005-05-27 16:48:52 -0400557 } else if (rc < 0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800558 dbg("%s - error checking slots", __func__);
Scott Murray43b7d7c2005-05-09 17:31:50 -0400559 thread_finished = 1;
Scott Murray0bec2c82007-07-09 11:55:57 -0700560 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Scott Murray0bec2c82007-07-09 11:55:57 -0700562 } while (atomic_read(&extracting) && !kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 msleep(100);
565 }
Scott Murray0bec2c82007-07-09 11:55:57 -0700566 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
568}
569
570static int
571cpci_start_thread(void)
572{
Scott Murraybcc488a2005-05-27 16:48:52 -0400573 if (controller->irq)
Scott Murray0bec2c82007-07-09 11:55:57 -0700574 cpci_thread = kthread_run(event_thread, NULL, "cpci_hp_eventd");
Scott Murraybcc488a2005-05-27 16:48:52 -0400575 else
Scott Murray0bec2c82007-07-09 11:55:57 -0700576 cpci_thread = kthread_run(poll_thread, NULL, "cpci_hp_polld");
577 if (IS_ERR(cpci_thread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 err("Can't start up our thread");
Scott Murray0bec2c82007-07-09 11:55:57 -0700579 return PTR_ERR(cpci_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Scott Murray0bec2c82007-07-09 11:55:57 -0700581 thread_finished = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
583}
584
585static void
586cpci_stop_thread(void)
587{
Scott Murray0bec2c82007-07-09 11:55:57 -0700588 kthread_stop(cpci_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 thread_finished = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592int
593cpci_hp_register_controller(struct cpci_hp_controller *new_controller)
594{
595 int status = 0;
596
Scott Murraybcc488a2005-05-27 16:48:52 -0400597 if (controller)
598 return -1;
599 if (!(new_controller && new_controller->ops))
600 return -EINVAL;
601 if (new_controller->irq) {
602 if (!(new_controller->ops->enable_irq &&
603 new_controller->ops->disable_irq))
604 status = -EINVAL;
605 if (request_irq(new_controller->irq,
606 cpci_hp_intr,
607 new_controller->irq_flags,
608 MY_NAME,
609 new_controller->dev_id)) {
610 err("Can't get irq %d for the hotplug cPCI controller",
611 new_controller->irq);
612 status = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Scott Murraybcc488a2005-05-27 16:48:52 -0400614 dbg("%s - acquired controller irq %d",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800615 __func__, new_controller->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Scott Murraybcc488a2005-05-27 16:48:52 -0400617 if (!status)
618 controller = new_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return status;
620}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600621EXPORT_SYMBOL_GPL(cpci_hp_register_controller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Scott Murraybcc488a2005-05-27 16:48:52 -0400623static void
624cleanup_slots(void)
625{
626 struct slot *slot;
627 struct slot *tmp;
628
629 /*
630 * Unregister all of our slots with the pci_hotplug subsystem,
631 * and free up all memory that we had allocated.
632 */
633 down_write(&list_rwsem);
634 if (!slots)
635 goto cleanup_null;
636 list_for_each_entry_safe(slot, tmp, &slot_list, slot_list) {
637 list_del(&slot->slot_list);
638 pci_hp_deregister(slot->hotplug_slot);
639 }
640cleanup_null:
641 up_write(&list_rwsem);
642 return;
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645int
646cpci_hp_unregister_controller(struct cpci_hp_controller *old_controller)
647{
648 int status = 0;
649
Scott Murraybcc488a2005-05-27 16:48:52 -0400650 if (controller) {
651 if (!thread_finished)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 cpci_stop_thread();
Scott Murraybcc488a2005-05-27 16:48:52 -0400653 if (controller->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 free_irq(controller->irq, controller->dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 controller = NULL;
Scott Murraybcc488a2005-05-27 16:48:52 -0400656 cleanup_slots();
657 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 status = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return status;
660}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600661EXPORT_SYMBOL_GPL(cpci_hp_unregister_controller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663int
664cpci_hp_start(void)
665{
666 static int first = 1;
667 int status;
668
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800669 dbg("%s - enter", __func__);
Scott Murraybcc488a2005-05-27 16:48:52 -0400670 if (!controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Scott Murray43b7d7c2005-05-09 17:31:50 -0400673 down_read(&list_rwsem);
Scott Murraybcc488a2005-05-27 16:48:52 -0400674 if (list_empty(&slot_list)) {
Scott Murray43b7d7c2005-05-09 17:31:50 -0400675 up_read(&list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENODEV;
677 }
Scott Murray43b7d7c2005-05-09 17:31:50 -0400678 up_read(&list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Scott Murraybcc488a2005-05-27 16:48:52 -0400680 status = init_slots(first);
681 if (first)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 first = 0;
Scott Murraybcc488a2005-05-27 16:48:52 -0400683 if (status)
684 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 status = cpci_start_thread();
Scott Murraybcc488a2005-05-27 16:48:52 -0400687 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return status;
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800689 dbg("%s - thread started", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Scott Murraybcc488a2005-05-27 16:48:52 -0400691 if (controller->irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* Start enum interrupt processing */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800693 dbg("%s - enabling irq", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 controller->ops->enable_irq();
695 }
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800696 dbg("%s - exit", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600699EXPORT_SYMBOL_GPL(cpci_hp_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701int
702cpci_hp_stop(void)
703{
Scott Murraybcc488a2005-05-27 16:48:52 -0400704 if (!controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENODEV;
Scott Murraybcc488a2005-05-27 16:48:52 -0400706 if (controller->irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* Stop enum interrupt processing */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800708 dbg("%s - disabling irq", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 controller->ops->disable_irq();
710 }
711 cpci_stop_thread();
712 return 0;
713}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600714EXPORT_SYMBOL_GPL(cpci_hp_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716int __init
717cpci_hotplug_init(int debug)
718{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 cpci_debug = debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 0;
721}