cpqphp_pci.c 39 KB

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