pciehp_pci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * PCI Express Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/slab.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/pci.h>
  37. #include "../pci.h"
  38. #include "pciehp.h"
  39. #ifndef CONFIG_IA64
  40. #include "../../../arch/i386/pci/pci.h" /* horrible hack showing how processor dependant we are... */
  41. #endif
  42. int pciehp_configure_device (struct controller* ctrl, struct pci_func* func)
  43. {
  44. unsigned char bus;
  45. struct pci_bus *child;
  46. int num;
  47. if (func->pci_dev == NULL)
  48. func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
  49. /* Still NULL ? Well then scan for it ! */
  50. if (func->pci_dev == NULL) {
  51. dbg("%s: pci_dev still null. do pci_scan_slot\n", __FUNCTION__);
  52. num = pci_scan_slot(ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function));
  53. if (num)
  54. pci_bus_add_devices(ctrl->pci_dev->subordinate);
  55. func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
  56. if (func->pci_dev == NULL) {
  57. dbg("ERROR: pci_dev still null\n");
  58. return 0;
  59. }
  60. }
  61. if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  62. pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
  63. child = pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
  64. pci_do_scan_bus(child);
  65. }
  66. return 0;
  67. }
  68. int pciehp_unconfigure_device(struct pci_func* func)
  69. {
  70. int rc = 0;
  71. int j;
  72. struct pci_bus *pbus;
  73. dbg("%s: bus/dev/func = %x/%x/%x\n", __FUNCTION__, func->bus,
  74. func->device, func->function);
  75. pbus = func->pci_dev->bus;
  76. for (j=0; j<8 ; j++) {
  77. struct pci_dev* temp = pci_find_slot(func->bus,
  78. (func->device << 3) | j);
  79. if (temp) {
  80. pci_remove_bus_device(temp);
  81. }
  82. }
  83. /*
  84. * Some PCI Express root ports require fixup after hot-plug operation.
  85. */
  86. if (pcie_mch_quirk)
  87. pci_fixup_device(pci_fixup_final, pbus->self);
  88. return rc;
  89. }
  90. /*
  91. * pciehp_set_irq
  92. *
  93. * @bus_num: bus number of PCI device
  94. * @dev_num: device number of PCI device
  95. * @slot: pointer to u8 where slot number will be returned
  96. */
  97. int pciehp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
  98. {
  99. #if defined(CONFIG_X86) && !defined(CONFIG_X86_IO_APIC) && !defined(CONFIG_X86_64)
  100. int rc;
  101. u16 temp_word;
  102. struct pci_dev fakedev;
  103. struct pci_bus fakebus;
  104. fakedev.devfn = dev_num << 3;
  105. fakedev.bus = &fakebus;
  106. fakebus.number = bus_num;
  107. dbg("%s: dev %d, bus %d, pin %d, num %d\n",
  108. __FUNCTION__, dev_num, bus_num, int_pin, irq_num);
  109. rc = pcibios_set_irq_routing(&fakedev, int_pin - 0x0a, irq_num);
  110. dbg("%s: rc %d\n", __FUNCTION__, rc);
  111. if (!rc)
  112. return !rc;
  113. /* set the Edge Level Control Register (ELCR) */
  114. temp_word = inb(0x4d0);
  115. temp_word |= inb(0x4d1) << 8;
  116. temp_word |= 0x01 << irq_num;
  117. /* This should only be for x86 as it sets the Edge Level Control Register */
  118. outb((u8) (temp_word & 0xFF), 0x4d0);
  119. outb((u8) ((temp_word & 0xFF00) >> 8), 0x4d1);
  120. #endif
  121. return 0;
  122. }
  123. /* More PCI configuration routines; this time centered around hotplug controller */
  124. /*
  125. * pciehp_save_config
  126. *
  127. * Reads configuration for all slots in a PCI bus and saves info.
  128. *
  129. * Note: For non-hot plug busses, the slot # saved is the device #
  130. *
  131. * returns 0 if success
  132. */
  133. int pciehp_save_config(struct controller *ctrl, int busnumber, int num_ctlr_slots, int first_device_num)
  134. {
  135. int rc;
  136. u8 class_code;
  137. u8 header_type;
  138. u32 ID;
  139. u8 secondary_bus;
  140. struct pci_func *new_slot;
  141. int sub_bus;
  142. int max_functions;
  143. int function;
  144. u8 DevError;
  145. int device = 0;
  146. int cloop = 0;
  147. int stop_it;
  148. int index;
  149. int is_hot_plug = num_ctlr_slots || first_device_num;
  150. struct pci_bus lpci_bus, *pci_bus;
  151. int FirstSupported, LastSupported;
  152. dbg("%s: Enter\n", __FUNCTION__);
  153. memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
  154. pci_bus = &lpci_bus;
  155. dbg("%s: num_ctlr_slots = %d, first_device_num = %d\n", __FUNCTION__,
  156. num_ctlr_slots, first_device_num);
  157. /* Decide which slots are supported */
  158. if (is_hot_plug) {
  159. /*********************************
  160. * is_hot_plug is the slot mask
  161. *********************************/
  162. FirstSupported = first_device_num;
  163. LastSupported = FirstSupported + num_ctlr_slots - 1;
  164. } else {
  165. FirstSupported = 0;
  166. LastSupported = 0x1F;
  167. }
  168. dbg("FirstSupported = %d, LastSupported = %d\n", FirstSupported,
  169. LastSupported);
  170. /* Save PCI configuration space for all devices in supported slots */
  171. dbg("%s: pci_bus->number = %x\n", __FUNCTION__, pci_bus->number);
  172. pci_bus->number = busnumber;
  173. dbg("%s: bus = %x, dev = %x\n", __FUNCTION__, busnumber, device);
  174. for (device = FirstSupported; device <= LastSupported; device++) {
  175. ID = 0xFFFFFFFF;
  176. rc = pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0),
  177. PCI_VENDOR_ID, &ID);
  178. if (ID != 0xFFFFFFFF) { /* device in slot */
  179. dbg("%s: ID = %x\n", __FUNCTION__, ID);
  180. rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, 0),
  181. 0x0B, &class_code);
  182. if (rc)
  183. return rc;
  184. rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, 0),
  185. PCI_HEADER_TYPE, &header_type);
  186. if (rc)
  187. return rc;
  188. dbg("class_code = %x, header_type = %x\n", class_code, header_type);
  189. /* If multi-function device, set max_functions to 8 */
  190. if (header_type & 0x80)
  191. max_functions = 8;
  192. else
  193. max_functions = 1;
  194. function = 0;
  195. do {
  196. DevError = 0;
  197. dbg("%s: In do loop\n", __FUNCTION__);
  198. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* P-P Bridge */
  199. /* Recurse the subordinate bus
  200. * get the subordinate bus number
  201. */
  202. rc = pci_bus_read_config_byte(pci_bus,
  203. PCI_DEVFN(device, function),
  204. PCI_SECONDARY_BUS, &secondary_bus);
  205. if (rc) {
  206. return rc;
  207. } else {
  208. sub_bus = (int) secondary_bus;
  209. /* Save secondary bus cfg spc with this recursive call. */
  210. rc = pciehp_save_config(ctrl, sub_bus, 0, 0);
  211. if (rc)
  212. return rc;
  213. }
  214. }
  215. index = 0;
  216. new_slot = pciehp_slot_find(busnumber, device, index++);
  217. dbg("%s: new_slot = %p bus %x dev %x fun %x\n",
  218. __FUNCTION__, new_slot, busnumber, device, index-1);
  219. while (new_slot && (new_slot->function != (u8) function)) {
  220. new_slot = pciehp_slot_find(busnumber, device, index++);
  221. dbg("%s: while loop, new_slot = %p bus %x dev %x fun %x\n",
  222. __FUNCTION__, new_slot, busnumber, device, index-1);
  223. }
  224. if (!new_slot) {
  225. /* Setup slot structure. */
  226. new_slot = pciehp_slot_create(busnumber);
  227. dbg("%s: if, new_slot = %p bus %x dev %x fun %x\n",
  228. __FUNCTION__, new_slot, busnumber, device, function);
  229. if (new_slot == NULL)
  230. return(1);
  231. }
  232. new_slot->bus = (u8) busnumber;
  233. new_slot->device = (u8) device;
  234. new_slot->function = (u8) function;
  235. new_slot->is_a_board = 1;
  236. new_slot->switch_save = 0x10;
  237. /* In case of unsupported board */
  238. new_slot->status = DevError;
  239. new_slot->pci_dev = pci_find_slot(new_slot->bus,
  240. (new_slot->device << 3) | new_slot->function);
  241. dbg("new_slot->pci_dev = %p\n", new_slot->pci_dev);
  242. for (cloop = 0; cloop < 0x20; cloop++) {
  243. rc = pci_bus_read_config_dword(pci_bus,
  244. PCI_DEVFN(device, function),
  245. cloop << 2,
  246. (u32 *) &(new_slot->config_space [cloop]));
  247. /* dbg("new_slot->config_space[%x] = %x\n",
  248. cloop, new_slot->config_space[cloop]); */
  249. if (rc)
  250. return rc;
  251. }
  252. function++;
  253. stop_it = 0;
  254. /* this loop skips to the next present function
  255. * reading in Class Code and Header type.
  256. */
  257. while ((function < max_functions)&&(!stop_it)) {
  258. dbg("%s: In while loop \n", __FUNCTION__);
  259. rc = pci_bus_read_config_dword(pci_bus,
  260. PCI_DEVFN(device, function),
  261. PCI_VENDOR_ID, &ID);
  262. if (ID == 0xFFFFFFFF) { /* nothing there. */
  263. function++;
  264. dbg("Nothing there\n");
  265. } else { /* Something there */
  266. rc = pci_bus_read_config_byte(pci_bus,
  267. PCI_DEVFN(device, function),
  268. 0x0B, &class_code);
  269. if (rc)
  270. return rc;
  271. rc = pci_bus_read_config_byte(pci_bus,
  272. PCI_DEVFN(device, function),
  273. PCI_HEADER_TYPE, &header_type);
  274. if (rc)
  275. return rc;
  276. dbg("class_code = %x, header_type = %x\n", class_code, header_type);
  277. stop_it++;
  278. }
  279. }
  280. } while (function < max_functions);
  281. /* End of IF (device in slot?) */
  282. } else if (is_hot_plug) {
  283. /* Setup slot structure with entry for empty slot */
  284. new_slot = pciehp_slot_create(busnumber);
  285. if (new_slot == NULL) {
  286. return(1);
  287. }
  288. dbg("new_slot = %p, bus = %x, dev = %x, fun = %x\n", new_slot,
  289. new_slot->bus, new_slot->device, new_slot->function);
  290. new_slot->bus = (u8) busnumber;
  291. new_slot->device = (u8) device;
  292. new_slot->function = 0;
  293. new_slot->is_a_board = 0;
  294. new_slot->presence_save = 0;
  295. new_slot->switch_save = 0;
  296. }
  297. } /* End of FOR loop */
  298. dbg("%s: Exit\n", __FUNCTION__);
  299. return(0);
  300. }
  301. /*
  302. * pciehp_save_slot_config
  303. *
  304. * Saves configuration info for all PCI devices in a given slot
  305. * including subordinate busses.
  306. *
  307. * returns 0 if success
  308. */
  309. int pciehp_save_slot_config(struct controller *ctrl, struct pci_func * new_slot)
  310. {
  311. int rc;
  312. u8 class_code;
  313. u8 header_type;
  314. u32 ID;
  315. u8 secondary_bus;
  316. int sub_bus;
  317. int max_functions;
  318. int function;
  319. int cloop = 0;
  320. int stop_it;
  321. struct pci_bus lpci_bus, *pci_bus;
  322. memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
  323. pci_bus = &lpci_bus;
  324. pci_bus->number = new_slot->bus;
  325. ID = 0xFFFFFFFF;
  326. pci_bus_read_config_dword(pci_bus, PCI_DEVFN(new_slot->device, 0),
  327. PCI_VENDOR_ID, &ID);
  328. if (ID != 0xFFFFFFFF) { /* device in slot */
  329. pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, 0),
  330. 0x0B, &class_code);
  331. pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, 0),
  332. PCI_HEADER_TYPE, &header_type);
  333. if (header_type & 0x80) /* Multi-function device */
  334. max_functions = 8;
  335. else
  336. max_functions = 1;
  337. function = 0;
  338. do {
  339. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
  340. /* Recurse the subordinate bus */
  341. pci_bus_read_config_byte(pci_bus,
  342. PCI_DEVFN(new_slot->device, function),
  343. PCI_SECONDARY_BUS, &secondary_bus);
  344. sub_bus = (int) secondary_bus;
  345. /* Save the config headers for the secondary bus. */
  346. rc = pciehp_save_config(ctrl, sub_bus, 0, 0);
  347. if (rc)
  348. return rc;
  349. } /* End of IF */
  350. new_slot->status = 0;
  351. for (cloop = 0; cloop < 0x20; cloop++) {
  352. pci_bus_read_config_dword(pci_bus,
  353. PCI_DEVFN(new_slot->device, function),
  354. cloop << 2,
  355. (u32 *) &(new_slot->config_space [cloop]));
  356. }
  357. function++;
  358. stop_it = 0;
  359. /* this loop skips to the next present function
  360. * reading in the Class Code and the Header type.
  361. */
  362. while ((function < max_functions) && (!stop_it)) {
  363. pci_bus_read_config_dword(pci_bus,
  364. PCI_DEVFN(new_slot->device, function),
  365. PCI_VENDOR_ID, &ID);
  366. if (ID == 0xFFFFFFFF) { /* nothing there. */
  367. function++;
  368. } else { /* Something there */
  369. pci_bus_read_config_byte(pci_bus,
  370. PCI_DEVFN(new_slot->device, function),
  371. 0x0B, &class_code);
  372. pci_bus_read_config_byte(pci_bus,
  373. PCI_DEVFN(new_slot->device, function),
  374. PCI_HEADER_TYPE, &header_type);
  375. stop_it++;
  376. }
  377. }
  378. } while (function < max_functions);
  379. } /* End of IF (device in slot?) */
  380. else {
  381. return 2;
  382. }
  383. return 0;
  384. }
  385. /*
  386. * pciehp_save_used_resources
  387. *
  388. * Stores used resource information for existing boards. this is
  389. * for boards that were in the system when this driver was loaded.
  390. * this function is for hot plug ADD
  391. *
  392. * returns 0 if success
  393. * if disable == 1(DISABLE_CARD),
  394. * it loops for all functions of the slot and disables them.
  395. * else, it just get resources of the function and return.
  396. */
  397. int pciehp_save_used_resources(struct controller *ctrl, struct pci_func *func, int disable)
  398. {
  399. u8 cloop;
  400. u8 header_type;
  401. u8 secondary_bus;
  402. u8 temp_byte;
  403. u16 command;
  404. u16 save_command;
  405. u16 w_base, w_length;
  406. u32 temp_register;
  407. u32 save_base;
  408. u32 base, length;
  409. u64 base64 = 0;
  410. int index = 0;
  411. unsigned int devfn;
  412. struct pci_resource *mem_node = NULL;
  413. struct pci_resource *p_mem_node = NULL;
  414. struct pci_resource *t_mem_node;
  415. struct pci_resource *io_node;
  416. struct pci_resource *bus_node;
  417. struct pci_bus lpci_bus, *pci_bus;
  418. memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
  419. pci_bus = &lpci_bus;
  420. if (disable)
  421. func = pciehp_slot_find(func->bus, func->device, index++);
  422. while ((func != NULL) && func->is_a_board) {
  423. pci_bus->number = func->bus;
  424. devfn = PCI_DEVFN(func->device, func->function);
  425. /* Save the command register */
  426. pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
  427. if (disable) {
  428. /* disable card */
  429. command = 0x00;
  430. pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
  431. }
  432. /* Check for Bridge */
  433. pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
  434. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
  435. dbg("Save_used_res of PCI bridge b:d=0x%x:%x, sc=0x%x\n",
  436. func->bus, func->device, save_command);
  437. if (disable) {
  438. /* Clear Bridge Control Register */
  439. command = 0x00;
  440. pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
  441. }
  442. pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
  443. pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
  444. bus_node = kmalloc(sizeof(struct pci_resource),
  445. GFP_KERNEL);
  446. if (!bus_node)
  447. return -ENOMEM;
  448. bus_node->base = (ulong)secondary_bus;
  449. bus_node->length = (ulong)(temp_byte - secondary_bus + 1);
  450. bus_node->next = func->bus_head;
  451. func->bus_head = bus_node;
  452. /* Save IO base and Limit registers */
  453. pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &temp_byte);
  454. base = temp_byte;
  455. pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &temp_byte);
  456. length = temp_byte;
  457. if ((base <= length) && (!disable || (save_command & PCI_COMMAND_IO))) {
  458. io_node = kmalloc(sizeof(struct pci_resource),
  459. GFP_KERNEL);
  460. if (!io_node)
  461. return -ENOMEM;
  462. io_node->base = (ulong)(base & PCI_IO_RANGE_MASK) << 8;
  463. io_node->length = (ulong)(length - base + 0x10) << 8;
  464. io_node->next = func->io_head;
  465. func->io_head = io_node;
  466. }
  467. /* Save memory base and Limit registers */
  468. pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
  469. pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
  470. if ((w_base <= w_length) && (!disable || (save_command & PCI_COMMAND_MEMORY))) {
  471. mem_node = kmalloc(sizeof(struct pci_resource),
  472. GFP_KERNEL);
  473. if (!mem_node)
  474. return -ENOMEM;
  475. mem_node->base = (ulong)w_base << 16;
  476. mem_node->length = (ulong)(w_length - w_base + 0x10) << 16;
  477. mem_node->next = func->mem_head;
  478. func->mem_head = mem_node;
  479. }
  480. /* Save prefetchable memory base and Limit registers */
  481. pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
  482. pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
  483. if ((w_base <= w_length) && (!disable || (save_command & PCI_COMMAND_MEMORY))) {
  484. p_mem_node = kmalloc(sizeof(struct pci_resource),
  485. GFP_KERNEL);
  486. if (!p_mem_node)
  487. return -ENOMEM;
  488. p_mem_node->base = (ulong)w_base << 16;
  489. p_mem_node->length = (ulong)(w_length - w_base + 0x10) << 16;
  490. p_mem_node->next = func->p_mem_head;
  491. func->p_mem_head = p_mem_node;
  492. }
  493. } else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
  494. dbg("Save_used_res of PCI adapter b:d=0x%x:%x, sc=0x%x\n",
  495. func->bus, func->device, save_command);
  496. /* Figure out IO and memory base lengths */
  497. for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
  498. pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
  499. temp_register = 0xFFFFFFFF;
  500. pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
  501. pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
  502. if (!disable)
  503. pci_bus_write_config_dword(pci_bus, devfn, cloop, save_base);
  504. if (!temp_register)
  505. continue;
  506. base = temp_register;
  507. if ((base & PCI_BASE_ADDRESS_SPACE_IO) &&
  508. (!disable || (save_command & PCI_COMMAND_IO))) {
  509. /* IO base */
  510. /* set temp_register = amount of IO space requested */
  511. base = base & 0xFFFFFFFCL;
  512. base = (~base) + 1;
  513. io_node = kmalloc(sizeof (struct pci_resource),
  514. GFP_KERNEL);
  515. if (!io_node)
  516. return -ENOMEM;
  517. io_node->base = (ulong)save_base & PCI_BASE_ADDRESS_IO_MASK;
  518. io_node->length = (ulong)base;
  519. dbg("sur adapter: IO bar=0x%x(length=0x%x)\n",
  520. io_node->base, io_node->length);
  521. io_node->next = func->io_head;
  522. func->io_head = io_node;
  523. } else { /* map Memory */
  524. int prefetchable = 1;
  525. /* struct pci_resources **res_node; */
  526. char *res_type_str = "PMEM";
  527. u32 temp_register2;
  528. t_mem_node = kmalloc(sizeof (struct pci_resource),
  529. GFP_KERNEL);
  530. if (!t_mem_node)
  531. return -ENOMEM;
  532. if (!(base & PCI_BASE_ADDRESS_MEM_PREFETCH) &&
  533. (!disable || (save_command & PCI_COMMAND_MEMORY))) {
  534. prefetchable = 0;
  535. mem_node = t_mem_node;
  536. res_type_str++;
  537. } else
  538. p_mem_node = t_mem_node;
  539. base = base & 0xFFFFFFF0L;
  540. base = (~base) + 1;
  541. switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
  542. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  543. if (prefetchable) {
  544. p_mem_node->base = (ulong)save_base & PCI_BASE_ADDRESS_MEM_MASK;
  545. p_mem_node->length = (ulong)base;
  546. dbg("sur adapter: 32 %s bar=0x%x(length=0x%x)\n",
  547. res_type_str,
  548. p_mem_node->base,
  549. p_mem_node->length);
  550. p_mem_node->next = func->p_mem_head;
  551. func->p_mem_head = p_mem_node;
  552. } else {
  553. mem_node->base = (ulong)save_base & PCI_BASE_ADDRESS_MEM_MASK;
  554. mem_node->length = (ulong)base;
  555. dbg("sur adapter: 32 %s bar=0x%x(length=0x%x)\n",
  556. res_type_str,
  557. mem_node->base,
  558. mem_node->length);
  559. mem_node->next = func->mem_head;
  560. func->mem_head = mem_node;
  561. }
  562. break;
  563. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  564. pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
  565. base64 = temp_register2;
  566. base64 = (base64 << 32) | save_base;
  567. if (temp_register2) {
  568. dbg("sur adapter: 64 %s high dword of base64(0x%x:%x) masked to 0\n",
  569. res_type_str, temp_register2, (u32)base64);
  570. base64 &= 0x00000000FFFFFFFFL;
  571. }
  572. if (prefetchable) {
  573. p_mem_node->base = base64 & PCI_BASE_ADDRESS_MEM_MASK;
  574. p_mem_node->length = base;
  575. dbg("sur adapter: 64 %s base=0x%x(len=0x%x)\n",
  576. res_type_str,
  577. p_mem_node->base,
  578. p_mem_node->length);
  579. p_mem_node->next = func->p_mem_head;
  580. func->p_mem_head = p_mem_node;
  581. } else {
  582. mem_node->base = base64 & PCI_BASE_ADDRESS_MEM_MASK;
  583. mem_node->length = base;
  584. dbg("sur adapter: 64 %s base=0x%x(len=0x%x)\n",
  585. res_type_str,
  586. mem_node->base,
  587. mem_node->length);
  588. mem_node->next = func->mem_head;
  589. func->mem_head = mem_node;
  590. }
  591. cloop += 4;
  592. break;
  593. default:
  594. dbg("asur: reserved BAR type=0x%x\n",
  595. temp_register);
  596. break;
  597. }
  598. }
  599. } /* End of base register loop */
  600. } else { /* Some other unknown header type */
  601. dbg("Save_used_res of PCI unknown type b:d=0x%x:%x. skip.\n",
  602. func->bus, func->device);
  603. }
  604. /* find the next device in this slot */
  605. if (!disable)
  606. break;
  607. func = pciehp_slot_find(func->bus, func->device, index++);
  608. }
  609. return 0;
  610. }
  611. /**
  612. * kfree_resource_list: release memory of all list members
  613. * @res: resource list to free
  614. */
  615. static inline void
  616. return_resource_list(struct pci_resource **func, struct pci_resource **res)
  617. {
  618. struct pci_resource *node;
  619. struct pci_resource *t_node;
  620. node = *func;
  621. *func = NULL;
  622. while (node) {
  623. t_node = node->next;
  624. return_resource(res, node);
  625. node = t_node;
  626. }
  627. }
  628. /*
  629. * pciehp_return_board_resources
  630. *
  631. * this routine returns all resources allocated to a board to
  632. * the available pool.
  633. *
  634. * returns 0 if success
  635. */
  636. int pciehp_return_board_resources(struct pci_func * func,
  637. struct resource_lists * resources)
  638. {
  639. int rc;
  640. dbg("%s\n", __FUNCTION__);
  641. if (!func)
  642. return 1;
  643. return_resource_list(&(func->io_head),&(resources->io_head));
  644. return_resource_list(&(func->mem_head),&(resources->mem_head));
  645. return_resource_list(&(func->p_mem_head),&(resources->p_mem_head));
  646. return_resource_list(&(func->bus_head),&(resources->bus_head));
  647. rc = pciehp_resource_sort_and_combine(&(resources->mem_head));
  648. rc |= pciehp_resource_sort_and_combine(&(resources->p_mem_head));
  649. rc |= pciehp_resource_sort_and_combine(&(resources->io_head));
  650. rc |= pciehp_resource_sort_and_combine(&(resources->bus_head));
  651. return rc;
  652. }
  653. /**
  654. * kfree_resource_list: release memory of all list members
  655. * @res: resource list to free
  656. */
  657. static inline void
  658. kfree_resource_list(struct pci_resource **r)
  659. {
  660. struct pci_resource *res, *tres;
  661. res = *r;
  662. *r = NULL;
  663. while (res) {
  664. tres = res;
  665. res = res->next;
  666. kfree(tres);
  667. }
  668. }
  669. /**
  670. * pciehp_destroy_resource_list: put node back in the resource list
  671. * @resources: list to put nodes back
  672. */
  673. void pciehp_destroy_resource_list(struct resource_lists * resources)
  674. {
  675. kfree_resource_list(&(resources->io_head));
  676. kfree_resource_list(&(resources->mem_head));
  677. kfree_resource_list(&(resources->p_mem_head));
  678. kfree_resource_list(&(resources->bus_head));
  679. }
  680. /**
  681. * pciehp_destroy_board_resources: put node back in the resource list
  682. * @resources: list to put nodes back
  683. */
  684. void pciehp_destroy_board_resources(struct pci_func * func)
  685. {
  686. kfree_resource_list(&(func->io_head));
  687. kfree_resource_list(&(func->mem_head));
  688. kfree_resource_list(&(func->p_mem_head));
  689. kfree_resource_list(&(func->bus_head));
  690. }