shpchp_ctrl.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  1. /*
  2. * Standard Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  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 <greg@kroah.com>, <kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/slab.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/delay.h>
  37. #include <linux/wait.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/pci.h>
  40. #include "shpchp.h"
  41. #include "shpchprm.h"
  42. static u32 configure_new_device(struct controller *ctrl, struct pci_func *func,
  43. u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev);
  44. static int configure_new_function( struct controller *ctrl, struct pci_func *func,
  45. u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev);
  46. static void interrupt_event_handler(struct controller *ctrl);
  47. static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
  48. static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
  49. static int event_finished;
  50. static unsigned long pushbutton_pending; /* = 0 */
  51. u8 shpchp_disk_irq;
  52. u8 shpchp_nic_irq;
  53. u8 shpchp_handle_attention_button(u8 hp_slot, void *inst_id)
  54. {
  55. struct controller *ctrl = (struct controller *) inst_id;
  56. struct slot *p_slot;
  57. u8 rc = 0;
  58. u8 getstatus;
  59. struct pci_func *func;
  60. struct event_info *taskInfo;
  61. /* Attention Button Change */
  62. dbg("shpchp: Attention button interrupt received.\n");
  63. func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
  64. /* This is the structure that tells the worker thread what to do */
  65. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  66. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  67. p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
  68. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  69. ctrl->next_event = (ctrl->next_event + 1) % 10;
  70. taskInfo->hp_slot = hp_slot;
  71. rc++;
  72. /*
  73. * Button pressed - See if need to TAKE ACTION!!!
  74. */
  75. info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
  76. taskInfo->event_type = INT_BUTTON_PRESS;
  77. if ((p_slot->state == BLINKINGON_STATE)
  78. || (p_slot->state == BLINKINGOFF_STATE)) {
  79. /* Cancel if we are still blinking; this means that we press the
  80. * attention again before the 5 sec. limit expires to cancel hot-add
  81. * or hot-remove
  82. */
  83. taskInfo->event_type = INT_BUTTON_CANCEL;
  84. info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
  85. } else if ((p_slot->state == POWERON_STATE)
  86. || (p_slot->state == POWEROFF_STATE)) {
  87. /* Ignore if the slot is on power-on or power-off state; this
  88. * means that the previous attention button action to hot-add or
  89. * hot-remove is undergoing
  90. */
  91. taskInfo->event_type = INT_BUTTON_IGNORE;
  92. info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
  93. }
  94. if (rc)
  95. up(&event_semaphore); /* signal event thread that new event is posted */
  96. return 0;
  97. }
  98. u8 shpchp_handle_switch_change(u8 hp_slot, void *inst_id)
  99. {
  100. struct controller *ctrl = (struct controller *) inst_id;
  101. struct slot *p_slot;
  102. u8 rc = 0;
  103. u8 getstatus;
  104. struct pci_func *func;
  105. struct event_info *taskInfo;
  106. /* Switch Change */
  107. dbg("shpchp: Switch interrupt received.\n");
  108. func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
  109. /* This is the structure that tells the worker thread
  110. * what to do
  111. */
  112. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  113. ctrl->next_event = (ctrl->next_event + 1) % 10;
  114. taskInfo->hp_slot = hp_slot;
  115. rc++;
  116. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  117. p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
  118. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  119. dbg("%s: Card present %x Power status %x\n", __FUNCTION__,
  120. func->presence_save, func->pwr_save);
  121. if (getstatus) {
  122. /*
  123. * Switch opened
  124. */
  125. info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
  126. func->switch_save = 0;
  127. taskInfo->event_type = INT_SWITCH_OPEN;
  128. if (func->pwr_save && func->presence_save) {
  129. taskInfo->event_type = INT_POWER_FAULT;
  130. err("Surprise Removal of card\n");
  131. }
  132. } else {
  133. /*
  134. * Switch closed
  135. */
  136. info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
  137. func->switch_save = 0x10;
  138. taskInfo->event_type = INT_SWITCH_CLOSE;
  139. }
  140. if (rc)
  141. up(&event_semaphore); /* signal event thread that new event is posted */
  142. return rc;
  143. }
  144. u8 shpchp_handle_presence_change(u8 hp_slot, void *inst_id)
  145. {
  146. struct controller *ctrl = (struct controller *) inst_id;
  147. struct slot *p_slot;
  148. u8 rc = 0;
  149. /*u8 temp_byte;*/
  150. struct pci_func *func;
  151. struct event_info *taskInfo;
  152. /* Presence Change */
  153. dbg("shpchp: Presence/Notify input change.\n");
  154. func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
  155. /* This is the structure that tells the worker thread
  156. * what to do
  157. */
  158. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  159. ctrl->next_event = (ctrl->next_event + 1) % 10;
  160. taskInfo->hp_slot = hp_slot;
  161. rc++;
  162. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  163. /*
  164. * Save the presence state
  165. */
  166. p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
  167. if (func->presence_save) {
  168. /*
  169. * Card Present
  170. */
  171. info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
  172. taskInfo->event_type = INT_PRESENCE_ON;
  173. } else {
  174. /*
  175. * Not Present
  176. */
  177. info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
  178. taskInfo->event_type = INT_PRESENCE_OFF;
  179. }
  180. if (rc)
  181. up(&event_semaphore); /* signal event thread that new event is posted */
  182. return rc;
  183. }
  184. u8 shpchp_handle_power_fault(u8 hp_slot, void *inst_id)
  185. {
  186. struct controller *ctrl = (struct controller *) inst_id;
  187. struct slot *p_slot;
  188. u8 rc = 0;
  189. struct pci_func *func;
  190. struct event_info *taskInfo;
  191. /* Power fault */
  192. dbg("shpchp: Power fault interrupt received.\n");
  193. func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
  194. /* This is the structure that tells the worker thread
  195. * what to do
  196. */
  197. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  198. ctrl->next_event = (ctrl->next_event + 1) % 10;
  199. taskInfo->hp_slot = hp_slot;
  200. rc++;
  201. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  202. if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
  203. /*
  204. * Power fault Cleared
  205. */
  206. info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
  207. func->status = 0x00;
  208. taskInfo->event_type = INT_POWER_FAULT_CLEAR;
  209. } else {
  210. /*
  211. * Power fault
  212. */
  213. info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
  214. taskInfo->event_type = INT_POWER_FAULT;
  215. /* set power fault status for this board */
  216. func->status = 0xFF;
  217. info("power fault bit %x set\n", hp_slot);
  218. }
  219. if (rc)
  220. up(&event_semaphore); /* signal event thread that new event is posted */
  221. return rc;
  222. }
  223. /*
  224. * sort_by_size
  225. *
  226. * Sorts nodes on the list by their length.
  227. * Smallest first.
  228. *
  229. */
  230. static int sort_by_size(struct pci_resource **head)
  231. {
  232. struct pci_resource *current_res;
  233. struct pci_resource *next_res;
  234. int out_of_order = 1;
  235. if (!(*head))
  236. return(1);
  237. if (!((*head)->next))
  238. return(0);
  239. while (out_of_order) {
  240. out_of_order = 0;
  241. /* Special case for swapping list head */
  242. if (((*head)->next) &&
  243. ((*head)->length > (*head)->next->length)) {
  244. out_of_order++;
  245. current_res = *head;
  246. *head = (*head)->next;
  247. current_res->next = (*head)->next;
  248. (*head)->next = current_res;
  249. }
  250. current_res = *head;
  251. while (current_res->next && current_res->next->next) {
  252. if (current_res->next->length > current_res->next->next->length) {
  253. out_of_order++;
  254. next_res = current_res->next;
  255. current_res->next = current_res->next->next;
  256. current_res = current_res->next;
  257. next_res->next = current_res->next;
  258. current_res->next = next_res;
  259. } else
  260. current_res = current_res->next;
  261. }
  262. } /* End of out_of_order loop */
  263. return(0);
  264. }
  265. /*
  266. * sort_by_max_size
  267. *
  268. * Sorts nodes on the list by their length.
  269. * Largest first.
  270. *
  271. */
  272. static int sort_by_max_size(struct pci_resource **head)
  273. {
  274. struct pci_resource *current_res;
  275. struct pci_resource *next_res;
  276. int out_of_order = 1;
  277. if (!(*head))
  278. return(1);
  279. if (!((*head)->next))
  280. return(0);
  281. while (out_of_order) {
  282. out_of_order = 0;
  283. /* Special case for swapping list head */
  284. if (((*head)->next) &&
  285. ((*head)->length < (*head)->next->length)) {
  286. out_of_order++;
  287. current_res = *head;
  288. *head = (*head)->next;
  289. current_res->next = (*head)->next;
  290. (*head)->next = current_res;
  291. }
  292. current_res = *head;
  293. while (current_res->next && current_res->next->next) {
  294. if (current_res->next->length < current_res->next->next->length) {
  295. out_of_order++;
  296. next_res = current_res->next;
  297. current_res->next = current_res->next->next;
  298. current_res = current_res->next;
  299. next_res->next = current_res->next;
  300. current_res->next = next_res;
  301. } else
  302. current_res = current_res->next;
  303. }
  304. } /* End of out_of_order loop */
  305. return(0);
  306. }
  307. /*
  308. * do_pre_bridge_resource_split
  309. *
  310. * Returns zero or one node of resources that aren't in use
  311. *
  312. */
  313. static struct pci_resource *do_pre_bridge_resource_split (struct pci_resource **head, struct pci_resource **orig_head, u32 alignment)
  314. {
  315. struct pci_resource *prevnode = NULL;
  316. struct pci_resource *node;
  317. struct pci_resource *split_node;
  318. u32 rc;
  319. u32 temp_dword;
  320. dbg("do_pre_bridge_resource_split\n");
  321. if (!(*head) || !(*orig_head))
  322. return(NULL);
  323. rc = shpchp_resource_sort_and_combine(head);
  324. if (rc)
  325. return(NULL);
  326. if ((*head)->base != (*orig_head)->base)
  327. return(NULL);
  328. if ((*head)->length == (*orig_head)->length)
  329. return(NULL);
  330. /* If we got here, there the bridge requires some of the resource, but
  331. * we may be able to split some off of the front
  332. */
  333. node = *head;
  334. if (node->length & (alignment -1)) {
  335. /* This one isn't an aligned length, so we'll make a new entry
  336. * and split it up.
  337. */
  338. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  339. if (!split_node)
  340. return(NULL);
  341. temp_dword = (node->length | (alignment-1)) + 1 - alignment;
  342. split_node->base = node->base;
  343. split_node->length = temp_dword;
  344. node->length -= temp_dword;
  345. node->base += split_node->length;
  346. /* Put it in the list */
  347. *head = split_node;
  348. split_node->next = node;
  349. }
  350. if (node->length < alignment) {
  351. return(NULL);
  352. }
  353. /* Now unlink it */
  354. if (*head == node) {
  355. *head = node->next;
  356. node->next = NULL;
  357. } else {
  358. prevnode = *head;
  359. while (prevnode->next != node)
  360. prevnode = prevnode->next;
  361. prevnode->next = node->next;
  362. node->next = NULL;
  363. }
  364. return(node);
  365. }
  366. /*
  367. * do_bridge_resource_split
  368. *
  369. * Returns zero or one node of resources that aren't in use
  370. *
  371. */
  372. static struct pci_resource *do_bridge_resource_split (struct pci_resource **head, u32 alignment)
  373. {
  374. struct pci_resource *prevnode = NULL;
  375. struct pci_resource *node;
  376. u32 rc;
  377. u32 temp_dword;
  378. if (!(*head))
  379. return(NULL);
  380. rc = shpchp_resource_sort_and_combine(head);
  381. if (rc)
  382. return(NULL);
  383. node = *head;
  384. while (node->next) {
  385. prevnode = node;
  386. node = node->next;
  387. kfree(prevnode);
  388. }
  389. if (node->length < alignment) {
  390. kfree(node);
  391. return(NULL);
  392. }
  393. if (node->base & (alignment - 1)) {
  394. /* Short circuit if adjusted size is too small */
  395. temp_dword = (node->base | (alignment-1)) + 1;
  396. if ((node->length - (temp_dword - node->base)) < alignment) {
  397. kfree(node);
  398. return(NULL);
  399. }
  400. node->length -= (temp_dword - node->base);
  401. node->base = temp_dword;
  402. }
  403. if (node->length & (alignment - 1)) {
  404. /* There's stuff in use after this node */
  405. kfree(node);
  406. return(NULL);
  407. }
  408. return(node);
  409. }
  410. /*
  411. * get_io_resource
  412. *
  413. * this function sorts the resource list by size and then
  414. * returns the first node of "size" length that is not in the
  415. * ISA aliasing window. If it finds a node larger than "size"
  416. * it will split it up.
  417. *
  418. * size must be a power of two.
  419. */
  420. static struct pci_resource *get_io_resource (struct pci_resource **head, u32 size)
  421. {
  422. struct pci_resource *prevnode;
  423. struct pci_resource *node;
  424. struct pci_resource *split_node = NULL;
  425. u32 temp_dword;
  426. if (!(*head))
  427. return(NULL);
  428. if ( shpchp_resource_sort_and_combine(head) )
  429. return(NULL);
  430. if ( sort_by_size(head) )
  431. return(NULL);
  432. for (node = *head; node; node = node->next) {
  433. if (node->length < size)
  434. continue;
  435. if (node->base & (size - 1)) {
  436. /* This one isn't base aligned properly
  437. so we'll make a new entry and split it up */
  438. temp_dword = (node->base | (size-1)) + 1;
  439. /*/ Short circuit if adjusted size is too small */
  440. if ((node->length - (temp_dword - node->base)) < size)
  441. continue;
  442. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  443. if (!split_node)
  444. return(NULL);
  445. split_node->base = node->base;
  446. split_node->length = temp_dword - node->base;
  447. node->base = temp_dword;
  448. node->length -= split_node->length;
  449. /* Put it in the list */
  450. split_node->next = node->next;
  451. node->next = split_node;
  452. } /* End of non-aligned base */
  453. /* Don't need to check if too small since we already did */
  454. if (node->length > size) {
  455. /* This one is longer than we need
  456. so we'll make a new entry and split it up */
  457. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  458. if (!split_node)
  459. return(NULL);
  460. split_node->base = node->base + size;
  461. split_node->length = node->length - size;
  462. node->length = size;
  463. /* Put it in the list */
  464. split_node->next = node->next;
  465. node->next = split_node;
  466. } /* End of too big on top end */
  467. /* For IO make sure it's not in the ISA aliasing space */
  468. if (node->base & 0x300L)
  469. continue;
  470. /* If we got here, then it is the right size
  471. Now take it out of the list */
  472. if (*head == node) {
  473. *head = node->next;
  474. } else {
  475. prevnode = *head;
  476. while (prevnode->next != node)
  477. prevnode = prevnode->next;
  478. prevnode->next = node->next;
  479. }
  480. node->next = NULL;
  481. /* Stop looping */
  482. break;
  483. }
  484. return(node);
  485. }
  486. /*
  487. * get_max_resource
  488. *
  489. * Gets the largest node that is at least "size" big from the
  490. * list pointed to by head. It aligns the node on top and bottom
  491. * to "size" alignment before returning it.
  492. * J.I. modified to put max size limits of; 64M->32M->16M->8M->4M->1M
  493. * This is needed to avoid allocating entire ACPI _CRS res to one child bridge/slot.
  494. */
  495. static struct pci_resource *get_max_resource (struct pci_resource **head, u32 size)
  496. {
  497. struct pci_resource *max;
  498. struct pci_resource *temp;
  499. struct pci_resource *split_node;
  500. u32 temp_dword;
  501. u32 max_size[] = { 0x4000000, 0x2000000, 0x1000000, 0x0800000, 0x0400000, 0x0200000, 0x0100000, 0x00 };
  502. int i;
  503. if (!(*head))
  504. return(NULL);
  505. if (shpchp_resource_sort_and_combine(head))
  506. return(NULL);
  507. if (sort_by_max_size(head))
  508. return(NULL);
  509. for (max = *head;max; max = max->next) {
  510. /* If not big enough we could probably just bail,
  511. instead we'll continue to the next. */
  512. if (max->length < size)
  513. continue;
  514. if (max->base & (size - 1)) {
  515. /* This one isn't base aligned properly
  516. so we'll make a new entry and split it up */
  517. temp_dword = (max->base | (size-1)) + 1;
  518. /* Short circuit if adjusted size is too small */
  519. if ((max->length - (temp_dword - max->base)) < size)
  520. continue;
  521. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  522. if (!split_node)
  523. return(NULL);
  524. split_node->base = max->base;
  525. split_node->length = temp_dword - max->base;
  526. max->base = temp_dword;
  527. max->length -= split_node->length;
  528. /* Put it next in the list */
  529. split_node->next = max->next;
  530. max->next = split_node;
  531. }
  532. if ((max->base + max->length) & (size - 1)) {
  533. /* This one isn't end aligned properly at the top
  534. so we'll make a new entry and split it up */
  535. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  536. if (!split_node)
  537. return(NULL);
  538. temp_dword = ((max->base + max->length) & ~(size - 1));
  539. split_node->base = temp_dword;
  540. split_node->length = max->length + max->base
  541. - split_node->base;
  542. max->length -= split_node->length;
  543. /* Put it in the list */
  544. split_node->next = max->next;
  545. max->next = split_node;
  546. }
  547. /* Make sure it didn't shrink too much when we aligned it */
  548. if (max->length < size)
  549. continue;
  550. for ( i = 0; max_size[i] > size; i++) {
  551. if (max->length > max_size[i]) {
  552. split_node = kmalloc(sizeof(*split_node),
  553. GFP_KERNEL);
  554. if (!split_node)
  555. break; /* return (NULL); */
  556. split_node->base = max->base + max_size[i];
  557. split_node->length = max->length - max_size[i];
  558. max->length = max_size[i];
  559. /* Put it next in the list */
  560. split_node->next = max->next;
  561. max->next = split_node;
  562. break;
  563. }
  564. }
  565. /* Now take it out of the list */
  566. temp = (struct pci_resource*) *head;
  567. if (temp == max) {
  568. *head = max->next;
  569. } else {
  570. while (temp && temp->next != max) {
  571. temp = temp->next;
  572. }
  573. temp->next = max->next;
  574. }
  575. max->next = NULL;
  576. return(max);
  577. }
  578. /* If we get here, we couldn't find one */
  579. return(NULL);
  580. }
  581. /*
  582. * get_resource
  583. *
  584. * this function sorts the resource list by size and then
  585. * returns the first node of "size" length. If it finds a node
  586. * larger than "size" it will split it up.
  587. *
  588. * size must be a power of two.
  589. */
  590. static struct pci_resource *get_resource (struct pci_resource **head, u32 size)
  591. {
  592. struct pci_resource *prevnode;
  593. struct pci_resource *node;
  594. struct pci_resource *split_node;
  595. u32 temp_dword;
  596. if (!(*head))
  597. return(NULL);
  598. if ( shpchp_resource_sort_and_combine(head) )
  599. return(NULL);
  600. if ( sort_by_size(head) )
  601. return(NULL);
  602. for (node = *head; node; node = node->next) {
  603. dbg("%s: req_size =0x%x node=%p, base=0x%x, length=0x%x\n",
  604. __FUNCTION__, size, node, node->base, node->length);
  605. if (node->length < size)
  606. continue;
  607. if (node->base & (size - 1)) {
  608. dbg("%s: not aligned\n", __FUNCTION__);
  609. /* this one isn't base aligned properly
  610. so we'll make a new entry and split it up */
  611. temp_dword = (node->base | (size-1)) + 1;
  612. /* Short circuit if adjusted size is too small */
  613. if ((node->length - (temp_dword - node->base)) < size)
  614. continue;
  615. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  616. if (!split_node)
  617. return(NULL);
  618. split_node->base = node->base;
  619. split_node->length = temp_dword - node->base;
  620. node->base = temp_dword;
  621. node->length -= split_node->length;
  622. /* Put it in the list */
  623. split_node->next = node->next;
  624. node->next = split_node;
  625. } /* End of non-aligned base */
  626. /* Don't need to check if too small since we already did */
  627. if (node->length > size) {
  628. dbg("%s: too big\n", __FUNCTION__);
  629. /* this one is longer than we need
  630. so we'll make a new entry and split it up */
  631. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  632. if (!split_node)
  633. return(NULL);
  634. split_node->base = node->base + size;
  635. split_node->length = node->length - size;
  636. node->length = size;
  637. /* Put it in the list */
  638. split_node->next = node->next;
  639. node->next = split_node;
  640. } /* End of too big on top end */
  641. dbg("%s: got one!!!\n", __FUNCTION__);
  642. /* If we got here, then it is the right size
  643. Now take it out of the list */
  644. if (*head == node) {
  645. *head = node->next;
  646. } else {
  647. prevnode = *head;
  648. while (prevnode->next != node)
  649. prevnode = prevnode->next;
  650. prevnode->next = node->next;
  651. }
  652. node->next = NULL;
  653. /* Stop looping */
  654. break;
  655. }
  656. return(node);
  657. }
  658. /*
  659. * shpchp_resource_sort_and_combine
  660. *
  661. * Sorts all of the nodes in the list in ascending order by
  662. * their base addresses. Also does garbage collection by
  663. * combining adjacent nodes.
  664. *
  665. * returns 0 if success
  666. */
  667. int shpchp_resource_sort_and_combine(struct pci_resource **head)
  668. {
  669. struct pci_resource *node1;
  670. struct pci_resource *node2;
  671. int out_of_order = 1;
  672. dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
  673. if (!(*head))
  674. return(1);
  675. dbg("*head->next = %p\n",(*head)->next);
  676. if (!(*head)->next)
  677. return(0); /* only one item on the list, already sorted! */
  678. dbg("*head->base = 0x%x\n",(*head)->base);
  679. dbg("*head->next->base = 0x%x\n",(*head)->next->base);
  680. while (out_of_order) {
  681. out_of_order = 0;
  682. /* Special case for swapping list head */
  683. if (((*head)->next) &&
  684. ((*head)->base > (*head)->next->base)) {
  685. node1 = *head;
  686. (*head) = (*head)->next;
  687. node1->next = (*head)->next;
  688. (*head)->next = node1;
  689. out_of_order++;
  690. }
  691. node1 = (*head);
  692. while (node1->next && node1->next->next) {
  693. if (node1->next->base > node1->next->next->base) {
  694. out_of_order++;
  695. node2 = node1->next;
  696. node1->next = node1->next->next;
  697. node1 = node1->next;
  698. node2->next = node1->next;
  699. node1->next = node2;
  700. } else
  701. node1 = node1->next;
  702. }
  703. } /* End of out_of_order loop */
  704. node1 = *head;
  705. while (node1 && node1->next) {
  706. if ((node1->base + node1->length) == node1->next->base) {
  707. /* Combine */
  708. dbg("8..\n");
  709. node1->length += node1->next->length;
  710. node2 = node1->next;
  711. node1->next = node1->next->next;
  712. kfree(node2);
  713. } else
  714. node1 = node1->next;
  715. }
  716. return(0);
  717. }
  718. /**
  719. * shpchp_slot_create - Creates a node and adds it to the proper bus.
  720. * @busnumber - bus where new node is to be located
  721. *
  722. * Returns pointer to the new node or NULL if unsuccessful
  723. */
  724. struct pci_func *shpchp_slot_create(u8 busnumber)
  725. {
  726. struct pci_func *new_slot;
  727. struct pci_func *next;
  728. new_slot = kmalloc(sizeof(*new_slot), GFP_KERNEL);
  729. if (new_slot == NULL) {
  730. return(new_slot);
  731. }
  732. memset(new_slot, 0, sizeof(struct pci_func));
  733. new_slot->next = NULL;
  734. new_slot->configured = 1;
  735. if (shpchp_slot_list[busnumber] == NULL) {
  736. shpchp_slot_list[busnumber] = new_slot;
  737. } else {
  738. next = shpchp_slot_list[busnumber];
  739. while (next->next != NULL)
  740. next = next->next;
  741. next->next = new_slot;
  742. }
  743. return(new_slot);
  744. }
  745. /*
  746. * slot_remove - Removes a node from the linked list of slots.
  747. * @old_slot: slot to remove
  748. *
  749. * Returns 0 if successful, !0 otherwise.
  750. */
  751. static int slot_remove(struct pci_func * old_slot)
  752. {
  753. struct pci_func *next;
  754. if (old_slot == NULL)
  755. return(1);
  756. next = shpchp_slot_list[old_slot->bus];
  757. if (next == NULL) {
  758. return(1);
  759. }
  760. if (next == old_slot) {
  761. shpchp_slot_list[old_slot->bus] = old_slot->next;
  762. shpchp_destroy_board_resources(old_slot);
  763. kfree(old_slot);
  764. return(0);
  765. }
  766. while ((next->next != old_slot) && (next->next != NULL)) {
  767. next = next->next;
  768. }
  769. if (next->next == old_slot) {
  770. next->next = old_slot->next;
  771. shpchp_destroy_board_resources(old_slot);
  772. kfree(old_slot);
  773. return(0);
  774. } else
  775. return(2);
  776. }
  777. /**
  778. * bridge_slot_remove - Removes a node from the linked list of slots.
  779. * @bridge: bridge to remove
  780. *
  781. * Returns 0 if successful, !0 otherwise.
  782. */
  783. static int bridge_slot_remove(struct pci_func *bridge)
  784. {
  785. u8 subordinateBus, secondaryBus;
  786. u8 tempBus;
  787. struct pci_func *next;
  788. if (bridge == NULL)
  789. return(1);
  790. secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
  791. subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
  792. for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
  793. next = shpchp_slot_list[tempBus];
  794. while (!slot_remove(next)) {
  795. next = shpchp_slot_list[tempBus];
  796. }
  797. }
  798. next = shpchp_slot_list[bridge->bus];
  799. if (next == NULL) {
  800. return(1);
  801. }
  802. if (next == bridge) {
  803. shpchp_slot_list[bridge->bus] = bridge->next;
  804. kfree(bridge);
  805. return(0);
  806. }
  807. while ((next->next != bridge) && (next->next != NULL)) {
  808. next = next->next;
  809. }
  810. if (next->next == bridge) {
  811. next->next = bridge->next;
  812. kfree(bridge);
  813. return(0);
  814. } else
  815. return(2);
  816. }
  817. /**
  818. * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
  819. * @bus: bus to find
  820. * @device: device to find
  821. * @index: is 0 for first function found, 1 for the second...
  822. *
  823. * Returns pointer to the node if successful, %NULL otherwise.
  824. */
  825. struct pci_func *shpchp_slot_find(u8 bus, u8 device, u8 index)
  826. {
  827. int found = -1;
  828. struct pci_func *func;
  829. func = shpchp_slot_list[bus];
  830. if ((func == NULL) || ((func->device == device) && (index == 0)))
  831. return(func);
  832. if (func->device == device)
  833. found++;
  834. while (func->next != NULL) {
  835. func = func->next;
  836. if (func->device == device)
  837. found++;
  838. if (found == index)
  839. return(func);
  840. }
  841. return(NULL);
  842. }
  843. static int is_bridge(struct pci_func * func)
  844. {
  845. /* Check the header type */
  846. if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
  847. return 1;
  848. else
  849. return 0;
  850. }
  851. /* The following routines constitute the bulk of the
  852. hotplug controller logic
  853. */
  854. static u32 change_bus_speed(struct controller *ctrl, struct slot *p_slot, enum pci_bus_speed speed)
  855. {
  856. u32 rc = 0;
  857. dbg("%s: change to speed %d\n", __FUNCTION__, speed);
  858. down(&ctrl->crit_sect);
  859. if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, speed))) {
  860. err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
  861. up(&ctrl->crit_sect);
  862. return WRONG_BUS_FREQUENCY;
  863. }
  864. wait_for_ctrl_irq (ctrl);
  865. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  866. err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
  867. __FUNCTION__);
  868. err("%s: Error code (%d)\n", __FUNCTION__, rc);
  869. up(&ctrl->crit_sect);
  870. return WRONG_BUS_FREQUENCY;
  871. }
  872. up(&ctrl->crit_sect);
  873. return rc;
  874. }
  875. static u32 fix_bus_speed(struct controller *ctrl, struct slot *pslot, u8 flag,
  876. enum pci_bus_speed asp, enum pci_bus_speed bsp, enum pci_bus_speed msp)
  877. {
  878. u32 rc = 0;
  879. if (flag != 0) { /* Other slots on the same bus are occupied */
  880. if ( asp < bsp ) {
  881. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bsp, asp);
  882. return WRONG_BUS_FREQUENCY;
  883. }
  884. } else {
  885. /* Other slots on the same bus are empty */
  886. if (msp == bsp) {
  887. /* if adapter_speed >= bus_speed, do nothing */
  888. if (asp < bsp) {
  889. /*
  890. * Try to lower bus speed to accommodate the adapter if other slots
  891. * on the same controller are empty
  892. */
  893. if ((rc = change_bus_speed(ctrl, pslot, asp)))
  894. return rc;
  895. }
  896. } else {
  897. if (asp < msp) {
  898. if ((rc = change_bus_speed(ctrl, pslot, asp)))
  899. return rc;
  900. } else {
  901. if ((rc = change_bus_speed(ctrl, pslot, msp)))
  902. return rc;
  903. }
  904. }
  905. }
  906. return rc;
  907. }
  908. /**
  909. * board_added - Called after a board has been added to the system.
  910. *
  911. * Turns power on for the board
  912. * Configures board
  913. *
  914. */
  915. static u32 board_added(struct pci_func * func, struct controller * ctrl)
  916. {
  917. u8 hp_slot;
  918. u8 slots_not_empty = 0;
  919. int index;
  920. u32 temp_register = 0xFFFFFFFF;
  921. u32 retval, rc = 0;
  922. struct pci_func *new_func = NULL;
  923. struct slot *p_slot;
  924. struct resource_lists res_lists;
  925. enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
  926. u8 pi, mode;
  927. p_slot = shpchp_find_slot(ctrl, func->device);
  928. hp_slot = func->device - ctrl->slot_device_offset;
  929. dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
  930. /* Wait for exclusive access to hardware */
  931. down(&ctrl->crit_sect);
  932. /* Power on slot without connecting to bus */
  933. rc = p_slot->hpc_ops->power_on_slot(p_slot);
  934. if (rc) {
  935. err("%s: Failed to power on slot\n", __FUNCTION__);
  936. /* Done with exclusive hardware access */
  937. up(&ctrl->crit_sect);
  938. return -1;
  939. }
  940. /* Wait for the command to complete */
  941. wait_for_ctrl_irq (ctrl);
  942. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  943. if (rc) {
  944. err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
  945. /* Done with exclusive hardware access */
  946. up(&ctrl->crit_sect);
  947. return -1;
  948. }
  949. if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
  950. if (slots_not_empty)
  951. return WRONG_BUS_FREQUENCY;
  952. if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz))) {
  953. err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
  954. up(&ctrl->crit_sect);
  955. return WRONG_BUS_FREQUENCY;
  956. }
  957. wait_for_ctrl_irq (ctrl);
  958. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  959. err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
  960. __FUNCTION__);
  961. err("%s: Error code (%d)\n", __FUNCTION__, rc);
  962. up(&ctrl->crit_sect);
  963. return WRONG_BUS_FREQUENCY;
  964. }
  965. /* turn on board, blink green LED, turn off Amber LED */
  966. if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
  967. err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
  968. up(&ctrl->crit_sect);
  969. return rc;
  970. }
  971. wait_for_ctrl_irq (ctrl);
  972. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  973. err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
  974. up(&ctrl->crit_sect);
  975. return rc;
  976. }
  977. }
  978. rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
  979. /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
  980. /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
  981. /* 0xd = PCI-X 133 Mhz 533 */
  982. /* This encoding is different from the one used in cur_bus_speed & */
  983. /* max_bus_speed */
  984. if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
  985. err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
  986. /* Done with exclusive hardware access */
  987. up(&ctrl->crit_sect);
  988. return WRONG_BUS_FREQUENCY;
  989. }
  990. rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
  991. if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
  992. err("%s: Can't get bus operation speed\n", __FUNCTION__);
  993. /* Done with exclusive hardware access */
  994. up(&ctrl->crit_sect);
  995. return WRONG_BUS_FREQUENCY;
  996. }
  997. rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
  998. if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
  999. err("%s: Can't get max bus operation speed\n", __FUNCTION__);
  1000. max_bus_speed = bus_speed;
  1001. }
  1002. /* Done with exclusive hardware access */
  1003. up(&ctrl->crit_sect);
  1004. if ((rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi))) {
  1005. err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
  1006. pi = 1;
  1007. }
  1008. /* Check if there are other slots or devices on the same bus */
  1009. if (!list_empty(&ctrl->pci_dev->subordinate->devices))
  1010. slots_not_empty = 1;
  1011. dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__,
  1012. slots_not_empty, pi);
  1013. dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
  1014. adapter_speed, bus_speed, max_bus_speed);
  1015. if (pi == 2) {
  1016. dbg("%s: In PI = %d\n", __FUNCTION__, pi);
  1017. if ((rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode))) {
  1018. err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__);
  1019. mode = 0;
  1020. }
  1021. switch (adapter_speed) {
  1022. case PCI_SPEED_133MHz_PCIX_533:
  1023. case PCI_SPEED_133MHz_PCIX_266:
  1024. if ((bus_speed != adapter_speed) &&
  1025. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  1026. return rc;
  1027. break;
  1028. case PCI_SPEED_133MHz_PCIX_ECC:
  1029. case PCI_SPEED_133MHz_PCIX:
  1030. if (mode) { /* Bus - Mode 1 ECC */
  1031. if ((bus_speed != 0x7) &&
  1032. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  1033. return rc;
  1034. } else {
  1035. if ((bus_speed != 0x4) &&
  1036. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  1037. return rc;
  1038. }
  1039. break;
  1040. case PCI_SPEED_66MHz_PCIX_ECC:
  1041. case PCI_SPEED_66MHz_PCIX:
  1042. if (mode) { /* Bus - Mode 1 ECC */
  1043. if ((bus_speed != 0x5) &&
  1044. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  1045. return rc;
  1046. } else {
  1047. if ((bus_speed != 0x2) &&
  1048. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  1049. return rc;
  1050. }
  1051. break;
  1052. case PCI_SPEED_66MHz:
  1053. if ((bus_speed != 0x1) &&
  1054. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  1055. return rc;
  1056. break;
  1057. case PCI_SPEED_33MHz:
  1058. if (bus_speed > 0x0) {
  1059. if (slots_not_empty == 0) {
  1060. if ((rc = change_bus_speed(ctrl, p_slot, adapter_speed)))
  1061. return rc;
  1062. } else {
  1063. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
  1064. return WRONG_BUS_FREQUENCY;
  1065. }
  1066. }
  1067. break;
  1068. default:
  1069. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
  1070. return WRONG_BUS_FREQUENCY;
  1071. }
  1072. } else {
  1073. /* If adpater_speed == bus_speed, nothing to do here */
  1074. dbg("%s: In PI = %d\n", __FUNCTION__, pi);
  1075. if ((adapter_speed != bus_speed) &&
  1076. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  1077. return rc;
  1078. }
  1079. down(&ctrl->crit_sect);
  1080. /* turn on board, blink green LED, turn off Amber LED */
  1081. if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
  1082. err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
  1083. up(&ctrl->crit_sect);
  1084. return rc;
  1085. }
  1086. wait_for_ctrl_irq (ctrl);
  1087. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  1088. err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
  1089. up(&ctrl->crit_sect);
  1090. return rc;
  1091. }
  1092. up(&ctrl->crit_sect);
  1093. /* Wait for ~1 second */
  1094. dbg("%s: before long_delay\n", __FUNCTION__);
  1095. wait_for_ctrl_irq (ctrl);
  1096. dbg("%s: after long_delay\n", __FUNCTION__);
  1097. dbg("%s: func status = %x\n", __FUNCTION__, func->status);
  1098. /* Check for a power fault */
  1099. if (func->status == 0xFF) {
  1100. /* power fault occurred, but it was benign */
  1101. temp_register = 0xFFFFFFFF;
  1102. dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
  1103. rc = POWER_FAILURE;
  1104. func->status = 0;
  1105. } else {
  1106. /* Get vendor/device ID u32 */
  1107. rc = pci_bus_read_config_dword (ctrl->pci_dev->subordinate, PCI_DEVFN(func->device, func->function),
  1108. PCI_VENDOR_ID, &temp_register);
  1109. dbg("%s: pci_bus_read_config_dword returns %d\n", __FUNCTION__, rc);
  1110. dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
  1111. if (rc != 0) {
  1112. /* Something's wrong here */
  1113. temp_register = 0xFFFFFFFF;
  1114. dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
  1115. }
  1116. /* Preset return code. It will be changed later if things go okay. */
  1117. rc = NO_ADAPTER_PRESENT;
  1118. }
  1119. /* All F's is an empty slot or an invalid board */
  1120. if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
  1121. res_lists.io_head = ctrl->io_head;
  1122. res_lists.mem_head = ctrl->mem_head;
  1123. res_lists.p_mem_head = ctrl->p_mem_head;
  1124. res_lists.bus_head = ctrl->bus_head;
  1125. res_lists.irqs = NULL;
  1126. rc = configure_new_device(ctrl, func, 0, &res_lists, 0, 0);
  1127. dbg("%s: back from configure_new_device\n", __FUNCTION__);
  1128. ctrl->io_head = res_lists.io_head;
  1129. ctrl->mem_head = res_lists.mem_head;
  1130. ctrl->p_mem_head = res_lists.p_mem_head;
  1131. ctrl->bus_head = res_lists.bus_head;
  1132. shpchp_resource_sort_and_combine(&(ctrl->mem_head));
  1133. shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1134. shpchp_resource_sort_and_combine(&(ctrl->io_head));
  1135. shpchp_resource_sort_and_combine(&(ctrl->bus_head));
  1136. if (rc) {
  1137. /* Wait for exclusive access to hardware */
  1138. down(&ctrl->crit_sect);
  1139. /* turn off slot, turn on Amber LED, turn off Green LED */
  1140. retval = p_slot->hpc_ops->slot_disable(p_slot);
  1141. if (retval) {
  1142. err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
  1143. /* Done with exclusive hardware access */
  1144. up(&ctrl->crit_sect);
  1145. return retval;
  1146. }
  1147. /* Wait for the command to complete */
  1148. wait_for_ctrl_irq (ctrl);
  1149. retval = p_slot->hpc_ops->check_cmd_status(ctrl);
  1150. if (retval) {
  1151. err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, retval);
  1152. /* Done with exclusive hardware access */
  1153. up(&ctrl->crit_sect);
  1154. return retval;
  1155. }
  1156. /* Done with exclusive hardware access */
  1157. up(&ctrl->crit_sect);
  1158. return(rc);
  1159. }
  1160. shpchp_save_slot_config(ctrl, func);
  1161. func->status = 0;
  1162. func->switch_save = 0x10;
  1163. func->is_a_board = 0x01;
  1164. func->pwr_save = 1;
  1165. /* Next, we will instantiate the linux pci_dev structures
  1166. * (with appropriate driver notification, if already present)
  1167. */
  1168. index = 0;
  1169. do {
  1170. new_func = shpchp_slot_find(ctrl->slot_bus, func->device, index++);
  1171. if (new_func && !new_func->pci_dev) {
  1172. dbg("%s:call pci_hp_configure_dev\n", __FUNCTION__);
  1173. shpchp_configure_device(ctrl, new_func);
  1174. }
  1175. } while (new_func);
  1176. /* Wait for exclusive access to hardware */
  1177. down(&ctrl->crit_sect);
  1178. p_slot->hpc_ops->green_led_on(p_slot);
  1179. /* Wait for the command to complete */
  1180. wait_for_ctrl_irq (ctrl);
  1181. /* Done with exclusive hardware access */
  1182. up(&ctrl->crit_sect);
  1183. } else {
  1184. /* Wait for exclusive access to hardware */
  1185. down(&ctrl->crit_sect);
  1186. /* turn off slot, turn on Amber LED, turn off Green LED */
  1187. rc = p_slot->hpc_ops->slot_disable(p_slot);
  1188. if (rc) {
  1189. err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
  1190. /* Done with exclusive hardware access */
  1191. up(&ctrl->crit_sect);
  1192. return rc;
  1193. }
  1194. /* Wait for the command to complete */
  1195. wait_for_ctrl_irq (ctrl);
  1196. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  1197. if (rc) {
  1198. err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
  1199. /* Done with exclusive hardware access */
  1200. up(&ctrl->crit_sect);
  1201. return rc;
  1202. }
  1203. /* Done with exclusive hardware access */
  1204. up(&ctrl->crit_sect);
  1205. return(rc);
  1206. }
  1207. return 0;
  1208. }
  1209. /**
  1210. * remove_board - Turns off slot and LED's
  1211. *
  1212. */
  1213. static u32 remove_board(struct pci_func *func, struct controller *ctrl)
  1214. {
  1215. int index;
  1216. u8 skip = 0;
  1217. u8 device;
  1218. u8 hp_slot;
  1219. u32 rc;
  1220. struct resource_lists res_lists;
  1221. struct pci_func *temp_func;
  1222. struct slot *p_slot;
  1223. if (func == NULL)
  1224. return(1);
  1225. if (shpchp_unconfigure_device(func))
  1226. return(1);
  1227. device = func->device;
  1228. hp_slot = func->device - ctrl->slot_device_offset;
  1229. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1230. dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
  1231. if ((ctrl->add_support) &&
  1232. !(func->bus_head || func->mem_head || func->p_mem_head || func->io_head)) {
  1233. /* Here we check to see if we've saved any of the board's
  1234. * resources already. If so, we'll skip the attempt to
  1235. * determine what's being used.
  1236. */
  1237. index = 0;
  1238. temp_func = func;
  1239. while ((temp_func = shpchp_slot_find(temp_func->bus, temp_func->device, index++))) {
  1240. if (temp_func->bus_head || temp_func->mem_head
  1241. || temp_func->p_mem_head || temp_func->io_head) {
  1242. skip = 1;
  1243. break;
  1244. }
  1245. }
  1246. if (!skip)
  1247. rc = shpchp_save_used_resources(ctrl, func, DISABLE_CARD);
  1248. }
  1249. /* Change status to shutdown */
  1250. if (func->is_a_board)
  1251. func->status = 0x01;
  1252. func->configured = 0;
  1253. /* Wait for exclusive access to hardware */
  1254. down(&ctrl->crit_sect);
  1255. /* turn off slot, turn on Amber LED, turn off Green LED */
  1256. rc = p_slot->hpc_ops->slot_disable(p_slot);
  1257. if (rc) {
  1258. err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
  1259. /* Done with exclusive hardware access */
  1260. up(&ctrl->crit_sect);
  1261. return rc;
  1262. }
  1263. /* Wait for the command to complete */
  1264. wait_for_ctrl_irq (ctrl);
  1265. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  1266. if (rc) {
  1267. err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
  1268. /* Done with exclusive hardware access */
  1269. up(&ctrl->crit_sect);
  1270. return rc;
  1271. }
  1272. rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
  1273. if (rc) {
  1274. err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
  1275. /* Done with exclusive hardware access */
  1276. up(&ctrl->crit_sect);
  1277. return rc;
  1278. }
  1279. /* Wait for the command to complete */
  1280. wait_for_ctrl_irq (ctrl);
  1281. /* Done with exclusive hardware access */
  1282. up(&ctrl->crit_sect);
  1283. if (ctrl->add_support) {
  1284. while (func) {
  1285. res_lists.io_head = ctrl->io_head;
  1286. res_lists.mem_head = ctrl->mem_head;
  1287. res_lists.p_mem_head = ctrl->p_mem_head;
  1288. res_lists.bus_head = ctrl->bus_head;
  1289. dbg("Returning resources to ctlr lists for (B/D/F) = (%#x/%#x/%#x)\n", func->bus,
  1290. func->device, func->function);
  1291. shpchp_return_board_resources(func, &res_lists);
  1292. ctrl->io_head = res_lists.io_head;
  1293. ctrl->mem_head = res_lists.mem_head;
  1294. ctrl->p_mem_head = res_lists.p_mem_head;
  1295. ctrl->bus_head = res_lists.bus_head;
  1296. shpchp_resource_sort_and_combine(&(ctrl->mem_head));
  1297. shpchp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1298. shpchp_resource_sort_and_combine(&(ctrl->io_head));
  1299. shpchp_resource_sort_and_combine(&(ctrl->bus_head));
  1300. if (is_bridge(func)) {
  1301. dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
  1302. func->device, func->function);
  1303. bridge_slot_remove(func);
  1304. } else
  1305. dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
  1306. func->device, func->function);
  1307. slot_remove(func);
  1308. func = shpchp_slot_find(ctrl->slot_bus, device, 0);
  1309. }
  1310. /* Setup slot structure with entry for empty slot */
  1311. func = shpchp_slot_create(ctrl->slot_bus);
  1312. if (func == NULL) {
  1313. return(1);
  1314. }
  1315. func->bus = ctrl->slot_bus;
  1316. func->device = device;
  1317. func->function = 0;
  1318. func->configured = 0;
  1319. func->switch_save = 0x10;
  1320. func->pwr_save = 0;
  1321. func->is_a_board = 0;
  1322. }
  1323. return 0;
  1324. }
  1325. static void pushbutton_helper_thread (unsigned long data)
  1326. {
  1327. pushbutton_pending = data;
  1328. up(&event_semaphore);
  1329. }
  1330. /**
  1331. * shpchp_pushbutton_thread
  1332. *
  1333. * Scheduled procedure to handle blocking stuff for the pushbuttons
  1334. * Handles all pending events and exits.
  1335. *
  1336. */
  1337. static void shpchp_pushbutton_thread (unsigned long slot)
  1338. {
  1339. struct slot *p_slot = (struct slot *) slot;
  1340. u8 getstatus;
  1341. pushbutton_pending = 0;
  1342. if (!p_slot) {
  1343. dbg("%s: Error! slot NULL\n", __FUNCTION__);
  1344. return;
  1345. }
  1346. p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  1347. if (getstatus) {
  1348. p_slot->state = POWEROFF_STATE;
  1349. dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
  1350. shpchp_disable_slot(p_slot);
  1351. p_slot->state = STATIC_STATE;
  1352. } else {
  1353. p_slot->state = POWERON_STATE;
  1354. dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
  1355. if (shpchp_enable_slot(p_slot)) {
  1356. /* Wait for exclusive access to hardware */
  1357. down(&p_slot->ctrl->crit_sect);
  1358. p_slot->hpc_ops->green_led_off(p_slot);
  1359. /* Wait for the command to complete */
  1360. wait_for_ctrl_irq (p_slot->ctrl);
  1361. /* Done with exclusive hardware access */
  1362. up(&p_slot->ctrl->crit_sect);
  1363. }
  1364. p_slot->state = STATIC_STATE;
  1365. }
  1366. return;
  1367. }
  1368. /* this is the main worker thread */
  1369. static int event_thread(void* data)
  1370. {
  1371. struct controller *ctrl;
  1372. lock_kernel();
  1373. daemonize("shpchpd_event");
  1374. unlock_kernel();
  1375. while (1) {
  1376. dbg("!!!!event_thread sleeping\n");
  1377. down_interruptible (&event_semaphore);
  1378. dbg("event_thread woken finished = %d\n", event_finished);
  1379. if (event_finished || signal_pending(current))
  1380. break;
  1381. /* Do stuff here */
  1382. if (pushbutton_pending)
  1383. shpchp_pushbutton_thread(pushbutton_pending);
  1384. else
  1385. for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
  1386. interrupt_event_handler(ctrl);
  1387. }
  1388. dbg("event_thread signals exit\n");
  1389. up(&event_exit);
  1390. return 0;
  1391. }
  1392. int shpchp_event_start_thread (void)
  1393. {
  1394. int pid;
  1395. /* initialize our semaphores */
  1396. init_MUTEX_LOCKED(&event_exit);
  1397. event_finished=0;
  1398. init_MUTEX_LOCKED(&event_semaphore);
  1399. pid = kernel_thread(event_thread, NULL, 0);
  1400. if (pid < 0) {
  1401. err ("Can't start up our event thread\n");
  1402. return -1;
  1403. }
  1404. dbg("Our event thread pid = %d\n", pid);
  1405. return 0;
  1406. }
  1407. void shpchp_event_stop_thread (void)
  1408. {
  1409. event_finished = 1;
  1410. dbg("event_thread finish command given\n");
  1411. up(&event_semaphore);
  1412. dbg("wait for event_thread to exit\n");
  1413. down(&event_exit);
  1414. }
  1415. static int update_slot_info (struct slot *slot)
  1416. {
  1417. struct hotplug_slot_info *info;
  1418. int result;
  1419. info = kmalloc(sizeof(*info), GFP_KERNEL);
  1420. if (!info)
  1421. return -ENOMEM;
  1422. slot->hpc_ops->get_power_status(slot, &(info->power_status));
  1423. slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
  1424. slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
  1425. slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
  1426. result = pci_hp_change_slot_info(slot->hotplug_slot, info);
  1427. kfree (info);
  1428. return result;
  1429. }
  1430. static void interrupt_event_handler(struct controller *ctrl)
  1431. {
  1432. int loop = 0;
  1433. int change = 1;
  1434. struct pci_func *func;
  1435. u8 hp_slot;
  1436. u8 getstatus;
  1437. struct slot *p_slot;
  1438. dbg("%s:\n", __FUNCTION__);
  1439. while (change) {
  1440. change = 0;
  1441. for (loop = 0; loop < 10; loop++) {
  1442. if (ctrl->event_queue[loop].event_type != 0) {
  1443. dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
  1444. ctrl->event_queue[loop].event_type);
  1445. hp_slot = ctrl->event_queue[loop].hp_slot;
  1446. func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
  1447. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1448. dbg("%s: hp_slot %d, func %p, p_slot %p\n", __FUNCTION__, hp_slot, func, p_slot);
  1449. if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
  1450. dbg("%s: button cancel\n", __FUNCTION__);
  1451. del_timer(&p_slot->task_event);
  1452. switch (p_slot->state) {
  1453. case BLINKINGOFF_STATE:
  1454. /* Wait for exclusive access to hardware */
  1455. down(&ctrl->crit_sect);
  1456. p_slot->hpc_ops->green_led_on(p_slot);
  1457. /* Wait for the command to complete */
  1458. wait_for_ctrl_irq (ctrl);
  1459. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  1460. /* Wait for the command to complete */
  1461. wait_for_ctrl_irq (ctrl);
  1462. /* Done with exclusive hardware access */
  1463. up(&ctrl->crit_sect);
  1464. break;
  1465. case BLINKINGON_STATE:
  1466. /* Wait for exclusive access to hardware */
  1467. down(&ctrl->crit_sect);
  1468. p_slot->hpc_ops->green_led_off(p_slot);
  1469. /* Wait for the command to complete */
  1470. wait_for_ctrl_irq (ctrl);
  1471. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  1472. /* Wait for the command to complete */
  1473. wait_for_ctrl_irq (ctrl);
  1474. /* Done with exclusive hardware access */
  1475. up(&ctrl->crit_sect);
  1476. break;
  1477. default:
  1478. warn("Not a valid state\n");
  1479. return;
  1480. }
  1481. info(msg_button_cancel, p_slot->number);
  1482. p_slot->state = STATIC_STATE;
  1483. } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
  1484. /* Button Pressed (No action on 1st press...) */
  1485. dbg("%s: Button pressed\n", __FUNCTION__);
  1486. p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  1487. if (getstatus) {
  1488. /* slot is on */
  1489. dbg("%s: slot is on\n", __FUNCTION__);
  1490. p_slot->state = BLINKINGOFF_STATE;
  1491. info(msg_button_off, p_slot->number);
  1492. } else {
  1493. /* slot is off */
  1494. dbg("%s: slot is off\n", __FUNCTION__);
  1495. p_slot->state = BLINKINGON_STATE;
  1496. info(msg_button_on, p_slot->number);
  1497. }
  1498. /* Wait for exclusive access to hardware */
  1499. down(&ctrl->crit_sect);
  1500. /* blink green LED and turn off amber */
  1501. p_slot->hpc_ops->green_led_blink(p_slot);
  1502. /* Wait for the command to complete */
  1503. wait_for_ctrl_irq (ctrl);
  1504. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  1505. /* Wait for the command to complete */
  1506. wait_for_ctrl_irq (ctrl);
  1507. /* Done with exclusive hardware access */
  1508. up(&ctrl->crit_sect);
  1509. init_timer(&p_slot->task_event);
  1510. p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
  1511. p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
  1512. p_slot->task_event.data = (unsigned long) p_slot;
  1513. dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
  1514. add_timer(&p_slot->task_event);
  1515. } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
  1516. /***********POWER FAULT********************/
  1517. dbg("%s: power fault\n", __FUNCTION__);
  1518. /* Wait for exclusive access to hardware */
  1519. down(&ctrl->crit_sect);
  1520. p_slot->hpc_ops->set_attention_status(p_slot, 1);
  1521. /* Wait for the command to complete */
  1522. wait_for_ctrl_irq (ctrl);
  1523. p_slot->hpc_ops->green_led_off(p_slot);
  1524. /* Wait for the command to complete */
  1525. wait_for_ctrl_irq (ctrl);
  1526. /* Done with exclusive hardware access */
  1527. up(&ctrl->crit_sect);
  1528. } else {
  1529. /* refresh notification */
  1530. if (p_slot)
  1531. update_slot_info(p_slot);
  1532. }
  1533. ctrl->event_queue[loop].event_type = 0;
  1534. change = 1;
  1535. }
  1536. } /* End of FOR loop */
  1537. }
  1538. return;
  1539. }
  1540. int shpchp_enable_slot (struct slot *p_slot)
  1541. {
  1542. u8 getstatus = 0;
  1543. int rc;
  1544. struct pci_func *func;
  1545. func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
  1546. if (!func) {
  1547. dbg("%s: Error! slot NULL\n", __FUNCTION__);
  1548. return -ENODEV;
  1549. }
  1550. /* Check to see if (latch closed, card present, power off) */
  1551. down(&p_slot->ctrl->crit_sect);
  1552. rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
  1553. if (rc || !getstatus) {
  1554. info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
  1555. up(&p_slot->ctrl->crit_sect);
  1556. return -ENODEV;
  1557. }
  1558. rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  1559. if (rc || getstatus) {
  1560. info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
  1561. up(&p_slot->ctrl->crit_sect);
  1562. return -ENODEV;
  1563. }
  1564. rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  1565. if (rc || getstatus) {
  1566. info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
  1567. up(&p_slot->ctrl->crit_sect);
  1568. return -ENODEV;
  1569. }
  1570. up(&p_slot->ctrl->crit_sect);
  1571. slot_remove(func);
  1572. func = shpchp_slot_create(p_slot->bus);
  1573. if (func == NULL)
  1574. return -ENOMEM;
  1575. func->bus = p_slot->bus;
  1576. func->device = p_slot->device;
  1577. func->function = 0;
  1578. func->configured = 0;
  1579. func->is_a_board = 1;
  1580. /* We have to save the presence info for these slots */
  1581. p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
  1582. p_slot->hpc_ops->get_power_status(p_slot, &(func->pwr_save));
  1583. dbg("%s: func->pwr_save %x\n", __FUNCTION__, func->pwr_save);
  1584. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  1585. func->switch_save = !getstatus? 0x10:0;
  1586. rc = board_added(func, p_slot->ctrl);
  1587. if (rc) {
  1588. if (is_bridge(func))
  1589. bridge_slot_remove(func);
  1590. else
  1591. slot_remove(func);
  1592. /* Setup slot structure with entry for empty slot */
  1593. func = shpchp_slot_create(p_slot->bus);
  1594. if (func == NULL)
  1595. return -ENOMEM; /* Out of memory */
  1596. func->bus = p_slot->bus;
  1597. func->device = p_slot->device;
  1598. func->function = 0;
  1599. func->configured = 0;
  1600. func->is_a_board = 1;
  1601. /* We have to save the presence info for these slots */
  1602. p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
  1603. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  1604. func->switch_save = !getstatus? 0x10:0;
  1605. }
  1606. if (p_slot)
  1607. update_slot_info(p_slot);
  1608. return rc;
  1609. }
  1610. int shpchp_disable_slot (struct slot *p_slot)
  1611. {
  1612. u8 class_code, header_type, BCR;
  1613. u8 index = 0;
  1614. u8 getstatus = 0;
  1615. u32 rc = 0;
  1616. int ret = 0;
  1617. unsigned int devfn;
  1618. struct pci_bus *pci_bus;
  1619. struct pci_func *func;
  1620. if (!p_slot->ctrl)
  1621. return -ENODEV;
  1622. pci_bus = p_slot->ctrl->pci_dev->subordinate;
  1623. /* Check to see if (latch closed, card present, power on) */
  1624. down(&p_slot->ctrl->crit_sect);
  1625. ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
  1626. if (ret || !getstatus) {
  1627. info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
  1628. up(&p_slot->ctrl->crit_sect);
  1629. return -ENODEV;
  1630. }
  1631. ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  1632. if (ret || getstatus) {
  1633. info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
  1634. up(&p_slot->ctrl->crit_sect);
  1635. return -ENODEV;
  1636. }
  1637. ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  1638. if (ret || !getstatus) {
  1639. info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
  1640. up(&p_slot->ctrl->crit_sect);
  1641. return -ENODEV;
  1642. }
  1643. up(&p_slot->ctrl->crit_sect);
  1644. func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
  1645. /* Make sure there are no video controllers here
  1646. * for all func of p_slot
  1647. */
  1648. while (func && !rc) {
  1649. pci_bus->number = func->bus;
  1650. devfn = PCI_DEVFN(func->device, func->function);
  1651. /* Check the Class Code */
  1652. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  1653. if (rc)
  1654. return -ENODEV;
  1655. if (class_code == PCI_BASE_CLASS_DISPLAY) {
  1656. /* Display/Video adapter (not supported) */
  1657. rc = REMOVE_NOT_SUPPORTED;
  1658. } else {
  1659. /* See if it's a bridge */
  1660. rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
  1661. if (rc)
  1662. return -ENODEV;
  1663. /* If it's a bridge, check the VGA Enable bit */
  1664. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  1665. rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
  1666. if (rc)
  1667. return -ENODEV;
  1668. /* If the VGA Enable bit is set, remove isn't supported */
  1669. if (BCR & PCI_BRIDGE_CTL_VGA) {
  1670. rc = REMOVE_NOT_SUPPORTED;
  1671. }
  1672. }
  1673. }
  1674. func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
  1675. }
  1676. func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
  1677. if ((func != NULL) && !rc) {
  1678. rc = remove_board(func, p_slot->ctrl);
  1679. } else if (!rc)
  1680. rc = -ENODEV;
  1681. if (p_slot)
  1682. update_slot_info(p_slot);
  1683. return rc;
  1684. }
  1685. /**
  1686. * configure_new_device - Configures the PCI header information of one board.
  1687. *
  1688. * @ctrl: pointer to controller structure
  1689. * @func: pointer to function structure
  1690. * @behind_bridge: 1 if this is a recursive call, 0 if not
  1691. * @resources: pointer to set of resource lists
  1692. *
  1693. * Returns 0 if success
  1694. *
  1695. */
  1696. static u32 configure_new_device (struct controller * ctrl, struct pci_func * func,
  1697. u8 behind_bridge, struct resource_lists * resources, u8 bridge_bus, u8 bridge_dev)
  1698. {
  1699. u8 temp_byte, function, max_functions, stop_it;
  1700. int rc;
  1701. u32 ID;
  1702. struct pci_func *new_slot;
  1703. struct pci_bus lpci_bus, *pci_bus;
  1704. int index;
  1705. new_slot = func;
  1706. dbg("%s\n", __FUNCTION__);
  1707. memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
  1708. pci_bus = &lpci_bus;
  1709. pci_bus->number = func->bus;
  1710. /* Check for Multi-function device */
  1711. rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
  1712. if (rc) {
  1713. dbg("%s: rc = %d\n", __FUNCTION__, rc);
  1714. return rc;
  1715. }
  1716. if (temp_byte & 0x80) /* Multi-function device */
  1717. max_functions = 8;
  1718. else
  1719. max_functions = 1;
  1720. function = 0;
  1721. do {
  1722. rc = configure_new_function(ctrl, new_slot, behind_bridge, resources, bridge_bus, bridge_dev);
  1723. if (rc) {
  1724. dbg("configure_new_function failed %d\n",rc);
  1725. index = 0;
  1726. while (new_slot) {
  1727. new_slot = shpchp_slot_find(new_slot->bus, new_slot->device, index++);
  1728. if (new_slot)
  1729. shpchp_return_board_resources(new_slot, resources);
  1730. }
  1731. return(rc);
  1732. }
  1733. function++;
  1734. stop_it = 0;
  1735. /* The following loop skips to the next present function
  1736. * and creates a board structure
  1737. */
  1738. while ((function < max_functions) && (!stop_it)) {
  1739. pci_bus_read_config_dword(pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
  1740. if (ID == 0xFFFFFFFF) { /* There's nothing there. */
  1741. function++;
  1742. } else { /* There's something there */
  1743. /* Setup slot structure. */
  1744. new_slot = shpchp_slot_create(func->bus);
  1745. if (new_slot == NULL) {
  1746. /* Out of memory */
  1747. return(1);
  1748. }
  1749. new_slot->bus = func->bus;
  1750. new_slot->device = func->device;
  1751. new_slot->function = function;
  1752. new_slot->is_a_board = 1;
  1753. new_slot->status = 0;
  1754. stop_it++;
  1755. }
  1756. }
  1757. } while (function < max_functions);
  1758. dbg("returning from configure_new_device\n");
  1759. return 0;
  1760. }
  1761. /*
  1762. * Configuration logic that involves the hotplug data structures and
  1763. * their bookkeeping
  1764. */
  1765. /**
  1766. * configure_new_function - Configures the PCI header information of one device
  1767. *
  1768. * @ctrl: pointer to controller structure
  1769. * @func: pointer to function structure
  1770. * @behind_bridge: 1 if this is a recursive call, 0 if not
  1771. * @resources: pointer to set of resource lists
  1772. *
  1773. * Calls itself recursively for bridged devices.
  1774. * Returns 0 if success
  1775. *
  1776. */
  1777. static int configure_new_function (struct controller * ctrl, struct pci_func * func,
  1778. u8 behind_bridge, struct resource_lists *resources, u8 bridge_bus, u8 bridge_dev)
  1779. {
  1780. int cloop;
  1781. u8 temp_byte;
  1782. u8 device;
  1783. u8 class_code;
  1784. u16 temp_word;
  1785. u32 rc;
  1786. u32 temp_register;
  1787. u32 base;
  1788. u32 ID;
  1789. unsigned int devfn;
  1790. struct pci_resource *mem_node;
  1791. struct pci_resource *p_mem_node;
  1792. struct pci_resource *io_node;
  1793. struct pci_resource *bus_node;
  1794. struct pci_resource *hold_mem_node;
  1795. struct pci_resource *hold_p_mem_node;
  1796. struct pci_resource *hold_IO_node;
  1797. struct pci_resource *hold_bus_node;
  1798. struct irq_mapping irqs;
  1799. struct pci_func *new_slot;
  1800. struct pci_bus lpci_bus, *pci_bus;
  1801. struct resource_lists temp_resources;
  1802. #if defined(CONFIG_X86_64)
  1803. u8 IRQ=0;
  1804. #endif
  1805. memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
  1806. pci_bus = &lpci_bus;
  1807. pci_bus->number = func->bus;
  1808. devfn = PCI_DEVFN(func->device, func->function);
  1809. /* Check for Bridge */
  1810. rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
  1811. if (rc)
  1812. return rc;
  1813. if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
  1814. /* set Primary bus */
  1815. dbg("set Primary bus = 0x%x\n", func->bus);
  1816. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
  1817. if (rc)
  1818. return rc;
  1819. /* find range of busses to use */
  1820. bus_node = get_max_resource(&resources->bus_head, 1L);
  1821. /* If we don't have any busses to allocate, we can't continue */
  1822. if (!bus_node) {
  1823. err("Got NO bus resource to use\n");
  1824. return -ENOMEM;
  1825. }
  1826. dbg("Got ranges of buses to use: base:len=0x%x:%x\n", bus_node->base, bus_node->length);
  1827. /* set Secondary bus */
  1828. temp_byte = (u8)bus_node->base;
  1829. dbg("set Secondary bus = 0x%x\n", temp_byte);
  1830. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
  1831. if (rc)
  1832. return rc;
  1833. /* set subordinate bus */
  1834. temp_byte = (u8)(bus_node->base + bus_node->length - 1);
  1835. dbg("set subordinate bus = 0x%x\n", temp_byte);
  1836. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
  1837. if (rc)
  1838. return rc;
  1839. /* Set HP parameters (Cache Line Size, Latency Timer) */
  1840. rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
  1841. if (rc)
  1842. return rc;
  1843. /* Setup the IO, memory, and prefetchable windows */
  1844. io_node = get_max_resource(&(resources->io_head), 0x1000L);
  1845. if (io_node) {
  1846. dbg("io_node(base, len, next) (%x, %x, %p)\n", io_node->base, io_node->length, io_node->next);
  1847. }
  1848. mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
  1849. if (mem_node) {
  1850. dbg("mem_node(base, len, next) (%x, %x, %p)\n", mem_node->base, mem_node->length, mem_node->next);
  1851. }
  1852. if (resources->p_mem_head)
  1853. p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000L);
  1854. else {
  1855. /*
  1856. * In some platform implementation, MEM and PMEM are not
  1857. * distinguished, and hence ACPI _CRS has only MEM entries
  1858. * for both MEM and PMEM.
  1859. */
  1860. dbg("using MEM for PMEM\n");
  1861. p_mem_node = get_max_resource(&(resources->mem_head), 0x100000L);
  1862. }
  1863. if (p_mem_node) {
  1864. dbg("p_mem_node(base, len, next) (%x, %x, %p)\n", p_mem_node->base, p_mem_node->length, p_mem_node->next);
  1865. }
  1866. /* set up the IRQ info */
  1867. if (!resources->irqs) {
  1868. irqs.barber_pole = 0;
  1869. irqs.interrupt[0] = 0;
  1870. irqs.interrupt[1] = 0;
  1871. irqs.interrupt[2] = 0;
  1872. irqs.interrupt[3] = 0;
  1873. irqs.valid_INT = 0;
  1874. } else {
  1875. irqs.barber_pole = resources->irqs->barber_pole;
  1876. irqs.interrupt[0] = resources->irqs->interrupt[0];
  1877. irqs.interrupt[1] = resources->irqs->interrupt[1];
  1878. irqs.interrupt[2] = resources->irqs->interrupt[2];
  1879. irqs.interrupt[3] = resources->irqs->interrupt[3];
  1880. irqs.valid_INT = resources->irqs->valid_INT;
  1881. }
  1882. /* set up resource lists that are now aligned on top and bottom
  1883. * for anything behind the bridge.
  1884. */
  1885. temp_resources.bus_head = bus_node;
  1886. temp_resources.io_head = io_node;
  1887. temp_resources.mem_head = mem_node;
  1888. temp_resources.p_mem_head = p_mem_node;
  1889. temp_resources.irqs = &irqs;
  1890. /* Make copies of the nodes we are going to pass down so that
  1891. * if there is a problem,we can just use these to free resources
  1892. */
  1893. hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
  1894. hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
  1895. hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
  1896. hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
  1897. if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
  1898. kfree(hold_bus_node);
  1899. kfree(hold_IO_node);
  1900. kfree(hold_mem_node);
  1901. kfree(hold_p_mem_node);
  1902. return 1;
  1903. }
  1904. memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
  1905. bus_node->base += 1;
  1906. bus_node->length -= 1;
  1907. bus_node->next = NULL;
  1908. /* If we have IO resources copy them and fill in the bridge's
  1909. * IO range registers
  1910. */
  1911. if (io_node) {
  1912. memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
  1913. io_node->next = NULL;
  1914. /* set IO base and Limit registers */
  1915. RES_CHECK(io_node->base, 8);
  1916. temp_byte = (u8)(io_node->base >> 8);
  1917. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
  1918. RES_CHECK(io_node->base + io_node->length - 1, 8);
  1919. temp_byte = (u8)((io_node->base + io_node->length - 1) >> 8);
  1920. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  1921. } else {
  1922. kfree(hold_IO_node);
  1923. hold_IO_node = NULL;
  1924. }
  1925. /* If we have memory resources copy them and fill in the bridge's
  1926. * memory range registers. Otherwise, fill in the range
  1927. * registers with values that disable them.
  1928. */
  1929. if (mem_node) {
  1930. memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
  1931. mem_node->next = NULL;
  1932. /* set Mem base and Limit registers */
  1933. RES_CHECK(mem_node->base, 16);
  1934. temp_word = (u32)(mem_node->base >> 16);
  1935. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  1936. RES_CHECK(mem_node->base + mem_node->length - 1, 16);
  1937. temp_word = (u32)((mem_node->base + mem_node->length - 1) >> 16);
  1938. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  1939. } else {
  1940. temp_word = 0xFFFF;
  1941. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  1942. temp_word = 0x0000;
  1943. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  1944. kfree(hold_mem_node);
  1945. hold_mem_node = NULL;
  1946. }
  1947. /* If we have prefetchable memory resources copy them and
  1948. * fill in the bridge's memory range registers. Otherwise,
  1949. * fill in the range registers with values that disable them.
  1950. */
  1951. if (p_mem_node) {
  1952. memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
  1953. p_mem_node->next = NULL;
  1954. /* set Pre Mem base and Limit registers */
  1955. RES_CHECK(p_mem_node->base, 16);
  1956. temp_word = (u32)(p_mem_node->base >> 16);
  1957. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  1958. RES_CHECK(p_mem_node->base + p_mem_node->length - 1, 16);
  1959. temp_word = (u32)((p_mem_node->base + p_mem_node->length - 1) >> 16);
  1960. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  1961. } else {
  1962. temp_word = 0xFFFF;
  1963. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  1964. temp_word = 0x0000;
  1965. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  1966. kfree(hold_p_mem_node);
  1967. hold_p_mem_node = NULL;
  1968. }
  1969. /* Adjust this to compensate for extra adjustment in first loop */
  1970. irqs.barber_pole--;
  1971. rc = 0;
  1972. /* Here we actually find the devices and configure them */
  1973. for (device = 0; (device <= 0x1F) && !rc; device++) {
  1974. irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
  1975. ID = 0xFFFFFFFF;
  1976. pci_bus->number = hold_bus_node->base;
  1977. pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0),
  1978. PCI_VENDOR_ID, &ID);
  1979. pci_bus->number = func->bus;
  1980. if (ID != 0xFFFFFFFF) { /* device Present */
  1981. /* Setup slot structure. */
  1982. new_slot = shpchp_slot_create(hold_bus_node->base);
  1983. if (new_slot == NULL) {
  1984. /* Out of memory */
  1985. rc = -ENOMEM;
  1986. continue;
  1987. }
  1988. new_slot->bus = hold_bus_node->base;
  1989. new_slot->device = device;
  1990. new_slot->function = 0;
  1991. new_slot->is_a_board = 1;
  1992. new_slot->status = 0;
  1993. rc = configure_new_device(ctrl, new_slot, 1, &temp_resources, func->bus, func->device);
  1994. dbg("configure_new_device rc=0x%x\n",rc);
  1995. } /* End of IF (device in slot?) */
  1996. } /* End of FOR loop */
  1997. if (rc) {
  1998. shpchp_destroy_resource_list(&temp_resources);
  1999. return_resource(&(resources->bus_head), hold_bus_node);
  2000. return_resource(&(resources->io_head), hold_IO_node);
  2001. return_resource(&(resources->mem_head), hold_mem_node);
  2002. return_resource(&(resources->p_mem_head), hold_p_mem_node);
  2003. return(rc);
  2004. }
  2005. /* save the interrupt routing information */
  2006. if (resources->irqs) {
  2007. resources->irqs->interrupt[0] = irqs.interrupt[0];
  2008. resources->irqs->interrupt[1] = irqs.interrupt[1];
  2009. resources->irqs->interrupt[2] = irqs.interrupt[2];
  2010. resources->irqs->interrupt[3] = irqs.interrupt[3];
  2011. resources->irqs->valid_INT = irqs.valid_INT;
  2012. } else if (!behind_bridge) {
  2013. /* We need to hook up the interrupts here */
  2014. for (cloop = 0; cloop < 4; cloop++) {
  2015. if (irqs.valid_INT & (0x01 << cloop)) {
  2016. rc = shpchp_set_irq(func->bus, func->device,
  2017. 0x0A + cloop, irqs.interrupt[cloop]);
  2018. if (rc) {
  2019. shpchp_destroy_resource_list (&temp_resources);
  2020. return_resource(&(resources->bus_head), hold_bus_node);
  2021. return_resource(&(resources->io_head), hold_IO_node);
  2022. return_resource(&(resources->mem_head), hold_mem_node);
  2023. return_resource(&(resources->p_mem_head), hold_p_mem_node);
  2024. return rc;
  2025. }
  2026. }
  2027. } /* end of for loop */
  2028. }
  2029. /* Return unused bus resources
  2030. * First use the temporary node to store information for the board
  2031. */
  2032. if (hold_bus_node && bus_node && temp_resources.bus_head) {
  2033. hold_bus_node->length = bus_node->base - hold_bus_node->base;
  2034. hold_bus_node->next = func->bus_head;
  2035. func->bus_head = hold_bus_node;
  2036. temp_byte = (u8)(temp_resources.bus_head->base - 1);
  2037. /* set subordinate bus */
  2038. dbg("re-set subordinate bus = 0x%x\n", temp_byte);
  2039. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
  2040. if (temp_resources.bus_head->length == 0) {
  2041. kfree(temp_resources.bus_head);
  2042. temp_resources.bus_head = NULL;
  2043. } else {
  2044. dbg("return bus res of b:d(0x%x:%x) base:len(0x%x:%x)\n",
  2045. func->bus, func->device, temp_resources.bus_head->base, temp_resources.bus_head->length);
  2046. return_resource(&(resources->bus_head), temp_resources.bus_head);
  2047. }
  2048. }
  2049. /* If we have IO space available and there is some left,
  2050. * return the unused portion
  2051. */
  2052. if (hold_IO_node && temp_resources.io_head) {
  2053. io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
  2054. &hold_IO_node, 0x1000);
  2055. /* Check if we were able to split something off */
  2056. if (io_node) {
  2057. hold_IO_node->base = io_node->base + io_node->length;
  2058. RES_CHECK(hold_IO_node->base, 8);
  2059. temp_byte = (u8)((hold_IO_node->base) >> 8);
  2060. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_BASE, temp_byte);
  2061. return_resource(&(resources->io_head), io_node);
  2062. }
  2063. io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
  2064. /* Check if we were able to split something off */
  2065. if (io_node) {
  2066. /* First use the temporary node to store information for the board */
  2067. hold_IO_node->length = io_node->base - hold_IO_node->base;
  2068. /* If we used any, add it to the board's list */
  2069. if (hold_IO_node->length) {
  2070. hold_IO_node->next = func->io_head;
  2071. func->io_head = hold_IO_node;
  2072. RES_CHECK(io_node->base - 1, 8);
  2073. temp_byte = (u8)((io_node->base - 1) >> 8);
  2074. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  2075. return_resource(&(resources->io_head), io_node);
  2076. } else {
  2077. /* it doesn't need any IO */
  2078. temp_byte = 0x00;
  2079. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  2080. return_resource(&(resources->io_head), io_node);
  2081. kfree(hold_IO_node);
  2082. }
  2083. } else {
  2084. /* it used most of the range */
  2085. hold_IO_node->next = func->io_head;
  2086. func->io_head = hold_IO_node;
  2087. }
  2088. } else if (hold_IO_node) {
  2089. /* it used the whole range */
  2090. hold_IO_node->next = func->io_head;
  2091. func->io_head = hold_IO_node;
  2092. }
  2093. /* If we have memory space available and there is some left,
  2094. * return the unused portion
  2095. */
  2096. if (hold_mem_node && temp_resources.mem_head) {
  2097. mem_node = do_pre_bridge_resource_split(&(temp_resources.mem_head), &hold_mem_node, 0x100000L);
  2098. /* Check if we were able to split something off */
  2099. if (mem_node) {
  2100. hold_mem_node->base = mem_node->base + mem_node->length;
  2101. RES_CHECK(hold_mem_node->base, 16);
  2102. temp_word = (u32)((hold_mem_node->base) >> 16);
  2103. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2104. return_resource(&(resources->mem_head), mem_node);
  2105. }
  2106. mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000L);
  2107. /* Check if we were able to split something off */
  2108. if (mem_node) {
  2109. /* First use the temporary node to store information for the board */
  2110. hold_mem_node->length = mem_node->base - hold_mem_node->base;
  2111. if (hold_mem_node->length) {
  2112. hold_mem_node->next = func->mem_head;
  2113. func->mem_head = hold_mem_node;
  2114. /* configure end address */
  2115. RES_CHECK(mem_node->base - 1, 16);
  2116. temp_word = (u32)((mem_node->base - 1) >> 16);
  2117. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2118. /* Return unused resources to the pool */
  2119. return_resource(&(resources->mem_head), mem_node);
  2120. } else {
  2121. /* it doesn't need any Mem */
  2122. temp_word = 0x0000;
  2123. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2124. return_resource(&(resources->mem_head), mem_node);
  2125. kfree(hold_mem_node);
  2126. }
  2127. } else {
  2128. /* it used most of the range */
  2129. hold_mem_node->next = func->mem_head;
  2130. func->mem_head = hold_mem_node;
  2131. }
  2132. } else if (hold_mem_node) {
  2133. /* it used the whole range */
  2134. hold_mem_node->next = func->mem_head;
  2135. func->mem_head = hold_mem_node;
  2136. }
  2137. /* If we have prefetchable memory space available and there is some
  2138. * left at the end, return the unused portion
  2139. */
  2140. if (hold_p_mem_node && temp_resources.p_mem_head) {
  2141. p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
  2142. &hold_p_mem_node, 0x100000L);
  2143. /* Check if we were able to split something off */
  2144. if (p_mem_node) {
  2145. hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
  2146. RES_CHECK(hold_p_mem_node->base, 16);
  2147. temp_word = (u32)((hold_p_mem_node->base) >> 16);
  2148. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  2149. return_resource(&(resources->p_mem_head), p_mem_node);
  2150. }
  2151. p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000L);
  2152. /* Check if we were able to split something off */
  2153. if (p_mem_node) {
  2154. /* First use the temporary node to store information for the board */
  2155. hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
  2156. /* If we used any, add it to the board's list */
  2157. if (hold_p_mem_node->length) {
  2158. hold_p_mem_node->next = func->p_mem_head;
  2159. func->p_mem_head = hold_p_mem_node;
  2160. RES_CHECK(p_mem_node->base - 1, 16);
  2161. temp_word = (u32)((p_mem_node->base - 1) >> 16);
  2162. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2163. return_resource(&(resources->p_mem_head), p_mem_node);
  2164. } else {
  2165. /* it doesn't need any PMem */
  2166. temp_word = 0x0000;
  2167. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2168. return_resource(&(resources->p_mem_head), p_mem_node);
  2169. kfree(hold_p_mem_node);
  2170. }
  2171. } else {
  2172. /* it used the most of the range */
  2173. hold_p_mem_node->next = func->p_mem_head;
  2174. func->p_mem_head = hold_p_mem_node;
  2175. }
  2176. } else if (hold_p_mem_node) {
  2177. /* it used the whole range */
  2178. hold_p_mem_node->next = func->p_mem_head;
  2179. func->p_mem_head = hold_p_mem_node;
  2180. }
  2181. /* We should be configuring an IRQ and the bridge's base address
  2182. * registers if it needs them. Although we have never seen such
  2183. * a device
  2184. */
  2185. shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_BRIDGE);
  2186. dbg("PCI Bridge Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
  2187. } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
  2188. /* Standard device */
  2189. u64 base64;
  2190. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  2191. if (class_code == PCI_BASE_CLASS_DISPLAY)
  2192. return (DEVICE_TYPE_NOT_SUPPORTED);
  2193. /* Figure out IO and memory needs */
  2194. for (cloop = PCI_BASE_ADDRESS_0; cloop <= PCI_BASE_ADDRESS_5; cloop += 4) {
  2195. temp_register = 0xFFFFFFFF;
  2196. rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
  2197. rc = pci_bus_read_config_dword(pci_bus, devfn, cloop, &temp_register);
  2198. dbg("Bar[%x]=0x%x on bus:dev:func(0x%x:%x:%x)\n", cloop, temp_register, func->bus, func->device,
  2199. func->function);
  2200. if (!temp_register)
  2201. continue;
  2202. base64 = 0L;
  2203. if (temp_register & PCI_BASE_ADDRESS_SPACE_IO) {
  2204. /* Map IO */
  2205. /* set base = amount of IO space */
  2206. base = temp_register & 0xFFFFFFFC;
  2207. base = ~base + 1;
  2208. dbg("NEED IO length(0x%x)\n", base);
  2209. io_node = get_io_resource(&(resources->io_head),(ulong)base);
  2210. /* allocate the resource to the board */
  2211. if (io_node) {
  2212. dbg("Got IO base=0x%x(length=0x%x)\n", io_node->base, io_node->length);
  2213. base = (u32)io_node->base;
  2214. io_node->next = func->io_head;
  2215. func->io_head = io_node;
  2216. } else {
  2217. err("Got NO IO resource(length=0x%x)\n", base);
  2218. return -ENOMEM;
  2219. }
  2220. } else { /* map MEM */
  2221. int prefetchable = 1;
  2222. struct pci_resource **res_node = &func->p_mem_head;
  2223. char *res_type_str = "PMEM";
  2224. u32 temp_register2;
  2225. if (!(temp_register & PCI_BASE_ADDRESS_MEM_PREFETCH)) {
  2226. prefetchable = 0;
  2227. res_node = &func->mem_head;
  2228. res_type_str++;
  2229. }
  2230. base = temp_register & 0xFFFFFFF0;
  2231. base = ~base + 1;
  2232. switch (temp_register & PCI_BASE_ADDRESS_MEM_TYPE_MASK) {
  2233. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  2234. dbg("NEED 32 %s bar=0x%x(length=0x%x)\n", res_type_str, temp_register, base);
  2235. if (prefetchable && resources->p_mem_head)
  2236. mem_node=get_resource(&(resources->p_mem_head), (ulong)base);
  2237. else {
  2238. if (prefetchable)
  2239. dbg("using MEM for PMEM\n");
  2240. mem_node=get_resource(&(resources->mem_head), (ulong)base);
  2241. }
  2242. /* allocate the resource to the board */
  2243. if (mem_node) {
  2244. base = (u32)mem_node->base;
  2245. mem_node->next = *res_node;
  2246. *res_node = mem_node;
  2247. dbg("Got 32 %s base=0x%x(length=0x%x)\n", res_type_str, mem_node->base,
  2248. mem_node->length);
  2249. } else {
  2250. err("Got NO 32 %s resource(length=0x%x)\n", res_type_str, base);
  2251. return -ENOMEM;
  2252. }
  2253. break;
  2254. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  2255. rc = pci_bus_read_config_dword(pci_bus, devfn, cloop+4, &temp_register2);
  2256. dbg("NEED 64 %s bar=0x%x:%x(length=0x%x)\n", res_type_str, temp_register2,
  2257. temp_register, base);
  2258. if (prefetchable && resources->p_mem_head)
  2259. mem_node = get_resource(&(resources->p_mem_head), (ulong)base);
  2260. else {
  2261. if (prefetchable)
  2262. dbg("using MEM for PMEM\n");
  2263. mem_node = get_resource(&(resources->mem_head), (ulong)base);
  2264. }
  2265. /* allocate the resource to the board */
  2266. if (mem_node) {
  2267. base64 = mem_node->base;
  2268. mem_node->next = *res_node;
  2269. *res_node = mem_node;
  2270. dbg("Got 64 %s base=0x%x:%x(length=%x)\n", res_type_str, (u32)(base64 >> 32),
  2271. (u32)base64, mem_node->length);
  2272. } else {
  2273. err("Got NO 64 %s resource(length=0x%x)\n", res_type_str, base);
  2274. return -ENOMEM;
  2275. }
  2276. break;
  2277. default:
  2278. dbg("reserved BAR type=0x%x\n", temp_register);
  2279. break;
  2280. }
  2281. }
  2282. if (base64) {
  2283. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
  2284. cloop += 4;
  2285. base64 >>= 32;
  2286. if (base64) {
  2287. dbg("%s: high dword of base64(0x%x) set to 0\n", __FUNCTION__, (u32)base64);
  2288. base64 = 0x0L;
  2289. }
  2290. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, (u32)base64);
  2291. } else {
  2292. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
  2293. }
  2294. } /* End of base register loop */
  2295. #if defined(CONFIG_X86_64)
  2296. /* Figure out which interrupt pin this function uses */
  2297. rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_INTERRUPT_PIN, &temp_byte);
  2298. /* If this function needs an interrupt and we are behind a bridge
  2299. and the pin is tied to something that's alread mapped,
  2300. set this one the same
  2301. */
  2302. if (temp_byte && resources->irqs &&
  2303. (resources->irqs->valid_INT &
  2304. (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
  2305. /* We have to share with something already set up */
  2306. IRQ = resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03];
  2307. } else {
  2308. /* Program IRQ based on card type */
  2309. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  2310. if (class_code == PCI_BASE_CLASS_STORAGE) {
  2311. IRQ = shpchp_disk_irq;
  2312. } else {
  2313. IRQ = shpchp_nic_irq;
  2314. }
  2315. }
  2316. /* IRQ Line */
  2317. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
  2318. if (!behind_bridge) {
  2319. rc = shpchp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
  2320. if (rc)
  2321. return(1);
  2322. } else {
  2323. /* TBD - this code may also belong in the other clause of this If statement */
  2324. resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
  2325. resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
  2326. }
  2327. #endif
  2328. /* Disable ROM base Address */
  2329. temp_word = 0x00L;
  2330. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_ROM_ADDRESS, temp_word);
  2331. /* Set HP parameters (Cache Line Size, Latency Timer) */
  2332. rc = shpchprm_set_hpp(ctrl, func, PCI_HEADER_TYPE_NORMAL);
  2333. if (rc)
  2334. return rc;
  2335. shpchprm_enable_card(ctrl, func, PCI_HEADER_TYPE_NORMAL);
  2336. dbg("PCI function Hot-Added s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus, func->device, func->function);
  2337. } /* End of Not-A-Bridge else */
  2338. else {
  2339. /* It's some strange type of PCI adapter (Cardbus?) */
  2340. return(DEVICE_TYPE_NOT_SUPPORTED);
  2341. }
  2342. func->configured = 1;
  2343. return 0;
  2344. }