cpqphp_ctrl.c 76 KB

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