cpqphp_pci.c 40 KB

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