cpqphp_pci.c 40 KB

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