pciehp: fix NULL dereference in interrupt handler
[linux-2.6/mini2440.git] / drivers / pci / hotplug / pciehp_ctrl.c
blob7ad8a7dbc1a48108833ab97a972ce1cbcb9cb10b
1 /*
2 * PCI Express Hot Plug Controller Driver
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
9 * All rights reserved.
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.
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.
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.
26 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
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"
39 static void interrupt_event_handler(struct work_struct *work);
41 static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
43 struct event_info *info;
45 info = kmalloc(sizeof(*info), GFP_ATOMIC);
46 if (!info)
47 return -ENOMEM;
49 info->event_type = event_type;
50 info->p_slot = p_slot;
51 INIT_WORK(&info->work, interrupt_event_handler);
53 schedule_work(&info->work);
55 return 0;
58 u8 pciehp_handle_attention_button(struct slot *p_slot)
60 u32 event_type;
62 /* Attention Button Change */
63 dbg("pciehp: Attention button interrupt received.\n");
66 * Button pressed - See if need to TAKE ACTION!!!
68 info("Button pressed on Slot(%s)\n", p_slot->name);
69 event_type = INT_BUTTON_PRESS;
71 queue_interrupt_event(p_slot, event_type);
73 return 0;
76 u8 pciehp_handle_switch_change(struct slot *p_slot)
78 u8 getstatus;
79 u32 event_type;
81 /* Switch Change */
82 dbg("pciehp: Switch interrupt received.\n");
84 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
85 if (getstatus) {
87 * Switch opened
89 info("Latch open on Slot(%s)\n", p_slot->name);
90 event_type = INT_SWITCH_OPEN;
91 } else {
93 * Switch closed
95 info("Latch close on Slot(%s)\n", p_slot->name);
96 event_type = INT_SWITCH_CLOSE;
99 queue_interrupt_event(p_slot, event_type);
101 return 1;
104 u8 pciehp_handle_presence_change(struct slot *p_slot)
106 u32 event_type;
107 u8 presence_save;
109 /* Presence Change */
110 dbg("pciehp: Presence/Notify input change.\n");
112 /* Switch is open, assume a presence change
113 * Save the presence state
115 p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
116 if (presence_save) {
118 * Card Present
120 info("Card present on Slot(%s)\n", p_slot->name);
121 event_type = INT_PRESENCE_ON;
122 } else {
124 * Not Present
126 info("Card not present on Slot(%s)\n", p_slot->name);
127 event_type = INT_PRESENCE_OFF;
130 queue_interrupt_event(p_slot, event_type);
132 return 1;
135 u8 pciehp_handle_power_fault(struct slot *p_slot)
137 u32 event_type;
139 /* power fault */
140 dbg("pciehp: Power fault interrupt received.\n");
142 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
144 * power fault Cleared
146 info("Power fault cleared on Slot(%s)\n", p_slot->name);
147 event_type = INT_POWER_FAULT_CLEAR;
148 } else {
150 * power fault
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);
157 queue_interrupt_event(p_slot, event_type);
159 return 1;
162 /* The following routines constitute the bulk of the
163 hotplug controller logic
166 static void set_slot_off(struct controller *ctrl, struct slot * pslot)
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;
177 if (PWR_LED(ctrl))
178 pslot->hpc_ops->green_led_off(pslot);
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;
190 * board_added - Called after a board has been added to the system.
191 * @p_slot: &slot where board is added
193 * Turns power on for the board.
194 * Configures board.
196 static int board_added(struct slot *p_slot)
198 int retval = 0;
199 struct controller *ctrl = p_slot->ctrl;
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);
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;
212 if (PWR_LED(ctrl))
213 p_slot->hpc_ops->green_led_blink(p_slot);
215 /* Wait for ~1 second */
216 msleep(1000);
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;
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;
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;
241 * Some PCI Express root ports require fixup after hot-plug operation.
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);
248 return 0;
250 err_exit:
251 set_slot_off(ctrl, p_slot);
252 return retval;
256 * remove_board - Turns off slot and LEDs
257 * @p_slot: slot where board is being removed
259 static int remove_board(struct slot *p_slot)
261 int retval = 0;
262 struct controller *ctrl = p_slot->ctrl;
264 retval = pciehp_unconfigure_device(p_slot);
265 if (retval)
266 return retval;
268 dbg("In %s, hp_slot = %d\n", __func__, p_slot->hp_slot);
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;
280 if (PWR_LED(ctrl))
281 /* turn off Green LED */
282 p_slot->hpc_ops->green_led_off(p_slot);
284 return 0;
287 struct power_work_info {
288 struct slot *p_slot;
289 struct work_struct work;
293 * pciehp_power_thread - handle pushbutton events
294 * @work: &struct work_struct describing work to be done
296 * Scheduled procedure to handle blocking stuff for the pushbuttons.
297 * Handles all pending events and exits.
299 static void pciehp_power_thread(struct work_struct *work)
301 struct power_work_info *info =
302 container_of(work, struct power_work_info, work);
303 struct slot *p_slot = info->p_slot;
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;
326 mutex_unlock(&p_slot->lock);
328 kfree(info);
331 void pciehp_queue_pushbutton_work(struct work_struct *work)
333 struct slot *p_slot = container_of(work, struct slot, work.work);
334 struct power_work_info *info;
336 info = kmalloc(sizeof(*info), GFP_KERNEL);
337 if (!info) {
338 err("%s: Cannot allocate memory\n", __func__);
339 return;
341 info->p_slot = p_slot;
342 INIT_WORK(&info->work, pciehp_power_thread);
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;
355 queue_work(pciehp_wq, &info->work);
356 out:
357 mutex_unlock(&p_slot->lock);
360 static int update_slot_info(struct slot *slot)
362 struct hotplug_slot_info *info;
363 int result;
365 info = kmalloc(sizeof(*info), GFP_KERNEL);
366 if (!info)
367 return -ENOMEM;
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));
374 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
375 kfree (info);
376 return result;
380 * Note: This function must be called with slot->lock held
382 static void handle_button_press_event(struct slot *p_slot)
384 struct controller *ctrl = p_slot->ctrl;
385 u8 getstatus;
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);
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);
405 schedule_delayed_work(&p_slot->work, 5*HZ);
406 break;
407 case BLINKINGOFF_STATE:
408 case BLINKINGON_STATE:
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
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);
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:
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
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;
447 * Note: This function must be called with slot->lock held
449 static void handle_surprise_event(struct slot *p_slot)
451 u8 getstatus;
452 struct power_work_info *info;
454 info = kmalloc(sizeof(*info), GFP_KERNEL);
455 if (!info) {
456 err("%s: Cannot allocate memory\n", __func__);
457 return;
459 info->p_slot = p_slot;
460 INIT_WORK(&info->work, pciehp_power_thread);
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;
468 queue_work(pciehp_wq, &info->work);
471 static void interrupt_event_handler(struct work_struct *work)
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;
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;
502 mutex_unlock(&p_slot->lock);
504 kfree(info);
507 int pciehp_enable_slot(struct slot *p_slot)
509 u8 getstatus = 0;
510 int rc;
512 /* Check to see if (latch closed, card present, power off) */
513 mutex_lock(&p_slot->ctrl->crit_sect);
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;
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;
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;
542 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
544 rc = board_added(p_slot);
545 if (rc) {
546 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
549 update_slot_info(p_slot);
551 mutex_unlock(&p_slot->ctrl->crit_sect);
552 return rc;
556 int pciehp_disable_slot(struct slot *p_slot)
558 u8 getstatus = 0;
559 int ret = 0;
561 if (!p_slot->ctrl)
562 return 1;
564 /* Check to see if (latch closed, card present, power on) */
565 mutex_lock(&p_slot->ctrl->crit_sect);
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;
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;
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;
597 ret = remove_board(p_slot);
598 update_slot_info(p_slot);
600 mutex_unlock(&p_slot->ctrl->crit_sect);
601 return ret;
604 int pciehp_sysfs_enable_slot(struct slot *p_slot)
606 int retval = -ENODEV;
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;
631 mutex_unlock(&p_slot->lock);
633 return retval;
636 int pciehp_sysfs_disable_slot(struct slot *p_slot)
638 int retval = -ENODEV;
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;
663 mutex_unlock(&p_slot->lock);
665 return retval;