cpqphp_pci.c 40 KB

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