PCI: pciehp: remove slot_list field
[pandora-kernel.git] / drivers / pci / hotplug / pciehp_core.c
1 /*
2  * PCI Express Hot Plug Controller Driver
3  *
4  * Copyright (C) 1995,2001 Compaq Computer Corporation
5  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6  * Copyright (C) 2001 IBM Corp.
7  * Copyright (C) 2003-2004 Intel Corporation
8  *
9  * All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19  * NON INFRINGEMENT.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
27  *
28  */
29
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/pci.h>
35 #include "pciehp.h"
36 #include <linux/interrupt.h>
37 #include <linux/time.h>
38
39 /* Global variables */
40 int pciehp_debug;
41 int pciehp_poll_mode;
42 int pciehp_poll_time;
43 int pciehp_force;
44 struct workqueue_struct *pciehp_wq;
45
46 #define DRIVER_VERSION  "0.4"
47 #define DRIVER_AUTHOR   "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
48 #define DRIVER_DESC     "PCI Express Hot Plug Controller Driver"
49
50 MODULE_AUTHOR(DRIVER_AUTHOR);
51 MODULE_DESCRIPTION(DRIVER_DESC);
52 MODULE_LICENSE("GPL");
53
54 module_param(pciehp_debug, bool, 0644);
55 module_param(pciehp_poll_mode, bool, 0644);
56 module_param(pciehp_poll_time, int, 0644);
57 module_param(pciehp_force, bool, 0644);
58 MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
59 MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
60 MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
61 MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
62
63 #define PCIE_MODULE_NAME "pciehp"
64
65 static int set_attention_status (struct hotplug_slot *slot, u8 value);
66 static int enable_slot          (struct hotplug_slot *slot);
67 static int disable_slot         (struct hotplug_slot *slot);
68 static int get_power_status     (struct hotplug_slot *slot, u8 *value);
69 static int get_attention_status (struct hotplug_slot *slot, u8 *value);
70 static int get_latch_status     (struct hotplug_slot *slot, u8 *value);
71 static int get_adapter_status   (struct hotplug_slot *slot, u8 *value);
72 static int get_max_bus_speed    (struct hotplug_slot *slot, enum pci_bus_speed *value);
73 static int get_cur_bus_speed    (struct hotplug_slot *slot, enum pci_bus_speed *value);
74
75 static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
76         .set_attention_status = set_attention_status,
77         .enable_slot =          enable_slot,
78         .disable_slot =         disable_slot,
79         .get_power_status =     get_power_status,
80         .get_attention_status = get_attention_status,
81         .get_latch_status =     get_latch_status,
82         .get_adapter_status =   get_adapter_status,
83         .get_max_bus_speed =    get_max_bus_speed,
84         .get_cur_bus_speed =    get_cur_bus_speed,
85 };
86
87 /**
88  * release_slot - free up the memory used by a slot
89  * @hotplug_slot: slot to free
90  */
91 static void release_slot(struct hotplug_slot *hotplug_slot)
92 {
93         struct slot *slot = hotplug_slot->private;
94
95         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
96                  __func__, hotplug_slot_name(hotplug_slot));
97
98         kfree(hotplug_slot->info);
99         kfree(hotplug_slot);
100 }
101
102 static int init_slot(struct controller *ctrl)
103 {
104         struct slot *slot = ctrl->slot;
105         struct hotplug_slot *hotplug = NULL;
106         struct hotplug_slot_info *info = NULL;
107         char name[SLOT_NAME_SIZE];
108         int retval = -ENOMEM;
109
110         hotplug = kzalloc(sizeof(*hotplug), GFP_KERNEL);
111         if (!hotplug)
112                 goto out;
113
114         info = kzalloc(sizeof(*info), GFP_KERNEL);
115         if (!info)
116                 goto out;
117
118         /* register this slot with the hotplug pci core */
119         hotplug->info = info;
120         hotplug->private = slot;
121         hotplug->release = &release_slot;
122         hotplug->ops = &pciehp_hotplug_slot_ops;
123         slot->hotplug_slot = hotplug;
124         snprintf(name, SLOT_NAME_SIZE, "%u", slot->number);
125
126         ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x "
127                  "hp_slot=%x sun=%x slot_device_offset=%x\n",
128                  pci_domain_nr(ctrl->pci_dev->subordinate),
129                  slot->bus, slot->device, slot->hp_slot, slot->number,
130                  ctrl->slot_device_offset);
131         retval = pci_hp_register(hotplug,
132                                  ctrl->pci_dev->subordinate,
133                                  slot->device,
134                                  name);
135         if (retval) {
136                 ctrl_err(ctrl,
137                          "pci_hp_register failed with error %d\n", retval);
138                 goto out;
139         }
140         get_power_status(hotplug, &info->power_status);
141         get_attention_status(hotplug, &info->attention_status);
142         get_latch_status(hotplug, &info->latch_status);
143         get_adapter_status(hotplug, &info->adapter_status);
144 out:
145         if (retval) {
146                 kfree(info);
147                 kfree(hotplug);
148         }
149         return retval;
150 }
151
152 static void cleanup_slot(struct controller *ctrl)
153 {
154         pci_hp_deregister(ctrl->slot->hotplug_slot);
155 }
156
157 /*
158  * set_attention_status - Turns the Amber LED for a slot on, off or blink
159  */
160 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
161 {
162         struct slot *slot = hotplug_slot->private;
163
164         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
165                   __func__, slot_name(slot));
166
167         hotplug_slot->info->attention_status = status;
168
169         if (ATTN_LED(slot->ctrl))
170                 slot->hpc_ops->set_attention_status(slot, status);
171
172         return 0;
173 }
174
175
176 static int enable_slot(struct hotplug_slot *hotplug_slot)
177 {
178         struct slot *slot = hotplug_slot->private;
179
180         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
181                  __func__, slot_name(slot));
182
183         return pciehp_sysfs_enable_slot(slot);
184 }
185
186
187 static int disable_slot(struct hotplug_slot *hotplug_slot)
188 {
189         struct slot *slot = hotplug_slot->private;
190
191         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
192                   __func__, slot_name(slot));
193
194         return pciehp_sysfs_disable_slot(slot);
195 }
196
197 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
198 {
199         struct slot *slot = hotplug_slot->private;
200         int retval;
201
202         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
203                   __func__, slot_name(slot));
204
205         retval = slot->hpc_ops->get_power_status(slot, value);
206         if (retval < 0)
207                 *value = hotplug_slot->info->power_status;
208
209         return 0;
210 }
211
212 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
213 {
214         struct slot *slot = hotplug_slot->private;
215         int retval;
216
217         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
218                   __func__, slot_name(slot));
219
220         retval = slot->hpc_ops->get_attention_status(slot, value);
221         if (retval < 0)
222                 *value = hotplug_slot->info->attention_status;
223
224         return 0;
225 }
226
227 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
228 {
229         struct slot *slot = hotplug_slot->private;
230         int retval;
231
232         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
233                  __func__, slot_name(slot));
234
235         retval = slot->hpc_ops->get_latch_status(slot, value);
236         if (retval < 0)
237                 *value = hotplug_slot->info->latch_status;
238
239         return 0;
240 }
241
242 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
243 {
244         struct slot *slot = hotplug_slot->private;
245         int retval;
246
247         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
248                  __func__, slot_name(slot));
249
250         retval = slot->hpc_ops->get_adapter_status(slot, value);
251         if (retval < 0)
252                 *value = hotplug_slot->info->adapter_status;
253
254         return 0;
255 }
256
257 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
258                                 enum pci_bus_speed *value)
259 {
260         struct slot *slot = hotplug_slot->private;
261         int retval;
262
263         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
264                  __func__, slot_name(slot));
265
266         retval = slot->hpc_ops->get_max_bus_speed(slot, value);
267         if (retval < 0)
268                 *value = PCI_SPEED_UNKNOWN;
269
270         return 0;
271 }
272
273 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
274 {
275         struct slot *slot = hotplug_slot->private;
276         int retval;
277
278         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
279                  __func__, slot_name(slot));
280
281         retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
282         if (retval < 0)
283                 *value = PCI_SPEED_UNKNOWN;
284
285         return 0;
286 }
287
288 static int pciehp_probe(struct pcie_device *dev)
289 {
290         int rc;
291         struct controller *ctrl;
292         struct slot *slot;
293         u8 value;
294         struct pci_dev *pdev = dev->port;
295
296         if (pciehp_force)
297                 dev_info(&dev->device,
298                          "Bypassing BIOS check for pciehp use on %s\n",
299                          pci_name(pdev));
300         else if (pciehp_get_hp_hw_control_from_firmware(pdev))
301                 goto err_out_none;
302
303         ctrl = pcie_init(dev);
304         if (!ctrl) {
305                 dev_err(&dev->device, "Controller initialization failed\n");
306                 goto err_out_none;
307         }
308         set_service_data(dev, ctrl);
309
310         /* Setup the slot information structures */
311         rc = init_slot(ctrl);
312         if (rc) {
313                 if (rc == -EBUSY)
314                         ctrl_warn(ctrl, "Slot already registered by another "
315                                   "hotplug driver\n");
316                 else
317                         ctrl_err(ctrl, "Slot initialization failed\n");
318                 goto err_out_release_ctlr;
319         }
320
321         /* Enable events after we have setup the data structures */
322         rc = pcie_init_notification(ctrl);
323         if (rc) {
324                 ctrl_err(ctrl, "Notification initialization failed\n");
325                 goto err_out_release_ctlr;
326         }
327
328         /* Check if slot is occupied */
329         slot = ctrl->slot;
330         slot->hpc_ops->get_adapter_status(slot, &value);
331         if (value) {
332                 if (pciehp_force)
333                         pciehp_enable_slot(slot);
334         } else {
335                 /* Power off slot if not occupied */
336                 if (POWER_CTRL(ctrl)) {
337                         rc = slot->hpc_ops->power_off_slot(slot);
338                         if (rc)
339                                 goto err_out_free_ctrl_slot;
340                 }
341         }
342
343         return 0;
344
345 err_out_free_ctrl_slot:
346         cleanup_slot(ctrl);
347 err_out_release_ctlr:
348         ctrl->hpc_ops->release_ctlr(ctrl);
349 err_out_none:
350         return -ENODEV;
351 }
352
353 static void pciehp_remove (struct pcie_device *dev)
354 {
355         struct controller *ctrl = get_service_data(dev);
356
357         cleanup_slot(ctrl);
358         ctrl->hpc_ops->release_ctlr(ctrl);
359 }
360
361 #ifdef CONFIG_PM
362 static int pciehp_suspend (struct pcie_device *dev)
363 {
364         dev_info(&dev->device, "%s ENTRY\n", __func__);
365         return 0;
366 }
367
368 static int pciehp_resume (struct pcie_device *dev)
369 {
370         dev_info(&dev->device, "%s ENTRY\n", __func__);
371         if (pciehp_force) {
372                 struct controller *ctrl = get_service_data(dev);
373                 struct slot *slot;
374                 u8 status;
375
376                 /* reinitialize the chipset's event detection logic */
377                 pcie_enable_notification(ctrl);
378
379                 slot = ctrl->slot;
380
381                 /* Check if slot is occupied */
382                 slot->hpc_ops->get_adapter_status(slot, &status);
383                 if (status)
384                         pciehp_enable_slot(slot);
385                 else
386                         pciehp_disable_slot(slot);
387         }
388         return 0;
389 }
390 #endif /* PM */
391
392 static struct pcie_port_service_driver hpdriver_portdrv = {
393         .name           = PCIE_MODULE_NAME,
394         .port_type      = PCIE_ANY_PORT,
395         .service        = PCIE_PORT_SERVICE_HP,
396
397         .probe          = pciehp_probe,
398         .remove         = pciehp_remove,
399
400 #ifdef  CONFIG_PM
401         .suspend        = pciehp_suspend,
402         .resume         = pciehp_resume,
403 #endif  /* PM */
404 };
405
406 static int __init pcied_init(void)
407 {
408         int retval = 0;
409
410         pciehp_firmware_init();
411         retval = pcie_port_service_register(&hpdriver_portdrv);
412         dbg("pcie_port_service_register = %d\n", retval);
413         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
414         if (retval)
415                 dbg("Failure to register service\n");
416         return retval;
417 }
418
419 static void __exit pcied_cleanup(void)
420 {
421         dbg("unload_pciehpd()\n");
422         pcie_port_service_unregister(&hpdriver_portdrv);
423         info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
424 }
425
426 module_init(pcied_init);
427 module_exit(pcied_cleanup);