cpqphp_ctrl.c 74 KB

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