ibmphp_pci.c 54 KB

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