blob: a55653b54eeddc5b53ac33bbd03f6e7d3f7dac79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Compaq 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 *
8 * All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
19 * details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * Send feedback to <greg@kroah.com>
26 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/slab.h>
33#include <linux/workqueue.h>
34#include <linux/interrupt.h>
35#include <linux/delay.h>
36#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070038#include <linux/pci_hotplug.h>
Christoph Hellwigfa007d82007-08-14 16:17:15 -070039#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "cpqphp.h"
41
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040042static u32 configure_new_device(struct controller *ctrl, struct pci_func *func,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 u8 behind_bridge, struct resource_lists *resources);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040044static int configure_new_function(struct controller *ctrl, struct pci_func *func,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 u8 behind_bridge, struct resource_lists *resources);
46static void interrupt_event_handler(struct controller *ctrl);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Christoph Hellwigfa007d82007-08-14 16:17:15 -070049static struct task_struct *cpqhp_event_thread;
50static unsigned long pushbutton_pending; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* delay is in jiffies to wait for */
53static void long_delay(int delay)
54{
Christoph Hellwigfa007d82007-08-14 16:17:15 -070055 /*
56 * XXX(hch): if someone is bored please convert all callers
57 * to call msleep_interruptible directly. They really want
58 * to specify timeouts in natural units and spend a lot of
59 * effort converting them to jiffies..
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 msleep_interruptible(jiffies_to_msecs(delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64
65/* FIXME: The following line needs to be somewhere else... */
66#define WRONG_BUS_FREQUENCY 0x07
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040067static u8 handle_switch_change(u8 change, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int hp_slot;
70 u8 rc = 0;
71 u16 temp_word;
72 struct pci_func *func;
73 struct event_info *taskInfo;
74
75 if (!change)
76 return 0;
77
78 /* Switch Change */
79 dbg("cpqsbd: Switch interrupt received.\n");
80
81 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
82 if (change & (0x1L << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -060083 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -060085 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 func = cpqhp_slot_find(ctrl->bus,
87 (hp_slot + ctrl->slot_device_offset), 0);
88
89 /* this is the structure that tells the worker thread
Alex Chiang427438c2009-03-31 09:23:16 -060090 * what to do
91 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
93 ctrl->next_event = (ctrl->next_event + 1) % 10;
94 taskInfo->hp_slot = hp_slot;
95
96 rc++;
97
98 temp_word = ctrl->ctrl_int_comp >> 16;
99 func->presence_save = (temp_word >> hp_slot) & 0x01;
100 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
101
102 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600103 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * Switch opened
Alex Chiang427438c2009-03-31 09:23:16 -0600105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 func->switch_save = 0;
108
109 taskInfo->event_type = INT_SWITCH_OPEN;
110 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600111 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Switch closed
Alex Chiang427438c2009-03-31 09:23:16 -0600113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 func->switch_save = 0x10;
116
117 taskInfo->event_type = INT_SWITCH_CLOSE;
118 }
119 }
120 }
121
122 return rc;
123}
124
125/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800126 * cpqhp_find_slot - find the struct slot of given device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * @ctrl: scan lots of this controller
128 * @device: the device id to find
129 */
130static struct slot *cpqhp_find_slot(struct controller *ctrl, u8 device)
131{
132 struct slot *slot = ctrl->slot;
133
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600134 while (slot && (slot->device != device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 slot = slot->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 return slot;
138}
139
140
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400141static u8 handle_presence_change(u16 change, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 int hp_slot;
144 u8 rc = 0;
145 u8 temp_byte;
146 u16 temp_word;
147 struct pci_func *func;
148 struct event_info *taskInfo;
149 struct slot *p_slot;
150
151 if (!change)
152 return 0;
153
Alex Chiang427438c2009-03-31 09:23:16 -0600154 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * Presence Change
Alex Chiang427438c2009-03-31 09:23:16 -0600156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 dbg("cpqsbd: Presence/Notify input change.\n");
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800158 dbg(" Changed bits are 0x%4.4x\n", change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
161 if (change & (0x0101 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600162 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -0600164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 func = cpqhp_slot_find(ctrl->bus,
166 (hp_slot + ctrl->slot_device_offset), 0);
167
168 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
169 ctrl->next_event = (ctrl->next_event + 1) % 10;
170 taskInfo->hp_slot = hp_slot;
171
172 rc++;
173
174 p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
175 if (!p_slot)
176 return 0;
177
178 /* If the switch closed, must be a button
Alex Chiang427438c2009-03-31 09:23:16 -0600179 * If not in button mode, nevermind
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (func->switch_save && (ctrl->push_button == 1)) {
182 temp_word = ctrl->ctrl_int_comp >> 16;
183 temp_byte = (temp_word >> hp_slot) & 0x01;
184 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
185
186 if (temp_byte != func->presence_save) {
Alex Chiang427438c2009-03-31 09:23:16 -0600187 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * button Pressed (doesn't do anything)
Alex Chiang427438c2009-03-31 09:23:16 -0600189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 dbg("hp_slot %d button pressed\n", hp_slot);
191 taskInfo->event_type = INT_BUTTON_PRESS;
192 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600193 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * button Released - TAKE ACTION!!!!
Alex Chiang427438c2009-03-31 09:23:16 -0600195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 dbg("hp_slot %d button released\n", hp_slot);
197 taskInfo->event_type = INT_BUTTON_RELEASE;
198
199 /* Cancel if we are still blinking */
200 if ((p_slot->state == BLINKINGON_STATE)
201 || (p_slot->state == BLINKINGOFF_STATE)) {
202 taskInfo->event_type = INT_BUTTON_CANCEL;
203 dbg("hp_slot %d button cancel\n", hp_slot);
204 } else if ((p_slot->state == POWERON_STATE)
205 || (p_slot->state == POWEROFF_STATE)) {
206 /* info(msg_button_ignore, p_slot->number); */
207 taskInfo->event_type = INT_BUTTON_IGNORE;
208 dbg("hp_slot %d button ignore\n", hp_slot);
209 }
210 }
211 } else {
212 /* Switch is open, assume a presence change
Alex Chiang427438c2009-03-31 09:23:16 -0600213 * Save the presence state
214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 temp_word = ctrl->ctrl_int_comp >> 16;
216 func->presence_save = (temp_word >> hp_slot) & 0x01;
217 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
218
219 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
220 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
221 /* Present */
222 taskInfo->event_type = INT_PRESENCE_ON;
223 } else {
224 /* Not Present */
225 taskInfo->event_type = INT_PRESENCE_OFF;
226 }
227 }
228 }
229 }
230
231 return rc;
232}
233
234
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400235static u8 handle_power_fault(u8 change, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int hp_slot;
238 u8 rc = 0;
239 struct pci_func *func;
240 struct event_info *taskInfo;
241
242 if (!change)
243 return 0;
244
Alex Chiang427438c2009-03-31 09:23:16 -0600245 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * power fault
Alex Chiang427438c2009-03-31 09:23:16 -0600247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 info("power fault interrupt\n");
250
251 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
252 if (change & (0x01 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600253 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -0600255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 func = cpqhp_slot_find(ctrl->bus,
257 (hp_slot + ctrl->slot_device_offset), 0);
258
259 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
260 ctrl->next_event = (ctrl->next_event + 1) % 10;
261 taskInfo->hp_slot = hp_slot;
262
263 rc++;
264
265 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600266 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * power fault Cleared
Alex Chiang427438c2009-03-31 09:23:16 -0600268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 func->status = 0x00;
270
271 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
272 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600273 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * power fault
Alex Chiang427438c2009-03-31 09:23:16 -0600275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 taskInfo->event_type = INT_POWER_FAULT;
277
278 if (ctrl->rev < 4) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800279 amber_LED_on(ctrl, hp_slot);
280 green_LED_off(ctrl, hp_slot);
281 set_SOGO(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* this is a fatal condition, we want
284 * to crash the machine to protect from
285 * data corruption. simulated_NMI
286 * shouldn't ever return */
287 /* FIXME
288 simulated_NMI(hp_slot, ctrl); */
289
290 /* The following code causes a software
291 * crash just in case simulated_NMI did
292 * return */
293 /*FIXME
294 panic(msg_power_fault); */
295 } else {
296 /* set power fault status for this board */
297 func->status = 0xFF;
298 info("power fault bit %x set\n", hp_slot);
299 }
300 }
301 }
302 }
303
304 return rc;
305}
306
307
308/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800309 * sort_by_size - sort nodes on the list by their length, smallest first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * @head: list to sort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312static int sort_by_size(struct pci_resource **head)
313{
314 struct pci_resource *current_res;
315 struct pci_resource *next_res;
316 int out_of_order = 1;
317
318 if (!(*head))
319 return 1;
320
321 if (!((*head)->next))
322 return 0;
323
324 while (out_of_order) {
325 out_of_order = 0;
326
327 /* Special case for swapping list head */
328 if (((*head)->next) &&
329 ((*head)->length > (*head)->next->length)) {
330 out_of_order++;
331 current_res = *head;
332 *head = (*head)->next;
333 current_res->next = (*head)->next;
334 (*head)->next = current_res;
335 }
336
337 current_res = *head;
338
339 while (current_res->next && current_res->next->next) {
340 if (current_res->next->length > current_res->next->next->length) {
341 out_of_order++;
342 next_res = current_res->next;
343 current_res->next = current_res->next->next;
344 current_res = current_res->next;
345 next_res->next = current_res->next;
346 current_res->next = next_res;
347 } else
348 current_res = current_res->next;
349 }
350 } /* End of out_of_order loop */
351
352 return 0;
353}
354
355
356/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800357 * sort_by_max_size - sort nodes on the list by their length, largest first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * @head: list to sort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
360static int sort_by_max_size(struct pci_resource **head)
361{
362 struct pci_resource *current_res;
363 struct pci_resource *next_res;
364 int out_of_order = 1;
365
366 if (!(*head))
367 return 1;
368
369 if (!((*head)->next))
370 return 0;
371
372 while (out_of_order) {
373 out_of_order = 0;
374
375 /* Special case for swapping list head */
376 if (((*head)->next) &&
377 ((*head)->length < (*head)->next->length)) {
378 out_of_order++;
379 current_res = *head;
380 *head = (*head)->next;
381 current_res->next = (*head)->next;
382 (*head)->next = current_res;
383 }
384
385 current_res = *head;
386
387 while (current_res->next && current_res->next->next) {
388 if (current_res->next->length < current_res->next->next->length) {
389 out_of_order++;
390 next_res = current_res->next;
391 current_res->next = current_res->next->next;
392 current_res = current_res->next;
393 next_res->next = current_res->next;
394 current_res->next = next_res;
395 } else
396 current_res = current_res->next;
397 }
398 } /* End of out_of_order loop */
399
400 return 0;
401}
402
403
404/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800405 * do_pre_bridge_resource_split - find node of resources that are unused
406 * @head: new list head
407 * @orig_head: original list head
408 * @alignment: max node size (?)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
410static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
411 struct pci_resource **orig_head, u32 alignment)
412{
413 struct pci_resource *prevnode = NULL;
414 struct pci_resource *node;
415 struct pci_resource *split_node;
416 u32 rc;
417 u32 temp_dword;
418 dbg("do_pre_bridge_resource_split\n");
419
420 if (!(*head) || !(*orig_head))
421 return NULL;
422
423 rc = cpqhp_resource_sort_and_combine(head);
424
425 if (rc)
426 return NULL;
427
428 if ((*head)->base != (*orig_head)->base)
429 return NULL;
430
431 if ((*head)->length == (*orig_head)->length)
432 return NULL;
433
434
435 /* If we got here, there the bridge requires some of the resource, but
Alex Chiang427438c2009-03-31 09:23:16 -0600436 * we may be able to split some off of the front
437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 node = *head;
440
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800441 if (node->length & (alignment - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* this one isn't an aligned length, so we'll make a new entry
Alex Chiang427438c2009-03-31 09:23:16 -0600443 * and split it up.
444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
446
447 if (!split_node)
448 return NULL;
449
450 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
451
452 split_node->base = node->base;
453 split_node->length = temp_dword;
454
455 node->length -= temp_dword;
456 node->base += split_node->length;
457
458 /* Put it in the list */
459 *head = split_node;
460 split_node->next = node;
461 }
462
463 if (node->length < alignment)
464 return NULL;
465
466 /* Now unlink it */
467 if (*head == node) {
468 *head = node->next;
469 } else {
470 prevnode = *head;
471 while (prevnode->next != node)
472 prevnode = prevnode->next;
473
474 prevnode->next = node->next;
475 }
476 node->next = NULL;
477
478 return node;
479}
480
481
482/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800483 * do_bridge_resource_split - find one node of resources that aren't in use
484 * @head: list head
485 * @alignment: max node size (?)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
487static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
488{
489 struct pci_resource *prevnode = NULL;
490 struct pci_resource *node;
491 u32 rc;
492 u32 temp_dword;
493
494 rc = cpqhp_resource_sort_and_combine(head);
495
496 if (rc)
497 return NULL;
498
499 node = *head;
500
501 while (node->next) {
502 prevnode = node;
503 node = node->next;
504 kfree(prevnode);
505 }
506
507 if (node->length < alignment)
508 goto error;
509
510 if (node->base & (alignment - 1)) {
511 /* Short circuit if adjusted size is too small */
512 temp_dword = (node->base | (alignment-1)) + 1;
513 if ((node->length - (temp_dword - node->base)) < alignment)
514 goto error;
515
516 node->length -= (temp_dword - node->base);
517 node->base = temp_dword;
518 }
519
520 if (node->length & (alignment - 1))
521 /* There's stuff in use after this node */
522 goto error;
523
524 return node;
525error:
526 kfree(node);
527 return NULL;
528}
529
530
531/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800532 * get_io_resource - find first node of given size not in ISA aliasing window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * @head: list to search
534 * @size: size of node to find, must be a power of two.
535 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800536 * Description: This function sorts the resource list by size and then returns
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * returns the first node of "size" length that is not in the ISA aliasing
538 * window. If it finds a node larger than "size" it will split it up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
541{
542 struct pci_resource *prevnode;
543 struct pci_resource *node;
544 struct pci_resource *split_node;
545 u32 temp_dword;
546
547 if (!(*head))
548 return NULL;
549
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600550 if (cpqhp_resource_sort_and_combine(head))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return NULL;
552
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600553 if (sort_by_size(head))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return NULL;
555
556 for (node = *head; node; node = node->next) {
557 if (node->length < size)
558 continue;
559
560 if (node->base & (size - 1)) {
561 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600562 * so we'll make a new entry and split it up
563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 temp_dword = (node->base | (size-1)) + 1;
565
566 /* Short circuit if adjusted size is too small */
567 if ((node->length - (temp_dword - node->base)) < size)
568 continue;
569
570 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
571
572 if (!split_node)
573 return NULL;
574
575 split_node->base = node->base;
576 split_node->length = temp_dword - node->base;
577 node->base = temp_dword;
578 node->length -= split_node->length;
579
580 /* Put it in the list */
581 split_node->next = node->next;
582 node->next = split_node;
583 } /* End of non-aligned base */
584
585 /* Don't need to check if too small since we already did */
586 if (node->length > size) {
587 /* this one is longer than we need
Alex Chiang427438c2009-03-31 09:23:16 -0600588 * so we'll make a new entry and split it up
589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
591
592 if (!split_node)
593 return NULL;
594
595 split_node->base = node->base + size;
596 split_node->length = node->length - size;
597 node->length = size;
598
599 /* Put it in the list */
600 split_node->next = node->next;
601 node->next = split_node;
602 } /* End of too big on top end */
603
604 /* For IO make sure it's not in the ISA aliasing space */
605 if (node->base & 0x300L)
606 continue;
607
608 /* If we got here, then it is the right size
Alex Chiang427438c2009-03-31 09:23:16 -0600609 * Now take it out of the list and break
610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (*head == node) {
612 *head = node->next;
613 } else {
614 prevnode = *head;
615 while (prevnode->next != node)
616 prevnode = prevnode->next;
617
618 prevnode->next = node->next;
619 }
620 node->next = NULL;
621 break;
622 }
623
624 return node;
625}
626
627
628/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800629 * get_max_resource - get largest node which has at least the given size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * @head: the list to search the node in
631 * @size: the minimum size of the node to find
632 *
633 * Description: Gets the largest node that is at least "size" big from the
634 * list pointed to by head. It aligns the node on top and bottom
635 * to "size" alignment before returning it.
636 */
637static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
638{
639 struct pci_resource *max;
640 struct pci_resource *temp;
641 struct pci_resource *split_node;
642 u32 temp_dword;
643
644 if (cpqhp_resource_sort_and_combine(head))
645 return NULL;
646
647 if (sort_by_max_size(head))
648 return NULL;
649
650 for (max = *head; max; max = max->next) {
Alex Chiang861fefb2009-03-31 09:23:11 -0600651 /* If not big enough we could probably just bail,
Alex Chiang427438c2009-03-31 09:23:16 -0600652 * instead we'll continue to the next.
653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (max->length < size)
655 continue;
656
657 if (max->base & (size - 1)) {
658 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600659 * so we'll make a new entry and split it up
660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 temp_dword = (max->base | (size-1)) + 1;
662
663 /* Short circuit if adjusted size is too small */
664 if ((max->length - (temp_dword - max->base)) < size)
665 continue;
666
667 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
668
669 if (!split_node)
670 return NULL;
671
672 split_node->base = max->base;
673 split_node->length = temp_dword - max->base;
674 max->base = temp_dword;
675 max->length -= split_node->length;
676
677 split_node->next = max->next;
678 max->next = split_node;
679 }
680
681 if ((max->base + max->length) & (size - 1)) {
682 /* this one isn't end aligned properly at the top
Alex Chiang427438c2009-03-31 09:23:16 -0600683 * so we'll make a new entry and split it up
684 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
686
687 if (!split_node)
688 return NULL;
689 temp_dword = ((max->base + max->length) & ~(size - 1));
690 split_node->base = temp_dword;
691 split_node->length = max->length + max->base
692 - split_node->base;
693 max->length -= split_node->length;
694
695 split_node->next = max->next;
696 max->next = split_node;
697 }
698
699 /* Make sure it didn't shrink too much when we aligned it */
700 if (max->length < size)
701 continue;
702
703 /* Now take it out of the list */
704 temp = *head;
705 if (temp == max) {
706 *head = max->next;
707 } else {
Quentin Lambert656f9782014-09-07 20:02:47 +0200708 while (temp && temp->next != max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 temp = temp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Rickard Strandqvistcab9a122014-05-18 18:02:57 +0200711 if (temp)
712 temp->next = max->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
715 max->next = NULL;
716 break;
717 }
718
719 return max;
720}
721
722
723/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800724 * get_resource - find resource of given size and split up larger ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * @head: the list to search for resources
726 * @size: the size limit to use
727 *
728 * Description: This function sorts the resource list by size and then
729 * returns the first node of "size" length. If it finds a node
730 * larger than "size" it will split it up.
731 *
732 * size must be a power of two.
733 */
734static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
735{
736 struct pci_resource *prevnode;
737 struct pci_resource *node;
738 struct pci_resource *split_node;
739 u32 temp_dword;
740
741 if (cpqhp_resource_sort_and_combine(head))
742 return NULL;
743
744 if (sort_by_size(head))
745 return NULL;
746
747 for (node = *head; node; node = node->next) {
748 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800749 __func__, size, node, node->base, node->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (node->length < size)
751 continue;
752
753 if (node->base & (size - 1)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800754 dbg("%s: not aligned\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600756 * so we'll make a new entry and split it up
757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 temp_dword = (node->base | (size-1)) + 1;
759
760 /* Short circuit if adjusted size is too small */
761 if ((node->length - (temp_dword - node->base)) < size)
762 continue;
763
764 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
765
766 if (!split_node)
767 return NULL;
768
769 split_node->base = node->base;
770 split_node->length = temp_dword - node->base;
771 node->base = temp_dword;
772 node->length -= split_node->length;
773
774 split_node->next = node->next;
775 node->next = split_node;
776 } /* End of non-aligned base */
777
778 /* Don't need to check if too small since we already did */
779 if (node->length > size) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800780 dbg("%s: too big\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* this one is longer than we need
Alex Chiang427438c2009-03-31 09:23:16 -0600782 * so we'll make a new entry and split it up
783 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
785
786 if (!split_node)
787 return NULL;
788
789 split_node->base = node->base + size;
790 split_node->length = node->length - size;
791 node->length = size;
792
793 /* Put it in the list */
794 split_node->next = node->next;
795 node->next = split_node;
796 } /* End of too big on top end */
797
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800798 dbg("%s: got one!!!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* If we got here, then it is the right size
800 * Now take it out of the list */
801 if (*head == node) {
802 *head = node->next;
803 } else {
804 prevnode = *head;
805 while (prevnode->next != node)
806 prevnode = prevnode->next;
807
808 prevnode->next = node->next;
809 }
810 node->next = NULL;
811 break;
812 }
813 return node;
814}
815
816
817/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800818 * cpqhp_resource_sort_and_combine - sort nodes by base addresses and clean up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * @head: the list to sort and clean up
820 *
821 * Description: Sorts all of the nodes in the list in ascending order by
822 * their base addresses. Also does garbage collection by
823 * combining adjacent nodes.
824 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800825 * Returns %0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
827int cpqhp_resource_sort_and_combine(struct pci_resource **head)
828{
829 struct pci_resource *node1;
830 struct pci_resource *node2;
831 int out_of_order = 1;
832
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800833 dbg("%s: head = %p, *head = %p\n", __func__, head, *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (!(*head))
836 return 1;
837
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800838 dbg("*head->next = %p\n", (*head)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (!(*head)->next)
841 return 0; /* only one item on the list, already sorted! */
842
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800843 dbg("*head->base = 0x%x\n", (*head)->base);
844 dbg("*head->next->base = 0x%x\n", (*head)->next->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 while (out_of_order) {
846 out_of_order = 0;
847
848 /* Special case for swapping list head */
849 if (((*head)->next) &&
850 ((*head)->base > (*head)->next->base)) {
851 node1 = *head;
852 (*head) = (*head)->next;
853 node1->next = (*head)->next;
854 (*head)->next = node1;
855 out_of_order++;
856 }
857
858 node1 = (*head);
859
860 while (node1->next && node1->next->next) {
861 if (node1->next->base > node1->next->next->base) {
862 out_of_order++;
863 node2 = node1->next;
864 node1->next = node1->next->next;
865 node1 = node1->next;
866 node2->next = node1->next;
867 node1->next = node2;
868 } else
869 node1 = node1->next;
870 }
871 } /* End of out_of_order loop */
872
873 node1 = *head;
874
875 while (node1 && node1->next) {
876 if ((node1->base + node1->length) == node1->next->base) {
877 /* Combine */
878 dbg("8..\n");
879 node1->length += node1->next->length;
880 node2 = node1->next;
881 node1->next = node1->next->next;
882 kfree(node2);
883 } else
884 node1 = node1->next;
885 }
886
887 return 0;
888}
889
890
David Howells7d12e782006-10-05 14:55:46 +0100891irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct controller *ctrl = data;
894 u8 schedule_flag = 0;
895 u8 reset;
896 u16 misc;
897 u32 Diff;
898 u32 temp_dword;
899
Alex Chiang861fefb2009-03-31 09:23:11 -0600900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 misc = readw(ctrl->hpc_reg + MISC);
Alex Chiang427438c2009-03-31 09:23:16 -0600902 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 * Check to see if it was our interrupt
Alex Chiang427438c2009-03-31 09:23:16 -0600904 */
Quentin Lambert656f9782014-09-07 20:02:47 +0200905 if (!(misc & 0x000C))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (misc & 0x0004) {
Alex Chiang427438c2009-03-31 09:23:16 -0600909 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * Serial Output interrupt Pending
Alex Chiang427438c2009-03-31 09:23:16 -0600911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /* Clear the interrupt */
914 misc |= 0x0004;
915 writew(misc, ctrl->hpc_reg + MISC);
916
917 /* Read to clear posted writes */
918 misc = readw(ctrl->hpc_reg + MISC);
919
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800920 dbg("%s - waking up\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 wake_up_interruptible(&ctrl->queue);
922 }
923
924 if (misc & 0x0008) {
925 /* General-interrupt-input interrupt Pending */
926 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
927
928 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
929
930 /* Clear the interrupt */
931 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
932
933 /* Read it back to clear any posted writes */
934 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
935
936 if (!Diff)
937 /* Clear all interrupts */
938 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
939
940 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
941 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
942 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
943 }
944
945 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
946 if (reset & 0x40) {
947 /* Bus reset has completed */
948 reset &= 0xCF;
949 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
950 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
951 wake_up_interruptible(&ctrl->queue);
952 }
953
954 if (schedule_flag) {
Christoph Hellwigfa007d82007-08-14 16:17:15 -0700955 wake_up_process(cpqhp_event_thread);
956 dbg("Waking even thread");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958 return IRQ_HANDLED;
959}
960
961
962/**
963 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
Randy Dunlap26e6c662007-11-28 09:04:30 -0800964 * @busnumber: bus where new node is to be located
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800966 * Returns pointer to the new node or %NULL if unsuccessful.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
968struct pci_func *cpqhp_slot_create(u8 busnumber)
969{
970 struct pci_func *new_slot;
971 struct pci_func *next;
972
Mariusz Kozlowski73a985a2007-07-31 19:14:28 +0200973 new_slot = kzalloc(sizeof(*new_slot), GFP_KERNEL);
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600974 if (new_slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return new_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 new_slot->next = NULL;
978 new_slot->configured = 1;
979
980 if (cpqhp_slot_list[busnumber] == NULL) {
981 cpqhp_slot_list[busnumber] = new_slot;
982 } else {
983 next = cpqhp_slot_list[busnumber];
984 while (next->next != NULL)
985 next = next->next;
986 next->next = new_slot;
987 }
988 return new_slot;
989}
990
991
992/**
993 * slot_remove - Removes a node from the linked list of slots.
994 * @old_slot: slot to remove
995 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800996 * Returns %0 if successful, !0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400998static int slot_remove(struct pci_func *old_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct pci_func *next;
1001
1002 if (old_slot == NULL)
1003 return 1;
1004
1005 next = cpqhp_slot_list[old_slot->bus];
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001006 if (next == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (next == old_slot) {
1010 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1011 cpqhp_destroy_board_resources(old_slot);
1012 kfree(old_slot);
1013 return 0;
1014 }
1015
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001016 while ((next->next != old_slot) && (next->next != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 next = next->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (next->next == old_slot) {
1020 next->next = old_slot->next;
1021 cpqhp_destroy_board_resources(old_slot);
1022 kfree(old_slot);
1023 return 0;
1024 } else
1025 return 2;
1026}
1027
1028
1029/**
1030 * bridge_slot_remove - Removes a node from the linked list of slots.
1031 * @bridge: bridge to remove
1032 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001033 * Returns %0 if successful, !0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
1035static int bridge_slot_remove(struct pci_func *bridge)
1036{
1037 u8 subordinateBus, secondaryBus;
1038 u8 tempBus;
1039 struct pci_func *next;
1040
1041 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1042 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1043
1044 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1045 next = cpqhp_slot_list[tempBus];
1046
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001047 while (!slot_remove(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 next = cpqhp_slot_list[tempBus];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
1051 next = cpqhp_slot_list[bridge->bus];
1052
1053 if (next == NULL)
1054 return 1;
1055
1056 if (next == bridge) {
1057 cpqhp_slot_list[bridge->bus] = bridge->next;
1058 goto out;
1059 }
1060
1061 while ((next->next != bridge) && (next->next != NULL))
1062 next = next->next;
1063
1064 if (next->next != bridge)
1065 return 2;
1066 next->next = bridge->next;
1067out:
1068 kfree(bridge);
1069 return 0;
1070}
1071
1072
1073/**
1074 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1075 * @bus: bus to find
1076 * @device: device to find
Randy Dunlap26e6c662007-11-28 09:04:30 -08001077 * @index: is %0 for first function found, %1 for the second...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *
1079 * Returns pointer to the node if successful, %NULL otherwise.
1080 */
1081struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1082{
1083 int found = -1;
1084 struct pci_func *func;
1085
1086 func = cpqhp_slot_list[bus];
1087
1088 if ((func == NULL) || ((func->device == device) && (index == 0)))
1089 return func;
1090
1091 if (func->device == device)
1092 found++;
1093
1094 while (func->next != NULL) {
1095 func = func->next;
1096
1097 if (func->device == device)
1098 found++;
1099
1100 if (found == index)
1101 return func;
1102 }
1103
1104 return NULL;
1105}
1106
1107
1108/* DJZ: I don't think is_bridge will work as is.
1109 * FIXME */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001110static int is_bridge(struct pci_func *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 /* Check the header type */
1113 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1114 return 1;
1115 else
1116 return 0;
1117}
1118
1119
1120/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001121 * set_controller_speed - set the frequency and/or mode of a specific controller segment.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * @ctrl: controller to change frequency/mode for.
1123 * @adapter_speed: the speed of the adapter we want to match.
1124 * @hp_slot: the slot number where the adapter is installed.
1125 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001126 * Returns %0 if we successfully change frequency and/or mode to match the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * adapter speed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
1129static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
1130{
1131 struct slot *slot;
Matthew Wilcox3749c512009-12-13 08:11:32 -05001132 struct pci_bus *bus = ctrl->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 u8 reg;
1134 u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
1135 u16 reg16;
1136 u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
Alex Chiang861fefb2009-03-31 09:23:11 -06001137
Matthew Wilcox3749c512009-12-13 08:11:32 -05001138 if (bus->cur_bus_speed == adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
Alex Chiang861fefb2009-03-31 09:23:11 -06001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /* We don't allow freq/mode changes if we find another adapter running
Alex Chiang427438c2009-03-31 09:23:16 -06001142 * in another slot on this controller
1143 */
Quentin Lambert382a9c92014-09-07 20:02:04 +02001144 for (slot = ctrl->slot; slot; slot = slot->next) {
Alex Chiang861fefb2009-03-31 09:23:11 -06001145 if (slot->device == (hp_slot + ctrl->slot_device_offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 continue;
Julia Lawall05a34f52008-10-22 16:44:00 -07001147 if (!slot->hotplug_slot || !slot->hotplug_slot->info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 continue;
Alex Chiang861fefb2009-03-31 09:23:11 -06001149 if (slot->hotplug_slot->info->adapter_status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 continue;
1151 /* If another adapter is running on the same segment but at a
1152 * lower speed/mode, we allow the new adapter to function at
Alex Chiang427438c2009-03-31 09:23:16 -06001153 * this rate if supported
1154 */
Matthew Wilcox3749c512009-12-13 08:11:32 -05001155 if (bus->cur_bus_speed < adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return 0;
1157
1158 return 1;
1159 }
Alex Chiang861fefb2009-03-31 09:23:11 -06001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /* If the controller doesn't support freq/mode changes and the
Alex Chiang427438c2009-03-31 09:23:16 -06001162 * controller is running at a higher mode, we bail
1163 */
Matthew Wilcox3749c512009-12-13 08:11:32 -05001164 if ((bus->cur_bus_speed > adapter_speed) && (!ctrl->pcix_speed_capability))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return 1;
Alex Chiang861fefb2009-03-31 09:23:11 -06001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /* But we allow the adapter to run at a lower rate if possible */
Matthew Wilcox3749c512009-12-13 08:11:32 -05001168 if ((bus->cur_bus_speed < adapter_speed) && (!ctrl->pcix_speed_capability))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
1170
1171 /* We try to set the max speed supported by both the adapter and
Alex Chiang427438c2009-03-31 09:23:16 -06001172 * controller
1173 */
Matthew Wilcox3749c512009-12-13 08:11:32 -05001174 if (bus->max_bus_speed < adapter_speed) {
1175 if (bus->cur_bus_speed == bus->max_bus_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 0;
Matthew Wilcox3749c512009-12-13 08:11:32 -05001177 adapter_speed = bus->max_bus_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179
1180 writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
1181 writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
Alex Chiang861fefb2009-03-31 09:23:11 -06001182
1183 set_SOGO(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 wait_for_ctrl_irq(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (adapter_speed != PCI_SPEED_133MHz_PCIX)
1187 reg = 0xF5;
1188 else
Alex Chiang861fefb2009-03-31 09:23:11 -06001189 reg = 0xF4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
Alex Chiang861fefb2009-03-31 09:23:11 -06001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
1193 reg16 &= ~0x000F;
Quentin Lambert382a9c92014-09-07 20:02:04 +02001194 switch (adapter_speed) {
Alex Chiang861fefb2009-03-31 09:23:11 -06001195 case(PCI_SPEED_133MHz_PCIX):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 reg = 0x75;
Alex Chiang861fefb2009-03-31 09:23:11 -06001197 reg16 |= 0xB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 break;
1199 case(PCI_SPEED_100MHz_PCIX):
1200 reg = 0x74;
1201 reg16 |= 0xA;
1202 break;
1203 case(PCI_SPEED_66MHz_PCIX):
1204 reg = 0x73;
1205 reg16 |= 0x9;
1206 break;
1207 case(PCI_SPEED_66MHz):
1208 reg = 0x73;
1209 reg16 |= 0x1;
1210 break;
1211 default: /* 33MHz PCI 2.2 */
1212 reg = 0x71;
1213 break;
Alex Chiang861fefb2009-03-31 09:23:11 -06001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 reg16 |= 0xB << 12;
1217 writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
Alex Chiang861fefb2009-03-31 09:23:11 -06001218
1219 mdelay(5);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* Reenable interrupts */
1222 writel(0, ctrl->hpc_reg + INT_MASK);
1223
Alex Chiang861fefb2009-03-31 09:23:11 -06001224 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* Restart state machine */
1227 reg = ~0xF;
1228 pci_read_config_byte(ctrl->pci_dev, 0x43, &reg);
1229 pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
Alex Chiang861fefb2009-03-31 09:23:11 -06001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Only if mode change...*/
Matthew Wilcox3749c512009-12-13 08:11:32 -05001232 if (((bus->cur_bus_speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001233 ((bus->cur_bus_speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 set_SOGO(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 wait_for_ctrl_irq(ctrl);
1237 mdelay(1100);
Alex Chiang861fefb2009-03-31 09:23:11 -06001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* Restore LED/Slot state */
1240 writel(leds, ctrl->hpc_reg + LED_CONTROL);
1241 writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
Alex Chiang861fefb2009-03-31 09:23:11 -06001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 set_SOGO(ctrl);
1244 wait_for_ctrl_irq(ctrl);
1245
Matthew Wilcox3749c512009-12-13 08:11:32 -05001246 bus->cur_bus_speed = adapter_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1248
Alex Chiang861fefb2009-03-31 09:23:11 -06001249 info("Successfully changed frequency/mode for adapter in slot %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 slot->number);
1251 return 0;
1252}
1253
Alex Chiang861fefb2009-03-31 09:23:11 -06001254/* the following routines constitute the bulk of the
Alex Chiang427438c2009-03-31 09:23:16 -06001255 * hotplug controller logic
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
1257
1258
1259/**
1260 * board_replaced - Called after a board has been replaced in the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -08001261 * @func: PCI device/function information
1262 * @ctrl: hotplug controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001264 * This is only used if we don't have resources for hot add.
1265 * Turns power on for the board.
1266 * Checks to see if board is the same.
1267 * If board is same, reconfigures it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * If board isn't same, turns it back off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
1270static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
1271{
Matthew Wilcox3749c512009-12-13 08:11:32 -05001272 struct pci_bus *bus = ctrl->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 u8 hp_slot;
1274 u8 temp_byte;
1275 u8 adapter_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 u32 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 hp_slot = func->device - ctrl->slot_device_offset;
1279
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001280 /*
1281 * The switch is open.
1282 */
1283 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 rc = INTERLOCK_OPEN;
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001285 /*
1286 * The board is already on
1287 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001288 else if (is_slot_enabled(ctrl, hp_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 rc = CARD_FUNCTIONING;
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001290 else {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001291 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 /* turn on board without attaching to the bus */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001294 enable_slot_power(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 set_SOGO(ctrl);
1297
1298 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001299 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 /* Change bits in slot power register to force another shift out
1302 * NOTE: this is to work around the timer bug */
1303 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1304 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1305 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1306
1307 set_SOGO(ctrl);
1308
1309 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001310 wait_for_ctrl_irq(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 adapter_speed = get_adapter_speed(ctrl, hp_slot);
Matthew Wilcox3749c512009-12-13 08:11:32 -05001313 if (bus->cur_bus_speed != adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1315 rc = WRONG_BUS_FREQUENCY;
1316
1317 /* turn off board without attaching to the bus */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001318 disable_slot_power(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 set_SOGO(ctrl);
1321
1322 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001323 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001325 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (rc)
1328 return rc;
1329
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001330 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001332 slot_enable(ctrl, hp_slot);
1333 green_LED_blink(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001335 amber_LED_off(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 set_SOGO(ctrl);
1338
1339 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001340 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001342 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 /* Wait for ~1 second because of hot plug spec */
1345 long_delay(1*HZ);
1346
1347 /* Check for a power fault */
1348 if (func->status == 0xFF) {
1349 /* power fault occurred, but it was benign */
1350 rc = POWER_FAILURE;
1351 func->status = 0;
1352 } else
1353 rc = cpqhp_valid_replace(ctrl, func);
1354
1355 if (!rc) {
1356 /* It must be the same board */
1357
1358 rc = cpqhp_configure_board(ctrl, func);
1359
Adrian Bunk1305e912006-02-26 22:16:51 +01001360 /* If configuration fails, turn it off
1361 * Get slot won't work for devices behind
1362 * bridges, but in this case it will always be
1363 * called for the "base" bus/dev/func of an
Alex Chiang427438c2009-03-31 09:23:16 -06001364 * adapter.
1365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001367 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001369 amber_LED_on(ctrl, hp_slot);
1370 green_LED_off(ctrl, hp_slot);
1371 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 set_SOGO(ctrl);
1374
1375 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001376 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001378 mutex_unlock(&ctrl->crit_sect);
Adrian Bunk1305e912006-02-26 22:16:51 +01001379
1380 if (rc)
1381 return rc;
1382 else
1383 return 1;
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 } else {
1386 /* Something is wrong
1387
1388 * Get slot won't work for devices behind bridges, but
1389 * in this case it will always be called for the "base"
Alex Chiang427438c2009-03-31 09:23:16 -06001390 * bus/dev/func of an adapter.
1391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001393 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001395 amber_LED_on(ctrl, hp_slot);
1396 green_LED_off(ctrl, hp_slot);
1397 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 set_SOGO(ctrl);
1400
1401 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001402 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001404 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
1407 }
1408 return rc;
1409
1410}
1411
1412
1413/**
1414 * board_added - Called after a board has been added to the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -08001415 * @func: PCI device/function info
1416 * @ctrl: hotplug controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001418 * Turns power on for the board.
1419 * Configures board.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
1421static u32 board_added(struct pci_func *func, struct controller *ctrl)
1422{
1423 u8 hp_slot;
1424 u8 temp_byte;
1425 u8 adapter_speed;
1426 int index;
1427 u32 temp_register = 0xFFFFFFFF;
1428 u32 rc = 0;
1429 struct pci_func *new_slot = NULL;
Matthew Wilcox3749c512009-12-13 08:11:32 -05001430 struct pci_bus *bus = ctrl->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 struct slot *p_slot;
1432 struct resource_lists res_lists;
1433
1434 hp_slot = func->device - ctrl->slot_device_offset;
1435 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001436 __func__, func->device, ctrl->slot_device_offset, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001438 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 /* turn on board without attaching to the bus */
1441 enable_slot_power(ctrl, hp_slot);
1442
1443 set_SOGO(ctrl);
1444
1445 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001446 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 /* Change bits in slot power register to force another shift out
Alex Chiang427438c2009-03-31 09:23:16 -06001449 * NOTE: this is to work around the timer bug
1450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1452 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1453 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1454
1455 set_SOGO(ctrl);
1456
1457 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001458 wait_for_ctrl_irq(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 adapter_speed = get_adapter_speed(ctrl, hp_slot);
Matthew Wilcox3749c512009-12-13 08:11:32 -05001461 if (bus->cur_bus_speed != adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1463 rc = WRONG_BUS_FREQUENCY;
Alex Chiang861fefb2009-03-31 09:23:11 -06001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* turn off board without attaching to the bus */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001466 disable_slot_power(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 set_SOGO(ctrl);
1469
1470 /* Wait for SOBS to be unset */
1471 wait_for_ctrl_irq(ctrl);
1472
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001473 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (rc)
1476 return rc;
Alex Chiang861fefb2009-03-31 09:23:11 -06001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1479
1480 /* turn on board and blink green LED */
1481
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001482 dbg("%s: before down\n", __func__);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001483 mutex_lock(&ctrl->crit_sect);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001484 dbg("%s: after down\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001486 dbg("%s: before slot_enable\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001487 slot_enable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001489 dbg("%s: before green_LED_blink\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001490 green_LED_blink(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001492 dbg("%s: before amber_LED_blink\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001493 amber_LED_off(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001495 dbg("%s: before set_SOGO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 set_SOGO(ctrl);
1497
1498 /* Wait for SOBS to be unset */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001499 dbg("%s: before wait_for_ctrl_irq\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001500 wait_for_ctrl_irq(ctrl);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001501 dbg("%s: after wait_for_ctrl_irq\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001503 dbg("%s: before up\n", __func__);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001504 mutex_unlock(&ctrl->crit_sect);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001505 dbg("%s: after up\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 /* Wait for ~1 second because of hot plug spec */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001508 dbg("%s: before long_delay\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 long_delay(1*HZ);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001510 dbg("%s: after long_delay\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001512 dbg("%s: func status = %x\n", __func__, func->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /* Check for a power fault */
1514 if (func->status == 0xFF) {
1515 /* power fault occurred, but it was benign */
1516 temp_register = 0xFFFFFFFF;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001517 dbg("%s: temp register set to %x by power fault\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 rc = POWER_FAILURE;
1519 func->status = 0;
1520 } else {
1521 /* Get vendor/device ID u32 */
1522 ctrl->pci_bus->number = func->bus;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001523 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001524 dbg("%s: pci_read_config_dword returns %d\n", __func__, rc);
1525 dbg("%s: temp_register is %x\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (rc != 0) {
1528 /* Something's wrong here */
1529 temp_register = 0xFFFFFFFF;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001530 dbg("%s: temp register set to %x by error\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 /* Preset return code. It will be changed later if things go okay. */
1533 rc = NO_ADAPTER_PRESENT;
1534 }
1535
1536 /* All F's is an empty slot or an invalid board */
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001537 if (temp_register != 0xFFFFFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 res_lists.io_head = ctrl->io_head;
1539 res_lists.mem_head = ctrl->mem_head;
1540 res_lists.p_mem_head = ctrl->p_mem_head;
1541 res_lists.bus_head = ctrl->bus_head;
1542 res_lists.irqs = NULL;
1543
1544 rc = configure_new_device(ctrl, func, 0, &res_lists);
1545
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001546 dbg("%s: back from configure_new_device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 ctrl->io_head = res_lists.io_head;
1548 ctrl->mem_head = res_lists.mem_head;
1549 ctrl->p_mem_head = res_lists.p_mem_head;
1550 ctrl->bus_head = res_lists.bus_head;
1551
1552 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1553 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1554 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1555 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1556
1557 if (rc) {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001558 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001560 amber_LED_on(ctrl, hp_slot);
1561 green_LED_off(ctrl, hp_slot);
1562 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 set_SOGO(ctrl);
1565
1566 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001567 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001569 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return rc;
1571 } else {
1572 cpqhp_save_slot_config(ctrl, func);
1573 }
1574
1575
1576 func->status = 0;
1577 func->switch_save = 0x10;
1578 func->is_a_board = 0x01;
1579
1580 /* next, we will instantiate the linux pci_dev structures (with
1581 * appropriate driver notification, if already present) */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001582 dbg("%s: configure linux pci_dev structure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 index = 0;
1584 do {
1585 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001586 if (new_slot && !new_slot->pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 cpqhp_configure_device(ctrl, new_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 } while (new_slot);
1589
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001590 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001592 green_LED_on(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 set_SOGO(ctrl);
1595
1596 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001597 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001599 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 } else {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001601 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001603 amber_LED_on(ctrl, hp_slot);
1604 green_LED_off(ctrl, hp_slot);
1605 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 set_SOGO(ctrl);
1608
1609 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001610 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001612 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 return rc;
1615 }
1616 return 0;
1617}
1618
1619
1620/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001621 * remove_board - Turns off slot and LEDs
1622 * @func: PCI device/function info
1623 * @replace_flag: whether replacing or adding a new device
1624 * @ctrl: target controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001626static u32 remove_board(struct pci_func *func, u32 replace_flag, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 int index;
1629 u8 skip = 0;
1630 u8 device;
1631 u8 hp_slot;
1632 u8 temp_byte;
1633 u32 rc;
1634 struct resource_lists res_lists;
1635 struct pci_func *temp_func;
1636
1637 if (cpqhp_unconfigure_device(func))
1638 return 1;
1639
1640 device = func->device;
1641
1642 hp_slot = func->device - ctrl->slot_device_offset;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001643 dbg("In %s, hp_slot = %d\n", __func__, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /* When we get here, it is safe to change base address registers.
1646 * We will attempt to save the base address register lengths */
1647 if (replace_flag || !ctrl->add_support)
1648 rc = cpqhp_save_base_addr_length(ctrl, func);
1649 else if (!func->bus_head && !func->mem_head &&
1650 !func->p_mem_head && !func->io_head) {
1651 /* Here we check to see if we've saved any of the board's
1652 * resources already. If so, we'll skip the attempt to
1653 * determine what's being used. */
1654 index = 0;
1655 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1656 while (temp_func) {
1657 if (temp_func->bus_head || temp_func->mem_head
1658 || temp_func->p_mem_head || temp_func->io_head) {
1659 skip = 1;
1660 break;
1661 }
1662 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1663 }
1664
1665 if (!skip)
1666 rc = cpqhp_save_used_resources(ctrl, func);
1667 }
1668 /* Change status to shutdown */
1669 if (func->is_a_board)
1670 func->status = 0x01;
1671 func->configured = 0;
1672
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001673 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001675 green_LED_off(ctrl, hp_slot);
1676 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 set_SOGO(ctrl);
1679
1680 /* turn off SERR for slot */
1681 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1682 temp_byte &= ~(0x01 << hp_slot);
1683 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1684
1685 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001686 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001688 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (!replace_flag && ctrl->add_support) {
1691 while (func) {
1692 res_lists.io_head = ctrl->io_head;
1693 res_lists.mem_head = ctrl->mem_head;
1694 res_lists.p_mem_head = ctrl->p_mem_head;
1695 res_lists.bus_head = ctrl->bus_head;
1696
1697 cpqhp_return_board_resources(func, &res_lists);
1698
1699 ctrl->io_head = res_lists.io_head;
1700 ctrl->mem_head = res_lists.mem_head;
1701 ctrl->p_mem_head = res_lists.p_mem_head;
1702 ctrl->bus_head = res_lists.bus_head;
1703
1704 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1705 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1706 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1707 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1708
1709 if (is_bridge(func)) {
1710 bridge_slot_remove(func);
1711 } else
1712 slot_remove(func);
1713
1714 func = cpqhp_slot_find(ctrl->bus, device, 0);
1715 }
1716
1717 /* Setup slot structure with entry for empty slot */
1718 func = cpqhp_slot_create(ctrl->bus);
1719
1720 if (func == NULL)
1721 return 1;
1722
1723 func->bus = ctrl->bus;
1724 func->device = device;
1725 func->function = 0;
1726 func->configured = 0;
1727 func->switch_save = 0x10;
1728 func->is_a_board = 0;
1729 func->p_task_event = NULL;
1730 }
1731
1732 return 0;
1733}
1734
1735static void pushbutton_helper_thread(unsigned long data)
1736{
1737 pushbutton_pending = data;
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001738 wake_up_process(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
1741
1742/* this is the main worker thread */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001743static int event_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 struct controller *ctrl;
Ingo Molnar60ac8f22007-07-24 11:16:37 +02001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 while (1) {
1748 dbg("!!!!event_thread sleeping\n");
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001749 set_current_state(TASK_INTERRUPTIBLE);
1750 schedule();
1751
1752 if (kthread_should_stop())
1753 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /* Do stuff here */
1755 if (pushbutton_pending)
1756 cpqhp_pushbutton_thread(pushbutton_pending);
1757 else
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001758 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl = ctrl->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 interrupt_event_handler(ctrl);
1760 }
1761 dbg("event_thread signals exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765int cpqhp_event_start_thread(void)
1766{
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001767 cpqhp_event_thread = kthread_run(event_thread, NULL, "phpd_event");
1768 if (IS_ERR(cpqhp_event_thread)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001769 err("Can't start up our event thread\n");
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001770 return PTR_ERR(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774}
1775
1776
1777void cpqhp_event_stop_thread(void)
1778{
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001779 kthread_stop(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
1782
1783static int update_slot_info(struct controller *ctrl, struct slot *slot)
1784{
1785 struct hotplug_slot_info *info;
1786 int result;
1787
1788 info = kmalloc(sizeof(*info), GFP_KERNEL);
1789 if (!info)
1790 return -ENOMEM;
1791
1792 info->power_status = get_slot_enabled(ctrl, slot);
1793 info->attention_status = cpq_get_attention_status(ctrl, slot);
1794 info->latch_status = cpq_get_latch_status(ctrl, slot);
1795 info->adapter_status = get_presence_status(ctrl, slot);
1796 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001797 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return result;
1799}
1800
1801static void interrupt_event_handler(struct controller *ctrl)
1802{
1803 int loop = 0;
1804 int change = 1;
1805 struct pci_func *func;
1806 u8 hp_slot;
1807 struct slot *p_slot;
1808
1809 while (change) {
1810 change = 0;
1811
1812 for (loop = 0; loop < 10; loop++) {
1813 /* dbg("loop %d\n", loop); */
1814 if (ctrl->event_queue[loop].event_type != 0) {
1815 hp_slot = ctrl->event_queue[loop].hp_slot;
1816
1817 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1818 if (!func)
1819 return;
1820
1821 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1822 if (!p_slot)
1823 return;
1824
1825 dbg("hp_slot %d, func %p, p_slot %p\n",
1826 hp_slot, func, p_slot);
1827
1828 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1829 dbg("button pressed\n");
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001830 } else if (ctrl->event_queue[loop].event_type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 INT_BUTTON_CANCEL) {
1832 dbg("button cancel\n");
1833 del_timer(&p_slot->task_event);
1834
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001835 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 if (p_slot->state == BLINKINGOFF_STATE) {
1838 /* slot is on */
1839 dbg("turn on green LED\n");
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001840 green_LED_on(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 } else if (p_slot->state == BLINKINGON_STATE) {
1842 /* slot is off */
1843 dbg("turn off green LED\n");
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001844 green_LED_off(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846
1847 info(msg_button_cancel, p_slot->number);
1848
1849 p_slot->state = STATIC_STATE;
1850
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001851 amber_LED_off(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 set_SOGO(ctrl);
1854
1855 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001856 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001858 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 /*** button Released (No action on press...) */
1861 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1862 dbg("button release\n");
1863
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001864 if (is_slot_enabled(ctrl, hp_slot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 dbg("slot is on\n");
1866 p_slot->state = BLINKINGOFF_STATE;
1867 info(msg_button_off, p_slot->number);
1868 } else {
1869 dbg("slot is off\n");
1870 p_slot->state = BLINKINGON_STATE;
1871 info(msg_button_on, p_slot->number);
1872 }
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001873 mutex_lock(&ctrl->crit_sect);
Alex Chiang861fefb2009-03-31 09:23:11 -06001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 dbg("blink green LED and turn off amber\n");
Alex Chiang861fefb2009-03-31 09:23:11 -06001876
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001877 amber_LED_off(ctrl, hp_slot);
1878 green_LED_blink(ctrl, hp_slot);
Alex Chiang861fefb2009-03-31 09:23:11 -06001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 set_SOGO(ctrl);
1881
1882 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001883 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001885 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 init_timer(&p_slot->task_event);
1887 p_slot->hp_slot = hp_slot;
1888 p_slot->ctrl = ctrl;
1889/* p_slot->physical_slot = physical_slot; */
1890 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1891 p_slot->task_event.function = pushbutton_helper_thread;
1892 p_slot->task_event.data = (u32) p_slot;
1893
1894 dbg("add_timer p_slot = %p\n", p_slot);
1895 add_timer(&p_slot->task_event);
1896 }
1897 /***********POWER FAULT */
1898 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1899 dbg("power fault\n");
1900 } else {
1901 /* refresh notification */
Sasha Levin3ecd9d02012-12-20 14:11:23 -05001902 update_slot_info(ctrl, p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
1905 ctrl->event_queue[loop].event_type = 0;
1906
1907 change = 1;
1908 }
1909 } /* End of FOR loop */
1910 }
1911
1912 return;
1913}
1914
1915
1916/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001917 * cpqhp_pushbutton_thread - handle pushbutton events
1918 * @slot: target slot (struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001920 * Scheduled procedure to handle blocking stuff for the pushbuttons.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 * Handles all pending events and exits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 */
1923void cpqhp_pushbutton_thread(unsigned long slot)
1924{
1925 u8 hp_slot;
1926 u8 device;
1927 struct pci_func *func;
1928 struct slot *p_slot = (struct slot *) slot;
1929 struct controller *ctrl = (struct controller *) p_slot->ctrl;
1930
1931 pushbutton_pending = 0;
1932 hp_slot = p_slot->hp_slot;
1933
1934 device = p_slot->device;
1935
1936 if (is_slot_enabled(ctrl, hp_slot)) {
1937 p_slot->state = POWEROFF_STATE;
1938 /* power Down board */
1939 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1940 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
1941 if (!func) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001942 dbg("Error! func NULL in %s\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001943 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
Adrian Bunk00395412007-10-24 18:25:00 +02001946 if (cpqhp_process_SS(ctrl, func) != 0) {
1947 amber_LED_on(ctrl, hp_slot);
1948 green_LED_on(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Adrian Bunk00395412007-10-24 18:25:00 +02001950 set_SOGO(ctrl);
1951
1952 /* Wait for SOBS to be unset */
1953 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
1956 p_slot->state = STATIC_STATE;
1957 } else {
1958 p_slot->state = POWERON_STATE;
1959 /* slot is off */
1960
1961 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1962 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1963 if (!func) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001964 dbg("Error! func NULL in %s\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001965 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967
Julia Lawallb8d9cb22008-12-21 16:39:37 +01001968 if (ctrl != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (cpqhp_process_SI(ctrl, func) != 0) {
1970 amber_LED_on(ctrl, hp_slot);
1971 green_LED_off(ctrl, hp_slot);
Alex Chiang861fefb2009-03-31 09:23:11 -06001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 set_SOGO(ctrl);
1974
1975 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001976 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 }
1979
1980 p_slot->state = STATIC_STATE;
1981 }
1982
1983 return;
1984}
1985
1986
1987int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
1988{
1989 u8 device, hp_slot;
1990 u16 temp_word;
1991 u32 tempdword;
1992 int rc;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001993 struct slot *p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 int physical_slot = 0;
1995
1996 tempdword = 0;
1997
1998 device = func->device;
1999 hp_slot = device - ctrl->slot_device_offset;
2000 p_slot = cpqhp_find_slot(ctrl, device);
2001 if (p_slot)
2002 physical_slot = p_slot->number;
2003
2004 /* Check to see if the interlock is closed */
2005 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
2006
Quentin Lambert656f9782014-09-07 20:02:47 +02002007 if (tempdword & (0x01 << hp_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 if (func->is_a_board) {
2011 rc = board_replaced(func, ctrl);
2012 } else {
2013 /* add board */
2014 slot_remove(func);
2015
2016 func = cpqhp_slot_create(ctrl->bus);
2017 if (func == NULL)
2018 return 1;
2019
2020 func->bus = ctrl->bus;
2021 func->device = device;
2022 func->function = 0;
2023 func->configured = 0;
2024 func->is_a_board = 1;
2025
2026 /* We have to save the presence info for these slots */
2027 temp_word = ctrl->ctrl_int_comp >> 16;
2028 func->presence_save = (temp_word >> hp_slot) & 0x01;
2029 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2030
2031 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2032 func->switch_save = 0;
2033 } else {
2034 func->switch_save = 0x10;
2035 }
2036
2037 rc = board_added(func, ctrl);
2038 if (rc) {
2039 if (is_bridge(func)) {
2040 bridge_slot_remove(func);
2041 } else
2042 slot_remove(func);
2043
2044 /* Setup slot structure with entry for empty slot */
2045 func = cpqhp_slot_create(ctrl->bus);
2046
2047 if (func == NULL)
2048 return 1;
2049
2050 func->bus = ctrl->bus;
2051 func->device = device;
2052 func->function = 0;
2053 func->configured = 0;
2054 func->is_a_board = 0;
2055
2056 /* We have to save the presence info for these slots */
2057 temp_word = ctrl->ctrl_int_comp >> 16;
2058 func->presence_save = (temp_word >> hp_slot) & 0x01;
2059 func->presence_save |=
2060 (temp_word >> (hp_slot + 7)) & 0x02;
2061
2062 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2063 func->switch_save = 0;
2064 } else {
2065 func->switch_save = 0x10;
2066 }
2067 }
2068 }
2069
Quentin Lambert656f9782014-09-07 20:02:47 +02002070 if (rc)
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002071 dbg("%s: rc = %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 if (p_slot)
2074 update_slot_info(ctrl, p_slot);
2075
2076 return rc;
2077}
2078
2079
2080int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
2081{
2082 u8 device, class_code, header_type, BCR;
2083 u8 index = 0;
2084 u8 replace_flag;
2085 u32 rc = 0;
2086 unsigned int devfn;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002087 struct slot *p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 struct pci_bus *pci_bus = ctrl->pci_bus;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002089 int physical_slot = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Alex Chiang861fefb2009-03-31 09:23:11 -06002091 device = func->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 func = cpqhp_slot_find(ctrl->bus, device, index++);
2093 p_slot = cpqhp_find_slot(ctrl, device);
Quentin Lambert656f9782014-09-07 20:02:47 +02002094 if (p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 physical_slot = p_slot->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 /* Make sure there are no video controllers here */
2098 while (func && !rc) {
2099 pci_bus->number = func->bus;
2100 devfn = PCI_DEVFN(func->device, func->function);
2101
2102 /* Check the Class Code */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002103 rc = pci_bus_read_config_byte(pci_bus, devfn, 0x0B, &class_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (rc)
2105 return rc;
2106
2107 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2108 /* Display/Video adapter (not supported) */
2109 rc = REMOVE_NOT_SUPPORTED;
2110 } else {
2111 /* See if it's a bridge */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002112 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (rc)
2114 return rc;
2115
2116 /* If it's a bridge, check the VGA Enable bit */
2117 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002118 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (rc)
2120 return rc;
2121
2122 /* If the VGA Enable bit is set, remove isn't
2123 * supported */
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002124 if (BCR & PCI_BRIDGE_CTL_VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 rc = REMOVE_NOT_SUPPORTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
2127 }
2128
2129 func = cpqhp_slot_find(ctrl->bus, device, index++);
2130 }
2131
2132 func = cpqhp_slot_find(ctrl->bus, device, 0);
2133 if ((func != NULL) && !rc) {
2134 /* FIXME: Replace flag should be passed into process_SS */
2135 replace_flag = !(ctrl->add_support);
2136 rc = remove_board(func, replace_flag, ctrl);
2137 } else if (!rc) {
2138 rc = 1;
2139 }
2140
2141 if (p_slot)
2142 update_slot_info(ctrl, p_slot);
2143
2144 return rc;
2145}
2146
2147/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08002148 * switch_leds - switch the leds, go from one site to the other.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 * @ctrl: controller to use
2150 * @num_of_slots: number of slots to use
Randy Dunlap26e6c662007-11-28 09:04:30 -08002151 * @work_LED: LED control value
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 * @direction: 1 to start from the left side, 0 to start right.
2153 */
2154static void switch_leds(struct controller *ctrl, const int num_of_slots,
2155 u32 *work_LED, const int direction)
2156{
2157 int loop;
2158
2159 for (loop = 0; loop < num_of_slots; loop++) {
2160 if (direction)
2161 *work_LED = *work_LED >> 1;
2162 else
2163 *work_LED = *work_LED << 1;
2164 writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
2165
2166 set_SOGO(ctrl);
2167
2168 /* Wait for SOGO interrupt */
2169 wait_for_ctrl_irq(ctrl);
2170
2171 /* Get ready for next iteration */
2172 long_delay((2*HZ)/10);
2173 }
2174}
2175
2176/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08002177 * cpqhp_hardware_test - runs hardware tests
2178 * @ctrl: target controller
2179 * @test_num: the number written to the "test" file in sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 *
2181 * For hot plug ctrl folks to play with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 */
2183int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2184{
2185 u32 save_LED;
2186 u32 work_LED;
2187 int loop;
2188 int num_of_slots;
2189
2190 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2191
2192 switch (test_num) {
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002193 case 1:
2194 /* Do stuff here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002196 /* Do that funky LED thing */
2197 /* so we can restore them later */
2198 save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
2199 work_LED = 0x01010101;
2200 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2201 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2202 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2203 switch_leds(ctrl, num_of_slots, &work_LED, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002205 work_LED = 0x01010000;
2206 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2207 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2208 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2209 work_LED = 0x00000101;
2210 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2211 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2212 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2213
2214 work_LED = 0x01010000;
2215 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2216 for (loop = 0; loop < num_of_slots; loop++) {
2217 set_SOGO(ctrl);
2218
2219 /* Wait for SOGO interrupt */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002220 wait_for_ctrl_irq(ctrl);
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002221
2222 /* Get ready for next iteration */
2223 long_delay((3*HZ)/10);
2224 work_LED = work_LED >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 set_SOGO(ctrl);
2228
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002229 /* Wait for SOGO interrupt */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002230 wait_for_ctrl_irq(ctrl);
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002231
2232 /* Get ready for next iteration */
2233 long_delay((3*HZ)/10);
2234 work_LED = work_LED << 16;
2235 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2236 work_LED = work_LED << 1;
2237 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2238 }
2239
2240 /* put it back the way it was */
2241 writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
2242
2243 set_SOGO(ctrl);
2244
2245 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002246 wait_for_ctrl_irq(ctrl);
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002247 break;
2248 case 2:
2249 /* Do other stuff here! */
2250 break;
2251 case 3:
2252 /* and more... */
2253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255 return 0;
2256}
2257
2258
2259/**
2260 * configure_new_device - Configures the PCI header information of one board.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 * @ctrl: pointer to controller structure
2262 * @func: pointer to function structure
2263 * @behind_bridge: 1 if this is a recursive call, 0 if not
2264 * @resources: pointer to set of resource lists
2265 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08002266 * Returns 0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002268static u32 configure_new_device(struct controller *ctrl, struct pci_func *func,
2269 u8 behind_bridge, struct resource_lists *resources)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 u8 temp_byte, function, max_functions, stop_it;
2272 int rc;
2273 u32 ID;
2274 struct pci_func *new_slot;
2275 int index;
2276
2277 new_slot = func;
2278
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002279 dbg("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 /* Check for Multi-function device */
2281 ctrl->pci_bus->number = func->bus;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002282 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (rc) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002284 dbg("%s: rc = %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return rc;
2286 }
2287
2288 if (temp_byte & 0x80) /* Multi-function device */
2289 max_functions = 8;
2290 else
2291 max_functions = 1;
2292
2293 function = 0;
2294
2295 do {
2296 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
2297
2298 if (rc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002299 dbg("configure_new_function failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 index = 0;
2301
2302 while (new_slot) {
2303 new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
2304
2305 if (new_slot)
2306 cpqhp_return_board_resources(new_slot, resources);
2307 }
2308
2309 return rc;
2310 }
2311
2312 function++;
2313
2314 stop_it = 0;
2315
2316 /* The following loop skips to the next present function
2317 * and creates a board structure */
2318
2319 while ((function < max_functions) && (!stop_it)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002320 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002322 if (ID == 0xFFFFFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 function++;
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002324 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 /* Setup slot structure. */
2326 new_slot = cpqhp_slot_create(func->bus);
2327
2328 if (new_slot == NULL)
2329 return 1;
2330
2331 new_slot->bus = func->bus;
2332 new_slot->device = func->device;
2333 new_slot->function = function;
2334 new_slot->is_a_board = 1;
2335 new_slot->status = 0;
2336
2337 stop_it++;
2338 }
2339 }
2340
2341 } while (function < max_functions);
2342 dbg("returning from configure_new_device\n");
2343
2344 return 0;
2345}
2346
2347
2348/*
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002349 * Configuration logic that involves the hotplug data structures and
2350 * their bookkeeping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 */
2352
2353
2354/**
2355 * configure_new_function - Configures the PCI header information of one device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 * @ctrl: pointer to controller structure
2357 * @func: pointer to function structure
2358 * @behind_bridge: 1 if this is a recursive call, 0 if not
2359 * @resources: pointer to set of resource lists
2360 *
2361 * Calls itself recursively for bridged devices.
Randy Dunlap26e6c662007-11-28 09:04:30 -08002362 * Returns 0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
2364static int configure_new_function(struct controller *ctrl, struct pci_func *func,
2365 u8 behind_bridge,
2366 struct resource_lists *resources)
2367{
2368 int cloop;
2369 u8 IRQ = 0;
2370 u8 temp_byte;
2371 u8 device;
2372 u8 class_code;
2373 u16 command;
2374 u16 temp_word;
2375 u32 temp_dword;
2376 u32 rc;
2377 u32 temp_register;
2378 u32 base;
2379 u32 ID;
2380 unsigned int devfn;
2381 struct pci_resource *mem_node;
2382 struct pci_resource *p_mem_node;
2383 struct pci_resource *io_node;
2384 struct pci_resource *bus_node;
2385 struct pci_resource *hold_mem_node;
2386 struct pci_resource *hold_p_mem_node;
2387 struct pci_resource *hold_IO_node;
2388 struct pci_resource *hold_bus_node;
2389 struct irq_mapping irqs;
2390 struct pci_func *new_slot;
2391 struct pci_bus *pci_bus;
2392 struct resource_lists temp_resources;
2393
2394 pci_bus = ctrl->pci_bus;
2395 pci_bus->number = func->bus;
2396 devfn = PCI_DEVFN(func->device, func->function);
2397
2398 /* Check for Bridge */
2399 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2400 if (rc)
2401 return rc;
2402
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002403 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 /* set Primary bus */
2405 dbg("set Primary bus = %d\n", func->bus);
2406 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2407 if (rc)
2408 return rc;
2409
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002410 /* find range of buses to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 dbg("find ranges of buses to use\n");
2412 bus_node = get_max_resource(&(resources->bus_head), 1);
2413
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002414 /* If we don't have any buses to allocate, we can't continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (!bus_node)
2416 return -ENOMEM;
2417
2418 /* set Secondary bus */
2419 temp_byte = bus_node->base;
2420 dbg("set Secondary bus = %d\n", bus_node->base);
2421 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2422 if (rc)
2423 return rc;
2424
2425 /* set subordinate bus */
2426 temp_byte = bus_node->base + bus_node->length - 1;
2427 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2428 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2429 if (rc)
2430 return rc;
2431
2432 /* set subordinate Latency Timer and base Latency Timer */
2433 temp_byte = 0x40;
2434 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2435 if (rc)
2436 return rc;
2437 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2438 if (rc)
2439 return rc;
2440
2441 /* set Cache Line size */
2442 temp_byte = 0x08;
2443 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2444 if (rc)
2445 return rc;
2446
2447 /* Setup the IO, memory, and prefetchable windows */
2448 io_node = get_max_resource(&(resources->io_head), 0x1000);
2449 if (!io_node)
2450 return -ENOMEM;
2451 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2452 if (!mem_node)
2453 return -ENOMEM;
2454 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2455 if (!p_mem_node)
2456 return -ENOMEM;
2457 dbg("Setup the IO, memory, and prefetchable windows\n");
2458 dbg("io_node\n");
2459 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
2460 io_node->length, io_node->next);
2461 dbg("mem_node\n");
2462 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
2463 mem_node->length, mem_node->next);
2464 dbg("p_mem_node\n");
2465 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2466 p_mem_node->length, p_mem_node->next);
2467
2468 /* set up the IRQ info */
2469 if (!resources->irqs) {
2470 irqs.barber_pole = 0;
2471 irqs.interrupt[0] = 0;
2472 irqs.interrupt[1] = 0;
2473 irqs.interrupt[2] = 0;
2474 irqs.interrupt[3] = 0;
2475 irqs.valid_INT = 0;
2476 } else {
2477 irqs.barber_pole = resources->irqs->barber_pole;
2478 irqs.interrupt[0] = resources->irqs->interrupt[0];
2479 irqs.interrupt[1] = resources->irqs->interrupt[1];
2480 irqs.interrupt[2] = resources->irqs->interrupt[2];
2481 irqs.interrupt[3] = resources->irqs->interrupt[3];
2482 irqs.valid_INT = resources->irqs->valid_INT;
2483 }
2484
2485 /* set up resource lists that are now aligned on top and bottom
2486 * for anything behind the bridge. */
2487 temp_resources.bus_head = bus_node;
2488 temp_resources.io_head = io_node;
2489 temp_resources.mem_head = mem_node;
2490 temp_resources.p_mem_head = p_mem_node;
2491 temp_resources.irqs = &irqs;
2492
2493 /* Make copies of the nodes we are going to pass down so that
Alex Chiang427438c2009-03-31 09:23:16 -06002494 * if there is a problem,we can just use these to free resources
2495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2497 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2498 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2499 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2500
2501 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2502 kfree(hold_bus_node);
2503 kfree(hold_IO_node);
2504 kfree(hold_mem_node);
2505 kfree(hold_p_mem_node);
2506
2507 return 1;
2508 }
2509
2510 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2511
2512 bus_node->base += 1;
2513 bus_node->length -= 1;
2514 bus_node->next = NULL;
2515
2516 /* If we have IO resources copy them and fill in the bridge's
2517 * IO range registers */
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002518 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2519 io_node->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002521 /* set IO base and Limit registers */
2522 temp_byte = io_node->base >> 8;
2523 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002525 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2526 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002528 /* Copy the memory resources and fill in the bridge's memory
2529 * range registers.
2530 */
2531 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2532 mem_node->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002534 /* set Mem base and Limit registers */
2535 temp_word = mem_node->base >> 16;
2536 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002538 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2539 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002541 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2542 p_mem_node->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002544 /* set Pre Mem base and Limit registers */
2545 temp_word = p_mem_node->base >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002546 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002548 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002549 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Alex Chiang427438c2009-03-31 09:23:16 -06002551 /* Adjust this to compensate for extra adjustment in first loop
2552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 irqs.barber_pole--;
2554
2555 rc = 0;
2556
2557 /* Here we actually find the devices and configure them */
2558 for (device = 0; (device <= 0x1F) && !rc; device++) {
2559 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2560
2561 ID = 0xFFFFFFFF;
2562 pci_bus->number = hold_bus_node->base;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002563 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 pci_bus->number = func->bus;
2565
2566 if (ID != 0xFFFFFFFF) { /* device present */
2567 /* Setup slot structure. */
2568 new_slot = cpqhp_slot_create(hold_bus_node->base);
2569
2570 if (new_slot == NULL) {
2571 rc = -ENOMEM;
2572 continue;
2573 }
2574
2575 new_slot->bus = hold_bus_node->base;
2576 new_slot->device = device;
2577 new_slot->function = 0;
2578 new_slot->is_a_board = 1;
2579 new_slot->status = 0;
2580
2581 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002582 dbg("configure_new_device rc=0x%x\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 } /* End of IF (device in slot?) */
2584 } /* End of FOR loop */
2585
2586 if (rc)
2587 goto free_and_out;
2588 /* save the interrupt routing information */
2589 if (resources->irqs) {
2590 resources->irqs->interrupt[0] = irqs.interrupt[0];
2591 resources->irqs->interrupt[1] = irqs.interrupt[1];
2592 resources->irqs->interrupt[2] = irqs.interrupt[2];
2593 resources->irqs->interrupt[3] = irqs.interrupt[3];
2594 resources->irqs->valid_INT = irqs.valid_INT;
2595 } else if (!behind_bridge) {
2596 /* We need to hook up the interrupts here */
2597 for (cloop = 0; cloop < 4; cloop++) {
2598 if (irqs.valid_INT & (0x01 << cloop)) {
2599 rc = cpqhp_set_irq(func->bus, func->device,
Bjorn Helgaas98d33332008-12-09 16:11:41 -07002600 cloop + 1, irqs.interrupt[cloop]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if (rc)
2602 goto free_and_out;
2603 }
2604 } /* end of for loop */
2605 }
2606 /* Return unused bus resources
2607 * First use the temporary node to store information for
2608 * the board */
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002609 if (bus_node && temp_resources.bus_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2611
2612 hold_bus_node->next = func->bus_head;
2613 func->bus_head = hold_bus_node;
2614
2615 temp_byte = temp_resources.bus_head->base - 1;
2616
2617 /* set subordinate bus */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002618 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 if (temp_resources.bus_head->length == 0) {
2621 kfree(temp_resources.bus_head);
2622 temp_resources.bus_head = NULL;
2623 } else {
2624 return_resource(&(resources->bus_head), temp_resources.bus_head);
2625 }
2626 }
2627
2628 /* If we have IO space available and there is some left,
2629 * return the unused portion */
2630 if (hold_IO_node && temp_resources.io_head) {
2631 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2632 &hold_IO_node, 0x1000);
2633
2634 /* Check if we were able to split something off */
2635 if (io_node) {
2636 hold_IO_node->base = io_node->base + io_node->length;
2637
2638 temp_byte = (hold_IO_node->base) >> 8;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002639 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_IO_BASE, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 return_resource(&(resources->io_head), io_node);
2642 }
2643
2644 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2645
2646 /* Check if we were able to split something off */
2647 if (io_node) {
2648 /* First use the temporary node to store
2649 * information for the board */
2650 hold_IO_node->length = io_node->base - hold_IO_node->base;
2651
2652 /* If we used any, add it to the board's list */
2653 if (hold_IO_node->length) {
2654 hold_IO_node->next = func->io_head;
2655 func->io_head = hold_IO_node;
2656
2657 temp_byte = (io_node->base - 1) >> 8;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002658 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 return_resource(&(resources->io_head), io_node);
2661 } else {
2662 /* it doesn't need any IO */
2663 temp_word = 0x0000;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002664 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_IO_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 return_resource(&(resources->io_head), io_node);
2667 kfree(hold_IO_node);
2668 }
2669 } else {
2670 /* it used most of the range */
2671 hold_IO_node->next = func->io_head;
2672 func->io_head = hold_IO_node;
2673 }
2674 } else if (hold_IO_node) {
2675 /* it used the whole range */
2676 hold_IO_node->next = func->io_head;
2677 func->io_head = hold_IO_node;
2678 }
2679 /* If we have memory space available and there is some left,
2680 * return the unused portion */
2681 if (hold_mem_node && temp_resources.mem_head) {
2682 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2683 &hold_mem_node, 0x100000);
2684
2685 /* Check if we were able to split something off */
2686 if (mem_node) {
2687 hold_mem_node->base = mem_node->base + mem_node->length;
2688
2689 temp_word = (hold_mem_node->base) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002690 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692 return_resource(&(resources->mem_head), mem_node);
2693 }
2694
2695 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2696
2697 /* Check if we were able to split something off */
2698 if (mem_node) {
2699 /* First use the temporary node to store
2700 * information for the board */
2701 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2702
2703 if (hold_mem_node->length) {
2704 hold_mem_node->next = func->mem_head;
2705 func->mem_head = hold_mem_node;
2706
2707 /* configure end address */
2708 temp_word = (mem_node->base - 1) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002709 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 /* Return unused resources to the pool */
2712 return_resource(&(resources->mem_head), mem_node);
2713 } else {
2714 /* it doesn't need any Mem */
2715 temp_word = 0x0000;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002716 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 return_resource(&(resources->mem_head), mem_node);
2719 kfree(hold_mem_node);
2720 }
2721 } else {
2722 /* it used most of the range */
2723 hold_mem_node->next = func->mem_head;
2724 func->mem_head = hold_mem_node;
2725 }
2726 } else if (hold_mem_node) {
2727 /* it used the whole range */
2728 hold_mem_node->next = func->mem_head;
2729 func->mem_head = hold_mem_node;
2730 }
2731 /* If we have prefetchable memory space available and there
2732 * is some left at the end, return the unused portion */
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002733 if (temp_resources.p_mem_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2735 &hold_p_mem_node, 0x100000);
2736
2737 /* Check if we were able to split something off */
2738 if (p_mem_node) {
2739 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2740
2741 temp_word = (hold_p_mem_node->base) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002742 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 return_resource(&(resources->p_mem_head), p_mem_node);
2745 }
2746
2747 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2748
2749 /* Check if we were able to split something off */
2750 if (p_mem_node) {
2751 /* First use the temporary node to store
2752 * information for the board */
2753 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2754
2755 /* If we used any, add it to the board's list */
2756 if (hold_p_mem_node->length) {
2757 hold_p_mem_node->next = func->p_mem_head;
2758 func->p_mem_head = hold_p_mem_node;
2759
2760 temp_word = (p_mem_node->base - 1) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002761 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 return_resource(&(resources->p_mem_head), p_mem_node);
2764 } else {
2765 /* it doesn't need any PMem */
2766 temp_word = 0x0000;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002767 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 return_resource(&(resources->p_mem_head), p_mem_node);
2770 kfree(hold_p_mem_node);
2771 }
2772 } else {
2773 /* it used the most of the range */
2774 hold_p_mem_node->next = func->p_mem_head;
2775 func->p_mem_head = hold_p_mem_node;
2776 }
2777 } else if (hold_p_mem_node) {
2778 /* it used the whole range */
2779 hold_p_mem_node->next = func->p_mem_head;
2780 func->p_mem_head = hold_p_mem_node;
2781 }
2782 /* We should be configuring an IRQ and the bridge's base address
2783 * registers if it needs them. Although we have never seen such
2784 * a device */
2785
2786 /* enable card */
2787 command = 0x0157; /* = PCI_COMMAND_IO |
2788 * PCI_COMMAND_MEMORY |
2789 * PCI_COMMAND_MASTER |
2790 * PCI_COMMAND_INVALIDATE |
2791 * PCI_COMMAND_PARITY |
2792 * PCI_COMMAND_SERR */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002793 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 /* set Bridge Control Register */
2796 command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2797 * PCI_BRIDGE_CTL_SERR |
2798 * PCI_BRIDGE_CTL_NO_ISA */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002799 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2801 /* Standard device */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002802 rc = pci_bus_read_config_byte(pci_bus, devfn, 0x0B, &class_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2805 /* Display (video) adapter (not supported) */
2806 return DEVICE_TYPE_NOT_SUPPORTED;
2807 }
2808 /* Figure out IO and memory needs */
2809 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2810 temp_register = 0xFFFFFFFF;
2811
2812 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002813 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002815 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 dbg("CND: base = 0x%x\n", temp_register);
2817
2818 if (temp_register) { /* If this register is implemented */
2819 if ((temp_register & 0x03L) == 0x01) {
2820 /* Map IO */
2821
2822 /* set base = amount of IO space */
2823 base = temp_register & 0xFFFFFFFC;
2824 base = ~base + 1;
2825
2826 dbg("CND: length = 0x%x\n", base);
2827 io_node = get_io_resource(&(resources->io_head), base);
2828 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2829 io_node->base, io_node->length, io_node->next);
2830 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2831
2832 /* allocate the resource to the board */
2833 if (io_node) {
2834 base = io_node->base;
2835
2836 io_node->next = func->io_head;
2837 func->io_head = io_node;
2838 } else
2839 return -ENOMEM;
2840 } else if ((temp_register & 0x0BL) == 0x08) {
2841 /* Map prefetchable memory */
2842 base = temp_register & 0xFFFFFFF0;
2843 base = ~base + 1;
2844
2845 dbg("CND: length = 0x%x\n", base);
2846 p_mem_node = get_resource(&(resources->p_mem_head), base);
2847
2848 /* allocate the resource to the board */
2849 if (p_mem_node) {
2850 base = p_mem_node->base;
2851
2852 p_mem_node->next = func->p_mem_head;
2853 func->p_mem_head = p_mem_node;
2854 } else
2855 return -ENOMEM;
2856 } else if ((temp_register & 0x0BL) == 0x00) {
2857 /* Map memory */
2858 base = temp_register & 0xFFFFFFF0;
2859 base = ~base + 1;
2860
2861 dbg("CND: length = 0x%x\n", base);
2862 mem_node = get_resource(&(resources->mem_head), base);
2863
2864 /* allocate the resource to the board */
2865 if (mem_node) {
2866 base = mem_node->base;
2867
2868 mem_node->next = func->mem_head;
2869 func->mem_head = mem_node;
2870 } else
2871 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 } else {
Alan Coxb161dab2012-09-04 15:39:57 +01002873 /* Reserved bits or requesting space below 1M */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 return NOT_ENOUGH_RESOURCES;
2875 }
2876
2877 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2878
2879 /* Check for 64-bit base */
2880 if ((temp_register & 0x07L) == 0x04) {
2881 cloop += 4;
2882
2883 /* Upper 32 bits of address always zero
2884 * on today's systems */
2885 /* FIXME this is probably not true on
2886 * Alpha and ia64??? */
2887 base = 0;
2888 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2889 }
2890 }
2891 } /* End of base register loop */
2892 if (cpqhp_legacy_mode) {
2893 /* Figure out which interrupt pin this function uses */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002894 rc = pci_bus_read_config_byte(pci_bus, devfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 PCI_INTERRUPT_PIN, &temp_byte);
2896
2897 /* If this function needs an interrupt and we are behind
2898 * a bridge and the pin is tied to something that's
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002899 * already mapped, set this one the same */
Alex Chiang861fefb2009-03-31 09:23:11 -06002900 if (temp_byte && resources->irqs &&
2901 (resources->irqs->valid_INT &
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2903 /* We have to share with something already set up */
Alex Chiang861fefb2009-03-31 09:23:11 -06002904 IRQ = resources->irqs->interrupt[(temp_byte +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 resources->irqs->barber_pole - 1) & 0x03];
2906 } else {
2907 /* Program IRQ based on card type */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002908 rc = pci_bus_read_config_byte(pci_bus, devfn, 0x0B, &class_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002910 if (class_code == PCI_BASE_CLASS_STORAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 IRQ = cpqhp_disk_irq;
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002912 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 IRQ = cpqhp_nic_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915
2916 /* IRQ Line */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002917 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919
2920 if (!behind_bridge) {
Bjorn Helgaas98d33332008-12-09 16:11:41 -07002921 rc = cpqhp_set_irq(func->bus, func->device, temp_byte, IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 if (rc)
2923 return 1;
2924 } else {
2925 /* TBD - this code may also belong in the other clause
2926 * of this If statement */
2927 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
2928 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
2929 }
2930
2931 /* Latency Timer */
2932 temp_byte = 0x40;
2933 rc = pci_bus_write_config_byte(pci_bus, devfn,
2934 PCI_LATENCY_TIMER, temp_byte);
2935
2936 /* Cache Line size */
2937 temp_byte = 0x08;
2938 rc = pci_bus_write_config_byte(pci_bus, devfn,
2939 PCI_CACHE_LINE_SIZE, temp_byte);
2940
2941 /* disable ROM base Address */
2942 temp_dword = 0x00L;
2943 rc = pci_bus_write_config_word(pci_bus, devfn,
2944 PCI_ROM_ADDRESS, temp_dword);
2945
2946 /* enable card */
2947 temp_word = 0x0157; /* = PCI_COMMAND_IO |
2948 * PCI_COMMAND_MEMORY |
2949 * PCI_COMMAND_MASTER |
2950 * PCI_COMMAND_INVALIDATE |
2951 * PCI_COMMAND_PARITY |
2952 * PCI_COMMAND_SERR */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002953 rc = pci_bus_write_config_word(pci_bus, devfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 PCI_COMMAND, temp_word);
2955 } else { /* End of Not-A-Bridge else */
2956 /* It's some strange type of PCI adapter (Cardbus?) */
2957 return DEVICE_TYPE_NOT_SUPPORTED;
2958 }
2959
2960 func->configured = 1;
2961
2962 return 0;
2963free_and_out:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002964 cpqhp_destroy_resource_list(&temp_resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002966 return_resource(&(resources->bus_head), hold_bus_node);
2967 return_resource(&(resources->io_head), hold_IO_node);
2968 return_resource(&(resources->mem_head), hold_mem_node);
2969 return_resource(&(resources->p_mem_head), hold_p_mem_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return rc;
2971}