cpqphp_ctrl.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:79k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  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. #include <linux/config.h>
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/types.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/delay.h>
  35. #include <linux/wait.h>
  36. #include <linux/smp_lock.h>
  37. #include <linux/pci.h>
  38. #include "cpqphp.h"
  39. static u32 configure_new_device(struct controller* ctrl, struct pci_func *func,u8 behind_bridge, struct resource_lists *resources);
  40. static int configure_new_function(struct controller* ctrl, struct pci_func *func,u8 behind_bridge, struct resource_lists *resources);
  41. static void interrupt_event_handler(struct controller *ctrl);
  42. static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
  43. static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
  44. static int event_finished;
  45. static unsigned long pushbutton_pending; /* = 0 */
  46. /* things needed for the long_delay function */
  47. static struct semaphore delay_sem;
  48. static wait_queue_head_t delay_wait;
  49. /* delay is in jiffies to wait for */
  50. static void long_delay (int delay)
  51. {
  52. DECLARE_WAITQUEUE(wait, current);
  53. /* only allow 1 customer into the delay queue at once
  54.  * yes this makes some people wait even longer, but who really cares?
  55.  * this is for _huge_ delays to make the hardware happy as the 
  56.  * signals bounce around
  57.  */
  58. down (&delay_sem);
  59. init_waitqueue_head (&delay_wait);
  60. add_wait_queue(&delay_wait, &wait);
  61. set_current_state(TASK_INTERRUPTIBLE);
  62. schedule_timeout(delay);
  63. remove_wait_queue(&delay_wait, &wait);
  64. set_current_state(TASK_RUNNING);
  65. up (&delay_sem);
  66. }
  67. //FIXME: The following line needs to be somewhere else...
  68. #define WRONG_BUS_FREQUENCY 0x07
  69. static u8 handle_switch_change(u8 change, struct controller * ctrl)
  70. {
  71. int hp_slot;
  72. u8 rc = 0;
  73. u16 temp_word;
  74. struct pci_func *func;
  75. struct event_info *taskInfo;
  76. if (!change)
  77. return 0;
  78. // Switch Change
  79. dbg("cpqsbd:  Switch interrupt received.n");
  80. for (hp_slot = 0; hp_slot < 6; hp_slot++) {
  81. if (change & (0x1L << hp_slot)) {
  82. //*********************************
  83. // this one changed.
  84. //*********************************
  85. func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
  86. //this is the structure that tells the worker thread
  87. //what to do
  88. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  89. ctrl->next_event = (ctrl->next_event + 1) % 10;
  90. taskInfo->hp_slot = hp_slot;
  91. rc++;
  92. temp_word = ctrl->ctrl_int_comp >> 16;
  93. func->presence_save = (temp_word >> hp_slot) & 0x01;
  94. func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
  95. if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
  96. //*********************************
  97. // Switch opened
  98. //*********************************
  99. func->switch_save = 0;
  100. taskInfo->event_type = INT_SWITCH_OPEN;
  101. } else {
  102. //*********************************
  103. // Switch closed
  104. //*********************************
  105. func->switch_save = 0x10;
  106. taskInfo->event_type = INT_SWITCH_CLOSE;
  107. }
  108. }
  109. }
  110. return rc;
  111. }
  112. /*
  113.  * find_slot
  114.  */
  115. static inline struct slot *find_slot (struct controller * ctrl, u8 device)
  116. {
  117. struct slot *slot;
  118. if (!ctrl)
  119. return NULL;
  120. slot = ctrl->slot;
  121. while (slot && (slot->device != device)) {
  122. slot = slot->next;
  123. }
  124. return slot;
  125. }
  126. static u8 handle_presence_change(u16 change, struct controller * ctrl)
  127. {
  128. int hp_slot;
  129. u8 rc = 0;
  130. u8 temp_byte;
  131. u16 temp_word;
  132. struct pci_func *func;
  133. struct event_info *taskInfo;
  134. struct slot *p_slot;
  135. if (!change)
  136. return 0;
  137. //*********************************
  138. // Presence Change
  139. //*********************************
  140. dbg("cpqsbd:  Presence/Notify input change.n");
  141. dbg("         Changed bits are 0x%4.4xn", change );
  142. for (hp_slot = 0; hp_slot < 6; hp_slot++) {
  143. if (change & (0x0101 << hp_slot)) {
  144. //*********************************
  145. // this one changed.
  146. //*********************************
  147. func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
  148. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  149. ctrl->next_event = (ctrl->next_event + 1) % 10;
  150. taskInfo->hp_slot = hp_slot;
  151. rc++;
  152. p_slot = find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
  153. // If the switch closed, must be a button
  154. // If not in button mode, nevermind
  155. if (func->switch_save && (ctrl->push_button == 1)) {
  156. temp_word = ctrl->ctrl_int_comp >> 16;
  157. temp_byte = (temp_word >> hp_slot) & 0x01;
  158. temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
  159. if (temp_byte != func->presence_save) {
  160. //*********************************
  161. // button Pressed (doesn't do anything)
  162. //*********************************
  163. dbg("hp_slot %d button pressedn", hp_slot);
  164. taskInfo->event_type = INT_BUTTON_PRESS;
  165. } else {
  166. //*********************************
  167. // button Released - TAKE ACTION!!!!
  168. //*********************************
  169. dbg("hp_slot %d button releasedn", hp_slot);
  170. taskInfo->event_type = INT_BUTTON_RELEASE;
  171. // Cancel if we are still blinking
  172. if ((p_slot->state == BLINKINGON_STATE)
  173.     || (p_slot->state == BLINKINGOFF_STATE)) {
  174. taskInfo->event_type = INT_BUTTON_CANCEL;
  175. dbg("hp_slot %d button canceln", hp_slot);
  176. } else if ((p_slot->state == POWERON_STATE)
  177.    || (p_slot->state == POWEROFF_STATE)) {
  178. //info(msg_button_ignore, p_slot->number);
  179. taskInfo->event_type = INT_BUTTON_IGNORE;
  180. dbg("hp_slot %d button ignoren", hp_slot);
  181. }
  182. }
  183. } else {
  184. // Switch is open, assume a presence change
  185. // Save the presence state
  186. temp_word = ctrl->ctrl_int_comp >> 16;
  187. func->presence_save = (temp_word >> hp_slot) & 0x01;
  188. func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
  189. if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
  190.     (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
  191. //*********************************
  192. // Present
  193. //*********************************
  194. taskInfo->event_type = INT_PRESENCE_ON;
  195. } else {
  196. //*********************************
  197. // Not Present
  198. //*********************************
  199. taskInfo->event_type = INT_PRESENCE_OFF;
  200. }
  201. }
  202. }
  203. }
  204. return rc;
  205. }
  206. static u8 handle_power_fault(u8 change, struct controller * ctrl)
  207. {
  208. int hp_slot;
  209. u8 rc = 0;
  210. struct pci_func *func;
  211. struct event_info *taskInfo;
  212. if (!change)
  213. return 0;
  214. //*********************************
  215. // power fault
  216. //*********************************
  217. info("power fault interruptn");
  218. for (hp_slot = 0; hp_slot < 6; hp_slot++) {
  219. if (change & (0x01 << hp_slot)) {
  220. //*********************************
  221. // this one changed.
  222. //*********************************
  223. func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
  224. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  225. ctrl->next_event = (ctrl->next_event + 1) % 10;
  226. taskInfo->hp_slot = hp_slot;
  227. rc++;
  228. if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
  229. //*********************************
  230. // power fault Cleared
  231. //*********************************
  232. func->status = 0x00;
  233. taskInfo->event_type = INT_POWER_FAULT_CLEAR;
  234. } else {
  235. //*********************************
  236. // power fault
  237. //*********************************
  238. taskInfo->event_type = INT_POWER_FAULT;
  239. if (ctrl->rev < 4) {
  240. amber_LED_on (ctrl, hp_slot);
  241. green_LED_off (ctrl, hp_slot);
  242. set_SOGO (ctrl);
  243. // this is a fatal condition, we want to crash the
  244. // machine to protect from data corruption
  245. // simulated_NMI shouldn't ever return
  246. //FIXME
  247. //simulated_NMI(hp_slot, ctrl);
  248. //The following code causes a software crash just in
  249. //case simulated_NMI did return
  250. //FIXME
  251. //panic(msg_power_fault);
  252. } else {
  253. // set power fault status for this board
  254. func->status = 0xFF;
  255. info("power fault bit %x setn", hp_slot);
  256. }
  257. }
  258. }
  259. }
  260. return rc;
  261. }
  262. /*
  263.  * sort_by_size
  264.  *
  265.  * Sorts nodes on the list by their length.
  266.  * Smallest first.
  267.  *
  268.  */
  269. static int sort_by_size(struct pci_resource **head)
  270. {
  271. struct pci_resource *current_res;
  272. struct pci_resource *next_res;
  273. int out_of_order = 1;
  274. if (!(*head))
  275. return(1);
  276. if (!((*head)->next))
  277. return(0);
  278. while (out_of_order) {
  279. out_of_order = 0;
  280. // Special case for swapping list head
  281. if (((*head)->next) &&
  282.     ((*head)->length > (*head)->next->length)) {
  283. out_of_order++;
  284. current_res = *head;
  285. *head = (*head)->next;
  286. current_res->next = (*head)->next;
  287. (*head)->next = current_res;
  288. }
  289. current_res = *head;
  290. while (current_res->next && current_res->next->next) {
  291. if (current_res->next->length > current_res->next->next->length) {
  292. out_of_order++;
  293. next_res = current_res->next;
  294. current_res->next = current_res->next->next;
  295. current_res = current_res->next;
  296. next_res->next = current_res->next;
  297. current_res->next = next_res;
  298. } else
  299. current_res = current_res->next;
  300. }
  301. }  // End of out_of_order loop
  302. return(0);
  303. }
  304. /*
  305.  * sort_by_max_size
  306.  *
  307.  * Sorts nodes on the list by their length.
  308.  * Largest first.
  309.  *
  310.  */
  311. static int sort_by_max_size(struct pci_resource **head)
  312. {
  313. struct pci_resource *current_res;
  314. struct pci_resource *next_res;
  315. int out_of_order = 1;
  316. if (!(*head))
  317. return(1);
  318. if (!((*head)->next))
  319. return(0);
  320. while (out_of_order) {
  321. out_of_order = 0;
  322. // Special case for swapping list head
  323. if (((*head)->next) &&
  324.     ((*head)->length < (*head)->next->length)) {
  325. out_of_order++;
  326. current_res = *head;
  327. *head = (*head)->next;
  328. current_res->next = (*head)->next;
  329. (*head)->next = current_res;
  330. }
  331. current_res = *head;
  332. while (current_res->next && current_res->next->next) {
  333. if (current_res->next->length < current_res->next->next->length) {
  334. out_of_order++;
  335. next_res = current_res->next;
  336. current_res->next = current_res->next->next;
  337. current_res = current_res->next;
  338. next_res->next = current_res->next;
  339. current_res->next = next_res;
  340. } else
  341. current_res = current_res->next;
  342. }
  343. }  // End of out_of_order loop
  344. return(0);
  345. }
  346. /*
  347.  * do_pre_bridge_resource_split
  348.  *
  349.  * Returns zero or one node of resources that aren't in use
  350.  *
  351.  */
  352. static struct pci_resource *do_pre_bridge_resource_split (struct pci_resource **head, struct pci_resource **orig_head, u32 alignment)
  353. {
  354. struct pci_resource *prevnode = NULL;
  355. struct pci_resource *node;
  356. struct pci_resource *split_node;
  357. u32 rc;
  358. u32 temp_dword;
  359. dbg("do_pre_bridge_resource_splitn");
  360. if (!(*head) || !(*orig_head))
  361. return(NULL);
  362. rc = cpqhp_resource_sort_and_combine(head);
  363. if (rc)
  364. return(NULL);
  365. if ((*head)->base != (*orig_head)->base)
  366. return(NULL);
  367. if ((*head)->length == (*orig_head)->length)
  368. return(NULL);
  369. // If we got here, there the bridge requires some of the resource, but
  370. // we may be able to split some off of the front
  371. node = *head;
  372. if (node->length & (alignment -1)) {
  373. // this one isn't an aligned length, so we'll make a new entry
  374. // and split it up.
  375. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  376. if (!split_node)
  377. return(NULL);
  378. temp_dword = (node->length | (alignment-1)) + 1 - alignment;
  379. split_node->base = node->base;
  380. split_node->length = temp_dword;
  381. node->length -= temp_dword;
  382. node->base += split_node->length;
  383. // Put it in the list
  384. *head = split_node;
  385. split_node->next = node;
  386. }
  387. if (node->length < alignment) {
  388. return(NULL);
  389. }
  390. // Now unlink it
  391. if (*head == node) {
  392. *head = node->next;
  393. node->next = NULL;
  394. } else {
  395. prevnode = *head;
  396. while (prevnode->next != node)
  397. prevnode = prevnode->next;
  398. prevnode->next = node->next;
  399. node->next = NULL;
  400. }
  401. return(node);
  402. }
  403. /*
  404.  * do_bridge_resource_split
  405.  *
  406.  * Returns zero or one node of resources that aren't in use
  407.  *
  408.  */
  409. static struct pci_resource *do_bridge_resource_split (struct pci_resource **head, u32 alignment)
  410. {
  411. struct pci_resource *prevnode = NULL;
  412. struct pci_resource *node;
  413. u32 rc;
  414. u32 temp_dword;
  415. if (!(*head))
  416. return(NULL);
  417. rc = cpqhp_resource_sort_and_combine(head);
  418. if (rc)
  419. return(NULL);
  420. node = *head;
  421. while (node->next) {
  422. prevnode = node;
  423. node = node->next;
  424. kfree(prevnode);
  425. }
  426. if (node->length < alignment) {
  427. kfree(node);
  428. return(NULL);
  429. }
  430. if (node->base & (alignment - 1)) {
  431. // Short circuit if adjusted size is too small
  432. temp_dword = (node->base | (alignment-1)) + 1;
  433. if ((node->length - (temp_dword - node->base)) < alignment) {
  434. kfree(node);
  435. return(NULL);
  436. }
  437. node->length -= (temp_dword - node->base);
  438. node->base = temp_dword;
  439. }
  440. if (node->length & (alignment - 1)) {
  441. // There's stuff in use after this node
  442. kfree(node);
  443. return(NULL);
  444. }
  445. return(node);
  446. }
  447. /*
  448.  * get_io_resource
  449.  *
  450.  * this function sorts the resource list by size and then
  451.  * returns the first node of "size" length that is not in the
  452.  * ISA aliasing window.  If it finds a node larger than "size"
  453.  * it will split it up.
  454.  *
  455.  * size must be a power of two.
  456.  */
  457. static struct pci_resource *get_io_resource (struct pci_resource **head, u32 size)
  458. {
  459. struct pci_resource *prevnode;
  460. struct pci_resource *node;
  461. struct pci_resource *split_node;
  462. u32 temp_dword;
  463. if (!(*head))
  464. return(NULL);
  465. if ( cpqhp_resource_sort_and_combine(head) )
  466. return(NULL);
  467. if ( sort_by_size(head) )
  468. return(NULL);
  469. for (node = *head; node; node = node->next) {
  470. if (node->length < size)
  471. continue;
  472. if (node->base & (size - 1)) {
  473. // this one isn't base aligned properly
  474. // so we'll make a new entry and split it up
  475. temp_dword = (node->base | (size-1)) + 1;
  476. // Short circuit if adjusted size is too small
  477. if ((node->length - (temp_dword - node->base)) < size)
  478. continue;
  479. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  480. if (!split_node)
  481. return(NULL);
  482. split_node->base = node->base;
  483. split_node->length = temp_dword - node->base;
  484. node->base = temp_dword;
  485. node->length -= split_node->length;
  486. // Put it in the list
  487. split_node->next = node->next;
  488. node->next = split_node;
  489. } // End of non-aligned base
  490. // Don't need to check if too small since we already did
  491. if (node->length > size) {
  492. // this one is longer than we need
  493. // so we'll make a new entry and split it up
  494. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  495. if (!split_node)
  496. return(NULL);
  497. split_node->base = node->base + size;
  498. split_node->length = node->length - size;
  499. node->length = size;
  500. // Put it in the list
  501. split_node->next = node->next;
  502. node->next = split_node;
  503. }  // End of too big on top end
  504. // For IO make sure it's not in the ISA aliasing space
  505. if (node->base & 0x300L)
  506. continue;
  507. // If we got here, then it is the right size
  508. // Now take it out of the list
  509. if (*head == node) {
  510. *head = node->next;
  511. } else {
  512. prevnode = *head;
  513. while (prevnode->next != node)
  514. prevnode = prevnode->next;
  515. prevnode->next = node->next;
  516. }
  517. node->next = NULL;
  518. // Stop looping
  519. break;
  520. }
  521. return(node);
  522. }
  523. /*
  524.  * get_max_resource
  525.  *
  526.  * Gets the largest node that is at least "size" big from the
  527.  * list pointed to by head.  It aligns the node on top and bottom
  528.  * to "size" alignment before returning it.
  529.  */
  530. static struct pci_resource *get_max_resource (struct pci_resource **head, u32 size)
  531. {
  532. struct pci_resource *max;
  533. struct pci_resource *temp;
  534. struct pci_resource *split_node;
  535. u32 temp_dword;
  536. if (!(*head))
  537. return(NULL);
  538. if (cpqhp_resource_sort_and_combine(head))
  539. return(NULL);
  540. if (sort_by_max_size(head))
  541. return(NULL);
  542. for (max = *head;max; max = max->next) {
  543. // If not big enough we could probably just bail, 
  544. // instead we'll continue to the next.
  545. if (max->length < size)
  546. continue;
  547. if (max->base & (size - 1)) {
  548. // this one isn't base aligned properly
  549. // so we'll make a new entry and split it up
  550. temp_dword = (max->base | (size-1)) + 1;
  551. // Short circuit if adjusted size is too small
  552. if ((max->length - (temp_dword - max->base)) < size)
  553. continue;
  554. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  555. if (!split_node)
  556. return(NULL);
  557. split_node->base = max->base;
  558. split_node->length = temp_dword - max->base;
  559. max->base = temp_dword;
  560. max->length -= split_node->length;
  561. // Put it next in the list
  562. split_node->next = max->next;
  563. max->next = split_node;
  564. }
  565. if ((max->base + max->length) & (size - 1)) {
  566. // this one isn't end aligned properly at the top
  567. // so we'll make a new entry and split it up
  568. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  569. if (!split_node)
  570. return(NULL);
  571. temp_dword = ((max->base + max->length) & ~(size - 1));
  572. split_node->base = temp_dword;
  573. split_node->length = max->length + max->base
  574.      - split_node->base;
  575. max->length -= split_node->length;
  576. // Put it in the list
  577. split_node->next = max->next;
  578. max->next = split_node;
  579. }
  580. // Make sure it didn't shrink too much when we aligned it
  581. if (max->length < size)
  582. continue;
  583. // Now take it out of the list
  584. temp = (struct pci_resource*) *head;
  585. if (temp == max) {
  586. *head = max->next;
  587. } else {
  588. while (temp && temp->next != max) {
  589. temp = temp->next;
  590. }
  591. temp->next = max->next;
  592. }
  593. max->next = NULL;
  594. return(max);
  595. }
  596. // If we get here, we couldn't find one
  597. return(NULL);
  598. }
  599. /*
  600.  * get_resource
  601.  *
  602.  * this function sorts the resource list by size and then
  603.  * returns the first node of "size" length.  If it finds a node
  604.  * larger than "size" it will split it up.
  605.  *
  606.  * size must be a power of two.
  607.  */
  608. static struct pci_resource *get_resource (struct pci_resource **head, u32 size)
  609. {
  610. struct pci_resource *prevnode;
  611. struct pci_resource *node;
  612. struct pci_resource *split_node;
  613. u32 temp_dword;
  614. if (!(*head))
  615. return(NULL);
  616. if ( cpqhp_resource_sort_and_combine(head) )
  617. return(NULL);
  618. if ( sort_by_size(head) )
  619. return(NULL);
  620. for (node = *head; node; node = node->next) {
  621. dbg(__FUNCTION__": req_size =%x node=%p, base=%x, length=%xn",
  622.     size, node, node->base, node->length);
  623. if (node->length < size)
  624. continue;
  625. if (node->base & (size - 1)) {
  626. dbg(__FUNCTION__": not alignedn");
  627. // this one isn't base aligned properly
  628. // so we'll make a new entry and split it up
  629. temp_dword = (node->base | (size-1)) + 1;
  630. // Short circuit if adjusted size is too small
  631. if ((node->length - (temp_dword - node->base)) < size)
  632. continue;
  633. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  634. if (!split_node)
  635. return(NULL);
  636. split_node->base = node->base;
  637. split_node->length = temp_dword - node->base;
  638. node->base = temp_dword;
  639. node->length -= split_node->length;
  640. // Put it in the list
  641. split_node->next = node->next;
  642. node->next = split_node;
  643. } // End of non-aligned base
  644. // Don't need to check if too small since we already did
  645. if (node->length > size) {
  646. dbg(__FUNCTION__": too bign");
  647. // this one is longer than we need
  648. // so we'll make a new entry and split it up
  649. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  650. if (!split_node)
  651. return(NULL);
  652. split_node->base = node->base + size;
  653. split_node->length = node->length - size;
  654. node->length = size;
  655. // Put it in the list
  656. split_node->next = node->next;
  657. node->next = split_node;
  658. }  // End of too big on top end
  659. dbg(__FUNCTION__": got one!!!n");
  660. // If we got here, then it is the right size
  661. // Now take it out of the list
  662. if (*head == node) {
  663. *head = node->next;
  664. } else {
  665. prevnode = *head;
  666. while (prevnode->next != node)
  667. prevnode = prevnode->next;
  668. prevnode->next = node->next;
  669. }
  670. node->next = NULL;
  671. // Stop looping
  672. break;
  673. }
  674. return(node);
  675. }
  676. /*
  677.  * cpqhp_resource_sort_and_combine
  678.  *
  679.  * Sorts all of the nodes in the list in ascending order by
  680.  * their base addresses.  Also does garbage collection by
  681.  * combining adjacent nodes.
  682.  *
  683.  * returns 0 if success
  684.  */
  685. int cpqhp_resource_sort_and_combine(struct pci_resource **head)
  686. {
  687. struct pci_resource *node1;
  688. struct pci_resource *node2;
  689. int out_of_order = 1;
  690. dbg(__FUNCTION__": head = %p, *head = %pn", head, *head);
  691. if (!(*head))
  692. return(1);
  693. dbg("*head->next = %pn",(*head)->next);
  694. if (!(*head)->next)
  695. return(0); /* only one item on the list, already sorted! */
  696. dbg("*head->base = 0x%xn",(*head)->base);
  697. dbg("*head->next->base = 0x%xn",(*head)->next->base);
  698. while (out_of_order) {
  699. out_of_order = 0;
  700. // Special case for swapping list head
  701. if (((*head)->next) &&
  702.     ((*head)->base > (*head)->next->base)) {
  703. node1 = *head;
  704. (*head) = (*head)->next;
  705. node1->next = (*head)->next;
  706. (*head)->next = node1;
  707. out_of_order++;
  708. }
  709. node1 = (*head);
  710. while (node1->next && node1->next->next) {
  711. if (node1->next->base > node1->next->next->base) {
  712. out_of_order++;
  713. node2 = node1->next;
  714. node1->next = node1->next->next;
  715. node1 = node1->next;
  716. node2->next = node1->next;
  717. node1->next = node2;
  718. } else
  719. node1 = node1->next;
  720. }
  721. }  // End of out_of_order loop
  722. node1 = *head;
  723. while (node1 && node1->next) {
  724. if ((node1->base + node1->length) == node1->next->base) {
  725. // Combine
  726. dbg("8..n");
  727. node1->length += node1->next->length;
  728. node2 = node1->next;
  729. node1->next = node1->next->next;
  730. kfree(node2);
  731. } else
  732. node1 = node1->next;
  733. }
  734. return(0);
  735. }
  736. void cpqhp_ctrl_intr(int IRQ, struct controller * ctrl, struct pt_regs *regs)
  737. {
  738. u8 schedule_flag = 0;
  739. u16 misc;
  740. u32 Diff;
  741. u32 temp_dword;
  742. misc = readw(ctrl->hpc_reg + MISC);
  743. //*********************************
  744. // Check to see if it was our interrupt
  745. //*********************************
  746. if (!(misc & 0x000C)) {
  747. return;
  748. }
  749. if (misc & 0x0004) {
  750. //*********************************
  751. // Serial Output interrupt Pending
  752. //*********************************
  753. // Clear the interrupt
  754. misc |= 0x0004;
  755. writew(misc, ctrl->hpc_reg + MISC);
  756. // Read to clear posted writes
  757. misc = readw(ctrl->hpc_reg + MISC);
  758. dbg (__FUNCTION__" - waking upn");
  759. wake_up_interruptible(&ctrl->queue);
  760. }
  761. if (misc & 0x0008) {
  762. // General-interrupt-input interrupt Pending
  763. Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
  764. ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  765. // Clear the interrupt
  766. writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
  767. // Read it back to clear any posted writes
  768. temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  769. if (!Diff) {
  770. // Clear all interrupts
  771. writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
  772. }
  773. schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
  774. schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
  775. schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
  776. }
  777. if (schedule_flag) {
  778. up(&event_semaphore);
  779. dbg("Signal event_semaphoren");
  780. mark_bh(IMMEDIATE_BH);
  781. }
  782. }
  783. /**
  784.  * cpqhp_slot_create - Creates a node and adds it to the proper bus.
  785.  * @busnumber - bus where new node is to be located
  786.  *
  787.  * Returns pointer to the new node or NULL if unsuccessful
  788.  */
  789. struct pci_func *cpqhp_slot_create(u8 busnumber)
  790. {
  791. struct pci_func *new_slot;
  792. struct pci_func *next;
  793. new_slot = (struct pci_func *) kmalloc(sizeof(struct pci_func), GFP_KERNEL);
  794. if (new_slot == NULL) {
  795. // I'm not dead yet!
  796. // You will be.
  797. return(new_slot);
  798. }
  799. memset(new_slot, 0, sizeof(struct pci_func));
  800. new_slot->next = NULL;
  801. new_slot->configured = 1;
  802. if (cpqhp_slot_list[busnumber] == NULL) {
  803. cpqhp_slot_list[busnumber] = new_slot;
  804. } else {
  805. next = cpqhp_slot_list[busnumber];
  806. while (next->next != NULL)
  807. next = next->next;
  808. next->next = new_slot;
  809. }
  810. return(new_slot);
  811. }
  812. /*
  813.  * slot_remove - Removes a node from the linked list of slots.
  814.  * @old_slot: slot to remove
  815.  *
  816.  * Returns 0 if successful, !0 otherwise.
  817.  */
  818. static int slot_remove(struct pci_func * old_slot)
  819. {
  820. struct pci_func *next;
  821. if (old_slot == NULL)
  822. return(1);
  823. next = cpqhp_slot_list[old_slot->bus];
  824. if (next == NULL) {
  825. return(1);
  826. }
  827. if (next == old_slot) {
  828. cpqhp_slot_list[old_slot->bus] = old_slot->next;
  829. cpqhp_destroy_board_resources(old_slot);
  830. kfree(old_slot);
  831. return(0);
  832. }
  833. while ((next->next != old_slot) && (next->next != NULL)) {
  834. next = next->next;
  835. }
  836. if (next->next == old_slot) {
  837. next->next = old_slot->next;
  838. cpqhp_destroy_board_resources(old_slot);
  839. kfree(old_slot);
  840. return(0);
  841. } else
  842. return(2);
  843. }
  844. /**
  845.  * bridge_slot_remove - Removes a node from the linked list of slots.
  846.  * @bridge: bridge to remove
  847.  *
  848.  * Returns 0 if successful, !0 otherwise.
  849.  */
  850. static int bridge_slot_remove(struct pci_func *bridge)
  851. {
  852. u8 subordinateBus, secondaryBus;
  853. u8 tempBus;
  854. struct pci_func *next;
  855. if (bridge == NULL)
  856. return(1);
  857. secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
  858. subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
  859. for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
  860. next = cpqhp_slot_list[tempBus];
  861. while (!slot_remove(next)) {
  862. next = cpqhp_slot_list[tempBus];
  863. }
  864. }
  865. next = cpqhp_slot_list[bridge->bus];
  866. if (next == NULL) {
  867. return(1);
  868. }
  869. if (next == bridge) {
  870. cpqhp_slot_list[bridge->bus] = bridge->next;
  871. kfree(bridge);
  872. return(0);
  873. }
  874. while ((next->next != bridge) && (next->next != NULL)) {
  875. next = next->next;
  876. }
  877. if (next->next == bridge) {
  878. next->next = bridge->next;
  879. kfree(bridge);
  880. return(0);
  881. } else
  882. return(2);
  883. }
  884. /**
  885.  * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
  886.  * @bus: bus to find
  887.  * @device: device to find
  888.  * @index: is 0 for first function found, 1 for the second...
  889.  *
  890.  * Returns pointer to the node if successful, %NULL otherwise.
  891.  */
  892. struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
  893. {
  894. int found = -1;
  895. struct pci_func *func;
  896. func = cpqhp_slot_list[bus];
  897. if ((func == NULL) || ((func->device == device) && (index == 0)))
  898. return(func);
  899. if (func->device == device)
  900. found++;
  901. while (func->next != NULL) {
  902. func = func->next;
  903. if (func->device == device)
  904. found++;
  905. if (found == index)
  906. return(func);
  907. }
  908. return(NULL);
  909. }
  910. // DJZ: I don't think is_bridge will work as is.
  911. //FIXME
  912. static int is_bridge(struct pci_func * func)
  913. {
  914. // Check the header type
  915. if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
  916. return 1;
  917. else
  918. return 0;
  919. }
  920. /* the following routines constitute the bulk of the 
  921.    hotplug controller logic
  922.  */
  923. /**
  924.  * board_replaced - Called after a board has been replaced in the system.
  925.  *
  926.  * This is only used if we don't have resources for hot add
  927.  * Turns power on for the board
  928.  * Checks to see if board is the same
  929.  * If board is same, reconfigures it
  930.  * If board isn't same, turns it back off.
  931.  *
  932.  */
  933. static u32 board_replaced(struct pci_func * func, struct controller * ctrl)
  934. {
  935. u8 hp_slot;
  936. u8 temp_byte;
  937. u32 index;
  938. u32 rc = 0;
  939. u32 src = 8;
  940. hp_slot = func->device - ctrl->slot_device_offset;
  941. if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
  942. //*********************************
  943. // The switch is open.
  944. //*********************************
  945. rc = INTERLOCK_OPEN;
  946. } else if (is_slot_enabled (ctrl, hp_slot)) {
  947. //*********************************
  948. // The board is already on
  949. //*********************************
  950. rc = CARD_FUNCTIONING;
  951. } else {
  952. if (ctrl->speed == 1) {
  953. // Wait for exclusive access to hardware
  954. down(&ctrl->crit_sect);
  955. // turn on board without attaching to the bus
  956. enable_slot_power (ctrl, hp_slot);
  957. set_SOGO(ctrl);
  958. // Wait for SOBS to be unset
  959. wait_for_ctrl_irq (ctrl);
  960. // Change bits in slot power register to force another shift out
  961. // NOTE: this is to work around the timer bug
  962. temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
  963. writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
  964. writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
  965. set_SOGO(ctrl);
  966. // Wait for SOBS to be unset
  967. wait_for_ctrl_irq (ctrl);
  968. if (!(readl(ctrl->hpc_reg + NON_INT_INPUT) & (0x01 << hp_slot))) {
  969. rc = WRONG_BUS_FREQUENCY;
  970. }
  971. // turn off board without attaching to the bus
  972. disable_slot_power (ctrl, hp_slot);
  973. set_SOGO(ctrl);
  974. // Wait for SOBS to be unset
  975. wait_for_ctrl_irq (ctrl);
  976. // Done with exclusive hardware access
  977. up(&ctrl->crit_sect);
  978. if (rc)
  979. return(rc);
  980. }
  981. // Wait for exclusive access to hardware
  982. down(&ctrl->crit_sect);
  983. slot_enable (ctrl, hp_slot);
  984. green_LED_blink (ctrl, hp_slot);
  985. amber_LED_off (ctrl, hp_slot);
  986. set_SOGO(ctrl);
  987. // Wait for SOBS to be unset
  988. wait_for_ctrl_irq (ctrl);
  989. // Done with exclusive hardware access
  990. up(&ctrl->crit_sect);
  991. // Wait for ~1 second because of hot plug spec
  992. long_delay(1*HZ);
  993. // Check for a power fault
  994. if (func->status == 0xFF) {
  995. // power fault occurred, but it was benign
  996. rc = POWER_FAILURE;
  997. func->status = 0;
  998. } else
  999. rc = cpqhp_valid_replace(ctrl, func);
  1000. if (!rc) {
  1001. // It must be the same board
  1002. rc = cpqhp_configure_board(ctrl, func);
  1003. if (rc || src) {
  1004. // If configuration fails, turn it off
  1005. // Get slot won't work for devices behind bridges, but
  1006. // in this case it will always be called for the "base"
  1007. // bus/dev/func of an adapter.
  1008. // Wait for exclusive access to hardware
  1009. down(&ctrl->crit_sect);
  1010. amber_LED_on (ctrl, hp_slot);
  1011. green_LED_off (ctrl, hp_slot);
  1012. slot_disable (ctrl, hp_slot);
  1013. set_SOGO(ctrl);
  1014. // Wait for SOBS to be unset
  1015. wait_for_ctrl_irq (ctrl);
  1016. // Done with exclusive hardware access
  1017. up(&ctrl->crit_sect);
  1018. if (rc)
  1019. return(rc);
  1020. else
  1021. return(1);
  1022. }
  1023. func->status = 0;
  1024. func->switch_save = 0x10;
  1025. index = 1;
  1026. while (((func = cpqhp_slot_find(func->bus, func->device, index)) != NULL) && !rc) {
  1027. rc |= cpqhp_configure_board(ctrl, func);
  1028. index++;
  1029. }
  1030. if (rc) {
  1031. // If configuration fails, turn it off
  1032. // Get slot won't work for devices behind bridges, but
  1033. // in this case it will always be called for the "base"
  1034. // bus/dev/func of an adapter.
  1035. // Wait for exclusive access to hardware
  1036. down(&ctrl->crit_sect);
  1037. amber_LED_on (ctrl, hp_slot);
  1038. green_LED_off (ctrl, hp_slot);
  1039. slot_disable (ctrl, hp_slot);
  1040. set_SOGO(ctrl);
  1041. // Wait for SOBS to be unset
  1042. wait_for_ctrl_irq (ctrl);
  1043. // Done with exclusive hardware access
  1044. up(&ctrl->crit_sect);
  1045. return(rc);
  1046. }
  1047. // Done configuring so turn LED on full time
  1048. // Wait for exclusive access to hardware
  1049. down(&ctrl->crit_sect);
  1050. green_LED_on (ctrl, hp_slot);
  1051. set_SOGO(ctrl);
  1052. // Wait for SOBS to be unset
  1053. wait_for_ctrl_irq (ctrl);
  1054. // Done with exclusive hardware access
  1055. up(&ctrl->crit_sect);
  1056. rc = 0;
  1057. } else {
  1058. // Something is wrong
  1059. // Get slot won't work for devices behind bridges, but
  1060. // in this case it will always be called for the "base"
  1061. // bus/dev/func of an adapter.
  1062. // Wait for exclusive access to hardware
  1063. down(&ctrl->crit_sect);
  1064. amber_LED_on (ctrl, hp_slot);
  1065. green_LED_off (ctrl, hp_slot);
  1066. slot_disable (ctrl, hp_slot);
  1067. set_SOGO(ctrl);
  1068. // Wait for SOBS to be unset
  1069. wait_for_ctrl_irq (ctrl);
  1070. // Done with exclusive hardware access
  1071. up(&ctrl->crit_sect);
  1072. }
  1073. }
  1074. return(rc);
  1075. }
  1076. /**
  1077.  * board_added - Called after a board has been added to the system.
  1078.  *
  1079.  * Turns power on for the board
  1080.  * Configures board
  1081.  *
  1082.  */
  1083. static u32 board_added(struct pci_func * func, struct controller * ctrl)
  1084. {
  1085. u8 hp_slot;
  1086. u8 temp_byte;
  1087. int index;
  1088. u32 temp_register = 0xFFFFFFFF;
  1089. u32 rc = 0;
  1090. struct pci_func *new_slot = NULL;
  1091. struct slot *p_slot;
  1092. struct resource_lists res_lists;
  1093. hp_slot = func->device - ctrl->slot_device_offset;
  1094. dbg(__FUNCTION__": func->device, slot_offset, hp_slot = %d, %d ,%dn",
  1095.     func->device, ctrl->slot_device_offset, hp_slot);
  1096. if (ctrl->speed == 1) {
  1097. // Wait for exclusive access to hardware
  1098. down(&ctrl->crit_sect);
  1099. // turn on board without attaching to the bus
  1100. enable_slot_power (ctrl, hp_slot);
  1101. set_SOGO(ctrl);
  1102. // Wait for SOBS to be unset
  1103. wait_for_ctrl_irq (ctrl);
  1104. // Change bits in slot power register to force another shift out
  1105. // NOTE: this is to work around the timer bug
  1106. temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
  1107. writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
  1108. writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
  1109. set_SOGO(ctrl);
  1110. // Wait for SOBS to be unset
  1111. wait_for_ctrl_irq (ctrl);
  1112. if (!(readl(ctrl->hpc_reg + NON_INT_INPUT) & (0x01 << hp_slot))) {
  1113. rc = WRONG_BUS_FREQUENCY;
  1114. }
  1115. // turn off board without attaching to the bus
  1116. disable_slot_power (ctrl, hp_slot);
  1117. set_SOGO(ctrl);
  1118. // Wait for SOBS to be unset
  1119. wait_for_ctrl_irq (ctrl);
  1120. // Done with exclusive hardware access
  1121. up(&ctrl->crit_sect);
  1122. if (rc)
  1123. return(rc);
  1124. }
  1125. p_slot = find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1126. // turn on board and blink green LED
  1127. // Wait for exclusive access to hardware
  1128. dbg(__FUNCTION__": before downn");
  1129. down(&ctrl->crit_sect);
  1130. dbg(__FUNCTION__": after downn");
  1131. dbg(__FUNCTION__": before slot_enablen");
  1132. slot_enable (ctrl, hp_slot);
  1133. dbg(__FUNCTION__": before green_LED_blinkn");
  1134. green_LED_blink (ctrl, hp_slot);
  1135. dbg(__FUNCTION__": before amber_LED_blinkn");
  1136. amber_LED_off (ctrl, hp_slot);
  1137. dbg(__FUNCTION__": before set_SOGOn");
  1138. set_SOGO(ctrl);
  1139. // Wait for SOBS to be unset
  1140. dbg(__FUNCTION__": before wait_for_ctrl_irqn");
  1141. wait_for_ctrl_irq (ctrl);
  1142. dbg(__FUNCTION__": after wait_for_ctrl_irqn");
  1143. // Done with exclusive hardware access
  1144. dbg(__FUNCTION__": before upn");
  1145. up(&ctrl->crit_sect);
  1146. dbg(__FUNCTION__": after upn");
  1147. // Wait for ~1 second because of hot plug spec
  1148. dbg(__FUNCTION__": before long_delayn");
  1149. long_delay(1*HZ);
  1150. dbg(__FUNCTION__": after long_delayn");
  1151. dbg(__FUNCTION__": func status = %xn", func->status);
  1152. // Check for a power fault
  1153. if (func->status == 0xFF) {
  1154. // power fault occurred, but it was benign
  1155. temp_register = 0xFFFFFFFF;
  1156. dbg(__FUNCTION__": temp register set to %x by power faultn", temp_register);
  1157. rc = POWER_FAILURE;
  1158. func->status = 0;
  1159. } else {
  1160. // Get vendor/device ID u32
  1161. rc = pci_read_config_dword_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_VENDOR_ID, &temp_register);
  1162. dbg(__FUNCTION__": pci_read_config_dword returns %dn", rc);
  1163. dbg(__FUNCTION__": temp_register is %xn", temp_register);
  1164. if (rc != 0) {
  1165. // Something's wrong here
  1166. temp_register = 0xFFFFFFFF;
  1167. dbg(__FUNCTION__": temp register set to %x by errorn", temp_register);
  1168. }
  1169. // Preset return code.  It will be changed later if things go okay.
  1170. rc = NO_ADAPTER_PRESENT;
  1171. }
  1172. // All F's is an empty slot or an invalid board
  1173. if (temp_register != 0xFFFFFFFF) {   // Check for a board in the slot
  1174. res_lists.io_head = ctrl->io_head;
  1175. res_lists.mem_head = ctrl->mem_head;
  1176. res_lists.p_mem_head = ctrl->p_mem_head;
  1177. res_lists.bus_head = ctrl->bus_head;
  1178. res_lists.irqs = NULL;
  1179. rc = configure_new_device(ctrl, func, 0, &res_lists);
  1180. dbg(__FUNCTION__": back from configure_new_devicen");
  1181. ctrl->io_head = res_lists.io_head;
  1182. ctrl->mem_head = res_lists.mem_head;
  1183. ctrl->p_mem_head = res_lists.p_mem_head;
  1184. ctrl->bus_head = res_lists.bus_head;
  1185. cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1186. cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1187. cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1188. cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1189. if (rc) {
  1190. // Wait for exclusive access to hardware
  1191. down(&ctrl->crit_sect);
  1192. amber_LED_on (ctrl, hp_slot);
  1193. green_LED_off (ctrl, hp_slot);
  1194. slot_disable (ctrl, hp_slot);
  1195. set_SOGO(ctrl);
  1196. // Wait for SOBS to be unset
  1197. wait_for_ctrl_irq (ctrl);
  1198. // Done with exclusive hardware access
  1199. up(&ctrl->crit_sect);
  1200. return(rc);
  1201. } else {
  1202. cpqhp_save_slot_config(ctrl, func);
  1203. }
  1204. func->status = 0;
  1205. func->switch_save = 0x10;
  1206. func->is_a_board = 0x01;
  1207. //next, we will instantiate the linux pci_dev structures (with appropriate driver notification, if already present)
  1208. dbg(__FUNCTION__": configure linux pci_dev structuren");
  1209. index = 0;
  1210. do {
  1211. new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
  1212. if (new_slot && !new_slot->pci_dev) {
  1213. cpqhp_configure_device(ctrl, new_slot);
  1214. }
  1215. } while (new_slot);
  1216. // Wait for exclusive access to hardware
  1217. down(&ctrl->crit_sect);
  1218. green_LED_on (ctrl, hp_slot);
  1219. set_SOGO(ctrl);
  1220. // Wait for SOBS to be unset
  1221. wait_for_ctrl_irq (ctrl);
  1222. // Done with exclusive hardware access
  1223. up(&ctrl->crit_sect);
  1224. } else {
  1225. // Wait for exclusive access to hardware
  1226. down(&ctrl->crit_sect);
  1227. amber_LED_on (ctrl, hp_slot);
  1228. green_LED_off (ctrl, hp_slot);
  1229. slot_disable (ctrl, hp_slot);
  1230. set_SOGO(ctrl);
  1231. // Wait for SOBS to be unset
  1232. wait_for_ctrl_irq (ctrl);
  1233. // Done with exclusive hardware access
  1234. up(&ctrl->crit_sect);
  1235. return(rc);
  1236. }
  1237. return 0;
  1238. }
  1239. /**
  1240.  * remove_board - Turns off slot and LED's
  1241.  *
  1242.  */
  1243. static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
  1244. {
  1245. int index;
  1246. u8 skip = 0;
  1247. u8 device;
  1248. u8 hp_slot;
  1249. u8 temp_byte;
  1250. u32 rc;
  1251. struct resource_lists res_lists;
  1252. struct pci_func *temp_func;
  1253. if (func == NULL)
  1254. return(1);
  1255. if (cpqhp_unconfigure_device(func))
  1256. return(1);
  1257. device = func->device;
  1258. hp_slot = func->device - ctrl->slot_device_offset;
  1259. dbg("In "__FUNCTION__", hp_slot = %dn", hp_slot);
  1260. // When we get here, it is safe to change base Address Registers.
  1261. // We will attempt to save the base Address Register Lengths
  1262. if (replace_flag || !ctrl->add_support)
  1263. rc = cpqhp_save_base_addr_length(ctrl, func);
  1264. else if (!func->bus_head && !func->mem_head &&
  1265.  !func->p_mem_head && !func->io_head) {
  1266. // Here we check to see if we've saved any of the board's
  1267. // resources already.  If so, we'll skip the attempt to
  1268. // determine what's being used.
  1269. index = 0;
  1270. temp_func = cpqhp_slot_find(func->bus, func->device, index++);
  1271. while (temp_func) {
  1272. if (temp_func->bus_head || temp_func->mem_head
  1273.     || temp_func->p_mem_head || temp_func->io_head) {
  1274. skip = 1;
  1275. break;
  1276. }
  1277. temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
  1278. }
  1279. if (!skip)
  1280. rc = cpqhp_save_used_resources(ctrl, func);
  1281. }
  1282. // Change status to shutdown
  1283. if (func->is_a_board)
  1284. func->status = 0x01;
  1285. func->configured = 0;
  1286. // Wait for exclusive access to hardware
  1287. down(&ctrl->crit_sect);
  1288. green_LED_off (ctrl, hp_slot);
  1289. slot_disable (ctrl, hp_slot);
  1290. set_SOGO(ctrl);
  1291. // turn off SERR for slot
  1292. temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
  1293. temp_byte &= ~(0x01 << hp_slot);
  1294. writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
  1295. // Wait for SOBS to be unset
  1296. wait_for_ctrl_irq (ctrl);
  1297. // Done with exclusive hardware access
  1298. up(&ctrl->crit_sect);
  1299. if (!replace_flag && ctrl->add_support) {
  1300. while (func) {
  1301. res_lists.io_head = ctrl->io_head;
  1302. res_lists.mem_head = ctrl->mem_head;
  1303. res_lists.p_mem_head = ctrl->p_mem_head;
  1304. res_lists.bus_head = ctrl->bus_head;
  1305. cpqhp_return_board_resources(func, &res_lists);
  1306. ctrl->io_head = res_lists.io_head;
  1307. ctrl->mem_head = res_lists.mem_head;
  1308. ctrl->p_mem_head = res_lists.p_mem_head;
  1309. ctrl->bus_head = res_lists.bus_head;
  1310. cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1311. cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1312. cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1313. cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1314. if (is_bridge(func)) {
  1315. bridge_slot_remove(func);
  1316. } else
  1317. slot_remove(func);
  1318. func = cpqhp_slot_find(ctrl->bus, device, 0);
  1319. }
  1320. // Setup slot structure with entry for empty slot
  1321. func = cpqhp_slot_create(ctrl->bus);
  1322. if (func == NULL) {
  1323. // Out of memory
  1324. return(1);
  1325. }
  1326. func->bus = ctrl->bus;
  1327. func->device = device;
  1328. func->function = 0;
  1329. func->configured = 0;
  1330. func->switch_save = 0x10;
  1331. func->is_a_board = 0;
  1332. func->p_task_event = NULL;
  1333. }
  1334. return 0;
  1335. }
  1336. static void pushbutton_helper_thread (unsigned long data)
  1337. {
  1338. pushbutton_pending = data;
  1339. up(&event_semaphore);
  1340. }
  1341. // this is the main worker thread
  1342. static int event_thread(void* data)
  1343. {
  1344. struct controller *ctrl;
  1345. lock_kernel();
  1346. daemonize();
  1347. //  New name
  1348. strcpy(current->comm, "phpd_event");
  1349. unlock_kernel();
  1350. while (1) {
  1351. dbg("!!!!event_thread sleepingn");
  1352. down_interruptible (&event_semaphore);
  1353. dbg("event_thread woken finished = %dn", event_finished);
  1354. if (event_finished) break;
  1355. /* Do stuff here */
  1356. if (pushbutton_pending)
  1357. cpqhp_pushbutton_thread(pushbutton_pending);
  1358. else
  1359. for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
  1360. interrupt_event_handler(ctrl);
  1361. }
  1362. dbg("event_thread signals exitn");
  1363. up(&event_exit);
  1364. return 0;
  1365. }
  1366. int cpqhp_event_start_thread (void)
  1367. {
  1368. int pid;
  1369. /* initialize our semaphores */
  1370. init_MUTEX(&delay_sem);
  1371. init_MUTEX_LOCKED(&event_semaphore);
  1372. init_MUTEX_LOCKED(&event_exit);
  1373. event_finished=0;
  1374. pid = kernel_thread(event_thread, 0, 0);
  1375. if (pid < 0) {
  1376. err ("Can't start up our event threadn");
  1377. return -1;
  1378. }
  1379. dbg("Our event thread pid = %dn", pid);
  1380. return 0;
  1381. }
  1382. void cpqhp_event_stop_thread (void)
  1383. {
  1384. event_finished = 1;
  1385. dbg("event_thread finish command givenn");
  1386. up(&event_semaphore);
  1387. dbg("wait for event_thread to exitn");
  1388. down(&event_exit);
  1389. }
  1390. static int update_slot_info (struct controller *ctrl, struct slot *slot)
  1391. {
  1392. struct hotplug_slot_info *info;
  1393. char buffer[SLOT_NAME_SIZE];
  1394. int result;
  1395. info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
  1396. if (!info)
  1397. return -ENOMEM;
  1398. make_slot_name (&buffer[0], SLOT_NAME_SIZE, slot);
  1399. info->power_status = get_slot_enabled(ctrl, slot);
  1400. info->attention_status = cpq_get_attention_status(ctrl, slot);
  1401. info->latch_status = cpq_get_latch_status(ctrl, slot);
  1402. info->adapter_status = get_presence_status(ctrl, slot);
  1403. result = pci_hp_change_slot_info(buffer, info);
  1404. kfree (info);
  1405. return result;
  1406. }
  1407. static void interrupt_event_handler(struct controller *ctrl)
  1408. {
  1409. int loop = 0;
  1410. int change = 1;
  1411. struct pci_func *func;
  1412. u8 hp_slot;
  1413. struct slot *p_slot;
  1414. while (change) {
  1415. change = 0;
  1416. for (loop = 0; loop < 10; loop++) {
  1417. //dbg("loop %dn", loop);
  1418. if (ctrl->event_queue[loop].event_type != 0) {
  1419. hp_slot = ctrl->event_queue[loop].hp_slot;
  1420. func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
  1421. p_slot = find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1422. dbg("hp_slot %d, func %p, p_slot %pn",
  1423.     hp_slot, func, p_slot);
  1424. if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
  1425. dbg("button pressedn");
  1426. } else if (ctrl->event_queue[loop].event_type == 
  1427.    INT_BUTTON_CANCEL) {
  1428. dbg("button canceln");
  1429. del_timer(&p_slot->task_event);
  1430. // Wait for exclusive access to hardware
  1431. down(&ctrl->crit_sect);
  1432. if (p_slot->state == BLINKINGOFF_STATE) {
  1433. // slot is on
  1434. // turn on green LED
  1435. dbg("turn on green LEDn");
  1436. green_LED_on (ctrl, hp_slot);
  1437. } else if (p_slot->state == BLINKINGON_STATE) {
  1438. // slot is off
  1439. // turn off green LED
  1440. dbg("turn off green LEDn");
  1441. green_LED_off (ctrl, hp_slot);
  1442. }
  1443. info(msg_button_cancel, p_slot->number);
  1444. p_slot->state = STATIC_STATE;
  1445. amber_LED_off (ctrl, hp_slot);
  1446. set_SOGO(ctrl);
  1447. // Wait for SOBS to be unset
  1448. wait_for_ctrl_irq (ctrl);
  1449. // Done with exclusive hardware access
  1450. up(&ctrl->crit_sect);
  1451. }
  1452. // ***********button Released (No action on press...)
  1453. else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
  1454. dbg("button releasen");
  1455. if (is_slot_enabled (ctrl, hp_slot)) {
  1456. // slot is on
  1457. dbg("slot is onn");
  1458. p_slot->state = BLINKINGOFF_STATE;
  1459. info(msg_button_off, p_slot->number);
  1460. } else {
  1461. // slot is off
  1462. dbg("slot is offn");
  1463. p_slot->state = BLINKINGON_STATE;
  1464. info(msg_button_on, p_slot->number);
  1465. }
  1466. // Wait for exclusive access to hardware
  1467. down(&ctrl->crit_sect);
  1468. dbg("blink green LED and turn off ambern");
  1469. amber_LED_off (ctrl, hp_slot);
  1470. green_LED_blink (ctrl, hp_slot);
  1471. set_SOGO(ctrl);
  1472. // Wait for SOBS to be unset
  1473. wait_for_ctrl_irq (ctrl);
  1474. // Done with exclusive hardware access
  1475. up(&ctrl->crit_sect);
  1476. init_timer(&p_slot->task_event);
  1477. p_slot->hp_slot = hp_slot;
  1478. p_slot->ctrl = ctrl;
  1479. // p_slot->physical_slot = physical_slot;
  1480. p_slot->task_event.expires = jiffies + 5 * HZ;   // 5 second delay
  1481. p_slot->task_event.function = pushbutton_helper_thread;
  1482. p_slot->task_event.data = (u32) p_slot;
  1483. dbg("add_timer p_slot = %pn", p_slot);
  1484. add_timer(&p_slot->task_event);
  1485. }
  1486. // ***********POWER FAULT
  1487. else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
  1488. dbg("power faultn");
  1489. } else {
  1490. /* refresh notification */
  1491. if (p_slot)
  1492. update_slot_info(ctrl, p_slot);
  1493. }
  1494. ctrl->event_queue[loop].event_type = 0;
  1495. change = 1;
  1496. }
  1497. } // End of FOR loop
  1498. }
  1499. return;
  1500. }
  1501. /**
  1502.  * cpqhp_pushbutton_thread
  1503.  *
  1504.  * Scheduled procedure to handle blocking stuff for the pushbuttons
  1505.  * Handles all pending events and exits.
  1506.  *
  1507.  */
  1508. void cpqhp_pushbutton_thread (unsigned long slot)
  1509. {
  1510. u8 hp_slot;
  1511. u8 device;
  1512. struct pci_func *func;
  1513. struct slot *p_slot = (struct slot *) slot;
  1514. struct controller *ctrl = (struct controller *) p_slot->ctrl;
  1515. pushbutton_pending = 0;
  1516. hp_slot = p_slot->hp_slot;
  1517. device = p_slot->device;
  1518. if (is_slot_enabled (ctrl, hp_slot)) {
  1519. p_slot->state = POWEROFF_STATE;
  1520. // power Down board
  1521. func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
  1522. dbg("In power_down_board, func = %p, ctrl = %pn", func, ctrl);
  1523. if (!func) {
  1524. dbg("Error! func NULL in "__FUNCTION__"n");
  1525. return ;
  1526. }
  1527. if (func != NULL && ctrl != NULL) {
  1528. if (cpqhp_process_SS(ctrl, func) != 0) {
  1529. amber_LED_on (ctrl, hp_slot);
  1530. green_LED_on (ctrl, hp_slot);
  1531. set_SOGO(ctrl);
  1532. // Wait for SOBS to be unset
  1533. wait_for_ctrl_irq (ctrl);
  1534. }
  1535. }
  1536. p_slot->state = STATIC_STATE;
  1537. } else {
  1538. p_slot->state = POWERON_STATE;
  1539. // slot is off
  1540. func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
  1541. dbg("In add_board, func = %p, ctrl = %pn", func, ctrl);
  1542. if (!func) {
  1543. dbg("Error! func NULL in "__FUNCTION__"n");
  1544. return ;
  1545. }
  1546. if (func != NULL && ctrl != NULL) {
  1547. if (cpqhp_process_SI(ctrl, func) != 0) {
  1548. amber_LED_on (ctrl, hp_slot);
  1549. green_LED_off (ctrl, hp_slot);
  1550. set_SOGO(ctrl);
  1551. // Wait for SOBS to be unset
  1552. wait_for_ctrl_irq (ctrl);
  1553. }
  1554. }
  1555. p_slot->state = STATIC_STATE;
  1556. }
  1557. return;
  1558. }
  1559. int cpqhp_process_SI (struct controller *ctrl, struct pci_func *func)
  1560. {
  1561. u8 device, hp_slot;
  1562. u16 temp_word;
  1563. u32 tempdword;
  1564. int rc;
  1565. struct slot* p_slot;
  1566. int physical_slot = 0;
  1567. if (!ctrl)
  1568. return(1);
  1569. tempdword = 0;
  1570. device = func->device;
  1571. hp_slot = device - ctrl->slot_device_offset;
  1572. p_slot = find_slot(ctrl, device);
  1573. if (p_slot) {
  1574. physical_slot = p_slot->number;
  1575. }
  1576. // Check to see if the interlock is closed
  1577. tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  1578. if (tempdword & (0x01 << hp_slot)) {
  1579. return(1);
  1580. }
  1581. if (func->is_a_board) {
  1582. rc = board_replaced(func, ctrl);
  1583. } else {
  1584. // add board
  1585. slot_remove(func);
  1586. func = cpqhp_slot_create(ctrl->bus);
  1587. if (func == NULL) {
  1588. return(1);
  1589. }
  1590. func->bus = ctrl->bus;
  1591. func->device = device;
  1592. func->function = 0;
  1593. func->configured = 0;
  1594. func->is_a_board = 1;
  1595. // We have to save the presence info for these slots
  1596. temp_word = ctrl->ctrl_int_comp >> 16;
  1597. func->presence_save = (temp_word >> hp_slot) & 0x01;
  1598. func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
  1599. if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
  1600. func->switch_save = 0;
  1601. } else {
  1602. func->switch_save = 0x10;
  1603. }
  1604. rc = board_added(func, ctrl);
  1605. if (rc) {
  1606. if (is_bridge(func)) {
  1607. bridge_slot_remove(func);
  1608. } else
  1609. slot_remove(func);
  1610. // Setup slot structure with entry for empty slot
  1611. func = cpqhp_slot_create(ctrl->bus);
  1612. if (func == NULL) {
  1613. // Out of memory
  1614. return(1);
  1615. }
  1616. func->bus = ctrl->bus;
  1617. func->device = device;
  1618. func->function = 0;
  1619. func->configured = 0;
  1620. func->is_a_board = 0;
  1621. // We have to save the presence info for these slots
  1622. temp_word = ctrl->ctrl_int_comp >> 16;
  1623. func->presence_save = (temp_word >> hp_slot) & 0x01;
  1624. func->presence_save |=
  1625. (temp_word >> (hp_slot + 7)) & 0x02;
  1626. if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
  1627. func->switch_save = 0;
  1628. } else {
  1629. func->switch_save = 0x10;
  1630. }
  1631. }
  1632. }
  1633. if (rc) {
  1634. dbg(__FUNCTION__": rc = %dn", rc);
  1635. }
  1636. if (p_slot)
  1637. update_slot_info(ctrl, p_slot);
  1638. return rc;
  1639. }
  1640. int cpqhp_process_SS (struct controller *ctrl, struct pci_func *func)
  1641. {
  1642. u8 device, class_code, header_type, BCR;
  1643. u8 index = 0;
  1644. u8 replace_flag;
  1645. u32 rc = 0;
  1646. struct slot* p_slot;
  1647. int physical_slot=0;
  1648. device = func->device; 
  1649. func = cpqhp_slot_find(ctrl->bus, device, index++);
  1650. p_slot = find_slot(ctrl, device);
  1651. if (p_slot) {
  1652. physical_slot = p_slot->number;
  1653. }
  1654. // Make sure there are no video controllers here
  1655. while (func && !rc) {
  1656. // Check the Class Code
  1657. rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, 0x0B, &class_code);
  1658. if (rc)
  1659. return rc;
  1660. if (class_code == PCI_BASE_CLASS_DISPLAY) {
  1661. /* Display/Video adapter (not supported) */
  1662. rc = REMOVE_NOT_SUPPORTED;
  1663. } else {
  1664. // See if it's a bridge
  1665. rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_HEADER_TYPE, &header_type);
  1666. if (rc)
  1667. return rc;
  1668. // If it's a bridge, check the VGA Enable bit
  1669. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  1670. rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_BRIDGE_CONTROL, &BCR);
  1671. if (rc)
  1672. return rc;
  1673. // If the VGA Enable bit is set, remove isn't supported
  1674. if (BCR & PCI_BRIDGE_CTL_VGA) {
  1675. rc = REMOVE_NOT_SUPPORTED;
  1676. }
  1677. }
  1678. }
  1679. func = cpqhp_slot_find(ctrl->bus, device, index++);
  1680. }
  1681. func = cpqhp_slot_find(ctrl->bus, device, 0);
  1682. if ((func != NULL) && !rc) {
  1683. //FIXME: Replace flag should be passed into process_SS
  1684. replace_flag = !(ctrl->add_support);
  1685. rc = remove_board(func, replace_flag, ctrl);
  1686. } else if (!rc) {
  1687. rc = 1;
  1688. }
  1689. if (p_slot)
  1690. update_slot_info(ctrl, p_slot);
  1691. return(rc);
  1692. }
  1693. /**
  1694.  * hardware_test - runs hardware tests
  1695.  *
  1696.  * For hot plug ctrl folks to play with.
  1697.  * test_num is the number entered in the GUI
  1698.  *
  1699.  */
  1700. int cpqhp_hardware_test(struct controller *ctrl, int test_num)
  1701. {
  1702. u32 save_LED;
  1703. u32 work_LED;
  1704. int loop;
  1705. int num_of_slots;
  1706. num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
  1707. switch (test_num) {
  1708. case 1:
  1709. // Do stuff here!
  1710. // Do that funky LED thing
  1711. save_LED = readl(ctrl->hpc_reg + LED_CONTROL); // so we can restore them later
  1712. work_LED = 0x01010101;
  1713. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1714. for (loop = 0; loop < num_of_slots; loop++) {
  1715. set_SOGO(ctrl);
  1716. // Wait for SOGO interrupt
  1717. wait_for_ctrl_irq (ctrl);
  1718. // Get ready for next iteration
  1719. work_LED = work_LED << 1;
  1720. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1721. long_delay((2*HZ)/10);
  1722. }
  1723. for (loop = 0; loop < num_of_slots; loop++) {
  1724. work_LED = work_LED >> 1;
  1725. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1726. set_SOGO(ctrl);
  1727. // Wait for SOGO interrupt
  1728. wait_for_ctrl_irq (ctrl);
  1729. // Get ready for next iteration
  1730. long_delay((2*HZ)/10);
  1731. }
  1732. for (loop = 0; loop < num_of_slots; loop++) {
  1733. work_LED = work_LED << 1;
  1734. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1735. set_SOGO(ctrl);
  1736. // Wait for SOGO interrupt
  1737. wait_for_ctrl_irq (ctrl);
  1738. // Get ready for next iteration
  1739. long_delay((2*HZ)/10);
  1740. }
  1741. for (loop = 0; loop < num_of_slots; loop++) {
  1742. work_LED = work_LED >> 1;
  1743. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1744. set_SOGO(ctrl);
  1745. // Wait for SOGO interrupt
  1746. wait_for_ctrl_irq (ctrl);
  1747. // Get ready for next iteration
  1748. long_delay((2*HZ)/10);
  1749. }
  1750. work_LED = 0x01010000;
  1751. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1752. for (loop = 0; loop < num_of_slots; loop++) {
  1753. set_SOGO(ctrl);
  1754. // Wait for SOGO interrupt
  1755. wait_for_ctrl_irq (ctrl);
  1756. // Get ready for next iteration
  1757. work_LED = work_LED << 1;
  1758. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1759. long_delay((2*HZ)/10);
  1760. }
  1761. for (loop = 0; loop < num_of_slots; loop++) {
  1762. work_LED = work_LED >> 1;
  1763. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1764. set_SOGO(ctrl);
  1765. // Wait for SOGO interrupt
  1766. wait_for_ctrl_irq (ctrl);
  1767. // Get ready for next iteration
  1768. long_delay((2*HZ)/10);
  1769. }
  1770. work_LED = 0x00000101;
  1771. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1772. for (loop = 0; loop < num_of_slots; loop++) {
  1773. work_LED = work_LED << 1;
  1774. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1775. set_SOGO(ctrl);
  1776. // Wait for SOGO interrupt
  1777. wait_for_ctrl_irq (ctrl);
  1778. // Get ready for next iteration
  1779. long_delay((2*HZ)/10);
  1780. }
  1781. for (loop = 0; loop < num_of_slots; loop++) {
  1782. work_LED = work_LED >> 1;
  1783. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1784. set_SOGO(ctrl);
  1785. // Wait for SOGO interrupt
  1786. wait_for_ctrl_irq (ctrl);
  1787. // Get ready for next iteration
  1788. long_delay((2*HZ)/10);
  1789. }
  1790. work_LED = 0x01010000;
  1791. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1792. for (loop = 0; loop < num_of_slots; loop++) {
  1793. set_SOGO(ctrl);
  1794. // Wait for SOGO interrupt
  1795. wait_for_ctrl_irq (ctrl);
  1796. // Get ready for next iteration
  1797. long_delay((3*HZ)/10);
  1798. work_LED = work_LED >> 16;
  1799. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1800. set_SOGO(ctrl);
  1801. // Wait for SOGO interrupt
  1802. wait_for_ctrl_irq (ctrl);
  1803. // Get ready for next iteration
  1804. long_delay((3*HZ)/10);
  1805. work_LED = work_LED << 16;
  1806. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1807. work_LED = work_LED << 1;
  1808. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1809. }
  1810. writel (save_LED, ctrl->hpc_reg + LED_CONTROL); // put it back the way it was
  1811. set_SOGO(ctrl);
  1812. // Wait for SOBS to be unset
  1813. wait_for_ctrl_irq (ctrl);
  1814. break;
  1815. case 2:
  1816. // Do other stuff here!
  1817. break;
  1818. case 3:
  1819. // and more...
  1820. break;
  1821. }
  1822. return 0;
  1823. }
  1824. /**
  1825.  * configure_new_device - Configures the PCI header information of one board.
  1826.  *
  1827.  * @ctrl: pointer to controller structure
  1828.  * @func: pointer to function structure
  1829.  * @behind_bridge: 1 if this is a recursive call, 0 if not
  1830.  * @resources: pointer to set of resource lists
  1831.  *
  1832.  * Returns 0 if success
  1833.  *
  1834.  */
  1835. static u32 configure_new_device (struct controller * ctrl, struct pci_func * func,
  1836.  u8 behind_bridge, struct resource_lists * resources)
  1837. {
  1838. u8 temp_byte, function, max_functions, stop_it;
  1839. int rc;
  1840. u32 ID;
  1841. struct pci_func *new_slot;
  1842. int index;
  1843. new_slot = func;
  1844. dbg(__FUNCTION__"n");
  1845. // Check for Multi-function device
  1846. rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, 0x0E, &temp_byte);
  1847. if (rc) {
  1848. dbg(__FUNCTION__": rc = %dn", rc);
  1849. return rc;
  1850. }
  1851. if (temp_byte & 0x80) // Multi-function device
  1852. max_functions = 8;
  1853. else
  1854. max_functions = 1;
  1855. function = 0;
  1856. do {
  1857. rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
  1858. if (rc) {
  1859. dbg("configure_new_function failed %dn",rc);
  1860. index = 0;
  1861. while (new_slot) {
  1862. new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
  1863. if (new_slot)
  1864. cpqhp_return_board_resources(new_slot, resources);
  1865. }
  1866. return(rc);
  1867. }
  1868. function++;
  1869. stop_it = 0;
  1870. //  The following loop skips to the next present function
  1871. //  and creates a board structure
  1872. while ((function < max_functions) && (!stop_it)) {
  1873. pci_read_config_dword_nodev (ctrl->pci_ops, func->bus, func->device, function, 0x00, &ID);
  1874. if (ID == 0xFFFFFFFF) {   // There's nothing there. 
  1875. function++;
  1876. } else {  // There's something there
  1877. // Setup slot structure.
  1878. new_slot = cpqhp_slot_create(func->bus);
  1879. if (new_slot == NULL) {
  1880. // Out of memory
  1881. return(1);
  1882. }
  1883. new_slot->bus = func->bus;
  1884. new_slot->device = func->device;
  1885. new_slot->function = function;
  1886. new_slot->is_a_board = 1;
  1887. new_slot->status = 0;
  1888. stop_it++;
  1889. }
  1890. }
  1891. } while (function < max_functions);
  1892. dbg("returning from configure_new_devicen");
  1893. return 0;
  1894. }
  1895. /*
  1896.   Configuration logic that involves the hotplug data structures and 
  1897.   their bookkeeping
  1898.  */
  1899. /**
  1900.  * configure_new_function - Configures the PCI header information of one device
  1901.  *
  1902.  * @ctrl: pointer to controller structure
  1903.  * @func: pointer to function structure
  1904.  * @behind_bridge: 1 if this is a recursive call, 0 if not
  1905.  * @resources: pointer to set of resource lists
  1906.  *
  1907.  * Calls itself recursively for bridged devices.
  1908.  * Returns 0 if success
  1909.  *
  1910.  */
  1911. static int configure_new_function (struct controller * ctrl, struct pci_func * func,
  1912.    u8 behind_bridge, struct resource_lists * resources)
  1913. {
  1914. int cloop;
  1915. u8 IRQ;
  1916. u8 temp_byte;
  1917. u8 device;
  1918. u8 class_code;
  1919. u16 command;
  1920. u16 temp_word;
  1921. u32 temp_dword;
  1922. u32 rc;
  1923. u32 temp_register;
  1924. u32 base;
  1925. u32 ID;
  1926. struct pci_resource *mem_node;
  1927. struct pci_resource *p_mem_node;
  1928. struct pci_resource *io_node;
  1929. struct pci_resource *bus_node;
  1930. struct pci_resource *hold_mem_node;
  1931. struct pci_resource *hold_p_mem_node;
  1932. struct pci_resource *hold_IO_node;
  1933. struct pci_resource *hold_bus_node;
  1934. struct irq_mapping irqs;
  1935. struct pci_func *new_slot;
  1936. struct resource_lists temp_resources;
  1937. // Check for Bridge
  1938. rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_HEADER_TYPE, &temp_byte);
  1939. if (rc)
  1940. return rc;
  1941. if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
  1942. // set Primary bus
  1943. dbg("set Primary bus = %dn", func->bus);
  1944. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PRIMARY_BUS, func->bus);
  1945. if (rc)
  1946. return rc;
  1947. // find range of busses to use
  1948. dbg("find ranges of buses to usen");
  1949. bus_node = get_max_resource(&resources->bus_head, 1);
  1950. // If we don't have any busses to allocate, we can't continue
  1951. if (!bus_node)
  1952. return -ENOMEM;
  1953. // set Secondary bus
  1954. temp_byte = bus_node->base;
  1955. dbg("set Secondary bus = %dn", bus_node->base);
  1956. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_SECONDARY_BUS, temp_byte);
  1957. if (rc)
  1958. return rc;
  1959. // set subordinate bus
  1960. temp_byte = bus_node->base + bus_node->length - 1;
  1961. dbg("set subordinate bus = %dn", bus_node->base + bus_node->length - 1);
  1962. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_SUBORDINATE_BUS, temp_byte);
  1963. if (rc)
  1964. return rc;
  1965. // set subordinate Latency Timer and base Latency Timer
  1966. temp_byte = 0x40;
  1967. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_SEC_LATENCY_TIMER, temp_byte);
  1968. if (rc)
  1969. return rc;
  1970. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_LATENCY_TIMER, temp_byte);
  1971. if (rc)
  1972. return rc;
  1973. // set Cache Line size
  1974. temp_byte = 0x08;
  1975. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_CACHE_LINE_SIZE, temp_byte);
  1976. if (rc)
  1977. return rc;
  1978. // Setup the IO, memory, and prefetchable windows
  1979. io_node = get_max_resource(&(resources->io_head), 0x1000);
  1980. mem_node = get_max_resource(&(resources->mem_head), 0x100000);
  1981. p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
  1982. dbg("Setup the IO, memory, and prefetchable windowsn");
  1983. dbg("io_noden");
  1984. dbg("(base, len, next) (%x, %x, %p)n", io_node->base, io_node->length, io_node->next);
  1985. dbg("mem_noden");
  1986. dbg("(base, len, next) (%x, %x, %p)n", mem_node->base, mem_node->length, mem_node->next);
  1987. dbg("p_mem_noden");
  1988. dbg("(base, len, next) (%x, %x, %p)n", p_mem_node->base, p_mem_node->length, p_mem_node->next);
  1989. // set up the IRQ info
  1990. if (!resources->irqs) {
  1991. irqs.barber_pole = 0;
  1992. irqs.interrupt[0] = 0;
  1993. irqs.interrupt[1] = 0;
  1994. irqs.interrupt[2] = 0;
  1995. irqs.interrupt[3] = 0;
  1996. irqs.valid_INT = 0;
  1997. } else {
  1998. irqs.barber_pole = resources->irqs->barber_pole;
  1999. irqs.interrupt[0] = resources->irqs->interrupt[0];
  2000. irqs.interrupt[1] = resources->irqs->interrupt[1];
  2001. irqs.interrupt[2] = resources->irqs->interrupt[2];
  2002. irqs.interrupt[3] = resources->irqs->interrupt[3];
  2003. irqs.valid_INT = resources->irqs->valid_INT;
  2004. }
  2005. // set up resource lists that are now aligned on top and bottom
  2006. // for anything behind the bridge.
  2007. temp_resources.bus_head = bus_node;
  2008. temp_resources.io_head = io_node;
  2009. temp_resources.mem_head = mem_node;
  2010. temp_resources.p_mem_head = p_mem_node;
  2011. temp_resources.irqs = &irqs;
  2012. // Make copies of the nodes we are going to pass down so that
  2013. // if there is a problem,we can just use these to free resources
  2014. hold_bus_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  2015. hold_IO_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  2016. hold_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  2017. hold_p_mem_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  2018. if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
  2019. if (hold_bus_node)
  2020. kfree(hold_bus_node);
  2021. if (hold_IO_node)
  2022. kfree(hold_IO_node);
  2023. if (hold_mem_node)
  2024. kfree(hold_mem_node);
  2025. if (hold_p_mem_node)
  2026. kfree(hold_p_mem_node);
  2027. return(1);
  2028. }
  2029. memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
  2030. bus_node->base += 1;
  2031. bus_node->length -= 1;
  2032. bus_node->next = NULL;
  2033. // If we have IO resources copy them and fill in the bridge's
  2034. // IO range registers
  2035. if (io_node) {
  2036. memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
  2037. io_node->next = NULL;
  2038. // set IO base and Limit registers
  2039. temp_byte = io_node->base >> 8;
  2040. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_BASE, temp_byte);
  2041. temp_byte = (io_node->base + io_node->length - 1) >> 8;
  2042. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_LIMIT, temp_byte);
  2043. } else {
  2044. kfree(hold_IO_node);
  2045. hold_IO_node = NULL;
  2046. }
  2047. // If we have memory resources copy them and fill in the bridge's
  2048. // memory range registers.  Otherwise, fill in the range
  2049. // registers with values that disable them.
  2050. if (mem_node) {
  2051. memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
  2052. mem_node->next = NULL;
  2053. // set Mem base and Limit registers
  2054. temp_word = mem_node->base >> 16;
  2055. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_BASE, temp_word);
  2056. temp_word = (mem_node->base + mem_node->length - 1) >> 16;
  2057. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_LIMIT, temp_word);
  2058. } else {
  2059. temp_word = 0xFFFF;
  2060. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_BASE, temp_word);
  2061. temp_word = 0x0000;
  2062. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_LIMIT, temp_word);
  2063. kfree(hold_mem_node);
  2064. hold_mem_node = NULL;
  2065. }
  2066. // If we have prefetchable memory resources copy them and 
  2067. // fill in the bridge's memory range registers.  Otherwise,
  2068. // fill in the range registers with values that disable them.
  2069. if (p_mem_node) {
  2070. memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
  2071. p_mem_node->next = NULL;
  2072. // set Pre Mem base and Limit registers
  2073. temp_word = p_mem_node->base >> 16;
  2074. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_BASE, temp_word);
  2075. temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
  2076. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_LIMIT, temp_word);
  2077. } else {
  2078. temp_word = 0xFFFF;
  2079. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_BASE, temp_word);
  2080. temp_word = 0x0000;
  2081. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_LIMIT, temp_word);
  2082. kfree(hold_p_mem_node);
  2083. hold_p_mem_node = NULL;
  2084. }
  2085. // Adjust this to compensate for extra adjustment in first loop
  2086. irqs.barber_pole--;
  2087. rc = 0;
  2088. // Here we actually find the devices and configure them
  2089. for (device = 0; (device <= 0x1F) && !rc; device++) {
  2090. irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
  2091. ID = 0xFFFFFFFF;
  2092. pci_read_config_dword_nodev (ctrl->pci_ops, hold_bus_node->base, device, 0, 0x00, &ID);
  2093. if (ID != 0xFFFFFFFF) {   //  device Present
  2094. // Setup slot structure.
  2095. new_slot = cpqhp_slot_create(hold_bus_node->base);
  2096. if (new_slot == NULL) {
  2097. // Out of memory
  2098. rc = -ENOMEM;
  2099. continue;
  2100. }
  2101. new_slot->bus = hold_bus_node->base;
  2102. new_slot->device = device;
  2103. new_slot->function = 0;
  2104. new_slot->is_a_board = 1;
  2105. new_slot->status = 0;
  2106. rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
  2107. dbg("configure_new_device rc=0x%xn",rc);
  2108. } // End of IF (device in slot?)
  2109. } // End of FOR loop
  2110. if (rc) {
  2111. cpqhp_destroy_resource_list(&temp_resources);
  2112. return_resource(&(resources->bus_head), hold_bus_node);
  2113. return_resource(&(resources->io_head), hold_IO_node);
  2114. return_resource(&(resources->mem_head), hold_mem_node);
  2115. return_resource(&(resources->p_mem_head), hold_p_mem_node);
  2116. return(rc);
  2117. }
  2118. // save the interrupt routing information
  2119. if (resources->irqs) {
  2120. resources->irqs->interrupt[0] = irqs.interrupt[0];
  2121. resources->irqs->interrupt[1] = irqs.interrupt[1];
  2122. resources->irqs->interrupt[2] = irqs.interrupt[2];
  2123. resources->irqs->interrupt[3] = irqs.interrupt[3];
  2124. resources->irqs->valid_INT = irqs.valid_INT;
  2125. } else if (!behind_bridge) {
  2126. // We need to hook up the interrupts here
  2127. for (cloop = 0; cloop < 4; cloop++) {
  2128. if (irqs.valid_INT & (0x01 << cloop)) {
  2129. rc = cpqhp_set_irq(func->bus, func->device,
  2130.    0x0A + cloop, irqs.interrupt[cloop]);
  2131. if (rc) {
  2132. cpqhp_destroy_resource_list (&temp_resources);
  2133. return_resource(&(resources-> bus_head), hold_bus_node);
  2134. return_resource(&(resources-> io_head), hold_IO_node);
  2135. return_resource(&(resources-> mem_head), hold_mem_node);
  2136. return_resource(&(resources-> p_mem_head), hold_p_mem_node);
  2137. return rc;
  2138. }
  2139. }
  2140. } // end of for loop
  2141. }
  2142. // Return unused bus resources
  2143. // First use the temporary node to store information for the board
  2144. if (hold_bus_node && bus_node && temp_resources.bus_head) {
  2145. hold_bus_node->length = bus_node->base - hold_bus_node->base;
  2146. hold_bus_node->next = func->bus_head;
  2147. func->bus_head = hold_bus_node;
  2148. temp_byte = temp_resources.bus_head->base - 1;
  2149. // set subordinate bus
  2150. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_SUBORDINATE_BUS, temp_byte);
  2151. if (temp_resources.bus_head->length == 0) {
  2152. kfree(temp_resources.bus_head);
  2153. temp_resources.bus_head = NULL;
  2154. } else {
  2155. return_resource(&(resources->bus_head), temp_resources.bus_head);
  2156. }
  2157. }
  2158. // If we have IO space available and there is some left,
  2159. // return the unused portion
  2160. if (hold_IO_node && temp_resources.io_head) {
  2161. io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
  2162.        &hold_IO_node, 0x1000);
  2163. // Check if we were able to split something off
  2164. if (io_node) {
  2165. hold_IO_node->base = io_node->base + io_node->length;
  2166. temp_byte = (hold_IO_node->base) >> 8;
  2167. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_BASE, temp_byte);
  2168. return_resource(&(resources->io_head), io_node);
  2169. }
  2170. io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
  2171. // Check if we were able to split something off
  2172. if (io_node) {
  2173. // First use the temporary node to store information for the board
  2174. hold_IO_node->length = io_node->base - hold_IO_node->base;
  2175. // If we used any, add it to the board's list
  2176. if (hold_IO_node->length) {
  2177. hold_IO_node->next = func->io_head;
  2178. func->io_head = hold_IO_node;
  2179. temp_byte = (io_node->base - 1) >> 8;
  2180. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_LIMIT, temp_byte);
  2181. return_resource(&(resources->io_head), io_node);
  2182. } else {
  2183. // it doesn't need any IO
  2184. temp_word = 0x0000;
  2185. pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_IO_LIMIT, temp_word);
  2186. return_resource(&(resources->io_head), io_node);
  2187. kfree(hold_IO_node);
  2188. }
  2189. } else {
  2190. // it used most of the range
  2191. hold_IO_node->next = func->io_head;
  2192. func->io_head = hold_IO_node;
  2193. }
  2194. } else if (hold_IO_node) {
  2195. // it used the whole range
  2196. hold_IO_node->next = func->io_head;
  2197. func->io_head = hold_IO_node;
  2198. }
  2199. // If we have memory space available and there is some left,
  2200. // return the unused portion
  2201. if (hold_mem_node && temp_resources.mem_head) {
  2202. mem_node = do_pre_bridge_resource_split(&(temp_resources.  mem_head),
  2203. &hold_mem_node, 0x100000);
  2204. // Check if we were able to split something off
  2205. if (mem_node) {
  2206. hold_mem_node->base = mem_node->base + mem_node->length;
  2207. temp_word = (hold_mem_node->base) >> 16;
  2208. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_BASE, temp_word);
  2209. return_resource(&(resources->mem_head), mem_node);
  2210. }
  2211. mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
  2212. // Check if we were able to split something off
  2213. if (mem_node) {
  2214. // First use the temporary node to store information for the board
  2215. hold_mem_node->length = mem_node->base - hold_mem_node->base;
  2216. if (hold_mem_node->length) {
  2217. hold_mem_node->next = func->mem_head;
  2218. func->mem_head = hold_mem_node;
  2219. // configure end address
  2220. temp_word = (mem_node->base - 1) >> 16;
  2221. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_LIMIT, temp_word);
  2222. // Return unused resources to the pool
  2223. return_resource(&(resources->mem_head), mem_node);
  2224. } else {
  2225. // it doesn't need any Mem
  2226. temp_word = 0x0000;
  2227. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_MEMORY_LIMIT, temp_word);
  2228. return_resource(&(resources->mem_head), mem_node);
  2229. kfree(hold_mem_node);
  2230. }
  2231. } else {
  2232. // it used most of the range
  2233. hold_mem_node->next = func->mem_head;
  2234. func->mem_head = hold_mem_node;
  2235. }
  2236. } else if (hold_mem_node) {
  2237. // it used the whole range
  2238. hold_mem_node->next = func->mem_head;
  2239. func->mem_head = hold_mem_node;
  2240. }
  2241. // If we have prefetchable memory space available and there is some 
  2242. // left at the end, return the unused portion
  2243. if (hold_p_mem_node && temp_resources.p_mem_head) {
  2244. p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
  2245.   &hold_p_mem_node, 0x100000);
  2246. // Check if we were able to split something off
  2247. if (p_mem_node) {
  2248. hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
  2249. temp_word = (hold_p_mem_node->base) >> 16;
  2250. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_BASE, temp_word);
  2251. return_resource(&(resources->p_mem_head), p_mem_node);
  2252. }
  2253. p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
  2254. // Check if we were able to split something off
  2255. if (p_mem_node) {
  2256. // First use the temporary node to store information for the board
  2257. hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
  2258. // If we used any, add it to the board's list
  2259. if (hold_p_mem_node->length) {
  2260. hold_p_mem_node->next = func->p_mem_head;
  2261. func->p_mem_head = hold_p_mem_node;
  2262. temp_word = (p_mem_node->base - 1) >> 16;
  2263. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_LIMIT, temp_word);
  2264. return_resource(&(resources->p_mem_head), p_mem_node);
  2265. } else {
  2266. // it doesn't need any PMem
  2267. temp_word = 0x0000;
  2268. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_PREF_MEMORY_LIMIT, temp_word);
  2269. return_resource(&(resources->p_mem_head), p_mem_node);
  2270. kfree(hold_p_mem_node);
  2271. }
  2272. } else {
  2273. // it used the most of the range
  2274. hold_p_mem_node->next = func->p_mem_head;
  2275. func->p_mem_head = hold_p_mem_node;
  2276. }
  2277. } else if (hold_p_mem_node) {
  2278. // it used the whole range
  2279. hold_p_mem_node->next = func->p_mem_head;
  2280. func->p_mem_head = hold_p_mem_node;
  2281. }
  2282. // We should be configuring an IRQ and the bridge's base address
  2283. // registers if it needs them.  Although we have never seen such
  2284. // a device
  2285. // enable card
  2286. command = 0x0157; // = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |  PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | PCI_COMMAND_SERR
  2287. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_COMMAND, command);
  2288. // set Bridge Control Register
  2289. command = 0x07; // = PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR | PCI_BRIDGE_CTL_NO_ISA
  2290. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_BRIDGE_CONTROL, command);
  2291. } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
  2292. // Standard device
  2293. rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, 0x0B, &class_code);
  2294. if (class_code == PCI_BASE_CLASS_DISPLAY) {
  2295. // Display (video) adapter (not supported)
  2296. return(DEVICE_TYPE_NOT_SUPPORTED);
  2297. }
  2298. // Figure out IO and memory needs
  2299. for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
  2300. temp_register = 0xFFFFFFFF;
  2301. dbg("CND: bus=%d, device=%d, func=%d, offset=%dn", func->bus, func->device, func->function, cloop);
  2302. rc = pci_write_config_dword_nodev(ctrl->pci_ops, func->bus, func->device, func->function, cloop, temp_register);
  2303. rc = pci_read_config_dword_nodev (ctrl->pci_ops, func->bus, func->device, func->function, cloop, &temp_register);
  2304. dbg("CND: base = 0x%xn", temp_register);
  2305. if (temp_register) {   // If this register is implemented
  2306. if ((temp_register & 0x03L) == 0x01) {
  2307. // Map IO
  2308. // set base = amount of IO space
  2309. base = temp_register & 0xFFFFFFFC;
  2310. base = ~base + 1;
  2311. dbg("CND:      length = 0x%xn", base);
  2312. io_node = get_io_resource(&(resources->io_head), base);
  2313. dbg("Got io_node start = %8.8x, length = %8.8x next (%p)n",
  2314.     io_node->base, io_node->length, io_node->next);
  2315. dbg("func (%p) io_head (%p)n", func, func->io_head);
  2316. // allocate the resource to the board
  2317. if (io_node) {
  2318. base = io_node->base;
  2319. io_node->next = func->io_head;
  2320. func->io_head = io_node;
  2321. } else
  2322. return -ENOMEM;
  2323. } else if ((temp_register & 0x0BL) == 0x08) {
  2324. // Map prefetchable memory
  2325. base = temp_register & 0xFFFFFFF0;
  2326. base = ~base + 1;
  2327. dbg("CND:      length = 0x%xn", base);
  2328. p_mem_node = get_resource(&(resources->p_mem_head), base);
  2329. // allocate the resource to the board
  2330. if (p_mem_node) {
  2331. base = p_mem_node->base;
  2332. p_mem_node->next = func->p_mem_head;
  2333. func->p_mem_head = p_mem_node;
  2334. } else
  2335. return -ENOMEM;
  2336. } else if ((temp_register & 0x0BL) == 0x00) {
  2337. // Map memory
  2338. base = temp_register & 0xFFFFFFF0;
  2339. base = ~base + 1;
  2340. dbg("CND:      length = 0x%xn", base);
  2341. mem_node = get_resource(&(resources->mem_head), base);
  2342. // allocate the resource to the board
  2343. if (mem_node) {
  2344. base = mem_node->base;
  2345. mem_node->next = func->mem_head;
  2346. func->mem_head = mem_node;
  2347. } else
  2348. return -ENOMEM;
  2349. } else if ((temp_register & 0x0BL) == 0x04) {
  2350. // Map memory
  2351. base = temp_register & 0xFFFFFFF0;
  2352. base = ~base + 1;
  2353. dbg("CND:      length = 0x%xn", base);
  2354. mem_node = get_resource(&(resources->mem_head), base);
  2355. // allocate the resource to the board
  2356. if (mem_node) {
  2357. base = mem_node->base;
  2358. mem_node->next = func->mem_head;
  2359. func->mem_head = mem_node;
  2360. } else
  2361. return -ENOMEM;
  2362. } else if ((temp_register & 0x0BL) == 0x06) {
  2363. // Those bits are reserved, we can't handle this
  2364. return(1);
  2365. } else {
  2366. // Requesting space below 1M
  2367. return(NOT_ENOUGH_RESOURCES);
  2368. }
  2369. rc = pci_write_config_dword_nodev(ctrl->pci_ops, func->bus, func->device, func->function, cloop, base);
  2370. // Check for 64-bit base
  2371. if ((temp_register & 0x07L) == 0x04) {
  2372. cloop += 4;
  2373. // Upper 32 bits of address always zero on today's systems
  2374. // FIXME this is probably not true on Alpha and ia64???
  2375. base = 0;
  2376. rc = pci_write_config_dword_nodev(ctrl->pci_ops, func->bus, func->device, func->function, cloop, base);
  2377. }
  2378. }
  2379. } // End of base register loop
  2380. // Figure out which interrupt pin this function uses
  2381. rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, PCI_INTERRUPT_PIN, &temp_byte);
  2382. // If this function needs an interrupt and we are behind a bridge
  2383. // and the pin is tied to something that's alread mapped,
  2384. // set this one the same
  2385. if (temp_byte && resources->irqs && 
  2386.     (resources->irqs->valid_INT & 
  2387.      (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
  2388. // We have to share with something already set up
  2389. IRQ = resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03];
  2390. } else {
  2391. // Program IRQ based on card type
  2392. rc = pci_read_config_byte_nodev (ctrl->pci_ops, func->bus, func->device, func->function, 0x0B, &class_code);
  2393. if (class_code == PCI_BASE_CLASS_STORAGE) {
  2394. IRQ = cpqhp_disk_irq;
  2395. } else {
  2396. IRQ = cpqhp_nic_irq;
  2397. }
  2398. }
  2399. // IRQ Line
  2400. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_INTERRUPT_LINE, IRQ);
  2401. if (!behind_bridge) {
  2402. rc = cpqhp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
  2403. if (rc)
  2404. return(1);
  2405. } else {
  2406. //TBD - this code may also belong in the other clause of this If statement
  2407. resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
  2408. resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
  2409. }
  2410. // Latency Timer
  2411. temp_byte = 0x40;
  2412. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_LATENCY_TIMER, temp_byte);
  2413. // Cache Line size
  2414. temp_byte = 0x08;
  2415. rc = pci_write_config_byte_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_CACHE_LINE_SIZE, temp_byte);
  2416. // disable ROM base Address
  2417. temp_dword = 0x00L;
  2418. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_ROM_ADDRESS, temp_dword);
  2419. // enable card
  2420. temp_word = 0x0157; // = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |  PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | PCI_COMMAND_SERR
  2421. rc = pci_write_config_word_nodev(ctrl->pci_ops, func->bus, func->device, func->function, PCI_COMMAND, temp_word);
  2422. } // End of Not-A-Bridge else
  2423. else {
  2424. // It's some strange type of PCI adapter (Cardbus?)
  2425. return(DEVICE_TYPE_NOT_SUPPORTED);
  2426. }
  2427. func->configured = 1;
  2428. return 0;
  2429. }