ibmphp_pci.c 56 KB

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