cpqphp_ctrl.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976
  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. if (temp)
  578. temp->next = max->next;
  579. }
  580. max->next = NULL;
  581. break;
  582. }
  583. return max;
  584. }
  585. /**
  586. * get_resource - find resource of given size and split up larger ones.
  587. * @head: the list to search for resources
  588. * @size: the size limit to use
  589. *
  590. * Description: This function sorts the resource list by size and then
  591. * returns the first node of "size" length. If it finds a node
  592. * larger than "size" it will split it up.
  593. *
  594. * size must be a power of two.
  595. */
  596. static struct pci_resource *get_resource(struct pci_resource **head, u32 size)
  597. {
  598. struct pci_resource *prevnode;
  599. struct pci_resource *node;
  600. struct pci_resource *split_node;
  601. u32 temp_dword;
  602. if (cpqhp_resource_sort_and_combine(head))
  603. return NULL;
  604. if (sort_by_size(head))
  605. return NULL;
  606. for (node = *head; node; node = node->next) {
  607. dbg("%s: req_size =%x node=%p, base=%x, length=%x\n",
  608. __func__, size, node, node->base, node->length);
  609. if (node->length < size)
  610. continue;
  611. if (node->base & (size - 1)) {
  612. dbg("%s: not aligned\n", __func__);
  613. /* this one isn't base aligned properly
  614. * so we'll make a new entry and split it up
  615. */
  616. temp_dword = (node->base | (size-1)) + 1;
  617. /* Short circuit if adjusted size is too small */
  618. if ((node->length - (temp_dword - node->base)) < size)
  619. continue;
  620. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  621. if (!split_node)
  622. return NULL;
  623. split_node->base = node->base;
  624. split_node->length = temp_dword - node->base;
  625. node->base = temp_dword;
  626. node->length -= split_node->length;
  627. split_node->next = node->next;
  628. node->next = split_node;
  629. } /* End of non-aligned base */
  630. /* Don't need to check if too small since we already did */
  631. if (node->length > size) {
  632. dbg("%s: too big\n", __func__);
  633. /* this one is longer than we need
  634. * so we'll make a new entry and split it up
  635. */
  636. split_node = kmalloc(sizeof(*split_node), GFP_KERNEL);
  637. if (!split_node)
  638. return NULL;
  639. split_node->base = node->base + size;
  640. split_node->length = node->length - size;
  641. node->length = size;
  642. /* Put it in the list */
  643. split_node->next = node->next;
  644. node->next = split_node;
  645. } /* End of too big on top end */
  646. dbg("%s: got one!!!\n", __func__);
  647. /* If we got here, then it is the right size
  648. * Now take it out of the list */
  649. if (*head == node) {
  650. *head = node->next;
  651. } else {
  652. prevnode = *head;
  653. while (prevnode->next != node)
  654. prevnode = prevnode->next;
  655. prevnode->next = node->next;
  656. }
  657. node->next = NULL;
  658. break;
  659. }
  660. return node;
  661. }
  662. /**
  663. * cpqhp_resource_sort_and_combine - sort nodes by base addresses and clean up
  664. * @head: the list to sort and clean up
  665. *
  666. * Description: Sorts all of the nodes in the list in ascending order by
  667. * their base addresses. Also does garbage collection by
  668. * combining adjacent nodes.
  669. *
  670. * Returns %0 if success.
  671. */
  672. int cpqhp_resource_sort_and_combine(struct pci_resource **head)
  673. {
  674. struct pci_resource *node1;
  675. struct pci_resource *node2;
  676. int out_of_order = 1;
  677. dbg("%s: head = %p, *head = %p\n", __func__, head, *head);
  678. if (!(*head))
  679. return 1;
  680. dbg("*head->next = %p\n",(*head)->next);
  681. if (!(*head)->next)
  682. return 0; /* only one item on the list, already sorted! */
  683. dbg("*head->base = 0x%x\n",(*head)->base);
  684. dbg("*head->next->base = 0x%x\n",(*head)->next->base);
  685. while (out_of_order) {
  686. out_of_order = 0;
  687. /* Special case for swapping list head */
  688. if (((*head)->next) &&
  689. ((*head)->base > (*head)->next->base)) {
  690. node1 = *head;
  691. (*head) = (*head)->next;
  692. node1->next = (*head)->next;
  693. (*head)->next = node1;
  694. out_of_order++;
  695. }
  696. node1 = (*head);
  697. while (node1->next && node1->next->next) {
  698. if (node1->next->base > node1->next->next->base) {
  699. out_of_order++;
  700. node2 = node1->next;
  701. node1->next = node1->next->next;
  702. node1 = node1->next;
  703. node2->next = node1->next;
  704. node1->next = node2;
  705. } else
  706. node1 = node1->next;
  707. }
  708. } /* End of out_of_order loop */
  709. node1 = *head;
  710. while (node1 && node1->next) {
  711. if ((node1->base + node1->length) == node1->next->base) {
  712. /* Combine */
  713. dbg("8..\n");
  714. node1->length += node1->next->length;
  715. node2 = node1->next;
  716. node1->next = node1->next->next;
  717. kfree(node2);
  718. } else
  719. node1 = node1->next;
  720. }
  721. return 0;
  722. }
  723. irqreturn_t cpqhp_ctrl_intr(int IRQ, void *data)
  724. {
  725. struct controller *ctrl = data;
  726. u8 schedule_flag = 0;
  727. u8 reset;
  728. u16 misc;
  729. u32 Diff;
  730. u32 temp_dword;
  731. misc = readw(ctrl->hpc_reg + MISC);
  732. /*
  733. * Check to see if it was our interrupt
  734. */
  735. if (!(misc & 0x000C)) {
  736. return IRQ_NONE;
  737. }
  738. if (misc & 0x0004) {
  739. /*
  740. * Serial Output interrupt Pending
  741. */
  742. /* Clear the interrupt */
  743. misc |= 0x0004;
  744. writew(misc, ctrl->hpc_reg + MISC);
  745. /* Read to clear posted writes */
  746. misc = readw(ctrl->hpc_reg + MISC);
  747. dbg ("%s - waking up\n", __func__);
  748. wake_up_interruptible(&ctrl->queue);
  749. }
  750. if (misc & 0x0008) {
  751. /* General-interrupt-input interrupt Pending */
  752. Diff = readl(ctrl->hpc_reg + INT_INPUT_CLEAR) ^ ctrl->ctrl_int_comp;
  753. ctrl->ctrl_int_comp = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  754. /* Clear the interrupt */
  755. writel(Diff, ctrl->hpc_reg + INT_INPUT_CLEAR);
  756. /* Read it back to clear any posted writes */
  757. temp_dword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  758. if (!Diff)
  759. /* Clear all interrupts */
  760. writel(0xFFFFFFFF, ctrl->hpc_reg + INT_INPUT_CLEAR);
  761. schedule_flag += handle_switch_change((u8)(Diff & 0xFFL), ctrl);
  762. schedule_flag += handle_presence_change((u16)((Diff & 0xFFFF0000L) >> 16), ctrl);
  763. schedule_flag += handle_power_fault((u8)((Diff & 0xFF00L) >> 8), ctrl);
  764. }
  765. reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
  766. if (reset & 0x40) {
  767. /* Bus reset has completed */
  768. reset &= 0xCF;
  769. writeb(reset, ctrl->hpc_reg + RESET_FREQ_MODE);
  770. reset = readb(ctrl->hpc_reg + RESET_FREQ_MODE);
  771. wake_up_interruptible(&ctrl->queue);
  772. }
  773. if (schedule_flag) {
  774. wake_up_process(cpqhp_event_thread);
  775. dbg("Waking even thread");
  776. }
  777. return IRQ_HANDLED;
  778. }
  779. /**
  780. * cpqhp_slot_create - Creates a node and adds it to the proper bus.
  781. * @busnumber: bus where new node is to be located
  782. *
  783. * Returns pointer to the new node or %NULL if unsuccessful.
  784. */
  785. struct pci_func *cpqhp_slot_create(u8 busnumber)
  786. {
  787. struct pci_func *new_slot;
  788. struct pci_func *next;
  789. new_slot = kzalloc(sizeof(*new_slot), GFP_KERNEL);
  790. if (new_slot == NULL)
  791. return new_slot;
  792. new_slot->next = NULL;
  793. new_slot->configured = 1;
  794. if (cpqhp_slot_list[busnumber] == NULL) {
  795. cpqhp_slot_list[busnumber] = new_slot;
  796. } else {
  797. next = cpqhp_slot_list[busnumber];
  798. while (next->next != NULL)
  799. next = next->next;
  800. next->next = new_slot;
  801. }
  802. return new_slot;
  803. }
  804. /**
  805. * slot_remove - Removes a node from the linked list of slots.
  806. * @old_slot: slot to remove
  807. *
  808. * Returns %0 if successful, !0 otherwise.
  809. */
  810. static int slot_remove(struct pci_func *old_slot)
  811. {
  812. struct pci_func *next;
  813. if (old_slot == NULL)
  814. return 1;
  815. next = cpqhp_slot_list[old_slot->bus];
  816. if (next == NULL)
  817. return 1;
  818. if (next == old_slot) {
  819. cpqhp_slot_list[old_slot->bus] = old_slot->next;
  820. cpqhp_destroy_board_resources(old_slot);
  821. kfree(old_slot);
  822. return 0;
  823. }
  824. while ((next->next != old_slot) && (next->next != NULL))
  825. next = next->next;
  826. if (next->next == old_slot) {
  827. next->next = old_slot->next;
  828. cpqhp_destroy_board_resources(old_slot);
  829. kfree(old_slot);
  830. return 0;
  831. } else
  832. return 2;
  833. }
  834. /**
  835. * bridge_slot_remove - Removes a node from the linked list of slots.
  836. * @bridge: bridge to remove
  837. *
  838. * Returns %0 if successful, !0 otherwise.
  839. */
  840. static int bridge_slot_remove(struct pci_func *bridge)
  841. {
  842. u8 subordinateBus, secondaryBus;
  843. u8 tempBus;
  844. struct pci_func *next;
  845. secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
  846. subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
  847. for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
  848. next = cpqhp_slot_list[tempBus];
  849. while (!slot_remove(next))
  850. next = cpqhp_slot_list[tempBus];
  851. }
  852. next = cpqhp_slot_list[bridge->bus];
  853. if (next == NULL)
  854. return 1;
  855. if (next == bridge) {
  856. cpqhp_slot_list[bridge->bus] = bridge->next;
  857. goto out;
  858. }
  859. while ((next->next != bridge) && (next->next != NULL))
  860. next = next->next;
  861. if (next->next != bridge)
  862. return 2;
  863. next->next = bridge->next;
  864. out:
  865. kfree(bridge);
  866. return 0;
  867. }
  868. /**
  869. * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
  870. * @bus: bus to find
  871. * @device: device to find
  872. * @index: is %0 for first function found, %1 for the second...
  873. *
  874. * Returns pointer to the node if successful, %NULL otherwise.
  875. */
  876. struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
  877. {
  878. int found = -1;
  879. struct pci_func *func;
  880. func = cpqhp_slot_list[bus];
  881. if ((func == NULL) || ((func->device == device) && (index == 0)))
  882. return func;
  883. if (func->device == device)
  884. found++;
  885. while (func->next != NULL) {
  886. func = func->next;
  887. if (func->device == device)
  888. found++;
  889. if (found == index)
  890. return func;
  891. }
  892. return NULL;
  893. }
  894. /* DJZ: I don't think is_bridge will work as is.
  895. * FIXME */
  896. static int is_bridge(struct pci_func *func)
  897. {
  898. /* Check the header type */
  899. if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
  900. return 1;
  901. else
  902. return 0;
  903. }
  904. /**
  905. * set_controller_speed - set the frequency and/or mode of a specific controller segment.
  906. * @ctrl: controller to change frequency/mode for.
  907. * @adapter_speed: the speed of the adapter we want to match.
  908. * @hp_slot: the slot number where the adapter is installed.
  909. *
  910. * Returns %0 if we successfully change frequency and/or mode to match the
  911. * adapter speed.
  912. */
  913. static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
  914. {
  915. struct slot *slot;
  916. struct pci_bus *bus = ctrl->pci_bus;
  917. u8 reg;
  918. u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
  919. u16 reg16;
  920. u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
  921. if (bus->cur_bus_speed == adapter_speed)
  922. return 0;
  923. /* We don't allow freq/mode changes if we find another adapter running
  924. * in another slot on this controller
  925. */
  926. for(slot = ctrl->slot; slot; slot = slot->next) {
  927. if (slot->device == (hp_slot + ctrl->slot_device_offset))
  928. continue;
  929. if (!slot->hotplug_slot || !slot->hotplug_slot->info)
  930. continue;
  931. if (slot->hotplug_slot->info->adapter_status == 0)
  932. continue;
  933. /* If another adapter is running on the same segment but at a
  934. * lower speed/mode, we allow the new adapter to function at
  935. * this rate if supported
  936. */
  937. if (bus->cur_bus_speed < adapter_speed)
  938. return 0;
  939. return 1;
  940. }
  941. /* If the controller doesn't support freq/mode changes and the
  942. * controller is running at a higher mode, we bail
  943. */
  944. if ((bus->cur_bus_speed > adapter_speed) && (!ctrl->pcix_speed_capability))
  945. return 1;
  946. /* But we allow the adapter to run at a lower rate if possible */
  947. if ((bus->cur_bus_speed < adapter_speed) && (!ctrl->pcix_speed_capability))
  948. return 0;
  949. /* We try to set the max speed supported by both the adapter and
  950. * controller
  951. */
  952. if (bus->max_bus_speed < adapter_speed) {
  953. if (bus->cur_bus_speed == bus->max_bus_speed)
  954. return 0;
  955. adapter_speed = bus->max_bus_speed;
  956. }
  957. writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
  958. writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
  959. set_SOGO(ctrl);
  960. wait_for_ctrl_irq(ctrl);
  961. if (adapter_speed != PCI_SPEED_133MHz_PCIX)
  962. reg = 0xF5;
  963. else
  964. reg = 0xF4;
  965. pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
  966. reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
  967. reg16 &= ~0x000F;
  968. switch(adapter_speed) {
  969. case(PCI_SPEED_133MHz_PCIX):
  970. reg = 0x75;
  971. reg16 |= 0xB;
  972. break;
  973. case(PCI_SPEED_100MHz_PCIX):
  974. reg = 0x74;
  975. reg16 |= 0xA;
  976. break;
  977. case(PCI_SPEED_66MHz_PCIX):
  978. reg = 0x73;
  979. reg16 |= 0x9;
  980. break;
  981. case(PCI_SPEED_66MHz):
  982. reg = 0x73;
  983. reg16 |= 0x1;
  984. break;
  985. default: /* 33MHz PCI 2.2 */
  986. reg = 0x71;
  987. break;
  988. }
  989. reg16 |= 0xB << 12;
  990. writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
  991. mdelay(5);
  992. /* Reenable interrupts */
  993. writel(0, ctrl->hpc_reg + INT_MASK);
  994. pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
  995. /* Restart state machine */
  996. reg = ~0xF;
  997. pci_read_config_byte(ctrl->pci_dev, 0x43, &reg);
  998. pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
  999. /* Only if mode change...*/
  1000. if (((bus->cur_bus_speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
  1001. ((bus->cur_bus_speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
  1002. set_SOGO(ctrl);
  1003. wait_for_ctrl_irq(ctrl);
  1004. mdelay(1100);
  1005. /* Restore LED/Slot state */
  1006. writel(leds, ctrl->hpc_reg + LED_CONTROL);
  1007. writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
  1008. set_SOGO(ctrl);
  1009. wait_for_ctrl_irq(ctrl);
  1010. bus->cur_bus_speed = adapter_speed;
  1011. slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1012. info("Successfully changed frequency/mode for adapter in slot %d\n",
  1013. slot->number);
  1014. return 0;
  1015. }
  1016. /* the following routines constitute the bulk of the
  1017. * hotplug controller logic
  1018. */
  1019. /**
  1020. * board_replaced - Called after a board has been replaced in the system.
  1021. * @func: PCI device/function information
  1022. * @ctrl: hotplug controller
  1023. *
  1024. * This is only used if we don't have resources for hot add.
  1025. * Turns power on for the board.
  1026. * Checks to see if board is the same.
  1027. * If board is same, reconfigures it.
  1028. * If board isn't same, turns it back off.
  1029. */
  1030. static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
  1031. {
  1032. struct pci_bus *bus = ctrl->pci_bus;
  1033. u8 hp_slot;
  1034. u8 temp_byte;
  1035. u8 adapter_speed;
  1036. u32 rc = 0;
  1037. hp_slot = func->device - ctrl->slot_device_offset;
  1038. /*
  1039. * The switch is open.
  1040. */
  1041. if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot))
  1042. rc = INTERLOCK_OPEN;
  1043. /*
  1044. * The board is already on
  1045. */
  1046. else if (is_slot_enabled (ctrl, hp_slot))
  1047. rc = CARD_FUNCTIONING;
  1048. else {
  1049. mutex_lock(&ctrl->crit_sect);
  1050. /* turn on board without attaching to the bus */
  1051. enable_slot_power (ctrl, hp_slot);
  1052. set_SOGO(ctrl);
  1053. /* Wait for SOBS to be unset */
  1054. wait_for_ctrl_irq (ctrl);
  1055. /* Change bits in slot power register to force another shift out
  1056. * NOTE: this is to work around the timer bug */
  1057. temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
  1058. writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
  1059. writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
  1060. set_SOGO(ctrl);
  1061. /* Wait for SOBS to be unset */
  1062. wait_for_ctrl_irq (ctrl);
  1063. adapter_speed = get_adapter_speed(ctrl, hp_slot);
  1064. if (bus->cur_bus_speed != adapter_speed)
  1065. if (set_controller_speed(ctrl, adapter_speed, hp_slot))
  1066. rc = WRONG_BUS_FREQUENCY;
  1067. /* turn off board without attaching to the bus */
  1068. disable_slot_power (ctrl, hp_slot);
  1069. set_SOGO(ctrl);
  1070. /* Wait for SOBS to be unset */
  1071. wait_for_ctrl_irq (ctrl);
  1072. mutex_unlock(&ctrl->crit_sect);
  1073. if (rc)
  1074. return rc;
  1075. mutex_lock(&ctrl->crit_sect);
  1076. slot_enable (ctrl, hp_slot);
  1077. green_LED_blink (ctrl, hp_slot);
  1078. amber_LED_off (ctrl, hp_slot);
  1079. set_SOGO(ctrl);
  1080. /* Wait for SOBS to be unset */
  1081. wait_for_ctrl_irq (ctrl);
  1082. mutex_unlock(&ctrl->crit_sect);
  1083. /* Wait for ~1 second because of hot plug spec */
  1084. long_delay(1*HZ);
  1085. /* Check for a power fault */
  1086. if (func->status == 0xFF) {
  1087. /* power fault occurred, but it was benign */
  1088. rc = POWER_FAILURE;
  1089. func->status = 0;
  1090. } else
  1091. rc = cpqhp_valid_replace(ctrl, func);
  1092. if (!rc) {
  1093. /* It must be the same board */
  1094. rc = cpqhp_configure_board(ctrl, func);
  1095. /* If configuration fails, turn it off
  1096. * Get slot won't work for devices behind
  1097. * bridges, but in this case it will always be
  1098. * called for the "base" bus/dev/func of an
  1099. * adapter.
  1100. */
  1101. mutex_lock(&ctrl->crit_sect);
  1102. amber_LED_on (ctrl, hp_slot);
  1103. green_LED_off (ctrl, hp_slot);
  1104. slot_disable (ctrl, hp_slot);
  1105. set_SOGO(ctrl);
  1106. /* Wait for SOBS to be unset */
  1107. wait_for_ctrl_irq (ctrl);
  1108. mutex_unlock(&ctrl->crit_sect);
  1109. if (rc)
  1110. return rc;
  1111. else
  1112. return 1;
  1113. } else {
  1114. /* Something is wrong
  1115. * Get slot won't work for devices behind bridges, but
  1116. * in this case it will always be called for the "base"
  1117. * bus/dev/func of an adapter.
  1118. */
  1119. mutex_lock(&ctrl->crit_sect);
  1120. amber_LED_on (ctrl, hp_slot);
  1121. green_LED_off (ctrl, hp_slot);
  1122. slot_disable (ctrl, hp_slot);
  1123. set_SOGO(ctrl);
  1124. /* Wait for SOBS to be unset */
  1125. wait_for_ctrl_irq (ctrl);
  1126. mutex_unlock(&ctrl->crit_sect);
  1127. }
  1128. }
  1129. return rc;
  1130. }
  1131. /**
  1132. * board_added - Called after a board has been added to the system.
  1133. * @func: PCI device/function info
  1134. * @ctrl: hotplug controller
  1135. *
  1136. * Turns power on for the board.
  1137. * Configures board.
  1138. */
  1139. static u32 board_added(struct pci_func *func, struct controller *ctrl)
  1140. {
  1141. u8 hp_slot;
  1142. u8 temp_byte;
  1143. u8 adapter_speed;
  1144. int index;
  1145. u32 temp_register = 0xFFFFFFFF;
  1146. u32 rc = 0;
  1147. struct pci_func *new_slot = NULL;
  1148. struct pci_bus *bus = ctrl->pci_bus;
  1149. struct slot *p_slot;
  1150. struct resource_lists res_lists;
  1151. hp_slot = func->device - ctrl->slot_device_offset;
  1152. dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
  1153. __func__, func->device, ctrl->slot_device_offset, hp_slot);
  1154. mutex_lock(&ctrl->crit_sect);
  1155. /* turn on board without attaching to the bus */
  1156. enable_slot_power(ctrl, hp_slot);
  1157. set_SOGO(ctrl);
  1158. /* Wait for SOBS to be unset */
  1159. wait_for_ctrl_irq (ctrl);
  1160. /* Change bits in slot power register to force another shift out
  1161. * NOTE: this is to work around the timer bug
  1162. */
  1163. temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
  1164. writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
  1165. writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
  1166. set_SOGO(ctrl);
  1167. /* Wait for SOBS to be unset */
  1168. wait_for_ctrl_irq (ctrl);
  1169. adapter_speed = get_adapter_speed(ctrl, hp_slot);
  1170. if (bus->cur_bus_speed != adapter_speed)
  1171. if (set_controller_speed(ctrl, adapter_speed, hp_slot))
  1172. rc = WRONG_BUS_FREQUENCY;
  1173. /* turn off board without attaching to the bus */
  1174. disable_slot_power (ctrl, hp_slot);
  1175. set_SOGO(ctrl);
  1176. /* Wait for SOBS to be unset */
  1177. wait_for_ctrl_irq(ctrl);
  1178. mutex_unlock(&ctrl->crit_sect);
  1179. if (rc)
  1180. return rc;
  1181. p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1182. /* turn on board and blink green LED */
  1183. dbg("%s: before down\n", __func__);
  1184. mutex_lock(&ctrl->crit_sect);
  1185. dbg("%s: after down\n", __func__);
  1186. dbg("%s: before slot_enable\n", __func__);
  1187. slot_enable (ctrl, hp_slot);
  1188. dbg("%s: before green_LED_blink\n", __func__);
  1189. green_LED_blink (ctrl, hp_slot);
  1190. dbg("%s: before amber_LED_blink\n", __func__);
  1191. amber_LED_off (ctrl, hp_slot);
  1192. dbg("%s: before set_SOGO\n", __func__);
  1193. set_SOGO(ctrl);
  1194. /* Wait for SOBS to be unset */
  1195. dbg("%s: before wait_for_ctrl_irq\n", __func__);
  1196. wait_for_ctrl_irq (ctrl);
  1197. dbg("%s: after wait_for_ctrl_irq\n", __func__);
  1198. dbg("%s: before up\n", __func__);
  1199. mutex_unlock(&ctrl->crit_sect);
  1200. dbg("%s: after up\n", __func__);
  1201. /* Wait for ~1 second because of hot plug spec */
  1202. dbg("%s: before long_delay\n", __func__);
  1203. long_delay(1*HZ);
  1204. dbg("%s: after long_delay\n", __func__);
  1205. dbg("%s: func status = %x\n", __func__, func->status);
  1206. /* Check for a power fault */
  1207. if (func->status == 0xFF) {
  1208. /* power fault occurred, but it was benign */
  1209. temp_register = 0xFFFFFFFF;
  1210. dbg("%s: temp register set to %x by power fault\n", __func__, temp_register);
  1211. rc = POWER_FAILURE;
  1212. func->status = 0;
  1213. } else {
  1214. /* Get vendor/device ID u32 */
  1215. ctrl->pci_bus->number = func->bus;
  1216. rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
  1217. dbg("%s: pci_read_config_dword returns %d\n", __func__, rc);
  1218. dbg("%s: temp_register is %x\n", __func__, temp_register);
  1219. if (rc != 0) {
  1220. /* Something's wrong here */
  1221. temp_register = 0xFFFFFFFF;
  1222. dbg("%s: temp register set to %x by error\n", __func__, temp_register);
  1223. }
  1224. /* Preset return code. It will be changed later if things go okay. */
  1225. rc = NO_ADAPTER_PRESENT;
  1226. }
  1227. /* All F's is an empty slot or an invalid board */
  1228. if (temp_register != 0xFFFFFFFF) {
  1229. res_lists.io_head = ctrl->io_head;
  1230. res_lists.mem_head = ctrl->mem_head;
  1231. res_lists.p_mem_head = ctrl->p_mem_head;
  1232. res_lists.bus_head = ctrl->bus_head;
  1233. res_lists.irqs = NULL;
  1234. rc = configure_new_device(ctrl, func, 0, &res_lists);
  1235. dbg("%s: back from configure_new_device\n", __func__);
  1236. ctrl->io_head = res_lists.io_head;
  1237. ctrl->mem_head = res_lists.mem_head;
  1238. ctrl->p_mem_head = res_lists.p_mem_head;
  1239. ctrl->bus_head = res_lists.bus_head;
  1240. cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1241. cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1242. cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1243. cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1244. if (rc) {
  1245. mutex_lock(&ctrl->crit_sect);
  1246. amber_LED_on (ctrl, hp_slot);
  1247. green_LED_off (ctrl, hp_slot);
  1248. slot_disable (ctrl, hp_slot);
  1249. set_SOGO(ctrl);
  1250. /* Wait for SOBS to be unset */
  1251. wait_for_ctrl_irq (ctrl);
  1252. mutex_unlock(&ctrl->crit_sect);
  1253. return rc;
  1254. } else {
  1255. cpqhp_save_slot_config(ctrl, func);
  1256. }
  1257. func->status = 0;
  1258. func->switch_save = 0x10;
  1259. func->is_a_board = 0x01;
  1260. /* next, we will instantiate the linux pci_dev structures (with
  1261. * appropriate driver notification, if already present) */
  1262. dbg("%s: configure linux pci_dev structure\n", __func__);
  1263. index = 0;
  1264. do {
  1265. new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
  1266. if (new_slot && !new_slot->pci_dev)
  1267. cpqhp_configure_device(ctrl, new_slot);
  1268. } while (new_slot);
  1269. mutex_lock(&ctrl->crit_sect);
  1270. green_LED_on (ctrl, hp_slot);
  1271. set_SOGO(ctrl);
  1272. /* Wait for SOBS to be unset */
  1273. wait_for_ctrl_irq (ctrl);
  1274. mutex_unlock(&ctrl->crit_sect);
  1275. } else {
  1276. mutex_lock(&ctrl->crit_sect);
  1277. amber_LED_on (ctrl, hp_slot);
  1278. green_LED_off (ctrl, hp_slot);
  1279. slot_disable (ctrl, hp_slot);
  1280. set_SOGO(ctrl);
  1281. /* Wait for SOBS to be unset */
  1282. wait_for_ctrl_irq (ctrl);
  1283. mutex_unlock(&ctrl->crit_sect);
  1284. return rc;
  1285. }
  1286. return 0;
  1287. }
  1288. /**
  1289. * remove_board - Turns off slot and LEDs
  1290. * @func: PCI device/function info
  1291. * @replace_flag: whether replacing or adding a new device
  1292. * @ctrl: target controller
  1293. */
  1294. static u32 remove_board(struct pci_func *func, u32 replace_flag, struct controller *ctrl)
  1295. {
  1296. int index;
  1297. u8 skip = 0;
  1298. u8 device;
  1299. u8 hp_slot;
  1300. u8 temp_byte;
  1301. u32 rc;
  1302. struct resource_lists res_lists;
  1303. struct pci_func *temp_func;
  1304. if (cpqhp_unconfigure_device(func))
  1305. return 1;
  1306. device = func->device;
  1307. hp_slot = func->device - ctrl->slot_device_offset;
  1308. dbg("In %s, hp_slot = %d\n", __func__, hp_slot);
  1309. /* When we get here, it is safe to change base address registers.
  1310. * We will attempt to save the base address register lengths */
  1311. if (replace_flag || !ctrl->add_support)
  1312. rc = cpqhp_save_base_addr_length(ctrl, func);
  1313. else if (!func->bus_head && !func->mem_head &&
  1314. !func->p_mem_head && !func->io_head) {
  1315. /* Here we check to see if we've saved any of the board's
  1316. * resources already. If so, we'll skip the attempt to
  1317. * determine what's being used. */
  1318. index = 0;
  1319. temp_func = cpqhp_slot_find(func->bus, func->device, index++);
  1320. while (temp_func) {
  1321. if (temp_func->bus_head || temp_func->mem_head
  1322. || temp_func->p_mem_head || temp_func->io_head) {
  1323. skip = 1;
  1324. break;
  1325. }
  1326. temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
  1327. }
  1328. if (!skip)
  1329. rc = cpqhp_save_used_resources(ctrl, func);
  1330. }
  1331. /* Change status to shutdown */
  1332. if (func->is_a_board)
  1333. func->status = 0x01;
  1334. func->configured = 0;
  1335. mutex_lock(&ctrl->crit_sect);
  1336. green_LED_off (ctrl, hp_slot);
  1337. slot_disable (ctrl, hp_slot);
  1338. set_SOGO(ctrl);
  1339. /* turn off SERR for slot */
  1340. temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
  1341. temp_byte &= ~(0x01 << hp_slot);
  1342. writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
  1343. /* Wait for SOBS to be unset */
  1344. wait_for_ctrl_irq (ctrl);
  1345. mutex_unlock(&ctrl->crit_sect);
  1346. if (!replace_flag && ctrl->add_support) {
  1347. while (func) {
  1348. res_lists.io_head = ctrl->io_head;
  1349. res_lists.mem_head = ctrl->mem_head;
  1350. res_lists.p_mem_head = ctrl->p_mem_head;
  1351. res_lists.bus_head = ctrl->bus_head;
  1352. cpqhp_return_board_resources(func, &res_lists);
  1353. ctrl->io_head = res_lists.io_head;
  1354. ctrl->mem_head = res_lists.mem_head;
  1355. ctrl->p_mem_head = res_lists.p_mem_head;
  1356. ctrl->bus_head = res_lists.bus_head;
  1357. cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1358. cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1359. cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1360. cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1361. if (is_bridge(func)) {
  1362. bridge_slot_remove(func);
  1363. } else
  1364. slot_remove(func);
  1365. func = cpqhp_slot_find(ctrl->bus, device, 0);
  1366. }
  1367. /* Setup slot structure with entry for empty slot */
  1368. func = cpqhp_slot_create(ctrl->bus);
  1369. if (func == NULL)
  1370. return 1;
  1371. func->bus = ctrl->bus;
  1372. func->device = device;
  1373. func->function = 0;
  1374. func->configured = 0;
  1375. func->switch_save = 0x10;
  1376. func->is_a_board = 0;
  1377. func->p_task_event = NULL;
  1378. }
  1379. return 0;
  1380. }
  1381. static void pushbutton_helper_thread(unsigned long data)
  1382. {
  1383. pushbutton_pending = data;
  1384. wake_up_process(cpqhp_event_thread);
  1385. }
  1386. /* this is the main worker thread */
  1387. static int event_thread(void *data)
  1388. {
  1389. struct controller *ctrl;
  1390. while (1) {
  1391. dbg("!!!!event_thread sleeping\n");
  1392. set_current_state(TASK_INTERRUPTIBLE);
  1393. schedule();
  1394. if (kthread_should_stop())
  1395. break;
  1396. /* Do stuff here */
  1397. if (pushbutton_pending)
  1398. cpqhp_pushbutton_thread(pushbutton_pending);
  1399. else
  1400. for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
  1401. interrupt_event_handler(ctrl);
  1402. }
  1403. dbg("event_thread signals exit\n");
  1404. return 0;
  1405. }
  1406. int cpqhp_event_start_thread(void)
  1407. {
  1408. cpqhp_event_thread = kthread_run(event_thread, NULL, "phpd_event");
  1409. if (IS_ERR(cpqhp_event_thread)) {
  1410. err ("Can't start up our event thread\n");
  1411. return PTR_ERR(cpqhp_event_thread);
  1412. }
  1413. return 0;
  1414. }
  1415. void cpqhp_event_stop_thread(void)
  1416. {
  1417. kthread_stop(cpqhp_event_thread);
  1418. }
  1419. static int update_slot_info(struct controller *ctrl, struct slot *slot)
  1420. {
  1421. struct hotplug_slot_info *info;
  1422. int result;
  1423. info = kmalloc(sizeof(*info), GFP_KERNEL);
  1424. if (!info)
  1425. return -ENOMEM;
  1426. info->power_status = get_slot_enabled(ctrl, slot);
  1427. info->attention_status = cpq_get_attention_status(ctrl, slot);
  1428. info->latch_status = cpq_get_latch_status(ctrl, slot);
  1429. info->adapter_status = get_presence_status(ctrl, slot);
  1430. result = pci_hp_change_slot_info(slot->hotplug_slot, info);
  1431. kfree (info);
  1432. return result;
  1433. }
  1434. static void interrupt_event_handler(struct controller *ctrl)
  1435. {
  1436. int loop = 0;
  1437. int change = 1;
  1438. struct pci_func *func;
  1439. u8 hp_slot;
  1440. struct slot *p_slot;
  1441. while (change) {
  1442. change = 0;
  1443. for (loop = 0; loop < 10; loop++) {
  1444. /* dbg("loop %d\n", loop); */
  1445. if (ctrl->event_queue[loop].event_type != 0) {
  1446. hp_slot = ctrl->event_queue[loop].hp_slot;
  1447. func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
  1448. if (!func)
  1449. return;
  1450. p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1451. if (!p_slot)
  1452. return;
  1453. dbg("hp_slot %d, func %p, p_slot %p\n",
  1454. hp_slot, func, p_slot);
  1455. if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
  1456. dbg("button pressed\n");
  1457. } else if (ctrl->event_queue[loop].event_type ==
  1458. INT_BUTTON_CANCEL) {
  1459. dbg("button cancel\n");
  1460. del_timer(&p_slot->task_event);
  1461. mutex_lock(&ctrl->crit_sect);
  1462. if (p_slot->state == BLINKINGOFF_STATE) {
  1463. /* slot is on */
  1464. dbg("turn on green LED\n");
  1465. green_LED_on (ctrl, hp_slot);
  1466. } else if (p_slot->state == BLINKINGON_STATE) {
  1467. /* slot is off */
  1468. dbg("turn off green LED\n");
  1469. green_LED_off (ctrl, hp_slot);
  1470. }
  1471. info(msg_button_cancel, p_slot->number);
  1472. p_slot->state = STATIC_STATE;
  1473. amber_LED_off (ctrl, hp_slot);
  1474. set_SOGO(ctrl);
  1475. /* Wait for SOBS to be unset */
  1476. wait_for_ctrl_irq (ctrl);
  1477. mutex_unlock(&ctrl->crit_sect);
  1478. }
  1479. /*** button Released (No action on press...) */
  1480. else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
  1481. dbg("button release\n");
  1482. if (is_slot_enabled (ctrl, hp_slot)) {
  1483. dbg("slot is on\n");
  1484. p_slot->state = BLINKINGOFF_STATE;
  1485. info(msg_button_off, p_slot->number);
  1486. } else {
  1487. dbg("slot is off\n");
  1488. p_slot->state = BLINKINGON_STATE;
  1489. info(msg_button_on, p_slot->number);
  1490. }
  1491. mutex_lock(&ctrl->crit_sect);
  1492. dbg("blink green LED and turn off amber\n");
  1493. amber_LED_off (ctrl, hp_slot);
  1494. green_LED_blink (ctrl, hp_slot);
  1495. set_SOGO(ctrl);
  1496. /* Wait for SOBS to be unset */
  1497. wait_for_ctrl_irq (ctrl);
  1498. mutex_unlock(&ctrl->crit_sect);
  1499. init_timer(&p_slot->task_event);
  1500. p_slot->hp_slot = hp_slot;
  1501. p_slot->ctrl = ctrl;
  1502. /* p_slot->physical_slot = physical_slot; */
  1503. p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
  1504. p_slot->task_event.function = pushbutton_helper_thread;
  1505. p_slot->task_event.data = (u32) p_slot;
  1506. dbg("add_timer p_slot = %p\n", p_slot);
  1507. add_timer(&p_slot->task_event);
  1508. }
  1509. /***********POWER FAULT */
  1510. else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
  1511. dbg("power fault\n");
  1512. } else {
  1513. /* refresh notification */
  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 buses 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 buses 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. memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
  2022. io_node->next = NULL;
  2023. /* set IO base and Limit registers */
  2024. temp_byte = io_node->base >> 8;
  2025. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
  2026. temp_byte = (io_node->base + io_node->length - 1) >> 8;
  2027. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  2028. /* Copy the memory resources and fill in the bridge's memory
  2029. * range registers.
  2030. */
  2031. memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
  2032. mem_node->next = NULL;
  2033. /* set Mem base and Limit registers */
  2034. temp_word = mem_node->base >> 16;
  2035. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2036. temp_word = (mem_node->base + mem_node->length - 1) >> 16;
  2037. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2038. memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
  2039. p_mem_node->next = NULL;
  2040. /* set Pre Mem base and Limit registers */
  2041. temp_word = p_mem_node->base >> 16;
  2042. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  2043. temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
  2044. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2045. /* Adjust this to compensate for extra adjustment in first loop
  2046. */
  2047. irqs.barber_pole--;
  2048. rc = 0;
  2049. /* Here we actually find the devices and configure them */
  2050. for (device = 0; (device <= 0x1F) && !rc; device++) {
  2051. irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
  2052. ID = 0xFFFFFFFF;
  2053. pci_bus->number = hold_bus_node->base;
  2054. pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
  2055. pci_bus->number = func->bus;
  2056. if (ID != 0xFFFFFFFF) { /* device present */
  2057. /* Setup slot structure. */
  2058. new_slot = cpqhp_slot_create(hold_bus_node->base);
  2059. if (new_slot == NULL) {
  2060. rc = -ENOMEM;
  2061. continue;
  2062. }
  2063. new_slot->bus = hold_bus_node->base;
  2064. new_slot->device = device;
  2065. new_slot->function = 0;
  2066. new_slot->is_a_board = 1;
  2067. new_slot->status = 0;
  2068. rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
  2069. dbg("configure_new_device rc=0x%x\n",rc);
  2070. } /* End of IF (device in slot?) */
  2071. } /* End of FOR loop */
  2072. if (rc)
  2073. goto free_and_out;
  2074. /* save the interrupt routing information */
  2075. if (resources->irqs) {
  2076. resources->irqs->interrupt[0] = irqs.interrupt[0];
  2077. resources->irqs->interrupt[1] = irqs.interrupt[1];
  2078. resources->irqs->interrupt[2] = irqs.interrupt[2];
  2079. resources->irqs->interrupt[3] = irqs.interrupt[3];
  2080. resources->irqs->valid_INT = irqs.valid_INT;
  2081. } else if (!behind_bridge) {
  2082. /* We need to hook up the interrupts here */
  2083. for (cloop = 0; cloop < 4; cloop++) {
  2084. if (irqs.valid_INT & (0x01 << cloop)) {
  2085. rc = cpqhp_set_irq(func->bus, func->device,
  2086. cloop + 1, irqs.interrupt[cloop]);
  2087. if (rc)
  2088. goto free_and_out;
  2089. }
  2090. } /* end of for loop */
  2091. }
  2092. /* Return unused bus resources
  2093. * First use the temporary node to store information for
  2094. * the board */
  2095. if (bus_node && temp_resources.bus_head) {
  2096. hold_bus_node->length = bus_node->base - hold_bus_node->base;
  2097. hold_bus_node->next = func->bus_head;
  2098. func->bus_head = hold_bus_node;
  2099. temp_byte = temp_resources.bus_head->base - 1;
  2100. /* set subordinate bus */
  2101. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
  2102. if (temp_resources.bus_head->length == 0) {
  2103. kfree(temp_resources.bus_head);
  2104. temp_resources.bus_head = NULL;
  2105. } else {
  2106. return_resource(&(resources->bus_head), temp_resources.bus_head);
  2107. }
  2108. }
  2109. /* If we have IO space available and there is some left,
  2110. * return the unused portion */
  2111. if (hold_IO_node && temp_resources.io_head) {
  2112. io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
  2113. &hold_IO_node, 0x1000);
  2114. /* Check if we were able to split something off */
  2115. if (io_node) {
  2116. hold_IO_node->base = io_node->base + io_node->length;
  2117. temp_byte = (hold_IO_node->base) >> 8;
  2118. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
  2119. return_resource(&(resources->io_head), io_node);
  2120. }
  2121. io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
  2122. /* Check if we were able to split something off */
  2123. if (io_node) {
  2124. /* First use the temporary node to store
  2125. * information for the board */
  2126. hold_IO_node->length = io_node->base - hold_IO_node->base;
  2127. /* If we used any, add it to the board's list */
  2128. if (hold_IO_node->length) {
  2129. hold_IO_node->next = func->io_head;
  2130. func->io_head = hold_IO_node;
  2131. temp_byte = (io_node->base - 1) >> 8;
  2132. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  2133. return_resource(&(resources->io_head), io_node);
  2134. } else {
  2135. /* it doesn't need any IO */
  2136. temp_word = 0x0000;
  2137. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
  2138. return_resource(&(resources->io_head), io_node);
  2139. kfree(hold_IO_node);
  2140. }
  2141. } else {
  2142. /* it used most of the range */
  2143. hold_IO_node->next = func->io_head;
  2144. func->io_head = hold_IO_node;
  2145. }
  2146. } else if (hold_IO_node) {
  2147. /* it used the whole range */
  2148. hold_IO_node->next = func->io_head;
  2149. func->io_head = hold_IO_node;
  2150. }
  2151. /* If we have memory space available and there is some left,
  2152. * return the unused portion */
  2153. if (hold_mem_node && temp_resources.mem_head) {
  2154. mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
  2155. &hold_mem_node, 0x100000);
  2156. /* Check if we were able to split something off */
  2157. if (mem_node) {
  2158. hold_mem_node->base = mem_node->base + mem_node->length;
  2159. temp_word = (hold_mem_node->base) >> 16;
  2160. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2161. return_resource(&(resources->mem_head), mem_node);
  2162. }
  2163. mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
  2164. /* Check if we were able to split something off */
  2165. if (mem_node) {
  2166. /* First use the temporary node to store
  2167. * information for the board */
  2168. hold_mem_node->length = mem_node->base - hold_mem_node->base;
  2169. if (hold_mem_node->length) {
  2170. hold_mem_node->next = func->mem_head;
  2171. func->mem_head = hold_mem_node;
  2172. /* configure end address */
  2173. temp_word = (mem_node->base - 1) >> 16;
  2174. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2175. /* Return unused resources to the pool */
  2176. return_resource(&(resources->mem_head), mem_node);
  2177. } else {
  2178. /* it doesn't need any Mem */
  2179. temp_word = 0x0000;
  2180. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2181. return_resource(&(resources->mem_head), mem_node);
  2182. kfree(hold_mem_node);
  2183. }
  2184. } else {
  2185. /* it used most of the range */
  2186. hold_mem_node->next = func->mem_head;
  2187. func->mem_head = hold_mem_node;
  2188. }
  2189. } else if (hold_mem_node) {
  2190. /* it used the whole range */
  2191. hold_mem_node->next = func->mem_head;
  2192. func->mem_head = hold_mem_node;
  2193. }
  2194. /* If we have prefetchable memory space available and there
  2195. * is some left at the end, return the unused portion */
  2196. if (temp_resources.p_mem_head) {
  2197. p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
  2198. &hold_p_mem_node, 0x100000);
  2199. /* Check if we were able to split something off */
  2200. if (p_mem_node) {
  2201. hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
  2202. temp_word = (hold_p_mem_node->base) >> 16;
  2203. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  2204. return_resource(&(resources->p_mem_head), p_mem_node);
  2205. }
  2206. p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
  2207. /* Check if we were able to split something off */
  2208. if (p_mem_node) {
  2209. /* First use the temporary node to store
  2210. * information for the board */
  2211. hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
  2212. /* If we used any, add it to the board's list */
  2213. if (hold_p_mem_node->length) {
  2214. hold_p_mem_node->next = func->p_mem_head;
  2215. func->p_mem_head = hold_p_mem_node;
  2216. temp_word = (p_mem_node->base - 1) >> 16;
  2217. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2218. return_resource(&(resources->p_mem_head), p_mem_node);
  2219. } else {
  2220. /* it doesn't need any PMem */
  2221. temp_word = 0x0000;
  2222. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2223. return_resource(&(resources->p_mem_head), p_mem_node);
  2224. kfree(hold_p_mem_node);
  2225. }
  2226. } else {
  2227. /* it used the most of the range */
  2228. hold_p_mem_node->next = func->p_mem_head;
  2229. func->p_mem_head = hold_p_mem_node;
  2230. }
  2231. } else if (hold_p_mem_node) {
  2232. /* it used the whole range */
  2233. hold_p_mem_node->next = func->p_mem_head;
  2234. func->p_mem_head = hold_p_mem_node;
  2235. }
  2236. /* We should be configuring an IRQ and the bridge's base address
  2237. * registers if it needs them. Although we have never seen such
  2238. * a device */
  2239. /* enable card */
  2240. command = 0x0157; /* = PCI_COMMAND_IO |
  2241. * PCI_COMMAND_MEMORY |
  2242. * PCI_COMMAND_MASTER |
  2243. * PCI_COMMAND_INVALIDATE |
  2244. * PCI_COMMAND_PARITY |
  2245. * PCI_COMMAND_SERR */
  2246. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
  2247. /* set Bridge Control Register */
  2248. command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
  2249. * PCI_BRIDGE_CTL_SERR |
  2250. * PCI_BRIDGE_CTL_NO_ISA */
  2251. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
  2252. } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
  2253. /* Standard device */
  2254. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  2255. if (class_code == PCI_BASE_CLASS_DISPLAY) {
  2256. /* Display (video) adapter (not supported) */
  2257. return DEVICE_TYPE_NOT_SUPPORTED;
  2258. }
  2259. /* Figure out IO and memory needs */
  2260. for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
  2261. temp_register = 0xFFFFFFFF;
  2262. dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
  2263. rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
  2264. rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
  2265. dbg("CND: base = 0x%x\n", temp_register);
  2266. if (temp_register) { /* If this register is implemented */
  2267. if ((temp_register & 0x03L) == 0x01) {
  2268. /* Map IO */
  2269. /* set base = amount of IO space */
  2270. base = temp_register & 0xFFFFFFFC;
  2271. base = ~base + 1;
  2272. dbg("CND: length = 0x%x\n", base);
  2273. io_node = get_io_resource(&(resources->io_head), base);
  2274. dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
  2275. io_node->base, io_node->length, io_node->next);
  2276. dbg("func (%p) io_head (%p)\n", func, func->io_head);
  2277. /* allocate the resource to the board */
  2278. if (io_node) {
  2279. base = io_node->base;
  2280. io_node->next = func->io_head;
  2281. func->io_head = io_node;
  2282. } else
  2283. return -ENOMEM;
  2284. } else if ((temp_register & 0x0BL) == 0x08) {
  2285. /* Map prefetchable memory */
  2286. base = temp_register & 0xFFFFFFF0;
  2287. base = ~base + 1;
  2288. dbg("CND: length = 0x%x\n", base);
  2289. p_mem_node = get_resource(&(resources->p_mem_head), base);
  2290. /* allocate the resource to the board */
  2291. if (p_mem_node) {
  2292. base = p_mem_node->base;
  2293. p_mem_node->next = func->p_mem_head;
  2294. func->p_mem_head = p_mem_node;
  2295. } else
  2296. return -ENOMEM;
  2297. } else if ((temp_register & 0x0BL) == 0x00) {
  2298. /* Map memory */
  2299. base = temp_register & 0xFFFFFFF0;
  2300. base = ~base + 1;
  2301. dbg("CND: length = 0x%x\n", base);
  2302. mem_node = get_resource(&(resources->mem_head), base);
  2303. /* allocate the resource to the board */
  2304. if (mem_node) {
  2305. base = mem_node->base;
  2306. mem_node->next = func->mem_head;
  2307. func->mem_head = mem_node;
  2308. } else
  2309. return -ENOMEM;
  2310. } else {
  2311. /* Reserved bits or requesting space below 1M */
  2312. return NOT_ENOUGH_RESOURCES;
  2313. }
  2314. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
  2315. /* Check for 64-bit base */
  2316. if ((temp_register & 0x07L) == 0x04) {
  2317. cloop += 4;
  2318. /* Upper 32 bits of address always zero
  2319. * on today's systems */
  2320. /* FIXME this is probably not true on
  2321. * Alpha and ia64??? */
  2322. base = 0;
  2323. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
  2324. }
  2325. }
  2326. } /* End of base register loop */
  2327. if (cpqhp_legacy_mode) {
  2328. /* Figure out which interrupt pin this function uses */
  2329. rc = pci_bus_read_config_byte (pci_bus, devfn,
  2330. PCI_INTERRUPT_PIN, &temp_byte);
  2331. /* If this function needs an interrupt and we are behind
  2332. * a bridge and the pin is tied to something that's
  2333. * already mapped, set this one the same */
  2334. if (temp_byte && resources->irqs &&
  2335. (resources->irqs->valid_INT &
  2336. (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
  2337. /* We have to share with something already set up */
  2338. IRQ = resources->irqs->interrupt[(temp_byte +
  2339. resources->irqs->barber_pole - 1) & 0x03];
  2340. } else {
  2341. /* Program IRQ based on card type */
  2342. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  2343. if (class_code == PCI_BASE_CLASS_STORAGE)
  2344. IRQ = cpqhp_disk_irq;
  2345. else
  2346. IRQ = cpqhp_nic_irq;
  2347. }
  2348. /* IRQ Line */
  2349. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
  2350. }
  2351. if (!behind_bridge) {
  2352. rc = cpqhp_set_irq(func->bus, func->device, temp_byte, IRQ);
  2353. if (rc)
  2354. return 1;
  2355. } else {
  2356. /* TBD - this code may also belong in the other clause
  2357. * of this If statement */
  2358. resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
  2359. resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
  2360. }
  2361. /* Latency Timer */
  2362. temp_byte = 0x40;
  2363. rc = pci_bus_write_config_byte(pci_bus, devfn,
  2364. PCI_LATENCY_TIMER, temp_byte);
  2365. /* Cache Line size */
  2366. temp_byte = 0x08;
  2367. rc = pci_bus_write_config_byte(pci_bus, devfn,
  2368. PCI_CACHE_LINE_SIZE, temp_byte);
  2369. /* disable ROM base Address */
  2370. temp_dword = 0x00L;
  2371. rc = pci_bus_write_config_word(pci_bus, devfn,
  2372. PCI_ROM_ADDRESS, temp_dword);
  2373. /* enable card */
  2374. temp_word = 0x0157; /* = PCI_COMMAND_IO |
  2375. * PCI_COMMAND_MEMORY |
  2376. * PCI_COMMAND_MASTER |
  2377. * PCI_COMMAND_INVALIDATE |
  2378. * PCI_COMMAND_PARITY |
  2379. * PCI_COMMAND_SERR */
  2380. rc = pci_bus_write_config_word (pci_bus, devfn,
  2381. PCI_COMMAND, temp_word);
  2382. } else { /* End of Not-A-Bridge else */
  2383. /* It's some strange type of PCI adapter (Cardbus?) */
  2384. return DEVICE_TYPE_NOT_SUPPORTED;
  2385. }
  2386. func->configured = 1;
  2387. return 0;
  2388. free_and_out:
  2389. cpqhp_destroy_resource_list (&temp_resources);
  2390. return_resource(&(resources-> bus_head), hold_bus_node);
  2391. return_resource(&(resources-> io_head), hold_IO_node);
  2392. return_resource(&(resources-> mem_head), hold_mem_node);
  2393. return_resource(&(resources-> p_mem_head), hold_p_mem_node);
  2394. return rc;
  2395. }