ibmphp_pci.c 56 KB

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