cpqphp_ctrl.c 77 KB

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