cpqphp_pci.c 39 KB

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