blob: 616df442520b4b69cb929ece971e54da0b8de806 [file] [log] [blame]
Bjorn Helgaas736759e2018-01-26 14:22:04 -06001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Compaq Hot Plug Controller Driver
4 *
5 * Copyright (C) 1995,2001 Compaq Computer Corporation
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001 IBM Corp.
8 *
9 * All rights reserved.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Send feedback to <greg@kroah.com>
12 *
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/slab.h>
19#include <linux/workqueue.h>
20#include <linux/interrupt.h>
21#include <linux/delay.h>
22#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070024#include <linux/pci_hotplug.h>
Christoph Hellwigfa007d82007-08-14 16:17:15 -070025#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "cpqphp.h"
27
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040028static u32 configure_new_device(struct controller *ctrl, struct pci_func *func,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 u8 behind_bridge, struct resource_lists *resources);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040030static int configure_new_function(struct controller *ctrl, struct pci_func *func,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 u8 behind_bridge, struct resource_lists *resources);
32static void interrupt_event_handler(struct controller *ctrl);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christoph Hellwigfa007d82007-08-14 16:17:15 -070035static struct task_struct *cpqhp_event_thread;
Kees Cook34d773f2017-10-16 16:18:02 -070036static struct timer_list *pushbutton_pending; /* = NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* delay is in jiffies to wait for */
39static void long_delay(int delay)
40{
Christoph Hellwigfa007d82007-08-14 16:17:15 -070041 /*
42 * XXX(hch): if someone is bored please convert all callers
43 * to call msleep_interruptible directly. They really want
44 * to specify timeouts in natural units and spend a lot of
45 * effort converting them to jiffies..
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 msleep_interruptible(jiffies_to_msecs(delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50
51/* FIXME: The following line needs to be somewhere else... */
52#define WRONG_BUS_FREQUENCY 0x07
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040053static u8 handle_switch_change(u8 change, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 int hp_slot;
56 u8 rc = 0;
57 u16 temp_word;
58 struct pci_func *func;
59 struct event_info *taskInfo;
60
61 if (!change)
62 return 0;
63
64 /* Switch Change */
65 dbg("cpqsbd: Switch interrupt received.\n");
66
67 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
68 if (change & (0x1L << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -060069 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -060071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 func = cpqhp_slot_find(ctrl->bus,
73 (hp_slot + ctrl->slot_device_offset), 0);
74
75 /* this is the structure that tells the worker thread
Alex Chiang427438c2009-03-31 09:23:16 -060076 * what to do
77 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
79 ctrl->next_event = (ctrl->next_event + 1) % 10;
80 taskInfo->hp_slot = hp_slot;
81
82 rc++;
83
84 temp_word = ctrl->ctrl_int_comp >> 16;
85 func->presence_save = (temp_word >> hp_slot) & 0x01;
86 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
87
88 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -060089 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Switch opened
Alex Chiang427438c2009-03-31 09:23:16 -060091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 func->switch_save = 0;
94
95 taskInfo->event_type = INT_SWITCH_OPEN;
96 } else {
Alex Chiang427438c2009-03-31 09:23:16 -060097 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * Switch closed
Alex Chiang427438c2009-03-31 09:23:16 -060099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 func->switch_save = 0x10;
102
103 taskInfo->event_type = INT_SWITCH_CLOSE;
104 }
105 }
106 }
107
108 return rc;
109}
110
111/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800112 * cpqhp_find_slot - find the struct slot of given device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * @ctrl: scan lots of this controller
114 * @device: the device id to find
115 */
116static struct slot *cpqhp_find_slot(struct controller *ctrl, u8 device)
117{
118 struct slot *slot = ctrl->slot;
119
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600120 while (slot && (slot->device != device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 slot = slot->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return slot;
124}
125
126
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400127static u8 handle_presence_change(u16 change, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 int hp_slot;
130 u8 rc = 0;
131 u8 temp_byte;
132 u16 temp_word;
133 struct pci_func *func;
134 struct event_info *taskInfo;
135 struct slot *p_slot;
136
137 if (!change)
138 return 0;
139
Alex Chiang427438c2009-03-31 09:23:16 -0600140 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * Presence Change
Alex Chiang427438c2009-03-31 09:23:16 -0600142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dbg("cpqsbd: Presence/Notify input change.\n");
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800144 dbg(" Changed bits are 0x%4.4x\n", change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
147 if (change & (0x0101 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600148 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -0600150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 func = cpqhp_slot_find(ctrl->bus,
152 (hp_slot + ctrl->slot_device_offset), 0);
153
154 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
155 ctrl->next_event = (ctrl->next_event + 1) % 10;
156 taskInfo->hp_slot = hp_slot;
157
158 rc++;
159
160 p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
161 if (!p_slot)
162 return 0;
163
164 /* If the switch closed, must be a button
Alex Chiang427438c2009-03-31 09:23:16 -0600165 * If not in button mode, nevermind
166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (func->switch_save && (ctrl->push_button == 1)) {
168 temp_word = ctrl->ctrl_int_comp >> 16;
169 temp_byte = (temp_word >> hp_slot) & 0x01;
170 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
171
172 if (temp_byte != func->presence_save) {
Alex Chiang427438c2009-03-31 09:23:16 -0600173 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * button Pressed (doesn't do anything)
Alex Chiang427438c2009-03-31 09:23:16 -0600175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 dbg("hp_slot %d button pressed\n", hp_slot);
177 taskInfo->event_type = INT_BUTTON_PRESS;
178 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * button Released - TAKE ACTION!!!!
Alex Chiang427438c2009-03-31 09:23:16 -0600181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 dbg("hp_slot %d button released\n", hp_slot);
183 taskInfo->event_type = INT_BUTTON_RELEASE;
184
185 /* Cancel if we are still blinking */
186 if ((p_slot->state == BLINKINGON_STATE)
187 || (p_slot->state == BLINKINGOFF_STATE)) {
188 taskInfo->event_type = INT_BUTTON_CANCEL;
189 dbg("hp_slot %d button cancel\n", hp_slot);
190 } else if ((p_slot->state == POWERON_STATE)
191 || (p_slot->state == POWEROFF_STATE)) {
192 /* info(msg_button_ignore, p_slot->number); */
193 taskInfo->event_type = INT_BUTTON_IGNORE;
194 dbg("hp_slot %d button ignore\n", hp_slot);
195 }
196 }
197 } else {
198 /* Switch is open, assume a presence change
Alex Chiang427438c2009-03-31 09:23:16 -0600199 * Save the presence state
200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 temp_word = ctrl->ctrl_int_comp >> 16;
202 func->presence_save = (temp_word >> hp_slot) & 0x01;
203 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
204
205 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
206 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
207 /* Present */
208 taskInfo->event_type = INT_PRESENCE_ON;
209 } else {
210 /* Not Present */
211 taskInfo->event_type = INT_PRESENCE_OFF;
212 }
213 }
214 }
215 }
216
217 return rc;
218}
219
220
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400221static u8 handle_power_fault(u8 change, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int hp_slot;
224 u8 rc = 0;
225 struct pci_func *func;
226 struct event_info *taskInfo;
227
228 if (!change)
229 return 0;
230
Alex Chiang427438c2009-03-31 09:23:16 -0600231 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * power fault
Alex Chiang427438c2009-03-31 09:23:16 -0600233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 info("power fault interrupt\n");
236
237 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
238 if (change & (0x01 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600239 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -0600241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 func = cpqhp_slot_find(ctrl->bus,
243 (hp_slot + ctrl->slot_device_offset), 0);
244
245 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
246 ctrl->next_event = (ctrl->next_event + 1) % 10;
247 taskInfo->hp_slot = hp_slot;
248
249 rc++;
250
251 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600252 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * power fault Cleared
Alex Chiang427438c2009-03-31 09:23:16 -0600254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 func->status = 0x00;
256
257 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
258 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600259 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * power fault
Alex Chiang427438c2009-03-31 09:23:16 -0600261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 taskInfo->event_type = INT_POWER_FAULT;
263
264 if (ctrl->rev < 4) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800265 amber_LED_on(ctrl, hp_slot);
266 green_LED_off(ctrl, hp_slot);
267 set_SOGO(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* this is a fatal condition, we want
270 * to crash the machine to protect from
271 * data corruption. simulated_NMI
272 * shouldn't ever return */
273 /* FIXME
274 simulated_NMI(hp_slot, ctrl); */
275
276 /* The following code causes a software
277 * crash just in case simulated_NMI did
278 * return */
279 /*FIXME
280 panic(msg_power_fault); */
281 } else {
282 /* set power fault status for this board */
283 func->status = 0xFF;
284 info("power fault bit %x set\n", hp_slot);
285 }
286 }
287 }
288 }
289
290 return rc;
291}
292
293
294/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800295 * sort_by_size - sort nodes on the list by their length, smallest first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * @head: list to sort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298static int sort_by_size(struct pci_resource **head)
299{
300 struct pci_resource *current_res;
301 struct pci_resource *next_res;
302 int out_of_order = 1;
303
304 if (!(*head))
305 return 1;
306
307 if (!((*head)->next))
308 return 0;
309
310 while (out_of_order) {
311 out_of_order = 0;
312
313 /* Special case for swapping list head */
314 if (((*head)->next) &&
315 ((*head)->length > (*head)->next->length)) {
316 out_of_order++;
317 current_res = *head;
318 *head = (*head)->next;
319 current_res->next = (*head)->next;
320 (*head)->next = current_res;
321 }
322
323 current_res = *head;
324
325 while (current_res->next && current_res->next->next) {
326 if (current_res->next->length > current_res->next->next->length) {
327 out_of_order++;
328 next_res = current_res->next;
329 current_res->next = current_res->next->next;
330 current_res = current_res->next;
331 next_res->next = current_res->next;
332 current_res->next = next_res;
333 } else
334 current_res = current_res->next;
335 }
336 } /* End of out_of_order loop */
337
338 return 0;
339}
340
341
342/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800343 * sort_by_max_size - sort nodes on the list by their length, largest first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * @head: list to sort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
346static int sort_by_max_size(struct pci_resource **head)
347{
348 struct pci_resource *current_res;
349 struct pci_resource *next_res;
350 int out_of_order = 1;
351
352 if (!(*head))
353 return 1;
354
355 if (!((*head)->next))
356 return 0;
357
358 while (out_of_order) {
359 out_of_order = 0;
360
361 /* Special case for swapping list head */
362 if (((*head)->next) &&
363 ((*head)->length < (*head)->next->length)) {
364 out_of_order++;
365 current_res = *head;
366 *head = (*head)->next;
367 current_res->next = (*head)->next;
368 (*head)->next = current_res;
369 }
370
371 current_res = *head;
372
373 while (current_res->next && current_res->next->next) {
374 if (current_res->next->length < current_res->next->next->length) {
375 out_of_order++;
376 next_res = current_res->next;
377 current_res->next = current_res->next->next;
378 current_res = current_res->next;
379 next_res->next = current_res->next;
380 current_res->next = next_res;
381 } else
382 current_res = current_res->next;
383 }
384 } /* End of out_of_order loop */
385
386 return 0;
387}
388
389
390/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800391 * do_pre_bridge_resource_split - find node of resources that are unused
392 * @head: new list head
393 * @orig_head: original list head
394 * @alignment: max node size (?)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
396static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
397 struct pci_resource **orig_head, u32 alignment)
398{
399 struct pci_resource *prevnode = NULL;
400 struct pci_resource *node;
401 struct pci_resource *split_node;
402 u32 rc;
403 u32 temp_dword;
404 dbg("do_pre_bridge_resource_split\n");
405
406 if (!(*head) || !(*orig_head))
407 return NULL;
408
409 rc = cpqhp_resource_sort_and_combine(head);
410
411 if (rc)
412 return NULL;
413
414 if ((*head)->base != (*orig_head)->base)
415 return NULL;
416
417 if ((*head)->length == (*orig_head)->length)
418 return NULL;
419
420
421 /* If we got here, there the bridge requires some of the resource, but
Alex Chiang427438c2009-03-31 09:23:16 -0600422 * we may be able to split some off of the front
423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 node = *head;
426
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800427 if (node->length & (alignment - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* this one isn't an aligned length, so we'll make a new entry
Alex Chiang427438c2009-03-31 09:23:16 -0600429 * and split it up.
430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
432
433 if (!split_node)
434 return NULL;
435
436 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
437
438 split_node->base = node->base;
439 split_node->length = temp_dword;
440
441 node->length -= temp_dword;
442 node->base += split_node->length;
443
444 /* Put it in the list */
445 *head = split_node;
446 split_node->next = node;
447 }
448
449 if (node->length < alignment)
450 return NULL;
451
452 /* Now unlink it */
453 if (*head == node) {
454 *head = node->next;
455 } else {
456 prevnode = *head;
457 while (prevnode->next != node)
458 prevnode = prevnode->next;
459
460 prevnode->next = node->next;
461 }
462 node->next = NULL;
463
464 return node;
465}
466
467
468/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800469 * do_bridge_resource_split - find one node of resources that aren't in use
470 * @head: list head
471 * @alignment: max node size (?)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
473static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
474{
475 struct pci_resource *prevnode = NULL;
476 struct pci_resource *node;
477 u32 rc;
478 u32 temp_dword;
479
480 rc = cpqhp_resource_sort_and_combine(head);
481
482 if (rc)
483 return NULL;
484
485 node = *head;
486
487 while (node->next) {
488 prevnode = node;
489 node = node->next;
490 kfree(prevnode);
491 }
492
493 if (node->length < alignment)
494 goto error;
495
496 if (node->base & (alignment - 1)) {
497 /* Short circuit if adjusted size is too small */
498 temp_dword = (node->base | (alignment-1)) + 1;
499 if ((node->length - (temp_dword - node->base)) < alignment)
500 goto error;
501
502 node->length -= (temp_dword - node->base);
503 node->base = temp_dword;
504 }
505
506 if (node->length & (alignment - 1))
507 /* There's stuff in use after this node */
508 goto error;
509
510 return node;
511error:
512 kfree(node);
513 return NULL;
514}
515
516
517/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800518 * get_io_resource - find first node of given size not in ISA aliasing window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * @head: list to search
520 * @size: size of node to find, must be a power of two.
521 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800522 * Description: This function sorts the resource list by size and then returns
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * returns the first node of "size" length that is not in the ISA aliasing
524 * window. If it finds a node larger than "size" it will split it up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
526static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
527{
528 struct pci_resource *prevnode;
529 struct pci_resource *node;
530 struct pci_resource *split_node;
531 u32 temp_dword;
532
533 if (!(*head))
534 return NULL;
535
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600536 if (cpqhp_resource_sort_and_combine(head))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return NULL;
538
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600539 if (sort_by_size(head))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return NULL;
541
542 for (node = *head; node; node = node->next) {
543 if (node->length < size)
544 continue;
545
546 if (node->base & (size - 1)) {
547 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600548 * so we'll make a new entry and split it up
549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 temp_dword = (node->base | (size-1)) + 1;
551
552 /* Short circuit if adjusted size is too small */
553 if ((node->length - (temp_dword - node->base)) < size)
554 continue;
555
556 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
557
558 if (!split_node)
559 return NULL;
560
561 split_node->base = node->base;
562 split_node->length = temp_dword - node->base;
563 node->base = temp_dword;
564 node->length -= split_node->length;
565
566 /* Put it in the list */
567 split_node->next = node->next;
568 node->next = split_node;
569 } /* End of non-aligned base */
570
571 /* Don't need to check if too small since we already did */
572 if (node->length > size) {
573 /* this one is longer than we need
Alex Chiang427438c2009-03-31 09:23:16 -0600574 * so we'll make a new entry and split it up
575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
577
578 if (!split_node)
579 return NULL;
580
581 split_node->base = node->base + size;
582 split_node->length = node->length - size;
583 node->length = size;
584
585 /* Put it in the list */
586 split_node->next = node->next;
587 node->next = split_node;
588 } /* End of too big on top end */
589
590 /* For IO make sure it's not in the ISA aliasing space */
591 if (node->base & 0x300L)
592 continue;
593
594 /* If we got here, then it is the right size
Alex Chiang427438c2009-03-31 09:23:16 -0600595 * Now take it out of the list and break
596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (*head == node) {
598 *head = node->next;
599 } else {
600 prevnode = *head;
601 while (prevnode->next != node)
602 prevnode = prevnode->next;
603
604 prevnode->next = node->next;
605 }
606 node->next = NULL;
607 break;
608 }
609
610 return node;
611}
612
613
614/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800615 * get_max_resource - get largest node which has at least the given size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * @head: the list to search the node in
617 * @size: the minimum size of the node to find
618 *
619 * Description: Gets the largest node that is at least "size" big from the
620 * list pointed to by head. It aligns the node on top and bottom
621 * to "size" alignment before returning it.
622 */
623static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
624{
625 struct pci_resource *max;
626 struct pci_resource *temp;
627 struct pci_resource *split_node;
628 u32 temp_dword;
629
630 if (cpqhp_resource_sort_and_combine(head))
631 return NULL;
632
633 if (sort_by_max_size(head))
634 return NULL;
635
636 for (max = *head; max; max = max->next) {
Alex Chiang861fefb2009-03-31 09:23:11 -0600637 /* If not big enough we could probably just bail,
Alex Chiang427438c2009-03-31 09:23:16 -0600638 * instead we'll continue to the next.
639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (max->length < size)
641 continue;
642
643 if (max->base & (size - 1)) {
644 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600645 * so we'll make a new entry and split it up
646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 temp_dword = (max->base | (size-1)) + 1;
648
649 /* Short circuit if adjusted size is too small */
650 if ((max->length - (temp_dword - max->base)) < size)
651 continue;
652
653 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
654
655 if (!split_node)
656 return NULL;
657
658 split_node->base = max->base;
659 split_node->length = temp_dword - max->base;
660 max->base = temp_dword;
661 max->length -= split_node->length;
662
663 split_node->next = max->next;
664 max->next = split_node;
665 }
666
667 if ((max->base + max->length) & (size - 1)) {
668 /* this one isn't end aligned properly at the top
Alex Chiang427438c2009-03-31 09:23:16 -0600669 * so we'll make a new entry and split it up
670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
672
673 if (!split_node)
674 return NULL;
675 temp_dword = ((max->base + max->length) & ~(size - 1));
676 split_node->base = temp_dword;
677 split_node->length = max->length + max->base
678 - split_node->base;
679 max->length -= split_node->length;
680
681 split_node->next = max->next;
682 max->next = split_node;
683 }
684
685 /* Make sure it didn't shrink too much when we aligned it */
686 if (max->length < size)
687 continue;
688
689 /* Now take it out of the list */
690 temp = *head;
691 if (temp == max) {
692 *head = max->next;
693 } else {
Quentin Lambert656f9782014-09-07 20:02:47 +0200694 while (temp && temp->next != max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 temp = temp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Rickard Strandqvistcab9a122014-05-18 18:02:57 +0200697 if (temp)
698 temp->next = max->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 max->next = NULL;
702 break;
703 }
704
705 return max;
706}
707
708
709/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800710 * get_resource - find resource of given size and split up larger ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 * @head: the list to search for resources
712 * @size: the size limit to use
713 *
714 * Description: This function sorts the resource list by size and then
715 * returns the first node of "size" length. If it finds a node
716 * larger than "size" it will split it up.
717 *
718 * size must be a power of two.
719 */
720static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
721{
722 struct pci_resource *prevnode;
723 struct pci_resource *node;
724 struct pci_resource *split_node;
725 u32 temp_dword;
726
727 if (cpqhp_resource_sort_and_combine(head))
728 return NULL;
729
730 if (sort_by_size(head))
731 return NULL;
732
733 for (node = *head; node; node = node->next) {
734 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800735 __func__, size, node, node->base, node->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (node->length < size)
737 continue;
738
739 if (node->base & (size - 1)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800740 dbg("%s: not aligned\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600742 * so we'll make a new entry and split it up
743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 temp_dword = (node->base | (size-1)) + 1;
745
746 /* Short circuit if adjusted size is too small */
747 if ((node->length - (temp_dword - node->base)) < size)
748 continue;
749
750 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
751
752 if (!split_node)
753 return NULL;
754
755 split_node->base = node->base;
756 split_node->length = temp_dword - node->base;
757 node->base = temp_dword;
758 node->length -= split_node->length;
759
760 split_node->next = node->next;
761 node->next = split_node;
762 } /* End of non-aligned base */
763
764 /* Don't need to check if too small since we already did */
765 if (node->length > size) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800766 dbg("%s: too big\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* this one is longer than we need
Alex Chiang427438c2009-03-31 09:23:16 -0600768 * so we'll make a new entry and split it up
769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
771
772 if (!split_node)
773 return NULL;
774
775 split_node->base = node->base + size;
776 split_node->length = node->length - size;
777 node->length = size;
778
779 /* Put it in the list */
780 split_node->next = node->next;
781 node->next = split_node;
782 } /* End of too big on top end */
783
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800784 dbg("%s: got one!!!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* If we got here, then it is the right size
786 * Now take it out of the list */
787 if (*head == node) {
788 *head = node->next;
789 } else {
790 prevnode = *head;
791 while (prevnode->next != node)
792 prevnode = prevnode->next;
793
794 prevnode->next = node->next;
795 }
796 node->next = NULL;
797 break;
798 }
799 return node;
800}
801
802
803/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800804 * cpqhp_resource_sort_and_combine - sort nodes by base addresses and clean up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * @head: the list to sort and clean up
806 *
807 * Description: Sorts all of the nodes in the list in ascending order by
808 * their base addresses. Also does garbage collection by
809 * combining adjacent nodes.
810 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800811 * Returns %0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
813int cpqhp_resource_sort_and_combine(struct pci_resource **head)
814{
815 struct pci_resource *node1;
816 struct pci_resource *node2;
817 int out_of_order = 1;
818
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800819 dbg("%s: head = %p, *head = %p\n", __func__, head, *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (!(*head))
822 return 1;
823
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800824 dbg("*head->next = %p\n", (*head)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (!(*head)->next)
827 return 0; /* only one item on the list, already sorted! */
828
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800829 dbg("*head->base = 0x%x\n", (*head)->base);
830 dbg("*head->next->base = 0x%x\n", (*head)->next->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 while (out_of_order) {
832 out_of_order = 0;
833
834 /* Special case for swapping list head */
835 if (((*head)->next) &&
836 ((*head)->base > (*head)->next->base)) {
837 node1 = *head;
838 (*head) = (*head)->next;
839 node1->next = (*head)->next;
840 (*head)->next = node1;
841 out_of_order++;
842 }
843
844 node1 = (*head);
845
846 while (node1->next && node1->next->next) {
847 if (node1->next->base > node1->next->next->base) {
848 out_of_order++;
849 node2 = node1->next;
850 node1->next = node1->next->next;
851 node1 = node1->next;
852 node2->next = node1->next;
853 node1->next = node2;
854 } else
855 node1 = node1->next;
856 }
857 } /* End of out_of_order loop */
858
859 node1 = *head;
860
861 while (node1 && node1->next) {
862 if ((node1->base + node1->length) == node1->next->base) {
863 /* Combine */
864 dbg("8..\n");
865 node1->length += node1->next->length;
866 node2 = node1->next;
867 node1->next = node1->next->next;
868 kfree(node2);
869 } else
870 node1 = node1->next;
871 }
872
873 return 0;
874}
875
876
David Howells7d12e782006-10-05 14:55:46 +0100877irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 struct controller *ctrl = data;
880 u8 schedule_flag = 0;
881 u8 reset;
882 u16 misc;
883 u32 Diff;
884 u32 temp_dword;
885
Alex Chiang861fefb2009-03-31 09:23:11 -0600886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 misc = readw(ctrl->hpc_reg + MISC);
Alex Chiang427438c2009-03-31 09:23:16 -0600888 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 * Check to see if it was our interrupt
Alex Chiang427438c2009-03-31 09:23:16 -0600890 */
Quentin Lambert656f9782014-09-07 20:02:47 +0200891 if (!(misc & 0x000C))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (misc & 0x0004) {
Alex Chiang427438c2009-03-31 09:23:16 -0600895 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * Serial Output interrupt Pending
Alex Chiang427438c2009-03-31 09:23:16 -0600897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* Clear the interrupt */
900 misc |= 0x0004;
901 writew(misc, ctrl->hpc_reg + MISC);
902
903 /* Read to clear posted writes */
904 misc = readw(ctrl->hpc_reg + MISC);
905
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800906 dbg("%s - waking up\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 wake_up_interruptible(&ctrl->queue);
908 }
909
910 if (misc & 0x0008) {
911 /* General-interrupt-input interrupt Pending */
912 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
913
914 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
915
916 /* Clear the interrupt */
917 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
918
919 /* Read it back to clear any posted writes */
920 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
921
922 if (!Diff)
923 /* Clear all interrupts */
924 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
925
926 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
927 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
928 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
929 }
930
931 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
932 if (reset & 0x40) {
933 /* Bus reset has completed */
934 reset &= 0xCF;
935 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
936 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
937 wake_up_interruptible(&ctrl->queue);
938 }
939
940 if (schedule_flag) {
Christoph Hellwigfa007d82007-08-14 16:17:15 -0700941 wake_up_process(cpqhp_event_thread);
942 dbg("Waking even thread");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 return IRQ_HANDLED;
945}
946
947
948/**
949 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
Randy Dunlap26e6c662007-11-28 09:04:30 -0800950 * @busnumber: bus where new node is to be located
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800952 * Returns pointer to the new node or %NULL if unsuccessful.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
954struct pci_func *cpqhp_slot_create(u8 busnumber)
955{
956 struct pci_func *new_slot;
957 struct pci_func *next;
958
Mariusz Kozlowski73a985a2007-07-31 19:14:28 +0200959 new_slot = kzalloc(sizeof(*new_slot), GFP_KERNEL);
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600960 if (new_slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return new_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 new_slot->next = NULL;
964 new_slot->configured = 1;
965
966 if (cpqhp_slot_list[busnumber] == NULL) {
967 cpqhp_slot_list[busnumber] = new_slot;
968 } else {
969 next = cpqhp_slot_list[busnumber];
970 while (next->next != NULL)
971 next = next->next;
972 next->next = new_slot;
973 }
974 return new_slot;
975}
976
977
978/**
979 * slot_remove - Removes a node from the linked list of slots.
980 * @old_slot: slot to remove
981 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800982 * Returns %0 if successful, !0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400984static int slot_remove(struct pci_func *old_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct pci_func *next;
987
988 if (old_slot == NULL)
989 return 1;
990
991 next = cpqhp_slot_list[old_slot->bus];
Alex Chiang1d3ecf12009-03-31 09:23:41 -0600992 if (next == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (next == old_slot) {
996 cpqhp_slot_list[old_slot->bus] = old_slot->next;
997 cpqhp_destroy_board_resources(old_slot);
998 kfree(old_slot);
999 return 0;
1000 }
1001
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001002 while ((next->next != old_slot) && (next->next != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 next = next->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (next->next == old_slot) {
1006 next->next = old_slot->next;
1007 cpqhp_destroy_board_resources(old_slot);
1008 kfree(old_slot);
1009 return 0;
1010 } else
1011 return 2;
1012}
1013
1014
1015/**
1016 * bridge_slot_remove - Removes a node from the linked list of slots.
1017 * @bridge: bridge to remove
1018 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001019 * Returns %0 if successful, !0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
1021static int bridge_slot_remove(struct pci_func *bridge)
1022{
1023 u8 subordinateBus, secondaryBus;
1024 u8 tempBus;
1025 struct pci_func *next;
1026
1027 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1028 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1029
1030 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1031 next = cpqhp_slot_list[tempBus];
1032
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001033 while (!slot_remove(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 next = cpqhp_slot_list[tempBus];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037 next = cpqhp_slot_list[bridge->bus];
1038
1039 if (next == NULL)
1040 return 1;
1041
1042 if (next == bridge) {
1043 cpqhp_slot_list[bridge->bus] = bridge->next;
1044 goto out;
1045 }
1046
1047 while ((next->next != bridge) && (next->next != NULL))
1048 next = next->next;
1049
1050 if (next->next != bridge)
1051 return 2;
1052 next->next = bridge->next;
1053out:
1054 kfree(bridge);
1055 return 0;
1056}
1057
1058
1059/**
1060 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1061 * @bus: bus to find
1062 * @device: device to find
Randy Dunlap26e6c662007-11-28 09:04:30 -08001063 * @index: is %0 for first function found, %1 for the second...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 *
1065 * Returns pointer to the node if successful, %NULL otherwise.
1066 */
1067struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1068{
1069 int found = -1;
1070 struct pci_func *func;
1071
1072 func = cpqhp_slot_list[bus];
1073
1074 if ((func == NULL) || ((func->device == device) && (index == 0)))
1075 return func;
1076
1077 if (func->device == device)
1078 found++;
1079
1080 while (func->next != NULL) {
1081 func = func->next;
1082
1083 if (func->device == device)
1084 found++;
1085
1086 if (found == index)
1087 return func;
1088 }
1089
1090 return NULL;
1091}
1092
1093
1094/* DJZ: I don't think is_bridge will work as is.
1095 * FIXME */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001096static int is_bridge(struct pci_func *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 /* Check the header type */
1099 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1100 return 1;
1101 else
1102 return 0;
1103}
1104
1105
1106/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001107 * set_controller_speed - set the frequency and/or mode of a specific controller segment.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * @ctrl: controller to change frequency/mode for.
1109 * @adapter_speed: the speed of the adapter we want to match.
1110 * @hp_slot: the slot number where the adapter is installed.
1111 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001112 * Returns %0 if we successfully change frequency and/or mode to match the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * adapter speed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
1115static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
1116{
1117 struct slot *slot;
Matthew Wilcox3749c512009-12-13 08:11:32 -05001118 struct pci_bus *bus = ctrl->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 u8 reg;
1120 u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
1121 u16 reg16;
1122 u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
Alex Chiang861fefb2009-03-31 09:23:11 -06001123
Matthew Wilcox3749c512009-12-13 08:11:32 -05001124 if (bus->cur_bus_speed == adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
Alex Chiang861fefb2009-03-31 09:23:11 -06001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /* We don't allow freq/mode changes if we find another adapter running
Alex Chiang427438c2009-03-31 09:23:16 -06001128 * in another slot on this controller
1129 */
Quentin Lambert382a9c92014-09-07 20:02:04 +02001130 for (slot = ctrl->slot; slot; slot = slot->next) {
Alex Chiang861fefb2009-03-31 09:23:11 -06001131 if (slot->device == (hp_slot + ctrl->slot_device_offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 continue;
Julia Lawall05a34f52008-10-22 16:44:00 -07001133 if (!slot->hotplug_slot || !slot->hotplug_slot->info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 continue;
Alex Chiang861fefb2009-03-31 09:23:11 -06001135 if (slot->hotplug_slot->info->adapter_status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 continue;
1137 /* If another adapter is running on the same segment but at a
1138 * lower speed/mode, we allow the new adapter to function at
Alex Chiang427438c2009-03-31 09:23:16 -06001139 * this rate if supported
1140 */
Matthew Wilcox3749c512009-12-13 08:11:32 -05001141 if (bus->cur_bus_speed < adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return 0;
1143
1144 return 1;
1145 }
Alex Chiang861fefb2009-03-31 09:23:11 -06001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* If the controller doesn't support freq/mode changes and the
Alex Chiang427438c2009-03-31 09:23:16 -06001148 * controller is running at a higher mode, we bail
1149 */
Matthew Wilcox3749c512009-12-13 08:11:32 -05001150 if ((bus->cur_bus_speed > adapter_speed) && (!ctrl->pcix_speed_capability))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 1;
Alex Chiang861fefb2009-03-31 09:23:11 -06001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* But we allow the adapter to run at a lower rate if possible */
Matthew Wilcox3749c512009-12-13 08:11:32 -05001154 if ((bus->cur_bus_speed < adapter_speed) && (!ctrl->pcix_speed_capability))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return 0;
1156
1157 /* We try to set the max speed supported by both the adapter and
Alex Chiang427438c2009-03-31 09:23:16 -06001158 * controller
1159 */
Matthew Wilcox3749c512009-12-13 08:11:32 -05001160 if (bus->max_bus_speed < adapter_speed) {
1161 if (bus->cur_bus_speed == bus->max_bus_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
Matthew Wilcox3749c512009-12-13 08:11:32 -05001163 adapter_speed = bus->max_bus_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165
1166 writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
1167 writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
Alex Chiang861fefb2009-03-31 09:23:11 -06001168
1169 set_SOGO(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 wait_for_ctrl_irq(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (adapter_speed != PCI_SPEED_133MHz_PCIX)
1173 reg = 0xF5;
1174 else
Alex Chiang861fefb2009-03-31 09:23:11 -06001175 reg = 0xF4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
Alex Chiang861fefb2009-03-31 09:23:11 -06001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
1179 reg16 &= ~0x000F;
Quentin Lambert382a9c92014-09-07 20:02:04 +02001180 switch (adapter_speed) {
Alex Chiang861fefb2009-03-31 09:23:11 -06001181 case(PCI_SPEED_133MHz_PCIX):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 reg = 0x75;
Alex Chiang861fefb2009-03-31 09:23:11 -06001183 reg16 |= 0xB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 break;
1185 case(PCI_SPEED_100MHz_PCIX):
1186 reg = 0x74;
1187 reg16 |= 0xA;
1188 break;
1189 case(PCI_SPEED_66MHz_PCIX):
1190 reg = 0x73;
1191 reg16 |= 0x9;
1192 break;
1193 case(PCI_SPEED_66MHz):
1194 reg = 0x73;
1195 reg16 |= 0x1;
1196 break;
1197 default: /* 33MHz PCI 2.2 */
1198 reg = 0x71;
1199 break;
Alex Chiang861fefb2009-03-31 09:23:11 -06001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202 reg16 |= 0xB << 12;
1203 writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
Alex Chiang861fefb2009-03-31 09:23:11 -06001204
1205 mdelay(5);
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /* Reenable interrupts */
1208 writel(0, ctrl->hpc_reg + INT_MASK);
1209
Alex Chiang861fefb2009-03-31 09:23:11 -06001210 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* Restart state machine */
1213 reg = ~0xF;
1214 pci_read_config_byte(ctrl->pci_dev, 0x43, &reg);
1215 pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
Alex Chiang861fefb2009-03-31 09:23:11 -06001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* Only if mode change...*/
Matthew Wilcox3749c512009-12-13 08:11:32 -05001218 if (((bus->cur_bus_speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001219 ((bus->cur_bus_speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 set_SOGO(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 wait_for_ctrl_irq(ctrl);
1223 mdelay(1100);
Alex Chiang861fefb2009-03-31 09:23:11 -06001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /* Restore LED/Slot state */
1226 writel(leds, ctrl->hpc_reg + LED_CONTROL);
1227 writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
Alex Chiang861fefb2009-03-31 09:23:11 -06001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 set_SOGO(ctrl);
1230 wait_for_ctrl_irq(ctrl);
1231
Matthew Wilcox3749c512009-12-13 08:11:32 -05001232 bus->cur_bus_speed = adapter_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1234
Alex Chiang861fefb2009-03-31 09:23:11 -06001235 info("Successfully changed frequency/mode for adapter in slot %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 slot->number);
1237 return 0;
1238}
1239
Alex Chiang861fefb2009-03-31 09:23:11 -06001240/* the following routines constitute the bulk of the
Alex Chiang427438c2009-03-31 09:23:16 -06001241 * hotplug controller logic
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
1243
1244
1245/**
1246 * board_replaced - Called after a board has been replaced in the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -08001247 * @func: PCI device/function information
1248 * @ctrl: hotplug controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001250 * This is only used if we don't have resources for hot add.
1251 * Turns power on for the board.
1252 * Checks to see if board is the same.
1253 * If board is same, reconfigures it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * If board isn't same, turns it back off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
1256static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
1257{
Matthew Wilcox3749c512009-12-13 08:11:32 -05001258 struct pci_bus *bus = ctrl->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 u8 hp_slot;
1260 u8 temp_byte;
1261 u8 adapter_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 u32 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 hp_slot = func->device - ctrl->slot_device_offset;
1265
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001266 /*
1267 * The switch is open.
1268 */
1269 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 rc = INTERLOCK_OPEN;
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001271 /*
1272 * The board is already on
1273 */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001274 else if (is_slot_enabled(ctrl, hp_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 rc = CARD_FUNCTIONING;
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001276 else {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001277 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /* turn on board without attaching to the bus */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001280 enable_slot_power(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 set_SOGO(ctrl);
1283
1284 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001285 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 /* Change bits in slot power register to force another shift out
1288 * NOTE: this is to work around the timer bug */
1289 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1290 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1291 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1292
1293 set_SOGO(ctrl);
1294
1295 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001296 wait_for_ctrl_irq(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 adapter_speed = get_adapter_speed(ctrl, hp_slot);
Matthew Wilcox3749c512009-12-13 08:11:32 -05001299 if (bus->cur_bus_speed != adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1301 rc = WRONG_BUS_FREQUENCY;
1302
1303 /* turn off board without attaching to the bus */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001304 disable_slot_power(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 set_SOGO(ctrl);
1307
1308 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001309 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001311 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 if (rc)
1314 return rc;
1315
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001316 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001318 slot_enable(ctrl, hp_slot);
1319 green_LED_blink(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001321 amber_LED_off(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 set_SOGO(ctrl);
1324
1325 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001326 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001328 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Wait for ~1 second because of hot plug spec */
1331 long_delay(1*HZ);
1332
1333 /* Check for a power fault */
1334 if (func->status == 0xFF) {
1335 /* power fault occurred, but it was benign */
1336 rc = POWER_FAILURE;
1337 func->status = 0;
1338 } else
1339 rc = cpqhp_valid_replace(ctrl, func);
1340
1341 if (!rc) {
1342 /* It must be the same board */
1343
1344 rc = cpqhp_configure_board(ctrl, func);
1345
Adrian Bunk1305e912006-02-26 22:16:51 +01001346 /* If configuration fails, turn it off
1347 * Get slot won't work for devices behind
1348 * bridges, but in this case it will always be
1349 * called for the "base" bus/dev/func of an
Alex Chiang427438c2009-03-31 09:23:16 -06001350 * adapter.
1351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001353 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001355 amber_LED_on(ctrl, hp_slot);
1356 green_LED_off(ctrl, hp_slot);
1357 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 set_SOGO(ctrl);
1360
1361 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001362 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001364 mutex_unlock(&ctrl->crit_sect);
Adrian Bunk1305e912006-02-26 22:16:51 +01001365
1366 if (rc)
1367 return rc;
1368 else
1369 return 1;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 } else {
1372 /* Something is wrong
1373
1374 * Get slot won't work for devices behind bridges, but
1375 * in this case it will always be called for the "base"
Alex Chiang427438c2009-03-31 09:23:16 -06001376 * bus/dev/func of an adapter.
1377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001379 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001381 amber_LED_on(ctrl, hp_slot);
1382 green_LED_off(ctrl, hp_slot);
1383 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 set_SOGO(ctrl);
1386
1387 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001388 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001390 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 }
1394 return rc;
1395
1396}
1397
1398
1399/**
1400 * board_added - Called after a board has been added to the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -08001401 * @func: PCI device/function info
1402 * @ctrl: hotplug controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001404 * Turns power on for the board.
1405 * Configures board.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
1407static u32 board_added(struct pci_func *func, struct controller *ctrl)
1408{
1409 u8 hp_slot;
1410 u8 temp_byte;
1411 u8 adapter_speed;
1412 int index;
1413 u32 temp_register = 0xFFFFFFFF;
1414 u32 rc = 0;
1415 struct pci_func *new_slot = NULL;
Matthew Wilcox3749c512009-12-13 08:11:32 -05001416 struct pci_bus *bus = ctrl->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct slot *p_slot;
1418 struct resource_lists res_lists;
1419
1420 hp_slot = func->device - ctrl->slot_device_offset;
1421 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001422 __func__, func->device, ctrl->slot_device_offset, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001424 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 /* turn on board without attaching to the bus */
1427 enable_slot_power(ctrl, hp_slot);
1428
1429 set_SOGO(ctrl);
1430
1431 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001432 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* Change bits in slot power register to force another shift out
Alex Chiang427438c2009-03-31 09:23:16 -06001435 * NOTE: this is to work around the timer bug
1436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1438 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1439 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1440
1441 set_SOGO(ctrl);
1442
1443 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001444 wait_for_ctrl_irq(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 adapter_speed = get_adapter_speed(ctrl, hp_slot);
Matthew Wilcox3749c512009-12-13 08:11:32 -05001447 if (bus->cur_bus_speed != adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1449 rc = WRONG_BUS_FREQUENCY;
Alex Chiang861fefb2009-03-31 09:23:11 -06001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /* turn off board without attaching to the bus */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001452 disable_slot_power(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 set_SOGO(ctrl);
1455
1456 /* Wait for SOBS to be unset */
1457 wait_for_ctrl_irq(ctrl);
1458
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001459 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 if (rc)
1462 return rc;
Alex Chiang861fefb2009-03-31 09:23:11 -06001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1465
1466 /* turn on board and blink green LED */
1467
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001468 dbg("%s: before down\n", __func__);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001469 mutex_lock(&ctrl->crit_sect);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001470 dbg("%s: after down\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001472 dbg("%s: before slot_enable\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001473 slot_enable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001475 dbg("%s: before green_LED_blink\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001476 green_LED_blink(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001478 dbg("%s: before amber_LED_blink\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001479 amber_LED_off(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001481 dbg("%s: before set_SOGO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 set_SOGO(ctrl);
1483
1484 /* Wait for SOBS to be unset */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001485 dbg("%s: before wait_for_ctrl_irq\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001486 wait_for_ctrl_irq(ctrl);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001487 dbg("%s: after wait_for_ctrl_irq\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001489 dbg("%s: before up\n", __func__);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001490 mutex_unlock(&ctrl->crit_sect);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001491 dbg("%s: after up\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* Wait for ~1 second because of hot plug spec */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001494 dbg("%s: before long_delay\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 long_delay(1*HZ);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001496 dbg("%s: after long_delay\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001498 dbg("%s: func status = %x\n", __func__, func->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /* Check for a power fault */
1500 if (func->status == 0xFF) {
1501 /* power fault occurred, but it was benign */
1502 temp_register = 0xFFFFFFFF;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001503 dbg("%s: temp register set to %x by power fault\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 rc = POWER_FAILURE;
1505 func->status = 0;
1506 } else {
1507 /* Get vendor/device ID u32 */
1508 ctrl->pci_bus->number = func->bus;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001509 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 -08001510 dbg("%s: pci_read_config_dword returns %d\n", __func__, rc);
1511 dbg("%s: temp_register is %x\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (rc != 0) {
1514 /* Something's wrong here */
1515 temp_register = 0xFFFFFFFF;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001516 dbg("%s: temp register set to %x by error\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 /* Preset return code. It will be changed later if things go okay. */
1519 rc = NO_ADAPTER_PRESENT;
1520 }
1521
1522 /* All F's is an empty slot or an invalid board */
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001523 if (temp_register != 0xFFFFFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 res_lists.io_head = ctrl->io_head;
1525 res_lists.mem_head = ctrl->mem_head;
1526 res_lists.p_mem_head = ctrl->p_mem_head;
1527 res_lists.bus_head = ctrl->bus_head;
1528 res_lists.irqs = NULL;
1529
1530 rc = configure_new_device(ctrl, func, 0, &res_lists);
1531
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001532 dbg("%s: back from configure_new_device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 ctrl->io_head = res_lists.io_head;
1534 ctrl->mem_head = res_lists.mem_head;
1535 ctrl->p_mem_head = res_lists.p_mem_head;
1536 ctrl->bus_head = res_lists.bus_head;
1537
1538 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1539 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1540 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1541 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1542
1543 if (rc) {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001544 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001546 amber_LED_on(ctrl, hp_slot);
1547 green_LED_off(ctrl, hp_slot);
1548 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 set_SOGO(ctrl);
1551
1552 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001553 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001555 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return rc;
1557 } else {
1558 cpqhp_save_slot_config(ctrl, func);
1559 }
1560
1561
1562 func->status = 0;
1563 func->switch_save = 0x10;
1564 func->is_a_board = 0x01;
1565
1566 /* next, we will instantiate the linux pci_dev structures (with
1567 * appropriate driver notification, if already present) */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001568 dbg("%s: configure linux pci_dev structure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 index = 0;
1570 do {
1571 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
Alex Chiang1d3ecf12009-03-31 09:23:41 -06001572 if (new_slot && !new_slot->pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 cpqhp_configure_device(ctrl, new_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } while (new_slot);
1575
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001576 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001578 green_LED_on(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 set_SOGO(ctrl);
1581
1582 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001583 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001585 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 } else {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001587 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001589 amber_LED_on(ctrl, hp_slot);
1590 green_LED_off(ctrl, hp_slot);
1591 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 set_SOGO(ctrl);
1594
1595 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001596 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001598 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 return rc;
1601 }
1602 return 0;
1603}
1604
1605
1606/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001607 * remove_board - Turns off slot and LEDs
1608 * @func: PCI device/function info
1609 * @replace_flag: whether replacing or adding a new device
1610 * @ctrl: target controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001612static u32 remove_board(struct pci_func *func, u32 replace_flag, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 int index;
1615 u8 skip = 0;
1616 u8 device;
1617 u8 hp_slot;
1618 u8 temp_byte;
1619 u32 rc;
1620 struct resource_lists res_lists;
1621 struct pci_func *temp_func;
1622
1623 if (cpqhp_unconfigure_device(func))
1624 return 1;
1625
1626 device = func->device;
1627
1628 hp_slot = func->device - ctrl->slot_device_offset;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001629 dbg("In %s, hp_slot = %d\n", __func__, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /* When we get here, it is safe to change base address registers.
1632 * We will attempt to save the base address register lengths */
1633 if (replace_flag || !ctrl->add_support)
1634 rc = cpqhp_save_base_addr_length(ctrl, func);
1635 else if (!func->bus_head && !func->mem_head &&
1636 !func->p_mem_head && !func->io_head) {
1637 /* Here we check to see if we've saved any of the board's
1638 * resources already. If so, we'll skip the attempt to
1639 * determine what's being used. */
1640 index = 0;
1641 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1642 while (temp_func) {
1643 if (temp_func->bus_head || temp_func->mem_head
1644 || temp_func->p_mem_head || temp_func->io_head) {
1645 skip = 1;
1646 break;
1647 }
1648 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1649 }
1650
1651 if (!skip)
1652 rc = cpqhp_save_used_resources(ctrl, func);
1653 }
1654 /* Change status to shutdown */
1655 if (func->is_a_board)
1656 func->status = 0x01;
1657 func->configured = 0;
1658
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001659 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001661 green_LED_off(ctrl, hp_slot);
1662 slot_disable(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 set_SOGO(ctrl);
1665
1666 /* turn off SERR for slot */
1667 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1668 temp_byte &= ~(0x01 << hp_slot);
1669 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1670
1671 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001672 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001674 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 if (!replace_flag && ctrl->add_support) {
1677 while (func) {
1678 res_lists.io_head = ctrl->io_head;
1679 res_lists.mem_head = ctrl->mem_head;
1680 res_lists.p_mem_head = ctrl->p_mem_head;
1681 res_lists.bus_head = ctrl->bus_head;
1682
1683 cpqhp_return_board_resources(func, &res_lists);
1684
1685 ctrl->io_head = res_lists.io_head;
1686 ctrl->mem_head = res_lists.mem_head;
1687 ctrl->p_mem_head = res_lists.p_mem_head;
1688 ctrl->bus_head = res_lists.bus_head;
1689
1690 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1691 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1692 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1693 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1694
1695 if (is_bridge(func)) {
1696 bridge_slot_remove(func);
1697 } else
1698 slot_remove(func);
1699
1700 func = cpqhp_slot_find(ctrl->bus, device, 0);
1701 }
1702
1703 /* Setup slot structure with entry for empty slot */
1704 func = cpqhp_slot_create(ctrl->bus);
1705
1706 if (func == NULL)
1707 return 1;
1708
1709 func->bus = ctrl->bus;
1710 func->device = device;
1711 func->function = 0;
1712 func->configured = 0;
1713 func->switch_save = 0x10;
1714 func->is_a_board = 0;
1715 func->p_task_event = NULL;
1716 }
1717
1718 return 0;
1719}
1720
Kees Cook34d773f2017-10-16 16:18:02 -07001721static void pushbutton_helper_thread(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Kees Cook34d773f2017-10-16 16:18:02 -07001723 pushbutton_pending = t;
1724
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001725 wake_up_process(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728
1729/* this is the main worker thread */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001730static int event_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 struct controller *ctrl;
Ingo Molnar60ac8f22007-07-24 11:16:37 +02001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 while (1) {
1735 dbg("!!!!event_thread sleeping\n");
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001736 set_current_state(TASK_INTERRUPTIBLE);
1737 schedule();
1738
1739 if (kthread_should_stop())
1740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* Do stuff here */
1742 if (pushbutton_pending)
1743 cpqhp_pushbutton_thread(pushbutton_pending);
1744 else
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001745 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl = ctrl->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 interrupt_event_handler(ctrl);
1747 }
1748 dbg("event_thread signals exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return 0;
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752int cpqhp_event_start_thread(void)
1753{
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001754 cpqhp_event_thread = kthread_run(event_thread, NULL, "phpd_event");
1755 if (IS_ERR(cpqhp_event_thread)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001756 err("Can't start up our event thread\n");
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001757 return PTR_ERR(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return 0;
1761}
1762
1763
1764void cpqhp_event_stop_thread(void)
1765{
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001766 kthread_stop(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
1769
1770static int update_slot_info(struct controller *ctrl, struct slot *slot)
1771{
1772 struct hotplug_slot_info *info;
1773 int result;
1774
1775 info = kmalloc(sizeof(*info), GFP_KERNEL);
1776 if (!info)
1777 return -ENOMEM;
1778
1779 info->power_status = get_slot_enabled(ctrl, slot);
1780 info->attention_status = cpq_get_attention_status(ctrl, slot);
1781 info->latch_status = cpq_get_latch_status(ctrl, slot);
1782 info->adapter_status = get_presence_status(ctrl, slot);
1783 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001784 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return result;
1786}
1787
1788static void interrupt_event_handler(struct controller *ctrl)
1789{
1790 int loop = 0;
1791 int change = 1;
1792 struct pci_func *func;
1793 u8 hp_slot;
1794 struct slot *p_slot;
1795
1796 while (change) {
1797 change = 0;
1798
1799 for (loop = 0; loop < 10; loop++) {
1800 /* dbg("loop %d\n", loop); */
1801 if (ctrl->event_queue[loop].event_type != 0) {
1802 hp_slot = ctrl->event_queue[loop].hp_slot;
1803
1804 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1805 if (!func)
1806 return;
1807
1808 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1809 if (!p_slot)
1810 return;
1811
1812 dbg("hp_slot %d, func %p, p_slot %p\n",
1813 hp_slot, func, p_slot);
1814
1815 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1816 dbg("button pressed\n");
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001817 } else if (ctrl->event_queue[loop].event_type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 INT_BUTTON_CANCEL) {
1819 dbg("button cancel\n");
1820 del_timer(&p_slot->task_event);
1821
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001822 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (p_slot->state == BLINKINGOFF_STATE) {
1825 /* slot is on */
1826 dbg("turn on green LED\n");
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001827 green_LED_on(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 } else if (p_slot->state == BLINKINGON_STATE) {
1829 /* slot is off */
1830 dbg("turn off green LED\n");
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001831 green_LED_off(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
1834 info(msg_button_cancel, p_slot->number);
1835
1836 p_slot->state = STATIC_STATE;
1837
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001838 amber_LED_off(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 set_SOGO(ctrl);
1841
1842 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001843 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001845 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847 /*** button Released (No action on press...) */
1848 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1849 dbg("button release\n");
1850
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001851 if (is_slot_enabled(ctrl, hp_slot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 dbg("slot is on\n");
1853 p_slot->state = BLINKINGOFF_STATE;
1854 info(msg_button_off, p_slot->number);
1855 } else {
1856 dbg("slot is off\n");
1857 p_slot->state = BLINKINGON_STATE;
1858 info(msg_button_on, p_slot->number);
1859 }
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001860 mutex_lock(&ctrl->crit_sect);
Alex Chiang861fefb2009-03-31 09:23:11 -06001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 dbg("blink green LED and turn off amber\n");
Alex Chiang861fefb2009-03-31 09:23:11 -06001863
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001864 amber_LED_off(ctrl, hp_slot);
1865 green_LED_blink(ctrl, hp_slot);
Alex Chiang861fefb2009-03-31 09:23:11 -06001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 set_SOGO(ctrl);
1868
1869 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001870 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001872 mutex_unlock(&ctrl->crit_sect);
Kees Cook34d773f2017-10-16 16:18:02 -07001873 timer_setup(&p_slot->task_event,
1874 pushbutton_helper_thread,
1875 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 p_slot->hp_slot = hp_slot;
1877 p_slot->ctrl = ctrl;
1878/* p_slot->physical_slot = physical_slot; */
1879 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 dbg("add_timer p_slot = %p\n", p_slot);
1882 add_timer(&p_slot->task_event);
1883 }
1884 /***********POWER FAULT */
1885 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1886 dbg("power fault\n");
1887 } else {
1888 /* refresh notification */
Sasha Levin3ecd9d02012-12-20 14:11:23 -05001889 update_slot_info(ctrl, p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891
1892 ctrl->event_queue[loop].event_type = 0;
1893
1894 change = 1;
1895 }
1896 } /* End of FOR loop */
1897 }
1898
1899 return;
1900}
1901
1902
1903/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001904 * cpqhp_pushbutton_thread - handle pushbutton events
1905 * @slot: target slot (struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001907 * Scheduled procedure to handle blocking stuff for the pushbuttons.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 * Handles all pending events and exits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
Kees Cook34d773f2017-10-16 16:18:02 -07001910void cpqhp_pushbutton_thread(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 u8 hp_slot;
1913 u8 device;
1914 struct pci_func *func;
Kees Cook34d773f2017-10-16 16:18:02 -07001915 struct slot *p_slot = from_timer(p_slot, t, task_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct controller *ctrl = (struct controller *) p_slot->ctrl;
1917
Kees Cook34d773f2017-10-16 16:18:02 -07001918 pushbutton_pending = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 hp_slot = p_slot->hp_slot;
1920
1921 device = p_slot->device;
1922
1923 if (is_slot_enabled(ctrl, hp_slot)) {
1924 p_slot->state = POWEROFF_STATE;
1925 /* power Down board */
1926 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1927 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
1928 if (!func) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001929 dbg("Error! func NULL in %s\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001930 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932
Adrian Bunk00395412007-10-24 18:25:00 +02001933 if (cpqhp_process_SS(ctrl, func) != 0) {
1934 amber_LED_on(ctrl, hp_slot);
1935 green_LED_on(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Adrian Bunk00395412007-10-24 18:25:00 +02001937 set_SOGO(ctrl);
1938
1939 /* Wait for SOBS to be unset */
1940 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
1943 p_slot->state = STATIC_STATE;
1944 } else {
1945 p_slot->state = POWERON_STATE;
1946 /* slot is off */
1947
1948 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1949 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1950 if (!func) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001951 dbg("Error! func NULL in %s\n", __func__);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001952 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954
Julia Lawallb8d9cb22008-12-21 16:39:37 +01001955 if (ctrl != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (cpqhp_process_SI(ctrl, func) != 0) {
1957 amber_LED_on(ctrl, hp_slot);
1958 green_LED_off(ctrl, hp_slot);
Alex Chiang861fefb2009-03-31 09:23:11 -06001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 set_SOGO(ctrl);
1961
1962 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001963 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 }
1966
1967 p_slot->state = STATIC_STATE;
1968 }
1969
1970 return;
1971}
1972
1973
1974int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
1975{
1976 u8 device, hp_slot;
1977 u16 temp_word;
1978 u32 tempdword;
1979 int rc;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001980 struct slot *p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 int physical_slot = 0;
1982
1983 tempdword = 0;
1984
1985 device = func->device;
1986 hp_slot = device - ctrl->slot_device_offset;
1987 p_slot = cpqhp_find_slot(ctrl, device);
1988 if (p_slot)
1989 physical_slot = p_slot->number;
1990
1991 /* Check to see if the interlock is closed */
1992 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
1993
Quentin Lambert656f9782014-09-07 20:02:47 +02001994 if (tempdword & (0x01 << hp_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 if (func->is_a_board) {
1998 rc = board_replaced(func, ctrl);
1999 } else {
2000 /* add board */
2001 slot_remove(func);
2002
2003 func = cpqhp_slot_create(ctrl->bus);
2004 if (func == NULL)
2005 return 1;
2006
2007 func->bus = ctrl->bus;
2008 func->device = device;
2009 func->function = 0;
2010 func->configured = 0;
2011 func->is_a_board = 1;
2012
2013 /* We have to save the presence info for these slots */
2014 temp_word = ctrl->ctrl_int_comp >> 16;
2015 func->presence_save = (temp_word >> hp_slot) & 0x01;
2016 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2017
2018 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2019 func->switch_save = 0;
2020 } else {
2021 func->switch_save = 0x10;
2022 }
2023
2024 rc = board_added(func, ctrl);
2025 if (rc) {
2026 if (is_bridge(func)) {
2027 bridge_slot_remove(func);
2028 } else
2029 slot_remove(func);
2030
2031 /* Setup slot structure with entry for empty slot */
2032 func = cpqhp_slot_create(ctrl->bus);
2033
2034 if (func == NULL)
2035 return 1;
2036
2037 func->bus = ctrl->bus;
2038 func->device = device;
2039 func->function = 0;
2040 func->configured = 0;
2041 func->is_a_board = 0;
2042
2043 /* We have to save the presence info for these slots */
2044 temp_word = ctrl->ctrl_int_comp >> 16;
2045 func->presence_save = (temp_word >> hp_slot) & 0x01;
2046 func->presence_save |=
2047 (temp_word >> (hp_slot + 7)) & 0x02;
2048
2049 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2050 func->switch_save = 0;
2051 } else {
2052 func->switch_save = 0x10;
2053 }
2054 }
2055 }
2056
Quentin Lambert656f9782014-09-07 20:02:47 +02002057 if (rc)
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002058 dbg("%s: rc = %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 if (p_slot)
2061 update_slot_info(ctrl, p_slot);
2062
2063 return rc;
2064}
2065
2066
2067int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
2068{
2069 u8 device, class_code, header_type, BCR;
2070 u8 index = 0;
2071 u8 replace_flag;
2072 u32 rc = 0;
2073 unsigned int devfn;
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002074 struct slot *p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct pci_bus *pci_bus = ctrl->pci_bus;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002076 int physical_slot = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Alex Chiang861fefb2009-03-31 09:23:11 -06002078 device = func->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 func = cpqhp_slot_find(ctrl->bus, device, index++);
2080 p_slot = cpqhp_find_slot(ctrl, device);
Quentin Lambert656f9782014-09-07 20:02:47 +02002081 if (p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 physical_slot = p_slot->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* Make sure there are no video controllers here */
2085 while (func && !rc) {
2086 pci_bus->number = func->bus;
2087 devfn = PCI_DEVFN(func->device, func->function);
2088
2089 /* Check the Class Code */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002090 rc = pci_bus_read_config_byte(pci_bus, devfn, 0x0B, &class_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (rc)
2092 return rc;
2093
2094 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2095 /* Display/Video adapter (not supported) */
2096 rc = REMOVE_NOT_SUPPORTED;
2097 } else {
2098 /* See if it's a bridge */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002099 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (rc)
2101 return rc;
2102
2103 /* If it's a bridge, check the VGA Enable bit */
2104 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002105 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (rc)
2107 return rc;
2108
2109 /* If the VGA Enable bit is set, remove isn't
2110 * supported */
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002111 if (BCR & PCI_BRIDGE_CTL_VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 rc = REMOVE_NOT_SUPPORTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 }
2115
2116 func = cpqhp_slot_find(ctrl->bus, device, index++);
2117 }
2118
2119 func = cpqhp_slot_find(ctrl->bus, device, 0);
2120 if ((func != NULL) && !rc) {
2121 /* FIXME: Replace flag should be passed into process_SS */
2122 replace_flag = !(ctrl->add_support);
2123 rc = remove_board(func, replace_flag, ctrl);
2124 } else if (!rc) {
2125 rc = 1;
2126 }
2127
2128 if (p_slot)
2129 update_slot_info(ctrl, p_slot);
2130
2131 return rc;
2132}
2133
2134/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08002135 * switch_leds - switch the leds, go from one site to the other.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 * @ctrl: controller to use
2137 * @num_of_slots: number of slots to use
Randy Dunlap26e6c662007-11-28 09:04:30 -08002138 * @work_LED: LED control value
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 * @direction: 1 to start from the left side, 0 to start right.
2140 */
2141static void switch_leds(struct controller *ctrl, const int num_of_slots,
2142 u32 *work_LED, const int direction)
2143{
2144 int loop;
2145
2146 for (loop = 0; loop < num_of_slots; loop++) {
2147 if (direction)
2148 *work_LED = *work_LED >> 1;
2149 else
2150 *work_LED = *work_LED << 1;
2151 writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
2152
2153 set_SOGO(ctrl);
2154
2155 /* Wait for SOGO interrupt */
2156 wait_for_ctrl_irq(ctrl);
2157
2158 /* Get ready for next iteration */
2159 long_delay((2*HZ)/10);
2160 }
2161}
2162
2163/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08002164 * cpqhp_hardware_test - runs hardware tests
2165 * @ctrl: target controller
2166 * @test_num: the number written to the "test" file in sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 *
2168 * For hot plug ctrl folks to play with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 */
2170int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2171{
2172 u32 save_LED;
2173 u32 work_LED;
2174 int loop;
2175 int num_of_slots;
2176
2177 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2178
2179 switch (test_num) {
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002180 case 1:
2181 /* Do stuff here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002183 /* Do that funky LED thing */
2184 /* so we can restore them later */
2185 save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
2186 work_LED = 0x01010101;
2187 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2188 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2189 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2190 switch_leds(ctrl, num_of_slots, &work_LED, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002192 work_LED = 0x01010000;
2193 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2194 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2195 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2196 work_LED = 0x00000101;
2197 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2198 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2199 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2200
2201 work_LED = 0x01010000;
2202 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2203 for (loop = 0; loop < num_of_slots; loop++) {
2204 set_SOGO(ctrl);
2205
2206 /* Wait for SOGO interrupt */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002207 wait_for_ctrl_irq(ctrl);
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002208
2209 /* Get ready for next iteration */
2210 long_delay((3*HZ)/10);
2211 work_LED = work_LED >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 set_SOGO(ctrl);
2215
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002216 /* Wait for SOGO interrupt */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002217 wait_for_ctrl_irq(ctrl);
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002218
2219 /* Get ready for next iteration */
2220 long_delay((3*HZ)/10);
2221 work_LED = work_LED << 16;
2222 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2223 work_LED = work_LED << 1;
2224 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2225 }
2226
2227 /* put it back the way it was */
2228 writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
2229
2230 set_SOGO(ctrl);
2231
2232 /* Wait for SOBS to be unset */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002233 wait_for_ctrl_irq(ctrl);
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002234 break;
2235 case 2:
2236 /* Do other stuff here! */
2237 break;
2238 case 3:
2239 /* and more... */
2240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 return 0;
2243}
2244
2245
2246/**
2247 * configure_new_device - Configures the PCI header information of one board.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 * @ctrl: pointer to controller structure
2249 * @func: pointer to function structure
2250 * @behind_bridge: 1 if this is a recursive call, 0 if not
2251 * @resources: pointer to set of resource lists
2252 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08002253 * Returns 0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04002255static u32 configure_new_device(struct controller *ctrl, struct pci_func *func,
2256 u8 behind_bridge, struct resource_lists *resources)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 u8 temp_byte, function, max_functions, stop_it;
2259 int rc;
2260 u32 ID;
2261 struct pci_func *new_slot;
2262 int index;
2263
2264 new_slot = func;
2265
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002266 dbg("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* Check for Multi-function device */
2268 ctrl->pci_bus->number = func->bus;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002269 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 -07002270 if (rc) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002271 dbg("%s: rc = %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return rc;
2273 }
2274
2275 if (temp_byte & 0x80) /* Multi-function device */
2276 max_functions = 8;
2277 else
2278 max_functions = 1;
2279
2280 function = 0;
2281
2282 do {
2283 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
2284
2285 if (rc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002286 dbg("configure_new_function failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 index = 0;
2288
2289 while (new_slot) {
2290 new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
2291
2292 if (new_slot)
2293 cpqhp_return_board_resources(new_slot, resources);
2294 }
2295
2296 return rc;
2297 }
2298
2299 function++;
2300
2301 stop_it = 0;
2302
2303 /* The following loop skips to the next present function
2304 * and creates a board structure */
2305
2306 while ((function < max_functions) && (!stop_it)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002307 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002309 if (ID == 0xFFFFFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 function++;
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002311 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* Setup slot structure. */
2313 new_slot = cpqhp_slot_create(func->bus);
2314
2315 if (new_slot == NULL)
2316 return 1;
2317
2318 new_slot->bus = func->bus;
2319 new_slot->device = func->device;
2320 new_slot->function = function;
2321 new_slot->is_a_board = 1;
2322 new_slot->status = 0;
2323
2324 stop_it++;
2325 }
2326 }
2327
2328 } while (function < max_functions);
2329 dbg("returning from configure_new_device\n");
2330
2331 return 0;
2332}
2333
2334
2335/*
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002336 * Configuration logic that involves the hotplug data structures and
2337 * their bookkeeping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
2339
2340
2341/**
2342 * configure_new_function - Configures the PCI header information of one device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 * @ctrl: pointer to controller structure
2344 * @func: pointer to function structure
2345 * @behind_bridge: 1 if this is a recursive call, 0 if not
2346 * @resources: pointer to set of resource lists
2347 *
2348 * Calls itself recursively for bridged devices.
Randy Dunlap26e6c662007-11-28 09:04:30 -08002349 * Returns 0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
2351static int configure_new_function(struct controller *ctrl, struct pci_func *func,
2352 u8 behind_bridge,
2353 struct resource_lists *resources)
2354{
2355 int cloop;
2356 u8 IRQ = 0;
2357 u8 temp_byte;
2358 u8 device;
2359 u8 class_code;
2360 u16 command;
2361 u16 temp_word;
2362 u32 temp_dword;
2363 u32 rc;
2364 u32 temp_register;
2365 u32 base;
2366 u32 ID;
2367 unsigned int devfn;
2368 struct pci_resource *mem_node;
2369 struct pci_resource *p_mem_node;
2370 struct pci_resource *io_node;
2371 struct pci_resource *bus_node;
2372 struct pci_resource *hold_mem_node;
2373 struct pci_resource *hold_p_mem_node;
2374 struct pci_resource *hold_IO_node;
2375 struct pci_resource *hold_bus_node;
2376 struct irq_mapping irqs;
2377 struct pci_func *new_slot;
2378 struct pci_bus *pci_bus;
2379 struct resource_lists temp_resources;
2380
2381 pci_bus = ctrl->pci_bus;
2382 pci_bus->number = func->bus;
2383 devfn = PCI_DEVFN(func->device, func->function);
2384
2385 /* Check for Bridge */
2386 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2387 if (rc)
2388 return rc;
2389
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002390 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 /* set Primary bus */
2392 dbg("set Primary bus = %d\n", func->bus);
2393 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2394 if (rc)
2395 return rc;
2396
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002397 /* find range of buses to use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 dbg("find ranges of buses to use\n");
2399 bus_node = get_max_resource(&(resources->bus_head), 1);
2400
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002401 /* If we don't have any buses to allocate, we can't continue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (!bus_node)
2403 return -ENOMEM;
2404
2405 /* set Secondary bus */
2406 temp_byte = bus_node->base;
2407 dbg("set Secondary bus = %d\n", bus_node->base);
2408 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2409 if (rc)
2410 return rc;
2411
2412 /* set subordinate bus */
2413 temp_byte = bus_node->base + bus_node->length - 1;
2414 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2415 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2416 if (rc)
2417 return rc;
2418
2419 /* set subordinate Latency Timer and base Latency Timer */
2420 temp_byte = 0x40;
2421 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2422 if (rc)
2423 return rc;
2424 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2425 if (rc)
2426 return rc;
2427
2428 /* set Cache Line size */
2429 temp_byte = 0x08;
2430 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2431 if (rc)
2432 return rc;
2433
2434 /* Setup the IO, memory, and prefetchable windows */
2435 io_node = get_max_resource(&(resources->io_head), 0x1000);
2436 if (!io_node)
2437 return -ENOMEM;
2438 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2439 if (!mem_node)
2440 return -ENOMEM;
2441 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2442 if (!p_mem_node)
2443 return -ENOMEM;
2444 dbg("Setup the IO, memory, and prefetchable windows\n");
2445 dbg("io_node\n");
2446 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
2447 io_node->length, io_node->next);
2448 dbg("mem_node\n");
2449 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
2450 mem_node->length, mem_node->next);
2451 dbg("p_mem_node\n");
2452 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2453 p_mem_node->length, p_mem_node->next);
2454
2455 /* set up the IRQ info */
2456 if (!resources->irqs) {
2457 irqs.barber_pole = 0;
2458 irqs.interrupt[0] = 0;
2459 irqs.interrupt[1] = 0;
2460 irqs.interrupt[2] = 0;
2461 irqs.interrupt[3] = 0;
2462 irqs.valid_INT = 0;
2463 } else {
2464 irqs.barber_pole = resources->irqs->barber_pole;
2465 irqs.interrupt[0] = resources->irqs->interrupt[0];
2466 irqs.interrupt[1] = resources->irqs->interrupt[1];
2467 irqs.interrupt[2] = resources->irqs->interrupt[2];
2468 irqs.interrupt[3] = resources->irqs->interrupt[3];
2469 irqs.valid_INT = resources->irqs->valid_INT;
2470 }
2471
2472 /* set up resource lists that are now aligned on top and bottom
2473 * for anything behind the bridge. */
2474 temp_resources.bus_head = bus_node;
2475 temp_resources.io_head = io_node;
2476 temp_resources.mem_head = mem_node;
2477 temp_resources.p_mem_head = p_mem_node;
2478 temp_resources.irqs = &irqs;
2479
2480 /* Make copies of the nodes we are going to pass down so that
Alex Chiang427438c2009-03-31 09:23:16 -06002481 * if there is a problem,we can just use these to free resources
2482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2484 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2485 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2486 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2487
2488 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2489 kfree(hold_bus_node);
2490 kfree(hold_IO_node);
2491 kfree(hold_mem_node);
2492 kfree(hold_p_mem_node);
2493
2494 return 1;
2495 }
2496
2497 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2498
2499 bus_node->base += 1;
2500 bus_node->length -= 1;
2501 bus_node->next = NULL;
2502
2503 /* If we have IO resources copy them and fill in the bridge's
2504 * IO range registers */
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002505 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2506 io_node->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002508 /* set IO base and Limit registers */
2509 temp_byte = io_node->base >> 8;
2510 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002512 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2513 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002515 /* Copy the memory resources and fill in the bridge's memory
2516 * range registers.
2517 */
2518 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2519 mem_node->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002521 /* set Mem base and Limit registers */
2522 temp_word = mem_node->base >> 16;
2523 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002525 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2526 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002528 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2529 p_mem_node->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002531 /* set Pre Mem base and Limit registers */
2532 temp_word = p_mem_node->base >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002533 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002535 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002536 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Alex Chiang427438c2009-03-31 09:23:16 -06002538 /* Adjust this to compensate for extra adjustment in first loop
2539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 irqs.barber_pole--;
2541
2542 rc = 0;
2543
2544 /* Here we actually find the devices and configure them */
2545 for (device = 0; (device <= 0x1F) && !rc; device++) {
2546 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2547
2548 ID = 0xFFFFFFFF;
2549 pci_bus->number = hold_bus_node->base;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002550 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 pci_bus->number = func->bus;
2552
2553 if (ID != 0xFFFFFFFF) { /* device present */
2554 /* Setup slot structure. */
2555 new_slot = cpqhp_slot_create(hold_bus_node->base);
2556
2557 if (new_slot == NULL) {
2558 rc = -ENOMEM;
2559 continue;
2560 }
2561
2562 new_slot->bus = hold_bus_node->base;
2563 new_slot->device = device;
2564 new_slot->function = 0;
2565 new_slot->is_a_board = 1;
2566 new_slot->status = 0;
2567
2568 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002569 dbg("configure_new_device rc=0x%x\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 } /* End of IF (device in slot?) */
2571 } /* End of FOR loop */
2572
2573 if (rc)
2574 goto free_and_out;
2575 /* save the interrupt routing information */
2576 if (resources->irqs) {
2577 resources->irqs->interrupt[0] = irqs.interrupt[0];
2578 resources->irqs->interrupt[1] = irqs.interrupt[1];
2579 resources->irqs->interrupt[2] = irqs.interrupt[2];
2580 resources->irqs->interrupt[3] = irqs.interrupt[3];
2581 resources->irqs->valid_INT = irqs.valid_INT;
2582 } else if (!behind_bridge) {
2583 /* We need to hook up the interrupts here */
2584 for (cloop = 0; cloop < 4; cloop++) {
2585 if (irqs.valid_INT & (0x01 << cloop)) {
2586 rc = cpqhp_set_irq(func->bus, func->device,
Bjorn Helgaas98d33332008-12-09 16:11:41 -07002587 cloop + 1, irqs.interrupt[cloop]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (rc)
2589 goto free_and_out;
2590 }
2591 } /* end of for loop */
2592 }
2593 /* Return unused bus resources
2594 * First use the temporary node to store information for
2595 * the board */
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002596 if (bus_node && temp_resources.bus_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2598
2599 hold_bus_node->next = func->bus_head;
2600 func->bus_head = hold_bus_node;
2601
2602 temp_byte = temp_resources.bus_head->base - 1;
2603
2604 /* set subordinate bus */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002605 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 if (temp_resources.bus_head->length == 0) {
2608 kfree(temp_resources.bus_head);
2609 temp_resources.bus_head = NULL;
2610 } else {
2611 return_resource(&(resources->bus_head), temp_resources.bus_head);
2612 }
2613 }
2614
2615 /* If we have IO space available and there is some left,
2616 * return the unused portion */
2617 if (hold_IO_node && temp_resources.io_head) {
2618 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2619 &hold_IO_node, 0x1000);
2620
2621 /* Check if we were able to split something off */
2622 if (io_node) {
2623 hold_IO_node->base = io_node->base + io_node->length;
2624
2625 temp_byte = (hold_IO_node->base) >> 8;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002626 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_IO_BASE, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 return_resource(&(resources->io_head), io_node);
2629 }
2630
2631 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2632
2633 /* Check if we were able to split something off */
2634 if (io_node) {
2635 /* First use the temporary node to store
2636 * information for the board */
2637 hold_IO_node->length = io_node->base - hold_IO_node->base;
2638
2639 /* If we used any, add it to the board's list */
2640 if (hold_IO_node->length) {
2641 hold_IO_node->next = func->io_head;
2642 func->io_head = hold_IO_node;
2643
2644 temp_byte = (io_node->base - 1) >> 8;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002645 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 return_resource(&(resources->io_head), io_node);
2648 } else {
2649 /* it doesn't need any IO */
2650 temp_word = 0x0000;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002651 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_IO_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653 return_resource(&(resources->io_head), io_node);
2654 kfree(hold_IO_node);
2655 }
2656 } else {
2657 /* it used most of the range */
2658 hold_IO_node->next = func->io_head;
2659 func->io_head = hold_IO_node;
2660 }
2661 } else if (hold_IO_node) {
2662 /* it used the whole range */
2663 hold_IO_node->next = func->io_head;
2664 func->io_head = hold_IO_node;
2665 }
2666 /* If we have memory space available and there is some left,
2667 * return the unused portion */
2668 if (hold_mem_node && temp_resources.mem_head) {
2669 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2670 &hold_mem_node, 0x100000);
2671
2672 /* Check if we were able to split something off */
2673 if (mem_node) {
2674 hold_mem_node->base = mem_node->base + mem_node->length;
2675
2676 temp_word = (hold_mem_node->base) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002677 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 return_resource(&(resources->mem_head), mem_node);
2680 }
2681
2682 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2683
2684 /* Check if we were able to split something off */
2685 if (mem_node) {
2686 /* First use the temporary node to store
2687 * information for the board */
2688 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2689
2690 if (hold_mem_node->length) {
2691 hold_mem_node->next = func->mem_head;
2692 func->mem_head = hold_mem_node;
2693
2694 /* configure end address */
2695 temp_word = (mem_node->base - 1) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002696 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 /* Return unused resources to the pool */
2699 return_resource(&(resources->mem_head), mem_node);
2700 } else {
2701 /* it doesn't need any Mem */
2702 temp_word = 0x0000;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002703 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 return_resource(&(resources->mem_head), mem_node);
2706 kfree(hold_mem_node);
2707 }
2708 } else {
2709 /* it used most of the range */
2710 hold_mem_node->next = func->mem_head;
2711 func->mem_head = hold_mem_node;
2712 }
2713 } else if (hold_mem_node) {
2714 /* it used the whole range */
2715 hold_mem_node->next = func->mem_head;
2716 func->mem_head = hold_mem_node;
2717 }
2718 /* If we have prefetchable memory space available and there
2719 * is some left at the end, return the unused portion */
Sasha Levin3ecd9d02012-12-20 14:11:23 -05002720 if (temp_resources.p_mem_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2722 &hold_p_mem_node, 0x100000);
2723
2724 /* Check if we were able to split something off */
2725 if (p_mem_node) {
2726 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2727
2728 temp_word = (hold_p_mem_node->base) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002729 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 return_resource(&(resources->p_mem_head), p_mem_node);
2732 }
2733
2734 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2735
2736 /* Check if we were able to split something off */
2737 if (p_mem_node) {
2738 /* First use the temporary node to store
2739 * information for the board */
2740 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2741
2742 /* If we used any, add it to the board's list */
2743 if (hold_p_mem_node->length) {
2744 hold_p_mem_node->next = func->p_mem_head;
2745 func->p_mem_head = hold_p_mem_node;
2746
2747 temp_word = (p_mem_node->base - 1) >> 16;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002748 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 return_resource(&(resources->p_mem_head), p_mem_node);
2751 } else {
2752 /* it doesn't need any PMem */
2753 temp_word = 0x0000;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002754 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 return_resource(&(resources->p_mem_head), p_mem_node);
2757 kfree(hold_p_mem_node);
2758 }
2759 } else {
2760 /* it used the most of the range */
2761 hold_p_mem_node->next = func->p_mem_head;
2762 func->p_mem_head = hold_p_mem_node;
2763 }
2764 } else if (hold_p_mem_node) {
2765 /* it used the whole range */
2766 hold_p_mem_node->next = func->p_mem_head;
2767 func->p_mem_head = hold_p_mem_node;
2768 }
2769 /* We should be configuring an IRQ and the bridge's base address
2770 * registers if it needs them. Although we have never seen such
2771 * a device */
2772
2773 /* enable card */
2774 command = 0x0157; /* = PCI_COMMAND_IO |
2775 * PCI_COMMAND_MEMORY |
2776 * PCI_COMMAND_MASTER |
2777 * PCI_COMMAND_INVALIDATE |
2778 * PCI_COMMAND_PARITY |
2779 * PCI_COMMAND_SERR */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002780 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 /* set Bridge Control Register */
2783 command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2784 * PCI_BRIDGE_CTL_SERR |
2785 * PCI_BRIDGE_CTL_NO_ISA */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002786 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2788 /* Standard device */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002789 rc = pci_bus_read_config_byte(pci_bus, devfn, 0x0B, &class_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2792 /* Display (video) adapter (not supported) */
2793 return DEVICE_TYPE_NOT_SUPPORTED;
2794 }
2795 /* Figure out IO and memory needs */
2796 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2797 temp_register = 0xFFFFFFFF;
2798
2799 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002800 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002802 rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 dbg("CND: base = 0x%x\n", temp_register);
2804
2805 if (temp_register) { /* If this register is implemented */
2806 if ((temp_register & 0x03L) == 0x01) {
2807 /* Map IO */
2808
2809 /* set base = amount of IO space */
2810 base = temp_register & 0xFFFFFFFC;
2811 base = ~base + 1;
2812
2813 dbg("CND: length = 0x%x\n", base);
2814 io_node = get_io_resource(&(resources->io_head), base);
Shawn Lin205adda2018-02-25 10:01:42 +08002815 if (!io_node)
2816 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2818 io_node->base, io_node->length, io_node->next);
2819 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2820
2821 /* allocate the resource to the board */
Shawn Lin205adda2018-02-25 10:01:42 +08002822 base = io_node->base;
2823 io_node->next = func->io_head;
2824 func->io_head = io_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 } else if ((temp_register & 0x0BL) == 0x08) {
2826 /* Map prefetchable memory */
2827 base = temp_register & 0xFFFFFFF0;
2828 base = ~base + 1;
2829
2830 dbg("CND: length = 0x%x\n", base);
2831 p_mem_node = get_resource(&(resources->p_mem_head), base);
2832
2833 /* allocate the resource to the board */
2834 if (p_mem_node) {
2835 base = p_mem_node->base;
2836
2837 p_mem_node->next = func->p_mem_head;
2838 func->p_mem_head = p_mem_node;
2839 } else
2840 return -ENOMEM;
2841 } else if ((temp_register & 0x0BL) == 0x00) {
2842 /* Map memory */
2843 base = temp_register & 0xFFFFFFF0;
2844 base = ~base + 1;
2845
2846 dbg("CND: length = 0x%x\n", base);
2847 mem_node = get_resource(&(resources->mem_head), base);
2848
2849 /* allocate the resource to the board */
2850 if (mem_node) {
2851 base = mem_node->base;
2852
2853 mem_node->next = func->mem_head;
2854 func->mem_head = mem_node;
2855 } else
2856 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 } else {
Alan Coxb161dab2012-09-04 15:39:57 +01002858 /* Reserved bits or requesting space below 1M */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return NOT_ENOUGH_RESOURCES;
2860 }
2861
2862 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2863
2864 /* Check for 64-bit base */
2865 if ((temp_register & 0x07L) == 0x04) {
2866 cloop += 4;
2867
2868 /* Upper 32 bits of address always zero
2869 * on today's systems */
2870 /* FIXME this is probably not true on
2871 * Alpha and ia64??? */
2872 base = 0;
2873 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2874 }
2875 }
2876 } /* End of base register loop */
2877 if (cpqhp_legacy_mode) {
2878 /* Figure out which interrupt pin this function uses */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002879 rc = pci_bus_read_config_byte(pci_bus, devfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 PCI_INTERRUPT_PIN, &temp_byte);
2881
2882 /* If this function needs an interrupt and we are behind
2883 * a bridge and the pin is tied to something that's
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002884 * already mapped, set this one the same */
Alex Chiang861fefb2009-03-31 09:23:11 -06002885 if (temp_byte && resources->irqs &&
2886 (resources->irqs->valid_INT &
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2888 /* We have to share with something already set up */
Alex Chiang861fefb2009-03-31 09:23:11 -06002889 IRQ = resources->irqs->interrupt[(temp_byte +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 resources->irqs->barber_pole - 1) & 0x03];
2891 } else {
2892 /* Program IRQ based on card type */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002893 rc = pci_bus_read_config_byte(pci_bus, devfn, 0x0B, &class_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002895 if (class_code == PCI_BASE_CLASS_STORAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 IRQ = cpqhp_disk_irq;
Alex Chiang1d3ecf12009-03-31 09:23:41 -06002897 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 IRQ = cpqhp_nic_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
2900
2901 /* IRQ Line */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002902 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
2904
2905 if (!behind_bridge) {
Bjorn Helgaas98d33332008-12-09 16:11:41 -07002906 rc = cpqhp_set_irq(func->bus, func->device, temp_byte, IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (rc)
2908 return 1;
2909 } else {
2910 /* TBD - this code may also belong in the other clause
2911 * of this If statement */
2912 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
2913 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
2914 }
2915
2916 /* Latency Timer */
2917 temp_byte = 0x40;
2918 rc = pci_bus_write_config_byte(pci_bus, devfn,
2919 PCI_LATENCY_TIMER, temp_byte);
2920
2921 /* Cache Line size */
2922 temp_byte = 0x08;
2923 rc = pci_bus_write_config_byte(pci_bus, devfn,
2924 PCI_CACHE_LINE_SIZE, temp_byte);
2925
2926 /* disable ROM base Address */
2927 temp_dword = 0x00L;
2928 rc = pci_bus_write_config_word(pci_bus, devfn,
2929 PCI_ROM_ADDRESS, temp_dword);
2930
2931 /* enable card */
2932 temp_word = 0x0157; /* = PCI_COMMAND_IO |
2933 * PCI_COMMAND_MEMORY |
2934 * PCI_COMMAND_MASTER |
2935 * PCI_COMMAND_INVALIDATE |
2936 * PCI_COMMAND_PARITY |
2937 * PCI_COMMAND_SERR */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002938 rc = pci_bus_write_config_word(pci_bus, devfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 PCI_COMMAND, temp_word);
2940 } else { /* End of Not-A-Bridge else */
2941 /* It's some strange type of PCI adapter (Cardbus?) */
2942 return DEVICE_TYPE_NOT_SUPPORTED;
2943 }
2944
2945 func->configured = 1;
2946
2947 return 0;
2948free_and_out:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002949 cpqhp_destroy_resource_list(&temp_resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08002951 return_resource(&(resources->bus_head), hold_bus_node);
2952 return_resource(&(resources->io_head), hold_IO_node);
2953 return_resource(&(resources->mem_head), hold_mem_node);
2954 return_resource(&(resources->p_mem_head), hold_p_mem_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return rc;
2956}