ibmphp_pci.c 57 KB

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