cpqphp_ctrl.c 78 KB

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