]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/pci/hotplug/pciehp_core.c
eb183d1d0912f89c7862cc1e43f9da600bfe9e6e
[lisovros/linux_canprio.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         .owner =                THIS_MODULE,
77         .set_attention_status = set_attention_status,
78         .enable_slot =          enable_slot,
79         .disable_slot =         disable_slot,
80         .get_power_status =     get_power_status,
81         .get_attention_status = get_attention_status,
82         .get_latch_status =     get_latch_status,
83         .get_adapter_status =   get_adapter_status,
84         .get_max_bus_speed =    get_max_bus_speed,
85         .get_cur_bus_speed =    get_cur_bus_speed,
86 };
87
88 /**
89  * release_slot - free up the memory used by a slot
90  * @hotplug_slot: slot to free
91  */
92 static void release_slot(struct hotplug_slot *hotplug_slot)
93 {
94         struct slot *slot = hotplug_slot->private;
95
96         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
97                  __func__, hotplug_slot_name(hotplug_slot));
98
99         kfree(hotplug_slot->info);
100         kfree(hotplug_slot);
101 }
102
103 static int init_slots(struct controller *ctrl)
104 {
105         struct slot *slot;
106         struct hotplug_slot *hotplug_slot;
107         struct hotplug_slot_info *info;
108         char name[SLOT_NAME_SIZE];
109         int retval = -ENOMEM;
110
111         list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
112                 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
113                 if (!hotplug_slot)
114                         goto error;
115
116                 info = kzalloc(sizeof(*info), GFP_KERNEL);
117                 if (!info)
118                         goto error_hpslot;
119
120                 /* register this slot with the hotplug pci core */
121                 hotplug_slot->info = info;
122                 hotplug_slot->private = slot;
123                 hotplug_slot->release = &release_slot;
124                 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
125                 slot->hotplug_slot = hotplug_slot;
126                 snprintf(name, SLOT_NAME_SIZE, "%u", slot->number);
127
128                 ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x "
129                          "hp_slot=%x sun=%x slot_device_offset=%x\n",
130                          pci_domain_nr(ctrl->pci_dev->subordinate),
131                          slot->bus, slot->device, slot->hp_slot, slot->number,
132                          ctrl->slot_device_offset);
133                 retval = pci_hp_register(hotplug_slot,
134                                          ctrl->pci_dev->subordinate,
135                                          slot->device,
136                                          name);
137                 if (retval) {
138                         ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
139                                  retval);
140                         goto error_info;
141                 }
142                 get_power_status(hotplug_slot, &info->power_status);
143                 get_attention_status(hotplug_slot, &info->attention_status);
144                 get_latch_status(hotplug_slot, &info->latch_status);
145                 get_adapter_status(hotplug_slot, &info->adapter_status);
146         }
147
148         return 0;
149 error_info:
150         kfree(info);
151 error_hpslot:
152         kfree(hotplug_slot);
153 error:
154         return retval;
155 }
156
157 static void cleanup_slots(struct controller *ctrl)
158 {
159         struct slot *slot;
160         list_for_each_entry(slot, &ctrl->slot_list, slot_list)
161                 pci_hp_deregister(slot->hotplug_slot);
162 }
163
164 /*
165  * set_attention_status - Turns the Amber LED for a slot on, off or blink
166  */
167 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
168 {
169         struct slot *slot = hotplug_slot->private;
170
171         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
172                   __func__, slot_name(slot));
173
174         hotplug_slot->info->attention_status = status;
175
176         if (ATTN_LED(slot->ctrl))
177                 slot->hpc_ops->set_attention_status(slot, status);
178
179         return 0;
180 }
181
182
183 static int enable_slot(struct hotplug_slot *hotplug_slot)
184 {
185         struct slot *slot = hotplug_slot->private;
186
187         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
188                  __func__, slot_name(slot));
189
190         return pciehp_sysfs_enable_slot(slot);
191 }
192
193
194 static int disable_slot(struct hotplug_slot *hotplug_slot)
195 {
196         struct slot *slot = hotplug_slot->private;
197
198         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
199                   __func__, slot_name(slot));
200
201         return pciehp_sysfs_disable_slot(slot);
202 }
203
204 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
205 {
206         struct slot *slot = hotplug_slot->private;
207         int retval;
208
209         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
210                   __func__, slot_name(slot));
211
212         retval = slot->hpc_ops->get_power_status(slot, value);
213         if (retval < 0)
214                 *value = hotplug_slot->info->power_status;
215
216         return 0;
217 }
218
219 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
220 {
221         struct slot *slot = hotplug_slot->private;
222         int retval;
223
224         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
225                   __func__, slot_name(slot));
226
227         retval = slot->hpc_ops->get_attention_status(slot, value);
228         if (retval < 0)
229                 *value = hotplug_slot->info->attention_status;
230
231         return 0;
232 }
233
234 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
235 {
236         struct slot *slot = hotplug_slot->private;
237         int retval;
238
239         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
240                  __func__, slot_name(slot));
241
242         retval = slot->hpc_ops->get_latch_status(slot, value);
243         if (retval < 0)
244                 *value = hotplug_slot->info->latch_status;
245
246         return 0;
247 }
248
249 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
250 {
251         struct slot *slot = hotplug_slot->private;
252         int retval;
253
254         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
255                  __func__, slot_name(slot));
256
257         retval = slot->hpc_ops->get_adapter_status(slot, value);
258         if (retval < 0)
259                 *value = hotplug_slot->info->adapter_status;
260
261         return 0;
262 }
263
264 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
265                                 enum pci_bus_speed *value)
266 {
267         struct slot *slot = hotplug_slot->private;
268         int retval;
269
270         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
271                  __func__, slot_name(slot));
272
273         retval = slot->hpc_ops->get_max_bus_speed(slot, value);
274         if (retval < 0)
275                 *value = PCI_SPEED_UNKNOWN;
276
277         return 0;
278 }
279
280 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
281 {
282         struct slot *slot = hotplug_slot->private;
283         int retval;
284
285         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
286                  __func__, slot_name(slot));
287
288         retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
289         if (retval < 0)
290                 *value = PCI_SPEED_UNKNOWN;
291
292         return 0;
293 }
294
295 static int pciehp_probe(struct pcie_device *dev)
296 {
297         int rc;
298         struct controller *ctrl;
299         struct slot *t_slot;
300         u8 value;
301         struct pci_dev *pdev = dev->port;
302
303         if (pciehp_force)
304                 dev_info(&dev->device,
305                          "Bypassing BIOS check for pciehp use on %s\n",
306                          pci_name(pdev));
307         else if (pciehp_get_hp_hw_control_from_firmware(pdev))
308                 goto err_out_none;
309
310         ctrl = pcie_init(dev);
311         if (!ctrl) {
312                 dev_err(&dev->device, "Controller initialization failed\n");
313                 goto err_out_none;
314         }
315         set_service_data(dev, ctrl);
316
317         /* Setup the slot information structures */
318         rc = init_slots(ctrl);
319         if (rc) {
320                 if (rc == -EBUSY)
321                         ctrl_warn(ctrl, "Slot already registered by another "
322                                   "hotplug driver\n");
323                 else
324                         ctrl_err(ctrl, "Slot initialization failed\n");
325                 goto err_out_release_ctlr;
326         }
327
328         /* Enable events after we have setup the data structures */
329         rc = pcie_init_notification(ctrl);
330         if (rc) {
331                 ctrl_err(ctrl, "Notification initialization failed\n");
332                 goto err_out_release_ctlr;
333         }
334
335         /* Check if slot is occupied */
336         t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
337         t_slot->hpc_ops->get_adapter_status(t_slot, &value);
338         if (value) {
339                 if (pciehp_force)
340                         pciehp_enable_slot(t_slot);
341         } else {
342                 /* Power off slot if not occupied */
343                 if (POWER_CTRL(ctrl)) {
344                         rc = t_slot->hpc_ops->power_off_slot(t_slot);
345                         if (rc)
346                                 goto err_out_free_ctrl_slot;
347                 }
348         }
349
350         return 0;
351
352 err_out_free_ctrl_slot:
353         cleanup_slots(ctrl);
354 err_out_release_ctlr:
355         ctrl->hpc_ops->release_ctlr(ctrl);
356 err_out_none:
357         return -ENODEV;
358 }
359
360 static void pciehp_remove (struct pcie_device *dev)
361 {
362         struct controller *ctrl = get_service_data(dev);
363
364         cleanup_slots(ctrl);
365         ctrl->hpc_ops->release_ctlr(ctrl);
366 }
367
368 #ifdef CONFIG_PM
369 static int pciehp_suspend (struct pcie_device *dev)
370 {
371         dev_info(&dev->device, "%s ENTRY\n", __func__);
372         return 0;
373 }
374
375 static int pciehp_resume (struct pcie_device *dev)
376 {
377         dev_info(&dev->device, "%s ENTRY\n", __func__);
378         if (pciehp_force) {
379                 struct controller *ctrl = get_service_data(dev);
380                 struct slot *t_slot;
381                 u8 status;
382
383                 /* reinitialize the chipset's event detection logic */
384                 pcie_enable_notification(ctrl);
385
386                 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
387
388                 /* Check if slot is occupied */
389                 t_slot->hpc_ops->get_adapter_status(t_slot, &status);
390                 if (status)
391                         pciehp_enable_slot(t_slot);
392                 else
393                         pciehp_disable_slot(t_slot);
394         }
395         return 0;
396 }
397 #endif /* PM */
398
399 static struct pcie_port_service_driver hpdriver_portdrv = {
400         .name           = PCIE_MODULE_NAME,
401         .port_type      = PCIE_ANY_PORT,
402         .service        = PCIE_PORT_SERVICE_HP,
403
404         .probe          = pciehp_probe,
405         .remove         = pciehp_remove,
406
407 #ifdef  CONFIG_PM
408         .suspend        = pciehp_suspend,
409         .resume         = pciehp_resume,
410 #endif  /* PM */
411 };
412
413 static int __init pcied_init(void)
414 {
415         int retval = 0;
416
417         pciehp_firmware_init();
418         retval = pcie_port_service_register(&hpdriver_portdrv);
419         dbg("pcie_port_service_register = %d\n", retval);
420         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
421         if (retval)
422                 dbg("Failure to register service\n");
423         return retval;
424 }
425
426 static void __exit pcied_cleanup(void)
427 {
428         dbg("unload_pciehpd()\n");
429         pcie_port_service_unregister(&hpdriver_portdrv);
430         info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
431 }
432
433 module_init(pcied_init);
434 module_exit(pcied_cleanup);