ibmphp_pci.c 54 KB

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