cpqphp_pci.c 39 KB

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