cpqphp_pci.c 40 KB

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