ibmphp_pci.c 56 KB

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