cpqphp_pci.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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/workqueue.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/pci.h>
  36. #include "../pci.h"
  37. #include "cpqphp.h"
  38. #include "cpqphp_nvram.h"
  39. #include "../../../arch/i386/pci/pci.h" /* horrible hack showing how processor dependent we are... */
  40. u8 cpqhp_nic_irq;
  41. u8 cpqhp_disk_irq;
  42. static u16 unused_IRQ;
  43. /*
  44. * detect_HRT_floating_pointer
  45. *
  46. * find the Hot Plug Resource Table in the specified region of memory.
  47. *
  48. */
  49. static void __iomem *detect_HRT_floating_pointer(void __iomem *begin, void __iomem *end)
  50. {
  51. void __iomem *fp;
  52. void __iomem *endp;
  53. u8 temp1, temp2, temp3, temp4;
  54. int status = 0;
  55. endp = (end - sizeof(struct hrt) + 1);
  56. for (fp = begin; fp <= endp; fp += 16) {
  57. temp1 = readb(fp + SIG0);
  58. temp2 = readb(fp + SIG1);
  59. temp3 = readb(fp + SIG2);
  60. temp4 = readb(fp + SIG3);
  61. if (temp1 == '$' &&
  62. temp2 == 'H' &&
  63. temp3 == 'R' &&
  64. temp4 == 'T') {
  65. status = 1;
  66. break;
  67. }
  68. }
  69. if (!status)
  70. fp = NULL;
  71. dbg("Discovered Hotplug Resource Table at %p\n", fp);
  72. return fp;
  73. }
  74. int cpqhp_configure_device (struct controller* ctrl, struct pci_func* func)
  75. {
  76. unsigned char bus;
  77. struct pci_bus *child;
  78. int num;
  79. if (func->pci_dev == NULL)
  80. func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
  81. /* No pci device, we need to create it then */
  82. if (func->pci_dev == NULL) {
  83. dbg("INFO: pci_dev still null\n");
  84. num = pci_scan_slot(ctrl->pci_dev->bus, PCI_DEVFN(func->device, func->function));
  85. if (num)
  86. pci_bus_add_devices(ctrl->pci_dev->bus);
  87. func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
  88. if (func->pci_dev == NULL) {
  89. dbg("ERROR: pci_dev still null\n");
  90. return 0;
  91. }
  92. }
  93. if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  94. pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
  95. child = (struct pci_bus*) pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
  96. pci_do_scan_bus(child);
  97. }
  98. return 0;
  99. }
  100. int cpqhp_unconfigure_device(struct pci_func* func)
  101. {
  102. int j;
  103. dbg("%s: bus/dev/func = %x/%x/%x\n", __FUNCTION__, func->bus, func->device, func->function);
  104. for (j=0; j<8 ; j++) {
  105. struct pci_dev* temp = pci_find_slot(func->bus, PCI_DEVFN(func->device, j));
  106. if (temp)
  107. pci_remove_bus_device(temp);
  108. }
  109. return 0;
  110. }
  111. static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
  112. {
  113. u32 vendID = 0;
  114. if (pci_bus_read_config_dword (bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
  115. return -1;
  116. if (vendID == 0xffffffff)
  117. return -1;
  118. return pci_bus_read_config_dword (bus, devfn, offset, value);
  119. }
  120. /*
  121. * cpqhp_set_irq
  122. *
  123. * @bus_num: bus number of PCI device
  124. * @dev_num: device number of PCI device
  125. * @slot: pointer to u8 where slot number will be returned
  126. */
  127. int cpqhp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
  128. {
  129. int rc = 0;
  130. if (cpqhp_legacy_mode) {
  131. struct pci_dev *fakedev;
  132. struct pci_bus *fakebus;
  133. u16 temp_word;
  134. fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
  135. fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
  136. if (!fakedev || !fakebus) {
  137. kfree(fakedev);
  138. kfree(fakebus);
  139. return -ENOMEM;
  140. }
  141. fakedev->devfn = dev_num << 3;
  142. fakedev->bus = fakebus;
  143. fakebus->number = bus_num;
  144. dbg("%s: dev %d, bus %d, pin %d, num %d\n",
  145. __FUNCTION__, dev_num, bus_num, int_pin, irq_num);
  146. rc = pcibios_set_irq_routing(fakedev, int_pin - 0x0a, irq_num);
  147. kfree(fakedev);
  148. kfree(fakebus);
  149. dbg("%s: rc %d\n", __FUNCTION__, rc);
  150. if (!rc)
  151. return !rc;
  152. // set the Edge Level Control Register (ELCR)
  153. temp_word = inb(0x4d0);
  154. temp_word |= inb(0x4d1) << 8;
  155. temp_word |= 0x01 << irq_num;
  156. // This should only be for x86 as it sets the Edge Level Control Register
  157. outb((u8) (temp_word & 0xFF), 0x4d0);
  158. outb((u8) ((temp_word & 0xFF00) >> 8), 0x4d1);
  159. rc = 0;
  160. }
  161. return rc;
  162. }
  163. /*
  164. * WTF??? This function isn't in the code, yet a function calls it, but the
  165. * compiler optimizes it away? strange. Here as a placeholder to keep the
  166. * compiler happy.
  167. */
  168. static int PCI_ScanBusNonBridge (u8 bus, u8 device)
  169. {
  170. return 0;
  171. }
  172. static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 * dev_num)
  173. {
  174. u16 tdevice;
  175. u32 work;
  176. u8 tbus;
  177. ctrl->pci_bus->number = bus_num;
  178. for (tdevice = 0; tdevice < 0xFF; tdevice++) {
  179. //Scan for access first
  180. if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
  181. continue;
  182. dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
  183. //Yep we got one. Not a bridge ?
  184. if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
  185. *dev_num = tdevice;
  186. dbg("found it !\n");
  187. return 0;
  188. }
  189. }
  190. for (tdevice = 0; tdevice < 0xFF; tdevice++) {
  191. //Scan for access first
  192. if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
  193. continue;
  194. dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
  195. //Yep we got one. bridge ?
  196. if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
  197. pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
  198. dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
  199. if (PCI_ScanBusNonBridge(tbus, tdevice) == 0)
  200. return 0;
  201. }
  202. }
  203. return -1;
  204. }
  205. static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
  206. {
  207. struct irq_routing_table *PCIIRQRoutingInfoLength;
  208. long len;
  209. long loop;
  210. u32 work;
  211. u8 tbus, tdevice, tslot;
  212. PCIIRQRoutingInfoLength = pcibios_get_irq_routing_table();
  213. if (!PCIIRQRoutingInfoLength)
  214. return -1;
  215. len = (PCIIRQRoutingInfoLength->size -
  216. sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
  217. // Make sure I got at least one entry
  218. if (len == 0) {
  219. kfree(PCIIRQRoutingInfoLength );
  220. return -1;
  221. }
  222. for (loop = 0; loop < len; ++loop) {
  223. tbus = PCIIRQRoutingInfoLength->slots[loop].bus;
  224. tdevice = PCIIRQRoutingInfoLength->slots[loop].devfn;
  225. tslot = PCIIRQRoutingInfoLength->slots[loop].slot;
  226. if (tslot == slot) {
  227. *bus_num = tbus;
  228. *dev_num = tdevice;
  229. ctrl->pci_bus->number = tbus;
  230. pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
  231. if (!nobridge || (work == 0xffffffff)) {
  232. kfree(PCIIRQRoutingInfoLength );
  233. return 0;
  234. }
  235. dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
  236. pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
  237. dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
  238. if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
  239. pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
  240. dbg("Scan bus for Non Bridge: bus %d\n", tbus);
  241. if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
  242. *bus_num = tbus;
  243. kfree(PCIIRQRoutingInfoLength );
  244. return 0;
  245. }
  246. } else {
  247. kfree(PCIIRQRoutingInfoLength );
  248. return 0;
  249. }
  250. }
  251. }
  252. kfree(PCIIRQRoutingInfoLength );
  253. return -1;
  254. }
  255. int cpqhp_get_bus_dev (struct controller *ctrl, u8 * bus_num, u8 * dev_num, u8 slot)
  256. {
  257. return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0); //plain (bridges allowed)
  258. }
  259. /* More PCI configuration routines; this time centered around hotplug controller */
  260. /*
  261. * cpqhp_save_config
  262. *
  263. * Reads configuration for all slots in a PCI bus and saves info.
  264. *
  265. * Note: For non-hot plug busses, the slot # saved is the device #
  266. *
  267. * returns 0 if success
  268. */
  269. int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
  270. {
  271. long rc;
  272. u8 class_code;
  273. u8 header_type;
  274. u32 ID;
  275. u8 secondary_bus;
  276. struct pci_func *new_slot;
  277. int sub_bus;
  278. int FirstSupported;
  279. int LastSupported;
  280. int max_functions;
  281. int function;
  282. u8 DevError;
  283. int device = 0;
  284. int cloop = 0;
  285. int stop_it;
  286. int index;
  287. // Decide which slots are supported
  288. if (is_hot_plug) {
  289. //*********************************
  290. // is_hot_plug is the slot mask
  291. //*********************************
  292. FirstSupported = is_hot_plug >> 4;
  293. LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
  294. } else {
  295. FirstSupported = 0;
  296. LastSupported = 0x1F;
  297. }
  298. // Save PCI configuration space for all devices in supported slots
  299. ctrl->pci_bus->number = busnumber;
  300. for (device = FirstSupported; device <= LastSupported; device++) {
  301. ID = 0xFFFFFFFF;
  302. rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
  303. if (ID != 0xFFFFFFFF) { // device in slot
  304. rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
  305. if (rc)
  306. return rc;
  307. rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
  308. if (rc)
  309. return rc;
  310. // If multi-function device, set max_functions to 8
  311. if (header_type & 0x80)
  312. max_functions = 8;
  313. else
  314. max_functions = 1;
  315. function = 0;
  316. do {
  317. DevError = 0;
  318. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // P-P Bridge
  319. // Recurse the subordinate bus
  320. // get the subordinate bus number
  321. rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
  322. if (rc) {
  323. return rc;
  324. } else {
  325. sub_bus = (int) secondary_bus;
  326. // Save secondary bus cfg spc
  327. // with this recursive call.
  328. rc = cpqhp_save_config(ctrl, sub_bus, 0);
  329. if (rc)
  330. return rc;
  331. ctrl->pci_bus->number = busnumber;
  332. }
  333. }
  334. index = 0;
  335. new_slot = cpqhp_slot_find(busnumber, device, index++);
  336. while (new_slot &&
  337. (new_slot->function != (u8) function))
  338. new_slot = cpqhp_slot_find(busnumber, device, index++);
  339. if (!new_slot) {
  340. // Setup slot structure.
  341. new_slot = cpqhp_slot_create(busnumber);
  342. if (new_slot == NULL)
  343. return(1);
  344. }
  345. new_slot->bus = (u8) busnumber;
  346. new_slot->device = (u8) device;
  347. new_slot->function = (u8) function;
  348. new_slot->is_a_board = 1;
  349. new_slot->switch_save = 0x10;
  350. // In case of unsupported board
  351. new_slot->status = DevError;
  352. new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
  353. for (cloop = 0; cloop < 0x20; cloop++) {
  354. rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
  355. if (rc)
  356. return rc;
  357. }
  358. function++;
  359. stop_it = 0;
  360. // this loop skips to the next present function
  361. // reading in Class Code and Header type.
  362. while ((function < max_functions)&&(!stop_it)) {
  363. rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
  364. if (ID == 0xFFFFFFFF) { // nothing there.
  365. function++;
  366. } else { // Something there
  367. rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
  368. if (rc)
  369. return rc;
  370. rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
  371. if (rc)
  372. return rc;
  373. stop_it++;
  374. }
  375. }
  376. } while (function < max_functions);
  377. } // End of IF (device in slot?)
  378. else if (is_hot_plug) {
  379. // Setup slot structure with entry for empty slot
  380. new_slot = cpqhp_slot_create(busnumber);
  381. if (new_slot == NULL) {
  382. return(1);
  383. }
  384. new_slot->bus = (u8) busnumber;
  385. new_slot->device = (u8) device;
  386. new_slot->function = 0;
  387. new_slot->is_a_board = 0;
  388. new_slot->presence_save = 0;
  389. new_slot->switch_save = 0;
  390. }
  391. } // End of FOR loop
  392. return(0);
  393. }
  394. /*
  395. * cpqhp_save_slot_config
  396. *
  397. * Saves configuration info for all PCI devices in a given slot
  398. * including subordinate busses.
  399. *
  400. * returns 0 if success
  401. */
  402. int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
  403. {
  404. long rc;
  405. u8 class_code;
  406. u8 header_type;
  407. u32 ID;
  408. u8 secondary_bus;
  409. int sub_bus;
  410. int max_functions;
  411. int function;
  412. int cloop = 0;
  413. int stop_it;
  414. ID = 0xFFFFFFFF;
  415. ctrl->pci_bus->number = new_slot->bus;
  416. pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
  417. if (ID != 0xFFFFFFFF) { // device in slot
  418. pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
  419. pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
  420. if (header_type & 0x80) // Multi-function device
  421. max_functions = 8;
  422. else
  423. max_functions = 1;
  424. function = 0;
  425. do {
  426. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
  427. // Recurse the subordinate bus
  428. pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
  429. sub_bus = (int) secondary_bus;
  430. // Save the config headers for the secondary bus.
  431. rc = cpqhp_save_config(ctrl, sub_bus, 0);
  432. if (rc)
  433. return(rc);
  434. ctrl->pci_bus->number = new_slot->bus;
  435. } // End of IF
  436. new_slot->status = 0;
  437. for (cloop = 0; cloop < 0x20; cloop++) {
  438. pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
  439. }
  440. function++;
  441. stop_it = 0;
  442. // this loop skips to the next present function
  443. // reading in the Class Code and the Header type.
  444. while ((function < max_functions) && (!stop_it)) {
  445. pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
  446. if (ID == 0xFFFFFFFF) { // nothing there.
  447. function++;
  448. } else { // Something there
  449. pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
  450. pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
  451. stop_it++;
  452. }
  453. }
  454. } while (function < max_functions);
  455. } // End of IF (device in slot?)
  456. else {
  457. return 2;
  458. }
  459. return 0;
  460. }
  461. /*
  462. * cpqhp_save_base_addr_length
  463. *
  464. * Saves the length of all base address registers for the
  465. * specified slot. this is for hot plug REPLACE
  466. *
  467. * returns 0 if success
  468. */
  469. int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func * func)
  470. {
  471. u8 cloop;
  472. u8 header_type;
  473. u8 secondary_bus;
  474. u8 type;
  475. int sub_bus;
  476. u32 temp_register;
  477. u32 base;
  478. u32 rc;
  479. struct pci_func *next;
  480. int index = 0;
  481. struct pci_bus *pci_bus = ctrl->pci_bus;
  482. unsigned int devfn;
  483. func = cpqhp_slot_find(func->bus, func->device, index++);
  484. while (func != NULL) {
  485. pci_bus->number = func->bus;
  486. devfn = PCI_DEVFN(func->device, func->function);
  487. // Check for Bridge
  488. pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
  489. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  490. // PCI-PCI Bridge
  491. pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
  492. sub_bus = (int) secondary_bus;
  493. next = cpqhp_slot_list[sub_bus];
  494. while (next != NULL) {
  495. rc = cpqhp_save_base_addr_length(ctrl, next);
  496. if (rc)
  497. return rc;
  498. next = next->next;
  499. }
  500. pci_bus->number = func->bus;
  501. //FIXME: this loop is duplicated in the non-bridge case. The two could be rolled together
  502. // Figure out IO and memory base lengths
  503. for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
  504. temp_register = 0xFFFFFFFF;
  505. pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
  506. pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
  507. if (base) { // If this register is implemented
  508. if (base & 0x01L) {
  509. // IO base
  510. // set base = amount of IO space requested
  511. base = base & 0xFFFFFFFE;
  512. base = (~base) + 1;
  513. type = 1;
  514. } else {
  515. // memory base
  516. base = base & 0xFFFFFFF0;
  517. base = (~base) + 1;
  518. type = 0;
  519. }
  520. } else {
  521. base = 0x0L;
  522. type = 0;
  523. }
  524. // Save information in slot structure
  525. func->base_length[(cloop - 0x10) >> 2] =
  526. base;
  527. func->base_type[(cloop - 0x10) >> 2] = type;
  528. } // End of base register loop
  529. } else if ((header_type & 0x7F) == 0x00) { // PCI-PCI Bridge
  530. // Figure out IO and memory base lengths
  531. for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
  532. temp_register = 0xFFFFFFFF;
  533. pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
  534. pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
  535. if (base) { // If this register is implemented
  536. if (base & 0x01L) {
  537. // IO base
  538. // base = amount of IO space requested
  539. base = base & 0xFFFFFFFE;
  540. base = (~base) + 1;
  541. type = 1;
  542. } else {
  543. // memory base
  544. // base = amount of memory space requested
  545. base = base & 0xFFFFFFF0;
  546. base = (~base) + 1;
  547. type = 0;
  548. }
  549. } else {
  550. base = 0x0L;
  551. type = 0;
  552. }
  553. // Save information in slot structure
  554. func->base_length[(cloop - 0x10) >> 2] = base;
  555. func->base_type[(cloop - 0x10) >> 2] = type;
  556. } // End of base register loop
  557. } else { // Some other unknown header type
  558. }
  559. // find the next device in this slot
  560. func = cpqhp_slot_find(func->bus, func->device, index++);
  561. }
  562. return(0);
  563. }
  564. /*
  565. * cpqhp_save_used_resources
  566. *
  567. * Stores used resource information for existing boards. this is
  568. * for boards that were in the system when this driver was loaded.
  569. * this function is for hot plug ADD
  570. *
  571. * returns 0 if success
  572. */
  573. int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func * func)
  574. {
  575. u8 cloop;
  576. u8 header_type;
  577. u8 secondary_bus;
  578. u8 temp_byte;
  579. u8 b_base;
  580. u8 b_length;
  581. u16 command;
  582. u16 save_command;
  583. u16 w_base;
  584. u16 w_length;
  585. u32 temp_register;
  586. u32 save_base;
  587. u32 base;
  588. int index = 0;
  589. struct pci_resource *mem_node;
  590. struct pci_resource *p_mem_node;
  591. struct pci_resource *io_node;
  592. struct pci_resource *bus_node;
  593. struct pci_bus *pci_bus = ctrl->pci_bus;
  594. unsigned int devfn;
  595. func = cpqhp_slot_find(func->bus, func->device, index++);
  596. while ((func != NULL) && func->is_a_board) {
  597. pci_bus->number = func->bus;
  598. devfn = PCI_DEVFN(func->device, func->function);
  599. // Save the command register
  600. pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
  601. // disable card
  602. command = 0x00;
  603. pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
  604. // Check for Bridge
  605. pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
  606. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
  607. // Clear Bridge Control Register
  608. command = 0x00;
  609. pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
  610. pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
  611. pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
  612. bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
  613. if (!bus_node)
  614. return -ENOMEM;
  615. bus_node->base = secondary_bus;
  616. bus_node->length = temp_byte - secondary_bus + 1;
  617. bus_node->next = func->bus_head;
  618. func->bus_head = bus_node;
  619. // Save IO base and Limit registers
  620. pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
  621. pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
  622. if ((b_base <= b_length) && (save_command & 0x01)) {
  623. io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
  624. if (!io_node)
  625. return -ENOMEM;
  626. io_node->base = (b_base & 0xF0) << 8;
  627. io_node->length = (b_length - b_base + 0x10) << 8;
  628. io_node->next = func->io_head;
  629. func->io_head = io_node;
  630. }
  631. // Save memory base and Limit registers
  632. pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
  633. pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
  634. if ((w_base <= w_length) && (save_command & 0x02)) {
  635. mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
  636. if (!mem_node)
  637. return -ENOMEM;
  638. mem_node->base = w_base << 16;
  639. mem_node->length = (w_length - w_base + 0x10) << 16;
  640. mem_node->next = func->mem_head;
  641. func->mem_head = mem_node;
  642. }
  643. // Save prefetchable memory base and Limit registers
  644. pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
  645. pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
  646. if ((w_base <= w_length) && (save_command & 0x02)) {
  647. p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
  648. if (!p_mem_node)
  649. return -ENOMEM;
  650. p_mem_node->base = w_base << 16;
  651. p_mem_node->length = (w_length - w_base + 0x10) << 16;
  652. p_mem_node->next = func->p_mem_head;
  653. func->p_mem_head = p_mem_node;
  654. }
  655. // Figure out IO and memory base lengths
  656. for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
  657. pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
  658. temp_register = 0xFFFFFFFF;
  659. pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
  660. pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
  661. temp_register = base;
  662. if (base) { // If this register is implemented
  663. if (((base & 0x03L) == 0x01)
  664. && (save_command & 0x01)) {
  665. // IO base
  666. // set temp_register = amount of IO space requested
  667. temp_register = base & 0xFFFFFFFE;
  668. temp_register = (~temp_register) + 1;
  669. io_node = kmalloc(sizeof(*io_node),
  670. GFP_KERNEL);
  671. if (!io_node)
  672. return -ENOMEM;
  673. io_node->base =
  674. save_base & (~0x03L);
  675. io_node->length = temp_register;
  676. io_node->next = func->io_head;
  677. func->io_head = io_node;
  678. } else
  679. if (((base & 0x0BL) == 0x08)
  680. && (save_command & 0x02)) {
  681. // prefetchable memory base
  682. temp_register = base & 0xFFFFFFF0;
  683. temp_register = (~temp_register) + 1;
  684. p_mem_node = kmalloc(sizeof(*p_mem_node),
  685. GFP_KERNEL);
  686. if (!p_mem_node)
  687. return -ENOMEM;
  688. p_mem_node->base = save_base & (~0x0FL);
  689. p_mem_node->length = temp_register;
  690. p_mem_node->next = func->p_mem_head;
  691. func->p_mem_head = p_mem_node;
  692. } else
  693. if (((base & 0x0BL) == 0x00)
  694. && (save_command & 0x02)) {
  695. // prefetchable memory base
  696. temp_register = base & 0xFFFFFFF0;
  697. temp_register = (~temp_register) + 1;
  698. mem_node = kmalloc(sizeof(*mem_node),
  699. GFP_KERNEL);
  700. if (!mem_node)
  701. return -ENOMEM;
  702. mem_node->base = save_base & (~0x0FL);
  703. mem_node->length = temp_register;
  704. mem_node->next = func->mem_head;
  705. func->mem_head = mem_node;
  706. } else
  707. return(1);
  708. }
  709. } // End of base register loop
  710. } else if ((header_type & 0x7F) == 0x00) { // Standard header
  711. // Figure out IO and memory base lengths
  712. for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
  713. pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
  714. temp_register = 0xFFFFFFFF;
  715. pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
  716. pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
  717. temp_register = base;
  718. if (base) { // If this register is implemented
  719. if (((base & 0x03L) == 0x01)
  720. && (save_command & 0x01)) {
  721. // IO base
  722. // set temp_register = amount of IO space requested
  723. temp_register = base & 0xFFFFFFFE;
  724. temp_register = (~temp_register) + 1;
  725. io_node = kmalloc(sizeof(*io_node),
  726. GFP_KERNEL);
  727. if (!io_node)
  728. return -ENOMEM;
  729. io_node->base = save_base & (~0x01L);
  730. io_node->length = temp_register;
  731. io_node->next = func->io_head;
  732. func->io_head = io_node;
  733. } else
  734. if (((base & 0x0BL) == 0x08)
  735. && (save_command & 0x02)) {
  736. // prefetchable memory base
  737. temp_register = base & 0xFFFFFFF0;
  738. temp_register = (~temp_register) + 1;
  739. p_mem_node = kmalloc(sizeof(*p_mem_node),
  740. GFP_KERNEL);
  741. if (!p_mem_node)
  742. return -ENOMEM;
  743. p_mem_node->base = save_base & (~0x0FL);
  744. p_mem_node->length = temp_register;
  745. p_mem_node->next = func->p_mem_head;
  746. func->p_mem_head = p_mem_node;
  747. } else
  748. if (((base & 0x0BL) == 0x00)
  749. && (save_command & 0x02)) {
  750. // prefetchable memory base
  751. temp_register = base & 0xFFFFFFF0;
  752. temp_register = (~temp_register) + 1;
  753. mem_node = kmalloc(sizeof(*mem_node),
  754. GFP_KERNEL);
  755. if (!mem_node)
  756. return -ENOMEM;
  757. mem_node->base = save_base & (~0x0FL);
  758. mem_node->length = temp_register;
  759. mem_node->next = func->mem_head;
  760. func->mem_head = mem_node;
  761. } else
  762. return(1);
  763. }
  764. } // End of base register loop
  765. } else { // Some other unknown header type
  766. }
  767. // find the next device in this slot
  768. func = cpqhp_slot_find(func->bus, func->device, index++);
  769. }
  770. return(0);
  771. }
  772. /*
  773. * cpqhp_configure_board
  774. *
  775. * Copies saved configuration information to one slot.
  776. * this is called recursively for bridge devices.
  777. * this is for hot plug REPLACE!
  778. *
  779. * returns 0 if success
  780. */
  781. int cpqhp_configure_board(struct controller *ctrl, struct pci_func * func)
  782. {
  783. int cloop;
  784. u8 header_type;
  785. u8 secondary_bus;
  786. int sub_bus;
  787. struct pci_func *next;
  788. u32 temp;
  789. u32 rc;
  790. int index = 0;
  791. struct pci_bus *pci_bus = ctrl->pci_bus;
  792. unsigned int devfn;
  793. func = cpqhp_slot_find(func->bus, func->device, index++);
  794. while (func != NULL) {
  795. pci_bus->number = func->bus;
  796. devfn = PCI_DEVFN(func->device, func->function);
  797. // Start at the top of config space so that the control
  798. // registers are programmed last
  799. for (cloop = 0x3C; cloop > 0; cloop -= 4) {
  800. pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
  801. }
  802. pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
  803. // If this is a bridge device, restore subordinate devices
  804. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
  805. pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
  806. sub_bus = (int) secondary_bus;
  807. next = cpqhp_slot_list[sub_bus];
  808. while (next != NULL) {
  809. rc = cpqhp_configure_board(ctrl, next);
  810. if (rc)
  811. return rc;
  812. next = next->next;
  813. }
  814. } else {
  815. // Check all the base Address Registers to make sure
  816. // they are the same. If not, the board is different.
  817. for (cloop = 16; cloop < 40; cloop += 4) {
  818. pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
  819. if (temp != func->config_space[cloop >> 2]) {
  820. dbg("Config space compare failure!!! offset = %x\n", cloop);
  821. dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
  822. dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
  823. return 1;
  824. }
  825. }
  826. }
  827. func->configured = 1;
  828. func = cpqhp_slot_find(func->bus, func->device, index++);
  829. }
  830. return 0;
  831. }
  832. /*
  833. * cpqhp_valid_replace
  834. *
  835. * this function checks to see if a board is the same as the
  836. * one it is replacing. this check will detect if the device's
  837. * vendor or device id's are the same
  838. *
  839. * returns 0 if the board is the same nonzero otherwise
  840. */
  841. int cpqhp_valid_replace(struct controller *ctrl, struct pci_func * func)
  842. {
  843. u8 cloop;
  844. u8 header_type;
  845. u8 secondary_bus;
  846. u8 type;
  847. u32 temp_register = 0;
  848. u32 base;
  849. u32 rc;
  850. struct pci_func *next;
  851. int index = 0;
  852. struct pci_bus *pci_bus = ctrl->pci_bus;
  853. unsigned int devfn;
  854. if (!func->is_a_board)
  855. return(ADD_NOT_SUPPORTED);
  856. func = cpqhp_slot_find(func->bus, func->device, index++);
  857. while (func != NULL) {
  858. pci_bus->number = func->bus;
  859. devfn = PCI_DEVFN(func->device, func->function);
  860. pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
  861. // No adapter present
  862. if (temp_register == 0xFFFFFFFF)
  863. return(NO_ADAPTER_PRESENT);
  864. if (temp_register != func->config_space[0])
  865. return(ADAPTER_NOT_SAME);
  866. // Check for same revision number and class code
  867. pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
  868. // Adapter not the same
  869. if (temp_register != func->config_space[0x08 >> 2])
  870. return(ADAPTER_NOT_SAME);
  871. // Check for Bridge
  872. pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
  873. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { // PCI-PCI Bridge
  874. // In order to continue checking, we must program the
  875. // bus registers in the bridge to respond to accesses
  876. // for it's subordinate bus(es)
  877. temp_register = func->config_space[0x18 >> 2];
  878. pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
  879. secondary_bus = (temp_register >> 8) & 0xFF;
  880. next = cpqhp_slot_list[secondary_bus];
  881. while (next != NULL) {
  882. rc = cpqhp_valid_replace(ctrl, next);
  883. if (rc)
  884. return rc;
  885. next = next->next;
  886. }
  887. }
  888. // Check to see if it is a standard config header
  889. else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
  890. // Check subsystem vendor and ID
  891. pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
  892. if (temp_register != func->config_space[0x2C >> 2]) {
  893. // If it's a SMART-2 and the register isn't filled
  894. // in, ignore the difference because
  895. // they just have an old rev of the firmware
  896. if (!((func->config_space[0] == 0xAE100E11)
  897. && (temp_register == 0x00L)))
  898. return(ADAPTER_NOT_SAME);
  899. }
  900. // Figure out IO and memory base lengths
  901. for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
  902. temp_register = 0xFFFFFFFF;
  903. pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
  904. pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
  905. if (base) { // If this register is implemented
  906. if (base & 0x01L) {
  907. // IO base
  908. // set base = amount of IO space requested
  909. base = base & 0xFFFFFFFE;
  910. base = (~base) + 1;
  911. type = 1;
  912. } else {
  913. // memory base
  914. base = base & 0xFFFFFFF0;
  915. base = (~base) + 1;
  916. type = 0;
  917. }
  918. } else {
  919. base = 0x0L;
  920. type = 0;
  921. }
  922. // Check information in slot structure
  923. if (func->base_length[(cloop - 0x10) >> 2] != base)
  924. return(ADAPTER_NOT_SAME);
  925. if (func->base_type[(cloop - 0x10) >> 2] != type)
  926. return(ADAPTER_NOT_SAME);
  927. } // End of base register loop
  928. } // End of (type 0 config space) else
  929. else {
  930. // this is not a type 0 or 1 config space header so
  931. // we don't know how to do it
  932. return(DEVICE_TYPE_NOT_SUPPORTED);
  933. }
  934. // Get the next function
  935. func = cpqhp_slot_find(func->bus, func->device, index++);
  936. }
  937. return 0;
  938. }
  939. /*
  940. * cpqhp_find_available_resources
  941. *
  942. * Finds available memory, IO, and IRQ resources for programming
  943. * devices which may be added to the system
  944. * this function is for hot plug ADD!
  945. *
  946. * returns 0 if success
  947. */
  948. int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
  949. {
  950. u8 temp;
  951. u8 populated_slot;
  952. u8 bridged_slot;
  953. void __iomem *one_slot;
  954. void __iomem *rom_resource_table;
  955. struct pci_func *func = NULL;
  956. int i = 10, index;
  957. u32 temp_dword, rc;
  958. struct pci_resource *mem_node;
  959. struct pci_resource *p_mem_node;
  960. struct pci_resource *io_node;
  961. struct pci_resource *bus_node;
  962. rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
  963. dbg("rom_resource_table = %p\n", rom_resource_table);
  964. if (rom_resource_table == NULL) {
  965. return -ENODEV;
  966. }
  967. // Sum all resources and setup resource maps
  968. unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
  969. dbg("unused_IRQ = %x\n", unused_IRQ);
  970. temp = 0;
  971. while (unused_IRQ) {
  972. if (unused_IRQ & 1) {
  973. cpqhp_disk_irq = temp;
  974. break;
  975. }
  976. unused_IRQ = unused_IRQ >> 1;
  977. temp++;
  978. }
  979. dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
  980. unused_IRQ = unused_IRQ >> 1;
  981. temp++;
  982. while (unused_IRQ) {
  983. if (unused_IRQ & 1) {
  984. cpqhp_nic_irq = temp;
  985. break;
  986. }
  987. unused_IRQ = unused_IRQ >> 1;
  988. temp++;
  989. }
  990. dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
  991. unused_IRQ = readl(rom_resource_table + PCIIRQ);
  992. temp = 0;
  993. if (!cpqhp_nic_irq) {
  994. cpqhp_nic_irq = ctrl->cfgspc_irq;
  995. }
  996. if (!cpqhp_disk_irq) {
  997. cpqhp_disk_irq = ctrl->cfgspc_irq;
  998. }
  999. dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
  1000. rc = compaq_nvram_load(rom_start, ctrl);
  1001. if (rc)
  1002. return rc;
  1003. one_slot = rom_resource_table + sizeof (struct hrt);
  1004. i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
  1005. dbg("number_of_entries = %d\n", i);
  1006. if (!readb(one_slot + SECONDARY_BUS))
  1007. return 1;
  1008. dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
  1009. while (i && readb(one_slot + SECONDARY_BUS)) {
  1010. u8 dev_func = readb(one_slot + DEV_FUNC);
  1011. u8 primary_bus = readb(one_slot + PRIMARY_BUS);
  1012. u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
  1013. u8 max_bus = readb(one_slot + MAX_BUS);
  1014. u16 io_base = readw(one_slot + IO_BASE);
  1015. u16 io_length = readw(one_slot + IO_LENGTH);
  1016. u16 mem_base = readw(one_slot + MEM_BASE);
  1017. u16 mem_length = readw(one_slot + MEM_LENGTH);
  1018. u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
  1019. u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
  1020. dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
  1021. dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
  1022. primary_bus, secondary_bus, max_bus);
  1023. // If this entry isn't for our controller's bus, ignore it
  1024. if (primary_bus != ctrl->bus) {
  1025. i--;
  1026. one_slot += sizeof (struct slot_rt);
  1027. continue;
  1028. }
  1029. // find out if this entry is for an occupied slot
  1030. ctrl->pci_bus->number = primary_bus;
  1031. pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
  1032. dbg("temp_D_word = %x\n", temp_dword);
  1033. if (temp_dword != 0xFFFFFFFF) {
  1034. index = 0;
  1035. func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
  1036. while (func && (func->function != (dev_func & 0x07))) {
  1037. dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
  1038. func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
  1039. }
  1040. // If we can't find a match, skip this table entry
  1041. if (!func) {
  1042. i--;
  1043. one_slot += sizeof (struct slot_rt);
  1044. continue;
  1045. }
  1046. // this may not work and shouldn't be used
  1047. if (secondary_bus != primary_bus)
  1048. bridged_slot = 1;
  1049. else
  1050. bridged_slot = 0;
  1051. populated_slot = 1;
  1052. } else {
  1053. populated_slot = 0;
  1054. bridged_slot = 0;
  1055. }
  1056. // If we've got a valid IO base, use it
  1057. temp_dword = io_base + io_length;
  1058. if ((io_base) && (temp_dword < 0x10000)) {
  1059. io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
  1060. if (!io_node)
  1061. return -ENOMEM;
  1062. io_node->base = io_base;
  1063. io_node->length = io_length;
  1064. dbg("found io_node(base, length) = %x, %x\n",
  1065. io_node->base, io_node->length);
  1066. dbg("populated slot =%d \n", populated_slot);
  1067. if (!populated_slot) {
  1068. io_node->next = ctrl->io_head;
  1069. ctrl->io_head = io_node;
  1070. } else {
  1071. io_node->next = func->io_head;
  1072. func->io_head = io_node;
  1073. }
  1074. }
  1075. // If we've got a valid memory base, use it
  1076. temp_dword = mem_base + mem_length;
  1077. if ((mem_base) && (temp_dword < 0x10000)) {
  1078. mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
  1079. if (!mem_node)
  1080. return -ENOMEM;
  1081. mem_node->base = mem_base << 16;
  1082. mem_node->length = mem_length << 16;
  1083. dbg("found mem_node(base, length) = %x, %x\n",
  1084. mem_node->base, mem_node->length);
  1085. dbg("populated slot =%d \n", populated_slot);
  1086. if (!populated_slot) {
  1087. mem_node->next = ctrl->mem_head;
  1088. ctrl->mem_head = mem_node;
  1089. } else {
  1090. mem_node->next = func->mem_head;
  1091. func->mem_head = mem_node;
  1092. }
  1093. }
  1094. // If we've got a valid prefetchable memory base, and
  1095. // the base + length isn't greater than 0xFFFF
  1096. temp_dword = pre_mem_base + pre_mem_length;
  1097. if ((pre_mem_base) && (temp_dword < 0x10000)) {
  1098. p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
  1099. if (!p_mem_node)
  1100. return -ENOMEM;
  1101. p_mem_node->base = pre_mem_base << 16;
  1102. p_mem_node->length = pre_mem_length << 16;
  1103. dbg("found p_mem_node(base, length) = %x, %x\n",
  1104. p_mem_node->base, p_mem_node->length);
  1105. dbg("populated slot =%d \n", populated_slot);
  1106. if (!populated_slot) {
  1107. p_mem_node->next = ctrl->p_mem_head;
  1108. ctrl->p_mem_head = p_mem_node;
  1109. } else {
  1110. p_mem_node->next = func->p_mem_head;
  1111. func->p_mem_head = p_mem_node;
  1112. }
  1113. }
  1114. // If we've got a valid bus number, use it
  1115. // The second condition is to ignore bus numbers on
  1116. // populated slots that don't have PCI-PCI bridges
  1117. if (secondary_bus && (secondary_bus != primary_bus)) {
  1118. bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
  1119. if (!bus_node)
  1120. return -ENOMEM;
  1121. bus_node->base = secondary_bus;
  1122. bus_node->length = max_bus - secondary_bus + 1;
  1123. dbg("found bus_node(base, length) = %x, %x\n",
  1124. bus_node->base, bus_node->length);
  1125. dbg("populated slot =%d \n", populated_slot);
  1126. if (!populated_slot) {
  1127. bus_node->next = ctrl->bus_head;
  1128. ctrl->bus_head = bus_node;
  1129. } else {
  1130. bus_node->next = func->bus_head;
  1131. func->bus_head = bus_node;
  1132. }
  1133. }
  1134. i--;
  1135. one_slot += sizeof (struct slot_rt);
  1136. }
  1137. // If all of the following fail, we don't have any resources for
  1138. // hot plug add
  1139. rc = 1;
  1140. rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1141. rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1142. rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1143. rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1144. return rc;
  1145. }
  1146. /*
  1147. * cpqhp_return_board_resources
  1148. *
  1149. * this routine returns all resources allocated to a board to
  1150. * the available pool.
  1151. *
  1152. * returns 0 if success
  1153. */
  1154. int cpqhp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
  1155. {
  1156. int rc = 0;
  1157. struct pci_resource *node;
  1158. struct pci_resource *t_node;
  1159. dbg("%s\n", __FUNCTION__);
  1160. if (!func)
  1161. return 1;
  1162. node = func->io_head;
  1163. func->io_head = NULL;
  1164. while (node) {
  1165. t_node = node->next;
  1166. return_resource(&(resources->io_head), node);
  1167. node = t_node;
  1168. }
  1169. node = func->mem_head;
  1170. func->mem_head = NULL;
  1171. while (node) {
  1172. t_node = node->next;
  1173. return_resource(&(resources->mem_head), node);
  1174. node = t_node;
  1175. }
  1176. node = func->p_mem_head;
  1177. func->p_mem_head = NULL;
  1178. while (node) {
  1179. t_node = node->next;
  1180. return_resource(&(resources->p_mem_head), node);
  1181. node = t_node;
  1182. }
  1183. node = func->bus_head;
  1184. func->bus_head = NULL;
  1185. while (node) {
  1186. t_node = node->next;
  1187. return_resource(&(resources->bus_head), node);
  1188. node = t_node;
  1189. }
  1190. rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
  1191. rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
  1192. rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
  1193. rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
  1194. return rc;
  1195. }
  1196. /*
  1197. * cpqhp_destroy_resource_list
  1198. *
  1199. * Puts node back in the resource list pointed to by head
  1200. */
  1201. void cpqhp_destroy_resource_list (struct resource_lists * resources)
  1202. {
  1203. struct pci_resource *res, *tres;
  1204. res = resources->io_head;
  1205. resources->io_head = NULL;
  1206. while (res) {
  1207. tres = res;
  1208. res = res->next;
  1209. kfree(tres);
  1210. }
  1211. res = resources->mem_head;
  1212. resources->mem_head = NULL;
  1213. while (res) {
  1214. tres = res;
  1215. res = res->next;
  1216. kfree(tres);
  1217. }
  1218. res = resources->p_mem_head;
  1219. resources->p_mem_head = NULL;
  1220. while (res) {
  1221. tres = res;
  1222. res = res->next;
  1223. kfree(tres);
  1224. }
  1225. res = resources->bus_head;
  1226. resources->bus_head = NULL;
  1227. while (res) {
  1228. tres = res;
  1229. res = res->next;
  1230. kfree(tres);
  1231. }
  1232. }
  1233. /*
  1234. * cpqhp_destroy_board_resources
  1235. *
  1236. * Puts node back in the resource list pointed to by head
  1237. */
  1238. void cpqhp_destroy_board_resources (struct pci_func * func)
  1239. {
  1240. struct pci_resource *res, *tres;
  1241. res = func->io_head;
  1242. func->io_head = NULL;
  1243. while (res) {
  1244. tres = res;
  1245. res = res->next;
  1246. kfree(tres);
  1247. }
  1248. res = func->mem_head;
  1249. func->mem_head = NULL;
  1250. while (res) {
  1251. tres = res;
  1252. res = res->next;
  1253. kfree(tres);
  1254. }
  1255. res = func->p_mem_head;
  1256. func->p_mem_head = NULL;
  1257. while (res) {
  1258. tres = res;
  1259. res = res->next;
  1260. kfree(tres);
  1261. }
  1262. res = func->bus_head;
  1263. func->bus_head = NULL;
  1264. while (res) {
  1265. tres = res;
  1266. res = res->next;
  1267. kfree(tres);
  1268. }
  1269. }