]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pci/hotplug/pciehp_ctrl.c
pciehp: fix slow probing
[karo-tx-linux.git] / drivers / pci / hotplug / pciehp_ctrl.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/kernel.h>
32 #include <linux/types.h>
33 #include <linux/smp_lock.h>
34 #include <linux/pci.h>
35 #include <linux/workqueue.h>
36 #include "../pci.h"
37 #include "pciehp.h"
38
39 static void interrupt_event_handler(struct work_struct *work);
40
41 static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
42 {
43         struct event_info *info;
44
45         info = kmalloc(sizeof(*info), GFP_ATOMIC);
46         if (!info)
47                 return -ENOMEM;
48
49         info->event_type = event_type;
50         info->p_slot = p_slot;
51         INIT_WORK(&info->work, interrupt_event_handler);
52
53         schedule_work(&info->work);
54
55         return 0;
56 }
57
58 u8 pciehp_handle_attention_button(struct slot *p_slot)
59 {
60         u32 event_type;
61
62         /* Attention Button Change */
63         dbg("pciehp:  Attention button interrupt received.\n");
64
65         /*
66          *  Button pressed - See if need to TAKE ACTION!!!
67          */
68         info("Button pressed on Slot(%s)\n", p_slot->name);
69         event_type = INT_BUTTON_PRESS;
70
71         queue_interrupt_event(p_slot, event_type);
72
73         return 0;
74 }
75
76 u8 pciehp_handle_switch_change(struct slot *p_slot)
77 {
78         u8 getstatus;
79         u32 event_type;
80
81         /* Switch Change */
82         dbg("pciehp:  Switch interrupt received.\n");
83
84         p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
85         if (getstatus) {
86                 /*
87                  * Switch opened
88                  */
89                 info("Latch open on Slot(%s)\n", p_slot->name);
90                 event_type = INT_SWITCH_OPEN;
91         } else {
92                 /*
93                  *  Switch closed
94                  */
95                 info("Latch close on Slot(%s)\n", p_slot->name);
96                 event_type = INT_SWITCH_CLOSE;
97         }
98
99         queue_interrupt_event(p_slot, event_type);
100
101         return 1;
102 }
103
104 u8 pciehp_handle_presence_change(struct slot *p_slot)
105 {
106         u32 event_type;
107         u8 presence_save;
108
109         /* Presence Change */
110         dbg("pciehp:  Presence/Notify input change.\n");
111
112         /* Switch is open, assume a presence change
113          * Save the presence state
114          */
115         p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
116         if (presence_save) {
117                 /*
118                  * Card Present
119                  */
120                 info("Card present on Slot(%s)\n", p_slot->name);
121                 event_type = INT_PRESENCE_ON;
122         } else {
123                 /*
124                  * Not Present
125                  */
126                 info("Card not present on Slot(%s)\n", p_slot->name);
127                 event_type = INT_PRESENCE_OFF;
128         }
129
130         queue_interrupt_event(p_slot, event_type);
131
132         return 1;
133 }
134
135 u8 pciehp_handle_power_fault(struct slot *p_slot)
136 {
137         u32 event_type;
138
139         /* power fault */
140         dbg("pciehp:  Power fault interrupt received.\n");
141
142         if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
143                 /*
144                  * power fault Cleared
145                  */
146                 info("Power fault cleared on Slot(%s)\n", p_slot->name);
147                 event_type = INT_POWER_FAULT_CLEAR;
148         } else {
149                 /*
150                  *   power fault
151                  */
152                 info("Power fault on Slot(%s)\n", p_slot->name);
153                 event_type = INT_POWER_FAULT;
154                 info("power fault bit %x set\n", 0);
155         }
156
157         queue_interrupt_event(p_slot, event_type);
158
159         return 1;
160 }
161
162 /* The following routines constitute the bulk of the
163    hotplug controller logic
164  */
165
166 static void set_slot_off(struct controller *ctrl, struct slot * pslot)
167 {
168         /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
169         if (POWER_CTRL(ctrl)) {
170                 if (pslot->hpc_ops->power_off_slot(pslot)) {
171                         err("%s: Issue of Slot Power Off command failed\n",
172                             __func__);
173                         return;
174                 }
175         }
176
177         if (PWR_LED(ctrl))
178                 pslot->hpc_ops->green_led_off(pslot);
179
180         if (ATTN_LED(ctrl)) {
181                 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
182                         err("%s: Issue of Set Attention Led command failed\n",
183                             __func__);
184                         return;
185                 }
186         }
187 }
188
189 /**
190  * board_added - Called after a board has been added to the system.
191  * @p_slot: &slot where board is added
192  *
193  * Turns power on for the board.
194  * Configures board.
195  */
196 static int board_added(struct slot *p_slot)
197 {
198         int retval = 0;
199         struct controller *ctrl = p_slot->ctrl;
200
201         dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
202                         __func__, p_slot->device,
203                         ctrl->slot_device_offset, p_slot->hp_slot);
204
205         if (POWER_CTRL(ctrl)) {
206                 /* Power on slot */
207                 retval = p_slot->hpc_ops->power_on_slot(p_slot);
208                 if (retval)
209                         return retval;
210         }
211
212         if (PWR_LED(ctrl))
213                 p_slot->hpc_ops->green_led_blink(p_slot);
214
215         /* Wait for ~1 second */
216         msleep(1000);
217
218         /* Check link training status */
219         retval = p_slot->hpc_ops->check_lnk_status(ctrl);
220         if (retval) {
221                 err("%s: Failed to check link status\n", __func__);
222                 set_slot_off(ctrl, p_slot);
223                 return retval;
224         }
225
226         /* Check for a power fault */
227         if (p_slot->hpc_ops->query_power_fault(p_slot)) {
228                 dbg("%s: power fault detected\n", __func__);
229                 retval = POWER_FAILURE;
230                 goto err_exit;
231         }
232
233         retval = pciehp_configure_device(p_slot);
234         if (retval) {
235                 err("Cannot add device 0x%x:%x\n", p_slot->bus,
236                     p_slot->device);
237                 goto err_exit;
238         }
239
240         /*
241          * Some PCI Express root ports require fixup after hot-plug operation.
242          */
243         if (pcie_mch_quirk)
244                 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
245         if (PWR_LED(ctrl))
246                 p_slot->hpc_ops->green_led_on(p_slot);
247
248         return 0;
249
250 err_exit:
251         set_slot_off(ctrl, p_slot);
252         return retval;
253 }
254
255 /**
256  * remove_board - Turns off slot and LEDs
257  * @p_slot: slot where board is being removed
258  */
259 static int remove_board(struct slot *p_slot)
260 {
261         int retval = 0;
262         struct controller *ctrl = p_slot->ctrl;
263
264         retval = pciehp_unconfigure_device(p_slot);
265         if (retval)
266                 return retval;
267
268         dbg("In %s, hp_slot = %d\n", __func__, p_slot->hp_slot);
269
270         if (POWER_CTRL(ctrl)) {
271                 /* power off slot */
272                 retval = p_slot->hpc_ops->power_off_slot(p_slot);
273                 if (retval) {
274                         err("%s: Issue of Slot Disable command failed\n",
275                             __func__);
276                         return retval;
277                 }
278         }
279
280         if (PWR_LED(ctrl))
281                 /* turn off Green LED */
282                 p_slot->hpc_ops->green_led_off(p_slot);
283
284         return 0;
285 }
286
287 struct power_work_info {
288         struct slot *p_slot;
289         struct work_struct work;
290 };
291
292 /**
293  * pciehp_power_thread - handle pushbutton events
294  * @work: &struct work_struct describing work to be done
295  *
296  * Scheduled procedure to handle blocking stuff for the pushbuttons.
297  * Handles all pending events and exits.
298  */
299 static void pciehp_power_thread(struct work_struct *work)
300 {
301         struct power_work_info *info =
302                 container_of(work, struct power_work_info, work);
303         struct slot *p_slot = info->p_slot;
304
305         mutex_lock(&p_slot->lock);
306         switch (p_slot->state) {
307         case POWEROFF_STATE:
308                 mutex_unlock(&p_slot->lock);
309                 dbg("%s: disabling bus:device(%x:%x)\n",
310                     __func__, p_slot->bus, p_slot->device);
311                 pciehp_disable_slot(p_slot);
312                 mutex_lock(&p_slot->lock);
313                 p_slot->state = STATIC_STATE;
314                 break;
315         case POWERON_STATE:
316                 mutex_unlock(&p_slot->lock);
317                 if (pciehp_enable_slot(p_slot) &&
318                     PWR_LED(p_slot->ctrl))
319                         p_slot->hpc_ops->green_led_off(p_slot);
320                 mutex_lock(&p_slot->lock);
321                 p_slot->state = STATIC_STATE;
322                 break;
323         default:
324                 break;
325         }
326         mutex_unlock(&p_slot->lock);
327
328         kfree(info);
329 }
330
331 void pciehp_queue_pushbutton_work(struct work_struct *work)
332 {
333         struct slot *p_slot = container_of(work, struct slot, work.work);
334         struct power_work_info *info;
335
336         info = kmalloc(sizeof(*info), GFP_KERNEL);
337         if (!info) {
338                 err("%s: Cannot allocate memory\n", __func__);
339                 return;
340         }
341         info->p_slot = p_slot;
342         INIT_WORK(&info->work, pciehp_power_thread);
343
344         mutex_lock(&p_slot->lock);
345         switch (p_slot->state) {
346         case BLINKINGOFF_STATE:
347                 p_slot->state = POWEROFF_STATE;
348                 break;
349         case BLINKINGON_STATE:
350                 p_slot->state = POWERON_STATE;
351                 break;
352         default:
353                 goto out;
354         }
355         queue_work(pciehp_wq, &info->work);
356  out:
357         mutex_unlock(&p_slot->lock);
358 }
359
360 static int update_slot_info(struct slot *slot)
361 {
362         struct hotplug_slot_info *info;
363         int result;
364
365         info = kmalloc(sizeof(*info), GFP_KERNEL);
366         if (!info)
367                 return -ENOMEM;
368
369         slot->hpc_ops->get_power_status(slot, &(info->power_status));
370         slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
371         slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
372         slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
373
374         result = pci_hp_change_slot_info(slot->hotplug_slot, info);
375         kfree (info);
376         return result;
377 }
378
379 /*
380  * Note: This function must be called with slot->lock held
381  */
382 static void handle_button_press_event(struct slot *p_slot)
383 {
384         struct controller *ctrl = p_slot->ctrl;
385         u8 getstatus;
386
387         switch (p_slot->state) {
388         case STATIC_STATE:
389                 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
390                 if (getstatus) {
391                         p_slot->state = BLINKINGOFF_STATE;
392                         info("PCI slot #%s - powering off due to button "
393                              "press.\n", p_slot->name);
394                 } else {
395                         p_slot->state = BLINKINGON_STATE;
396                         info("PCI slot #%s - powering on due to button "
397                              "press.\n", p_slot->name);
398                 }
399                 /* blink green LED and turn off amber */
400                 if (PWR_LED(ctrl))
401                         p_slot->hpc_ops->green_led_blink(p_slot);
402                 if (ATTN_LED(ctrl))
403                         p_slot->hpc_ops->set_attention_status(p_slot, 0);
404
405                 schedule_delayed_work(&p_slot->work, 5*HZ);
406                 break;
407         case BLINKINGOFF_STATE:
408         case BLINKINGON_STATE:
409                 /*
410                  * Cancel if we are still blinking; this means that we
411                  * press the attention again before the 5 sec. limit
412                  * expires to cancel hot-add or hot-remove
413                  */
414                 info("Button cancel on Slot(%s)\n", p_slot->name);
415                 dbg("%s: button cancel\n", __func__);
416                 cancel_delayed_work(&p_slot->work);
417                 if (p_slot->state == BLINKINGOFF_STATE) {
418                         if (PWR_LED(ctrl))
419                                 p_slot->hpc_ops->green_led_on(p_slot);
420                 } else {
421                         if (PWR_LED(ctrl))
422                                 p_slot->hpc_ops->green_led_off(p_slot);
423                 }
424                 if (ATTN_LED(ctrl))
425                         p_slot->hpc_ops->set_attention_status(p_slot, 0);
426                 info("PCI slot #%s - action canceled due to button press\n",
427                      p_slot->name);
428                 p_slot->state = STATIC_STATE;
429                 break;
430         case POWEROFF_STATE:
431         case POWERON_STATE:
432                 /*
433                  * Ignore if the slot is on power-on or power-off state;
434                  * this means that the previous attention button action
435                  * to hot-add or hot-remove is undergoing
436                  */
437                 info("Button ignore on Slot(%s)\n", p_slot->name);
438                 update_slot_info(p_slot);
439                 break;
440         default:
441                 warn("Not a valid state\n");
442                 break;
443         }
444 }
445
446 /*
447  * Note: This function must be called with slot->lock held
448  */
449 static void handle_surprise_event(struct slot *p_slot)
450 {
451         u8 getstatus;
452         struct power_work_info *info;
453
454         info = kmalloc(sizeof(*info), GFP_KERNEL);
455         if (!info) {
456                 err("%s: Cannot allocate memory\n", __func__);
457                 return;
458         }
459         info->p_slot = p_slot;
460         INIT_WORK(&info->work, pciehp_power_thread);
461
462         p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
463         if (!getstatus)
464                 p_slot->state = POWEROFF_STATE;
465         else
466                 p_slot->state = POWERON_STATE;
467
468         queue_work(pciehp_wq, &info->work);
469 }
470
471 static void interrupt_event_handler(struct work_struct *work)
472 {
473         struct event_info *info = container_of(work, struct event_info, work);
474         struct slot *p_slot = info->p_slot;
475         struct controller *ctrl = p_slot->ctrl;
476
477         mutex_lock(&p_slot->lock);
478         switch (info->event_type) {
479         case INT_BUTTON_PRESS:
480                 handle_button_press_event(p_slot);
481                 break;
482         case INT_POWER_FAULT:
483                 if (!POWER_CTRL(ctrl))
484                         break;
485                 if (ATTN_LED(ctrl))
486                         p_slot->hpc_ops->set_attention_status(p_slot, 1);
487                 if (PWR_LED(ctrl))
488                         p_slot->hpc_ops->green_led_off(p_slot);
489                 break;
490         case INT_PRESENCE_ON:
491         case INT_PRESENCE_OFF:
492                 if (!HP_SUPR_RM(ctrl))
493                         break;
494                 dbg("Surprise Removal\n");
495                 update_slot_info(p_slot);
496                 handle_surprise_event(p_slot);
497                 break;
498         default:
499                 update_slot_info(p_slot);
500                 break;
501         }
502         mutex_unlock(&p_slot->lock);
503
504         kfree(info);
505 }
506
507 int pciehp_enable_slot(struct slot *p_slot)
508 {
509         u8 getstatus = 0;
510         int rc;
511
512         /* Check to see if (latch closed, card present, power off) */
513         mutex_lock(&p_slot->ctrl->crit_sect);
514
515         rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
516         if (rc || !getstatus) {
517                 info("%s: no adapter on slot(%s)\n", __func__,
518                      p_slot->name);
519                 mutex_unlock(&p_slot->ctrl->crit_sect);
520                 return -ENODEV;
521         }
522         if (MRL_SENS(p_slot->ctrl)) {
523                 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
524                 if (rc || getstatus) {
525                         info("%s: latch open on slot(%s)\n", __func__,
526                              p_slot->name);
527                         mutex_unlock(&p_slot->ctrl->crit_sect);
528                         return -ENODEV;
529                 }
530         }
531
532         if (POWER_CTRL(p_slot->ctrl)) {
533                 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
534                 if (rc || getstatus) {
535                         info("%s: already enabled on slot(%s)\n", __func__,
536                              p_slot->name);
537                         mutex_unlock(&p_slot->ctrl->crit_sect);
538                         return -EINVAL;
539                 }
540         }
541
542         p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
543
544         rc = board_added(p_slot);
545         if (rc) {
546                 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
547         }
548
549         update_slot_info(p_slot);
550
551         mutex_unlock(&p_slot->ctrl->crit_sect);
552         return rc;
553 }
554
555
556 int pciehp_disable_slot(struct slot *p_slot)
557 {
558         u8 getstatus = 0;
559         int ret = 0;
560
561         if (!p_slot->ctrl)
562                 return 1;
563
564         /* Check to see if (latch closed, card present, power on) */
565         mutex_lock(&p_slot->ctrl->crit_sect);
566
567         if (!HP_SUPR_RM(p_slot->ctrl)) {
568                 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
569                 if (ret || !getstatus) {
570                         info("%s: no adapter on slot(%s)\n", __func__,
571                              p_slot->name);
572                         mutex_unlock(&p_slot->ctrl->crit_sect);
573                         return -ENODEV;
574                 }
575         }
576
577         if (MRL_SENS(p_slot->ctrl)) {
578                 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
579                 if (ret || getstatus) {
580                         info("%s: latch open on slot(%s)\n", __func__,
581                              p_slot->name);
582                         mutex_unlock(&p_slot->ctrl->crit_sect);
583                         return -ENODEV;
584                 }
585         }
586
587         if (POWER_CTRL(p_slot->ctrl)) {
588                 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
589                 if (ret || !getstatus) {
590                         info("%s: already disabled slot(%s)\n", __func__,
591                              p_slot->name);
592                         mutex_unlock(&p_slot->ctrl->crit_sect);
593                         return -EINVAL;
594                 }
595         }
596
597         ret = remove_board(p_slot);
598         update_slot_info(p_slot);
599
600         mutex_unlock(&p_slot->ctrl->crit_sect);
601         return ret;
602 }
603
604 int pciehp_sysfs_enable_slot(struct slot *p_slot)
605 {
606         int retval = -ENODEV;
607
608         mutex_lock(&p_slot->lock);
609         switch (p_slot->state) {
610         case BLINKINGON_STATE:
611                 cancel_delayed_work(&p_slot->work);
612         case STATIC_STATE:
613                 p_slot->state = POWERON_STATE;
614                 mutex_unlock(&p_slot->lock);
615                 retval = pciehp_enable_slot(p_slot);
616                 mutex_lock(&p_slot->lock);
617                 p_slot->state = STATIC_STATE;
618                 break;
619         case POWERON_STATE:
620                 info("Slot %s is already in powering on state\n",
621                      p_slot->name);
622                 break;
623         case BLINKINGOFF_STATE:
624         case POWEROFF_STATE:
625                 info("Already enabled on slot %s\n", p_slot->name);
626                 break;
627         default:
628                 err("Not a valid state on slot %s\n", p_slot->name);
629                 break;
630         }
631         mutex_unlock(&p_slot->lock);
632
633         return retval;
634 }
635
636 int pciehp_sysfs_disable_slot(struct slot *p_slot)
637 {
638         int retval = -ENODEV;
639
640         mutex_lock(&p_slot->lock);
641         switch (p_slot->state) {
642         case BLINKINGOFF_STATE:
643                 cancel_delayed_work(&p_slot->work);
644         case STATIC_STATE:
645                 p_slot->state = POWEROFF_STATE;
646                 mutex_unlock(&p_slot->lock);
647                 retval = pciehp_disable_slot(p_slot);
648                 mutex_lock(&p_slot->lock);
649                 p_slot->state = STATIC_STATE;
650                 break;
651         case POWEROFF_STATE:
652                 info("Slot %s is already in powering off state\n",
653                      p_slot->name);
654                 break;
655         case BLINKINGON_STATE:
656         case POWERON_STATE:
657                 info("Already disabled on slot %s\n", p_slot->name);
658                 break;
659         default:
660                 err("Not a valid state on slot %s\n", p_slot->name);
661                 break;
662         }
663         mutex_unlock(&p_slot->lock);
664
665         return retval;
666 }