cpqphp_ctrl.c 76 KB

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