cpqphp_ctrl.c 78 KB

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