pciehp: remove unnecessary php_ctlr
[sfrench/cifs-2.6.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
38 /* Global variables */
39 int pciehp_debug;
40 int pciehp_poll_mode;
41 int pciehp_poll_time;
42 int pciehp_force;
43 struct controller *pciehp_ctrl_list;
44
45 #define DRIVER_VERSION  "0.4"
46 #define DRIVER_AUTHOR   "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
47 #define DRIVER_DESC     "PCI Express Hot Plug Controller Driver"
48
49 MODULE_AUTHOR(DRIVER_AUTHOR);
50 MODULE_DESCRIPTION(DRIVER_DESC);
51 MODULE_LICENSE("GPL");
52
53 module_param(pciehp_debug, bool, 0644);
54 module_param(pciehp_poll_mode, bool, 0644);
55 module_param(pciehp_poll_time, int, 0644);
56 module_param(pciehp_force, bool, 0644);
57 MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
58 MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
59 MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
60 MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
61
62 #define PCIE_MODULE_NAME "pciehp"
63
64 static int pcie_start_thread (void);
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_address          (struct hotplug_slot *slot, u32 *value);
73 static int get_max_bus_speed    (struct hotplug_slot *slot, enum pci_bus_speed *value);
74 static int get_cur_bus_speed    (struct hotplug_slot *slot, enum pci_bus_speed *value);
75
76 static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
77         .owner =                THIS_MODULE,
78         .set_attention_status = set_attention_status,
79         .enable_slot =          enable_slot,
80         .disable_slot =         disable_slot,
81         .get_power_status =     get_power_status,
82         .get_attention_status = get_attention_status,
83         .get_latch_status =     get_latch_status,
84         .get_adapter_status =   get_adapter_status,
85         .get_address =          get_address,
86         .get_max_bus_speed =    get_max_bus_speed,
87         .get_cur_bus_speed =    get_cur_bus_speed,
88 };
89
90 /**
91  * release_slot - free up the memory used by a slot
92  * @hotplug_slot: slot to free
93  */
94 static void release_slot(struct hotplug_slot *hotplug_slot)
95 {
96         struct slot *slot = hotplug_slot->private;
97
98         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
99
100         kfree(slot->hotplug_slot->info);
101         kfree(slot->hotplug_slot);
102         kfree(slot);
103 }
104
105 static void make_slot_name(struct slot *slot)
106 {
107         snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
108                  slot->bus, slot->number);
109 }
110
111 static int init_slots(struct controller *ctrl)
112 {
113         struct slot *slot;
114         struct hotplug_slot *hotplug_slot;
115         struct hotplug_slot_info *info;
116         int retval = -ENOMEM;
117         int i;
118
119         for (i = 0; i < ctrl->num_slots; i++) {
120                 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
121                 if (!slot)
122                         goto error;
123
124                 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
125                 if (!hotplug_slot)
126                         goto error_slot;
127                 slot->hotplug_slot = hotplug_slot;
128
129                 info = kzalloc(sizeof(*info), GFP_KERNEL);
130                 if (!info)
131                         goto error_hpslot;
132                 hotplug_slot->info = info;
133
134                 hotplug_slot->name = slot->name;
135
136                 slot->hp_slot = i;
137                 slot->ctrl = ctrl;
138                 slot->bus = ctrl->pci_dev->subordinate->number;
139                 slot->device = ctrl->slot_device_offset + i;
140                 slot->hpc_ops = ctrl->hpc_ops;
141                 slot->number = ctrl->first_slot;
142
143                 /* register this slot with the hotplug pci core */
144                 hotplug_slot->private = slot;
145                 hotplug_slot->release = &release_slot;
146                 make_slot_name(slot);
147                 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
148
149                 get_power_status(hotplug_slot, &info->power_status);
150                 get_attention_status(hotplug_slot, &info->attention_status);
151                 get_latch_status(hotplug_slot, &info->latch_status);
152                 get_adapter_status(hotplug_slot, &info->adapter_status);
153
154                 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
155                     "slot_device_offset=%x\n", slot->bus, slot->device,
156                     slot->hp_slot, slot->number, ctrl->slot_device_offset);
157                 retval = pci_hp_register(hotplug_slot);
158                 if (retval) {
159                         err ("pci_hp_register failed with error %d\n", retval);
160                         goto error_info;
161                 }
162
163                 list_add(&slot->slot_list, &ctrl->slot_list);
164         }
165
166         return 0;
167 error_info:
168         kfree(info);
169 error_hpslot:
170         kfree(hotplug_slot);
171 error_slot:
172         kfree(slot);
173 error:
174         return retval;
175 }
176
177 static void cleanup_slots(struct controller *ctrl)
178 {
179         struct list_head *tmp;
180         struct list_head *next;
181         struct slot *slot;
182
183         list_for_each_safe(tmp, next, &ctrl->slot_list) {
184                 slot = list_entry(tmp, struct slot, slot_list);
185                 list_del(&slot->slot_list);
186                 pci_hp_deregister(slot->hotplug_slot);
187         }
188 }
189
190 /*
191  * set_attention_status - Turns the Amber LED for a slot on, off or blink
192  */
193 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
194 {
195         struct slot *slot = hotplug_slot->private;
196
197         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
198
199         hotplug_slot->info->attention_status = status;
200         
201         if (ATTN_LED(slot->ctrl->ctrlcap)) 
202                 slot->hpc_ops->set_attention_status(slot, status);
203
204         return 0;
205 }
206
207
208 static int enable_slot(struct hotplug_slot *hotplug_slot)
209 {
210         struct slot *slot = hotplug_slot->private;
211
212         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
213
214         return pciehp_enable_slot(slot);
215 }
216
217
218 static int disable_slot(struct hotplug_slot *hotplug_slot)
219 {
220         struct slot *slot = hotplug_slot->private;
221
222         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
223
224         return pciehp_disable_slot(slot);
225 }
226
227 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
228 {
229         struct slot *slot = hotplug_slot->private;
230         int retval;
231
232         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
233
234         retval = slot->hpc_ops->get_power_status(slot, value);
235         if (retval < 0)
236                 *value = hotplug_slot->info->power_status;
237
238         return 0;
239 }
240
241 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
242 {
243         struct slot *slot = hotplug_slot->private;
244         int retval;
245
246         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
247
248         retval = slot->hpc_ops->get_attention_status(slot, value);
249         if (retval < 0)
250                 *value = hotplug_slot->info->attention_status;
251
252         return 0;
253 }
254
255 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
256 {
257         struct slot *slot = hotplug_slot->private;
258         int retval;
259
260         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
261
262         retval = slot->hpc_ops->get_latch_status(slot, value);
263         if (retval < 0)
264                 *value = hotplug_slot->info->latch_status;
265
266         return 0;
267 }
268
269 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
270 {
271         struct slot *slot = hotplug_slot->private;
272         int retval;
273
274         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
275
276         retval = slot->hpc_ops->get_adapter_status(slot, value);
277         if (retval < 0)
278                 *value = hotplug_slot->info->adapter_status;
279
280         return 0;
281 }
282
283 static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
284 {
285         struct slot *slot = hotplug_slot->private;
286         struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
287
288         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
289
290         *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
291
292         return 0;
293 }
294
295 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
296 {
297         struct slot *slot = hotplug_slot->private;
298         int retval;
299
300         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
301         
302         retval = slot->hpc_ops->get_max_bus_speed(slot, value);
303         if (retval < 0)
304                 *value = PCI_SPEED_UNKNOWN;
305
306         return 0;
307 }
308
309 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
310 {
311         struct slot *slot = hotplug_slot->private;
312         int retval;
313
314         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
315         
316         retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
317         if (retval < 0)
318                 *value = PCI_SPEED_UNKNOWN;
319
320         return 0;
321 }
322
323 static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
324 {
325         int rc;
326         struct controller *ctrl;
327         struct slot *t_slot;
328         u8 value;
329         struct pci_dev *pdev;
330         
331         ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
332         if (!ctrl) {
333                 err("%s : out of memory\n", __FUNCTION__);
334                 goto err_out_none;
335         }
336         INIT_LIST_HEAD(&ctrl->slot_list);
337
338         pdev = dev->port;
339         ctrl->pci_dev = pdev;
340
341         rc = pcie_init(ctrl, dev);
342         if (rc) {
343                 dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
344                 goto err_out_free_ctrl;
345         }
346
347         pci_set_drvdata(pdev, ctrl);
348
349         ctrl->pci_bus = kmalloc(sizeof(*ctrl->pci_bus), GFP_KERNEL);
350         if (!ctrl->pci_bus) {
351                 err("%s: out of memory\n", __FUNCTION__);
352                 rc = -ENOMEM;
353                 goto err_out_unmap_mmio_region;
354         }
355         memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
356         ctrl->bus = pdev->bus->number;  /* ctrl bus */
357         ctrl->slot_bus = pdev->subordinate->number;  /* bus controlled by this HPC */
358
359         ctrl->device = PCI_SLOT(pdev->devfn);
360         ctrl->function = PCI_FUNC(pdev->devfn);
361         dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", __FUNCTION__,
362                 ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
363
364         /* Setup the slot information structures */
365         rc = init_slots(ctrl);
366         if (rc) {
367                 err(msg_initialization_err, 6);
368                 goto err_out_free_ctrl_slot;
369         }
370
371         t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
372
373         /*      Finish setting up the hot plug ctrl device */
374         ctrl->next_event = 0;
375
376         if (!pciehp_ctrl_list) {
377                 pciehp_ctrl_list = ctrl;
378                 ctrl->next = NULL;
379         } else {
380                 ctrl->next = pciehp_ctrl_list;
381                 pciehp_ctrl_list = ctrl;
382         }
383
384         /* Wait for exclusive access to hardware */
385         mutex_lock(&ctrl->ctrl_lock);
386
387         t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
388         
389         if ((POWER_CTRL(ctrl->ctrlcap)) && !value) {
390                 rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
391                 if (rc) {
392                         /* Done with exclusive hardware access */
393                         mutex_unlock(&ctrl->ctrl_lock);
394                         goto err_out_free_ctrl_slot;
395                 } else
396                         /* Wait for the command to complete */
397                         wait_for_ctrl_irq (ctrl);
398         }
399
400         /* Done with exclusive hardware access */
401         mutex_unlock(&ctrl->ctrl_lock);
402
403         return 0;
404
405 err_out_free_ctrl_slot:
406         cleanup_slots(ctrl);
407         kfree(ctrl->pci_bus);
408 err_out_unmap_mmio_region:
409         ctrl->hpc_ops->release_ctlr(ctrl);
410 err_out_free_ctrl:
411         kfree(ctrl);
412 err_out_none:
413         return -ENODEV;
414 }
415
416
417 static int pcie_start_thread(void)
418 {
419         int retval = 0;
420         
421         dbg("Initialize + Start the notification/polling mechanism \n");
422
423         retval = pciehp_event_start_thread();
424         if (retval) {
425                 dbg("pciehp_event_start_thread() failed\n");
426                 return retval;
427         }
428
429         return retval;
430 }
431
432 static void __exit unload_pciehpd(void)
433 {
434         struct controller *ctrl;
435         struct controller *tctrl;
436
437         ctrl = pciehp_ctrl_list;
438
439         while (ctrl) {
440                 cleanup_slots(ctrl);
441
442                 kfree (ctrl->pci_bus);
443
444                 ctrl->hpc_ops->release_ctlr(ctrl);
445
446                 tctrl = ctrl;
447                 ctrl = ctrl->next;
448
449                 kfree(tctrl);
450         }
451
452         /* Stop the notification mechanism */
453         pciehp_event_stop_thread();
454
455 }
456
457 static void pciehp_remove (struct pcie_device *device)
458 {
459         /* XXX - Needs to be adapted to device driver model */
460 }
461
462 #ifdef CONFIG_PM
463 static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
464 {
465         printk("%s ENTRY\n", __FUNCTION__);     
466         return 0;
467 }
468
469 static int pciehp_resume (struct pcie_device *dev)
470 {
471         printk("%s ENTRY\n", __FUNCTION__);     
472         return 0;
473 }
474 #endif
475
476 static struct pcie_port_service_id port_pci_ids[] = { { 
477         .vendor = PCI_ANY_ID, 
478         .device = PCI_ANY_ID,
479         .port_type = PCIE_ANY_PORT,
480         .service_type = PCIE_PORT_SERVICE_HP,
481         .driver_data =  0, 
482         }, { /* end: all zeroes */ }
483 };
484 static const char device_name[] = "hpdriver";
485
486 static struct pcie_port_service_driver hpdriver_portdrv = {
487         .name           = (char *)device_name,
488         .id_table       = &port_pci_ids[0],
489
490         .probe          = pciehp_probe,
491         .remove         = pciehp_remove,
492
493 #ifdef  CONFIG_PM
494         .suspend        = pciehp_suspend,
495         .resume         = pciehp_resume,
496 #endif  /* PM */
497 };
498
499 static int __init pcied_init(void)
500 {
501         int retval = 0;
502
503 #ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
504         pciehp_poll_mode = 1;
505 #endif
506
507         retval = pcie_start_thread();
508         if (retval)
509                 goto error_hpc_init;
510
511         retval = pcie_port_service_register(&hpdriver_portdrv);
512         dbg("pcie_port_service_register = %d\n", retval);
513         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
514         if (retval)
515                 dbg("%s: Failure to register service\n", __FUNCTION__);
516
517 error_hpc_init:
518         if (retval) {
519                 pciehp_event_stop_thread();
520         };
521
522         return retval;
523 }
524
525 static void __exit pcied_cleanup(void)
526 {
527         dbg("unload_pciehpd()\n");
528         unload_pciehpd();
529
530         pcie_port_service_unregister(&hpdriver_portdrv);
531
532         info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
533 }
534
535 module_init(pcied_init);
536 module_exit(pcied_cleanup);