cpqphp_pci.c 38 KB

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