cpqphp_ctrl.c 76 KB

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