cpqphp_pci.c 39 KB

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