blob: b02b8dddcf9febbfaa6eb79bd9ed05460e88c738 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Compaq Hot Plug Controller Driver
3 *
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
7 *
8 * All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
19 * details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * Send feedback to <greg@kroah.com>
26 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/slab.h>
33#include <linux/workqueue.h>
34#include <linux/interrupt.h>
35#include <linux/delay.h>
36#include <linux/wait.h>
37#include <linux/smp_lock.h>
38#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070039#include <linux/pci_hotplug.h>
Christoph Hellwigfa007d82007-08-14 16:17:15 -070040#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "cpqphp.h"
42
43static u32 configure_new_device(struct controller* ctrl, struct pci_func *func,
44 u8 behind_bridge, struct resource_lists *resources);
45static int configure_new_function(struct controller* ctrl, struct pci_func *func,
46 u8 behind_bridge, struct resource_lists *resources);
47static void interrupt_event_handler(struct controller *ctrl);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Christoph Hellwigfa007d82007-08-14 16:17:15 -070050static struct task_struct *cpqhp_event_thread;
51static unsigned long pushbutton_pending; /* = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* delay is in jiffies to wait for */
54static void long_delay(int delay)
55{
Christoph Hellwigfa007d82007-08-14 16:17:15 -070056 /*
57 * XXX(hch): if someone is bored please convert all callers
58 * to call msleep_interruptible directly. They really want
59 * to specify timeouts in natural units and spend a lot of
60 * effort converting them to jiffies..
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 msleep_interruptible(jiffies_to_msecs(delay));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65
66/* FIXME: The following line needs to be somewhere else... */
67#define WRONG_BUS_FREQUENCY 0x07
68static u8 handle_switch_change(u8 change, struct controller * ctrl)
69{
70 int hp_slot;
71 u8 rc = 0;
72 u16 temp_word;
73 struct pci_func *func;
74 struct event_info *taskInfo;
75
76 if (!change)
77 return 0;
78
79 /* Switch Change */
80 dbg("cpqsbd: Switch interrupt received.\n");
81
82 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
83 if (change & (0x1L << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -060084 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -060086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 func = cpqhp_slot_find(ctrl->bus,
88 (hp_slot + ctrl->slot_device_offset), 0);
89
90 /* this is the structure that tells the worker thread
Alex Chiang427438c2009-03-31 09:23:16 -060091 * what to do
92 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
94 ctrl->next_event = (ctrl->next_event + 1) % 10;
95 taskInfo->hp_slot = hp_slot;
96
97 rc++;
98
99 temp_word = ctrl->ctrl_int_comp >> 16;
100 func->presence_save = (temp_word >> hp_slot) & 0x01;
101 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
102
103 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600104 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Switch opened
Alex Chiang427438c2009-03-31 09:23:16 -0600106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 func->switch_save = 0;
109
110 taskInfo->event_type = INT_SWITCH_OPEN;
111 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600112 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Switch closed
Alex Chiang427438c2009-03-31 09:23:16 -0600114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 func->switch_save = 0x10;
117
118 taskInfo->event_type = INT_SWITCH_CLOSE;
119 }
120 }
121 }
122
123 return rc;
124}
125
126/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800127 * cpqhp_find_slot - find the struct slot of given device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * @ctrl: scan lots of this controller
129 * @device: the device id to find
130 */
131static struct slot *cpqhp_find_slot(struct controller *ctrl, u8 device)
132{
133 struct slot *slot = ctrl->slot;
134
135 while (slot && (slot->device != device)) {
136 slot = slot->next;
137 }
138
139 return slot;
140}
141
142
143static u8 handle_presence_change(u16 change, struct controller * ctrl)
144{
145 int hp_slot;
146 u8 rc = 0;
147 u8 temp_byte;
148 u16 temp_word;
149 struct pci_func *func;
150 struct event_info *taskInfo;
151 struct slot *p_slot;
152
153 if (!change)
154 return 0;
155
Alex Chiang427438c2009-03-31 09:23:16 -0600156 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * Presence Change
Alex Chiang427438c2009-03-31 09:23:16 -0600158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 dbg("cpqsbd: Presence/Notify input change.\n");
160 dbg(" Changed bits are 0x%4.4x\n", change );
161
162 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
163 if (change & (0x0101 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600164 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -0600166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 func = cpqhp_slot_find(ctrl->bus,
168 (hp_slot + ctrl->slot_device_offset), 0);
169
170 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
171 ctrl->next_event = (ctrl->next_event + 1) % 10;
172 taskInfo->hp_slot = hp_slot;
173
174 rc++;
175
176 p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
177 if (!p_slot)
178 return 0;
179
180 /* If the switch closed, must be a button
Alex Chiang427438c2009-03-31 09:23:16 -0600181 * If not in button mode, nevermind
182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (func->switch_save && (ctrl->push_button == 1)) {
184 temp_word = ctrl->ctrl_int_comp >> 16;
185 temp_byte = (temp_word >> hp_slot) & 0x01;
186 temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
187
188 if (temp_byte != func->presence_save) {
Alex Chiang427438c2009-03-31 09:23:16 -0600189 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * button Pressed (doesn't do anything)
Alex Chiang427438c2009-03-31 09:23:16 -0600191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 dbg("hp_slot %d button pressed\n", hp_slot);
193 taskInfo->event_type = INT_BUTTON_PRESS;
194 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600195 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * button Released - TAKE ACTION!!!!
Alex Chiang427438c2009-03-31 09:23:16 -0600197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 dbg("hp_slot %d button released\n", hp_slot);
199 taskInfo->event_type = INT_BUTTON_RELEASE;
200
201 /* Cancel if we are still blinking */
202 if ((p_slot->state == BLINKINGON_STATE)
203 || (p_slot->state == BLINKINGOFF_STATE)) {
204 taskInfo->event_type = INT_BUTTON_CANCEL;
205 dbg("hp_slot %d button cancel\n", hp_slot);
206 } else if ((p_slot->state == POWERON_STATE)
207 || (p_slot->state == POWEROFF_STATE)) {
208 /* info(msg_button_ignore, p_slot->number); */
209 taskInfo->event_type = INT_BUTTON_IGNORE;
210 dbg("hp_slot %d button ignore\n", hp_slot);
211 }
212 }
213 } else {
214 /* Switch is open, assume a presence change
Alex Chiang427438c2009-03-31 09:23:16 -0600215 * Save the presence state
216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 temp_word = ctrl->ctrl_int_comp >> 16;
218 func->presence_save = (temp_word >> hp_slot) & 0x01;
219 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
220
221 if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
222 (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
223 /* Present */
224 taskInfo->event_type = INT_PRESENCE_ON;
225 } else {
226 /* Not Present */
227 taskInfo->event_type = INT_PRESENCE_OFF;
228 }
229 }
230 }
231 }
232
233 return rc;
234}
235
236
237static u8 handle_power_fault(u8 change, struct controller * ctrl)
238{
239 int hp_slot;
240 u8 rc = 0;
241 struct pci_func *func;
242 struct event_info *taskInfo;
243
244 if (!change)
245 return 0;
246
Alex Chiang427438c2009-03-31 09:23:16 -0600247 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * power fault
Alex Chiang427438c2009-03-31 09:23:16 -0600249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 info("power fault interrupt\n");
252
253 for (hp_slot = 0; hp_slot < 6; hp_slot++) {
254 if (change & (0x01 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600255 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * this one changed.
Alex Chiang427438c2009-03-31 09:23:16 -0600257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 func = cpqhp_slot_find(ctrl->bus,
259 (hp_slot + ctrl->slot_device_offset), 0);
260
261 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
262 ctrl->next_event = (ctrl->next_event + 1) % 10;
263 taskInfo->hp_slot = hp_slot;
264
265 rc++;
266
267 if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600268 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * power fault Cleared
Alex Chiang427438c2009-03-31 09:23:16 -0600270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 func->status = 0x00;
272
273 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
274 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600275 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * power fault
Alex Chiang427438c2009-03-31 09:23:16 -0600277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 taskInfo->event_type = INT_POWER_FAULT;
279
280 if (ctrl->rev < 4) {
281 amber_LED_on (ctrl, hp_slot);
282 green_LED_off (ctrl, hp_slot);
283 set_SOGO (ctrl);
284
285 /* this is a fatal condition, we want
286 * to crash the machine to protect from
287 * data corruption. simulated_NMI
288 * shouldn't ever return */
289 /* FIXME
290 simulated_NMI(hp_slot, ctrl); */
291
292 /* The following code causes a software
293 * crash just in case simulated_NMI did
294 * return */
295 /*FIXME
296 panic(msg_power_fault); */
297 } else {
298 /* set power fault status for this board */
299 func->status = 0xFF;
300 info("power fault bit %x set\n", hp_slot);
301 }
302 }
303 }
304 }
305
306 return rc;
307}
308
309
310/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800311 * sort_by_size - sort nodes on the list by their length, smallest first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * @head: list to sort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314static int sort_by_size(struct pci_resource **head)
315{
316 struct pci_resource *current_res;
317 struct pci_resource *next_res;
318 int out_of_order = 1;
319
320 if (!(*head))
321 return 1;
322
323 if (!((*head)->next))
324 return 0;
325
326 while (out_of_order) {
327 out_of_order = 0;
328
329 /* Special case for swapping list head */
330 if (((*head)->next) &&
331 ((*head)->length > (*head)->next->length)) {
332 out_of_order++;
333 current_res = *head;
334 *head = (*head)->next;
335 current_res->next = (*head)->next;
336 (*head)->next = current_res;
337 }
338
339 current_res = *head;
340
341 while (current_res->next && current_res->next->next) {
342 if (current_res->next->length > current_res->next->next->length) {
343 out_of_order++;
344 next_res = current_res->next;
345 current_res->next = current_res->next->next;
346 current_res = current_res->next;
347 next_res->next = current_res->next;
348 current_res->next = next_res;
349 } else
350 current_res = current_res->next;
351 }
352 } /* End of out_of_order loop */
353
354 return 0;
355}
356
357
358/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800359 * sort_by_max_size - sort nodes on the list by their length, largest first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * @head: list to sort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
362static int sort_by_max_size(struct pci_resource **head)
363{
364 struct pci_resource *current_res;
365 struct pci_resource *next_res;
366 int out_of_order = 1;
367
368 if (!(*head))
369 return 1;
370
371 if (!((*head)->next))
372 return 0;
373
374 while (out_of_order) {
375 out_of_order = 0;
376
377 /* Special case for swapping list head */
378 if (((*head)->next) &&
379 ((*head)->length < (*head)->next->length)) {
380 out_of_order++;
381 current_res = *head;
382 *head = (*head)->next;
383 current_res->next = (*head)->next;
384 (*head)->next = current_res;
385 }
386
387 current_res = *head;
388
389 while (current_res->next && current_res->next->next) {
390 if (current_res->next->length < current_res->next->next->length) {
391 out_of_order++;
392 next_res = current_res->next;
393 current_res->next = current_res->next->next;
394 current_res = current_res->next;
395 next_res->next = current_res->next;
396 current_res->next = next_res;
397 } else
398 current_res = current_res->next;
399 }
400 } /* End of out_of_order loop */
401
402 return 0;
403}
404
405
406/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800407 * do_pre_bridge_resource_split - find node of resources that are unused
408 * @head: new list head
409 * @orig_head: original list head
410 * @alignment: max node size (?)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
412static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
413 struct pci_resource **orig_head, u32 alignment)
414{
415 struct pci_resource *prevnode = NULL;
416 struct pci_resource *node;
417 struct pci_resource *split_node;
418 u32 rc;
419 u32 temp_dword;
420 dbg("do_pre_bridge_resource_split\n");
421
422 if (!(*head) || !(*orig_head))
423 return NULL;
424
425 rc = cpqhp_resource_sort_and_combine(head);
426
427 if (rc)
428 return NULL;
429
430 if ((*head)->base != (*orig_head)->base)
431 return NULL;
432
433 if ((*head)->length == (*orig_head)->length)
434 return NULL;
435
436
437 /* If we got here, there the bridge requires some of the resource, but
Alex Chiang427438c2009-03-31 09:23:16 -0600438 * we may be able to split some off of the front
439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 node = *head;
442
443 if (node->length & (alignment -1)) {
444 /* this one isn't an aligned length, so we'll make a new entry
Alex Chiang427438c2009-03-31 09:23:16 -0600445 * and split it up.
446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
448
449 if (!split_node)
450 return NULL;
451
452 temp_dword = (node->length | (alignment-1)) + 1 - alignment;
453
454 split_node->base = node->base;
455 split_node->length = temp_dword;
456
457 node->length -= temp_dword;
458 node->base += split_node->length;
459
460 /* Put it in the list */
461 *head = split_node;
462 split_node->next = node;
463 }
464
465 if (node->length < alignment)
466 return NULL;
467
468 /* Now unlink it */
469 if (*head == node) {
470 *head = node->next;
471 } else {
472 prevnode = *head;
473 while (prevnode->next != node)
474 prevnode = prevnode->next;
475
476 prevnode->next = node->next;
477 }
478 node->next = NULL;
479
480 return node;
481}
482
483
484/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800485 * do_bridge_resource_split - find one node of resources that aren't in use
486 * @head: list head
487 * @alignment: max node size (?)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
489static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
490{
491 struct pci_resource *prevnode = NULL;
492 struct pci_resource *node;
493 u32 rc;
494 u32 temp_dword;
495
496 rc = cpqhp_resource_sort_and_combine(head);
497
498 if (rc)
499 return NULL;
500
501 node = *head;
502
503 while (node->next) {
504 prevnode = node;
505 node = node->next;
506 kfree(prevnode);
507 }
508
509 if (node->length < alignment)
510 goto error;
511
512 if (node->base & (alignment - 1)) {
513 /* Short circuit if adjusted size is too small */
514 temp_dword = (node->base | (alignment-1)) + 1;
515 if ((node->length - (temp_dword - node->base)) < alignment)
516 goto error;
517
518 node->length -= (temp_dword - node->base);
519 node->base = temp_dword;
520 }
521
522 if (node->length & (alignment - 1))
523 /* There's stuff in use after this node */
524 goto error;
525
526 return node;
527error:
528 kfree(node);
529 return NULL;
530}
531
532
533/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800534 * get_io_resource - find first node of given size not in ISA aliasing window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * @head: list to search
536 * @size: size of node to find, must be a power of two.
537 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800538 * Description: This function sorts the resource list by size and then returns
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * returns the first node of "size" length that is not in the ISA aliasing
540 * window. If it finds a node larger than "size" it will split it up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
543{
544 struct pci_resource *prevnode;
545 struct pci_resource *node;
546 struct pci_resource *split_node;
547 u32 temp_dword;
548
549 if (!(*head))
550 return NULL;
551
552 if ( cpqhp_resource_sort_and_combine(head) )
553 return NULL;
554
555 if ( sort_by_size(head) )
556 return NULL;
557
558 for (node = *head; node; node = node->next) {
559 if (node->length < size)
560 continue;
561
562 if (node->base & (size - 1)) {
563 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600564 * so we'll make a new entry and split it up
565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 temp_dword = (node->base | (size-1)) + 1;
567
568 /* Short circuit if adjusted size is too small */
569 if ((node->length - (temp_dword - node->base)) < size)
570 continue;
571
572 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
573
574 if (!split_node)
575 return NULL;
576
577 split_node->base = node->base;
578 split_node->length = temp_dword - node->base;
579 node->base = temp_dword;
580 node->length -= split_node->length;
581
582 /* Put it in the list */
583 split_node->next = node->next;
584 node->next = split_node;
585 } /* End of non-aligned base */
586
587 /* Don't need to check if too small since we already did */
588 if (node->length > size) {
589 /* this one is longer than we need
Alex Chiang427438c2009-03-31 09:23:16 -0600590 * so we'll make a new entry and split it up
591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
593
594 if (!split_node)
595 return NULL;
596
597 split_node->base = node->base + size;
598 split_node->length = node->length - size;
599 node->length = size;
600
601 /* Put it in the list */
602 split_node->next = node->next;
603 node->next = split_node;
604 } /* End of too big on top end */
605
606 /* For IO make sure it's not in the ISA aliasing space */
607 if (node->base & 0x300L)
608 continue;
609
610 /* If we got here, then it is the right size
Alex Chiang427438c2009-03-31 09:23:16 -0600611 * Now take it out of the list and break
612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (*head == node) {
614 *head = node->next;
615 } else {
616 prevnode = *head;
617 while (prevnode->next != node)
618 prevnode = prevnode->next;
619
620 prevnode->next = node->next;
621 }
622 node->next = NULL;
623 break;
624 }
625
626 return node;
627}
628
629
630/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800631 * get_max_resource - get largest node which has at least the given size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * @head: the list to search the node in
633 * @size: the minimum size of the node to find
634 *
635 * Description: Gets the largest node that is at least "size" big from the
636 * list pointed to by head. It aligns the node on top and bottom
637 * to "size" alignment before returning it.
638 */
639static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
640{
641 struct pci_resource *max;
642 struct pci_resource *temp;
643 struct pci_resource *split_node;
644 u32 temp_dword;
645
646 if (cpqhp_resource_sort_and_combine(head))
647 return NULL;
648
649 if (sort_by_max_size(head))
650 return NULL;
651
652 for (max = *head; max; max = max->next) {
Alex Chiang861fefb2009-03-31 09:23:11 -0600653 /* If not big enough we could probably just bail,
Alex Chiang427438c2009-03-31 09:23:16 -0600654 * instead we'll continue to the next.
655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (max->length < size)
657 continue;
658
659 if (max->base & (size - 1)) {
660 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600661 * so we'll make a new entry and split it up
662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 temp_dword = (max->base | (size-1)) + 1;
664
665 /* Short circuit if adjusted size is too small */
666 if ((max->length - (temp_dword - max->base)) < size)
667 continue;
668
669 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
670
671 if (!split_node)
672 return NULL;
673
674 split_node->base = max->base;
675 split_node->length = temp_dword - max->base;
676 max->base = temp_dword;
677 max->length -= split_node->length;
678
679 split_node->next = max->next;
680 max->next = split_node;
681 }
682
683 if ((max->base + max->length) & (size - 1)) {
684 /* this one isn't end aligned properly at the top
Alex Chiang427438c2009-03-31 09:23:16 -0600685 * so we'll make a new entry and split it up
686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
688
689 if (!split_node)
690 return NULL;
691 temp_dword = ((max->base + max->length) & ~(size - 1));
692 split_node->base = temp_dword;
693 split_node->length = max->length + max->base
694 - split_node->base;
695 max->length -= split_node->length;
696
697 split_node->next = max->next;
698 max->next = split_node;
699 }
700
701 /* Make sure it didn't shrink too much when we aligned it */
702 if (max->length < size)
703 continue;
704
705 /* Now take it out of the list */
706 temp = *head;
707 if (temp == max) {
708 *head = max->next;
709 } else {
710 while (temp && temp->next != max) {
711 temp = temp->next;
712 }
713
714 temp->next = max->next;
715 }
716
717 max->next = NULL;
718 break;
719 }
720
721 return max;
722}
723
724
725/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800726 * get_resource - find resource of given size and split up larger ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * @head: the list to search for resources
728 * @size: the size limit to use
729 *
730 * Description: This function sorts the resource list by size and then
731 * returns the first node of "size" length. If it finds a node
732 * larger than "size" it will split it up.
733 *
734 * size must be a power of two.
735 */
736static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
737{
738 struct pci_resource *prevnode;
739 struct pci_resource *node;
740 struct pci_resource *split_node;
741 u32 temp_dword;
742
743 if (cpqhp_resource_sort_and_combine(head))
744 return NULL;
745
746 if (sort_by_size(head))
747 return NULL;
748
749 for (node = *head; node; node = node->next) {
750 dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800751 __func__, size, node, node->base, node->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (node->length < size)
753 continue;
754
755 if (node->base & (size - 1)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800756 dbg("%s: not aligned\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* this one isn't base aligned properly
Alex Chiang427438c2009-03-31 09:23:16 -0600758 * so we'll make a new entry and split it up
759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 temp_dword = (node->base | (size-1)) + 1;
761
762 /* Short circuit if adjusted size is too small */
763 if ((node->length - (temp_dword - node->base)) < size)
764 continue;
765
766 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
767
768 if (!split_node)
769 return NULL;
770
771 split_node->base = node->base;
772 split_node->length = temp_dword - node->base;
773 node->base = temp_dword;
774 node->length -= split_node->length;
775
776 split_node->next = node->next;
777 node->next = split_node;
778 } /* End of non-aligned base */
779
780 /* Don't need to check if too small since we already did */
781 if (node->length > size) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800782 dbg("%s: too big\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* this one is longer than we need
Alex Chiang427438c2009-03-31 09:23:16 -0600784 * so we'll make a new entry and split it up
785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
787
788 if (!split_node)
789 return NULL;
790
791 split_node->base = node->base + size;
792 split_node->length = node->length - size;
793 node->length = size;
794
795 /* Put it in the list */
796 split_node->next = node->next;
797 node->next = split_node;
798 } /* End of too big on top end */
799
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800800 dbg("%s: got one!!!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* If we got here, then it is the right size
802 * Now take it out of the list */
803 if (*head == node) {
804 *head = node->next;
805 } else {
806 prevnode = *head;
807 while (prevnode->next != node)
808 prevnode = prevnode->next;
809
810 prevnode->next = node->next;
811 }
812 node->next = NULL;
813 break;
814 }
815 return node;
816}
817
818
819/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800820 * cpqhp_resource_sort_and_combine - sort nodes by base addresses and clean up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * @head: the list to sort and clean up
822 *
823 * Description: Sorts all of the nodes in the list in ascending order by
824 * their base addresses. Also does garbage collection by
825 * combining adjacent nodes.
826 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800827 * Returns %0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
829int cpqhp_resource_sort_and_combine(struct pci_resource **head)
830{
831 struct pci_resource *node1;
832 struct pci_resource *node2;
833 int out_of_order = 1;
834
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800835 dbg("%s: head = %p, *head = %p\n", __func__, head, *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (!(*head))
838 return 1;
839
840 dbg("*head->next = %p\n",(*head)->next);
841
842 if (!(*head)->next)
843 return 0; /* only one item on the list, already sorted! */
844
845 dbg("*head->base = 0x%x\n",(*head)->base);
846 dbg("*head->next->base = 0x%x\n",(*head)->next->base);
847 while (out_of_order) {
848 out_of_order = 0;
849
850 /* Special case for swapping list head */
851 if (((*head)->next) &&
852 ((*head)->base > (*head)->next->base)) {
853 node1 = *head;
854 (*head) = (*head)->next;
855 node1->next = (*head)->next;
856 (*head)->next = node1;
857 out_of_order++;
858 }
859
860 node1 = (*head);
861
862 while (node1->next && node1->next->next) {
863 if (node1->next->base > node1->next->next->base) {
864 out_of_order++;
865 node2 = node1->next;
866 node1->next = node1->next->next;
867 node1 = node1->next;
868 node2->next = node1->next;
869 node1->next = node2;
870 } else
871 node1 = node1->next;
872 }
873 } /* End of out_of_order loop */
874
875 node1 = *head;
876
877 while (node1 && node1->next) {
878 if ((node1->base + node1->length) == node1->next->base) {
879 /* Combine */
880 dbg("8..\n");
881 node1->length += node1->next->length;
882 node2 = node1->next;
883 node1->next = node1->next->next;
884 kfree(node2);
885 } else
886 node1 = node1->next;
887 }
888
889 return 0;
890}
891
892
David Howells7d12e782006-10-05 14:55:46 +0100893irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 struct controller *ctrl = data;
896 u8 schedule_flag = 0;
897 u8 reset;
898 u16 misc;
899 u32 Diff;
900 u32 temp_dword;
901
Alex Chiang861fefb2009-03-31 09:23:11 -0600902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 misc = readw(ctrl->hpc_reg + MISC);
Alex Chiang427438c2009-03-31 09:23:16 -0600904 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * Check to see if it was our interrupt
Alex Chiang427438c2009-03-31 09:23:16 -0600906 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (!(misc & 0x000C)) {
908 return IRQ_NONE;
909 }
910
911 if (misc & 0x0004) {
Alex Chiang427438c2009-03-31 09:23:16 -0600912 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * Serial Output interrupt Pending
Alex Chiang427438c2009-03-31 09:23:16 -0600914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /* Clear the interrupt */
917 misc |= 0x0004;
918 writew(misc, ctrl->hpc_reg + MISC);
919
920 /* Read to clear posted writes */
921 misc = readw(ctrl->hpc_reg + MISC);
922
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800923 dbg ("%s - waking up\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 wake_up_interruptible(&ctrl->queue);
925 }
926
927 if (misc & 0x0008) {
928 /* General-interrupt-input interrupt Pending */
929 Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
930
931 ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
932
933 /* Clear the interrupt */
934 writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
935
936 /* Read it back to clear any posted writes */
937 temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
938
939 if (!Diff)
940 /* Clear all interrupts */
941 writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
942
943 schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
944 schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
945 schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
946 }
947
948 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
949 if (reset & 0x40) {
950 /* Bus reset has completed */
951 reset &= 0xCF;
952 writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
953 reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
954 wake_up_interruptible(&ctrl->queue);
955 }
956
957 if (schedule_flag) {
Christoph Hellwigfa007d82007-08-14 16:17:15 -0700958 wake_up_process(cpqhp_event_thread);
959 dbg("Waking even thread");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 return IRQ_HANDLED;
962}
963
964
965/**
966 * cpqhp_slot_create - Creates a node and adds it to the proper bus.
Randy Dunlap26e6c662007-11-28 09:04:30 -0800967 * @busnumber: bus where new node is to be located
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800969 * Returns pointer to the new node or %NULL if unsuccessful.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
971struct pci_func *cpqhp_slot_create(u8 busnumber)
972{
973 struct pci_func *new_slot;
974 struct pci_func *next;
975
Mariusz Kozlowski73a985a2007-07-31 19:14:28 +0200976 new_slot = kzalloc(sizeof(*new_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (new_slot == NULL) {
978 /* I'm not dead yet!
Alex Chiang427438c2009-03-31 09:23:16 -0600979 * You will be.
980 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return new_slot;
982 }
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 new_slot->next = NULL;
985 new_slot->configured = 1;
986
987 if (cpqhp_slot_list[busnumber] == NULL) {
988 cpqhp_slot_list[busnumber] = new_slot;
989 } else {
990 next = cpqhp_slot_list[busnumber];
991 while (next->next != NULL)
992 next = next->next;
993 next->next = new_slot;
994 }
995 return new_slot;
996}
997
998
999/**
1000 * slot_remove - Removes a node from the linked list of slots.
1001 * @old_slot: slot to remove
1002 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001003 * Returns %0 if successful, !0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 */
1005static int slot_remove(struct pci_func * old_slot)
1006{
1007 struct pci_func *next;
1008
1009 if (old_slot == NULL)
1010 return 1;
1011
1012 next = cpqhp_slot_list[old_slot->bus];
1013
1014 if (next == NULL) {
1015 return 1;
1016 }
1017
1018 if (next == old_slot) {
1019 cpqhp_slot_list[old_slot->bus] = old_slot->next;
1020 cpqhp_destroy_board_resources(old_slot);
1021 kfree(old_slot);
1022 return 0;
1023 }
1024
1025 while ((next->next != old_slot) && (next->next != NULL)) {
1026 next = next->next;
1027 }
1028
1029 if (next->next == old_slot) {
1030 next->next = old_slot->next;
1031 cpqhp_destroy_board_resources(old_slot);
1032 kfree(old_slot);
1033 return 0;
1034 } else
1035 return 2;
1036}
1037
1038
1039/**
1040 * bridge_slot_remove - Removes a node from the linked list of slots.
1041 * @bridge: bridge to remove
1042 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001043 * Returns %0 if successful, !0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
1045static int bridge_slot_remove(struct pci_func *bridge)
1046{
1047 u8 subordinateBus, secondaryBus;
1048 u8 tempBus;
1049 struct pci_func *next;
1050
1051 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
1052 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
1053
1054 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
1055 next = cpqhp_slot_list[tempBus];
1056
1057 while (!slot_remove(next)) {
1058 next = cpqhp_slot_list[tempBus];
1059 }
1060 }
1061
1062 next = cpqhp_slot_list[bridge->bus];
1063
1064 if (next == NULL)
1065 return 1;
1066
1067 if (next == bridge) {
1068 cpqhp_slot_list[bridge->bus] = bridge->next;
1069 goto out;
1070 }
1071
1072 while ((next->next != bridge) && (next->next != NULL))
1073 next = next->next;
1074
1075 if (next->next != bridge)
1076 return 2;
1077 next->next = bridge->next;
1078out:
1079 kfree(bridge);
1080 return 0;
1081}
1082
1083
1084/**
1085 * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
1086 * @bus: bus to find
1087 * @device: device to find
Randy Dunlap26e6c662007-11-28 09:04:30 -08001088 * @index: is %0 for first function found, %1 for the second...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 *
1090 * Returns pointer to the node if successful, %NULL otherwise.
1091 */
1092struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
1093{
1094 int found = -1;
1095 struct pci_func *func;
1096
1097 func = cpqhp_slot_list[bus];
1098
1099 if ((func == NULL) || ((func->device == device) && (index == 0)))
1100 return func;
1101
1102 if (func->device == device)
1103 found++;
1104
1105 while (func->next != NULL) {
1106 func = func->next;
1107
1108 if (func->device == device)
1109 found++;
1110
1111 if (found == index)
1112 return func;
1113 }
1114
1115 return NULL;
1116}
1117
1118
1119/* DJZ: I don't think is_bridge will work as is.
1120 * FIXME */
1121static int is_bridge(struct pci_func * func)
1122{
1123 /* Check the header type */
1124 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
1125 return 1;
1126 else
1127 return 0;
1128}
1129
1130
1131/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001132 * set_controller_speed - set the frequency and/or mode of a specific controller segment.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * @ctrl: controller to change frequency/mode for.
1134 * @adapter_speed: the speed of the adapter we want to match.
1135 * @hp_slot: the slot number where the adapter is installed.
1136 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001137 * Returns %0 if we successfully change frequency and/or mode to match the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * adapter speed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 */
1140static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
1141{
1142 struct slot *slot;
1143 u8 reg;
1144 u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
1145 u16 reg16;
1146 u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
Alex Chiang861fefb2009-03-31 09:23:11 -06001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (ctrl->speed == adapter_speed)
1149 return 0;
Alex Chiang861fefb2009-03-31 09:23:11 -06001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* We don't allow freq/mode changes if we find another adapter running
Alex Chiang427438c2009-03-31 09:23:16 -06001152 * in another slot on this controller
1153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 for(slot = ctrl->slot; slot; slot = slot->next) {
Alex Chiang861fefb2009-03-31 09:23:11 -06001155 if (slot->device == (hp_slot + ctrl->slot_device_offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 continue;
Julia Lawall05a34f52008-10-22 16:44:00 -07001157 if (!slot->hotplug_slot || !slot->hotplug_slot->info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 continue;
Alex Chiang861fefb2009-03-31 09:23:11 -06001159 if (slot->hotplug_slot->info->adapter_status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 continue;
1161 /* If another adapter is running on the same segment but at a
1162 * lower speed/mode, we allow the new adapter to function at
Alex Chiang427438c2009-03-31 09:23:16 -06001163 * this rate if supported
1164 */
Alex Chiang861fefb2009-03-31 09:23:11 -06001165 if (ctrl->speed < adapter_speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
1167
1168 return 1;
1169 }
Alex Chiang861fefb2009-03-31 09:23:11 -06001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* If the controller doesn't support freq/mode changes and the
Alex Chiang427438c2009-03-31 09:23:16 -06001172 * controller is running at a higher mode, we bail
1173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if ((ctrl->speed > adapter_speed) && (!ctrl->pcix_speed_capability))
1175 return 1;
Alex Chiang861fefb2009-03-31 09:23:11 -06001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /* But we allow the adapter to run at a lower rate if possible */
1178 if ((ctrl->speed < adapter_speed) && (!ctrl->pcix_speed_capability))
1179 return 0;
1180
1181 /* We try to set the max speed supported by both the adapter and
Alex Chiang427438c2009-03-31 09:23:16 -06001182 * controller
1183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (ctrl->speed_capability < adapter_speed) {
1185 if (ctrl->speed == ctrl->speed_capability)
1186 return 0;
1187 adapter_speed = ctrl->speed_capability;
1188 }
1189
1190 writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
1191 writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
Alex Chiang861fefb2009-03-31 09:23:11 -06001192
1193 set_SOGO(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 wait_for_ctrl_irq(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (adapter_speed != PCI_SPEED_133MHz_PCIX)
1197 reg = 0xF5;
1198 else
Alex Chiang861fefb2009-03-31 09:23:11 -06001199 reg = 0xF4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
Alex Chiang861fefb2009-03-31 09:23:11 -06001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
1203 reg16 &= ~0x000F;
1204 switch(adapter_speed) {
Alex Chiang861fefb2009-03-31 09:23:11 -06001205 case(PCI_SPEED_133MHz_PCIX):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 reg = 0x75;
Alex Chiang861fefb2009-03-31 09:23:11 -06001207 reg16 |= 0xB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 break;
1209 case(PCI_SPEED_100MHz_PCIX):
1210 reg = 0x74;
1211 reg16 |= 0xA;
1212 break;
1213 case(PCI_SPEED_66MHz_PCIX):
1214 reg = 0x73;
1215 reg16 |= 0x9;
1216 break;
1217 case(PCI_SPEED_66MHz):
1218 reg = 0x73;
1219 reg16 |= 0x1;
1220 break;
1221 default: /* 33MHz PCI 2.2 */
1222 reg = 0x71;
1223 break;
Alex Chiang861fefb2009-03-31 09:23:11 -06001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 reg16 |= 0xB << 12;
1227 writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
Alex Chiang861fefb2009-03-31 09:23:11 -06001228
1229 mdelay(5);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Reenable interrupts */
1232 writel(0, ctrl->hpc_reg + INT_MASK);
1233
Alex Chiang861fefb2009-03-31 09:23:11 -06001234 pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* Restart state machine */
1237 reg = ~0xF;
1238 pci_read_config_byte(ctrl->pci_dev, 0x43, &reg);
1239 pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
Alex Chiang861fefb2009-03-31 09:23:11 -06001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* Only if mode change...*/
1242 if (((ctrl->speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
1243 ((ctrl->speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
1244 set_SOGO(ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 wait_for_ctrl_irq(ctrl);
1247 mdelay(1100);
Alex Chiang861fefb2009-03-31 09:23:11 -06001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* Restore LED/Slot state */
1250 writel(leds, ctrl->hpc_reg + LED_CONTROL);
1251 writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
Alex Chiang861fefb2009-03-31 09:23:11 -06001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 set_SOGO(ctrl);
1254 wait_for_ctrl_irq(ctrl);
1255
1256 ctrl->speed = adapter_speed;
1257 slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1258
Alex Chiang861fefb2009-03-31 09:23:11 -06001259 info("Successfully changed frequency/mode for adapter in slot %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 slot->number);
1261 return 0;
1262}
1263
Alex Chiang861fefb2009-03-31 09:23:11 -06001264/* the following routines constitute the bulk of the
Alex Chiang427438c2009-03-31 09:23:16 -06001265 * hotplug controller logic
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
1267
1268
1269/**
1270 * board_replaced - Called after a board has been replaced in the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -08001271 * @func: PCI device/function information
1272 * @ctrl: hotplug controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001274 * This is only used if we don't have resources for hot add.
1275 * Turns power on for the board.
1276 * Checks to see if board is the same.
1277 * If board is same, reconfigures it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * If board isn't same, turns it back off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
1280static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
1281{
1282 u8 hp_slot;
1283 u8 temp_byte;
1284 u8 adapter_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 u32 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 hp_slot = func->device - ctrl->slot_device_offset;
1288
1289 if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -06001290 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * The switch is open.
Alex Chiang427438c2009-03-31 09:23:16 -06001292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 rc = INTERLOCK_OPEN;
1294 } else if (is_slot_enabled (ctrl, hp_slot)) {
Alex Chiang427438c2009-03-31 09:23:16 -06001295 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * The board is already on
Alex Chiang427438c2009-03-31 09:23:16 -06001297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 rc = CARD_FUNCTIONING;
1299 } else {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001300 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* turn on board without attaching to the bus */
1303 enable_slot_power (ctrl, hp_slot);
1304
1305 set_SOGO(ctrl);
1306
1307 /* Wait for SOBS to be unset */
1308 wait_for_ctrl_irq (ctrl);
1309
1310 /* Change bits in slot power register to force another shift out
1311 * NOTE: this is to work around the timer bug */
1312 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1313 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1314 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1315
1316 set_SOGO(ctrl);
1317
1318 /* Wait for SOBS to be unset */
1319 wait_for_ctrl_irq (ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1322 if (ctrl->speed != adapter_speed)
1323 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1324 rc = WRONG_BUS_FREQUENCY;
1325
1326 /* turn off board without attaching to the bus */
1327 disable_slot_power (ctrl, hp_slot);
1328
1329 set_SOGO(ctrl);
1330
1331 /* Wait for SOBS to be unset */
1332 wait_for_ctrl_irq (ctrl);
1333
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001334 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 if (rc)
1337 return rc;
1338
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001339 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 slot_enable (ctrl, hp_slot);
1342 green_LED_blink (ctrl, hp_slot);
1343
1344 amber_LED_off (ctrl, hp_slot);
1345
1346 set_SOGO(ctrl);
1347
1348 /* Wait for SOBS to be unset */
1349 wait_for_ctrl_irq (ctrl);
1350
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001351 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* Wait for ~1 second because of hot plug spec */
1354 long_delay(1*HZ);
1355
1356 /* Check for a power fault */
1357 if (func->status == 0xFF) {
1358 /* power fault occurred, but it was benign */
1359 rc = POWER_FAILURE;
1360 func->status = 0;
1361 } else
1362 rc = cpqhp_valid_replace(ctrl, func);
1363
1364 if (!rc) {
1365 /* It must be the same board */
1366
1367 rc = cpqhp_configure_board(ctrl, func);
1368
Adrian Bunk1305e912006-02-26 22:16:51 +01001369 /* If configuration fails, turn it off
1370 * Get slot won't work for devices behind
1371 * bridges, but in this case it will always be
1372 * called for the "base" bus/dev/func of an
Alex Chiang427438c2009-03-31 09:23:16 -06001373 * adapter.
1374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001376 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Adrian Bunk1305e912006-02-26 22:16:51 +01001378 amber_LED_on (ctrl, hp_slot);
1379 green_LED_off (ctrl, hp_slot);
1380 slot_disable (ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 set_SOGO(ctrl);
1383
1384 /* Wait for SOBS to be unset */
1385 wait_for_ctrl_irq (ctrl);
1386
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001387 mutex_unlock(&ctrl->crit_sect);
Adrian Bunk1305e912006-02-26 22:16:51 +01001388
1389 if (rc)
1390 return rc;
1391 else
1392 return 1;
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 } else {
1395 /* Something is wrong
1396
1397 * Get slot won't work for devices behind bridges, but
1398 * in this case it will always be called for the "base"
Alex Chiang427438c2009-03-31 09:23:16 -06001399 * bus/dev/func of an adapter.
1400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001402 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 amber_LED_on (ctrl, hp_slot);
1405 green_LED_off (ctrl, hp_slot);
1406 slot_disable (ctrl, hp_slot);
1407
1408 set_SOGO(ctrl);
1409
1410 /* Wait for SOBS to be unset */
1411 wait_for_ctrl_irq (ctrl);
1412
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001413 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
1416 }
1417 return rc;
1418
1419}
1420
1421
1422/**
1423 * board_added - Called after a board has been added to the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -08001424 * @func: PCI device/function info
1425 * @ctrl: hotplug controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001427 * Turns power on for the board.
1428 * Configures board.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
1430static u32 board_added(struct pci_func *func, struct controller *ctrl)
1431{
1432 u8 hp_slot;
1433 u8 temp_byte;
1434 u8 adapter_speed;
1435 int index;
1436 u32 temp_register = 0xFFFFFFFF;
1437 u32 rc = 0;
1438 struct pci_func *new_slot = NULL;
1439 struct slot *p_slot;
1440 struct resource_lists res_lists;
1441
1442 hp_slot = func->device - ctrl->slot_device_offset;
1443 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001444 __func__, func->device, ctrl->slot_device_offset, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001446 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 /* turn on board without attaching to the bus */
1449 enable_slot_power(ctrl, hp_slot);
1450
1451 set_SOGO(ctrl);
1452
1453 /* Wait for SOBS to be unset */
1454 wait_for_ctrl_irq (ctrl);
1455
1456 /* Change bits in slot power register to force another shift out
Alex Chiang427438c2009-03-31 09:23:16 -06001457 * NOTE: this is to work around the timer bug
1458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
1460 writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
1461 writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
1462
1463 set_SOGO(ctrl);
1464
1465 /* Wait for SOBS to be unset */
1466 wait_for_ctrl_irq (ctrl);
Alex Chiang861fefb2009-03-31 09:23:11 -06001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 adapter_speed = get_adapter_speed(ctrl, hp_slot);
1469 if (ctrl->speed != adapter_speed)
1470 if (set_controller_speed(ctrl, adapter_speed, hp_slot))
1471 rc = WRONG_BUS_FREQUENCY;
Alex Chiang861fefb2009-03-31 09:23:11 -06001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /* turn off board without attaching to the bus */
1474 disable_slot_power (ctrl, hp_slot);
1475
1476 set_SOGO(ctrl);
1477
1478 /* Wait for SOBS to be unset */
1479 wait_for_ctrl_irq(ctrl);
1480
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001481 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (rc)
1484 return rc;
Alex Chiang861fefb2009-03-31 09:23:11 -06001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1487
1488 /* turn on board and blink green LED */
1489
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001490 dbg("%s: before down\n", __func__);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001491 mutex_lock(&ctrl->crit_sect);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001492 dbg("%s: after down\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001494 dbg("%s: before slot_enable\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 slot_enable (ctrl, hp_slot);
1496
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001497 dbg("%s: before green_LED_blink\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 green_LED_blink (ctrl, hp_slot);
1499
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001500 dbg("%s: before amber_LED_blink\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 amber_LED_off (ctrl, hp_slot);
1502
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001503 dbg("%s: before set_SOGO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 set_SOGO(ctrl);
1505
1506 /* Wait for SOBS to be unset */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001507 dbg("%s: before wait_for_ctrl_irq\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 wait_for_ctrl_irq (ctrl);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001509 dbg("%s: after wait_for_ctrl_irq\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001511 dbg("%s: before up\n", __func__);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001512 mutex_unlock(&ctrl->crit_sect);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001513 dbg("%s: after up\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 /* Wait for ~1 second because of hot plug spec */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001516 dbg("%s: before long_delay\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 long_delay(1*HZ);
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001518 dbg("%s: after long_delay\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001520 dbg("%s: func status = %x\n", __func__, func->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* Check for a power fault */
1522 if (func->status == 0xFF) {
1523 /* power fault occurred, but it was benign */
1524 temp_register = 0xFFFFFFFF;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001525 dbg("%s: temp register set to %x by power fault\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 rc = POWER_FAILURE;
1527 func->status = 0;
1528 } else {
1529 /* Get vendor/device ID u32 */
1530 ctrl->pci_bus->number = func->bus;
1531 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 -08001532 dbg("%s: pci_read_config_dword returns %d\n", __func__, rc);
1533 dbg("%s: temp_register is %x\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (rc != 0) {
1536 /* Something's wrong here */
1537 temp_register = 0xFFFFFFFF;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001538 dbg("%s: temp register set to %x by error\n", __func__, temp_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540 /* Preset return code. It will be changed later if things go okay. */
1541 rc = NO_ADAPTER_PRESENT;
1542 }
1543
1544 /* All F's is an empty slot or an invalid board */
1545 if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
1546 res_lists.io_head = ctrl->io_head;
1547 res_lists.mem_head = ctrl->mem_head;
1548 res_lists.p_mem_head = ctrl->p_mem_head;
1549 res_lists.bus_head = ctrl->bus_head;
1550 res_lists.irqs = NULL;
1551
1552 rc = configure_new_device(ctrl, func, 0, &res_lists);
1553
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001554 dbg("%s: back from configure_new_device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 ctrl->io_head = res_lists.io_head;
1556 ctrl->mem_head = res_lists.mem_head;
1557 ctrl->p_mem_head = res_lists.p_mem_head;
1558 ctrl->bus_head = res_lists.bus_head;
1559
1560 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1561 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1562 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1563 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1564
1565 if (rc) {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001566 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 amber_LED_on (ctrl, hp_slot);
1569 green_LED_off (ctrl, hp_slot);
1570 slot_disable (ctrl, hp_slot);
1571
1572 set_SOGO(ctrl);
1573
1574 /* Wait for SOBS to be unset */
1575 wait_for_ctrl_irq (ctrl);
1576
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001577 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return rc;
1579 } else {
1580 cpqhp_save_slot_config(ctrl, func);
1581 }
1582
1583
1584 func->status = 0;
1585 func->switch_save = 0x10;
1586 func->is_a_board = 0x01;
1587
1588 /* next, we will instantiate the linux pci_dev structures (with
1589 * appropriate driver notification, if already present) */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001590 dbg("%s: configure linux pci_dev structure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 index = 0;
1592 do {
1593 new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
1594 if (new_slot && !new_slot->pci_dev) {
1595 cpqhp_configure_device(ctrl, new_slot);
1596 }
1597 } while (new_slot);
1598
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001599 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 green_LED_on (ctrl, hp_slot);
1602
1603 set_SOGO(ctrl);
1604
1605 /* Wait for SOBS to be unset */
1606 wait_for_ctrl_irq (ctrl);
1607
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001608 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 } else {
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001610 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 amber_LED_on (ctrl, hp_slot);
1613 green_LED_off (ctrl, hp_slot);
1614 slot_disable (ctrl, hp_slot);
1615
1616 set_SOGO(ctrl);
1617
1618 /* Wait for SOBS to be unset */
1619 wait_for_ctrl_irq (ctrl);
1620
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001621 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 return rc;
1624 }
1625 return 0;
1626}
1627
1628
1629/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001630 * remove_board - Turns off slot and LEDs
1631 * @func: PCI device/function info
1632 * @replace_flag: whether replacing or adding a new device
1633 * @ctrl: target controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
1635static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
1636{
1637 int index;
1638 u8 skip = 0;
1639 u8 device;
1640 u8 hp_slot;
1641 u8 temp_byte;
1642 u32 rc;
1643 struct resource_lists res_lists;
1644 struct pci_func *temp_func;
1645
1646 if (cpqhp_unconfigure_device(func))
1647 return 1;
1648
1649 device = func->device;
1650
1651 hp_slot = func->device - ctrl->slot_device_offset;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001652 dbg("In %s, hp_slot = %d\n", __func__, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 /* When we get here, it is safe to change base address registers.
1655 * We will attempt to save the base address register lengths */
1656 if (replace_flag || !ctrl->add_support)
1657 rc = cpqhp_save_base_addr_length(ctrl, func);
1658 else if (!func->bus_head && !func->mem_head &&
1659 !func->p_mem_head && !func->io_head) {
1660 /* Here we check to see if we've saved any of the board's
1661 * resources already. If so, we'll skip the attempt to
1662 * determine what's being used. */
1663 index = 0;
1664 temp_func = cpqhp_slot_find(func->bus, func->device, index++);
1665 while (temp_func) {
1666 if (temp_func->bus_head || temp_func->mem_head
1667 || temp_func->p_mem_head || temp_func->io_head) {
1668 skip = 1;
1669 break;
1670 }
1671 temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
1672 }
1673
1674 if (!skip)
1675 rc = cpqhp_save_used_resources(ctrl, func);
1676 }
1677 /* Change status to shutdown */
1678 if (func->is_a_board)
1679 func->status = 0x01;
1680 func->configured = 0;
1681
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001682 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 green_LED_off (ctrl, hp_slot);
1685 slot_disable (ctrl, hp_slot);
1686
1687 set_SOGO(ctrl);
1688
1689 /* turn off SERR for slot */
1690 temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
1691 temp_byte &= ~(0x01 << hp_slot);
1692 writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
1693
1694 /* Wait for SOBS to be unset */
1695 wait_for_ctrl_irq (ctrl);
1696
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001697 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 if (!replace_flag && ctrl->add_support) {
1700 while (func) {
1701 res_lists.io_head = ctrl->io_head;
1702 res_lists.mem_head = ctrl->mem_head;
1703 res_lists.p_mem_head = ctrl->p_mem_head;
1704 res_lists.bus_head = ctrl->bus_head;
1705
1706 cpqhp_return_board_resources(func, &res_lists);
1707
1708 ctrl->io_head = res_lists.io_head;
1709 ctrl->mem_head = res_lists.mem_head;
1710 ctrl->p_mem_head = res_lists.p_mem_head;
1711 ctrl->bus_head = res_lists.bus_head;
1712
1713 cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1714 cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1715 cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1716 cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1717
1718 if (is_bridge(func)) {
1719 bridge_slot_remove(func);
1720 } else
1721 slot_remove(func);
1722
1723 func = cpqhp_slot_find(ctrl->bus, device, 0);
1724 }
1725
1726 /* Setup slot structure with entry for empty slot */
1727 func = cpqhp_slot_create(ctrl->bus);
1728
1729 if (func == NULL)
1730 return 1;
1731
1732 func->bus = ctrl->bus;
1733 func->device = device;
1734 func->function = 0;
1735 func->configured = 0;
1736 func->switch_save = 0x10;
1737 func->is_a_board = 0;
1738 func->p_task_event = NULL;
1739 }
1740
1741 return 0;
1742}
1743
1744static void pushbutton_helper_thread(unsigned long data)
1745{
1746 pushbutton_pending = data;
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001747 wake_up_process(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
1750
1751/* this is the main worker thread */
1752static int event_thread(void* data)
1753{
1754 struct controller *ctrl;
Ingo Molnar60ac8f22007-07-24 11:16:37 +02001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 while (1) {
1757 dbg("!!!!event_thread sleeping\n");
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001758 set_current_state(TASK_INTERRUPTIBLE);
1759 schedule();
1760
1761 if (kthread_should_stop())
1762 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /* Do stuff here */
1764 if (pushbutton_pending)
1765 cpqhp_pushbutton_thread(pushbutton_pending);
1766 else
1767 for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
1768 interrupt_event_handler(ctrl);
1769 }
1770 dbg("event_thread signals exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return 0;
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774int cpqhp_event_start_thread(void)
1775{
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001776 cpqhp_event_thread = kthread_run(event_thread, NULL, "phpd_event");
1777 if (IS_ERR(cpqhp_event_thread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 err ("Can't start up our event thread\n");
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001779 return PTR_ERR(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return 0;
1783}
1784
1785
1786void cpqhp_event_stop_thread(void)
1787{
Christoph Hellwigfa007d82007-08-14 16:17:15 -07001788 kthread_stop(cpqhp_event_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
1791
1792static int update_slot_info(struct controller *ctrl, struct slot *slot)
1793{
1794 struct hotplug_slot_info *info;
1795 int result;
1796
1797 info = kmalloc(sizeof(*info), GFP_KERNEL);
1798 if (!info)
1799 return -ENOMEM;
1800
1801 info->power_status = get_slot_enabled(ctrl, slot);
1802 info->attention_status = cpq_get_attention_status(ctrl, slot);
1803 info->latch_status = cpq_get_latch_status(ctrl, slot);
1804 info->adapter_status = get_presence_status(ctrl, slot);
1805 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1806 kfree (info);
1807 return result;
1808}
1809
1810static void interrupt_event_handler(struct controller *ctrl)
1811{
1812 int loop = 0;
1813 int change = 1;
1814 struct pci_func *func;
1815 u8 hp_slot;
1816 struct slot *p_slot;
1817
1818 while (change) {
1819 change = 0;
1820
1821 for (loop = 0; loop < 10; loop++) {
1822 /* dbg("loop %d\n", loop); */
1823 if (ctrl->event_queue[loop].event_type != 0) {
1824 hp_slot = ctrl->event_queue[loop].hp_slot;
1825
1826 func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
1827 if (!func)
1828 return;
1829
1830 p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1831 if (!p_slot)
1832 return;
1833
1834 dbg("hp_slot %d, func %p, p_slot %p\n",
1835 hp_slot, func, p_slot);
1836
1837 if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1838 dbg("button pressed\n");
1839 } else if (ctrl->event_queue[loop].event_type ==
1840 INT_BUTTON_CANCEL) {
1841 dbg("button cancel\n");
1842 del_timer(&p_slot->task_event);
1843
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001844 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 if (p_slot->state == BLINKINGOFF_STATE) {
1847 /* slot is on */
1848 dbg("turn on green LED\n");
1849 green_LED_on (ctrl, hp_slot);
1850 } else if (p_slot->state == BLINKINGON_STATE) {
1851 /* slot is off */
1852 dbg("turn off green LED\n");
1853 green_LED_off (ctrl, hp_slot);
1854 }
1855
1856 info(msg_button_cancel, p_slot->number);
1857
1858 p_slot->state = STATIC_STATE;
1859
1860 amber_LED_off (ctrl, hp_slot);
1861
1862 set_SOGO(ctrl);
1863
1864 /* Wait for SOBS to be unset */
1865 wait_for_ctrl_irq (ctrl);
1866
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001867 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 /*** button Released (No action on press...) */
1870 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
1871 dbg("button release\n");
1872
1873 if (is_slot_enabled (ctrl, hp_slot)) {
1874 dbg("slot is on\n");
1875 p_slot->state = BLINKINGOFF_STATE;
1876 info(msg_button_off, p_slot->number);
1877 } else {
1878 dbg("slot is off\n");
1879 p_slot->state = BLINKINGON_STATE;
1880 info(msg_button_on, p_slot->number);
1881 }
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001882 mutex_lock(&ctrl->crit_sect);
Alex Chiang861fefb2009-03-31 09:23:11 -06001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 dbg("blink green LED and turn off amber\n");
Alex Chiang861fefb2009-03-31 09:23:11 -06001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 amber_LED_off (ctrl, hp_slot);
1887 green_LED_blink (ctrl, hp_slot);
Alex Chiang861fefb2009-03-31 09:23:11 -06001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 set_SOGO(ctrl);
1890
1891 /* Wait for SOBS to be unset */
1892 wait_for_ctrl_irq (ctrl);
1893
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001894 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 init_timer(&p_slot->task_event);
1896 p_slot->hp_slot = hp_slot;
1897 p_slot->ctrl = ctrl;
1898/* p_slot->physical_slot = physical_slot; */
1899 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1900 p_slot->task_event.function = pushbutton_helper_thread;
1901 p_slot->task_event.data = (u32) p_slot;
1902
1903 dbg("add_timer p_slot = %p\n", p_slot);
1904 add_timer(&p_slot->task_event);
1905 }
1906 /***********POWER FAULT */
1907 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1908 dbg("power fault\n");
1909 } else {
1910 /* refresh notification */
1911 if (p_slot)
1912 update_slot_info(ctrl, p_slot);
1913 }
1914
1915 ctrl->event_queue[loop].event_type = 0;
1916
1917 change = 1;
1918 }
1919 } /* End of FOR loop */
1920 }
1921
1922 return;
1923}
1924
1925
1926/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08001927 * cpqhp_pushbutton_thread - handle pushbutton events
1928 * @slot: target slot (struct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001930 * Scheduled procedure to handle blocking stuff for the pushbuttons.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * Handles all pending events and exits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
1933void cpqhp_pushbutton_thread(unsigned long slot)
1934{
1935 u8 hp_slot;
1936 u8 device;
1937 struct pci_func *func;
1938 struct slot *p_slot = (struct slot *) slot;
1939 struct controller *ctrl = (struct controller *) p_slot->ctrl;
1940
1941 pushbutton_pending = 0;
1942 hp_slot = p_slot->hp_slot;
1943
1944 device = p_slot->device;
1945
1946 if (is_slot_enabled(ctrl, hp_slot)) {
1947 p_slot->state = POWEROFF_STATE;
1948 /* power Down board */
1949 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1950 dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
1951 if (!func) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001952 dbg("Error! func NULL in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return ;
1954 }
1955
Adrian Bunk00395412007-10-24 18:25:00 +02001956 if (cpqhp_process_SS(ctrl, func) != 0) {
1957 amber_LED_on(ctrl, hp_slot);
1958 green_LED_on(ctrl, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Adrian Bunk00395412007-10-24 18:25:00 +02001960 set_SOGO(ctrl);
1961
1962 /* Wait for SOBS to be unset */
1963 wait_for_ctrl_irq(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965
1966 p_slot->state = STATIC_STATE;
1967 } else {
1968 p_slot->state = POWERON_STATE;
1969 /* slot is off */
1970
1971 func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
1972 dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
1973 if (!func) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001974 dbg("Error! func NULL in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return ;
1976 }
1977
Julia Lawallb8d9cb22008-12-21 16:39:37 +01001978 if (ctrl != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (cpqhp_process_SI(ctrl, func) != 0) {
1980 amber_LED_on(ctrl, hp_slot);
1981 green_LED_off(ctrl, hp_slot);
Alex Chiang861fefb2009-03-31 09:23:11 -06001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 set_SOGO(ctrl);
1984
1985 /* Wait for SOBS to be unset */
1986 wait_for_ctrl_irq (ctrl);
1987 }
1988 }
1989
1990 p_slot->state = STATIC_STATE;
1991 }
1992
1993 return;
1994}
1995
1996
1997int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
1998{
1999 u8 device, hp_slot;
2000 u16 temp_word;
2001 u32 tempdword;
2002 int rc;
2003 struct slot* p_slot;
2004 int physical_slot = 0;
2005
2006 tempdword = 0;
2007
2008 device = func->device;
2009 hp_slot = device - ctrl->slot_device_offset;
2010 p_slot = cpqhp_find_slot(ctrl, device);
2011 if (p_slot)
2012 physical_slot = p_slot->number;
2013
2014 /* Check to see if the interlock is closed */
2015 tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
2016
2017 if (tempdword & (0x01 << hp_slot)) {
2018 return 1;
2019 }
2020
2021 if (func->is_a_board) {
2022 rc = board_replaced(func, ctrl);
2023 } else {
2024 /* add board */
2025 slot_remove(func);
2026
2027 func = cpqhp_slot_create(ctrl->bus);
2028 if (func == NULL)
2029 return 1;
2030
2031 func->bus = ctrl->bus;
2032 func->device = device;
2033 func->function = 0;
2034 func->configured = 0;
2035 func->is_a_board = 1;
2036
2037 /* We have to save the presence info for these slots */
2038 temp_word = ctrl->ctrl_int_comp >> 16;
2039 func->presence_save = (temp_word >> hp_slot) & 0x01;
2040 func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
2041
2042 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2043 func->switch_save = 0;
2044 } else {
2045 func->switch_save = 0x10;
2046 }
2047
2048 rc = board_added(func, ctrl);
2049 if (rc) {
2050 if (is_bridge(func)) {
2051 bridge_slot_remove(func);
2052 } else
2053 slot_remove(func);
2054
2055 /* Setup slot structure with entry for empty slot */
2056 func = cpqhp_slot_create(ctrl->bus);
2057
2058 if (func == NULL)
2059 return 1;
2060
2061 func->bus = ctrl->bus;
2062 func->device = device;
2063 func->function = 0;
2064 func->configured = 0;
2065 func->is_a_board = 0;
2066
2067 /* We have to save the presence info for these slots */
2068 temp_word = ctrl->ctrl_int_comp >> 16;
2069 func->presence_save = (temp_word >> hp_slot) & 0x01;
2070 func->presence_save |=
2071 (temp_word >> (hp_slot + 7)) & 0x02;
2072
2073 if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
2074 func->switch_save = 0;
2075 } else {
2076 func->switch_save = 0x10;
2077 }
2078 }
2079 }
2080
2081 if (rc) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002082 dbg("%s: rc = %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
2084
2085 if (p_slot)
2086 update_slot_info(ctrl, p_slot);
2087
2088 return rc;
2089}
2090
2091
2092int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
2093{
2094 u8 device, class_code, header_type, BCR;
2095 u8 index = 0;
2096 u8 replace_flag;
2097 u32 rc = 0;
2098 unsigned int devfn;
2099 struct slot* p_slot;
2100 struct pci_bus *pci_bus = ctrl->pci_bus;
2101 int physical_slot=0;
2102
Alex Chiang861fefb2009-03-31 09:23:11 -06002103 device = func->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 func = cpqhp_slot_find(ctrl->bus, device, index++);
2105 p_slot = cpqhp_find_slot(ctrl, device);
2106 if (p_slot) {
2107 physical_slot = p_slot->number;
2108 }
2109
2110 /* Make sure there are no video controllers here */
2111 while (func && !rc) {
2112 pci_bus->number = func->bus;
2113 devfn = PCI_DEVFN(func->device, func->function);
2114
2115 /* Check the Class Code */
2116 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2117 if (rc)
2118 return rc;
2119
2120 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2121 /* Display/Video adapter (not supported) */
2122 rc = REMOVE_NOT_SUPPORTED;
2123 } else {
2124 /* See if it's a bridge */
2125 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
2126 if (rc)
2127 return rc;
2128
2129 /* If it's a bridge, check the VGA Enable bit */
2130 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
2131 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
2132 if (rc)
2133 return rc;
2134
2135 /* If the VGA Enable bit is set, remove isn't
2136 * supported */
2137 if (BCR & PCI_BRIDGE_CTL_VGA) {
2138 rc = REMOVE_NOT_SUPPORTED;
2139 }
2140 }
2141 }
2142
2143 func = cpqhp_slot_find(ctrl->bus, device, index++);
2144 }
2145
2146 func = cpqhp_slot_find(ctrl->bus, device, 0);
2147 if ((func != NULL) && !rc) {
2148 /* FIXME: Replace flag should be passed into process_SS */
2149 replace_flag = !(ctrl->add_support);
2150 rc = remove_board(func, replace_flag, ctrl);
2151 } else if (!rc) {
2152 rc = 1;
2153 }
2154
2155 if (p_slot)
2156 update_slot_info(ctrl, p_slot);
2157
2158 return rc;
2159}
2160
2161/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08002162 * switch_leds - switch the leds, go from one site to the other.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * @ctrl: controller to use
2164 * @num_of_slots: number of slots to use
Randy Dunlap26e6c662007-11-28 09:04:30 -08002165 * @work_LED: LED control value
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * @direction: 1 to start from the left side, 0 to start right.
2167 */
2168static void switch_leds(struct controller *ctrl, const int num_of_slots,
2169 u32 *work_LED, const int direction)
2170{
2171 int loop;
2172
2173 for (loop = 0; loop < num_of_slots; loop++) {
2174 if (direction)
2175 *work_LED = *work_LED >> 1;
2176 else
2177 *work_LED = *work_LED << 1;
2178 writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
2179
2180 set_SOGO(ctrl);
2181
2182 /* Wait for SOGO interrupt */
2183 wait_for_ctrl_irq(ctrl);
2184
2185 /* Get ready for next iteration */
2186 long_delay((2*HZ)/10);
2187 }
2188}
2189
2190/**
Randy Dunlap26e6c662007-11-28 09:04:30 -08002191 * cpqhp_hardware_test - runs hardware tests
2192 * @ctrl: target controller
2193 * @test_num: the number written to the "test" file in sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 *
2195 * For hot plug ctrl folks to play with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 */
2197int cpqhp_hardware_test(struct controller *ctrl, int test_num)
2198{
2199 u32 save_LED;
2200 u32 work_LED;
2201 int loop;
2202 int num_of_slots;
2203
2204 num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
2205
2206 switch (test_num) {
2207 case 1:
2208 /* Do stuff here! */
2209
2210 /* Do that funky LED thing */
2211 /* so we can restore them later */
2212 save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
2213 work_LED = 0x01010101;
2214 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2215 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2216 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2217 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2218
2219 work_LED = 0x01010000;
2220 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2221 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2222 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2223 work_LED = 0x00000101;
2224 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2225 switch_leds(ctrl, num_of_slots, &work_LED, 0);
2226 switch_leds(ctrl, num_of_slots, &work_LED, 1);
2227
2228 work_LED = 0x01010000;
2229 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2230 for (loop = 0; loop < num_of_slots; loop++) {
2231 set_SOGO(ctrl);
2232
2233 /* Wait for SOGO interrupt */
2234 wait_for_ctrl_irq (ctrl);
2235
2236 /* Get ready for next iteration */
2237 long_delay((3*HZ)/10);
2238 work_LED = work_LED >> 16;
2239 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
Alex Chiang861fefb2009-03-31 09:23:11 -06002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 set_SOGO(ctrl);
2242
2243 /* Wait for SOGO interrupt */
2244 wait_for_ctrl_irq (ctrl);
2245
2246 /* Get ready for next iteration */
2247 long_delay((3*HZ)/10);
2248 work_LED = work_LED << 16;
2249 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2250 work_LED = work_LED << 1;
2251 writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
2252 }
2253
2254 /* put it back the way it was */
2255 writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
2256
2257 set_SOGO(ctrl);
2258
2259 /* Wait for SOBS to be unset */
2260 wait_for_ctrl_irq (ctrl);
2261 break;
2262 case 2:
2263 /* Do other stuff here! */
2264 break;
2265 case 3:
2266 /* and more... */
2267 break;
2268 }
2269 return 0;
2270}
2271
2272
2273/**
2274 * configure_new_device - Configures the PCI header information of one board.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * @ctrl: pointer to controller structure
2276 * @func: pointer to function structure
2277 * @behind_bridge: 1 if this is a recursive call, 0 if not
2278 * @resources: pointer to set of resource lists
2279 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08002280 * Returns 0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 */
2282static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
2283 u8 behind_bridge, struct resource_lists * resources)
2284{
2285 u8 temp_byte, function, max_functions, stop_it;
2286 int rc;
2287 u32 ID;
2288 struct pci_func *new_slot;
2289 int index;
2290
2291 new_slot = func;
2292
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002293 dbg("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /* Check for Multi-function device */
2295 ctrl->pci_bus->number = func->bus;
2296 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
2297 if (rc) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08002298 dbg("%s: rc = %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return rc;
2300 }
2301
2302 if (temp_byte & 0x80) /* Multi-function device */
2303 max_functions = 8;
2304 else
2305 max_functions = 1;
2306
2307 function = 0;
2308
2309 do {
2310 rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
2311
2312 if (rc) {
2313 dbg("configure_new_function failed %d\n",rc);
2314 index = 0;
2315
2316 while (new_slot) {
2317 new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
2318
2319 if (new_slot)
2320 cpqhp_return_board_resources(new_slot, resources);
2321 }
2322
2323 return rc;
2324 }
2325
2326 function++;
2327
2328 stop_it = 0;
2329
2330 /* The following loop skips to the next present function
2331 * and creates a board structure */
2332
2333 while ((function < max_functions) && (!stop_it)) {
2334 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
2335
2336 if (ID == 0xFFFFFFFF) { /* There's nothing there. */
2337 function++;
2338 } else { /* There's something there */
2339 /* Setup slot structure. */
2340 new_slot = cpqhp_slot_create(func->bus);
2341
2342 if (new_slot == NULL)
2343 return 1;
2344
2345 new_slot->bus = func->bus;
2346 new_slot->device = func->device;
2347 new_slot->function = function;
2348 new_slot->is_a_board = 1;
2349 new_slot->status = 0;
2350
2351 stop_it++;
2352 }
2353 }
2354
2355 } while (function < max_functions);
2356 dbg("returning from configure_new_device\n");
2357
2358 return 0;
2359}
2360
2361
2362/*
Alex Chiang861fefb2009-03-31 09:23:11 -06002363 Configuration logic that involves the hotplug data structures and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 their bookkeeping
2365 */
2366
2367
2368/**
2369 * configure_new_function - Configures the PCI header information of one device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 * @ctrl: pointer to controller structure
2371 * @func: pointer to function structure
2372 * @behind_bridge: 1 if this is a recursive call, 0 if not
2373 * @resources: pointer to set of resource lists
2374 *
2375 * Calls itself recursively for bridged devices.
Randy Dunlap26e6c662007-11-28 09:04:30 -08002376 * Returns 0 if success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 */
2378static int configure_new_function(struct controller *ctrl, struct pci_func *func,
2379 u8 behind_bridge,
2380 struct resource_lists *resources)
2381{
2382 int cloop;
2383 u8 IRQ = 0;
2384 u8 temp_byte;
2385 u8 device;
2386 u8 class_code;
2387 u16 command;
2388 u16 temp_word;
2389 u32 temp_dword;
2390 u32 rc;
2391 u32 temp_register;
2392 u32 base;
2393 u32 ID;
2394 unsigned int devfn;
2395 struct pci_resource *mem_node;
2396 struct pci_resource *p_mem_node;
2397 struct pci_resource *io_node;
2398 struct pci_resource *bus_node;
2399 struct pci_resource *hold_mem_node;
2400 struct pci_resource *hold_p_mem_node;
2401 struct pci_resource *hold_IO_node;
2402 struct pci_resource *hold_bus_node;
2403 struct irq_mapping irqs;
2404 struct pci_func *new_slot;
2405 struct pci_bus *pci_bus;
2406 struct resource_lists temp_resources;
2407
2408 pci_bus = ctrl->pci_bus;
2409 pci_bus->number = func->bus;
2410 devfn = PCI_DEVFN(func->device, func->function);
2411
2412 /* Check for Bridge */
2413 rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
2414 if (rc)
2415 return rc;
2416
2417 if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
2418 /* set Primary bus */
2419 dbg("set Primary bus = %d\n", func->bus);
2420 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
2421 if (rc)
2422 return rc;
2423
2424 /* find range of busses to use */
2425 dbg("find ranges of buses to use\n");
2426 bus_node = get_max_resource(&(resources->bus_head), 1);
2427
2428 /* If we don't have any busses to allocate, we can't continue */
2429 if (!bus_node)
2430 return -ENOMEM;
2431
2432 /* set Secondary bus */
2433 temp_byte = bus_node->base;
2434 dbg("set Secondary bus = %d\n", bus_node->base);
2435 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
2436 if (rc)
2437 return rc;
2438
2439 /* set subordinate bus */
2440 temp_byte = bus_node->base + bus_node->length - 1;
2441 dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
2442 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2443 if (rc)
2444 return rc;
2445
2446 /* set subordinate Latency Timer and base Latency Timer */
2447 temp_byte = 0x40;
2448 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
2449 if (rc)
2450 return rc;
2451 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
2452 if (rc)
2453 return rc;
2454
2455 /* set Cache Line size */
2456 temp_byte = 0x08;
2457 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
2458 if (rc)
2459 return rc;
2460
2461 /* Setup the IO, memory, and prefetchable windows */
2462 io_node = get_max_resource(&(resources->io_head), 0x1000);
2463 if (!io_node)
2464 return -ENOMEM;
2465 mem_node = get_max_resource(&(resources->mem_head), 0x100000);
2466 if (!mem_node)
2467 return -ENOMEM;
2468 p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
2469 if (!p_mem_node)
2470 return -ENOMEM;
2471 dbg("Setup the IO, memory, and prefetchable windows\n");
2472 dbg("io_node\n");
2473 dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
2474 io_node->length, io_node->next);
2475 dbg("mem_node\n");
2476 dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
2477 mem_node->length, mem_node->next);
2478 dbg("p_mem_node\n");
2479 dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
2480 p_mem_node->length, p_mem_node->next);
2481
2482 /* set up the IRQ info */
2483 if (!resources->irqs) {
2484 irqs.barber_pole = 0;
2485 irqs.interrupt[0] = 0;
2486 irqs.interrupt[1] = 0;
2487 irqs.interrupt[2] = 0;
2488 irqs.interrupt[3] = 0;
2489 irqs.valid_INT = 0;
2490 } else {
2491 irqs.barber_pole = resources->irqs->barber_pole;
2492 irqs.interrupt[0] = resources->irqs->interrupt[0];
2493 irqs.interrupt[1] = resources->irqs->interrupt[1];
2494 irqs.interrupt[2] = resources->irqs->interrupt[2];
2495 irqs.interrupt[3] = resources->irqs->interrupt[3];
2496 irqs.valid_INT = resources->irqs->valid_INT;
2497 }
2498
2499 /* set up resource lists that are now aligned on top and bottom
2500 * for anything behind the bridge. */
2501 temp_resources.bus_head = bus_node;
2502 temp_resources.io_head = io_node;
2503 temp_resources.mem_head = mem_node;
2504 temp_resources.p_mem_head = p_mem_node;
2505 temp_resources.irqs = &irqs;
2506
2507 /* Make copies of the nodes we are going to pass down so that
Alex Chiang427438c2009-03-31 09:23:16 -06002508 * if there is a problem,we can just use these to free resources
2509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
2511 hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
2512 hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
2513 hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
2514
2515 if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
2516 kfree(hold_bus_node);
2517 kfree(hold_IO_node);
2518 kfree(hold_mem_node);
2519 kfree(hold_p_mem_node);
2520
2521 return 1;
2522 }
2523
2524 memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
2525
2526 bus_node->base += 1;
2527 bus_node->length -= 1;
2528 bus_node->next = NULL;
2529
2530 /* If we have IO resources copy them and fill in the bridge's
2531 * IO range registers */
2532 if (io_node) {
2533 memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
2534 io_node->next = NULL;
2535
2536 /* set IO base and Limit registers */
2537 temp_byte = io_node->base >> 8;
2538 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
2539
2540 temp_byte = (io_node->base + io_node->length - 1) >> 8;
2541 rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2542 } else {
2543 kfree(hold_IO_node);
2544 hold_IO_node = NULL;
2545 }
2546
2547 /* If we have memory resources copy them and fill in the
2548 * bridge's memory range registers. Otherwise, fill in the
2549 * range registers with values that disable them. */
2550 if (mem_node) {
2551 memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
2552 mem_node->next = NULL;
2553
2554 /* set Mem base and Limit registers */
2555 temp_word = mem_node->base >> 16;
2556 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2557
2558 temp_word = (mem_node->base + mem_node->length - 1) >> 16;
2559 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2560 } else {
2561 temp_word = 0xFFFF;
2562 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2563
2564 temp_word = 0x0000;
2565 rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2566
2567 kfree(hold_mem_node);
2568 hold_mem_node = NULL;
2569 }
2570
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002571 memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
2572 p_mem_node->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002574 /* set Pre Mem base and Limit registers */
2575 temp_word = p_mem_node->base >> 16;
2576 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Adrian Bunk2555f7b2005-11-21 00:05:21 +01002578 temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
2579 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Alex Chiang427438c2009-03-31 09:23:16 -06002581 /* Adjust this to compensate for extra adjustment in first loop
2582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 irqs.barber_pole--;
2584
2585 rc = 0;
2586
2587 /* Here we actually find the devices and configure them */
2588 for (device = 0; (device <= 0x1F) && !rc; device++) {
2589 irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
2590
2591 ID = 0xFFFFFFFF;
2592 pci_bus->number = hold_bus_node->base;
2593 pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
2594 pci_bus->number = func->bus;
2595
2596 if (ID != 0xFFFFFFFF) { /* device present */
2597 /* Setup slot structure. */
2598 new_slot = cpqhp_slot_create(hold_bus_node->base);
2599
2600 if (new_slot == NULL) {
2601 rc = -ENOMEM;
2602 continue;
2603 }
2604
2605 new_slot->bus = hold_bus_node->base;
2606 new_slot->device = device;
2607 new_slot->function = 0;
2608 new_slot->is_a_board = 1;
2609 new_slot->status = 0;
2610
2611 rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
2612 dbg("configure_new_device rc=0x%x\n",rc);
2613 } /* End of IF (device in slot?) */
2614 } /* End of FOR loop */
2615
2616 if (rc)
2617 goto free_and_out;
2618 /* save the interrupt routing information */
2619 if (resources->irqs) {
2620 resources->irqs->interrupt[0] = irqs.interrupt[0];
2621 resources->irqs->interrupt[1] = irqs.interrupt[1];
2622 resources->irqs->interrupt[2] = irqs.interrupt[2];
2623 resources->irqs->interrupt[3] = irqs.interrupt[3];
2624 resources->irqs->valid_INT = irqs.valid_INT;
2625 } else if (!behind_bridge) {
2626 /* We need to hook up the interrupts here */
2627 for (cloop = 0; cloop < 4; cloop++) {
2628 if (irqs.valid_INT & (0x01 << cloop)) {
2629 rc = cpqhp_set_irq(func->bus, func->device,
Bjorn Helgaas98d33332008-12-09 16:11:41 -07002630 cloop + 1, irqs.interrupt[cloop]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (rc)
2632 goto free_and_out;
2633 }
2634 } /* end of for loop */
2635 }
2636 /* Return unused bus resources
2637 * First use the temporary node to store information for
2638 * the board */
2639 if (hold_bus_node && bus_node && temp_resources.bus_head) {
2640 hold_bus_node->length = bus_node->base - hold_bus_node->base;
2641
2642 hold_bus_node->next = func->bus_head;
2643 func->bus_head = hold_bus_node;
2644
2645 temp_byte = temp_resources.bus_head->base - 1;
2646
2647 /* set subordinate bus */
2648 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
2649
2650 if (temp_resources.bus_head->length == 0) {
2651 kfree(temp_resources.bus_head);
2652 temp_resources.bus_head = NULL;
2653 } else {
2654 return_resource(&(resources->bus_head), temp_resources.bus_head);
2655 }
2656 }
2657
2658 /* If we have IO space available and there is some left,
2659 * return the unused portion */
2660 if (hold_IO_node && temp_resources.io_head) {
2661 io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
2662 &hold_IO_node, 0x1000);
2663
2664 /* Check if we were able to split something off */
2665 if (io_node) {
2666 hold_IO_node->base = io_node->base + io_node->length;
2667
2668 temp_byte = (hold_IO_node->base) >> 8;
2669 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
2670
2671 return_resource(&(resources->io_head), io_node);
2672 }
2673
2674 io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
2675
2676 /* Check if we were able to split something off */
2677 if (io_node) {
2678 /* First use the temporary node to store
2679 * information for the board */
2680 hold_IO_node->length = io_node->base - hold_IO_node->base;
2681
2682 /* If we used any, add it to the board's list */
2683 if (hold_IO_node->length) {
2684 hold_IO_node->next = func->io_head;
2685 func->io_head = hold_IO_node;
2686
2687 temp_byte = (io_node->base - 1) >> 8;
2688 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
2689
2690 return_resource(&(resources->io_head), io_node);
2691 } else {
2692 /* it doesn't need any IO */
2693 temp_word = 0x0000;
2694 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
2695
2696 return_resource(&(resources->io_head), io_node);
2697 kfree(hold_IO_node);
2698 }
2699 } else {
2700 /* it used most of the range */
2701 hold_IO_node->next = func->io_head;
2702 func->io_head = hold_IO_node;
2703 }
2704 } else if (hold_IO_node) {
2705 /* it used the whole range */
2706 hold_IO_node->next = func->io_head;
2707 func->io_head = hold_IO_node;
2708 }
2709 /* If we have memory space available and there is some left,
2710 * return the unused portion */
2711 if (hold_mem_node && temp_resources.mem_head) {
2712 mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
2713 &hold_mem_node, 0x100000);
2714
2715 /* Check if we were able to split something off */
2716 if (mem_node) {
2717 hold_mem_node->base = mem_node->base + mem_node->length;
2718
2719 temp_word = (hold_mem_node->base) >> 16;
2720 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
2721
2722 return_resource(&(resources->mem_head), mem_node);
2723 }
2724
2725 mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
2726
2727 /* Check if we were able to split something off */
2728 if (mem_node) {
2729 /* First use the temporary node to store
2730 * information for the board */
2731 hold_mem_node->length = mem_node->base - hold_mem_node->base;
2732
2733 if (hold_mem_node->length) {
2734 hold_mem_node->next = func->mem_head;
2735 func->mem_head = hold_mem_node;
2736
2737 /* configure end address */
2738 temp_word = (mem_node->base - 1) >> 16;
2739 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2740
2741 /* Return unused resources to the pool */
2742 return_resource(&(resources->mem_head), mem_node);
2743 } else {
2744 /* it doesn't need any Mem */
2745 temp_word = 0x0000;
2746 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
2747
2748 return_resource(&(resources->mem_head), mem_node);
2749 kfree(hold_mem_node);
2750 }
2751 } else {
2752 /* it used most of the range */
2753 hold_mem_node->next = func->mem_head;
2754 func->mem_head = hold_mem_node;
2755 }
2756 } else if (hold_mem_node) {
2757 /* it used the whole range */
2758 hold_mem_node->next = func->mem_head;
2759 func->mem_head = hold_mem_node;
2760 }
2761 /* If we have prefetchable memory space available and there
2762 * is some left at the end, return the unused portion */
2763 if (hold_p_mem_node && temp_resources.p_mem_head) {
2764 p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
2765 &hold_p_mem_node, 0x100000);
2766
2767 /* Check if we were able to split something off */
2768 if (p_mem_node) {
2769 hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
2770
2771 temp_word = (hold_p_mem_node->base) >> 16;
2772 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
2773
2774 return_resource(&(resources->p_mem_head), p_mem_node);
2775 }
2776
2777 p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
2778
2779 /* Check if we were able to split something off */
2780 if (p_mem_node) {
2781 /* First use the temporary node to store
2782 * information for the board */
2783 hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
2784
2785 /* If we used any, add it to the board's list */
2786 if (hold_p_mem_node->length) {
2787 hold_p_mem_node->next = func->p_mem_head;
2788 func->p_mem_head = hold_p_mem_node;
2789
2790 temp_word = (p_mem_node->base - 1) >> 16;
2791 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2792
2793 return_resource(&(resources->p_mem_head), p_mem_node);
2794 } else {
2795 /* it doesn't need any PMem */
2796 temp_word = 0x0000;
2797 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
2798
2799 return_resource(&(resources->p_mem_head), p_mem_node);
2800 kfree(hold_p_mem_node);
2801 }
2802 } else {
2803 /* it used the most of the range */
2804 hold_p_mem_node->next = func->p_mem_head;
2805 func->p_mem_head = hold_p_mem_node;
2806 }
2807 } else if (hold_p_mem_node) {
2808 /* it used the whole range */
2809 hold_p_mem_node->next = func->p_mem_head;
2810 func->p_mem_head = hold_p_mem_node;
2811 }
2812 /* We should be configuring an IRQ and the bridge's base address
2813 * registers if it needs them. Although we have never seen such
2814 * a device */
2815
2816 /* enable card */
2817 command = 0x0157; /* = PCI_COMMAND_IO |
2818 * PCI_COMMAND_MEMORY |
2819 * PCI_COMMAND_MASTER |
2820 * PCI_COMMAND_INVALIDATE |
2821 * PCI_COMMAND_PARITY |
2822 * PCI_COMMAND_SERR */
2823 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
2824
2825 /* set Bridge Control Register */
2826 command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
2827 * PCI_BRIDGE_CTL_SERR |
2828 * PCI_BRIDGE_CTL_NO_ISA */
2829 rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
2830 } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
2831 /* Standard device */
2832 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2833
2834 if (class_code == PCI_BASE_CLASS_DISPLAY) {
2835 /* Display (video) adapter (not supported) */
2836 return DEVICE_TYPE_NOT_SUPPORTED;
2837 }
2838 /* Figure out IO and memory needs */
2839 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
2840 temp_register = 0xFFFFFFFF;
2841
2842 dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
2843 rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
2844
2845 rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
2846 dbg("CND: base = 0x%x\n", temp_register);
2847
2848 if (temp_register) { /* If this register is implemented */
2849 if ((temp_register & 0x03L) == 0x01) {
2850 /* Map IO */
2851
2852 /* set base = amount of IO space */
2853 base = temp_register & 0xFFFFFFFC;
2854 base = ~base + 1;
2855
2856 dbg("CND: length = 0x%x\n", base);
2857 io_node = get_io_resource(&(resources->io_head), base);
2858 dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
2859 io_node->base, io_node->length, io_node->next);
2860 dbg("func (%p) io_head (%p)\n", func, func->io_head);
2861
2862 /* allocate the resource to the board */
2863 if (io_node) {
2864 base = io_node->base;
2865
2866 io_node->next = func->io_head;
2867 func->io_head = io_node;
2868 } else
2869 return -ENOMEM;
2870 } else if ((temp_register & 0x0BL) == 0x08) {
2871 /* Map prefetchable memory */
2872 base = temp_register & 0xFFFFFFF0;
2873 base = ~base + 1;
2874
2875 dbg("CND: length = 0x%x\n", base);
2876 p_mem_node = get_resource(&(resources->p_mem_head), base);
2877
2878 /* allocate the resource to the board */
2879 if (p_mem_node) {
2880 base = p_mem_node->base;
2881
2882 p_mem_node->next = func->p_mem_head;
2883 func->p_mem_head = p_mem_node;
2884 } else
2885 return -ENOMEM;
2886 } else if ((temp_register & 0x0BL) == 0x00) {
2887 /* Map memory */
2888 base = temp_register & 0xFFFFFFF0;
2889 base = ~base + 1;
2890
2891 dbg("CND: length = 0x%x\n", base);
2892 mem_node = get_resource(&(resources->mem_head), base);
2893
2894 /* allocate the resource to the board */
2895 if (mem_node) {
2896 base = mem_node->base;
2897
2898 mem_node->next = func->mem_head;
2899 func->mem_head = mem_node;
2900 } else
2901 return -ENOMEM;
2902 } else if ((temp_register & 0x0BL) == 0x04) {
2903 /* Map memory */
2904 base = temp_register & 0xFFFFFFF0;
2905 base = ~base + 1;
2906
2907 dbg("CND: length = 0x%x\n", base);
2908 mem_node = get_resource(&(resources->mem_head), base);
2909
2910 /* allocate the resource to the board */
2911 if (mem_node) {
2912 base = mem_node->base;
2913
2914 mem_node->next = func->mem_head;
2915 func->mem_head = mem_node;
2916 } else
2917 return -ENOMEM;
2918 } else if ((temp_register & 0x0BL) == 0x06) {
2919 /* Those bits are reserved, we can't handle this */
2920 return 1;
2921 } else {
2922 /* Requesting space below 1M */
2923 return NOT_ENOUGH_RESOURCES;
2924 }
2925
2926 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2927
2928 /* Check for 64-bit base */
2929 if ((temp_register & 0x07L) == 0x04) {
2930 cloop += 4;
2931
2932 /* Upper 32 bits of address always zero
2933 * on today's systems */
2934 /* FIXME this is probably not true on
2935 * Alpha and ia64??? */
2936 base = 0;
2937 rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
2938 }
2939 }
2940 } /* End of base register loop */
2941 if (cpqhp_legacy_mode) {
2942 /* Figure out which interrupt pin this function uses */
Alex Chiang861fefb2009-03-31 09:23:11 -06002943 rc = pci_bus_read_config_byte (pci_bus, devfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 PCI_INTERRUPT_PIN, &temp_byte);
2945
2946 /* If this function needs an interrupt and we are behind
2947 * a bridge and the pin is tied to something that's
2948 * alread mapped, set this one the same */
Alex Chiang861fefb2009-03-31 09:23:11 -06002949 if (temp_byte && resources->irqs &&
2950 (resources->irqs->valid_INT &
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
2952 /* We have to share with something already set up */
Alex Chiang861fefb2009-03-31 09:23:11 -06002953 IRQ = resources->irqs->interrupt[(temp_byte +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 resources->irqs->barber_pole - 1) & 0x03];
2955 } else {
2956 /* Program IRQ based on card type */
2957 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
2958
2959 if (class_code == PCI_BASE_CLASS_STORAGE) {
2960 IRQ = cpqhp_disk_irq;
2961 } else {
2962 IRQ = cpqhp_nic_irq;
2963 }
2964 }
2965
2966 /* IRQ Line */
2967 rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
2968 }
2969
2970 if (!behind_bridge) {
Bjorn Helgaas98d33332008-12-09 16:11:41 -07002971 rc = cpqhp_set_irq(func->bus, func->device, temp_byte, IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (rc)
2973 return 1;
2974 } else {
2975 /* TBD - this code may also belong in the other clause
2976 * of this If statement */
2977 resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
2978 resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
2979 }
2980
2981 /* Latency Timer */
2982 temp_byte = 0x40;
2983 rc = pci_bus_write_config_byte(pci_bus, devfn,
2984 PCI_LATENCY_TIMER, temp_byte);
2985
2986 /* Cache Line size */
2987 temp_byte = 0x08;
2988 rc = pci_bus_write_config_byte(pci_bus, devfn,
2989 PCI_CACHE_LINE_SIZE, temp_byte);
2990
2991 /* disable ROM base Address */
2992 temp_dword = 0x00L;
2993 rc = pci_bus_write_config_word(pci_bus, devfn,
2994 PCI_ROM_ADDRESS, temp_dword);
2995
2996 /* enable card */
2997 temp_word = 0x0157; /* = PCI_COMMAND_IO |
2998 * PCI_COMMAND_MEMORY |
2999 * PCI_COMMAND_MASTER |
3000 * PCI_COMMAND_INVALIDATE |
3001 * PCI_COMMAND_PARITY |
3002 * PCI_COMMAND_SERR */
3003 rc = pci_bus_write_config_word (pci_bus, devfn,
3004 PCI_COMMAND, temp_word);
3005 } else { /* End of Not-A-Bridge else */
3006 /* It's some strange type of PCI adapter (Cardbus?) */
3007 return DEVICE_TYPE_NOT_SUPPORTED;
3008 }
3009
3010 func->configured = 1;
3011
3012 return 0;
3013free_and_out:
3014 cpqhp_destroy_resource_list (&temp_resources);
3015
3016 return_resource(&(resources-> bus_head), hold_bus_node);
3017 return_resource(&(resources-> io_head), hold_IO_node);
3018 return_resource(&(resources-> mem_head), hold_mem_node);
3019 return_resource(&(resources-> p_mem_head), hold_p_mem_node);
3020 return rc;
3021}