ibmphp_res.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. /*
  2. * IBM Hot Plug Controller Driver
  3. *
  4. * Written By: Irene Zubarev, IBM Corporation
  5. *
  6. * Copyright (C) 2001 Greg Kroah-Hartman ([email protected])
  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 <[email protected]>
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/pci.h>
  32. #include <linux/list.h>
  33. #include <linux/init.h>
  34. #include "ibmphp.h"
  35. static int flags = 0; /* for testing */
  36. static void update_resources(struct bus_node *bus_cur, int type, int rangeno);
  37. static int once_over(void);
  38. static int remove_ranges(struct bus_node *, struct bus_node *);
  39. static int update_bridge_ranges(struct bus_node **);
  40. static int add_bus_range(int type, struct range_node *, struct bus_node *);
  41. static void fix_resources(struct bus_node *);
  42. static struct bus_node *find_bus_wprev(u8, struct bus_node **, u8);
  43. static LIST_HEAD(gbuses);
  44. static struct bus_node * __init alloc_error_bus(struct ebda_pci_rsrc *curr, u8 busno, int flag)
  45. {
  46. struct bus_node *newbus;
  47. if (!(curr) && !(flag)) {
  48. err("NULL pointer passed\n");
  49. return NULL;
  50. }
  51. newbus = kzalloc(sizeof(struct bus_node), GFP_KERNEL);
  52. if (!newbus) {
  53. err("out of system memory\n");
  54. return NULL;
  55. }
  56. if (flag)
  57. newbus->busno = busno;
  58. else
  59. newbus->busno = curr->bus_num;
  60. list_add_tail(&newbus->bus_list, &gbuses);
  61. return newbus;
  62. }
  63. static struct resource_node * __init alloc_resources(struct ebda_pci_rsrc *curr)
  64. {
  65. struct resource_node *rs;
  66. if (!curr) {
  67. err("NULL passed to allocate\n");
  68. return NULL;
  69. }
  70. rs = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  71. if (!rs) {
  72. err("out of system memory\n");
  73. return NULL;
  74. }
  75. rs->busno = curr->bus_num;
  76. rs->devfunc = curr->dev_fun;
  77. rs->start = curr->start_addr;
  78. rs->end = curr->end_addr;
  79. rs->len = curr->end_addr - curr->start_addr + 1;
  80. return rs;
  81. }
  82. static int __init alloc_bus_range(struct bus_node **new_bus, struct range_node **new_range, struct ebda_pci_rsrc *curr, int flag, u8 first_bus)
  83. {
  84. struct bus_node *newbus;
  85. struct range_node *newrange;
  86. u8 num_ranges = 0;
  87. if (first_bus) {
  88. newbus = kzalloc(sizeof(struct bus_node), GFP_KERNEL);
  89. if (!newbus) {
  90. err("out of system memory.\n");
  91. return -ENOMEM;
  92. }
  93. newbus->busno = curr->bus_num;
  94. } else {
  95. newbus = *new_bus;
  96. switch (flag) {
  97. case MEM:
  98. num_ranges = newbus->noMemRanges;
  99. break;
  100. case PFMEM:
  101. num_ranges = newbus->noPFMemRanges;
  102. break;
  103. case IO:
  104. num_ranges = newbus->noIORanges;
  105. break;
  106. }
  107. }
  108. newrange = kzalloc(sizeof(struct range_node), GFP_KERNEL);
  109. if (!newrange) {
  110. if (first_bus)
  111. kfree(newbus);
  112. err("out of system memory\n");
  113. return -ENOMEM;
  114. }
  115. newrange->start = curr->start_addr;
  116. newrange->end = curr->end_addr;
  117. if (first_bus || (!num_ranges))
  118. newrange->rangeno = 1;
  119. else {
  120. /* need to insert our range */
  121. add_bus_range(flag, newrange, newbus);
  122. debug("%d resource Primary Bus inserted on bus %x [%x - %x]\n", flag, newbus->busno, newrange->start, newrange->end);
  123. }
  124. switch (flag) {
  125. case MEM:
  126. newbus->rangeMem = newrange;
  127. if (first_bus)
  128. newbus->noMemRanges = 1;
  129. else {
  130. debug("First Memory Primary on bus %x, [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  131. ++newbus->noMemRanges;
  132. fix_resources(newbus);
  133. }
  134. break;
  135. case IO:
  136. newbus->rangeIO = newrange;
  137. if (first_bus)
  138. newbus->noIORanges = 1;
  139. else {
  140. debug("First IO Primary on bus %x, [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  141. ++newbus->noIORanges;
  142. fix_resources(newbus);
  143. }
  144. break;
  145. case PFMEM:
  146. newbus->rangePFMem = newrange;
  147. if (first_bus)
  148. newbus->noPFMemRanges = 1;
  149. else {
  150. debug("1st PFMemory Primary on Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  151. ++newbus->noPFMemRanges;
  152. fix_resources(newbus);
  153. }
  154. break;
  155. }
  156. *new_bus = newbus;
  157. *new_range = newrange;
  158. return 0;
  159. }
  160. /* Notes:
  161. * 1. The ranges are ordered. The buses are not ordered. (First come)
  162. *
  163. * 2. If cannot allocate out of PFMem range, allocate from Mem ranges. PFmemFromMem
  164. * are not sorted. (no need since use mem node). To not change the entire code, we
  165. * also add mem node whenever this case happens so as not to change
  166. * ibmphp_check_mem_resource etc(and since it really is taking Mem resource)
  167. */
  168. /*****************************************************************************
  169. * This is the Resource Management initialization function. It will go through
  170. * the Resource list taken from EBDA and fill in this module's data structures
  171. *
  172. * THIS IS NOT TAKING INTO CONSIDERATION IO RESTRICTIONS OF PRIMARY BUSES,
  173. * SINCE WE'RE GOING TO ASSUME FOR NOW WE DON'T HAVE THOSE ON OUR BUSES FOR NOW
  174. *
  175. * Input: ptr to the head of the resource list from EBDA
  176. * Output: 0, -1 or error codes
  177. ***************************************************************************/
  178. int __init ibmphp_rsrc_init(void)
  179. {
  180. struct ebda_pci_rsrc *curr;
  181. struct range_node *newrange = NULL;
  182. struct bus_node *newbus = NULL;
  183. struct bus_node *bus_cur;
  184. struct bus_node *bus_prev;
  185. struct resource_node *new_io = NULL;
  186. struct resource_node *new_mem = NULL;
  187. struct resource_node *new_pfmem = NULL;
  188. int rc;
  189. list_for_each_entry(curr, &ibmphp_ebda_pci_rsrc_head,
  190. ebda_pci_rsrc_list) {
  191. if (!(curr->rsrc_type & PCIDEVMASK)) {
  192. /* EBDA still lists non PCI devices, so ignore... */
  193. debug("this is not a PCI DEVICE in rsrc_init, please take care\n");
  194. // continue;
  195. }
  196. /* this is a primary bus resource */
  197. if (curr->rsrc_type & PRIMARYBUSMASK) {
  198. /* memory */
  199. if ((curr->rsrc_type & RESTYPE) == MMASK) {
  200. /* no bus structure exists in place yet */
  201. if (list_empty(&gbuses)) {
  202. rc = alloc_bus_range(&newbus, &newrange, curr, MEM, 1);
  203. if (rc)
  204. return rc;
  205. list_add_tail(&newbus->bus_list, &gbuses);
  206. debug("gbuses = NULL, Memory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  207. } else {
  208. bus_cur = find_bus_wprev(curr->bus_num, &bus_prev, 1);
  209. /* found our bus */
  210. if (bus_cur) {
  211. rc = alloc_bus_range(&bus_cur, &newrange, curr, MEM, 0);
  212. if (rc)
  213. return rc;
  214. } else {
  215. /* went through all the buses and didn't find ours, need to create a new bus node */
  216. rc = alloc_bus_range(&newbus, &newrange, curr, MEM, 1);
  217. if (rc)
  218. return rc;
  219. list_add_tail(&newbus->bus_list, &gbuses);
  220. debug("New Bus, Memory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  221. }
  222. }
  223. } else if ((curr->rsrc_type & RESTYPE) == PFMASK) {
  224. /* prefetchable memory */
  225. if (list_empty(&gbuses)) {
  226. /* no bus structure exists in place yet */
  227. rc = alloc_bus_range(&newbus, &newrange, curr, PFMEM, 1);
  228. if (rc)
  229. return rc;
  230. list_add_tail(&newbus->bus_list, &gbuses);
  231. debug("gbuses = NULL, PFMemory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  232. } else {
  233. bus_cur = find_bus_wprev(curr->bus_num, &bus_prev, 1);
  234. if (bus_cur) {
  235. /* found our bus */
  236. rc = alloc_bus_range(&bus_cur, &newrange, curr, PFMEM, 0);
  237. if (rc)
  238. return rc;
  239. } else {
  240. /* went through all the buses and didn't find ours, need to create a new bus node */
  241. rc = alloc_bus_range(&newbus, &newrange, curr, PFMEM, 1);
  242. if (rc)
  243. return rc;
  244. list_add_tail(&newbus->bus_list, &gbuses);
  245. debug("1st Bus, PFMemory Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  246. }
  247. }
  248. } else if ((curr->rsrc_type & RESTYPE) == IOMASK) {
  249. /* IO */
  250. if (list_empty(&gbuses)) {
  251. /* no bus structure exists in place yet */
  252. rc = alloc_bus_range(&newbus, &newrange, curr, IO, 1);
  253. if (rc)
  254. return rc;
  255. list_add_tail(&newbus->bus_list, &gbuses);
  256. debug("gbuses = NULL, IO Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  257. } else {
  258. bus_cur = find_bus_wprev(curr->bus_num, &bus_prev, 1);
  259. if (bus_cur) {
  260. rc = alloc_bus_range(&bus_cur, &newrange, curr, IO, 0);
  261. if (rc)
  262. return rc;
  263. } else {
  264. /* went through all the buses and didn't find ours, need to create a new bus node */
  265. rc = alloc_bus_range(&newbus, &newrange, curr, IO, 1);
  266. if (rc)
  267. return rc;
  268. list_add_tail(&newbus->bus_list, &gbuses);
  269. debug("1st Bus, IO Primary Bus %x [%x - %x]\n", newbus->busno, newrange->start, newrange->end);
  270. }
  271. }
  272. } else {
  273. ; /* type is reserved WHAT TO DO IN THIS CASE???
  274. NOTHING TO DO??? */
  275. }
  276. } else {
  277. /* regular pci device resource */
  278. if ((curr->rsrc_type & RESTYPE) == MMASK) {
  279. /* Memory resource */
  280. new_mem = alloc_resources(curr);
  281. if (!new_mem)
  282. return -ENOMEM;
  283. new_mem->type = MEM;
  284. /*
  285. * if it didn't find the bus, means PCI dev
  286. * came b4 the Primary Bus info, so need to
  287. * create a bus rangeno becomes a problem...
  288. * assign a -1 and then update once the range
  289. * actually appears...
  290. */
  291. if (ibmphp_add_resource(new_mem) < 0) {
  292. newbus = alloc_error_bus(curr, 0, 0);
  293. if (!newbus)
  294. return -ENOMEM;
  295. newbus->firstMem = new_mem;
  296. ++newbus->needMemUpdate;
  297. new_mem->rangeno = -1;
  298. }
  299. debug("Memory resource for device %x, bus %x, [%x - %x]\n", new_mem->devfunc, new_mem->busno, new_mem->start, new_mem->end);
  300. } else if ((curr->rsrc_type & RESTYPE) == PFMASK) {
  301. /* PFMemory resource */
  302. new_pfmem = alloc_resources(curr);
  303. if (!new_pfmem)
  304. return -ENOMEM;
  305. new_pfmem->type = PFMEM;
  306. new_pfmem->fromMem = 0;
  307. if (ibmphp_add_resource(new_pfmem) < 0) {
  308. newbus = alloc_error_bus(curr, 0, 0);
  309. if (!newbus)
  310. return -ENOMEM;
  311. newbus->firstPFMem = new_pfmem;
  312. ++newbus->needPFMemUpdate;
  313. new_pfmem->rangeno = -1;
  314. }
  315. debug("PFMemory resource for device %x, bus %x, [%x - %x]\n", new_pfmem->devfunc, new_pfmem->busno, new_pfmem->start, new_pfmem->end);
  316. } else if ((curr->rsrc_type & RESTYPE) == IOMASK) {
  317. /* IO resource */
  318. new_io = alloc_resources(curr);
  319. if (!new_io)
  320. return -ENOMEM;
  321. new_io->type = IO;
  322. /*
  323. * if it didn't find the bus, means PCI dev
  324. * came b4 the Primary Bus info, so need to
  325. * create a bus rangeno becomes a problem...
  326. * Can assign a -1 and then update once the
  327. * range actually appears...
  328. */
  329. if (ibmphp_add_resource(new_io) < 0) {
  330. newbus = alloc_error_bus(curr, 0, 0);
  331. if (!newbus)
  332. return -ENOMEM;
  333. newbus->firstIO = new_io;
  334. ++newbus->needIOUpdate;
  335. new_io->rangeno = -1;
  336. }
  337. debug("IO resource for device %x, bus %x, [%x - %x]\n", new_io->devfunc, new_io->busno, new_io->start, new_io->end);
  338. }
  339. }
  340. }
  341. list_for_each_entry(bus_cur, &gbuses, bus_list) {
  342. /* This is to get info about PPB resources, since EBDA doesn't put this info into the primary bus info */
  343. rc = update_bridge_ranges(&bus_cur);
  344. if (rc)
  345. return rc;
  346. }
  347. return once_over(); /* This is to align ranges (so no -1) */
  348. }
  349. /********************************************************************************
  350. * This function adds a range into a sorted list of ranges per bus for a particular
  351. * range type, it then calls another routine to update the range numbers on the
  352. * pci devices' resources for the appropriate resource
  353. *
  354. * Input: type of the resource, range to add, current bus
  355. * Output: 0 or -1, bus and range ptrs
  356. ********************************************************************************/
  357. static int add_bus_range(int type, struct range_node *range, struct bus_node *bus_cur)
  358. {
  359. struct range_node *range_cur = NULL;
  360. struct range_node *range_prev;
  361. int count = 0, i_init;
  362. int noRanges = 0;
  363. switch (type) {
  364. case MEM:
  365. range_cur = bus_cur->rangeMem;
  366. noRanges = bus_cur->noMemRanges;
  367. break;
  368. case PFMEM:
  369. range_cur = bus_cur->rangePFMem;
  370. noRanges = bus_cur->noPFMemRanges;
  371. break;
  372. case IO:
  373. range_cur = bus_cur->rangeIO;
  374. noRanges = bus_cur->noIORanges;
  375. break;
  376. }
  377. range_prev = NULL;
  378. while (range_cur) {
  379. if (range->start < range_cur->start)
  380. break;
  381. range_prev = range_cur;
  382. range_cur = range_cur->next;
  383. count = count + 1;
  384. }
  385. if (!count) {
  386. /* our range will go at the beginning of the list */
  387. switch (type) {
  388. case MEM:
  389. bus_cur->rangeMem = range;
  390. break;
  391. case PFMEM:
  392. bus_cur->rangePFMem = range;
  393. break;
  394. case IO:
  395. bus_cur->rangeIO = range;
  396. break;
  397. }
  398. range->next = range_cur;
  399. range->rangeno = 1;
  400. i_init = 0;
  401. } else if (!range_cur) {
  402. /* our range will go at the end of the list */
  403. range->next = NULL;
  404. range_prev->next = range;
  405. range->rangeno = range_prev->rangeno + 1;
  406. return 0;
  407. } else {
  408. /* the range is in the middle */
  409. range_prev->next = range;
  410. range->next = range_cur;
  411. range->rangeno = range_cur->rangeno;
  412. i_init = range_prev->rangeno;
  413. }
  414. for (count = i_init; count < noRanges; ++count) {
  415. ++range_cur->rangeno;
  416. range_cur = range_cur->next;
  417. }
  418. update_resources(bus_cur, type, i_init + 1);
  419. return 0;
  420. }
  421. /*******************************************************************************
  422. * This routine goes through the list of resources of type 'type' and updates
  423. * the range numbers that they correspond to. It was called from add_bus_range fnc
  424. *
  425. * Input: bus, type of the resource, the rangeno starting from which to update
  426. ******************************************************************************/
  427. static void update_resources(struct bus_node *bus_cur, int type, int rangeno)
  428. {
  429. struct resource_node *res = NULL;
  430. u8 eol = 0; /* end of list indicator */
  431. switch (type) {
  432. case MEM:
  433. if (bus_cur->firstMem)
  434. res = bus_cur->firstMem;
  435. break;
  436. case PFMEM:
  437. if (bus_cur->firstPFMem)
  438. res = bus_cur->firstPFMem;
  439. break;
  440. case IO:
  441. if (bus_cur->firstIO)
  442. res = bus_cur->firstIO;
  443. break;
  444. }
  445. if (res) {
  446. while (res) {
  447. if (res->rangeno == rangeno)
  448. break;
  449. if (res->next)
  450. res = res->next;
  451. else if (res->nextRange)
  452. res = res->nextRange;
  453. else {
  454. eol = 1;
  455. break;
  456. }
  457. }
  458. if (!eol) {
  459. /* found the range */
  460. while (res) {
  461. ++res->rangeno;
  462. res = res->next;
  463. }
  464. }
  465. }
  466. }
  467. static void fix_me(struct resource_node *res, struct bus_node *bus_cur, struct range_node *range)
  468. {
  469. char *str = "";
  470. switch (res->type) {
  471. case IO:
  472. str = "io";
  473. break;
  474. case MEM:
  475. str = "mem";
  476. break;
  477. case PFMEM:
  478. str = "pfmem";
  479. break;
  480. }
  481. while (res) {
  482. if (res->rangeno == -1) {
  483. while (range) {
  484. if ((res->start >= range->start) && (res->end <= range->end)) {
  485. res->rangeno = range->rangeno;
  486. debug("%s->rangeno in fix_resources is %d\n", str, res->rangeno);
  487. switch (res->type) {
  488. case IO:
  489. --bus_cur->needIOUpdate;
  490. break;
  491. case MEM:
  492. --bus_cur->needMemUpdate;
  493. break;
  494. case PFMEM:
  495. --bus_cur->needPFMemUpdate;
  496. break;
  497. }
  498. break;
  499. }
  500. range = range->next;
  501. }
  502. }
  503. if (res->next)
  504. res = res->next;
  505. else
  506. res = res->nextRange;
  507. }
  508. }
  509. /*****************************************************************************
  510. * This routine reassigns the range numbers to the resources that had a -1
  511. * This case can happen only if upon initialization, resources taken by pci dev
  512. * appear in EBDA before the resources allocated for that bus, since we don't
  513. * know the range, we assign -1, and this routine is called after a new range
  514. * is assigned to see the resources with unknown range belong to the added range
  515. *
  516. * Input: current bus
  517. * Output: none, list of resources for that bus are fixed if can be
  518. *******************************************************************************/
  519. static void fix_resources(struct bus_node *bus_cur)
  520. {
  521. struct range_node *range;
  522. struct resource_node *res;
  523. debug("%s - bus_cur->busno = %d\n", __func__, bus_cur->busno);
  524. if (bus_cur->needIOUpdate) {
  525. res = bus_cur->firstIO;
  526. range = bus_cur->rangeIO;
  527. fix_me(res, bus_cur, range);
  528. }
  529. if (bus_cur->needMemUpdate) {
  530. res = bus_cur->firstMem;
  531. range = bus_cur->rangeMem;
  532. fix_me(res, bus_cur, range);
  533. }
  534. if (bus_cur->needPFMemUpdate) {
  535. res = bus_cur->firstPFMem;
  536. range = bus_cur->rangePFMem;
  537. fix_me(res, bus_cur, range);
  538. }
  539. }
  540. /*******************************************************************************
  541. * This routine adds a resource to the list of resources to the appropriate bus
  542. * based on their resource type and sorted by their starting addresses. It assigns
  543. * the ptrs to next and nextRange if needed.
  544. *
  545. * Input: resource ptr
  546. * Output: ptrs assigned (to the node)
  547. * 0 or -1
  548. *******************************************************************************/
  549. int ibmphp_add_resource(struct resource_node *res)
  550. {
  551. struct resource_node *res_cur;
  552. struct resource_node *res_prev;
  553. struct bus_node *bus_cur;
  554. struct range_node *range_cur = NULL;
  555. struct resource_node *res_start = NULL;
  556. debug("%s - enter\n", __func__);
  557. if (!res) {
  558. err("NULL passed to add\n");
  559. return -ENODEV;
  560. }
  561. bus_cur = find_bus_wprev(res->busno, NULL, 0);
  562. if (!bus_cur) {
  563. /* didn't find a bus, something's wrong!!! */
  564. debug("no bus in the system, either pci_dev's wrong or allocation failed\n");
  565. return -ENODEV;
  566. }
  567. /* Normal case */
  568. switch (res->type) {
  569. case IO:
  570. range_cur = bus_cur->rangeIO;
  571. res_start = bus_cur->firstIO;
  572. break;
  573. case MEM:
  574. range_cur = bus_cur->rangeMem;
  575. res_start = bus_cur->firstMem;
  576. break;
  577. case PFMEM:
  578. range_cur = bus_cur->rangePFMem;
  579. res_start = bus_cur->firstPFMem;
  580. break;
  581. default:
  582. err("cannot read the type of the resource to add... problem\n");
  583. return -EINVAL;
  584. }
  585. while (range_cur) {
  586. if ((res->start >= range_cur->start) && (res->end <= range_cur->end)) {
  587. res->rangeno = range_cur->rangeno;
  588. break;
  589. }
  590. range_cur = range_cur->next;
  591. }
  592. /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  593. * this is again the case of rangeno = -1
  594. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  595. */
  596. if (!range_cur) {
  597. switch (res->type) {
  598. case IO:
  599. ++bus_cur->needIOUpdate;
  600. break;
  601. case MEM:
  602. ++bus_cur->needMemUpdate;
  603. break;
  604. case PFMEM:
  605. ++bus_cur->needPFMemUpdate;
  606. break;
  607. }
  608. res->rangeno = -1;
  609. }
  610. debug("The range is %d\n", res->rangeno);
  611. if (!res_start) {
  612. /* no first{IO,Mem,Pfmem} on the bus, 1st IO/Mem/Pfmem resource ever */
  613. switch (res->type) {
  614. case IO:
  615. bus_cur->firstIO = res;
  616. break;
  617. case MEM:
  618. bus_cur->firstMem = res;
  619. break;
  620. case PFMEM:
  621. bus_cur->firstPFMem = res;
  622. break;
  623. }
  624. res->next = NULL;
  625. res->nextRange = NULL;
  626. } else {
  627. res_cur = res_start;
  628. res_prev = NULL;
  629. debug("res_cur->rangeno is %d\n", res_cur->rangeno);
  630. while (res_cur) {
  631. if (res_cur->rangeno >= res->rangeno)
  632. break;
  633. res_prev = res_cur;
  634. if (res_cur->next)
  635. res_cur = res_cur->next;
  636. else
  637. res_cur = res_cur->nextRange;
  638. }
  639. if (!res_cur) {
  640. /* at the end of the resource list */
  641. debug("i should be here, [%x - %x]\n", res->start, res->end);
  642. res_prev->nextRange = res;
  643. res->next = NULL;
  644. res->nextRange = NULL;
  645. } else if (res_cur->rangeno == res->rangeno) {
  646. /* in the same range */
  647. while (res_cur) {
  648. if (res->start < res_cur->start)
  649. break;
  650. res_prev = res_cur;
  651. res_cur = res_cur->next;
  652. }
  653. if (!res_cur) {
  654. /* the last resource in this range */
  655. res_prev->next = res;
  656. res->next = NULL;
  657. res->nextRange = res_prev->nextRange;
  658. res_prev->nextRange = NULL;
  659. } else if (res->start < res_cur->start) {
  660. /* at the beginning or middle of the range */
  661. if (!res_prev) {
  662. switch (res->type) {
  663. case IO:
  664. bus_cur->firstIO = res;
  665. break;
  666. case MEM:
  667. bus_cur->firstMem = res;
  668. break;
  669. case PFMEM:
  670. bus_cur->firstPFMem = res;
  671. break;
  672. }
  673. } else if (res_prev->rangeno == res_cur->rangeno)
  674. res_prev->next = res;
  675. else
  676. res_prev->nextRange = res;
  677. res->next = res_cur;
  678. res->nextRange = NULL;
  679. }
  680. } else {
  681. /* this is the case where it is 1st occurrence of the range */
  682. if (!res_prev) {
  683. /* at the beginning of the resource list */
  684. res->next = NULL;
  685. switch (res->type) {
  686. case IO:
  687. res->nextRange = bus_cur->firstIO;
  688. bus_cur->firstIO = res;
  689. break;
  690. case MEM:
  691. res->nextRange = bus_cur->firstMem;
  692. bus_cur->firstMem = res;
  693. break;
  694. case PFMEM:
  695. res->nextRange = bus_cur->firstPFMem;
  696. bus_cur->firstPFMem = res;
  697. break;
  698. }
  699. } else if (res_cur->rangeno > res->rangeno) {
  700. /* in the middle of the resource list */
  701. res_prev->nextRange = res;
  702. res->next = NULL;
  703. res->nextRange = res_cur;
  704. }
  705. }
  706. }
  707. debug("%s - exit\n", __func__);
  708. return 0;
  709. }
  710. /****************************************************************************
  711. * This routine will remove the resource from the list of resources
  712. *
  713. * Input: io, mem, and/or pfmem resource to be deleted
  714. * Output: modified resource list
  715. * 0 or error code
  716. ****************************************************************************/
  717. int ibmphp_remove_resource(struct resource_node *res)
  718. {
  719. struct bus_node *bus_cur;
  720. struct resource_node *res_cur = NULL;
  721. struct resource_node *res_prev;
  722. struct resource_node *mem_cur;
  723. char *type = "";
  724. if (!res) {
  725. err("resource to remove is NULL\n");
  726. return -ENODEV;
  727. }
  728. bus_cur = find_bus_wprev(res->busno, NULL, 0);
  729. if (!bus_cur) {
  730. err("cannot find corresponding bus of the io resource to remove bailing out...\n");
  731. return -ENODEV;
  732. }
  733. switch (res->type) {
  734. case IO:
  735. res_cur = bus_cur->firstIO;
  736. type = "io";
  737. break;
  738. case MEM:
  739. res_cur = bus_cur->firstMem;
  740. type = "mem";
  741. break;
  742. case PFMEM:
  743. res_cur = bus_cur->firstPFMem;
  744. type = "pfmem";
  745. break;
  746. default:
  747. err("unknown type for resource to remove\n");
  748. return -EINVAL;
  749. }
  750. res_prev = NULL;
  751. while (res_cur) {
  752. if ((res_cur->start == res->start) && (res_cur->end == res->end))
  753. break;
  754. res_prev = res_cur;
  755. if (res_cur->next)
  756. res_cur = res_cur->next;
  757. else
  758. res_cur = res_cur->nextRange;
  759. }
  760. if (!res_cur) {
  761. if (res->type == PFMEM) {
  762. /*
  763. * case where pfmem might be in the PFMemFromMem list
  764. * so will also need to remove the corresponding mem
  765. * entry
  766. */
  767. res_cur = bus_cur->firstPFMemFromMem;
  768. res_prev = NULL;
  769. while (res_cur) {
  770. if ((res_cur->start == res->start) && (res_cur->end == res->end)) {
  771. mem_cur = bus_cur->firstMem;
  772. while (mem_cur) {
  773. if ((mem_cur->start == res_cur->start)
  774. && (mem_cur->end == res_cur->end))
  775. break;
  776. if (mem_cur->next)
  777. mem_cur = mem_cur->next;
  778. else
  779. mem_cur = mem_cur->nextRange;
  780. }
  781. if (!mem_cur) {
  782. err("cannot find corresponding mem node for pfmem...\n");
  783. return -EINVAL;
  784. }
  785. ibmphp_remove_resource(mem_cur);
  786. if (!res_prev)
  787. bus_cur->firstPFMemFromMem = res_cur->next;
  788. else
  789. res_prev->next = res_cur->next;
  790. kfree(res_cur);
  791. return 0;
  792. }
  793. res_prev = res_cur;
  794. if (res_cur->next)
  795. res_cur = res_cur->next;
  796. else
  797. res_cur = res_cur->nextRange;
  798. }
  799. if (!res_cur) {
  800. err("cannot find pfmem to delete...\n");
  801. return -EINVAL;
  802. }
  803. } else {
  804. err("the %s resource is not in the list to be deleted...\n", type);
  805. return -EINVAL;
  806. }
  807. }
  808. if (!res_prev) {
  809. /* first device to be deleted */
  810. if (res_cur->next) {
  811. switch (res->type) {
  812. case IO:
  813. bus_cur->firstIO = res_cur->next;
  814. break;
  815. case MEM:
  816. bus_cur->firstMem = res_cur->next;
  817. break;
  818. case PFMEM:
  819. bus_cur->firstPFMem = res_cur->next;
  820. break;
  821. }
  822. } else if (res_cur->nextRange) {
  823. switch (res->type) {
  824. case IO:
  825. bus_cur->firstIO = res_cur->nextRange;
  826. break;
  827. case MEM:
  828. bus_cur->firstMem = res_cur->nextRange;
  829. break;
  830. case PFMEM:
  831. bus_cur->firstPFMem = res_cur->nextRange;
  832. break;
  833. }
  834. } else {
  835. switch (res->type) {
  836. case IO:
  837. bus_cur->firstIO = NULL;
  838. break;
  839. case MEM:
  840. bus_cur->firstMem = NULL;
  841. break;
  842. case PFMEM:
  843. bus_cur->firstPFMem = NULL;
  844. break;
  845. }
  846. }
  847. kfree(res_cur);
  848. return 0;
  849. } else {
  850. if (res_cur->next) {
  851. if (res_prev->rangeno == res_cur->rangeno)
  852. res_prev->next = res_cur->next;
  853. else
  854. res_prev->nextRange = res_cur->next;
  855. } else if (res_cur->nextRange) {
  856. res_prev->next = NULL;
  857. res_prev->nextRange = res_cur->nextRange;
  858. } else {
  859. res_prev->next = NULL;
  860. res_prev->nextRange = NULL;
  861. }
  862. kfree(res_cur);
  863. return 0;
  864. }
  865. return 0;
  866. }
  867. static struct range_node *find_range(struct bus_node *bus_cur, struct resource_node *res)
  868. {
  869. struct range_node *range = NULL;
  870. switch (res->type) {
  871. case IO:
  872. range = bus_cur->rangeIO;
  873. break;
  874. case MEM:
  875. range = bus_cur->rangeMem;
  876. break;
  877. case PFMEM:
  878. range = bus_cur->rangePFMem;
  879. break;
  880. default:
  881. err("cannot read resource type in find_range\n");
  882. }
  883. while (range) {
  884. if (res->rangeno == range->rangeno)
  885. break;
  886. range = range->next;
  887. }
  888. return range;
  889. }
  890. /*****************************************************************************
  891. * This routine will check to make sure the io/mem/pfmem->len that the device asked for
  892. * can fit w/i our list of available IO/MEM/PFMEM resources. If cannot, returns -EINVAL,
  893. * otherwise, returns 0
  894. *
  895. * Input: resource
  896. * Output: the correct start and end address are inputted into the resource node,
  897. * 0 or -EINVAL
  898. *****************************************************************************/
  899. int ibmphp_check_resource(struct resource_node *res, u8 bridge)
  900. {
  901. struct bus_node *bus_cur;
  902. struct range_node *range = NULL;
  903. struct resource_node *res_prev;
  904. struct resource_node *res_cur = NULL;
  905. u32 len_cur = 0, start_cur = 0, len_tmp = 0;
  906. int noranges = 0;
  907. u32 tmp_start; /* this is to make sure start address is divisible by the length needed */
  908. u32 tmp_divide;
  909. u8 flag = 0;
  910. if (!res)
  911. return -EINVAL;
  912. if (bridge) {
  913. /* The rules for bridges are different, 4K divisible for IO, 1M for (pf)mem*/
  914. if (res->type == IO)
  915. tmp_divide = IOBRIDGE;
  916. else
  917. tmp_divide = MEMBRIDGE;
  918. } else
  919. tmp_divide = res->len;
  920. bus_cur = find_bus_wprev(res->busno, NULL, 0);
  921. if (!bus_cur) {
  922. /* didn't find a bus, something's wrong!!! */
  923. debug("no bus in the system, either pci_dev's wrong or allocation failed\n");
  924. return -EINVAL;
  925. }
  926. debug("%s - enter\n", __func__);
  927. debug("bus_cur->busno is %d\n", bus_cur->busno);
  928. /* This is a quick fix to not mess up with the code very much. i.e.,
  929. * 2000-2fff, len = 1000, but when we compare, we need it to be fff */
  930. res->len -= 1;
  931. switch (res->type) {
  932. case IO:
  933. res_cur = bus_cur->firstIO;
  934. noranges = bus_cur->noIORanges;
  935. break;
  936. case MEM:
  937. res_cur = bus_cur->firstMem;
  938. noranges = bus_cur->noMemRanges;
  939. break;
  940. case PFMEM:
  941. res_cur = bus_cur->firstPFMem;
  942. noranges = bus_cur->noPFMemRanges;
  943. break;
  944. default:
  945. err("wrong type of resource to check\n");
  946. return -EINVAL;
  947. }
  948. res_prev = NULL;
  949. while (res_cur) {
  950. range = find_range(bus_cur, res_cur);
  951. debug("%s - rangeno = %d\n", __func__, res_cur->rangeno);
  952. if (!range) {
  953. err("no range for the device exists... bailing out...\n");
  954. return -EINVAL;
  955. }
  956. /* found our range */
  957. if (!res_prev) {
  958. /* first time in the loop */
  959. len_tmp = res_cur->start - 1 - range->start;
  960. if ((res_cur->start != range->start) && (len_tmp >= res->len)) {
  961. debug("len_tmp = %x\n", len_tmp);
  962. if ((len_tmp < len_cur) || (len_cur == 0)) {
  963. if ((range->start % tmp_divide) == 0) {
  964. /* just perfect, starting address is divisible by length */
  965. flag = 1;
  966. len_cur = len_tmp;
  967. start_cur = range->start;
  968. } else {
  969. /* Needs adjusting */
  970. tmp_start = range->start;
  971. flag = 0;
  972. while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
  973. if ((tmp_start % tmp_divide) == 0) {
  974. flag = 1;
  975. len_cur = len_tmp;
  976. start_cur = tmp_start;
  977. break;
  978. }
  979. tmp_start += tmp_divide - tmp_start % tmp_divide;
  980. if (tmp_start >= res_cur->start - 1)
  981. break;
  982. }
  983. }
  984. if (flag && len_cur == res->len) {
  985. debug("but we are not here, right?\n");
  986. res->start = start_cur;
  987. res->len += 1; /* To restore the balance */
  988. res->end = res->start + res->len - 1;
  989. return 0;
  990. }
  991. }
  992. }
  993. }
  994. if (!res_cur->next) {
  995. /* last device on the range */
  996. len_tmp = range->end - (res_cur->end + 1);
  997. if ((range->end != res_cur->end) && (len_tmp >= res->len)) {
  998. debug("len_tmp = %x\n", len_tmp);
  999. if ((len_tmp < len_cur) || (len_cur == 0)) {
  1000. if (((res_cur->end + 1) % tmp_divide) == 0) {
  1001. /* just perfect, starting address is divisible by length */
  1002. flag = 1;
  1003. len_cur = len_tmp;
  1004. start_cur = res_cur->end + 1;
  1005. } else {
  1006. /* Needs adjusting */
  1007. tmp_start = res_cur->end + 1;
  1008. flag = 0;
  1009. while ((len_tmp = range->end - tmp_start) >= res->len) {
  1010. if ((tmp_start % tmp_divide) == 0) {
  1011. flag = 1;
  1012. len_cur = len_tmp;
  1013. start_cur = tmp_start;
  1014. break;
  1015. }
  1016. tmp_start += tmp_divide - tmp_start % tmp_divide;
  1017. if (tmp_start >= range->end)
  1018. break;
  1019. }
  1020. }
  1021. if (flag && len_cur == res->len) {
  1022. res->start = start_cur;
  1023. res->len += 1; /* To restore the balance */
  1024. res->end = res->start + res->len - 1;
  1025. return 0;
  1026. }
  1027. }
  1028. }
  1029. }
  1030. if (res_prev) {
  1031. if (res_prev->rangeno != res_cur->rangeno) {
  1032. /* 1st device on this range */
  1033. len_tmp = res_cur->start - 1 - range->start;
  1034. if ((res_cur->start != range->start) && (len_tmp >= res->len)) {
  1035. if ((len_tmp < len_cur) || (len_cur == 0)) {
  1036. if ((range->start % tmp_divide) == 0) {
  1037. /* just perfect, starting address is divisible by length */
  1038. flag = 1;
  1039. len_cur = len_tmp;
  1040. start_cur = range->start;
  1041. } else {
  1042. /* Needs adjusting */
  1043. tmp_start = range->start;
  1044. flag = 0;
  1045. while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
  1046. if ((tmp_start % tmp_divide) == 0) {
  1047. flag = 1;
  1048. len_cur = len_tmp;
  1049. start_cur = tmp_start;
  1050. break;
  1051. }
  1052. tmp_start += tmp_divide - tmp_start % tmp_divide;
  1053. if (tmp_start >= res_cur->start - 1)
  1054. break;
  1055. }
  1056. }
  1057. if (flag && len_cur == res->len) {
  1058. res->start = start_cur;
  1059. res->len += 1; /* To restore the balance */
  1060. res->end = res->start + res->len - 1;
  1061. return 0;
  1062. }
  1063. }
  1064. }
  1065. } else {
  1066. /* in the same range */
  1067. len_tmp = res_cur->start - 1 - res_prev->end - 1;
  1068. if (len_tmp >= res->len) {
  1069. if ((len_tmp < len_cur) || (len_cur == 0)) {
  1070. if (((res_prev->end + 1) % tmp_divide) == 0) {
  1071. /* just perfect, starting address's divisible by length */
  1072. flag = 1;
  1073. len_cur = len_tmp;
  1074. start_cur = res_prev->end + 1;
  1075. } else {
  1076. /* Needs adjusting */
  1077. tmp_start = res_prev->end + 1;
  1078. flag = 0;
  1079. while ((len_tmp = res_cur->start - 1 - tmp_start) >= res->len) {
  1080. if ((tmp_start % tmp_divide) == 0) {
  1081. flag = 1;
  1082. len_cur = len_tmp;
  1083. start_cur = tmp_start;
  1084. break;
  1085. }
  1086. tmp_start += tmp_divide - tmp_start % tmp_divide;
  1087. if (tmp_start >= res_cur->start - 1)
  1088. break;
  1089. }
  1090. }
  1091. if (flag && len_cur == res->len) {
  1092. res->start = start_cur;
  1093. res->len += 1; /* To restore the balance */
  1094. res->end = res->start + res->len - 1;
  1095. return 0;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. }
  1101. /* end if (res_prev) */
  1102. res_prev = res_cur;
  1103. if (res_cur->next)
  1104. res_cur = res_cur->next;
  1105. else
  1106. res_cur = res_cur->nextRange;
  1107. } /* end of while */
  1108. if (!res_prev) {
  1109. /* 1st device ever */
  1110. /* need to find appropriate range */
  1111. switch (res->type) {
  1112. case IO:
  1113. range = bus_cur->rangeIO;
  1114. break;
  1115. case MEM:
  1116. range = bus_cur->rangeMem;
  1117. break;
  1118. case PFMEM:
  1119. range = bus_cur->rangePFMem;
  1120. break;
  1121. }
  1122. while (range) {
  1123. len_tmp = range->end - range->start;
  1124. if (len_tmp >= res->len) {
  1125. if ((len_tmp < len_cur) || (len_cur == 0)) {
  1126. if ((range->start % tmp_divide) == 0) {
  1127. /* just perfect, starting address's divisible by length */
  1128. flag = 1;
  1129. len_cur = len_tmp;
  1130. start_cur = range->start;
  1131. } else {
  1132. /* Needs adjusting */
  1133. tmp_start = range->start;
  1134. flag = 0;
  1135. while ((len_tmp = range->end - tmp_start) >= res->len) {
  1136. if ((tmp_start % tmp_divide) == 0) {
  1137. flag = 1;
  1138. len_cur = len_tmp;
  1139. start_cur = tmp_start;
  1140. break;
  1141. }
  1142. tmp_start += tmp_divide - tmp_start % tmp_divide;
  1143. if (tmp_start >= range->end)
  1144. break;
  1145. }
  1146. }
  1147. if (flag && len_cur == res->len) {
  1148. res->start = start_cur;
  1149. res->len += 1; /* To restore the balance */
  1150. res->end = res->start + res->len - 1;
  1151. return 0;
  1152. }
  1153. }
  1154. }
  1155. range = range->next;
  1156. } /* end of while */
  1157. if ((!range) && (len_cur == 0)) {
  1158. /* have gone through the list of devices and ranges and haven't found n.e.thing */
  1159. err("no appropriate range.. bailing out...\n");
  1160. return -EINVAL;
  1161. } else if (len_cur) {
  1162. res->start = start_cur;
  1163. res->len += 1; /* To restore the balance */
  1164. res->end = res->start + res->len - 1;
  1165. return 0;
  1166. }
  1167. }
  1168. if (!res_cur) {
  1169. debug("prev->rangeno = %d, noranges = %d\n", res_prev->rangeno, noranges);
  1170. if (res_prev->rangeno < noranges) {
  1171. /* if there're more ranges out there to check */
  1172. switch (res->type) {
  1173. case IO:
  1174. range = bus_cur->rangeIO;
  1175. break;
  1176. case MEM:
  1177. range = bus_cur->rangeMem;
  1178. break;
  1179. case PFMEM:
  1180. range = bus_cur->rangePFMem;
  1181. break;
  1182. }
  1183. while (range) {
  1184. len_tmp = range->end - range->start;
  1185. if (len_tmp >= res->len) {
  1186. if ((len_tmp < len_cur) || (len_cur == 0)) {
  1187. if ((range->start % tmp_divide) == 0) {
  1188. /* just perfect, starting address's divisible by length */
  1189. flag = 1;
  1190. len_cur = len_tmp;
  1191. start_cur = range->start;
  1192. } else {
  1193. /* Needs adjusting */
  1194. tmp_start = range->start;
  1195. flag = 0;
  1196. while ((len_tmp = range->end - tmp_start) >= res->len) {
  1197. if ((tmp_start % tmp_divide) == 0) {
  1198. flag = 1;
  1199. len_cur = len_tmp;
  1200. start_cur = tmp_start;
  1201. break;
  1202. }
  1203. tmp_start += tmp_divide - tmp_start % tmp_divide;
  1204. if (tmp_start >= range->end)
  1205. break;
  1206. }
  1207. }
  1208. if (flag && len_cur == res->len) {
  1209. res->start = start_cur;
  1210. res->len += 1; /* To restore the balance */
  1211. res->end = res->start + res->len - 1;
  1212. return 0;
  1213. }
  1214. }
  1215. }
  1216. range = range->next;
  1217. } /* end of while */
  1218. if ((!range) && (len_cur == 0)) {
  1219. /* have gone through the list of devices and ranges and haven't found n.e.thing */
  1220. err("no appropriate range.. bailing out...\n");
  1221. return -EINVAL;
  1222. } else if (len_cur) {
  1223. res->start = start_cur;
  1224. res->len += 1; /* To restore the balance */
  1225. res->end = res->start + res->len - 1;
  1226. return 0;
  1227. }
  1228. } else {
  1229. /* no more ranges to check on */
  1230. if (len_cur) {
  1231. res->start = start_cur;
  1232. res->len += 1; /* To restore the balance */
  1233. res->end = res->start + res->len - 1;
  1234. return 0;
  1235. } else {
  1236. /* have gone through the list of devices and haven't found n.e.thing */
  1237. err("no appropriate range.. bailing out...\n");
  1238. return -EINVAL;
  1239. }
  1240. }
  1241. } /* end if (!res_cur) */
  1242. return -EINVAL;
  1243. }
  1244. /********************************************************************************
  1245. * This routine is called from remove_card if the card contained PPB.
  1246. * It will remove all the resources on the bus as well as the bus itself
  1247. * Input: Bus
  1248. * Output: 0, -ENODEV
  1249. ********************************************************************************/
  1250. int ibmphp_remove_bus(struct bus_node *bus, u8 parent_busno)
  1251. {
  1252. struct resource_node *res_cur;
  1253. struct resource_node *res_tmp;
  1254. struct bus_node *prev_bus;
  1255. int rc;
  1256. prev_bus = find_bus_wprev(parent_busno, NULL, 0);
  1257. if (!prev_bus) {
  1258. debug("something terribly wrong. Cannot find parent bus to the one to remove\n");
  1259. return -ENODEV;
  1260. }
  1261. debug("In ibmphp_remove_bus... prev_bus->busno is %x\n", prev_bus->busno);
  1262. rc = remove_ranges(bus, prev_bus);
  1263. if (rc)
  1264. return rc;
  1265. if (bus->firstIO) {
  1266. res_cur = bus->firstIO;
  1267. while (res_cur) {
  1268. res_tmp = res_cur;
  1269. if (res_cur->next)
  1270. res_cur = res_cur->next;
  1271. else
  1272. res_cur = res_cur->nextRange;
  1273. kfree(res_tmp);
  1274. res_tmp = NULL;
  1275. }
  1276. bus->firstIO = NULL;
  1277. }
  1278. if (bus->firstMem) {
  1279. res_cur = bus->firstMem;
  1280. while (res_cur) {
  1281. res_tmp = res_cur;
  1282. if (res_cur->next)
  1283. res_cur = res_cur->next;
  1284. else
  1285. res_cur = res_cur->nextRange;
  1286. kfree(res_tmp);
  1287. res_tmp = NULL;
  1288. }
  1289. bus->firstMem = NULL;
  1290. }
  1291. if (bus->firstPFMem) {
  1292. res_cur = bus->firstPFMem;
  1293. while (res_cur) {
  1294. res_tmp = res_cur;
  1295. if (res_cur->next)
  1296. res_cur = res_cur->next;
  1297. else
  1298. res_cur = res_cur->nextRange;
  1299. kfree(res_tmp);
  1300. res_tmp = NULL;
  1301. }
  1302. bus->firstPFMem = NULL;
  1303. }
  1304. if (bus->firstPFMemFromMem) {
  1305. res_cur = bus->firstPFMemFromMem;
  1306. while (res_cur) {
  1307. res_tmp = res_cur;
  1308. res_cur = res_cur->next;
  1309. kfree(res_tmp);
  1310. res_tmp = NULL;
  1311. }
  1312. bus->firstPFMemFromMem = NULL;
  1313. }
  1314. list_del(&bus->bus_list);
  1315. kfree(bus);
  1316. return 0;
  1317. }
  1318. /******************************************************************************
  1319. * This routine deletes the ranges from a given bus, and the entries from the
  1320. * parent's bus in the resources
  1321. * Input: current bus, previous bus
  1322. * Output: 0, -EINVAL
  1323. ******************************************************************************/
  1324. static int remove_ranges(struct bus_node *bus_cur, struct bus_node *bus_prev)
  1325. {
  1326. struct range_node *range_cur;
  1327. struct range_node *range_tmp;
  1328. int i;
  1329. struct resource_node *res = NULL;
  1330. if (bus_cur->noIORanges) {
  1331. range_cur = bus_cur->rangeIO;
  1332. for (i = 0; i < bus_cur->noIORanges; i++) {
  1333. if (ibmphp_find_resource(bus_prev, range_cur->start, &res, IO) < 0)
  1334. return -EINVAL;
  1335. ibmphp_remove_resource(res);
  1336. range_tmp = range_cur;
  1337. range_cur = range_cur->next;
  1338. kfree(range_tmp);
  1339. range_tmp = NULL;
  1340. }
  1341. bus_cur->rangeIO = NULL;
  1342. }
  1343. if (bus_cur->noMemRanges) {
  1344. range_cur = bus_cur->rangeMem;
  1345. for (i = 0; i < bus_cur->noMemRanges; i++) {
  1346. if (ibmphp_find_resource(bus_prev, range_cur->start, &res, MEM) < 0)
  1347. return -EINVAL;
  1348. ibmphp_remove_resource(res);
  1349. range_tmp = range_cur;
  1350. range_cur = range_cur->next;
  1351. kfree(range_tmp);
  1352. range_tmp = NULL;
  1353. }
  1354. bus_cur->rangeMem = NULL;
  1355. }
  1356. if (bus_cur->noPFMemRanges) {
  1357. range_cur = bus_cur->rangePFMem;
  1358. for (i = 0; i < bus_cur->noPFMemRanges; i++) {
  1359. if (ibmphp_find_resource(bus_prev, range_cur->start, &res, PFMEM) < 0)
  1360. return -EINVAL;
  1361. ibmphp_remove_resource(res);
  1362. range_tmp = range_cur;
  1363. range_cur = range_cur->next;
  1364. kfree(range_tmp);
  1365. range_tmp = NULL;
  1366. }
  1367. bus_cur->rangePFMem = NULL;
  1368. }
  1369. return 0;
  1370. }
  1371. /*
  1372. * find the resource node in the bus
  1373. * Input: Resource needed, start address of the resource, type of resource
  1374. */
  1375. int ibmphp_find_resource(struct bus_node *bus, u32 start_address, struct resource_node **res, int flag)
  1376. {
  1377. struct resource_node *res_cur = NULL;
  1378. char *type = "";
  1379. if (!bus) {
  1380. err("The bus passed in NULL to find resource\n");
  1381. return -ENODEV;
  1382. }
  1383. switch (flag) {
  1384. case IO:
  1385. res_cur = bus->firstIO;
  1386. type = "io";
  1387. break;
  1388. case MEM:
  1389. res_cur = bus->firstMem;
  1390. type = "mem";
  1391. break;
  1392. case PFMEM:
  1393. res_cur = bus->firstPFMem;
  1394. type = "pfmem";
  1395. break;
  1396. default:
  1397. err("wrong type of flag\n");
  1398. return -EINVAL;
  1399. }
  1400. while (res_cur) {
  1401. if (res_cur->start == start_address) {
  1402. *res = res_cur;
  1403. break;
  1404. }
  1405. if (res_cur->next)
  1406. res_cur = res_cur->next;
  1407. else
  1408. res_cur = res_cur->nextRange;
  1409. }
  1410. if (!res_cur) {
  1411. if (flag == PFMEM) {
  1412. res_cur = bus->firstPFMemFromMem;
  1413. while (res_cur) {
  1414. if (res_cur->start == start_address) {
  1415. *res = res_cur;
  1416. break;
  1417. }
  1418. res_cur = res_cur->next;
  1419. }
  1420. if (!res_cur) {
  1421. debug("SOS...cannot find %s resource in the bus.\n", type);
  1422. return -EINVAL;
  1423. }
  1424. } else {
  1425. debug("SOS... cannot find %s resource in the bus.\n", type);
  1426. return -EINVAL;
  1427. }
  1428. }
  1429. if (*res)
  1430. debug("*res->start = %x\n", (*res)->start);
  1431. return 0;
  1432. }
  1433. /***********************************************************************
  1434. * This routine will free the resource structures used by the
  1435. * system. It is called from cleanup routine for the module
  1436. * Parameters: none
  1437. * Returns: none
  1438. ***********************************************************************/
  1439. void ibmphp_free_resources(void)
  1440. {
  1441. struct bus_node *bus_cur = NULL, *next;
  1442. struct bus_node *bus_tmp;
  1443. struct range_node *range_cur;
  1444. struct range_node *range_tmp;
  1445. struct resource_node *res_cur;
  1446. struct resource_node *res_tmp;
  1447. int i = 0;
  1448. flags = 1;
  1449. list_for_each_entry_safe(bus_cur, next, &gbuses, bus_list) {
  1450. if (bus_cur->noIORanges) {
  1451. range_cur = bus_cur->rangeIO;
  1452. for (i = 0; i < bus_cur->noIORanges; i++) {
  1453. if (!range_cur)
  1454. break;
  1455. range_tmp = range_cur;
  1456. range_cur = range_cur->next;
  1457. kfree(range_tmp);
  1458. range_tmp = NULL;
  1459. }
  1460. }
  1461. if (bus_cur->noMemRanges) {
  1462. range_cur = bus_cur->rangeMem;
  1463. for (i = 0; i < bus_cur->noMemRanges; i++) {
  1464. if (!range_cur)
  1465. break;
  1466. range_tmp = range_cur;
  1467. range_cur = range_cur->next;
  1468. kfree(range_tmp);
  1469. range_tmp = NULL;
  1470. }
  1471. }
  1472. if (bus_cur->noPFMemRanges) {
  1473. range_cur = bus_cur->rangePFMem;
  1474. for (i = 0; i < bus_cur->noPFMemRanges; i++) {
  1475. if (!range_cur)
  1476. break;
  1477. range_tmp = range_cur;
  1478. range_cur = range_cur->next;
  1479. kfree(range_tmp);
  1480. range_tmp = NULL;
  1481. }
  1482. }
  1483. if (bus_cur->firstIO) {
  1484. res_cur = bus_cur->firstIO;
  1485. while (res_cur) {
  1486. res_tmp = res_cur;
  1487. if (res_cur->next)
  1488. res_cur = res_cur->next;
  1489. else
  1490. res_cur = res_cur->nextRange;
  1491. kfree(res_tmp);
  1492. res_tmp = NULL;
  1493. }
  1494. bus_cur->firstIO = NULL;
  1495. }
  1496. if (bus_cur->firstMem) {
  1497. res_cur = bus_cur->firstMem;
  1498. while (res_cur) {
  1499. res_tmp = res_cur;
  1500. if (res_cur->next)
  1501. res_cur = res_cur->next;
  1502. else
  1503. res_cur = res_cur->nextRange;
  1504. kfree(res_tmp);
  1505. res_tmp = NULL;
  1506. }
  1507. bus_cur->firstMem = NULL;
  1508. }
  1509. if (bus_cur->firstPFMem) {
  1510. res_cur = bus_cur->firstPFMem;
  1511. while (res_cur) {
  1512. res_tmp = res_cur;
  1513. if (res_cur->next)
  1514. res_cur = res_cur->next;
  1515. else
  1516. res_cur = res_cur->nextRange;
  1517. kfree(res_tmp);
  1518. res_tmp = NULL;
  1519. }
  1520. bus_cur->firstPFMem = NULL;
  1521. }
  1522. if (bus_cur->firstPFMemFromMem) {
  1523. res_cur = bus_cur->firstPFMemFromMem;
  1524. while (res_cur) {
  1525. res_tmp = res_cur;
  1526. res_cur = res_cur->next;
  1527. kfree(res_tmp);
  1528. res_tmp = NULL;
  1529. }
  1530. bus_cur->firstPFMemFromMem = NULL;
  1531. }
  1532. bus_tmp = bus_cur;
  1533. list_del(&bus_cur->bus_list);
  1534. kfree(bus_tmp);
  1535. bus_tmp = NULL;
  1536. }
  1537. }
  1538. /*********************************************************************************
  1539. * This function will go over the PFmem resources to check if the EBDA allocated
  1540. * pfmem out of memory buckets of the bus. If so, it will change the range numbers
  1541. * and a flag to indicate that this resource is out of memory. It will also move the
  1542. * Pfmem out of the pfmem resource list to the PFMemFromMem list, and will create
  1543. * a new Mem node
  1544. * This routine is called right after initialization
  1545. *******************************************************************************/
  1546. static int __init once_over(void)
  1547. {
  1548. struct resource_node *pfmem_cur;
  1549. struct resource_node *pfmem_prev;
  1550. struct resource_node *mem;
  1551. struct bus_node *bus_cur;
  1552. list_for_each_entry(bus_cur, &gbuses, bus_list) {
  1553. if ((!bus_cur->rangePFMem) && (bus_cur->firstPFMem)) {
  1554. for (pfmem_cur = bus_cur->firstPFMem, pfmem_prev = NULL; pfmem_cur; pfmem_prev = pfmem_cur, pfmem_cur = pfmem_cur->next) {
  1555. pfmem_cur->fromMem = 1;
  1556. if (pfmem_prev)
  1557. pfmem_prev->next = pfmem_cur->next;
  1558. else
  1559. bus_cur->firstPFMem = pfmem_cur->next;
  1560. if (!bus_cur->firstPFMemFromMem)
  1561. pfmem_cur->next = NULL;
  1562. else
  1563. /* we don't need to sort PFMemFromMem since we're using mem node for
  1564. all the real work anyways, so just insert at the beginning of the
  1565. list
  1566. */
  1567. pfmem_cur->next = bus_cur->firstPFMemFromMem;
  1568. bus_cur->firstPFMemFromMem = pfmem_cur;
  1569. mem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  1570. if (!mem) {
  1571. err("out of system memory\n");
  1572. return -ENOMEM;
  1573. }
  1574. mem->type = MEM;
  1575. mem->busno = pfmem_cur->busno;
  1576. mem->devfunc = pfmem_cur->devfunc;
  1577. mem->start = pfmem_cur->start;
  1578. mem->end = pfmem_cur->end;
  1579. mem->len = pfmem_cur->len;
  1580. if (ibmphp_add_resource(mem) < 0)
  1581. err("Trouble...trouble... EBDA allocated pfmem from mem, but system doesn't display it has this space... unless not PCI device...\n");
  1582. pfmem_cur->rangeno = mem->rangeno;
  1583. } /* end for pfmem */
  1584. } /* end if */
  1585. } /* end list_for_each bus */
  1586. return 0;
  1587. }
  1588. int ibmphp_add_pfmem_from_mem(struct resource_node *pfmem)
  1589. {
  1590. struct bus_node *bus_cur = find_bus_wprev(pfmem->busno, NULL, 0);
  1591. if (!bus_cur) {
  1592. err("cannot find bus of pfmem to add...\n");
  1593. return -ENODEV;
  1594. }
  1595. if (bus_cur->firstPFMemFromMem)
  1596. pfmem->next = bus_cur->firstPFMemFromMem;
  1597. else
  1598. pfmem->next = NULL;
  1599. bus_cur->firstPFMemFromMem = pfmem;
  1600. return 0;
  1601. }
  1602. /* This routine just goes through the buses to see if the bus already exists.
  1603. * It is called from ibmphp_find_sec_number, to find out a secondary bus number for
  1604. * bridged cards
  1605. * Parameters: bus_number
  1606. * Returns: Bus pointer or NULL
  1607. */
  1608. struct bus_node *ibmphp_find_res_bus(u8 bus_number)
  1609. {
  1610. return find_bus_wprev(bus_number, NULL, 0);
  1611. }
  1612. static struct bus_node *find_bus_wprev(u8 bus_number, struct bus_node **prev, u8 flag)
  1613. {
  1614. struct bus_node *bus_cur;
  1615. list_for_each_entry(bus_cur, &gbuses, bus_list) {
  1616. if (flag)
  1617. *prev = list_prev_entry(bus_cur, bus_list);
  1618. if (bus_cur->busno == bus_number)
  1619. return bus_cur;
  1620. }
  1621. return NULL;
  1622. }
  1623. void ibmphp_print_test(void)
  1624. {
  1625. int i = 0;
  1626. struct bus_node *bus_cur = NULL;
  1627. struct range_node *range;
  1628. struct resource_node *res;
  1629. debug_pci("*****************START**********************\n");
  1630. if ((!list_empty(&gbuses)) && flags) {
  1631. err("The GBUSES is not NULL?!?!?!?!?\n");
  1632. return;
  1633. }
  1634. list_for_each_entry(bus_cur, &gbuses, bus_list) {
  1635. debug_pci ("This is bus # %d. There are\n", bus_cur->busno);
  1636. debug_pci ("IORanges = %d\t", bus_cur->noIORanges);
  1637. debug_pci ("MemRanges = %d\t", bus_cur->noMemRanges);
  1638. debug_pci ("PFMemRanges = %d\n", bus_cur->noPFMemRanges);
  1639. debug_pci ("The IO Ranges are as follows:\n");
  1640. if (bus_cur->rangeIO) {
  1641. range = bus_cur->rangeIO;
  1642. for (i = 0; i < bus_cur->noIORanges; i++) {
  1643. debug_pci("rangeno is %d\n", range->rangeno);
  1644. debug_pci("[%x - %x]\n", range->start, range->end);
  1645. range = range->next;
  1646. }
  1647. }
  1648. debug_pci("The Mem Ranges are as follows:\n");
  1649. if (bus_cur->rangeMem) {
  1650. range = bus_cur->rangeMem;
  1651. for (i = 0; i < bus_cur->noMemRanges; i++) {
  1652. debug_pci("rangeno is %d\n", range->rangeno);
  1653. debug_pci("[%x - %x]\n", range->start, range->end);
  1654. range = range->next;
  1655. }
  1656. }
  1657. debug_pci("The PFMem Ranges are as follows:\n");
  1658. if (bus_cur->rangePFMem) {
  1659. range = bus_cur->rangePFMem;
  1660. for (i = 0; i < bus_cur->noPFMemRanges; i++) {
  1661. debug_pci("rangeno is %d\n", range->rangeno);
  1662. debug_pci("[%x - %x]\n", range->start, range->end);
  1663. range = range->next;
  1664. }
  1665. }
  1666. debug_pci("The resources on this bus are as follows\n");
  1667. debug_pci("IO...\n");
  1668. if (bus_cur->firstIO) {
  1669. res = bus_cur->firstIO;
  1670. while (res) {
  1671. debug_pci("The range # is %d\n", res->rangeno);
  1672. debug_pci("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
  1673. debug_pci("[%x - %x], len=%x\n", res->start, res->end, res->len);
  1674. if (res->next)
  1675. res = res->next;
  1676. else if (res->nextRange)
  1677. res = res->nextRange;
  1678. else
  1679. break;
  1680. }
  1681. }
  1682. debug_pci("Mem...\n");
  1683. if (bus_cur->firstMem) {
  1684. res = bus_cur->firstMem;
  1685. while (res) {
  1686. debug_pci("The range # is %d\n", res->rangeno);
  1687. debug_pci("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
  1688. debug_pci("[%x - %x], len=%x\n", res->start, res->end, res->len);
  1689. if (res->next)
  1690. res = res->next;
  1691. else if (res->nextRange)
  1692. res = res->nextRange;
  1693. else
  1694. break;
  1695. }
  1696. }
  1697. debug_pci("PFMem...\n");
  1698. if (bus_cur->firstPFMem) {
  1699. res = bus_cur->firstPFMem;
  1700. while (res) {
  1701. debug_pci("The range # is %d\n", res->rangeno);
  1702. debug_pci("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
  1703. debug_pci("[%x - %x], len=%x\n", res->start, res->end, res->len);
  1704. if (res->next)
  1705. res = res->next;
  1706. else if (res->nextRange)
  1707. res = res->nextRange;
  1708. else
  1709. break;
  1710. }
  1711. }
  1712. debug_pci("PFMemFromMem...\n");
  1713. if (bus_cur->firstPFMemFromMem) {
  1714. res = bus_cur->firstPFMemFromMem;
  1715. while (res) {
  1716. debug_pci("The range # is %d\n", res->rangeno);
  1717. debug_pci("The bus, devfnc is %d, %x\n", res->busno, res->devfunc);
  1718. debug_pci("[%x - %x], len=%x\n", res->start, res->end, res->len);
  1719. res = res->next;
  1720. }
  1721. }
  1722. }
  1723. debug_pci("***********************END***********************\n");
  1724. }
  1725. static int range_exists_already(struct range_node *range, struct bus_node *bus_cur, u8 type)
  1726. {
  1727. struct range_node *range_cur = NULL;
  1728. switch (type) {
  1729. case IO:
  1730. range_cur = bus_cur->rangeIO;
  1731. break;
  1732. case MEM:
  1733. range_cur = bus_cur->rangeMem;
  1734. break;
  1735. case PFMEM:
  1736. range_cur = bus_cur->rangePFMem;
  1737. break;
  1738. default:
  1739. err("wrong type passed to find out if range already exists\n");
  1740. return -ENODEV;
  1741. }
  1742. while (range_cur) {
  1743. if ((range_cur->start == range->start) && (range_cur->end == range->end))
  1744. return 1;
  1745. range_cur = range_cur->next;
  1746. }
  1747. return 0;
  1748. }
  1749. /* This routine will read the windows for any PPB we have and update the
  1750. * range info for the secondary bus, and will also input this info into
  1751. * primary bus, since BIOS doesn't. This is for PPB that are in the system
  1752. * on bootup. For bridged cards that were added during previous load of the
  1753. * driver, only the ranges and the bus structure are added, the devices are
  1754. * added from NVRAM
  1755. * Input: primary busno
  1756. * Returns: none
  1757. * Note: this function doesn't take into account IO restrictions etc,
  1758. * so will only work for bridges with no video/ISA devices behind them It
  1759. * also will not work for onboard PPBs that can have more than 1 *bus
  1760. * behind them All these are TO DO.
  1761. * Also need to add more error checkings... (from fnc returns etc)
  1762. */
  1763. static int __init update_bridge_ranges(struct bus_node **bus)
  1764. {
  1765. u8 sec_busno, device, function, hdr_type, start_io_address, end_io_address;
  1766. u16 vendor_id, upper_io_start, upper_io_end, start_mem_address, end_mem_address;
  1767. u32 start_address, end_address, upper_start, upper_end;
  1768. struct bus_node *bus_sec;
  1769. struct bus_node *bus_cur;
  1770. struct resource_node *io;
  1771. struct resource_node *mem;
  1772. struct resource_node *pfmem;
  1773. struct range_node *range;
  1774. unsigned int devfn;
  1775. bus_cur = *bus;
  1776. if (!bus_cur)
  1777. return -ENODEV;
  1778. ibmphp_pci_bus->number = bus_cur->busno;
  1779. debug("inside %s\n", __func__);
  1780. debug("bus_cur->busno = %x\n", bus_cur->busno);
  1781. for (device = 0; device < 32; device++) {
  1782. for (function = 0x00; function < 0x08; function++) {
  1783. devfn = PCI_DEVFN(device, function);
  1784. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
  1785. if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
  1786. /* found correct device!!! */
  1787. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  1788. switch (hdr_type) {
  1789. case PCI_HEADER_TYPE_NORMAL:
  1790. function = 0x8;
  1791. break;
  1792. case PCI_HEADER_TYPE_MULTIDEVICE:
  1793. break;
  1794. case PCI_HEADER_TYPE_BRIDGE:
  1795. function = 0x8;
  1796. case PCI_HEADER_TYPE_MULTIBRIDGE:
  1797. /* We assume here that only 1 bus behind the bridge
  1798. TO DO: add functionality for several:
  1799. temp = secondary;
  1800. while (temp < subordinate) {
  1801. ...
  1802. temp++;
  1803. }
  1804. */
  1805. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_busno);
  1806. bus_sec = find_bus_wprev(sec_busno, NULL, 0);
  1807. /* this bus structure doesn't exist yet, PPB was configured during previous loading of ibmphp */
  1808. if (!bus_sec) {
  1809. bus_sec = alloc_error_bus(NULL, sec_busno, 1);
  1810. /* the rest will be populated during NVRAM call */
  1811. return 0;
  1812. }
  1813. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &start_io_address);
  1814. pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &end_io_address);
  1815. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, &upper_io_start);
  1816. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, &upper_io_end);
  1817. start_address = (start_io_address & PCI_IO_RANGE_MASK) << 8;
  1818. start_address |= (upper_io_start << 16);
  1819. end_address = (end_io_address & PCI_IO_RANGE_MASK) << 8;
  1820. end_address |= (upper_io_end << 16);
  1821. if ((start_address) && (start_address <= end_address)) {
  1822. range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
  1823. if (!range) {
  1824. err("out of system memory\n");
  1825. return -ENOMEM;
  1826. }
  1827. range->start = start_address;
  1828. range->end = end_address + 0xfff;
  1829. if (bus_sec->noIORanges > 0) {
  1830. if (!range_exists_already(range, bus_sec, IO)) {
  1831. add_bus_range(IO, range, bus_sec);
  1832. ++bus_sec->noIORanges;
  1833. } else {
  1834. kfree(range);
  1835. range = NULL;
  1836. }
  1837. } else {
  1838. /* 1st IO Range on the bus */
  1839. range->rangeno = 1;
  1840. bus_sec->rangeIO = range;
  1841. ++bus_sec->noIORanges;
  1842. }
  1843. fix_resources(bus_sec);
  1844. if (ibmphp_find_resource(bus_cur, start_address, &io, IO)) {
  1845. io = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  1846. if (!io) {
  1847. kfree(range);
  1848. err("out of system memory\n");
  1849. return -ENOMEM;
  1850. }
  1851. io->type = IO;
  1852. io->busno = bus_cur->busno;
  1853. io->devfunc = ((device << 3) | (function & 0x7));
  1854. io->start = start_address;
  1855. io->end = end_address + 0xfff;
  1856. io->len = io->end - io->start + 1;
  1857. ibmphp_add_resource(io);
  1858. }
  1859. }
  1860. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &start_mem_address);
  1861. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &end_mem_address);
  1862. start_address = 0x00000000 | (start_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
  1863. end_address = 0x00000000 | (end_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
  1864. if ((start_address) && (start_address <= end_address)) {
  1865. range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
  1866. if (!range) {
  1867. err("out of system memory\n");
  1868. return -ENOMEM;
  1869. }
  1870. range->start = start_address;
  1871. range->end = end_address + 0xfffff;
  1872. if (bus_sec->noMemRanges > 0) {
  1873. if (!range_exists_already(range, bus_sec, MEM)) {
  1874. add_bus_range(MEM, range, bus_sec);
  1875. ++bus_sec->noMemRanges;
  1876. } else {
  1877. kfree(range);
  1878. range = NULL;
  1879. }
  1880. } else {
  1881. /* 1st Mem Range on the bus */
  1882. range->rangeno = 1;
  1883. bus_sec->rangeMem = range;
  1884. ++bus_sec->noMemRanges;
  1885. }
  1886. fix_resources(bus_sec);
  1887. if (ibmphp_find_resource(bus_cur, start_address, &mem, MEM)) {
  1888. mem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  1889. if (!mem) {
  1890. kfree(range);
  1891. err("out of system memory\n");
  1892. return -ENOMEM;
  1893. }
  1894. mem->type = MEM;
  1895. mem->busno = bus_cur->busno;
  1896. mem->devfunc = ((device << 3) | (function & 0x7));
  1897. mem->start = start_address;
  1898. mem->end = end_address + 0xfffff;
  1899. mem->len = mem->end - mem->start + 1;
  1900. ibmphp_add_resource(mem);
  1901. }
  1902. }
  1903. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &start_mem_address);
  1904. pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &end_mem_address);
  1905. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, &upper_start);
  1906. pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, &upper_end);
  1907. start_address = 0x00000000 | (start_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
  1908. end_address = 0x00000000 | (end_mem_address & PCI_MEMORY_RANGE_MASK) << 16;
  1909. #if BITS_PER_LONG == 64
  1910. start_address |= ((long) upper_start) << 32;
  1911. end_address |= ((long) upper_end) << 32;
  1912. #endif
  1913. if ((start_address) && (start_address <= end_address)) {
  1914. range = kzalloc(sizeof(struct range_node), GFP_KERNEL);
  1915. if (!range) {
  1916. err("out of system memory\n");
  1917. return -ENOMEM;
  1918. }
  1919. range->start = start_address;
  1920. range->end = end_address + 0xfffff;
  1921. if (bus_sec->noPFMemRanges > 0) {
  1922. if (!range_exists_already(range, bus_sec, PFMEM)) {
  1923. add_bus_range(PFMEM, range, bus_sec);
  1924. ++bus_sec->noPFMemRanges;
  1925. } else {
  1926. kfree(range);
  1927. range = NULL;
  1928. }
  1929. } else {
  1930. /* 1st PFMem Range on the bus */
  1931. range->rangeno = 1;
  1932. bus_sec->rangePFMem = range;
  1933. ++bus_sec->noPFMemRanges;
  1934. }
  1935. fix_resources(bus_sec);
  1936. if (ibmphp_find_resource(bus_cur, start_address, &pfmem, PFMEM)) {
  1937. pfmem = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
  1938. if (!pfmem) {
  1939. kfree(range);
  1940. err("out of system memory\n");
  1941. return -ENOMEM;
  1942. }
  1943. pfmem->type = PFMEM;
  1944. pfmem->busno = bus_cur->busno;
  1945. pfmem->devfunc = ((device << 3) | (function & 0x7));
  1946. pfmem->start = start_address;
  1947. pfmem->end = end_address + 0xfffff;
  1948. pfmem->len = pfmem->end - pfmem->start + 1;
  1949. pfmem->fromMem = 0;
  1950. ibmphp_add_resource(pfmem);
  1951. }
  1952. }
  1953. break;
  1954. } /* end of switch */
  1955. } /* end if vendor */
  1956. } /* end for function */
  1957. } /* end for device */
  1958. bus = &bus_cur;
  1959. return 0;
  1960. }