]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/hotplug/pciehp_ctrl.c
Merge branch 'drm-intel-next' of git://anongit.freedesktop.org/git/drm-intel into...
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / hotplug / pciehp_ctrl.c
CommitLineData
1da177e4
LT
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 *
8cf4c195 26 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
1da177e4
LT
27 *
28 */
29
1da177e4
LT
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/types.h>
5a0e3ad6 33#include <linux/slab.h>
68db9bc8 34#include <linux/pm_runtime.h>
1da177e4
LT
35#include <linux/pci.h>
36#include "../pci.h"
37#include "pciehp.h"
1da177e4 38
5d386e1a 39static void interrupt_event_handler(struct work_struct *work);
1da177e4 40
4f092fec 41void pciehp_queue_interrupt_event(struct slot *p_slot, u32 event_type)
49ed2b49 42{
5d386e1a
KK
43 struct event_info *info;
44
45 info = kmalloc(sizeof(*info), GFP_ATOMIC);
7d852b68
BH
46 if (!info) {
47 ctrl_err(p_slot->ctrl, "dropped event %d (ENOMEM)\n", event_type);
48 return;
49 }
5d386e1a 50
7d852b68 51 INIT_WORK(&info->work, interrupt_event_handler);
5d386e1a
KK
52 info->event_type = event_type;
53 info->p_slot = p_slot;
c2be6f93 54 queue_work(p_slot->wq, &info->work);
49ed2b49
KK
55}
56
36ed27b0 57/* The following routines constitute the bulk of the
1da177e4
LT
58 hotplug controller logic
59 */
60
3c78bc61 61static void set_slot_off(struct controller *ctrl, struct slot *pslot)
1da177e4 62{
1da177e4 63 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
ae416e6b 64 if (POWER_CTRL(ctrl)) {
6dae6202
BH
65 pciehp_power_off_slot(pslot);
66
c7b4fee3
KK
67 /*
68 * After turning power off, we must wait for at least 1 second
69 * before taking any action that relies on power having been
70 * removed from the slot/adapter.
71 */
72 msleep(1000);
1da177e4
LT
73 }
74
af9ab791
BH
75 pciehp_green_led_off(pslot);
76 pciehp_set_attention_status(pslot, 1);
1da177e4
LT
77}
78
79/**
80 * board_added - Called after a board has been added to the system.
26e6c66e 81 * @p_slot: &slot where board is added
1da177e4 82 *
26e6c66e
RD
83 * Turns power on for the board.
84 * Configures board.
1da177e4 85 */
ed6cbcf2 86static int board_added(struct slot *p_slot)
1da177e4 87{
44ef4cef 88 int retval = 0;
ca22a5e4 89 struct controller *ctrl = p_slot->ctrl;
385e2491 90 struct pci_bus *parent = ctrl->pcie->port->subordinate;
1da177e4 91
ae416e6b 92 if (POWER_CTRL(ctrl)) {
1da177e4 93 /* Power on slot */
82a9e79e 94 retval = pciehp_power_on_slot(p_slot);
44ef4cef
KK
95 if (retval)
96 return retval;
1da177e4 97 }
36ed27b0 98
af9ab791 99 pciehp_green_led_blink(p_slot);
1da177e4 100
44ef4cef 101 /* Check link training status */
68db9bc8 102 pm_runtime_get_sync(&ctrl->pcie->port->dev);
82a9e79e 103 retval = pciehp_check_link_status(ctrl);
44ef4cef 104 if (retval) {
18b341b7 105 ctrl_err(ctrl, "Failed to check link status\n");
5651c48c 106 goto err_exit;
1da177e4
LT
107 }
108
1da177e4 109 /* Check for a power fault */
5651c48c 110 if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
6e49b304 111 ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(p_slot));
d9fb42a8 112 retval = -EIO;
71b720c0 113 goto err_exit;
1da177e4
LT
114 }
115
44ef4cef
KK
116 retval = pciehp_configure_device(p_slot);
117 if (retval) {
d689f7eb
KK
118 ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
119 pci_domain_nr(parent), parent->number);
50277c8b
YW
120 if (retval != -EEXIST)
121 goto err_exit;
71b720c0 122 }
68db9bc8 123 pm_runtime_put(&ctrl->pcie->port->dev);
1da177e4 124
af9ab791 125 pciehp_green_led_on(p_slot);
8b7c8b46 126 pciehp_set_attention_status(p_slot, 0);
1da177e4 127 return 0;
71b720c0
RS
128
129err_exit:
68db9bc8 130 pm_runtime_put(&ctrl->pcie->port->dev);
71b720c0 131 set_slot_off(ctrl, p_slot);
44ef4cef 132 return retval;
1da177e4
LT
133}
134
1da177e4 135/**
26e6c66e
RD
136 * remove_board - Turns off slot and LEDs
137 * @p_slot: slot where board is being removed
1da177e4 138 */
ed6cbcf2 139static int remove_board(struct slot *p_slot)
1da177e4 140{
6dae6202 141 int retval;
ca22a5e4 142 struct controller *ctrl = p_slot->ctrl;
1da177e4 143
68db9bc8 144 pm_runtime_get_sync(&ctrl->pcie->port->dev);
44ef4cef 145 retval = pciehp_unconfigure_device(p_slot);
68db9bc8 146 pm_runtime_put(&ctrl->pcie->port->dev);
44ef4cef
KK
147 if (retval)
148 return retval;
1da177e4 149
ae416e6b 150 if (POWER_CTRL(ctrl)) {
6dae6202
BH
151 pciehp_power_off_slot(p_slot);
152
c7b4fee3
KK
153 /*
154 * After turning power off, we must wait for at least 1 second
155 * before taking any action that relies on power having been
156 * removed from the slot/adapter.
157 */
158 msleep(1000);
1da177e4
LT
159 }
160
82a9e79e 161 /* turn off Green LED */
af9ab791 162 pciehp_green_led_off(p_slot);
1da177e4
LT
163 return 0;
164}
165
5d386e1a
KK
166struct power_work_info {
167 struct slot *p_slot;
168 struct work_struct work;
c4f2f5e4
RJ
169 unsigned int req;
170#define DISABLE_REQ 0
171#define ENABLE_REQ 1
5d386e1a 172};
1da177e4
LT
173
174/**
26e6c66e
RD
175 * pciehp_power_thread - handle pushbutton events
176 * @work: &struct work_struct describing work to be done
1da177e4 177 *
26e6c66e 178 * Scheduled procedure to handle blocking stuff for the pushbuttons.
1da177e4 179 * Handles all pending events and exits.
1da177e4 180 */
5d386e1a 181static void pciehp_power_thread(struct work_struct *work)
1da177e4 182{
5d386e1a
KK
183 struct power_work_info *info =
184 container_of(work, struct power_work_info, work);
185 struct slot *p_slot = info->p_slot;
50b52fde 186 int ret;
5d386e1a 187
c4f2f5e4
RJ
188 switch (info->req) {
189 case DISABLE_REQ:
50b52fde 190 mutex_lock(&p_slot->hotplug_lock);
1da177e4 191 pciehp_disable_slot(p_slot);
50b52fde 192 mutex_unlock(&p_slot->hotplug_lock);
5d386e1a 193 mutex_lock(&p_slot->lock);
1da177e4 194 p_slot->state = STATIC_STATE;
5d386e1a 195 mutex_unlock(&p_slot->lock);
c4f2f5e4
RJ
196 break;
197 case ENABLE_REQ:
50b52fde
RJ
198 mutex_lock(&p_slot->hotplug_lock);
199 ret = pciehp_enable_slot(p_slot);
200 mutex_unlock(&p_slot->hotplug_lock);
201 if (ret)
82a9e79e 202 pciehp_green_led_off(p_slot);
5d386e1a 203 mutex_lock(&p_slot->lock);
1da177e4 204 p_slot->state = STATIC_STATE;
c4f2f5e4 205 mutex_unlock(&p_slot->lock);
5d386e1a
KK
206 break;
207 default:
208 break;
1da177e4
LT
209 }
210
5d386e1a 211 kfree(info);
1da177e4
LT
212}
213
bee67756 214static void pciehp_queue_power_work(struct slot *p_slot, int req)
1da177e4 215{
5d386e1a 216 struct power_work_info *info;
1da177e4 217
bee67756
GR
218 p_slot->state = (req == ENABLE_REQ) ? POWERON_STATE : POWEROFF_STATE;
219
5d386e1a
KK
220 info = kmalloc(sizeof(*info), GFP_KERNEL);
221 if (!info) {
bee67756
GR
222 ctrl_err(p_slot->ctrl, "no memory to queue %s request\n",
223 (req == ENABLE_REQ) ? "poweron" : "poweroff");
1da177e4
LT
224 return;
225 }
5d386e1a
KK
226 info->p_slot = p_slot;
227 INIT_WORK(&info->work, pciehp_power_thread);
bee67756
GR
228 info->req = req;
229 queue_work(p_slot->wq, &info->work);
230}
231
232void pciehp_queue_pushbutton_work(struct work_struct *work)
233{
234 struct slot *p_slot = container_of(work, struct slot, work.work);
1da177e4 235
5d386e1a
KK
236 mutex_lock(&p_slot->lock);
237 switch (p_slot->state) {
238 case BLINKINGOFF_STATE:
bee67756 239 pciehp_queue_power_work(p_slot, DISABLE_REQ);
5d386e1a
KK
240 break;
241 case BLINKINGON_STATE:
bee67756 242 pciehp_queue_power_work(p_slot, ENABLE_REQ);
5d386e1a
KK
243 break;
244 default:
bee67756 245 break;
1da177e4 246 }
5d386e1a 247 mutex_unlock(&p_slot->lock);
1da177e4
LT
248}
249
5d386e1a
KK
250/*
251 * Note: This function must be called with slot->lock held
252 */
253static void handle_button_press_event(struct slot *p_slot)
1da177e4 254{
5d386e1a 255 struct controller *ctrl = p_slot->ctrl;
1da177e4 256 u8 getstatus;
1da177e4 257
5d386e1a
KK
258 switch (p_slot->state) {
259 case STATIC_STATE:
82a9e79e 260 pciehp_get_power_status(p_slot, &getstatus);
5d386e1a
KK
261 if (getstatus) {
262 p_slot->state = BLINKINGOFF_STATE;
6e49b304 263 ctrl_info(ctrl, "Slot(%s): Powering off due to button press\n",
227f0647 264 slot_name(p_slot));
5d386e1a
KK
265 } else {
266 p_slot->state = BLINKINGON_STATE;
6e49b304 267 ctrl_info(ctrl, "Slot(%s) Powering on due to button press\n",
227f0647 268 slot_name(p_slot));
5d386e1a
KK
269 }
270 /* blink green LED and turn off amber */
af9ab791
BH
271 pciehp_green_led_blink(p_slot);
272 pciehp_set_attention_status(p_slot, 0);
c2be6f93 273 queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
5d386e1a
KK
274 break;
275 case BLINKINGOFF_STATE:
276 case BLINKINGON_STATE:
277 /*
278 * Cancel if we are still blinking; this means that we
279 * press the attention again before the 5 sec. limit
280 * expires to cancel hot-add or hot-remove
281 */
6e49b304 282 ctrl_info(ctrl, "Slot(%s): Button cancel\n", slot_name(p_slot));
5d386e1a 283 cancel_delayed_work(&p_slot->work);
9cad7f58 284 if (p_slot->state == BLINKINGOFF_STATE)
af9ab791 285 pciehp_green_led_on(p_slot);
9cad7f58 286 else
af9ab791 287 pciehp_green_led_off(p_slot);
af9ab791 288 pciehp_set_attention_status(p_slot, 0);
6e49b304 289 ctrl_info(ctrl, "Slot(%s): Action canceled due to button press\n",
227f0647 290 slot_name(p_slot));
5d386e1a
KK
291 p_slot->state = STATIC_STATE;
292 break;
293 case POWEROFF_STATE:
294 case POWERON_STATE:
295 /*
296 * Ignore if the slot is on power-on or power-off state;
297 * this means that the previous attention button action
298 * to hot-add or hot-remove is undergoing
299 */
6e49b304
BH
300 ctrl_info(ctrl, "Slot(%s): Button ignored\n",
301 slot_name(p_slot));
5d386e1a
KK
302 break;
303 default:
6e49b304
BH
304 ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
305 slot_name(p_slot), p_slot->state);
5d386e1a 306 break;
1da177e4
LT
307 }
308}
309
e48f1b67
RJ
310/*
311 * Note: This function must be called with slot->lock held
312 */
313static void handle_link_event(struct slot *p_slot, u32 event)
314{
315 struct controller *ctrl = p_slot->ctrl;
e48f1b67
RJ
316
317 switch (p_slot->state) {
318 case BLINKINGON_STATE:
319 case BLINKINGOFF_STATE:
320 cancel_delayed_work(&p_slot->work);
321 /* Fall through */
322 case STATIC_STATE:
bee67756
GR
323 pciehp_queue_power_work(p_slot, event == INT_LINK_UP ?
324 ENABLE_REQ : DISABLE_REQ);
e48f1b67
RJ
325 break;
326 case POWERON_STATE:
327 if (event == INT_LINK_UP) {
6e49b304 328 ctrl_info(ctrl, "Slot(%s): Link Up event ignored; already powering on\n",
e48f1b67 329 slot_name(p_slot));
e48f1b67 330 } else {
6e49b304 331 ctrl_info(ctrl, "Slot(%s): Link Down event queued; currently getting powered on\n",
e48f1b67 332 slot_name(p_slot));
bee67756 333 pciehp_queue_power_work(p_slot, DISABLE_REQ);
e48f1b67
RJ
334 }
335 break;
336 case POWEROFF_STATE:
337 if (event == INT_LINK_UP) {
6e49b304 338 ctrl_info(ctrl, "Slot(%s): Link Up event queued; currently getting powered off\n",
e48f1b67 339 slot_name(p_slot));
bee67756 340 pciehp_queue_power_work(p_slot, ENABLE_REQ);
e48f1b67 341 } else {
6e49b304 342 ctrl_info(ctrl, "Slot(%s): Link Down event ignored; already powering off\n",
e48f1b67 343 slot_name(p_slot));
e48f1b67
RJ
344 }
345 break;
346 default:
6e49b304
BH
347 ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
348 slot_name(p_slot), p_slot->state);
e48f1b67
RJ
349 break;
350 }
351}
352
5d386e1a
KK
353static void interrupt_event_handler(struct work_struct *work)
354{
355 struct event_info *info = container_of(work, struct event_info, work);
356 struct slot *p_slot = info->p_slot;
357 struct controller *ctrl = p_slot->ctrl;
358
359 mutex_lock(&p_slot->lock);
360 switch (info->event_type) {
361 case INT_BUTTON_PRESS:
362 handle_button_press_event(p_slot);
363 break;
364 case INT_POWER_FAULT:
ae416e6b 365 if (!POWER_CTRL(ctrl))
5d386e1a 366 break;
af9ab791
BH
367 pciehp_set_attention_status(p_slot, 1);
368 pciehp_green_led_off(p_slot);
5d386e1a
KK
369 break;
370 case INT_PRESENCE_ON:
69bd3c5b 371 pciehp_queue_power_work(p_slot, ENABLE_REQ);
2b3940b6
RJ
372 break;
373 case INT_PRESENCE_OFF:
374 /*
375 * Regardless of surprise capability, we need to
376 * definitely remove a card that has been pulled out!
377 */
69bd3c5b 378 pciehp_queue_power_work(p_slot, DISABLE_REQ);
5d386e1a 379 break;
e48f1b67
RJ
380 case INT_LINK_UP:
381 case INT_LINK_DOWN:
382 handle_link_event(p_slot, info->event_type);
383 break;
5d386e1a 384 default:
5d386e1a
KK
385 break;
386 }
387 mutex_unlock(&p_slot->lock);
388
389 kfree(info);
390}
391
50b52fde
RJ
392/*
393 * Note: This function must be called with slot->hotplug_lock held
394 */
1da177e4
LT
395int pciehp_enable_slot(struct slot *p_slot)
396{
397 u8 getstatus = 0;
7f2feec1 398 struct controller *ctrl = p_slot->ctrl;
1da177e4 399
6dae6202
BH
400 pciehp_get_adapter_status(p_slot, &getstatus);
401 if (!getstatus) {
6e49b304 402 ctrl_info(ctrl, "Slot(%s): No adapter\n", slot_name(p_slot));
c9d86d76 403 return -ENODEV;
1da177e4 404 }
ae416e6b 405 if (MRL_SENS(p_slot->ctrl)) {
6dae6202
BH
406 pciehp_get_latch_status(p_slot, &getstatus);
407 if (getstatus) {
6e49b304 408 ctrl_info(ctrl, "Slot(%s): Latch open\n",
18b341b7 409 slot_name(p_slot));
c9d86d76 410 return -ENODEV;
1da177e4
LT
411 }
412 }
36ed27b0 413
ae416e6b 414 if (POWER_CTRL(p_slot->ctrl)) {
6dae6202
BH
415 pciehp_get_power_status(p_slot, &getstatus);
416 if (getstatus) {
6e49b304 417 ctrl_info(ctrl, "Slot(%s): Already enabled\n",
18b341b7 418 slot_name(p_slot));
c4ae2ade 419 return 0;
1da177e4
LT
420 }
421 }
1da177e4 422
29a654e5 423 return board_added(p_slot);
1da177e4
LT
424}
425
50b52fde
RJ
426/*
427 * Note: This function must be called with slot->hotplug_lock held
428 */
1da177e4
LT
429int pciehp_disable_slot(struct slot *p_slot)
430{
1da177e4 431 u8 getstatus = 0;
7f2feec1 432 struct controller *ctrl = p_slot->ctrl;
1da177e4
LT
433
434 if (!p_slot->ctrl)
435 return 1;
436
ae416e6b 437 if (POWER_CTRL(p_slot->ctrl)) {
6dae6202
BH
438 pciehp_get_power_status(p_slot, &getstatus);
439 if (!getstatus) {
6e49b304 440 ctrl_info(ctrl, "Slot(%s): Already disabled\n",
18b341b7 441 slot_name(p_slot));
c9d86d76 442 return -EINVAL;
1da177e4
LT
443 }
444 }
445
445f7985 446 return remove_board(p_slot);
1da177e4
LT
447}
448
5d386e1a
KK
449int pciehp_sysfs_enable_slot(struct slot *p_slot)
450{
451 int retval = -ENODEV;
7f2feec1 452 struct controller *ctrl = p_slot->ctrl;
5d386e1a
KK
453
454 mutex_lock(&p_slot->lock);
455 switch (p_slot->state) {
456 case BLINKINGON_STATE:
457 cancel_delayed_work(&p_slot->work);
458 case STATIC_STATE:
459 p_slot->state = POWERON_STATE;
460 mutex_unlock(&p_slot->lock);
50b52fde 461 mutex_lock(&p_slot->hotplug_lock);
5d386e1a 462 retval = pciehp_enable_slot(p_slot);
50b52fde 463 mutex_unlock(&p_slot->hotplug_lock);
5d386e1a
KK
464 mutex_lock(&p_slot->lock);
465 p_slot->state = STATIC_STATE;
466 break;
467 case POWERON_STATE:
6e49b304 468 ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
e1acb24f 469 slot_name(p_slot));
5d386e1a
KK
470 break;
471 case BLINKINGOFF_STATE:
472 case POWEROFF_STATE:
6e49b304 473 ctrl_info(ctrl, "Slot(%s): Already enabled\n",
e1acb24f 474 slot_name(p_slot));
5d386e1a
KK
475 break;
476 default:
6e49b304
BH
477 ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
478 slot_name(p_slot), p_slot->state);
5d386e1a
KK
479 break;
480 }
481 mutex_unlock(&p_slot->lock);
482
483 return retval;
484}
485
486int pciehp_sysfs_disable_slot(struct slot *p_slot)
487{
488 int retval = -ENODEV;
7f2feec1 489 struct controller *ctrl = p_slot->ctrl;
5d386e1a
KK
490
491 mutex_lock(&p_slot->lock);
492 switch (p_slot->state) {
493 case BLINKINGOFF_STATE:
494 cancel_delayed_work(&p_slot->work);
495 case STATIC_STATE:
496 p_slot->state = POWEROFF_STATE;
497 mutex_unlock(&p_slot->lock);
64609eaa 498 mutex_lock(&p_slot->hotplug_lock);
5d386e1a 499 retval = pciehp_disable_slot(p_slot);
64609eaa 500 mutex_unlock(&p_slot->hotplug_lock);
5d386e1a
KK
501 mutex_lock(&p_slot->lock);
502 p_slot->state = STATIC_STATE;
503 break;
504 case POWEROFF_STATE:
6e49b304 505 ctrl_info(ctrl, "Slot(%s): Already in powering off state\n",
e1acb24f 506 slot_name(p_slot));
5d386e1a
KK
507 break;
508 case BLINKINGON_STATE:
509 case POWERON_STATE:
6e49b304 510 ctrl_info(ctrl, "Slot(%s): Already disabled\n",
e1acb24f 511 slot_name(p_slot));
5d386e1a
KK
512 break;
513 default:
6e49b304
BH
514 ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
515 slot_name(p_slot), p_slot->state);
5d386e1a
KK
516 break;
517 }
518 mutex_unlock(&p_slot->lock);
519
520 return retval;
521}