cpqphp_pci.c 38 KB

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