cpqphp_ctrl.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096
  1. /*
  2. * Compaq 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. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Send feedback to <greg@kroah.com>
  26. *
  27. */
  28. #include <linux/config.h>
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/types.h>
  32. #include <linux/slab.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/delay.h>
  36. #include <linux/wait.h>
  37. #include <linux/smp_lock.h>
  38. #include <linux/pci.h>
  39. #include "cpqphp.h"
  40. static u32 configure_new_device(struct controller* ctrl, struct pci_func *func,
  41. u8 behind_bridge, struct resource_lists *resources);
  42. static int configure_new_function(struct controller* ctrl, struct pci_func *func,
  43. u8 behind_bridge, struct resource_lists *resources);
  44. static void interrupt_event_handler(struct controller *ctrl);
  45. static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
  46. static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
  47. static int event_finished;
  48. static unsigned long pushbutton_pending; /* = 0 */
  49. /* things needed for the long_delay function */
  50. static struct semaphore delay_sem;
  51. static wait_queue_head_t delay_wait;
  52. /* delay is in jiffies to wait for */
  53. static void long_delay(int delay)
  54. {
  55. DECLARE_WAITQUEUE(wait, current);
  56. /* only allow 1 customer into the delay queue at once
  57. * yes this makes some people wait even longer, but who really cares?
  58. * this is for _huge_ delays to make the hardware happy as the
  59. * signals bounce around
  60. */
  61. down (&delay_sem);
  62. init_waitqueue_head(&delay_wait);
  63. add_wait_queue(&delay_wait, &wait);
  64. msleep_interruptible(jiffies_to_msecs(delay));
  65. remove_wait_queue(&delay_wait, &wait);
  66. up(&delay_sem);
  67. }
  68. /* FIXME: The following line needs to be somewhere else... */
  69. #define WRONG_BUS_FREQUENCY 0x07
  70. static u8 handle_switch_change(u8 change, struct controller * ctrl)
  71. {
  72. int hp_slot;
  73. u8 rc = 0;
  74. u16 temp_word;
  75. struct pci_func *func;
  76. struct event_info *taskInfo;
  77. if (!change)
  78. return 0;
  79. /* Switch Change */
  80. dbg("cpqsbd: Switch interrupt received.\n");
  81. for (hp_slot = 0; hp_slot < 6; hp_slot++) {
  82. if (change & (0x1L << hp_slot)) {
  83. /**********************************
  84. * this one changed.
  85. **********************************/
  86. func = cpqhp_slot_find(ctrl->bus,
  87. (hp_slot + ctrl->slot_device_offset), 0);
  88. /* this is the structure that tells the worker thread
  89. *what to do */
  90. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  91. ctrl->next_event = (ctrl->next_event + 1) % 10;
  92. taskInfo->hp_slot = hp_slot;
  93. rc++;
  94. temp_word = ctrl->ctrl_int_comp >> 16;
  95. func->presence_save = (temp_word >> hp_slot) & 0x01;
  96. func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
  97. if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
  98. /**********************************
  99. * Switch opened
  100. **********************************/
  101. func->switch_save = 0;
  102. taskInfo->event_type = INT_SWITCH_OPEN;
  103. } else {
  104. /**********************************
  105. * Switch closed
  106. **********************************/
  107. func->switch_save = 0x10;
  108. taskInfo->event_type = INT_SWITCH_CLOSE;
  109. }
  110. }
  111. }
  112. return rc;
  113. }
  114. /**
  115. * cpqhp_find_slot: find the struct slot of given device
  116. * @ctrl: scan lots of this controller
  117. * @device: the device id to find
  118. */
  119. static struct slot *cpqhp_find_slot(struct controller *ctrl, u8 device)
  120. {
  121. struct slot *slot = ctrl->slot;
  122. while (slot && (slot->device != device)) {
  123. slot = slot->next;
  124. }
  125. return slot;
  126. }
  127. static u8 handle_presence_change(u16 change, struct controller * ctrl)
  128. {
  129. int hp_slot;
  130. u8 rc = 0;
  131. u8 temp_byte;
  132. u16 temp_word;
  133. struct pci_func *func;
  134. struct event_info *taskInfo;
  135. struct slot *p_slot;
  136. if (!change)
  137. return 0;
  138. /**********************************
  139. * Presence Change
  140. **********************************/
  141. dbg("cpqsbd: Presence/Notify input change.\n");
  142. dbg(" Changed bits are 0x%4.4x\n", change );
  143. for (hp_slot = 0; hp_slot < 6; hp_slot++) {
  144. if (change & (0x0101 << hp_slot)) {
  145. /**********************************
  146. * this one changed.
  147. **********************************/
  148. func = cpqhp_slot_find(ctrl->bus,
  149. (hp_slot + ctrl->slot_device_offset), 0);
  150. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  151. ctrl->next_event = (ctrl->next_event + 1) % 10;
  152. taskInfo->hp_slot = hp_slot;
  153. rc++;
  154. p_slot = cpqhp_find_slot(ctrl, hp_slot + (readb(ctrl->hpc_reg + SLOT_MASK) >> 4));
  155. if (!p_slot)
  156. return 0;
  157. /* If the switch closed, must be a button
  158. * If not in button mode, nevermind */
  159. if (func->switch_save && (ctrl->push_button == 1)) {
  160. temp_word = ctrl->ctrl_int_comp >> 16;
  161. temp_byte = (temp_word >> hp_slot) & 0x01;
  162. temp_byte |= (temp_word >> (hp_slot + 7)) & 0x02;
  163. if (temp_byte != func->presence_save) {
  164. /**************************************
  165. * button Pressed (doesn't do anything)
  166. **************************************/
  167. dbg("hp_slot %d button pressed\n", hp_slot);
  168. taskInfo->event_type = INT_BUTTON_PRESS;
  169. } else {
  170. /**********************************
  171. * button Released - TAKE ACTION!!!!
  172. **********************************/
  173. dbg("hp_slot %d button released\n", hp_slot);
  174. taskInfo->event_type = INT_BUTTON_RELEASE;
  175. /* Cancel if we are still blinking */
  176. if ((p_slot->state == BLINKINGON_STATE)
  177. || (p_slot->state == BLINKINGOFF_STATE)) {
  178. taskInfo->event_type = INT_BUTTON_CANCEL;
  179. dbg("hp_slot %d button cancel\n", hp_slot);
  180. } else if ((p_slot->state == POWERON_STATE)
  181. || (p_slot->state == POWEROFF_STATE)) {
  182. /* info(msg_button_ignore, p_slot->number); */
  183. taskInfo->event_type = INT_BUTTON_IGNORE;
  184. dbg("hp_slot %d button ignore\n", hp_slot);
  185. }
  186. }
  187. } else {
  188. /* Switch is open, assume a presence change
  189. * Save the presence state */
  190. temp_word = ctrl->ctrl_int_comp >> 16;
  191. func->presence_save = (temp_word >> hp_slot) & 0x01;
  192. func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
  193. if ((!(ctrl->ctrl_int_comp & (0x010000 << hp_slot))) ||
  194. (!(ctrl->ctrl_int_comp & (0x01000000 << hp_slot)))) {
  195. /* Present */
  196. taskInfo->event_type = INT_PRESENCE_ON;
  197. } else {
  198. /* Not Present */
  199. taskInfo->event_type = INT_PRESENCE_OFF;
  200. }
  201. }
  202. }
  203. }
  204. return rc;
  205. }
  206. static u8 handle_power_fault(u8 change, struct controller * ctrl)
  207. {
  208. int hp_slot;
  209. u8 rc = 0;
  210. struct pci_func *func;
  211. struct event_info *taskInfo;
  212. if (!change)
  213. return 0;
  214. /**********************************
  215. * power fault
  216. **********************************/
  217. info("power fault interrupt\n");
  218. for (hp_slot = 0; hp_slot < 6; hp_slot++) {
  219. if (change & (0x01 << hp_slot)) {
  220. /**********************************
  221. * this one changed.
  222. **********************************/
  223. func = cpqhp_slot_find(ctrl->bus,
  224. (hp_slot + ctrl->slot_device_offset), 0);
  225. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  226. ctrl->next_event = (ctrl->next_event + 1) % 10;
  227. taskInfo->hp_slot = hp_slot;
  228. rc++;
  229. if (ctrl->ctrl_int_comp & (0x00000100 << hp_slot)) {
  230. /**********************************
  231. * power fault Cleared
  232. **********************************/
  233. func->status = 0x00;
  234. taskInfo->event_type = INT_POWER_FAULT_CLEAR;
  235. } else {
  236. /**********************************
  237. * power fault
  238. **********************************/
  239. taskInfo->event_type = INT_POWER_FAULT;
  240. if (ctrl->rev < 4) {
  241. amber_LED_on (ctrl, hp_slot);
  242. green_LED_off (ctrl, hp_slot);
  243. set_SOGO (ctrl);
  244. /* this is a fatal condition, we want
  245. * to crash the machine to protect from
  246. * data corruption. simulated_NMI
  247. * shouldn't ever return */
  248. /* FIXME
  249. simulated_NMI(hp_slot, ctrl); */
  250. /* The following code causes a software
  251. * crash just in case simulated_NMI did
  252. * return */
  253. /*FIXME
  254. panic(msg_power_fault); */
  255. } else {
  256. /* set power fault status for this board */
  257. func->status = 0xFF;
  258. info("power fault bit %x set\n", hp_slot);
  259. }
  260. }
  261. }
  262. }
  263. return rc;
  264. }
  265. /**
  266. * sort_by_size: sort nodes on the list by their length, smallest first.
  267. * @head: list to sort
  268. *
  269. */
  270. static int sort_by_size(struct pci_resource **head)
  271. {
  272. struct pci_resource *current_res;
  273. struct pci_resource *next_res;
  274. int out_of_order = 1;
  275. if (!(*head))
  276. return 1;
  277. if (!((*head)->next))
  278. return 0;
  279. while (out_of_order) {
  280. out_of_order = 0;
  281. /* Special case for swapping list head */
  282. if (((*head)->next) &&
  283. ((*head)->length > (*head)->next->length)) {
  284. out_of_order++;
  285. current_res = *head;
  286. *head = (*head)->next;
  287. current_res->next = (*head)->next;
  288. (*head)->next = current_res;
  289. }
  290. current_res = *head;
  291. while (current_res->next && current_res->next->next) {
  292. if (current_res->next->length > current_res->next->next->length) {
  293. out_of_order++;
  294. next_res = current_res->next;
  295. current_res->next = current_res->next->next;
  296. current_res = current_res->next;
  297. next_res->next = current_res->next;
  298. current_res->next = next_res;
  299. } else
  300. current_res = current_res->next;
  301. }
  302. } /* End of out_of_order loop */
  303. return 0;
  304. }
  305. /**
  306. * sort_by_max_size: sort nodes on the list by their length, largest first.
  307. * @head: list to sort
  308. *
  309. */
  310. static int sort_by_max_size(struct pci_resource **head)
  311. {
  312. struct pci_resource *current_res;
  313. struct pci_resource *next_res;
  314. int out_of_order = 1;
  315. if (!(*head))
  316. return 1;
  317. if (!((*head)->next))
  318. return 0;
  319. while (out_of_order) {
  320. out_of_order = 0;
  321. /* Special case for swapping list head */
  322. if (((*head)->next) &&
  323. ((*head)->length < (*head)->next->length)) {
  324. out_of_order++;
  325. current_res = *head;
  326. *head = (*head)->next;
  327. current_res->next = (*head)->next;
  328. (*head)->next = current_res;
  329. }
  330. current_res = *head;
  331. while (current_res->next && current_res->next->next) {
  332. if (current_res->next->length < current_res->next->next->length) {
  333. out_of_order++;
  334. next_res = current_res->next;
  335. current_res->next = current_res->next->next;
  336. current_res = current_res->next;
  337. next_res->next = current_res->next;
  338. current_res->next = next_res;
  339. } else
  340. current_res = current_res->next;
  341. }
  342. } /* End of out_of_order loop */
  343. return 0;
  344. }
  345. /**
  346. * do_pre_bridge_resource_split: find node of resources that are unused
  347. *
  348. */
  349. static struct pci_resource *do_pre_bridge_resource_split(struct pci_resource **head,
  350. struct pci_resource **orig_head, u32 alignment)
  351. {
  352. struct pci_resource *prevnode = NULL;
  353. struct pci_resource *node;
  354. struct pci_resource *split_node;
  355. u32 rc;
  356. u32 temp_dword;
  357. dbg("do_pre_bridge_resource_split\n");
  358. if (!(*head) || !(*orig_head))
  359. return NULL;
  360. rc = cpqhp_resource_sort_and_combine(head);
  361. if (rc)
  362. return NULL;
  363. if ((*head)->base != (*orig_head)->base)
  364. return NULL;
  365. if ((*head)->length == (*orig_head)->length)
  366. return NULL;
  367. /* If we got here, there the bridge requires some of the resource, but
  368. * we may be able to split some off of the front */
  369. node = *head;
  370. if (node->length & (alignment -1)) {
  371. /* this one isn't an aligned length, so we'll make a new entry
  372. * and split it up. */
  373. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  374. if (!split_node)
  375. return NULL;
  376. temp_dword = (node->length | (alignment-1)) + 1 - alignment;
  377. split_node->base = node->base;
  378. split_node->length = temp_dword;
  379. node->length -= temp_dword;
  380. node->base += split_node->length;
  381. /* Put it in the list */
  382. *head = split_node;
  383. split_node->next = node;
  384. }
  385. if (node->length < alignment)
  386. return NULL;
  387. /* Now unlink it */
  388. if (*head == node) {
  389. *head = node->next;
  390. } else {
  391. prevnode = *head;
  392. while (prevnode->next != node)
  393. prevnode = prevnode->next;
  394. prevnode->next = node->next;
  395. }
  396. node->next = NULL;
  397. return node;
  398. }
  399. /**
  400. * do_bridge_resource_split: find one node of resources that aren't in use
  401. *
  402. */
  403. static struct pci_resource *do_bridge_resource_split(struct pci_resource **head, u32 alignment)
  404. {
  405. struct pci_resource *prevnode = NULL;
  406. struct pci_resource *node;
  407. u32 rc;
  408. u32 temp_dword;
  409. rc = cpqhp_resource_sort_and_combine(head);
  410. if (rc)
  411. return NULL;
  412. node = *head;
  413. while (node->next) {
  414. prevnode = node;
  415. node = node->next;
  416. kfree(prevnode);
  417. }
  418. if (node->length < alignment)
  419. goto error;
  420. if (node->base & (alignment - 1)) {
  421. /* Short circuit if adjusted size is too small */
  422. temp_dword = (node->base | (alignment-1)) + 1;
  423. if ((node->length - (temp_dword - node->base)) < alignment)
  424. goto error;
  425. node->length -= (temp_dword - node->base);
  426. node->base = temp_dword;
  427. }
  428. if (node->length & (alignment - 1))
  429. /* There's stuff in use after this node */
  430. goto error;
  431. return node;
  432. error:
  433. kfree(node);
  434. return NULL;
  435. }
  436. /**
  437. * get_io_resource: find first node of given size not in ISA aliasing window.
  438. * @head: list to search
  439. * @size: size of node to find, must be a power of two.
  440. *
  441. * Description: this function sorts the resource list by size and then returns
  442. * returns the first node of "size" length that is not in the ISA aliasing
  443. * window. If it finds a node larger than "size" it will split it up.
  444. *
  445. */
  446. static struct pci_resource *get_io_resource(struct pci_resource **head, u32 size)
  447. {
  448. struct pci_resource *prevnode;
  449. struct pci_resource *node;
  450. struct pci_resource *split_node;
  451. u32 temp_dword;
  452. if (!(*head))
  453. return NULL;
  454. if ( cpqhp_resource_sort_and_combine(head) )
  455. return NULL;
  456. if ( sort_by_size(head) )
  457. return NULL;
  458. for (node = *head; node; node = node->next) {
  459. if (node->length < size)
  460. continue;
  461. if (node->base & (size - 1)) {
  462. /* this one isn't base aligned properly
  463. * so we'll make a new entry and split it up */
  464. temp_dword = (node->base | (size-1)) + 1;
  465. /* Short circuit if adjusted size is too small */
  466. if ((node->length - (temp_dword - node->base)) < size)
  467. continue;
  468. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  469. if (!split_node)
  470. return NULL;
  471. split_node->base = node->base;
  472. split_node->length = temp_dword - node->base;
  473. node->base = temp_dword;
  474. node->length -= split_node->length;
  475. /* Put it in the list */
  476. split_node->next = node->next;
  477. node->next = split_node;
  478. } /* End of non-aligned base */
  479. /* Don't need to check if too small since we already did */
  480. if (node->length > size) {
  481. /* this one is longer than we need
  482. * so we'll make a new entry and split it up */
  483. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  484. if (!split_node)
  485. return NULL;
  486. split_node->base = node->base + size;
  487. split_node->length = node->length - size;
  488. node->length = size;
  489. /* Put it in the list */
  490. split_node->next = node->next;
  491. node->next = split_node;
  492. } /* End of too big on top end */
  493. /* For IO make sure it's not in the ISA aliasing space */
  494. if (node->base & 0x300L)
  495. continue;
  496. /* If we got here, then it is the right size
  497. * Now take it out of the list and break */
  498. if (*head == node) {
  499. *head = node->next;
  500. } else {
  501. prevnode = *head;
  502. while (prevnode->next != node)
  503. prevnode = prevnode->next;
  504. prevnode->next = node->next;
  505. }
  506. node->next = NULL;
  507. break;
  508. }
  509. return node;
  510. }
  511. /**
  512. * get_max_resource: get largest node which has at least the given size.
  513. * @head: the list to search the node in
  514. * @size: the minimum size of the node to find
  515. *
  516. * Description: Gets the largest node that is at least "size" big from the
  517. * list pointed to by head. It aligns the node on top and bottom
  518. * to "size" alignment before returning it.
  519. */
  520. static struct pci_resource *get_max_resource(struct pci_resource **head, u32 size)
  521. {
  522. struct pci_resource *max;
  523. struct pci_resource *temp;
  524. struct pci_resource *split_node;
  525. u32 temp_dword;
  526. if (cpqhp_resource_sort_and_combine(head))
  527. return NULL;
  528. if (sort_by_max_size(head))
  529. return NULL;
  530. for (max = *head; max; max = max->next) {
  531. /* If not big enough we could probably just bail,
  532. * instead we'll continue to the next. */
  533. if (max->length < size)
  534. continue;
  535. if (max->base & (size - 1)) {
  536. /* this one isn't base aligned properly
  537. * so we'll make a new entry and split it up */
  538. temp_dword = (max->base | (size-1)) + 1;
  539. /* Short circuit if adjusted size is too small */
  540. if ((max->length - (temp_dword - max->base)) < size)
  541. continue;
  542. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  543. if (!split_node)
  544. return NULL;
  545. split_node->base = max->base;
  546. split_node->length = temp_dword - max->base;
  547. max->base = temp_dword;
  548. max->length -= split_node->length;
  549. split_node->next = max->next;
  550. max->next = split_node;
  551. }
  552. if ((max->base + max->length) & (size - 1)) {
  553. /* this one isn't end aligned properly at the top
  554. * so we'll make a new entry and split it up */
  555. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  556. if (!split_node)
  557. return NULL;
  558. temp_dword = ((max->base + max->length) & ~(size - 1));
  559. split_node->base = temp_dword;
  560. split_node->length = max->length + max->base
  561. - split_node->base;
  562. max->length -= split_node->length;
  563. split_node->next = max->next;
  564. max->next = split_node;
  565. }
  566. /* Make sure it didn't shrink too much when we aligned it */
  567. if (max->length < size)
  568. continue;
  569. /* Now take it out of the list */
  570. temp = *head;
  571. if (temp == max) {
  572. *head = max->next;
  573. } else {
  574. while (temp && temp->next != max) {
  575. temp = temp->next;
  576. }
  577. temp->next = max->next;
  578. }
  579. max->next = NULL;
  580. break;
  581. }
  582. return max;
  583. }
  584. /**
  585. * get_resource: find resource of given size and split up larger ones.
  586. * @head: the list to search for resources
  587. * @size: the size limit to use
  588. *
  589. * Description: This function sorts the resource list by size and then
  590. * returns the first node of "size" length. If it finds a node
  591. * larger than "size" it will split it up.
  592. *
  593. * size must be a power of two.
  594. */
  595. static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
  596. {
  597. struct pci_resource *prevnode;
  598. struct pci_resource *node;
  599. struct pci_resource *split_node;
  600. u32 temp_dword;
  601. if (cpqhp_resource_sort_and_combine(head))
  602. return NULL;
  603. if (sort_by_size(head))
  604. return NULL;
  605. for (node = *head; node; node = node->next) {
  606. dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
  607. __FUNCTION__, size, node, node->base, node->length);
  608. if (node->length < size)
  609. continue;
  610. if (node->base & (size - 1)) {
  611. dbg("%s: not aligned\n", __FUNCTION__);
  612. /* this one isn't base aligned properly
  613. * so we'll make a new entry and split it up */
  614. temp_dword = (node->base | (size-1)) + 1;
  615. /* Short circuit if adjusted size is too small */
  616. if ((node->length - (temp_dword - node->base)) < size)
  617. continue;
  618. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  619. if (!split_node)
  620. return NULL;
  621. split_node->base = node->base;
  622. split_node->length = temp_dword - node->base;
  623. node->base = temp_dword;
  624. node->length -= split_node->length;
  625. split_node->next = node->next;
  626. node->next = split_node;
  627. } /* End of non-aligned base */
  628. /* Don't need to check if too small since we already did */
  629. if (node->length > size) {
  630. dbg("%s: too big\n", __FUNCTION__);
  631. /* this one is longer than we need
  632. * so we'll make a new entry and split it up */
  633. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  634. if (!split_node)
  635. return NULL;
  636. split_node->base = node->base + size;
  637. split_node->length = node->length - size;
  638. node->length = size;
  639. /* Put it in the list */
  640. split_node->next = node->next;
  641. node->next = split_node;
  642. } /* End of too big on top end */
  643. dbg("%s: got one!!!\n", __FUNCTION__);
  644. /* If we got here, then it is the right size
  645. * Now take it out of the list */
  646. if (*head == node) {
  647. *head = node->next;
  648. } else {
  649. prevnode = *head;
  650. while (prevnode->next != node)
  651. prevnode = prevnode->next;
  652. prevnode->next = node->next;
  653. }
  654. node->next = NULL;
  655. break;
  656. }
  657. return node;
  658. }
  659. /**
  660. * cpqhp_resource_sort_and_combine: sort nodes by base addresses and clean up.
  661. * @head: the list to sort and clean up
  662. *
  663. * Description: Sorts all of the nodes in the list in ascending order by
  664. * their base addresses. Also does garbage collection by
  665. * combining adjacent nodes.
  666. *
  667. * returns 0 if success
  668. */
  669. int cpqhp_resource_sort_and_combine(struct pci_resource **head)
  670. {
  671. struct pci_resource *node1;
  672. struct pci_resource *node2;
  673. int out_of_order = 1;
  674. dbg("%s: head = %p, *head = %p\n", __FUNCTION__, head, *head);
  675. if (!(*head))
  676. return 1;
  677. dbg("*head->next = %p\n",(*head)->next);
  678. if (!(*head)->next)
  679. return 0; /* only one item on the list, already sorted! */
  680. dbg("*head->base = 0x%x\n",(*head)->base);
  681. dbg("*head->next->base = 0x%x\n",(*head)->next->base);
  682. while (out_of_order) {
  683. out_of_order = 0;
  684. /* Special case for swapping list head */
  685. if (((*head)->next) &&
  686. ((*head)->base > (*head)->next->base)) {
  687. node1 = *head;
  688. (*head) = (*head)->next;
  689. node1->next = (*head)->next;
  690. (*head)->next = node1;
  691. out_of_order++;
  692. }
  693. node1 = (*head);
  694. while (node1->next && node1->next->next) {
  695. if (node1->next->base > node1->next->next->base) {
  696. out_of_order++;
  697. node2 = node1->next;
  698. node1->next = node1->next->next;
  699. node1 = node1->next;
  700. node2->next = node1->next;
  701. node1->next = node2;
  702. } else
  703. node1 = node1->next;
  704. }
  705. } /* End of out_of_order loop */
  706. node1 = *head;
  707. while (node1 && node1->next) {
  708. if ((node1->base + node1->length) == node1->next->base) {
  709. /* Combine */
  710. dbg("8..\n");
  711. node1->length += node1->next->length;
  712. node2 = node1->next;
  713. node1->next = node1->next->next;
  714. kfree(node2);
  715. } else
  716. node1 = node1->next;
  717. }
  718. return 0;
  719. }
  720. irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data, struct pt_regs *regs)
  721. {
  722. struct controller *ctrl = data;
  723. u8 schedule_flag = 0;
  724. u8 reset;
  725. u16 misc;
  726. u32 Diff;
  727. u32 temp_dword;
  728. misc = readw(ctrl->hpc_reg + MISC);
  729. /***************************************
  730. * Check to see if it was our interrupt
  731. ***************************************/
  732. if (!(misc & 0x000C)) {
  733. return IRQ_NONE;
  734. }
  735. if (misc & 0x0004) {
  736. /**********************************
  737. * Serial Output interrupt Pending
  738. **********************************/
  739. /* Clear the interrupt */
  740. misc |= 0x0004;
  741. writew(misc, ctrl->hpc_reg + MISC);
  742. /* Read to clear posted writes */
  743. misc = readw(ctrl->hpc_reg + MISC);
  744. dbg ("%s - waking up\n", __FUNCTION__);
  745. wake_up_interruptible(&ctrl->queue);
  746. }
  747. if (misc & 0x0008) {
  748. /* General-interrupt-input interrupt Pending */
  749. Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
  750. ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  751. /* Clear the interrupt */
  752. writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
  753. /* Read it back to clear any posted writes */
  754. temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  755. if (!Diff)
  756. /* Clear all interrupts */
  757. writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
  758. schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
  759. schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
  760. schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
  761. }
  762. reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
  763. if (reset & 0x40) {
  764. /* Bus reset has completed */
  765. reset &= 0xCF;
  766. writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
  767. reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
  768. wake_up_interruptible(&ctrl->queue);
  769. }
  770. if (schedule_flag) {
  771. up(&event_semaphore);
  772. dbg("Signal event_semaphore\n");
  773. }
  774. return IRQ_HANDLED;
  775. }
  776. /**
  777. * cpqhp_slot_create - Creates a node and adds it to the proper bus.
  778. * @busnumber - bus where new node is to be located
  779. *
  780. * Returns pointer to the new node or NULL if unsuccessful
  781. */
  782. struct pci_func *cpqhp_slot_create(u8 busnumber)
  783. {
  784. struct pci_func *new_slot;
  785. struct pci_func *next;
  786. new_slot = kmalloc(sizeof(*new_slot), GFP_KERNEL);
  787. if (new_slot == NULL) {
  788. /* I'm not dead yet!
  789. * You will be. */
  790. return new_slot;
  791. }
  792. memset(new_slot, 0, sizeof(struct pci_func));
  793. new_slot->next = NULL;
  794. new_slot->configured = 1;
  795. if (cpqhp_slot_list[busnumber] == NULL) {
  796. cpqhp_slot_list[busnumber] = new_slot;
  797. } else {
  798. next = cpqhp_slot_list[busnumber];
  799. while (next->next != NULL)
  800. next = next->next;
  801. next->next = new_slot;
  802. }
  803. return new_slot;
  804. }
  805. /**
  806. * slot_remove - Removes a node from the linked list of slots.
  807. * @old_slot: slot to remove
  808. *
  809. * Returns 0 if successful, !0 otherwise.
  810. */
  811. static int slot_remove(struct pci_func * old_slot)
  812. {
  813. struct pci_func *next;
  814. if (old_slot == NULL)
  815. return 1;
  816. next = cpqhp_slot_list[old_slot->bus];
  817. if (next == NULL) {
  818. return 1;
  819. }
  820. if (next == old_slot) {
  821. cpqhp_slot_list[old_slot->bus] = old_slot->next;
  822. cpqhp_destroy_board_resources(old_slot);
  823. kfree(old_slot);
  824. return 0;
  825. }
  826. while ((next->next != old_slot) && (next->next != NULL)) {
  827. next = next->next;
  828. }
  829. if (next->next == old_slot) {
  830. next->next = old_slot->next;
  831. cpqhp_destroy_board_resources(old_slot);
  832. kfree(old_slot);
  833. return 0;
  834. } else
  835. return 2;
  836. }
  837. /**
  838. * bridge_slot_remove - Removes a node from the linked list of slots.
  839. * @bridge: bridge to remove
  840. *
  841. * Returns 0 if successful, !0 otherwise.
  842. */
  843. static int bridge_slot_remove(struct pci_func *bridge)
  844. {
  845. u8 subordinateBus, secondaryBus;
  846. u8 tempBus;
  847. struct pci_func *next;
  848. secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
  849. subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
  850. for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
  851. next = cpqhp_slot_list[tempBus];
  852. while (!slot_remove(next)) {
  853. next = cpqhp_slot_list[tempBus];
  854. }
  855. }
  856. next = cpqhp_slot_list[bridge->bus];
  857. if (next == NULL)
  858. return 1;
  859. if (next == bridge) {
  860. cpqhp_slot_list[bridge->bus] = bridge->next;
  861. goto out;
  862. }
  863. while ((next->next != bridge) && (next->next != NULL))
  864. next = next->next;
  865. if (next->next != bridge)
  866. return 2;
  867. next->next = bridge->next;
  868. out:
  869. kfree(bridge);
  870. return 0;
  871. }
  872. /**
  873. * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
  874. * @bus: bus to find
  875. * @device: device to find
  876. * @index: is 0 for first function found, 1 for the second...
  877. *
  878. * Returns pointer to the node if successful, %NULL otherwise.
  879. */
  880. struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
  881. {
  882. int found = -1;
  883. struct pci_func *func;
  884. func = cpqhp_slot_list[bus];
  885. if ((func == NULL) || ((func->device == device) && (index == 0)))
  886. return func;
  887. if (func->device == device)
  888. found++;
  889. while (func->next != NULL) {
  890. func = func->next;
  891. if (func->device == device)
  892. found++;
  893. if (found == index)
  894. return func;
  895. }
  896. return NULL;
  897. }
  898. /* DJZ: I don't think is_bridge will work as is.
  899. * FIXME */
  900. static int is_bridge(struct pci_func * func)
  901. {
  902. /* Check the header type */
  903. if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
  904. return 1;
  905. else
  906. return 0;
  907. }
  908. /**
  909. * set_controller_speed - set the frequency and/or mode of a specific
  910. * controller segment.
  911. *
  912. * @ctrl: controller to change frequency/mode for.
  913. * @adapter_speed: the speed of the adapter we want to match.
  914. * @hp_slot: the slot number where the adapter is installed.
  915. *
  916. * Returns 0 if we successfully change frequency and/or mode to match the
  917. * adapter speed.
  918. *
  919. */
  920. static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
  921. {
  922. struct slot *slot;
  923. u8 reg;
  924. u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
  925. u16 reg16;
  926. u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
  927. if (ctrl->speed == adapter_speed)
  928. return 0;
  929. /* We don't allow freq/mode changes if we find another adapter running
  930. * in another slot on this controller */
  931. for(slot = ctrl->slot; slot; slot = slot->next) {
  932. if (slot->device == (hp_slot + ctrl->slot_device_offset))
  933. continue;
  934. if (!slot->hotplug_slot && !slot->hotplug_slot->info)
  935. continue;
  936. if (slot->hotplug_slot->info->adapter_status == 0)
  937. continue;
  938. /* If another adapter is running on the same segment but at a
  939. * lower speed/mode, we allow the new adapter to function at
  940. * this rate if supported */
  941. if (ctrl->speed < adapter_speed)
  942. return 0;
  943. return 1;
  944. }
  945. /* If the controller doesn't support freq/mode changes and the
  946. * controller is running at a higher mode, we bail */
  947. if ((ctrl->speed > adapter_speed) && (!ctrl->pcix_speed_capability))
  948. return 1;
  949. /* But we allow the adapter to run at a lower rate if possible */
  950. if ((ctrl->speed < adapter_speed) && (!ctrl->pcix_speed_capability))
  951. return 0;
  952. /* We try to set the max speed supported by both the adapter and
  953. * controller */
  954. if (ctrl->speed_capability < adapter_speed) {
  955. if (ctrl->speed == ctrl->speed_capability)
  956. return 0;
  957. adapter_speed = ctrl->speed_capability;
  958. }
  959. writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
  960. writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
  961. set_SOGO(ctrl);
  962. wait_for_ctrl_irq(ctrl);
  963. if (adapter_speed != PCI_SPEED_133MHz_PCIX)
  964. reg = 0xF5;
  965. else
  966. reg = 0xF4;
  967. pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
  968. reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
  969. reg16 &= ~0x000F;
  970. switch(adapter_speed) {
  971. case(PCI_SPEED_133MHz_PCIX):
  972. reg = 0x75;
  973. reg16 |= 0xB;
  974. break;
  975. case(PCI_SPEED_100MHz_PCIX):
  976. reg = 0x74;
  977. reg16 |= 0xA;
  978. break;
  979. case(PCI_SPEED_66MHz_PCIX):
  980. reg = 0x73;
  981. reg16 |= 0x9;
  982. break;
  983. case(PCI_SPEED_66MHz):
  984. reg = 0x73;
  985. reg16 |= 0x1;
  986. break;
  987. default: /* 33MHz PCI 2.2 */
  988. reg = 0x71;
  989. break;
  990. }
  991. reg16 |= 0xB << 12;
  992. writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
  993. mdelay(5);
  994. /* Reenable interrupts */
  995. writel(0, ctrl->hpc_reg + INT_MASK);
  996. pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
  997. /* Restart state machine */
  998. reg = ~0xF;
  999. pci_read_config_byte(ctrl->pci_dev, 0x43, &reg);
  1000. pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
  1001. /* Only if mode change...*/
  1002. if (((ctrl->speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
  1003. ((ctrl->speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
  1004. set_SOGO(ctrl);
  1005. wait_for_ctrl_irq(ctrl);
  1006. mdelay(1100);
  1007. /* Restore LED/Slot state */
  1008. writel(leds, ctrl->hpc_reg + LED_CONTROL);
  1009. writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
  1010. set_SOGO(ctrl);
  1011. wait_for_ctrl_irq(ctrl);
  1012. ctrl->speed = adapter_speed;
  1013. slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1014. info("Successfully changed frequency/mode for adapter in slot %d\n",
  1015. slot->number);
  1016. return 0;
  1017. }
  1018. /* the following routines constitute the bulk of the
  1019. hotplug controller logic
  1020. */
  1021. /**
  1022. * board_replaced - Called after a board has been replaced in the system.
  1023. *
  1024. * This is only used if we don't have resources for hot add
  1025. * Turns power on for the board
  1026. * Checks to see if board is the same
  1027. * If board is same, reconfigures it
  1028. * If board isn't same, turns it back off.
  1029. *
  1030. */
  1031. static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
  1032. {
  1033. u8 hp_slot;
  1034. u8 temp_byte;
  1035. u8 adapter_speed;
  1036. u32 index;
  1037. u32 rc = 0;
  1038. u32 src = 8;
  1039. hp_slot = func->device - ctrl->slot_device_offset;
  1040. if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
  1041. /**********************************
  1042. * The switch is open.
  1043. **********************************/
  1044. rc = INTERLOCK_OPEN;
  1045. } else if (is_slot_enabled (ctrl, hp_slot)) {
  1046. /**********************************
  1047. * The board is already on
  1048. **********************************/
  1049. rc = CARD_FUNCTIONING;
  1050. } else {
  1051. down(&ctrl->crit_sect);
  1052. /* turn on board without attaching to the bus */
  1053. enable_slot_power (ctrl, hp_slot);
  1054. set_SOGO(ctrl);
  1055. /* Wait for SOBS to be unset */
  1056. wait_for_ctrl_irq (ctrl);
  1057. /* Change bits in slot power register to force another shift out
  1058. * NOTE: this is to work around the timer bug */
  1059. temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
  1060. writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
  1061. writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
  1062. set_SOGO(ctrl);
  1063. /* Wait for SOBS to be unset */
  1064. wait_for_ctrl_irq (ctrl);
  1065. adapter_speed = get_adapter_speed(ctrl, hp_slot);
  1066. if (ctrl->speed != adapter_speed)
  1067. if (set_controller_speed(ctrl, adapter_speed, hp_slot))
  1068. rc = WRONG_BUS_FREQUENCY;
  1069. /* turn off board without attaching to the bus */
  1070. disable_slot_power (ctrl, hp_slot);
  1071. set_SOGO(ctrl);
  1072. /* Wait for SOBS to be unset */
  1073. wait_for_ctrl_irq (ctrl);
  1074. up(&ctrl->crit_sect);
  1075. if (rc)
  1076. return rc;
  1077. down(&ctrl->crit_sect);
  1078. slot_enable (ctrl, hp_slot);
  1079. green_LED_blink (ctrl, hp_slot);
  1080. amber_LED_off (ctrl, hp_slot);
  1081. set_SOGO(ctrl);
  1082. /* Wait for SOBS to be unset */
  1083. wait_for_ctrl_irq (ctrl);
  1084. up(&ctrl->crit_sect);
  1085. /* Wait for ~1 second because of hot plug spec */
  1086. long_delay(1*HZ);
  1087. /* Check for a power fault */
  1088. if (func->status == 0xFF) {
  1089. /* power fault occurred, but it was benign */
  1090. rc = POWER_FAILURE;
  1091. func->status = 0;
  1092. } else
  1093. rc = cpqhp_valid_replace(ctrl, func);
  1094. if (!rc) {
  1095. /* It must be the same board */
  1096. rc = cpqhp_configure_board(ctrl, func);
  1097. if (rc || src) {
  1098. /* If configuration fails, turn it off
  1099. * Get slot won't work for devices behind
  1100. * bridges, but in this case it will always be
  1101. * called for the "base" bus/dev/func of an
  1102. * adapter. */
  1103. down(&ctrl->crit_sect);
  1104. amber_LED_on (ctrl, hp_slot);
  1105. green_LED_off (ctrl, hp_slot);
  1106. slot_disable (ctrl, hp_slot);
  1107. set_SOGO(ctrl);
  1108. /* Wait for SOBS to be unset */
  1109. wait_for_ctrl_irq (ctrl);
  1110. up(&ctrl->crit_sect);
  1111. if (rc)
  1112. return rc;
  1113. else
  1114. return 1;
  1115. }
  1116. func->status = 0;
  1117. func->switch_save = 0x10;
  1118. index = 1;
  1119. while (((func = cpqhp_slot_find(func->bus, func->device, index)) != NULL) && !rc) {
  1120. rc |= cpqhp_configure_board(ctrl, func);
  1121. index++;
  1122. }
  1123. if (rc) {
  1124. /* If configuration fails, turn it off
  1125. * Get slot won't work for devices behind
  1126. * bridges, but in this case it will always be
  1127. * called for the "base" bus/dev/func of an
  1128. * adapter. */
  1129. down(&ctrl->crit_sect);
  1130. amber_LED_on (ctrl, hp_slot);
  1131. green_LED_off (ctrl, hp_slot);
  1132. slot_disable (ctrl, hp_slot);
  1133. set_SOGO(ctrl);
  1134. /* Wait for SOBS to be unset */
  1135. wait_for_ctrl_irq (ctrl);
  1136. up(&ctrl->crit_sect);
  1137. return rc;
  1138. }
  1139. /* Done configuring so turn LED on full time */
  1140. down(&ctrl->crit_sect);
  1141. green_LED_on (ctrl, hp_slot);
  1142. set_SOGO(ctrl);
  1143. /* Wait for SOBS to be unset */
  1144. wait_for_ctrl_irq (ctrl);
  1145. up(&ctrl->crit_sect);
  1146. rc = 0;
  1147. } else {
  1148. /* Something is wrong
  1149. * Get slot won't work for devices behind bridges, but
  1150. * in this case it will always be called for the "base"
  1151. * bus/dev/func of an adapter. */
  1152. down(&ctrl->crit_sect);
  1153. amber_LED_on (ctrl, hp_slot);
  1154. green_LED_off (ctrl, hp_slot);
  1155. slot_disable (ctrl, hp_slot);
  1156. set_SOGO(ctrl);
  1157. /* Wait for SOBS to be unset */
  1158. wait_for_ctrl_irq (ctrl);
  1159. up(&ctrl->crit_sect);
  1160. }
  1161. }
  1162. return rc;
  1163. }
  1164. /**
  1165. * board_added - Called after a board has been added to the system.
  1166. *
  1167. * Turns power on for the board
  1168. * Configures board
  1169. *
  1170. */
  1171. static u32 board_added(struct pci_func *func, struct controller *ctrl)
  1172. {
  1173. u8 hp_slot;
  1174. u8 temp_byte;
  1175. u8 adapter_speed;
  1176. int index;
  1177. u32 temp_register = 0xFFFFFFFF;
  1178. u32 rc = 0;
  1179. struct pci_func *new_slot = NULL;
  1180. struct slot *p_slot;
  1181. struct resource_lists res_lists;
  1182. hp_slot = func->device - ctrl->slot_device_offset;
  1183. dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
  1184. __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
  1185. down(&ctrl->crit_sect);
  1186. /* turn on board without attaching to the bus */
  1187. enable_slot_power(ctrl, hp_slot);
  1188. set_SOGO(ctrl);
  1189. /* Wait for SOBS to be unset */
  1190. wait_for_ctrl_irq (ctrl);
  1191. /* Change bits in slot power register to force another shift out
  1192. * NOTE: this is to work around the timer bug */
  1193. temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
  1194. writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
  1195. writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
  1196. set_SOGO(ctrl);
  1197. /* Wait for SOBS to be unset */
  1198. wait_for_ctrl_irq (ctrl);
  1199. adapter_speed = get_adapter_speed(ctrl, hp_slot);
  1200. if (ctrl->speed != adapter_speed)
  1201. if (set_controller_speed(ctrl, adapter_speed, hp_slot))
  1202. rc = WRONG_BUS_FREQUENCY;
  1203. /* turn off board without attaching to the bus */
  1204. disable_slot_power (ctrl, hp_slot);
  1205. set_SOGO(ctrl);
  1206. /* Wait for SOBS to be unset */
  1207. wait_for_ctrl_irq(ctrl);
  1208. up(&ctrl->crit_sect);
  1209. if (rc)
  1210. return rc;
  1211. p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1212. /* turn on board and blink green LED */
  1213. dbg("%s: before down\n", __FUNCTION__);
  1214. down(&ctrl->crit_sect);
  1215. dbg("%s: after down\n", __FUNCTION__);
  1216. dbg("%s: before slot_enable\n", __FUNCTION__);
  1217. slot_enable (ctrl, hp_slot);
  1218. dbg("%s: before green_LED_blink\n", __FUNCTION__);
  1219. green_LED_blink (ctrl, hp_slot);
  1220. dbg("%s: before amber_LED_blink\n", __FUNCTION__);
  1221. amber_LED_off (ctrl, hp_slot);
  1222. dbg("%s: before set_SOGO\n", __FUNCTION__);
  1223. set_SOGO(ctrl);
  1224. /* Wait for SOBS to be unset */
  1225. dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__);
  1226. wait_for_ctrl_irq (ctrl);
  1227. dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__);
  1228. dbg("%s: before up\n", __FUNCTION__);
  1229. up(&ctrl->crit_sect);
  1230. dbg("%s: after up\n", __FUNCTION__);
  1231. /* Wait for ~1 second because of hot plug spec */
  1232. dbg("%s: before long_delay\n", __FUNCTION__);
  1233. long_delay(1*HZ);
  1234. dbg("%s: after long_delay\n", __FUNCTION__);
  1235. dbg("%s: func status = %x\n", __FUNCTION__, func->status);
  1236. /* Check for a power fault */
  1237. if (func->status == 0xFF) {
  1238. /* power fault occurred, but it was benign */
  1239. temp_register = 0xFFFFFFFF;
  1240. dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
  1241. rc = POWER_FAILURE;
  1242. func->status = 0;
  1243. } else {
  1244. /* Get vendor/device ID u32 */
  1245. ctrl->pci_bus->number = func->bus;
  1246. rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
  1247. dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__, rc);
  1248. dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
  1249. if (rc != 0) {
  1250. /* Something's wrong here */
  1251. temp_register = 0xFFFFFFFF;
  1252. dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
  1253. }
  1254. /* Preset return code. It will be changed later if things go okay. */
  1255. rc = NO_ADAPTER_PRESENT;
  1256. }
  1257. /* All F's is an empty slot or an invalid board */
  1258. if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
  1259. res_lists.io_head = ctrl->io_head;
  1260. res_lists.mem_head = ctrl->mem_head;
  1261. res_lists.p_mem_head = ctrl->p_mem_head;
  1262. res_lists.bus_head = ctrl->bus_head;
  1263. res_lists.irqs = NULL;
  1264. rc = configure_new_device(ctrl, func, 0, &res_lists);
  1265. dbg("%s: back from configure_new_device\n", __FUNCTION__);
  1266. ctrl->io_head = res_lists.io_head;
  1267. ctrl->mem_head = res_lists.mem_head;
  1268. ctrl->p_mem_head = res_lists.p_mem_head;
  1269. ctrl->bus_head = res_lists.bus_head;
  1270. cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1271. cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1272. cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1273. cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1274. if (rc) {
  1275. down(&ctrl->crit_sect);
  1276. amber_LED_on (ctrl, hp_slot);
  1277. green_LED_off (ctrl, hp_slot);
  1278. slot_disable (ctrl, hp_slot);
  1279. set_SOGO(ctrl);
  1280. /* Wait for SOBS to be unset */
  1281. wait_for_ctrl_irq (ctrl);
  1282. up(&ctrl->crit_sect);
  1283. return rc;
  1284. } else {
  1285. cpqhp_save_slot_config(ctrl, func);
  1286. }
  1287. func->status = 0;
  1288. func->switch_save = 0x10;
  1289. func->is_a_board = 0x01;
  1290. /* next, we will instantiate the linux pci_dev structures (with
  1291. * appropriate driver notification, if already present) */
  1292. dbg("%s: configure linux pci_dev structure\n", __FUNCTION__);
  1293. index = 0;
  1294. do {
  1295. new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
  1296. if (new_slot && !new_slot->pci_dev) {
  1297. cpqhp_configure_device(ctrl, new_slot);
  1298. }
  1299. } while (new_slot);
  1300. down(&ctrl->crit_sect);
  1301. green_LED_on (ctrl, hp_slot);
  1302. set_SOGO(ctrl);
  1303. /* Wait for SOBS to be unset */
  1304. wait_for_ctrl_irq (ctrl);
  1305. up(&ctrl->crit_sect);
  1306. } else {
  1307. down(&ctrl->crit_sect);
  1308. amber_LED_on (ctrl, hp_slot);
  1309. green_LED_off (ctrl, hp_slot);
  1310. slot_disable (ctrl, hp_slot);
  1311. set_SOGO(ctrl);
  1312. /* Wait for SOBS to be unset */
  1313. wait_for_ctrl_irq (ctrl);
  1314. up(&ctrl->crit_sect);
  1315. return rc;
  1316. }
  1317. return 0;
  1318. }
  1319. /**
  1320. * remove_board - Turns off slot and LED's
  1321. *
  1322. */
  1323. static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
  1324. {
  1325. int index;
  1326. u8 skip = 0;
  1327. u8 device;
  1328. u8 hp_slot;
  1329. u8 temp_byte;
  1330. u32 rc;
  1331. struct resource_lists res_lists;
  1332. struct pci_func *temp_func;
  1333. if (cpqhp_unconfigure_device(func))
  1334. return 1;
  1335. device = func->device;
  1336. hp_slot = func->device - ctrl->slot_device_offset;
  1337. dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
  1338. /* When we get here, it is safe to change base address registers.
  1339. * We will attempt to save the base address register lengths */
  1340. if (replace_flag || !ctrl->add_support)
  1341. rc = cpqhp_save_base_addr_length(ctrl, func);
  1342. else if (!func->bus_head && !func->mem_head &&
  1343. !func->p_mem_head && !func->io_head) {
  1344. /* Here we check to see if we've saved any of the board's
  1345. * resources already. If so, we'll skip the attempt to
  1346. * determine what's being used. */
  1347. index = 0;
  1348. temp_func = cpqhp_slot_find(func->bus, func->device, index++);
  1349. while (temp_func) {
  1350. if (temp_func->bus_head || temp_func->mem_head
  1351. || temp_func->p_mem_head || temp_func->io_head) {
  1352. skip = 1;
  1353. break;
  1354. }
  1355. temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
  1356. }
  1357. if (!skip)
  1358. rc = cpqhp_save_used_resources(ctrl, func);
  1359. }
  1360. /* Change status to shutdown */
  1361. if (func->is_a_board)
  1362. func->status = 0x01;
  1363. func->configured = 0;
  1364. down(&ctrl->crit_sect);
  1365. green_LED_off (ctrl, hp_slot);
  1366. slot_disable (ctrl, hp_slot);
  1367. set_SOGO(ctrl);
  1368. /* turn off SERR for slot */
  1369. temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
  1370. temp_byte &= ~(0x01 << hp_slot);
  1371. writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
  1372. /* Wait for SOBS to be unset */
  1373. wait_for_ctrl_irq (ctrl);
  1374. up(&ctrl->crit_sect);
  1375. if (!replace_flag && ctrl->add_support) {
  1376. while (func) {
  1377. res_lists.io_head = ctrl->io_head;
  1378. res_lists.mem_head = ctrl->mem_head;
  1379. res_lists.p_mem_head = ctrl->p_mem_head;
  1380. res_lists.bus_head = ctrl->bus_head;
  1381. cpqhp_return_board_resources(func, &res_lists);
  1382. ctrl->io_head = res_lists.io_head;
  1383. ctrl->mem_head = res_lists.mem_head;
  1384. ctrl->p_mem_head = res_lists.p_mem_head;
  1385. ctrl->bus_head = res_lists.bus_head;
  1386. cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1387. cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1388. cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1389. cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1390. if (is_bridge(func)) {
  1391. bridge_slot_remove(func);
  1392. } else
  1393. slot_remove(func);
  1394. func = cpqhp_slot_find(ctrl->bus, device, 0);
  1395. }
  1396. /* Setup slot structure with entry for empty slot */
  1397. func = cpqhp_slot_create(ctrl->bus);
  1398. if (func == NULL)
  1399. return 1;
  1400. func->bus = ctrl->bus;
  1401. func->device = device;
  1402. func->function = 0;
  1403. func->configured = 0;
  1404. func->switch_save = 0x10;
  1405. func->is_a_board = 0;
  1406. func->p_task_event = NULL;
  1407. }
  1408. return 0;
  1409. }
  1410. static void pushbutton_helper_thread(unsigned long data)
  1411. {
  1412. pushbutton_pending = data;
  1413. up(&event_semaphore);
  1414. }
  1415. /* this is the main worker thread */
  1416. static int event_thread(void* data)
  1417. {
  1418. struct controller *ctrl;
  1419. lock_kernel();
  1420. daemonize("phpd_event");
  1421. unlock_kernel();
  1422. while (1) {
  1423. dbg("!!!!event_thread sleeping\n");
  1424. down_interruptible (&event_semaphore);
  1425. dbg("event_thread woken finished = %d\n", event_finished);
  1426. if (event_finished) break;
  1427. /* Do stuff here */
  1428. if (pushbutton_pending)
  1429. cpqhp_pushbutton_thread(pushbutton_pending);
  1430. else
  1431. for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
  1432. interrupt_event_handler(ctrl);
  1433. }
  1434. dbg("event_thread signals exit\n");
  1435. up(&event_exit);
  1436. return 0;
  1437. }
  1438. int cpqhp_event_start_thread(void)
  1439. {
  1440. int pid;
  1441. /* initialize our semaphores */
  1442. init_MUTEX(&delay_sem);
  1443. init_MUTEX_LOCKED(&event_semaphore);
  1444. init_MUTEX_LOCKED(&event_exit);
  1445. event_finished=0;
  1446. pid = kernel_thread(event_thread, NULL, 0);
  1447. if (pid < 0) {
  1448. err ("Can't start up our event thread\n");
  1449. return -1;
  1450. }
  1451. dbg("Our event thread pid = %d\n", pid);
  1452. return 0;
  1453. }
  1454. void cpqhp_event_stop_thread(void)
  1455. {
  1456. event_finished = 1;
  1457. dbg("event_thread finish command given\n");
  1458. up(&event_semaphore);
  1459. dbg("wait for event_thread to exit\n");
  1460. down(&event_exit);
  1461. }
  1462. static int update_slot_info(struct controller *ctrl, struct slot *slot)
  1463. {
  1464. struct hotplug_slot_info *info;
  1465. int result;
  1466. info = kmalloc(sizeof(*info), GFP_KERNEL);
  1467. if (!info)
  1468. return -ENOMEM;
  1469. info->power_status = get_slot_enabled(ctrl, slot);
  1470. info->attention_status = cpq_get_attention_status(ctrl, slot);
  1471. info->latch_status = cpq_get_latch_status(ctrl, slot);
  1472. info->adapter_status = get_presence_status(ctrl, slot);
  1473. result = pci_hp_change_slot_info(slot->hotplug_slot, info);
  1474. kfree (info);
  1475. return result;
  1476. }
  1477. static void interrupt_event_handler(struct controller *ctrl)
  1478. {
  1479. int loop = 0;
  1480. int change = 1;
  1481. struct pci_func *func;
  1482. u8 hp_slot;
  1483. struct slot *p_slot;
  1484. while (change) {
  1485. change = 0;
  1486. for (loop = 0; loop < 10; loop++) {
  1487. /* dbg("loop %d\n", loop); */
  1488. if (ctrl->event_queue[loop].event_type != 0) {
  1489. hp_slot = ctrl->event_queue[loop].hp_slot;
  1490. func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
  1491. if (!func)
  1492. return;
  1493. p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1494. if (!p_slot)
  1495. return;
  1496. dbg("hp_slot %d, func %p, p_slot %p\n",
  1497. hp_slot, func, p_slot);
  1498. if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
  1499. dbg("button pressed\n");
  1500. } else if (ctrl->event_queue[loop].event_type ==
  1501. INT_BUTTON_CANCEL) {
  1502. dbg("button cancel\n");
  1503. del_timer(&p_slot->task_event);
  1504. down(&ctrl->crit_sect);
  1505. if (p_slot->state == BLINKINGOFF_STATE) {
  1506. /* slot is on */
  1507. dbg("turn on green LED\n");
  1508. green_LED_on (ctrl, hp_slot);
  1509. } else if (p_slot->state == BLINKINGON_STATE) {
  1510. /* slot is off */
  1511. dbg("turn off green LED\n");
  1512. green_LED_off (ctrl, hp_slot);
  1513. }
  1514. info(msg_button_cancel, p_slot->number);
  1515. p_slot->state = STATIC_STATE;
  1516. amber_LED_off (ctrl, hp_slot);
  1517. set_SOGO(ctrl);
  1518. /* Wait for SOBS to be unset */
  1519. wait_for_ctrl_irq (ctrl);
  1520. up(&ctrl->crit_sect);
  1521. }
  1522. /*** button Released (No action on press...) */
  1523. else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
  1524. dbg("button release\n");
  1525. if (is_slot_enabled (ctrl, hp_slot)) {
  1526. dbg("slot is on\n");
  1527. p_slot->state = BLINKINGOFF_STATE;
  1528. info(msg_button_off, p_slot->number);
  1529. } else {
  1530. dbg("slot is off\n");
  1531. p_slot->state = BLINKINGON_STATE;
  1532. info(msg_button_on, p_slot->number);
  1533. }
  1534. down(&ctrl->crit_sect);
  1535. dbg("blink green LED and turn off amber\n");
  1536. amber_LED_off (ctrl, hp_slot);
  1537. green_LED_blink (ctrl, hp_slot);
  1538. set_SOGO(ctrl);
  1539. /* Wait for SOBS to be unset */
  1540. wait_for_ctrl_irq (ctrl);
  1541. up(&ctrl->crit_sect);
  1542. init_timer(&p_slot->task_event);
  1543. p_slot->hp_slot = hp_slot;
  1544. p_slot->ctrl = ctrl;
  1545. /* p_slot->physical_slot = physical_slot; */
  1546. p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
  1547. p_slot->task_event.function = pushbutton_helper_thread;
  1548. p_slot->task_event.data = (u32) p_slot;
  1549. dbg("add_timer p_slot = %p\n", p_slot);
  1550. add_timer(&p_slot->task_event);
  1551. }
  1552. /***********POWER FAULT */
  1553. else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
  1554. dbg("power fault\n");
  1555. } else {
  1556. /* refresh notification */
  1557. if (p_slot)
  1558. update_slot_info(ctrl, p_slot);
  1559. }
  1560. ctrl->event_queue[loop].event_type = 0;
  1561. change = 1;
  1562. }
  1563. } /* End of FOR loop */
  1564. }
  1565. return;
  1566. }
  1567. /**
  1568. * cpqhp_pushbutton_thread
  1569. *
  1570. * Scheduled procedure to handle blocking stuff for the pushbuttons
  1571. * Handles all pending events and exits.
  1572. *
  1573. */
  1574. void cpqhp_pushbutton_thread(unsigned long slot)
  1575. {
  1576. u8 hp_slot;
  1577. u8 device;
  1578. struct pci_func *func;
  1579. struct slot *p_slot = (struct slot *) slot;
  1580. struct controller *ctrl = (struct controller *) p_slot->ctrl;
  1581. pushbutton_pending = 0;
  1582. hp_slot = p_slot->hp_slot;
  1583. device = p_slot->device;
  1584. if (is_slot_enabled(ctrl, hp_slot)) {
  1585. p_slot->state = POWEROFF_STATE;
  1586. /* power Down board */
  1587. func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
  1588. dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
  1589. if (!func) {
  1590. dbg("Error! func NULL in %s\n", __FUNCTION__);
  1591. return ;
  1592. }
  1593. if (func != NULL && ctrl != NULL) {
  1594. if (cpqhp_process_SS(ctrl, func) != 0) {
  1595. amber_LED_on (ctrl, hp_slot);
  1596. green_LED_on (ctrl, hp_slot);
  1597. set_SOGO(ctrl);
  1598. /* Wait for SOBS to be unset */
  1599. wait_for_ctrl_irq (ctrl);
  1600. }
  1601. }
  1602. p_slot->state = STATIC_STATE;
  1603. } else {
  1604. p_slot->state = POWERON_STATE;
  1605. /* slot is off */
  1606. func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
  1607. dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
  1608. if (!func) {
  1609. dbg("Error! func NULL in %s\n", __FUNCTION__);
  1610. return ;
  1611. }
  1612. if (func != NULL && ctrl != NULL) {
  1613. if (cpqhp_process_SI(ctrl, func) != 0) {
  1614. amber_LED_on(ctrl, hp_slot);
  1615. green_LED_off(ctrl, hp_slot);
  1616. set_SOGO(ctrl);
  1617. /* Wait for SOBS to be unset */
  1618. wait_for_ctrl_irq (ctrl);
  1619. }
  1620. }
  1621. p_slot->state = STATIC_STATE;
  1622. }
  1623. return;
  1624. }
  1625. int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
  1626. {
  1627. u8 device, hp_slot;
  1628. u16 temp_word;
  1629. u32 tempdword;
  1630. int rc;
  1631. struct slot* p_slot;
  1632. int physical_slot = 0;
  1633. tempdword = 0;
  1634. device = func->device;
  1635. hp_slot = device - ctrl->slot_device_offset;
  1636. p_slot = cpqhp_find_slot(ctrl, device);
  1637. if (p_slot)
  1638. physical_slot = p_slot->number;
  1639. /* Check to see if the interlock is closed */
  1640. tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  1641. if (tempdword & (0x01 << hp_slot)) {
  1642. return 1;
  1643. }
  1644. if (func->is_a_board) {
  1645. rc = board_replaced(func, ctrl);
  1646. } else {
  1647. /* add board */
  1648. slot_remove(func);
  1649. func = cpqhp_slot_create(ctrl->bus);
  1650. if (func == NULL)
  1651. return 1;
  1652. func->bus = ctrl->bus;
  1653. func->device = device;
  1654. func->function = 0;
  1655. func->configured = 0;
  1656. func->is_a_board = 1;
  1657. /* We have to save the presence info for these slots */
  1658. temp_word = ctrl->ctrl_int_comp >> 16;
  1659. func->presence_save = (temp_word >> hp_slot) & 0x01;
  1660. func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
  1661. if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
  1662. func->switch_save = 0;
  1663. } else {
  1664. func->switch_save = 0x10;
  1665. }
  1666. rc = board_added(func, ctrl);
  1667. if (rc) {
  1668. if (is_bridge(func)) {
  1669. bridge_slot_remove(func);
  1670. } else
  1671. slot_remove(func);
  1672. /* Setup slot structure with entry for empty slot */
  1673. func = cpqhp_slot_create(ctrl->bus);
  1674. if (func == NULL)
  1675. return 1;
  1676. func->bus = ctrl->bus;
  1677. func->device = device;
  1678. func->function = 0;
  1679. func->configured = 0;
  1680. func->is_a_board = 0;
  1681. /* We have to save the presence info for these slots */
  1682. temp_word = ctrl->ctrl_int_comp >> 16;
  1683. func->presence_save = (temp_word >> hp_slot) & 0x01;
  1684. func->presence_save |=
  1685. (temp_word >> (hp_slot + 7)) & 0x02;
  1686. if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
  1687. func->switch_save = 0;
  1688. } else {
  1689. func->switch_save = 0x10;
  1690. }
  1691. }
  1692. }
  1693. if (rc) {
  1694. dbg("%s: rc = %d\n", __FUNCTION__, rc);
  1695. }
  1696. if (p_slot)
  1697. update_slot_info(ctrl, p_slot);
  1698. return rc;
  1699. }
  1700. int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
  1701. {
  1702. u8 device, class_code, header_type, BCR;
  1703. u8 index = 0;
  1704. u8 replace_flag;
  1705. u32 rc = 0;
  1706. unsigned int devfn;
  1707. struct slot* p_slot;
  1708. struct pci_bus *pci_bus = ctrl->pci_bus;
  1709. int physical_slot=0;
  1710. device = func->device;
  1711. func = cpqhp_slot_find(ctrl->bus, device, index++);
  1712. p_slot = cpqhp_find_slot(ctrl, device);
  1713. if (p_slot) {
  1714. physical_slot = p_slot->number;
  1715. }
  1716. /* Make sure there are no video controllers here */
  1717. while (func && !rc) {
  1718. pci_bus->number = func->bus;
  1719. devfn = PCI_DEVFN(func->device, func->function);
  1720. /* Check the Class Code */
  1721. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  1722. if (rc)
  1723. return rc;
  1724. if (class_code == PCI_BASE_CLASS_DISPLAY) {
  1725. /* Display/Video adapter (not supported) */
  1726. rc = REMOVE_NOT_SUPPORTED;
  1727. } else {
  1728. /* See if it's a bridge */
  1729. rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
  1730. if (rc)
  1731. return rc;
  1732. /* If it's a bridge, check the VGA Enable bit */
  1733. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  1734. rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
  1735. if (rc)
  1736. return rc;
  1737. /* If the VGA Enable bit is set, remove isn't
  1738. * supported */
  1739. if (BCR & PCI_BRIDGE_CTL_VGA) {
  1740. rc = REMOVE_NOT_SUPPORTED;
  1741. }
  1742. }
  1743. }
  1744. func = cpqhp_slot_find(ctrl->bus, device, index++);
  1745. }
  1746. func = cpqhp_slot_find(ctrl->bus, device, 0);
  1747. if ((func != NULL) && !rc) {
  1748. /* FIXME: Replace flag should be passed into process_SS */
  1749. replace_flag = !(ctrl->add_support);
  1750. rc = remove_board(func, replace_flag, ctrl);
  1751. } else if (!rc) {
  1752. rc = 1;
  1753. }
  1754. if (p_slot)
  1755. update_slot_info(ctrl, p_slot);
  1756. return rc;
  1757. }
  1758. /**
  1759. * switch_leds: switch the leds, go from one site to the other.
  1760. * @ctrl: controller to use
  1761. * @num_of_slots: number of slots to use
  1762. * @direction: 1 to start from the left side, 0 to start right.
  1763. */
  1764. static void switch_leds(struct controller *ctrl, const int num_of_slots,
  1765. u32 *work_LED, const int direction)
  1766. {
  1767. int loop;
  1768. for (loop = 0; loop < num_of_slots; loop++) {
  1769. if (direction)
  1770. *work_LED = *work_LED >> 1;
  1771. else
  1772. *work_LED = *work_LED << 1;
  1773. writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
  1774. set_SOGO(ctrl);
  1775. /* Wait for SOGO interrupt */
  1776. wait_for_ctrl_irq(ctrl);
  1777. /* Get ready for next iteration */
  1778. long_delay((2*HZ)/10);
  1779. }
  1780. }
  1781. /**
  1782. * hardware_test - runs hardware tests
  1783. *
  1784. * For hot plug ctrl folks to play with.
  1785. * test_num is the number written to the "test" file in sysfs
  1786. *
  1787. */
  1788. int cpqhp_hardware_test(struct controller *ctrl, int test_num)
  1789. {
  1790. u32 save_LED;
  1791. u32 work_LED;
  1792. int loop;
  1793. int num_of_slots;
  1794. num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
  1795. switch (test_num) {
  1796. case 1:
  1797. /* Do stuff here! */
  1798. /* Do that funky LED thing */
  1799. /* so we can restore them later */
  1800. save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
  1801. work_LED = 0x01010101;
  1802. switch_leds(ctrl, num_of_slots, &work_LED, 0);
  1803. switch_leds(ctrl, num_of_slots, &work_LED, 1);
  1804. switch_leds(ctrl, num_of_slots, &work_LED, 0);
  1805. switch_leds(ctrl, num_of_slots, &work_LED, 1);
  1806. work_LED = 0x01010000;
  1807. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1808. switch_leds(ctrl, num_of_slots, &work_LED, 0);
  1809. switch_leds(ctrl, num_of_slots, &work_LED, 1);
  1810. work_LED = 0x00000101;
  1811. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1812. switch_leds(ctrl, num_of_slots, &work_LED, 0);
  1813. switch_leds(ctrl, num_of_slots, &work_LED, 1);
  1814. work_LED = 0x01010000;
  1815. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1816. for (loop = 0; loop < num_of_slots; loop++) {
  1817. set_SOGO(ctrl);
  1818. /* Wait for SOGO interrupt */
  1819. wait_for_ctrl_irq (ctrl);
  1820. /* Get ready for next iteration */
  1821. long_delay((3*HZ)/10);
  1822. work_LED = work_LED >> 16;
  1823. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1824. set_SOGO(ctrl);
  1825. /* Wait for SOGO interrupt */
  1826. wait_for_ctrl_irq (ctrl);
  1827. /* Get ready for next iteration */
  1828. long_delay((3*HZ)/10);
  1829. work_LED = work_LED << 16;
  1830. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1831. work_LED = work_LED << 1;
  1832. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1833. }
  1834. /* put it back the way it was */
  1835. writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
  1836. set_SOGO(ctrl);
  1837. /* Wait for SOBS to be unset */
  1838. wait_for_ctrl_irq (ctrl);
  1839. break;
  1840. case 2:
  1841. /* Do other stuff here! */
  1842. break;
  1843. case 3:
  1844. /* and more... */
  1845. break;
  1846. }
  1847. return 0;
  1848. }
  1849. /**
  1850. * configure_new_device - Configures the PCI header information of one board.
  1851. *
  1852. * @ctrl: pointer to controller structure
  1853. * @func: pointer to function structure
  1854. * @behind_bridge: 1 if this is a recursive call, 0 if not
  1855. * @resources: pointer to set of resource lists
  1856. *
  1857. * Returns 0 if success
  1858. *
  1859. */
  1860. static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
  1861. u8 behind_bridge, struct resource_lists * resources)
  1862. {
  1863. u8 temp_byte, function, max_functions, stop_it;
  1864. int rc;
  1865. u32 ID;
  1866. struct pci_func *new_slot;
  1867. int index;
  1868. new_slot = func;
  1869. dbg("%s\n", __FUNCTION__);
  1870. /* Check for Multi-function device */
  1871. ctrl->pci_bus->number = func->bus;
  1872. rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
  1873. if (rc) {
  1874. dbg("%s: rc = %d\n", __FUNCTION__, rc);
  1875. return rc;
  1876. }
  1877. if (temp_byte & 0x80) /* Multi-function device */
  1878. max_functions = 8;
  1879. else
  1880. max_functions = 1;
  1881. function = 0;
  1882. do {
  1883. rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
  1884. if (rc) {
  1885. dbg("configure_new_function failed %d\n",rc);
  1886. index = 0;
  1887. while (new_slot) {
  1888. new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
  1889. if (new_slot)
  1890. cpqhp_return_board_resources(new_slot, resources);
  1891. }
  1892. return rc;
  1893. }
  1894. function++;
  1895. stop_it = 0;
  1896. /* The following loop skips to the next present function
  1897. * and creates a board structure */
  1898. while ((function < max_functions) && (!stop_it)) {
  1899. pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
  1900. if (ID == 0xFFFFFFFF) { /* There's nothing there. */
  1901. function++;
  1902. } else { /* There's something there */
  1903. /* Setup slot structure. */
  1904. new_slot = cpqhp_slot_create(func->bus);
  1905. if (new_slot == NULL)
  1906. return 1;
  1907. new_slot->bus = func->bus;
  1908. new_slot->device = func->device;
  1909. new_slot->function = function;
  1910. new_slot->is_a_board = 1;
  1911. new_slot->status = 0;
  1912. stop_it++;
  1913. }
  1914. }
  1915. } while (function < max_functions);
  1916. dbg("returning from configure_new_device\n");
  1917. return 0;
  1918. }
  1919. /*
  1920. Configuration logic that involves the hotplug data structures and
  1921. their bookkeeping
  1922. */
  1923. /**
  1924. * configure_new_function - Configures the PCI header information of one device
  1925. *
  1926. * @ctrl: pointer to controller structure
  1927. * @func: pointer to function structure
  1928. * @behind_bridge: 1 if this is a recursive call, 0 if not
  1929. * @resources: pointer to set of resource lists
  1930. *
  1931. * Calls itself recursively for bridged devices.
  1932. * Returns 0 if success
  1933. *
  1934. */
  1935. static int configure_new_function(struct controller *ctrl, struct pci_func *func,
  1936. u8 behind_bridge,
  1937. struct resource_lists *resources)
  1938. {
  1939. int cloop;
  1940. u8 IRQ = 0;
  1941. u8 temp_byte;
  1942. u8 device;
  1943. u8 class_code;
  1944. u16 command;
  1945. u16 temp_word;
  1946. u32 temp_dword;
  1947. u32 rc;
  1948. u32 temp_register;
  1949. u32 base;
  1950. u32 ID;
  1951. unsigned int devfn;
  1952. struct pci_resource *mem_node;
  1953. struct pci_resource *p_mem_node;
  1954. struct pci_resource *io_node;
  1955. struct pci_resource *bus_node;
  1956. struct pci_resource *hold_mem_node;
  1957. struct pci_resource *hold_p_mem_node;
  1958. struct pci_resource *hold_IO_node;
  1959. struct pci_resource *hold_bus_node;
  1960. struct irq_mapping irqs;
  1961. struct pci_func *new_slot;
  1962. struct pci_bus *pci_bus;
  1963. struct resource_lists temp_resources;
  1964. pci_bus = ctrl->pci_bus;
  1965. pci_bus->number = func->bus;
  1966. devfn = PCI_DEVFN(func->device, func->function);
  1967. /* Check for Bridge */
  1968. rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
  1969. if (rc)
  1970. return rc;
  1971. if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
  1972. /* set Primary bus */
  1973. dbg("set Primary bus = %d\n", func->bus);
  1974. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
  1975. if (rc)
  1976. return rc;
  1977. /* find range of busses to use */
  1978. dbg("find ranges of buses to use\n");
  1979. bus_node = get_max_resource(&(resources->bus_head), 1);
  1980. /* If we don't have any busses to allocate, we can't continue */
  1981. if (!bus_node)
  1982. return -ENOMEM;
  1983. /* set Secondary bus */
  1984. temp_byte = bus_node->base;
  1985. dbg("set Secondary bus = %d\n", bus_node->base);
  1986. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
  1987. if (rc)
  1988. return rc;
  1989. /* set subordinate bus */
  1990. temp_byte = bus_node->base + bus_node->length - 1;
  1991. dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
  1992. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
  1993. if (rc)
  1994. return rc;
  1995. /* set subordinate Latency Timer and base Latency Timer */
  1996. temp_byte = 0x40;
  1997. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
  1998. if (rc)
  1999. return rc;
  2000. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
  2001. if (rc)
  2002. return rc;
  2003. /* set Cache Line size */
  2004. temp_byte = 0x08;
  2005. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
  2006. if (rc)
  2007. return rc;
  2008. /* Setup the IO, memory, and prefetchable windows */
  2009. io_node = get_max_resource(&(resources->io_head), 0x1000);
  2010. if (!io_node)
  2011. return -ENOMEM;
  2012. mem_node = get_max_resource(&(resources->mem_head), 0x100000);
  2013. if (!mem_node)
  2014. return -ENOMEM;
  2015. p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
  2016. if (!p_mem_node)
  2017. return -ENOMEM;
  2018. dbg("Setup the IO, memory, and prefetchable windows\n");
  2019. dbg("io_node\n");
  2020. dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
  2021. io_node->length, io_node->next);
  2022. dbg("mem_node\n");
  2023. dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
  2024. mem_node->length, mem_node->next);
  2025. dbg("p_mem_node\n");
  2026. dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
  2027. p_mem_node->length, p_mem_node->next);
  2028. /* set up the IRQ info */
  2029. if (!resources->irqs) {
  2030. irqs.barber_pole = 0;
  2031. irqs.interrupt[0] = 0;
  2032. irqs.interrupt[1] = 0;
  2033. irqs.interrupt[2] = 0;
  2034. irqs.interrupt[3] = 0;
  2035. irqs.valid_INT = 0;
  2036. } else {
  2037. irqs.barber_pole = resources->irqs->barber_pole;
  2038. irqs.interrupt[0] = resources->irqs->interrupt[0];
  2039. irqs.interrupt[1] = resources->irqs->interrupt[1];
  2040. irqs.interrupt[2] = resources->irqs->interrupt[2];
  2041. irqs.interrupt[3] = resources->irqs->interrupt[3];
  2042. irqs.valid_INT = resources->irqs->valid_INT;
  2043. }
  2044. /* set up resource lists that are now aligned on top and bottom
  2045. * for anything behind the bridge. */
  2046. temp_resources.bus_head = bus_node;
  2047. temp_resources.io_head = io_node;
  2048. temp_resources.mem_head = mem_node;
  2049. temp_resources.p_mem_head = p_mem_node;
  2050. temp_resources.irqs = &irqs;
  2051. /* Make copies of the nodes we are going to pass down so that
  2052. * if there is a problem,we can just use these to free resources */
  2053. hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
  2054. hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
  2055. hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
  2056. hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
  2057. if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
  2058. kfree(hold_bus_node);
  2059. kfree(hold_IO_node);
  2060. kfree(hold_mem_node);
  2061. kfree(hold_p_mem_node);
  2062. return 1;
  2063. }
  2064. memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
  2065. bus_node->base += 1;
  2066. bus_node->length -= 1;
  2067. bus_node->next = NULL;
  2068. /* If we have IO resources copy them and fill in the bridge's
  2069. * IO range registers */
  2070. if (io_node) {
  2071. memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
  2072. io_node->next = NULL;
  2073. /* set IO base and Limit registers */
  2074. temp_byte = io_node->base >> 8;
  2075. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
  2076. temp_byte = (io_node->base + io_node->length - 1) >> 8;
  2077. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  2078. } else {
  2079. kfree(hold_IO_node);
  2080. hold_IO_node = NULL;
  2081. }
  2082. /* If we have memory resources copy them and fill in the
  2083. * bridge's memory range registers. Otherwise, fill in the
  2084. * range registers with values that disable them. */
  2085. if (mem_node) {
  2086. memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
  2087. mem_node->next = NULL;
  2088. /* set Mem base and Limit registers */
  2089. temp_word = mem_node->base >> 16;
  2090. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2091. temp_word = (mem_node->base + mem_node->length - 1) >> 16;
  2092. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2093. } else {
  2094. temp_word = 0xFFFF;
  2095. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2096. temp_word = 0x0000;
  2097. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2098. kfree(hold_mem_node);
  2099. hold_mem_node = NULL;
  2100. }
  2101. /* If we have prefetchable memory resources copy them and
  2102. * fill in the bridge's memory range registers. Otherwise,
  2103. * fill in the range registers with values that disable them. */
  2104. if (p_mem_node) {
  2105. memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
  2106. p_mem_node->next = NULL;
  2107. /* set Pre Mem base and Limit registers */
  2108. temp_word = p_mem_node->base >> 16;
  2109. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  2110. temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
  2111. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2112. } else {
  2113. temp_word = 0xFFFF;
  2114. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  2115. temp_word = 0x0000;
  2116. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2117. kfree(hold_p_mem_node);
  2118. hold_p_mem_node = NULL;
  2119. }
  2120. /* Adjust this to compensate for extra adjustment in first loop */
  2121. irqs.barber_pole--;
  2122. rc = 0;
  2123. /* Here we actually find the devices and configure them */
  2124. for (device = 0; (device <= 0x1F) && !rc; device++) {
  2125. irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
  2126. ID = 0xFFFFFFFF;
  2127. pci_bus->number = hold_bus_node->base;
  2128. pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
  2129. pci_bus->number = func->bus;
  2130. if (ID != 0xFFFFFFFF) { /* device present */
  2131. /* Setup slot structure. */
  2132. new_slot = cpqhp_slot_create(hold_bus_node->base);
  2133. if (new_slot == NULL) {
  2134. rc = -ENOMEM;
  2135. continue;
  2136. }
  2137. new_slot->bus = hold_bus_node->base;
  2138. new_slot->device = device;
  2139. new_slot->function = 0;
  2140. new_slot->is_a_board = 1;
  2141. new_slot->status = 0;
  2142. rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
  2143. dbg("configure_new_device rc=0x%x\n",rc);
  2144. } /* End of IF (device in slot?) */
  2145. } /* End of FOR loop */
  2146. if (rc)
  2147. goto free_and_out;
  2148. /* save the interrupt routing information */
  2149. if (resources->irqs) {
  2150. resources->irqs->interrupt[0] = irqs.interrupt[0];
  2151. resources->irqs->interrupt[1] = irqs.interrupt[1];
  2152. resources->irqs->interrupt[2] = irqs.interrupt[2];
  2153. resources->irqs->interrupt[3] = irqs.interrupt[3];
  2154. resources->irqs->valid_INT = irqs.valid_INT;
  2155. } else if (!behind_bridge) {
  2156. /* We need to hook up the interrupts here */
  2157. for (cloop = 0; cloop < 4; cloop++) {
  2158. if (irqs.valid_INT & (0x01 << cloop)) {
  2159. rc = cpqhp_set_irq(func->bus, func->device,
  2160. 0x0A + cloop, irqs.interrupt[cloop]);
  2161. if (rc)
  2162. goto free_and_out;
  2163. }
  2164. } /* end of for loop */
  2165. }
  2166. /* Return unused bus resources
  2167. * First use the temporary node to store information for
  2168. * the board */
  2169. if (hold_bus_node && bus_node && temp_resources.bus_head) {
  2170. hold_bus_node->length = bus_node->base - hold_bus_node->base;
  2171. hold_bus_node->next = func->bus_head;
  2172. func->bus_head = hold_bus_node;
  2173. temp_byte = temp_resources.bus_head->base - 1;
  2174. /* set subordinate bus */
  2175. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
  2176. if (temp_resources.bus_head->length == 0) {
  2177. kfree(temp_resources.bus_head);
  2178. temp_resources.bus_head = NULL;
  2179. } else {
  2180. return_resource(&(resources->bus_head), temp_resources.bus_head);
  2181. }
  2182. }
  2183. /* If we have IO space available and there is some left,
  2184. * return the unused portion */
  2185. if (hold_IO_node && temp_resources.io_head) {
  2186. io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
  2187. &hold_IO_node, 0x1000);
  2188. /* Check if we were able to split something off */
  2189. if (io_node) {
  2190. hold_IO_node->base = io_node->base + io_node->length;
  2191. temp_byte = (hold_IO_node->base) >> 8;
  2192. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
  2193. return_resource(&(resources->io_head), io_node);
  2194. }
  2195. io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
  2196. /* Check if we were able to split something off */
  2197. if (io_node) {
  2198. /* First use the temporary node to store
  2199. * information for the board */
  2200. hold_IO_node->length = io_node->base - hold_IO_node->base;
  2201. /* If we used any, add it to the board's list */
  2202. if (hold_IO_node->length) {
  2203. hold_IO_node->next = func->io_head;
  2204. func->io_head = hold_IO_node;
  2205. temp_byte = (io_node->base - 1) >> 8;
  2206. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  2207. return_resource(&(resources->io_head), io_node);
  2208. } else {
  2209. /* it doesn't need any IO */
  2210. temp_word = 0x0000;
  2211. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
  2212. return_resource(&(resources->io_head), io_node);
  2213. kfree(hold_IO_node);
  2214. }
  2215. } else {
  2216. /* it used most of the range */
  2217. hold_IO_node->next = func->io_head;
  2218. func->io_head = hold_IO_node;
  2219. }
  2220. } else if (hold_IO_node) {
  2221. /* it used the whole range */
  2222. hold_IO_node->next = func->io_head;
  2223. func->io_head = hold_IO_node;
  2224. }
  2225. /* If we have memory space available and there is some left,
  2226. * return the unused portion */
  2227. if (hold_mem_node && temp_resources.mem_head) {
  2228. mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
  2229. &hold_mem_node, 0x100000);
  2230. /* Check if we were able to split something off */
  2231. if (mem_node) {
  2232. hold_mem_node->base = mem_node->base + mem_node->length;
  2233. temp_word = (hold_mem_node->base) >> 16;
  2234. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2235. return_resource(&(resources->mem_head), mem_node);
  2236. }
  2237. mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
  2238. /* Check if we were able to split something off */
  2239. if (mem_node) {
  2240. /* First use the temporary node to store
  2241. * information for the board */
  2242. hold_mem_node->length = mem_node->base - hold_mem_node->base;
  2243. if (hold_mem_node->length) {
  2244. hold_mem_node->next = func->mem_head;
  2245. func->mem_head = hold_mem_node;
  2246. /* configure end address */
  2247. temp_word = (mem_node->base - 1) >> 16;
  2248. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2249. /* Return unused resources to the pool */
  2250. return_resource(&(resources->mem_head), mem_node);
  2251. } else {
  2252. /* it doesn't need any Mem */
  2253. temp_word = 0x0000;
  2254. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2255. return_resource(&(resources->mem_head), mem_node);
  2256. kfree(hold_mem_node);
  2257. }
  2258. } else {
  2259. /* it used most of the range */
  2260. hold_mem_node->next = func->mem_head;
  2261. func->mem_head = hold_mem_node;
  2262. }
  2263. } else if (hold_mem_node) {
  2264. /* it used the whole range */
  2265. hold_mem_node->next = func->mem_head;
  2266. func->mem_head = hold_mem_node;
  2267. }
  2268. /* If we have prefetchable memory space available and there
  2269. * is some left at the end, return the unused portion */
  2270. if (hold_p_mem_node && temp_resources.p_mem_head) {
  2271. p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
  2272. &hold_p_mem_node, 0x100000);
  2273. /* Check if we were able to split something off */
  2274. if (p_mem_node) {
  2275. hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
  2276. temp_word = (hold_p_mem_node->base) >> 16;
  2277. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  2278. return_resource(&(resources->p_mem_head), p_mem_node);
  2279. }
  2280. p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
  2281. /* Check if we were able to split something off */
  2282. if (p_mem_node) {
  2283. /* First use the temporary node to store
  2284. * information for the board */
  2285. hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
  2286. /* If we used any, add it to the board's list */
  2287. if (hold_p_mem_node->length) {
  2288. hold_p_mem_node->next = func->p_mem_head;
  2289. func->p_mem_head = hold_p_mem_node;
  2290. temp_word = (p_mem_node->base - 1) >> 16;
  2291. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2292. return_resource(&(resources->p_mem_head), p_mem_node);
  2293. } else {
  2294. /* it doesn't need any PMem */
  2295. temp_word = 0x0000;
  2296. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2297. return_resource(&(resources->p_mem_head), p_mem_node);
  2298. kfree(hold_p_mem_node);
  2299. }
  2300. } else {
  2301. /* it used the most of the range */
  2302. hold_p_mem_node->next = func->p_mem_head;
  2303. func->p_mem_head = hold_p_mem_node;
  2304. }
  2305. } else if (hold_p_mem_node) {
  2306. /* it used the whole range */
  2307. hold_p_mem_node->next = func->p_mem_head;
  2308. func->p_mem_head = hold_p_mem_node;
  2309. }
  2310. /* We should be configuring an IRQ and the bridge's base address
  2311. * registers if it needs them. Although we have never seen such
  2312. * a device */
  2313. /* enable card */
  2314. command = 0x0157; /* = PCI_COMMAND_IO |
  2315. * PCI_COMMAND_MEMORY |
  2316. * PCI_COMMAND_MASTER |
  2317. * PCI_COMMAND_INVALIDATE |
  2318. * PCI_COMMAND_PARITY |
  2319. * PCI_COMMAND_SERR */
  2320. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
  2321. /* set Bridge Control Register */
  2322. command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
  2323. * PCI_BRIDGE_CTL_SERR |
  2324. * PCI_BRIDGE_CTL_NO_ISA */
  2325. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
  2326. } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
  2327. /* Standard device */
  2328. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  2329. if (class_code == PCI_BASE_CLASS_DISPLAY) {
  2330. /* Display (video) adapter (not supported) */
  2331. return DEVICE_TYPE_NOT_SUPPORTED;
  2332. }
  2333. /* Figure out IO and memory needs */
  2334. for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
  2335. temp_register = 0xFFFFFFFF;
  2336. dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
  2337. rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
  2338. rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
  2339. dbg("CND: base = 0x%x\n", temp_register);
  2340. if (temp_register) { /* If this register is implemented */
  2341. if ((temp_register & 0x03L) == 0x01) {
  2342. /* Map IO */
  2343. /* set base = amount of IO space */
  2344. base = temp_register & 0xFFFFFFFC;
  2345. base = ~base + 1;
  2346. dbg("CND: length = 0x%x\n", base);
  2347. io_node = get_io_resource(&(resources->io_head), base);
  2348. dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
  2349. io_node->base, io_node->length, io_node->next);
  2350. dbg("func (%p) io_head (%p)\n", func, func->io_head);
  2351. /* allocate the resource to the board */
  2352. if (io_node) {
  2353. base = io_node->base;
  2354. io_node->next = func->io_head;
  2355. func->io_head = io_node;
  2356. } else
  2357. return -ENOMEM;
  2358. } else if ((temp_register & 0x0BL) == 0x08) {
  2359. /* Map prefetchable memory */
  2360. base = temp_register & 0xFFFFFFF0;
  2361. base = ~base + 1;
  2362. dbg("CND: length = 0x%x\n", base);
  2363. p_mem_node = get_resource(&(resources->p_mem_head), base);
  2364. /* allocate the resource to the board */
  2365. if (p_mem_node) {
  2366. base = p_mem_node->base;
  2367. p_mem_node->next = func->p_mem_head;
  2368. func->p_mem_head = p_mem_node;
  2369. } else
  2370. return -ENOMEM;
  2371. } else if ((temp_register & 0x0BL) == 0x00) {
  2372. /* Map memory */
  2373. base = temp_register & 0xFFFFFFF0;
  2374. base = ~base + 1;
  2375. dbg("CND: length = 0x%x\n", base);
  2376. mem_node = get_resource(&(resources->mem_head), base);
  2377. /* allocate the resource to the board */
  2378. if (mem_node) {
  2379. base = mem_node->base;
  2380. mem_node->next = func->mem_head;
  2381. func->mem_head = mem_node;
  2382. } else
  2383. return -ENOMEM;
  2384. } else if ((temp_register & 0x0BL) == 0x04) {
  2385. /* Map memory */
  2386. base = temp_register & 0xFFFFFFF0;
  2387. base = ~base + 1;
  2388. dbg("CND: length = 0x%x\n", base);
  2389. mem_node = get_resource(&(resources->mem_head), base);
  2390. /* allocate the resource to the board */
  2391. if (mem_node) {
  2392. base = mem_node->base;
  2393. mem_node->next = func->mem_head;
  2394. func->mem_head = mem_node;
  2395. } else
  2396. return -ENOMEM;
  2397. } else if ((temp_register & 0x0BL) == 0x06) {
  2398. /* Those bits are reserved, we can't handle this */
  2399. return 1;
  2400. } else {
  2401. /* Requesting space below 1M */
  2402. return NOT_ENOUGH_RESOURCES;
  2403. }
  2404. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
  2405. /* Check for 64-bit base */
  2406. if ((temp_register & 0x07L) == 0x04) {
  2407. cloop += 4;
  2408. /* Upper 32 bits of address always zero
  2409. * on today's systems */
  2410. /* FIXME this is probably not true on
  2411. * Alpha and ia64??? */
  2412. base = 0;
  2413. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
  2414. }
  2415. }
  2416. } /* End of base register loop */
  2417. if (cpqhp_legacy_mode) {
  2418. /* Figure out which interrupt pin this function uses */
  2419. rc = pci_bus_read_config_byte (pci_bus, devfn,
  2420. PCI_INTERRUPT_PIN, &temp_byte);
  2421. /* If this function needs an interrupt and we are behind
  2422. * a bridge and the pin is tied to something that's
  2423. * alread mapped, set this one the same */
  2424. if (temp_byte && resources->irqs &&
  2425. (resources->irqs->valid_INT &
  2426. (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
  2427. /* We have to share with something already set up */
  2428. IRQ = resources->irqs->interrupt[(temp_byte +
  2429. resources->irqs->barber_pole - 1) & 0x03];
  2430. } else {
  2431. /* Program IRQ based on card type */
  2432. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  2433. if (class_code == PCI_BASE_CLASS_STORAGE) {
  2434. IRQ = cpqhp_disk_irq;
  2435. } else {
  2436. IRQ = cpqhp_nic_irq;
  2437. }
  2438. }
  2439. /* IRQ Line */
  2440. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
  2441. }
  2442. if (!behind_bridge) {
  2443. rc = cpqhp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
  2444. if (rc)
  2445. return 1;
  2446. } else {
  2447. /* TBD - this code may also belong in the other clause
  2448. * of this If statement */
  2449. resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
  2450. resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
  2451. }
  2452. /* Latency Timer */
  2453. temp_byte = 0x40;
  2454. rc = pci_bus_write_config_byte(pci_bus, devfn,
  2455. PCI_LATENCY_TIMER, temp_byte);
  2456. /* Cache Line size */
  2457. temp_byte = 0x08;
  2458. rc = pci_bus_write_config_byte(pci_bus, devfn,
  2459. PCI_CACHE_LINE_SIZE, temp_byte);
  2460. /* disable ROM base Address */
  2461. temp_dword = 0x00L;
  2462. rc = pci_bus_write_config_word(pci_bus, devfn,
  2463. PCI_ROM_ADDRESS, temp_dword);
  2464. /* enable card */
  2465. temp_word = 0x0157; /* = PCI_COMMAND_IO |
  2466. * PCI_COMMAND_MEMORY |
  2467. * PCI_COMMAND_MASTER |
  2468. * PCI_COMMAND_INVALIDATE |
  2469. * PCI_COMMAND_PARITY |
  2470. * PCI_COMMAND_SERR */
  2471. rc = pci_bus_write_config_word (pci_bus, devfn,
  2472. PCI_COMMAND, temp_word);
  2473. } else { /* End of Not-A-Bridge else */
  2474. /* It's some strange type of PCI adapter (Cardbus?) */
  2475. return DEVICE_TYPE_NOT_SUPPORTED;
  2476. }
  2477. func->configured = 1;
  2478. return 0;
  2479. free_and_out:
  2480. cpqhp_destroy_resource_list (&temp_resources);
  2481. return_resource(&(resources-> bus_head), hold_bus_node);
  2482. return_resource(&(resources-> io_head), hold_IO_node);
  2483. return_resource(&(resources-> mem_head), hold_mem_node);
  2484. return_resource(&(resources-> p_mem_head), hold_p_mem_node);
  2485. return rc;
  2486. }