cpqphp_ctrl.c 76 KB

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