cpqphp_pci.c 38 KB

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