cpqphp_pci.c 38 KB

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