cpqphp_pci.c 40 KB

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