cpqphp_pci.c 39 KB

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