ibmphp_pci.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IBM Hot Plug Controller Driver
  4. *
  5. * Written By: Irene Zubarev, IBM Corporation
  6. *
  7. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  8. * Copyright (C) 2001,2002 IBM Corp.
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <gregkh@us.ibm.com>
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/pci.h>
  18. #include <linux/list.h>
  19. #include "ibmphp.h"
  20. static int configure_device(struct pci_func *);
  21. static int configure_bridge(struct pci_func **, u8);
  22. static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
  23. static int add_new_bus(struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
  24. static u8 find_sec_number(u8 primary_busno, u8 slotno);
  25. /*
  26. * NOTE..... If BIOS doesn't provide default routing, we assign:
  27. * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
  28. * If adapter is bridged, then we assign 11 to it and devices behind it.
  29. * We also assign the same irq numbers for multi function devices.
  30. * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
  31. */
  32. static void assign_alt_irq(struct pci_func *cur_func, u8 class_code)
  33. {
  34. int j;
  35. for (j = 0; j < 4; j++) {
  36. if (cur_func->irq[j] == 0xff) {
  37. switch (class_code) {
  38. case PCI_BASE_CLASS_STORAGE:
  39. cur_func->irq[j] = SCSI_IRQ;
  40. break;
  41. case PCI_BASE_CLASS_NETWORK:
  42. cur_func->irq[j] = LAN_IRQ;
  43. break;
  44. default:
  45. cur_func->irq[j] = OTHER_IRQ;
  46. break;
  47. }
  48. }
  49. }
  50. }
  51. /*
  52. * Configures the device to be added (will allocate needed resources if it
  53. * can), the device can be a bridge or a regular pci device, can also be
  54. * multi-functional
  55. *
  56. * Input: function to be added
  57. *
  58. * TO DO: The error case with Multifunction device or multi function bridge,
  59. * if there is an error, will need to go through all previous functions and
  60. * unconfigure....or can add some code into unconfigure_card....
  61. */
  62. int ibmphp_configure_card(struct pci_func *func, u8 slotno)
  63. {
  64. u16 vendor_id;
  65. u32 class;
  66. u8 class_code;
  67. u8 hdr_type, device, sec_number;
  68. u8 function;
  69. struct pci_func *newfunc; /* for multi devices */
  70. struct pci_func *cur_func, *prev_func;
  71. int rc, i, j;
  72. int cleanup_count;
  73. u8 flag;
  74. u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
  75. debug("inside configure_card, func->busno = %x\n", func->busno);
  76. device = func->device;
  77. cur_func = func;
  78. /* We only get bus and device from IRQ routing table. So at this point,
  79. * func->busno is correct, and func->device contains only device (at the 5
  80. * highest bits)
  81. */
  82. /* For every function on the card */
  83. for (function = 0x00; function < 0x08; function++) {
  84. unsigned int devfn = PCI_DEVFN(device, function);
  85. ibmphp_pci_bus->number = cur_func->busno;
  86. cur_func->function = function;
  87. debug("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
  88. cur_func->busno, cur_func->device, cur_func->function);
  89. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
  90. debug("vendor_id is %x\n", vendor_id);
  91. if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
  92. /* found correct device!!! */
  93. debug("found valid device, vendor_id = %x\n", vendor_id);
  94. ++valid_device;
  95. /* header: x x x x x x x x
  96. * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
  97. * |_=> 0 = single function device, 1 = multi-function device
  98. */
  99. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  100. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
  101. class_code = class >> 24;
  102. debug("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
  103. class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
  104. if (class == PCI_CLASS_NOT_DEFINED_VGA) {
  105. err("The device %x is VGA compatible and as is not supported for hot plugging. "
  106. "Please choose another device.\n", cur_func->device);
  107. return -ENODEV;
  108. } else if (class == PCI_CLASS_DISPLAY_VGA) {
  109. err("The device %x is not supported for hot plugging. Please choose another device.\n",
  110. cur_func->device);
  111. return -ENODEV;
  112. }
  113. switch (hdr_type) {
  114. case PCI_HEADER_TYPE_NORMAL:
  115. debug("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
  116. assign_alt_irq(cur_func, class_code);
  117. rc = configure_device(cur_func);
  118. if (rc < 0) {
  119. /* We need to do this in case some other BARs were properly inserted */
  120. err("was not able to configure devfunc %x on bus %x.\n",
  121. cur_func->device, cur_func->busno);
  122. cleanup_count = 6;
  123. goto error;
  124. }
  125. cur_func->next = NULL;
  126. function = 0x8;
  127. break;
  128. case PCI_HEADER_TYPE_MULTIDEVICE:
  129. assign_alt_irq(cur_func, class_code);
  130. rc = configure_device(cur_func);
  131. if (rc < 0) {
  132. /* We need to do this in case some other BARs were properly inserted */
  133. err("was not able to configure devfunc %x on bus %x...bailing out\n",
  134. cur_func->device, cur_func->busno);
  135. cleanup_count = 6;
  136. goto error;
  137. }
  138. newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
  139. if (!newfunc) {
  140. err("out of system memory\n");
  141. return -ENOMEM;
  142. }
  143. newfunc->busno = cur_func->busno;
  144. newfunc->device = device;
  145. cur_func->next = newfunc;
  146. cur_func = newfunc;
  147. for (j = 0; j < 4; j++)
  148. newfunc->irq[j] = cur_func->irq[j];
  149. break;
  150. case PCI_HEADER_TYPE_MULTIBRIDGE:
  151. class >>= 8;
  152. if (class != PCI_CLASS_BRIDGE_PCI) {
  153. err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
  154. cur_func->device);
  155. return -ENODEV;
  156. }
  157. assign_alt_irq(cur_func, class_code);
  158. rc = configure_bridge(&cur_func, slotno);
  159. if (rc == -ENODEV) {
  160. err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
  161. err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
  162. return rc;
  163. }
  164. if (rc) {
  165. /* We need to do this in case some other BARs were properly inserted */
  166. err("was not able to hot-add PPB properly.\n");
  167. func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
  168. cleanup_count = 2;
  169. goto error;
  170. }
  171. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
  172. flag = 0;
  173. for (i = 0; i < 32; i++) {
  174. if (func->devices[i]) {
  175. newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
  176. if (!newfunc) {
  177. err("out of system memory\n");
  178. return -ENOMEM;
  179. }
  180. newfunc->busno = sec_number;
  181. newfunc->device = (u8) i;
  182. for (j = 0; j < 4; j++)
  183. newfunc->irq[j] = cur_func->irq[j];
  184. if (flag) {
  185. for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
  186. prev_func->next = newfunc;
  187. } else
  188. cur_func->next = newfunc;
  189. rc = ibmphp_configure_card(newfunc, slotno);
  190. /* This could only happen if kmalloc failed */
  191. if (rc) {
  192. /* We need to do this in case bridge itself got configured properly, but devices behind it failed */
  193. func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
  194. cleanup_count = 2;
  195. goto error;
  196. }
  197. flag = 1;
  198. }
  199. }
  200. newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
  201. if (!newfunc) {
  202. err("out of system memory\n");
  203. return -ENOMEM;
  204. }
  205. newfunc->busno = cur_func->busno;
  206. newfunc->device = device;
  207. for (j = 0; j < 4; j++)
  208. newfunc->irq[j] = cur_func->irq[j];
  209. for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
  210. prev_func->next = newfunc;
  211. cur_func = newfunc;
  212. break;
  213. case PCI_HEADER_TYPE_BRIDGE:
  214. class >>= 8;
  215. debug("class now is %x\n", class);
  216. if (class != PCI_CLASS_BRIDGE_PCI) {
  217. err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging. Please insert another card.\n",
  218. cur_func->device);
  219. return -ENODEV;
  220. }
  221. assign_alt_irq(cur_func, class_code);
  222. debug("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
  223. rc = configure_bridge(&cur_func, slotno);
  224. if (rc == -ENODEV) {
  225. err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
  226. err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
  227. return rc;
  228. }
  229. if (rc) {
  230. /* We need to do this in case some other BARs were properly inserted */
  231. func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
  232. err("was not able to hot-add PPB properly.\n");
  233. cleanup_count = 2;
  234. goto error;
  235. }
  236. debug("cur_func->busno = %x, device = %x, function = %x\n",
  237. cur_func->busno, device, function);
  238. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
  239. debug("after configuring bridge..., sec_number = %x\n", sec_number);
  240. flag = 0;
  241. for (i = 0; i < 32; i++) {
  242. if (func->devices[i]) {
  243. debug("inside for loop, device is %x\n", i);
  244. newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
  245. if (!newfunc) {
  246. err(" out of system memory\n");
  247. return -ENOMEM;
  248. }
  249. newfunc->busno = sec_number;
  250. newfunc->device = (u8) i;
  251. for (j = 0; j < 4; j++)
  252. newfunc->irq[j] = cur_func->irq[j];
  253. if (flag) {
  254. for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
  255. prev_func->next = newfunc;
  256. } else
  257. cur_func->next = newfunc;
  258. rc = ibmphp_configure_card(newfunc, slotno);
  259. /* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
  260. if (rc) {
  261. /* We need to do this in case some other BARs were properly inserted */
  262. func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
  263. cleanup_count = 2;
  264. goto error;
  265. }
  266. flag = 1;
  267. }
  268. }
  269. function = 0x8;
  270. break;
  271. default:
  272. err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
  273. return -ENXIO;
  274. break;
  275. } /* end of switch */
  276. } /* end of valid device */
  277. } /* end of for */
  278. if (!valid_device) {
  279. err("Cannot find any valid devices on the card. Or unable to read from card.\n");
  280. return -ENODEV;
  281. }
  282. return 0;
  283. error:
  284. for (i = 0; i < cleanup_count; i++) {
  285. if (cur_func->io[i]) {
  286. ibmphp_remove_resource(cur_func->io[i]);
  287. cur_func->io[i] = NULL;
  288. } else if (cur_func->pfmem[i]) {
  289. ibmphp_remove_resource(cur_func->pfmem[i]);
  290. cur_func->pfmem[i] = NULL;
  291. } else if (cur_func->mem[i]) {
  292. ibmphp_remove_resource(cur_func->mem[i]);
  293. cur_func->mem[i] = NULL;
  294. }
  295. }
  296. return rc;
  297. }
  298. /*
  299. * This function configures the pci BARs of a single device.
  300. * Input: pointer to the pci_func
  301. * Output: configured PCI, 0, or error
  302. */
  303. static int configure_device(struct pci_func *func)
  304. {
  305. u32 bar[6];
  306. u32 address[] = {
  307. PCI_BASE_ADDRESS_0,
  308. PCI_BASE_ADDRESS_1,
  309. PCI_BASE_ADDRESS_2,
  310. PCI_BASE_ADDRESS_3,
  311. PCI_BASE_ADDRESS_4,
  312. PCI_BASE_ADDRESS_5,
  313. 0
  314. };
  315. u8 irq;
  316. int count;
  317. int len[6];
  318. struct resource_node *io[6];
  319. struct resource_node *mem[6];
  320. struct resource_node *mem_tmp;
  321. struct resource_node *pfmem[6];
  322. unsigned int devfn;
  323. debug("%s - inside\n", __func__);
  324. devfn = PCI_DEVFN(func->device, func->function);
  325. ibmphp_pci_bus->number = func->busno;
  326. for (count = 0; address[count]; count++) { /* for 6 BARs */
  327. /* not sure if i need this. per scott, said maybe need * something like this
  328. if devices don't adhere 100% to the spec, so don't want to write
  329. to the reserved bits
  330. pcibios_read_config_byte(cur_func->busno, cur_func->device,
  331. PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
  332. if (tmp & 0x01) // IO
  333. pcibios_write_config_dword(cur_func->busno, cur_func->device,
  334. PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
  335. else // Memory
  336. pcibios_write_config_dword(cur_func->busno, cur_func->device,
  337. PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
  338. */
  339. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
  340. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
  341. if (!bar[count]) /* This BAR is not implemented */
  342. continue;
  343. debug("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
  344. if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
  345. /* This is IO */
  346. debug("inside IO SPACE\n");
  347. len[count] = bar[count] & 0xFFFFFFFC;
  348. len[count] = ~len[count] + 1;
  349. debug("len[count] in IO %x, count %d\n", len[count], count);
  350. io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  351. if (!io[count]) {
  352. err("out of system memory\n");
  353. return -ENOMEM;
  354. }
  355. io[count]->type = IO;
  356. io[count]->busno = func->busno;
  357. io[count]->devfunc = PCI_DEVFN(func->device, func->function);
  358. io[count]->len = len[count];
  359. if (ibmphp_check_resource(io[count], 0) == 0) {
  360. ibmphp_add_resource(io[count]);
  361. func->io[count] = io[count];
  362. } else {
  363. err("cannot allocate requested io for bus %x device %x function %x len %x\n",
  364. func->busno, func->device, func->function, len[count]);
  365. kfree(io[count]);
  366. return -EIO;
  367. }
  368. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
  369. /* _______________This is for debugging purposes only_____________________ */
  370. debug("b4 writing, the IO address is %x\n", func->io[count]->start);
  371. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
  372. debug("after writing.... the start address is %x\n", bar[count]);
  373. /* _________________________________________________________________________*/
  374. } else {
  375. /* This is Memory */
  376. if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  377. /* pfmem */
  378. debug("PFMEM SPACE\n");
  379. len[count] = bar[count] & 0xFFFFFFF0;
  380. len[count] = ~len[count] + 1;
  381. debug("len[count] in PFMEM %x, count %d\n", len[count], count);
  382. pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  383. if (!pfmem[count]) {
  384. err("out of system memory\n");
  385. return -ENOMEM;
  386. }
  387. pfmem[count]->type = PFMEM;
  388. pfmem[count]->busno = func->busno;
  389. pfmem[count]->devfunc = PCI_DEVFN(func->device,
  390. func->function);
  391. pfmem[count]->len = len[count];
  392. pfmem[count]->fromMem = 0;
  393. if (ibmphp_check_resource(pfmem[count], 0) == 0) {
  394. ibmphp_add_resource(pfmem[count]);
  395. func->pfmem[count] = pfmem[count];
  396. } else {
  397. mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
  398. if (!mem_tmp) {
  399. err("out of system memory\n");
  400. kfree(pfmem[count]);
  401. return -ENOMEM;
  402. }
  403. mem_tmp->type = MEM;
  404. mem_tmp->busno = pfmem[count]->busno;
  405. mem_tmp->devfunc = pfmem[count]->devfunc;
  406. mem_tmp->len = pfmem[count]->len;
  407. debug("there's no pfmem... going into mem.\n");
  408. if (ibmphp_check_resource(mem_tmp, 0) == 0) {
  409. ibmphp_add_resource(mem_tmp);
  410. pfmem[count]->fromMem = 1;
  411. pfmem[count]->rangeno = mem_tmp->rangeno;
  412. pfmem[count]->start = mem_tmp->start;
  413. pfmem[count]->end = mem_tmp->end;
  414. ibmphp_add_pfmem_from_mem(pfmem[count]);
  415. func->pfmem[count] = pfmem[count];
  416. } else {
  417. err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
  418. func->busno, func->device, len[count]);
  419. kfree(mem_tmp);
  420. kfree(pfmem[count]);
  421. return -EIO;
  422. }
  423. }
  424. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
  425. /*_______________This is for debugging purposes only______________________________*/
  426. debug("b4 writing, start address is %x\n", func->pfmem[count]->start);
  427. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
  428. debug("after writing, start address is %x\n", bar[count]);
  429. /*_________________________________________________________________________________*/
  430. if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) { /* takes up another dword */
  431. debug("inside the mem 64 case, count %d\n", count);
  432. count += 1;
  433. /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
  434. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
  435. }
  436. } else {
  437. /* regular memory */
  438. debug("REGULAR MEM SPACE\n");
  439. len[count] = bar[count] & 0xFFFFFFF0;
  440. len[count] = ~len[count] + 1;
  441. debug("len[count] in Mem %x, count %d\n", len[count], count);
  442. mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  443. if (!mem[count]) {
  444. err("out of system memory\n");
  445. return -ENOMEM;
  446. }
  447. mem[count]->type = MEM;
  448. mem[count]->busno = func->busno;
  449. mem[count]->devfunc = PCI_DEVFN(func->device,
  450. func->function);
  451. mem[count]->len = len[count];
  452. if (ibmphp_check_resource(mem[count], 0) == 0) {
  453. ibmphp_add_resource(mem[count]);
  454. func->mem[count] = mem[count];
  455. } else {
  456. err("cannot allocate requested mem for bus %x, device %x, len %x\n",
  457. func->busno, func->device, len[count]);
  458. kfree(mem[count]);
  459. return -EIO;
  460. }
  461. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
  462. /* _______________________This is for debugging purposes only _______________________*/
  463. debug("b4 writing, start address is %x\n", func->mem[count]->start);
  464. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
  465. debug("after writing, the address is %x\n", bar[count]);
  466. /* __________________________________________________________________________________*/
  467. if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  468. /* takes up another dword */
  469. debug("inside mem 64 case, reg. mem, count %d\n", count);
  470. count += 1;
  471. /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
  472. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
  473. }
  474. }
  475. } /* end of mem */
  476. } /* end of for */
  477. func->bus = 0; /* To indicate that this is not a PPB */
  478. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
  479. if ((irq > 0x00) && (irq < 0x05))
  480. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
  481. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
  482. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
  483. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
  484. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
  485. return 0;
  486. }
  487. /******************************************************************************
  488. * This routine configures a PCI-2-PCI bridge and the functions behind it
  489. * Parameters: pci_func
  490. * Returns:
  491. ******************************************************************************/
  492. static int configure_bridge(struct pci_func **func_passed, u8 slotno)
  493. {
  494. int count;
  495. int i;
  496. int rc;
  497. u8 sec_number;
  498. u8 io_base;
  499. u16 pfmem_base;
  500. u32 bar[2];
  501. u32 len[2];
  502. u8 flag_io = 0;
  503. u8 flag_mem = 0;
  504. u8 flag_pfmem = 0;
  505. u8 need_io_upper = 0;
  506. u8 need_pfmem_upper = 0;
  507. struct res_needed *amount_needed = NULL;
  508. struct resource_node *io = NULL;
  509. struct resource_node *bus_io[2] = {NULL, NULL};
  510. struct resource_node *mem = NULL;
  511. struct resource_node *bus_mem[2] = {NULL, NULL};
  512. struct resource_node *mem_tmp = NULL;
  513. struct resource_node *pfmem = NULL;
  514. struct resource_node *bus_pfmem[2] = {NULL, NULL};
  515. struct bus_node *bus;
  516. u32 address[] = {
  517. PCI_BASE_ADDRESS_0,
  518. PCI_BASE_ADDRESS_1,
  519. 0
  520. };
  521. struct pci_func *func = *func_passed;
  522. unsigned int devfn;
  523. u8 irq;
  524. int retval;
  525. debug("%s - enter\n", __func__);
  526. devfn = PCI_DEVFN(func->function, func->device);
  527. ibmphp_pci_bus->number = func->busno;
  528. /* Configuring necessary info for the bridge so that we could see the devices
  529. * behind it
  530. */
  531. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
  532. /* _____________________For debugging purposes only __________________________
  533. pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
  534. debug("primary # written into the bridge is %x\n", pri_number);
  535. ___________________________________________________________________________*/
  536. /* in EBDA, only get allocated 1 additional bus # per slot */
  537. sec_number = find_sec_number(func->busno, slotno);
  538. if (sec_number == 0xff) {
  539. err("cannot allocate secondary bus number for the bridged device\n");
  540. return -EINVAL;
  541. }
  542. debug("after find_sec_number, the number we got is %x\n", sec_number);
  543. debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
  544. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
  545. /* __________________For debugging purposes only __________________________________
  546. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
  547. debug("sec_number after write/read is %x\n", sec_number);
  548. ________________________________________________________________________________*/
  549. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
  550. /* __________________For debugging purposes only ____________________________________
  551. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
  552. debug("subordinate number after write/read is %x\n", sec_number);
  553. __________________________________________________________________________________*/
  554. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
  555. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
  556. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
  557. debug("func->busno is %x\n", func->busno);
  558. debug("sec_number after writing is %x\n", sec_number);
  559. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  560. !!!!!!!!!!!!!!!NEED TO ADD!!! FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
  561. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  562. /* First we need to allocate mem/io for the bridge itself in case it needs it */
  563. for (count = 0; address[count]; count++) { /* for 2 BARs */
  564. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
  565. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
  566. if (!bar[count]) {
  567. /* This BAR is not implemented */
  568. debug("so we come here then, eh?, count = %d\n", count);
  569. continue;
  570. }
  571. // tmp_bar = bar[count];
  572. debug("Bar %d wants %x\n", count, bar[count]);
  573. if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
  574. /* This is IO */
  575. len[count] = bar[count] & 0xFFFFFFFC;
  576. len[count] = ~len[count] + 1;
  577. debug("len[count] in IO = %x\n", len[count]);
  578. bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  579. if (!bus_io[count]) {
  580. err("out of system memory\n");
  581. retval = -ENOMEM;
  582. goto error;
  583. }
  584. bus_io[count]->type = IO;
  585. bus_io[count]->busno = func->busno;
  586. bus_io[count]->devfunc = PCI_DEVFN(func->device,
  587. func->function);
  588. bus_io[count]->len = len[count];
  589. if (ibmphp_check_resource(bus_io[count], 0) == 0) {
  590. ibmphp_add_resource(bus_io[count]);
  591. func->io[count] = bus_io[count];
  592. } else {
  593. err("cannot allocate requested io for bus %x, device %x, len %x\n",
  594. func->busno, func->device, len[count]);
  595. kfree(bus_io[count]);
  596. return -EIO;
  597. }
  598. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
  599. } else {
  600. /* This is Memory */
  601. if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  602. /* pfmem */
  603. len[count] = bar[count] & 0xFFFFFFF0;
  604. len[count] = ~len[count] + 1;
  605. debug("len[count] in PFMEM = %x\n", len[count]);
  606. bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  607. if (!bus_pfmem[count]) {
  608. err("out of system memory\n");
  609. retval = -ENOMEM;
  610. goto error;
  611. }
  612. bus_pfmem[count]->type = PFMEM;
  613. bus_pfmem[count]->busno = func->busno;
  614. bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
  615. func->function);
  616. bus_pfmem[count]->len = len[count];
  617. bus_pfmem[count]->fromMem = 0;
  618. if (ibmphp_check_resource(bus_pfmem[count], 0) == 0) {
  619. ibmphp_add_resource(bus_pfmem[count]);
  620. func->pfmem[count] = bus_pfmem[count];
  621. } else {
  622. mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
  623. if (!mem_tmp) {
  624. err("out of system memory\n");
  625. retval = -ENOMEM;
  626. goto error;
  627. }
  628. mem_tmp->type = MEM;
  629. mem_tmp->busno = bus_pfmem[count]->busno;
  630. mem_tmp->devfunc = bus_pfmem[count]->devfunc;
  631. mem_tmp->len = bus_pfmem[count]->len;
  632. if (ibmphp_check_resource(mem_tmp, 0) == 0) {
  633. ibmphp_add_resource(mem_tmp);
  634. bus_pfmem[count]->fromMem = 1;
  635. bus_pfmem[count]->rangeno = mem_tmp->rangeno;
  636. ibmphp_add_pfmem_from_mem(bus_pfmem[count]);
  637. func->pfmem[count] = bus_pfmem[count];
  638. } else {
  639. err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
  640. func->busno, func->device, len[count]);
  641. kfree(mem_tmp);
  642. kfree(bus_pfmem[count]);
  643. return -EIO;
  644. }
  645. }
  646. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
  647. if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  648. /* takes up another dword */
  649. count += 1;
  650. /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
  651. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
  652. }
  653. } else {
  654. /* regular memory */
  655. len[count] = bar[count] & 0xFFFFFFF0;
  656. len[count] = ~len[count] + 1;
  657. debug("len[count] in Memory is %x\n", len[count]);
  658. bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  659. if (!bus_mem[count]) {
  660. err("out of system memory\n");
  661. retval = -ENOMEM;
  662. goto error;
  663. }
  664. bus_mem[count]->type = MEM;
  665. bus_mem[count]->busno = func->busno;
  666. bus_mem[count]->devfunc = PCI_DEVFN(func->device,
  667. func->function);
  668. bus_mem[count]->len = len[count];
  669. if (ibmphp_check_resource(bus_mem[count], 0) == 0) {
  670. ibmphp_add_resource(bus_mem[count]);
  671. func->mem[count] = bus_mem[count];
  672. } else {
  673. err("cannot allocate requested mem for bus %x, device %x, len %x\n",
  674. func->busno, func->device, len[count]);
  675. kfree(bus_mem[count]);
  676. return -EIO;
  677. }
  678. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
  679. if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  680. /* takes up another dword */
  681. count += 1;
  682. /* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
  683. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
  684. }
  685. }
  686. } /* end of mem */
  687. } /* end of for */
  688. /* Now need to see how much space the devices behind the bridge needed */
  689. amount_needed = scan_behind_bridge(func, sec_number);
  690. if (amount_needed == NULL)
  691. return -ENOMEM;
  692. ibmphp_pci_bus->number = func->busno;
  693. debug("after coming back from scan_behind_bridge\n");
  694. debug("amount_needed->not_correct = %x\n", amount_needed->not_correct);
  695. debug("amount_needed->io = %x\n", amount_needed->io);
  696. debug("amount_needed->mem = %x\n", amount_needed->mem);
  697. debug("amount_needed->pfmem = %x\n", amount_needed->pfmem);
  698. if (amount_needed->not_correct) {
  699. debug("amount_needed is not correct\n");
  700. for (count = 0; address[count]; count++) {
  701. /* for 2 BARs */
  702. if (bus_io[count]) {
  703. ibmphp_remove_resource(bus_io[count]);
  704. func->io[count] = NULL;
  705. } else if (bus_pfmem[count]) {
  706. ibmphp_remove_resource(bus_pfmem[count]);
  707. func->pfmem[count] = NULL;
  708. } else if (bus_mem[count]) {
  709. ibmphp_remove_resource(bus_mem[count]);
  710. func->mem[count] = NULL;
  711. }
  712. }
  713. kfree(amount_needed);
  714. return -ENODEV;
  715. }
  716. if (!amount_needed->io) {
  717. debug("it doesn't want IO?\n");
  718. flag_io = 1;
  719. } else {
  720. debug("it wants %x IO behind the bridge\n", amount_needed->io);
  721. io = kzalloc(sizeof(*io), GFP_KERNEL);
  722. if (!io) {
  723. err("out of system memory\n");
  724. retval = -ENOMEM;
  725. goto error;
  726. }
  727. io->type = IO;
  728. io->busno = func->busno;
  729. io->devfunc = PCI_DEVFN(func->device, func->function);
  730. io->len = amount_needed->io;
  731. if (ibmphp_check_resource(io, 1) == 0) {
  732. debug("were we able to add io\n");
  733. ibmphp_add_resource(io);
  734. flag_io = 1;
  735. }
  736. }
  737. if (!amount_needed->mem) {
  738. debug("it doesn't want n.e.memory?\n");
  739. flag_mem = 1;
  740. } else {
  741. debug("it wants %x memory behind the bridge\n", amount_needed->mem);
  742. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  743. if (!mem) {
  744. err("out of system memory\n");
  745. retval = -ENOMEM;
  746. goto error;
  747. }
  748. mem->type = MEM;
  749. mem->busno = func->busno;
  750. mem->devfunc = PCI_DEVFN(func->device, func->function);
  751. mem->len = amount_needed->mem;
  752. if (ibmphp_check_resource(mem, 1) == 0) {
  753. ibmphp_add_resource(mem);
  754. flag_mem = 1;
  755. debug("were we able to add mem\n");
  756. }
  757. }
  758. if (!amount_needed->pfmem) {
  759. debug("it doesn't want n.e.pfmem mem?\n");
  760. flag_pfmem = 1;
  761. } else {
  762. debug("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
  763. pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
  764. if (!pfmem) {
  765. err("out of system memory\n");
  766. retval = -ENOMEM;
  767. goto error;
  768. }
  769. pfmem->type = PFMEM;
  770. pfmem->busno = func->busno;
  771. pfmem->devfunc = PCI_DEVFN(func->device, func->function);
  772. pfmem->len = amount_needed->pfmem;
  773. pfmem->fromMem = 0;
  774. if (ibmphp_check_resource(pfmem, 1) == 0) {
  775. ibmphp_add_resource(pfmem);
  776. flag_pfmem = 1;
  777. } else {
  778. mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
  779. if (!mem_tmp) {
  780. err("out of system memory\n");
  781. retval = -ENOMEM;
  782. goto error;
  783. }
  784. mem_tmp->type = MEM;
  785. mem_tmp->busno = pfmem->busno;
  786. mem_tmp->devfunc = pfmem->devfunc;
  787. mem_tmp->len = pfmem->len;
  788. if (ibmphp_check_resource(mem_tmp, 1) == 0) {
  789. ibmphp_add_resource(mem_tmp);
  790. pfmem->fromMem = 1;
  791. pfmem->rangeno = mem_tmp->rangeno;
  792. ibmphp_add_pfmem_from_mem(pfmem);
  793. flag_pfmem = 1;
  794. }
  795. }
  796. }
  797. debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
  798. debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
  799. if (flag_io && flag_mem && flag_pfmem) {
  800. /* If on bootup, there was a bridged card in this slot,
  801. * then card was removed and ibmphp got unloaded and loaded
  802. * back again, there's no way for us to remove the bus
  803. * struct, so no need to kmalloc, can use existing node
  804. */
  805. bus = ibmphp_find_res_bus(sec_number);
  806. if (!bus) {
  807. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  808. if (!bus) {
  809. err("out of system memory\n");
  810. retval = -ENOMEM;
  811. goto error;
  812. }
  813. bus->busno = sec_number;
  814. debug("b4 adding new bus\n");
  815. rc = add_new_bus(bus, io, mem, pfmem, func->busno);
  816. } else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
  817. rc = add_new_bus(bus, io, mem, pfmem, 0xFF);
  818. else {
  819. err("expected bus structure not empty?\n");
  820. retval = -EIO;
  821. goto error;
  822. }
  823. if (rc) {
  824. if (rc == -ENOMEM) {
  825. ibmphp_remove_bus(bus, func->busno);
  826. kfree(amount_needed);
  827. return rc;
  828. }
  829. retval = rc;
  830. goto error;
  831. }
  832. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
  833. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
  834. if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  835. debug("io 32\n");
  836. need_io_upper = 1;
  837. }
  838. if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  839. debug("pfmem 64\n");
  840. need_pfmem_upper = 1;
  841. }
  842. if (bus->noIORanges) {
  843. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
  844. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
  845. /* _______________This is for debugging purposes only ____________________
  846. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
  847. debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
  848. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
  849. debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
  850. ________________________________________________________________________*/
  851. if (need_io_upper) { /* since can't support n.e.ways */
  852. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
  853. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
  854. }
  855. } else {
  856. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
  857. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
  858. }
  859. if (bus->noMemRanges) {
  860. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
  861. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
  862. /* ____________________This is for debugging purposes only ________________________
  863. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
  864. debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
  865. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
  866. debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
  867. __________________________________________________________________________________*/
  868. } else {
  869. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
  870. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
  871. }
  872. if (bus->noPFMemRanges) {
  873. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
  874. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
  875. /* __________________________This is for debugging purposes only _______________________
  876. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
  877. debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
  878. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
  879. debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
  880. ______________________________________________________________________________________*/
  881. if (need_pfmem_upper) { /* since can't support n.e.ways */
  882. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
  883. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
  884. }
  885. } else {
  886. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
  887. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
  888. }
  889. debug("b4 writing control information\n");
  890. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
  891. if ((irq > 0x00) && (irq < 0x05))
  892. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
  893. /*
  894. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
  895. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
  896. pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
  897. */
  898. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
  899. pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
  900. for (i = 0; i < 32; i++) {
  901. if (amount_needed->devices[i]) {
  902. debug("device where devices[i] is 1 = %x\n", i);
  903. func->devices[i] = 1;
  904. }
  905. }
  906. func->bus = 1; /* For unconfiguring, to indicate it's PPB */
  907. func_passed = &func;
  908. debug("func->busno b4 returning is %x\n", func->busno);
  909. debug("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
  910. kfree(amount_needed);
  911. return 0;
  912. } else {
  913. err("Configuring bridge was unsuccessful...\n");
  914. mem_tmp = NULL;
  915. retval = -EIO;
  916. goto error;
  917. }
  918. error:
  919. kfree(amount_needed);
  920. if (pfmem)
  921. ibmphp_remove_resource(pfmem);
  922. if (io)
  923. ibmphp_remove_resource(io);
  924. if (mem)
  925. ibmphp_remove_resource(mem);
  926. for (i = 0; i < 2; i++) { /* for 2 BARs */
  927. if (bus_io[i]) {
  928. ibmphp_remove_resource(bus_io[i]);
  929. func->io[i] = NULL;
  930. } else if (bus_pfmem[i]) {
  931. ibmphp_remove_resource(bus_pfmem[i]);
  932. func->pfmem[i] = NULL;
  933. } else if (bus_mem[i]) {
  934. ibmphp_remove_resource(bus_mem[i]);
  935. func->mem[i] = NULL;
  936. }
  937. }
  938. return retval;
  939. }
  940. /*****************************************************************************
  941. * This function adds up the amount of resources needed behind the PPB bridge
  942. * and passes it to the configure_bridge function
  943. * Input: bridge function
  944. * Output: amount of resources needed
  945. *****************************************************************************/
  946. static struct res_needed *scan_behind_bridge(struct pci_func *func, u8 busno)
  947. {
  948. int count, len[6];
  949. u16 vendor_id;
  950. u8 hdr_type;
  951. u8 device, function;
  952. unsigned int devfn;
  953. int howmany = 0; /*this is to see if there are any devices behind the bridge */
  954. u32 bar[6], class;
  955. u32 address[] = {
  956. PCI_BASE_ADDRESS_0,
  957. PCI_BASE_ADDRESS_1,
  958. PCI_BASE_ADDRESS_2,
  959. PCI_BASE_ADDRESS_3,
  960. PCI_BASE_ADDRESS_4,
  961. PCI_BASE_ADDRESS_5,
  962. 0
  963. };
  964. struct res_needed *amount;
  965. amount = kzalloc(sizeof(*amount), GFP_KERNEL);
  966. if (amount == NULL)
  967. return NULL;
  968. ibmphp_pci_bus->number = busno;
  969. debug("the bus_no behind the bridge is %x\n", busno);
  970. debug("scanning devices behind the bridge...\n");
  971. for (device = 0; device < 32; device++) {
  972. amount->devices[device] = 0;
  973. for (function = 0; function < 8; function++) {
  974. devfn = PCI_DEVFN(device, function);
  975. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
  976. if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
  977. /* found correct device!!! */
  978. howmany++;
  979. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  980. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
  981. debug("hdr_type behind the bridge is %x\n", hdr_type);
  982. if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
  983. err("embedded bridges not supported for hot-plugging.\n");
  984. amount->not_correct = 1;
  985. return amount;
  986. }
  987. class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
  988. if (class == PCI_CLASS_NOT_DEFINED_VGA) {
  989. err("The device %x is VGA compatible and as is not supported for hot plugging. Please choose another device.\n", device);
  990. amount->not_correct = 1;
  991. return amount;
  992. } else if (class == PCI_CLASS_DISPLAY_VGA) {
  993. err("The device %x is not supported for hot plugging. Please choose another device.\n", device);
  994. amount->not_correct = 1;
  995. return amount;
  996. }
  997. amount->devices[device] = 1;
  998. for (count = 0; address[count]; count++) {
  999. /* for 6 BARs */
  1000. /*
  1001. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
  1002. if (tmp & 0x01) // IO
  1003. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
  1004. else // MEMORY
  1005. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
  1006. */
  1007. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
  1008. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
  1009. debug("what is bar[count]? %x, count = %d\n", bar[count], count);
  1010. if (!bar[count]) /* This BAR is not implemented */
  1011. continue;
  1012. //tmp_bar = bar[count];
  1013. debug("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
  1014. if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
  1015. /* This is IO */
  1016. len[count] = bar[count] & 0xFFFFFFFC;
  1017. len[count] = ~len[count] + 1;
  1018. amount->io += len[count];
  1019. } else {
  1020. /* This is Memory */
  1021. if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  1022. /* pfmem */
  1023. len[count] = bar[count] & 0xFFFFFFF0;
  1024. len[count] = ~len[count] + 1;
  1025. amount->pfmem += len[count];
  1026. if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
  1027. /* takes up another dword */
  1028. count += 1;
  1029. } else {
  1030. /* regular memory */
  1031. len[count] = bar[count] & 0xFFFFFFF0;
  1032. len[count] = ~len[count] + 1;
  1033. amount->mem += len[count];
  1034. if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  1035. /* takes up another dword */
  1036. count += 1;
  1037. }
  1038. }
  1039. }
  1040. } /* end for */
  1041. } /* end if (valid) */
  1042. } /* end for */
  1043. } /* end for */
  1044. if (!howmany)
  1045. amount->not_correct = 1;
  1046. else
  1047. amount->not_correct = 0;
  1048. if ((amount->io) && (amount->io < IOBRIDGE))
  1049. amount->io = IOBRIDGE;
  1050. if ((amount->mem) && (amount->mem < MEMBRIDGE))
  1051. amount->mem = MEMBRIDGE;
  1052. if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
  1053. amount->pfmem = MEMBRIDGE;
  1054. return amount;
  1055. }
  1056. /* The following 3 unconfigure_boot_ routines deal with the case when we had the card
  1057. * upon bootup in the system, since we don't allocate func to such case, we need to read
  1058. * the start addresses from pci config space and then find the corresponding entries in
  1059. * our resource lists. The functions return either 0, -ENODEV, or -1 (general failure)
  1060. * Change: we also call these functions even if we configured the card ourselves (i.e., not
  1061. * the bootup case), since it should work same way
  1062. */
  1063. static int unconfigure_boot_device(u8 busno, u8 device, u8 function)
  1064. {
  1065. u32 start_address;
  1066. u32 address[] = {
  1067. PCI_BASE_ADDRESS_0,
  1068. PCI_BASE_ADDRESS_1,
  1069. PCI_BASE_ADDRESS_2,
  1070. PCI_BASE_ADDRESS_3,
  1071. PCI_BASE_ADDRESS_4,
  1072. PCI_BASE_ADDRESS_5,
  1073. 0
  1074. };
  1075. int count;
  1076. struct resource_node *io;
  1077. struct resource_node *mem;
  1078. struct resource_node *pfmem;
  1079. struct bus_node *bus;
  1080. u32 end_address;
  1081. u32 temp_end;
  1082. u32 size;
  1083. u32 tmp_address;
  1084. unsigned int devfn;
  1085. debug("%s - enter\n", __func__);
  1086. bus = ibmphp_find_res_bus(busno);
  1087. if (!bus) {
  1088. debug("cannot find corresponding bus.\n");
  1089. return -EINVAL;
  1090. }
  1091. devfn = PCI_DEVFN(device, function);
  1092. ibmphp_pci_bus->number = busno;
  1093. for (count = 0; address[count]; count++) { /* for 6 BARs */
  1094. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
  1095. /* We can do this here, b/c by that time the device driver of the card has been stopped */
  1096. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
  1097. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &size);
  1098. pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], start_address);
  1099. debug("start_address is %x\n", start_address);
  1100. debug("busno, device, function %x %x %x\n", busno, device, function);
  1101. if (!size) {
  1102. /* This BAR is not implemented */
  1103. debug("is this bar no implemented?, count = %d\n", count);
  1104. continue;
  1105. }
  1106. tmp_address = start_address;
  1107. if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
  1108. /* This is IO */
  1109. start_address &= PCI_BASE_ADDRESS_IO_MASK;
  1110. size = size & 0xFFFFFFFC;
  1111. size = ~size + 1;
  1112. end_address = start_address + size - 1;
  1113. if (ibmphp_find_resource(bus, start_address, &io, IO))
  1114. goto report_search_failure;
  1115. debug("io->start = %x\n", io->start);
  1116. temp_end = io->end;
  1117. start_address = io->end + 1;
  1118. ibmphp_remove_resource(io);
  1119. /* This is needed b/c of the old I/O restrictions in the BIOS */
  1120. while (temp_end < end_address) {
  1121. if (ibmphp_find_resource(bus, start_address,
  1122. &io, IO))
  1123. goto report_search_failure;
  1124. debug("io->start = %x\n", io->start);
  1125. temp_end = io->end;
  1126. start_address = io->end + 1;
  1127. ibmphp_remove_resource(io);
  1128. }
  1129. /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
  1130. } else {
  1131. /* This is Memory */
  1132. if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  1133. /* pfmem */
  1134. debug("start address of pfmem is %x\n", start_address);
  1135. start_address &= PCI_BASE_ADDRESS_MEM_MASK;
  1136. if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
  1137. err("cannot find corresponding PFMEM resource to remove\n");
  1138. return -EIO;
  1139. }
  1140. if (pfmem) {
  1141. debug("pfmem->start = %x\n", pfmem->start);
  1142. ibmphp_remove_resource(pfmem);
  1143. }
  1144. } else {
  1145. /* regular memory */
  1146. debug("start address of mem is %x\n", start_address);
  1147. start_address &= PCI_BASE_ADDRESS_MEM_MASK;
  1148. if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
  1149. err("cannot find corresponding MEM resource to remove\n");
  1150. return -EIO;
  1151. }
  1152. if (mem) {
  1153. debug("mem->start = %x\n", mem->start);
  1154. ibmphp_remove_resource(mem);
  1155. }
  1156. }
  1157. if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  1158. /* takes up another dword */
  1159. count += 1;
  1160. }
  1161. } /* end of mem */
  1162. } /* end of for */
  1163. return 0;
  1164. report_search_failure:
  1165. err("cannot find corresponding IO resource to remove\n");
  1166. return -EIO;
  1167. }
  1168. static int unconfigure_boot_bridge(u8 busno, u8 device, u8 function)
  1169. {
  1170. int count;
  1171. int bus_no, pri_no, sub_no, sec_no = 0;
  1172. u32 start_address, tmp_address;
  1173. u8 sec_number, sub_number, pri_number;
  1174. struct resource_node *io = NULL;
  1175. struct resource_node *mem = NULL;
  1176. struct resource_node *pfmem = NULL;
  1177. struct bus_node *bus;
  1178. u32 address[] = {
  1179. PCI_BASE_ADDRESS_0,
  1180. PCI_BASE_ADDRESS_1,
  1181. 0
  1182. };
  1183. unsigned int devfn;
  1184. devfn = PCI_DEVFN(device, function);
  1185. ibmphp_pci_bus->number = busno;
  1186. bus_no = (int) busno;
  1187. debug("busno is %x\n", busno);
  1188. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
  1189. debug("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
  1190. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
  1191. debug("sec_number is %x\n", sec_number);
  1192. sec_no = (int) sec_number;
  1193. pri_no = (int) pri_number;
  1194. if (pri_no != bus_no) {
  1195. err("primary numbers in our structures and pci config space don't match.\n");
  1196. return -EINVAL;
  1197. }
  1198. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
  1199. sub_no = (int) sub_number;
  1200. debug("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
  1201. if (sec_no != sub_number) {
  1202. err("there're more buses behind this bridge. Hot removal is not supported. Please choose another card\n");
  1203. return -ENODEV;
  1204. }
  1205. bus = ibmphp_find_res_bus(sec_number);
  1206. if (!bus) {
  1207. err("cannot find Bus structure for the bridged device\n");
  1208. return -EINVAL;
  1209. }
  1210. debug("bus->busno is %x\n", bus->busno);
  1211. debug("sec_number is %x\n", sec_number);
  1212. ibmphp_remove_bus(bus, busno);
  1213. for (count = 0; address[count]; count++) {
  1214. /* for 2 BARs */
  1215. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
  1216. if (!start_address) {
  1217. /* This BAR is not implemented */
  1218. continue;
  1219. }
  1220. tmp_address = start_address;
  1221. if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
  1222. /* This is IO */
  1223. start_address &= PCI_BASE_ADDRESS_IO_MASK;
  1224. if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
  1225. err("cannot find corresponding IO resource to remove\n");
  1226. return -EIO;
  1227. }
  1228. if (io)
  1229. debug("io->start = %x\n", io->start);
  1230. ibmphp_remove_resource(io);
  1231. /* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
  1232. } else {
  1233. /* This is Memory */
  1234. if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
  1235. /* pfmem */
  1236. start_address &= PCI_BASE_ADDRESS_MEM_MASK;
  1237. if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
  1238. err("cannot find corresponding PFMEM resource to remove\n");
  1239. return -EINVAL;
  1240. }
  1241. if (pfmem) {
  1242. debug("pfmem->start = %x\n", pfmem->start);
  1243. ibmphp_remove_resource(pfmem);
  1244. }
  1245. } else {
  1246. /* regular memory */
  1247. start_address &= PCI_BASE_ADDRESS_MEM_MASK;
  1248. if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
  1249. err("cannot find corresponding MEM resource to remove\n");
  1250. return -EINVAL;
  1251. }
  1252. if (mem) {
  1253. debug("mem->start = %x\n", mem->start);
  1254. ibmphp_remove_resource(mem);
  1255. }
  1256. }
  1257. if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  1258. /* takes up another dword */
  1259. count += 1;
  1260. }
  1261. } /* end of mem */
  1262. } /* end of for */
  1263. debug("%s - exiting, returning success\n", __func__);
  1264. return 0;
  1265. }
  1266. static int unconfigure_boot_card(struct slot *slot_cur)
  1267. {
  1268. u16 vendor_id;
  1269. u32 class;
  1270. u8 hdr_type;
  1271. u8 device;
  1272. u8 busno;
  1273. u8 function;
  1274. int rc;
  1275. unsigned int devfn;
  1276. u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
  1277. debug("%s - enter\n", __func__);
  1278. device = slot_cur->device;
  1279. busno = slot_cur->bus;
  1280. debug("b4 for loop, device is %x\n", device);
  1281. /* For every function on the card */
  1282. for (function = 0x0; function < 0x08; function++) {
  1283. devfn = PCI_DEVFN(device, function);
  1284. ibmphp_pci_bus->number = busno;
  1285. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
  1286. if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
  1287. /* found correct device!!! */
  1288. ++valid_device;
  1289. debug("%s - found correct device\n", __func__);
  1290. /* header: x x x x x x x x
  1291. * | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
  1292. * |_=> 0 = single function device, 1 = multi-function device
  1293. */
  1294. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  1295. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
  1296. debug("hdr_type %x, class %x\n", hdr_type, class);
  1297. class >>= 8; /* to take revision out, class = class.subclass.prog i/f */
  1298. if (class == PCI_CLASS_NOT_DEFINED_VGA) {
  1299. err("The device %x function %x is VGA compatible and is not supported for hot removing. Please choose another device.\n", device, function);
  1300. return -ENODEV;
  1301. } else if (class == PCI_CLASS_DISPLAY_VGA) {
  1302. err("The device %x function %x is not supported for hot removing. Please choose another device.\n", device, function);
  1303. return -ENODEV;
  1304. }
  1305. switch (hdr_type) {
  1306. case PCI_HEADER_TYPE_NORMAL:
  1307. rc = unconfigure_boot_device(busno, device, function);
  1308. if (rc) {
  1309. err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
  1310. device, function, busno);
  1311. return rc;
  1312. }
  1313. function = 0x8;
  1314. break;
  1315. case PCI_HEADER_TYPE_MULTIDEVICE:
  1316. rc = unconfigure_boot_device(busno, device, function);
  1317. if (rc) {
  1318. err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
  1319. device, function, busno);
  1320. return rc;
  1321. }
  1322. break;
  1323. case PCI_HEADER_TYPE_BRIDGE:
  1324. class >>= 8;
  1325. if (class != PCI_CLASS_BRIDGE_PCI) {
  1326. err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing. Please try another card.\n", device, function);
  1327. return -ENODEV;
  1328. }
  1329. rc = unconfigure_boot_bridge(busno, device, function);
  1330. if (rc != 0) {
  1331. err("was not able to hot-remove PPB properly.\n");
  1332. return rc;
  1333. }
  1334. function = 0x8;
  1335. break;
  1336. case PCI_HEADER_TYPE_MULTIBRIDGE:
  1337. class >>= 8;
  1338. if (class != PCI_CLASS_BRIDGE_PCI) {
  1339. err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing. Please try another card.\n", device, function);
  1340. return -ENODEV;
  1341. }
  1342. rc = unconfigure_boot_bridge(busno, device, function);
  1343. if (rc != 0) {
  1344. err("was not able to hot-remove PPB properly.\n");
  1345. return rc;
  1346. }
  1347. break;
  1348. default:
  1349. err("MAJOR PROBLEM!!!! Cannot read device's header\n");
  1350. return -1;
  1351. break;
  1352. } /* end of switch */
  1353. } /* end of valid device */
  1354. } /* end of for */
  1355. if (!valid_device) {
  1356. err("Could not find device to unconfigure. Or could not read the card.\n");
  1357. return -1;
  1358. }
  1359. return 0;
  1360. }
  1361. /*
  1362. * free the resources of the card (multi, single, or bridged)
  1363. * Parameters: slot, flag to say if this is for removing entire module or just
  1364. * unconfiguring the device
  1365. * TO DO: will probably need to add some code in case there was some resource,
  1366. * to remove it... this is from when we have errors in the configure_card...
  1367. * !!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
  1368. * Returns: 0, -1, -ENODEV
  1369. */
  1370. int ibmphp_unconfigure_card(struct slot **slot_cur, int the_end)
  1371. {
  1372. int i;
  1373. int count;
  1374. int rc;
  1375. struct slot *sl = *slot_cur;
  1376. struct pci_func *cur_func = NULL;
  1377. struct pci_func *temp_func;
  1378. debug("%s - enter\n", __func__);
  1379. if (!the_end) {
  1380. /* Need to unconfigure the card */
  1381. rc = unconfigure_boot_card(sl);
  1382. if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
  1383. /* In all other cases, will still need to get rid of func structure if it exists */
  1384. return rc;
  1385. }
  1386. }
  1387. if (sl->func) {
  1388. cur_func = sl->func;
  1389. while (cur_func) {
  1390. /* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
  1391. if (cur_func->bus) {
  1392. /* in other words, it's a PPB */
  1393. count = 2;
  1394. } else {
  1395. count = 6;
  1396. }
  1397. for (i = 0; i < count; i++) {
  1398. if (cur_func->io[i]) {
  1399. debug("io[%d] exists\n", i);
  1400. if (the_end > 0)
  1401. ibmphp_remove_resource(cur_func->io[i]);
  1402. cur_func->io[i] = NULL;
  1403. }
  1404. if (cur_func->mem[i]) {
  1405. debug("mem[%d] exists\n", i);
  1406. if (the_end > 0)
  1407. ibmphp_remove_resource(cur_func->mem[i]);
  1408. cur_func->mem[i] = NULL;
  1409. }
  1410. if (cur_func->pfmem[i]) {
  1411. debug("pfmem[%d] exists\n", i);
  1412. if (the_end > 0)
  1413. ibmphp_remove_resource(cur_func->pfmem[i]);
  1414. cur_func->pfmem[i] = NULL;
  1415. }
  1416. }
  1417. temp_func = cur_func->next;
  1418. kfree(cur_func);
  1419. cur_func = temp_func;
  1420. }
  1421. }
  1422. sl->func = NULL;
  1423. *slot_cur = sl;
  1424. debug("%s - exit\n", __func__);
  1425. return 0;
  1426. }
  1427. /*
  1428. * add a new bus resulting from hot-plugging a PPB bridge with devices
  1429. *
  1430. * Input: bus and the amount of resources needed (we know we can assign those,
  1431. * since they've been checked already
  1432. * Output: bus added to the correct spot
  1433. * 0, -1, error
  1434. */
  1435. static int add_new_bus(struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
  1436. {
  1437. struct range_node *io_range = NULL;
  1438. struct range_node *mem_range = NULL;
  1439. struct range_node *pfmem_range = NULL;
  1440. struct bus_node *cur_bus = NULL;
  1441. /* Trying to find the parent bus number */
  1442. if (parent_busno != 0xFF) {
  1443. cur_bus = ibmphp_find_res_bus(parent_busno);
  1444. if (!cur_bus) {
  1445. err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
  1446. return -ENODEV;
  1447. }
  1448. list_add(&bus->bus_list, &cur_bus->bus_list);
  1449. }
  1450. if (io) {
  1451. io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
  1452. if (!io_range) {
  1453. err("out of system memory\n");
  1454. return -ENOMEM;
  1455. }
  1456. io_range->start = io->start;
  1457. io_range->end = io->end;
  1458. io_range->rangeno = 1;
  1459. bus->noIORanges = 1;
  1460. bus->rangeIO = io_range;
  1461. }
  1462. if (mem) {
  1463. mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
  1464. if (!mem_range) {
  1465. err("out of system memory\n");
  1466. return -ENOMEM;
  1467. }
  1468. mem_range->start = mem->start;
  1469. mem_range->end = mem->end;
  1470. mem_range->rangeno = 1;
  1471. bus->noMemRanges = 1;
  1472. bus->rangeMem = mem_range;
  1473. }
  1474. if (pfmem) {
  1475. pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
  1476. if (!pfmem_range) {
  1477. err("out of system memory\n");
  1478. return -ENOMEM;
  1479. }
  1480. pfmem_range->start = pfmem->start;
  1481. pfmem_range->end = pfmem->end;
  1482. pfmem_range->rangeno = 1;
  1483. bus->noPFMemRanges = 1;
  1484. bus->rangePFMem = pfmem_range;
  1485. }
  1486. return 0;
  1487. }
  1488. /*
  1489. * find the 1st available bus number for PPB to set as its secondary bus
  1490. * Parameters: bus_number of the primary bus
  1491. * Returns: bus_number of the secondary bus or 0xff in case of failure
  1492. */
  1493. static u8 find_sec_number(u8 primary_busno, u8 slotno)
  1494. {
  1495. int min, max;
  1496. u8 busno;
  1497. struct bus_info *bus;
  1498. struct bus_node *bus_cur;
  1499. bus = ibmphp_find_same_bus_num(primary_busno);
  1500. if (!bus) {
  1501. err("cannot get slot range of the bus from the BIOS\n");
  1502. return 0xff;
  1503. }
  1504. max = bus->slot_max;
  1505. min = bus->slot_min;
  1506. if ((slotno > max) || (slotno < min)) {
  1507. err("got the wrong range\n");
  1508. return 0xff;
  1509. }
  1510. busno = (u8) (slotno - (u8) min);
  1511. busno += primary_busno + 0x01;
  1512. bus_cur = ibmphp_find_res_bus(busno);
  1513. /* either there is no such bus number, or there are no ranges, which
  1514. * can only happen if we removed the bridged device in previous load
  1515. * of the driver, and now only have the skeleton bus struct
  1516. */
  1517. if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
  1518. return busno;
  1519. return 0xff;
  1520. }