cpqphp_pci.c 40 KB

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