cpqphp_ctrl.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032
  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 = kmalloc(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. memset(new_slot, 0, sizeof(struct pci_func));
  793. new_slot->next = NULL;
  794. new_slot->configured = 1;
  795. if (cpqhp_slot_list[busnumber] == NULL) {
  796. cpqhp_slot_list[busnumber] = new_slot;
  797. } else {
  798. next = cpqhp_slot_list[busnumber];
  799. while (next->next != NULL)
  800. next = next->next;
  801. next->next = new_slot;
  802. }
  803. return new_slot;
  804. }
  805. /**
  806. * slot_remove - Removes a node from the linked list of slots.
  807. * @old_slot: slot to remove
  808. *
  809. * Returns 0 if successful, !0 otherwise.
  810. */
  811. static int slot_remove(struct pci_func * old_slot)
  812. {
  813. struct pci_func *next;
  814. if (old_slot == NULL)
  815. return 1;
  816. next = cpqhp_slot_list[old_slot->bus];
  817. if (next == NULL) {
  818. return 1;
  819. }
  820. if (next == old_slot) {
  821. cpqhp_slot_list[old_slot->bus] = old_slot->next;
  822. cpqhp_destroy_board_resources(old_slot);
  823. kfree(old_slot);
  824. return 0;
  825. }
  826. while ((next->next != old_slot) && (next->next != NULL)) {
  827. next = next->next;
  828. }
  829. if (next->next == old_slot) {
  830. next->next = old_slot->next;
  831. cpqhp_destroy_board_resources(old_slot);
  832. kfree(old_slot);
  833. return 0;
  834. } else
  835. return 2;
  836. }
  837. /**
  838. * bridge_slot_remove - Removes a node from the linked list of slots.
  839. * @bridge: bridge to remove
  840. *
  841. * Returns 0 if successful, !0 otherwise.
  842. */
  843. static int bridge_slot_remove(struct pci_func *bridge)
  844. {
  845. u8 subordinateBus, secondaryBus;
  846. u8 tempBus;
  847. struct pci_func *next;
  848. secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
  849. subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
  850. for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
  851. next = cpqhp_slot_list[tempBus];
  852. while (!slot_remove(next)) {
  853. next = cpqhp_slot_list[tempBus];
  854. }
  855. }
  856. next = cpqhp_slot_list[bridge->bus];
  857. if (next == NULL)
  858. return 1;
  859. if (next == bridge) {
  860. cpqhp_slot_list[bridge->bus] = bridge->next;
  861. goto out;
  862. }
  863. while ((next->next != bridge) && (next->next != NULL))
  864. next = next->next;
  865. if (next->next != bridge)
  866. return 2;
  867. next->next = bridge->next;
  868. out:
  869. kfree(bridge);
  870. return 0;
  871. }
  872. /**
  873. * cpqhp_slot_find - Looks for a node by bus, and device, multiple functions accessed
  874. * @bus: bus to find
  875. * @device: device to find
  876. * @index: is 0 for first function found, 1 for the second...
  877. *
  878. * Returns pointer to the node if successful, %NULL otherwise.
  879. */
  880. struct pci_func *cpqhp_slot_find(u8 bus, u8 device, u8 index)
  881. {
  882. int found = -1;
  883. struct pci_func *func;
  884. func = cpqhp_slot_list[bus];
  885. if ((func == NULL) || ((func->device == device) && (index == 0)))
  886. return func;
  887. if (func->device == device)
  888. found++;
  889. while (func->next != NULL) {
  890. func = func->next;
  891. if (func->device == device)
  892. found++;
  893. if (found == index)
  894. return func;
  895. }
  896. return NULL;
  897. }
  898. /* DJZ: I don't think is_bridge will work as is.
  899. * FIXME */
  900. static int is_bridge(struct pci_func * func)
  901. {
  902. /* Check the header type */
  903. if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
  904. return 1;
  905. else
  906. return 0;
  907. }
  908. /**
  909. * set_controller_speed - set the frequency and/or mode of a specific
  910. * controller segment.
  911. *
  912. * @ctrl: controller to change frequency/mode for.
  913. * @adapter_speed: the speed of the adapter we want to match.
  914. * @hp_slot: the slot number where the adapter is installed.
  915. *
  916. * Returns 0 if we successfully change frequency and/or mode to match the
  917. * adapter speed.
  918. *
  919. */
  920. static u8 set_controller_speed(struct controller *ctrl, u8 adapter_speed, u8 hp_slot)
  921. {
  922. struct slot *slot;
  923. u8 reg;
  924. u8 slot_power = readb(ctrl->hpc_reg + SLOT_POWER);
  925. u16 reg16;
  926. u32 leds = readl(ctrl->hpc_reg + LED_CONTROL);
  927. if (ctrl->speed == adapter_speed)
  928. return 0;
  929. /* We don't allow freq/mode changes if we find another adapter running
  930. * in another slot on this controller */
  931. for(slot = ctrl->slot; slot; slot = slot->next) {
  932. if (slot->device == (hp_slot + ctrl->slot_device_offset))
  933. continue;
  934. if (!slot->hotplug_slot && !slot->hotplug_slot->info)
  935. continue;
  936. if (slot->hotplug_slot->info->adapter_status == 0)
  937. continue;
  938. /* If another adapter is running on the same segment but at a
  939. * lower speed/mode, we allow the new adapter to function at
  940. * this rate if supported */
  941. if (ctrl->speed < adapter_speed)
  942. return 0;
  943. return 1;
  944. }
  945. /* If the controller doesn't support freq/mode changes and the
  946. * controller is running at a higher mode, we bail */
  947. if ((ctrl->speed > adapter_speed) && (!ctrl->pcix_speed_capability))
  948. return 1;
  949. /* But we allow the adapter to run at a lower rate if possible */
  950. if ((ctrl->speed < adapter_speed) && (!ctrl->pcix_speed_capability))
  951. return 0;
  952. /* We try to set the max speed supported by both the adapter and
  953. * controller */
  954. if (ctrl->speed_capability < adapter_speed) {
  955. if (ctrl->speed == ctrl->speed_capability)
  956. return 0;
  957. adapter_speed = ctrl->speed_capability;
  958. }
  959. writel(0x0L, ctrl->hpc_reg + LED_CONTROL);
  960. writeb(0x00, ctrl->hpc_reg + SLOT_ENABLE);
  961. set_SOGO(ctrl);
  962. wait_for_ctrl_irq(ctrl);
  963. if (adapter_speed != PCI_SPEED_133MHz_PCIX)
  964. reg = 0xF5;
  965. else
  966. reg = 0xF4;
  967. pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
  968. reg16 = readw(ctrl->hpc_reg + NEXT_CURR_FREQ);
  969. reg16 &= ~0x000F;
  970. switch(adapter_speed) {
  971. case(PCI_SPEED_133MHz_PCIX):
  972. reg = 0x75;
  973. reg16 |= 0xB;
  974. break;
  975. case(PCI_SPEED_100MHz_PCIX):
  976. reg = 0x74;
  977. reg16 |= 0xA;
  978. break;
  979. case(PCI_SPEED_66MHz_PCIX):
  980. reg = 0x73;
  981. reg16 |= 0x9;
  982. break;
  983. case(PCI_SPEED_66MHz):
  984. reg = 0x73;
  985. reg16 |= 0x1;
  986. break;
  987. default: /* 33MHz PCI 2.2 */
  988. reg = 0x71;
  989. break;
  990. }
  991. reg16 |= 0xB << 12;
  992. writew(reg16, ctrl->hpc_reg + NEXT_CURR_FREQ);
  993. mdelay(5);
  994. /* Reenable interrupts */
  995. writel(0, ctrl->hpc_reg + INT_MASK);
  996. pci_write_config_byte(ctrl->pci_dev, 0x41, reg);
  997. /* Restart state machine */
  998. reg = ~0xF;
  999. pci_read_config_byte(ctrl->pci_dev, 0x43, &reg);
  1000. pci_write_config_byte(ctrl->pci_dev, 0x43, reg);
  1001. /* Only if mode change...*/
  1002. if (((ctrl->speed == PCI_SPEED_66MHz) && (adapter_speed == PCI_SPEED_66MHz_PCIX)) ||
  1003. ((ctrl->speed == PCI_SPEED_66MHz_PCIX) && (adapter_speed == PCI_SPEED_66MHz)))
  1004. set_SOGO(ctrl);
  1005. wait_for_ctrl_irq(ctrl);
  1006. mdelay(1100);
  1007. /* Restore LED/Slot state */
  1008. writel(leds, ctrl->hpc_reg + LED_CONTROL);
  1009. writeb(slot_power, ctrl->hpc_reg + SLOT_ENABLE);
  1010. set_SOGO(ctrl);
  1011. wait_for_ctrl_irq(ctrl);
  1012. ctrl->speed = adapter_speed;
  1013. slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1014. info("Successfully changed frequency/mode for adapter in slot %d\n",
  1015. slot->number);
  1016. return 0;
  1017. }
  1018. /* the following routines constitute the bulk of the
  1019. hotplug controller logic
  1020. */
  1021. /**
  1022. * board_replaced - Called after a board has been replaced in the system.
  1023. *
  1024. * This is only used if we don't have resources for hot add
  1025. * Turns power on for the board
  1026. * Checks to see if board is the same
  1027. * If board is same, reconfigures it
  1028. * If board isn't same, turns it back off.
  1029. *
  1030. */
  1031. static u32 board_replaced(struct pci_func *func, struct controller *ctrl)
  1032. {
  1033. u8 hp_slot;
  1034. u8 temp_byte;
  1035. u8 adapter_speed;
  1036. u32 rc = 0;
  1037. hp_slot = func->device - ctrl->slot_device_offset;
  1038. if (readl(ctrl->hpc_reg + INT_INPUT_CLEAR) & (0x01L << hp_slot)) {
  1039. /**********************************
  1040. * The switch is open.
  1041. **********************************/
  1042. rc = INTERLOCK_OPEN;
  1043. } else if (is_slot_enabled (ctrl, hp_slot)) {
  1044. /**********************************
  1045. * The board is already on
  1046. **********************************/
  1047. rc = CARD_FUNCTIONING;
  1048. } else {
  1049. mutex_lock(&ctrl->crit_sect);
  1050. /* turn on board without attaching to the bus */
  1051. enable_slot_power (ctrl, hp_slot);
  1052. set_SOGO(ctrl);
  1053. /* Wait for SOBS to be unset */
  1054. wait_for_ctrl_irq (ctrl);
  1055. /* Change bits in slot power register to force another shift out
  1056. * NOTE: this is to work around the timer bug */
  1057. temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
  1058. writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
  1059. writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
  1060. set_SOGO(ctrl);
  1061. /* Wait for SOBS to be unset */
  1062. wait_for_ctrl_irq (ctrl);
  1063. adapter_speed = get_adapter_speed(ctrl, hp_slot);
  1064. if (ctrl->speed != adapter_speed)
  1065. if (set_controller_speed(ctrl, adapter_speed, hp_slot))
  1066. rc = WRONG_BUS_FREQUENCY;
  1067. /* turn off board without attaching to the bus */
  1068. disable_slot_power (ctrl, hp_slot);
  1069. set_SOGO(ctrl);
  1070. /* Wait for SOBS to be unset */
  1071. wait_for_ctrl_irq (ctrl);
  1072. mutex_unlock(&ctrl->crit_sect);
  1073. if (rc)
  1074. return rc;
  1075. mutex_lock(&ctrl->crit_sect);
  1076. slot_enable (ctrl, hp_slot);
  1077. green_LED_blink (ctrl, hp_slot);
  1078. amber_LED_off (ctrl, hp_slot);
  1079. set_SOGO(ctrl);
  1080. /* Wait for SOBS to be unset */
  1081. wait_for_ctrl_irq (ctrl);
  1082. mutex_unlock(&ctrl->crit_sect);
  1083. /* Wait for ~1 second because of hot plug spec */
  1084. long_delay(1*HZ);
  1085. /* Check for a power fault */
  1086. if (func->status == 0xFF) {
  1087. /* power fault occurred, but it was benign */
  1088. rc = POWER_FAILURE;
  1089. func->status = 0;
  1090. } else
  1091. rc = cpqhp_valid_replace(ctrl, func);
  1092. if (!rc) {
  1093. /* It must be the same board */
  1094. rc = cpqhp_configure_board(ctrl, func);
  1095. /* If configuration fails, turn it off
  1096. * Get slot won't work for devices behind
  1097. * bridges, but in this case it will always be
  1098. * called for the "base" bus/dev/func of an
  1099. * adapter. */
  1100. mutex_lock(&ctrl->crit_sect);
  1101. amber_LED_on (ctrl, hp_slot);
  1102. green_LED_off (ctrl, hp_slot);
  1103. slot_disable (ctrl, hp_slot);
  1104. set_SOGO(ctrl);
  1105. /* Wait for SOBS to be unset */
  1106. wait_for_ctrl_irq (ctrl);
  1107. mutex_unlock(&ctrl->crit_sect);
  1108. if (rc)
  1109. return rc;
  1110. else
  1111. return 1;
  1112. } else {
  1113. /* Something is wrong
  1114. * Get slot won't work for devices behind bridges, but
  1115. * in this case it will always be called for the "base"
  1116. * bus/dev/func of an adapter. */
  1117. mutex_lock(&ctrl->crit_sect);
  1118. amber_LED_on (ctrl, hp_slot);
  1119. green_LED_off (ctrl, hp_slot);
  1120. slot_disable (ctrl, hp_slot);
  1121. set_SOGO(ctrl);
  1122. /* Wait for SOBS to be unset */
  1123. wait_for_ctrl_irq (ctrl);
  1124. mutex_unlock(&ctrl->crit_sect);
  1125. }
  1126. }
  1127. return rc;
  1128. }
  1129. /**
  1130. * board_added - Called after a board has been added to the system.
  1131. *
  1132. * Turns power on for the board
  1133. * Configures board
  1134. *
  1135. */
  1136. static u32 board_added(struct pci_func *func, struct controller *ctrl)
  1137. {
  1138. u8 hp_slot;
  1139. u8 temp_byte;
  1140. u8 adapter_speed;
  1141. int index;
  1142. u32 temp_register = 0xFFFFFFFF;
  1143. u32 rc = 0;
  1144. struct pci_func *new_slot = NULL;
  1145. struct slot *p_slot;
  1146. struct resource_lists res_lists;
  1147. hp_slot = func->device - ctrl->slot_device_offset;
  1148. dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n",
  1149. __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
  1150. mutex_lock(&ctrl->crit_sect);
  1151. /* turn on board without attaching to the bus */
  1152. enable_slot_power(ctrl, hp_slot);
  1153. set_SOGO(ctrl);
  1154. /* Wait for SOBS to be unset */
  1155. wait_for_ctrl_irq (ctrl);
  1156. /* Change bits in slot power register to force another shift out
  1157. * NOTE: this is to work around the timer bug */
  1158. temp_byte = readb(ctrl->hpc_reg + SLOT_POWER);
  1159. writeb(0x00, ctrl->hpc_reg + SLOT_POWER);
  1160. writeb(temp_byte, ctrl->hpc_reg + SLOT_POWER);
  1161. set_SOGO(ctrl);
  1162. /* Wait for SOBS to be unset */
  1163. wait_for_ctrl_irq (ctrl);
  1164. adapter_speed = get_adapter_speed(ctrl, hp_slot);
  1165. if (ctrl->speed != adapter_speed)
  1166. if (set_controller_speed(ctrl, adapter_speed, hp_slot))
  1167. rc = WRONG_BUS_FREQUENCY;
  1168. /* turn off board without attaching to the bus */
  1169. disable_slot_power (ctrl, hp_slot);
  1170. set_SOGO(ctrl);
  1171. /* Wait for SOBS to be unset */
  1172. wait_for_ctrl_irq(ctrl);
  1173. mutex_unlock(&ctrl->crit_sect);
  1174. if (rc)
  1175. return rc;
  1176. p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1177. /* turn on board and blink green LED */
  1178. dbg("%s: before down\n", __FUNCTION__);
  1179. mutex_lock(&ctrl->crit_sect);
  1180. dbg("%s: after down\n", __FUNCTION__);
  1181. dbg("%s: before slot_enable\n", __FUNCTION__);
  1182. slot_enable (ctrl, hp_slot);
  1183. dbg("%s: before green_LED_blink\n", __FUNCTION__);
  1184. green_LED_blink (ctrl, hp_slot);
  1185. dbg("%s: before amber_LED_blink\n", __FUNCTION__);
  1186. amber_LED_off (ctrl, hp_slot);
  1187. dbg("%s: before set_SOGO\n", __FUNCTION__);
  1188. set_SOGO(ctrl);
  1189. /* Wait for SOBS to be unset */
  1190. dbg("%s: before wait_for_ctrl_irq\n", __FUNCTION__);
  1191. wait_for_ctrl_irq (ctrl);
  1192. dbg("%s: after wait_for_ctrl_irq\n", __FUNCTION__);
  1193. dbg("%s: before up\n", __FUNCTION__);
  1194. mutex_unlock(&ctrl->crit_sect);
  1195. dbg("%s: after up\n", __FUNCTION__);
  1196. /* Wait for ~1 second because of hot plug spec */
  1197. dbg("%s: before long_delay\n", __FUNCTION__);
  1198. long_delay(1*HZ);
  1199. dbg("%s: after long_delay\n", __FUNCTION__);
  1200. dbg("%s: func status = %x\n", __FUNCTION__, func->status);
  1201. /* Check for a power fault */
  1202. if (func->status == 0xFF) {
  1203. /* power fault occurred, but it was benign */
  1204. temp_register = 0xFFFFFFFF;
  1205. dbg("%s: temp register set to %x by power fault\n", __FUNCTION__, temp_register);
  1206. rc = POWER_FAILURE;
  1207. func->status = 0;
  1208. } else {
  1209. /* Get vendor/device ID u32 */
  1210. ctrl->pci_bus->number = func->bus;
  1211. rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), PCI_VENDOR_ID, &temp_register);
  1212. dbg("%s: pci_read_config_dword returns %d\n", __FUNCTION__, rc);
  1213. dbg("%s: temp_register is %x\n", __FUNCTION__, temp_register);
  1214. if (rc != 0) {
  1215. /* Something's wrong here */
  1216. temp_register = 0xFFFFFFFF;
  1217. dbg("%s: temp register set to %x by error\n", __FUNCTION__, temp_register);
  1218. }
  1219. /* Preset return code. It will be changed later if things go okay. */
  1220. rc = NO_ADAPTER_PRESENT;
  1221. }
  1222. /* All F's is an empty slot or an invalid board */
  1223. if (temp_register != 0xFFFFFFFF) { /* Check for a board in the slot */
  1224. res_lists.io_head = ctrl->io_head;
  1225. res_lists.mem_head = ctrl->mem_head;
  1226. res_lists.p_mem_head = ctrl->p_mem_head;
  1227. res_lists.bus_head = ctrl->bus_head;
  1228. res_lists.irqs = NULL;
  1229. rc = configure_new_device(ctrl, func, 0, &res_lists);
  1230. dbg("%s: back from configure_new_device\n", __FUNCTION__);
  1231. ctrl->io_head = res_lists.io_head;
  1232. ctrl->mem_head = res_lists.mem_head;
  1233. ctrl->p_mem_head = res_lists.p_mem_head;
  1234. ctrl->bus_head = res_lists.bus_head;
  1235. cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1236. cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1237. cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1238. cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1239. if (rc) {
  1240. mutex_lock(&ctrl->crit_sect);
  1241. amber_LED_on (ctrl, hp_slot);
  1242. green_LED_off (ctrl, hp_slot);
  1243. slot_disable (ctrl, hp_slot);
  1244. set_SOGO(ctrl);
  1245. /* Wait for SOBS to be unset */
  1246. wait_for_ctrl_irq (ctrl);
  1247. mutex_unlock(&ctrl->crit_sect);
  1248. return rc;
  1249. } else {
  1250. cpqhp_save_slot_config(ctrl, func);
  1251. }
  1252. func->status = 0;
  1253. func->switch_save = 0x10;
  1254. func->is_a_board = 0x01;
  1255. /* next, we will instantiate the linux pci_dev structures (with
  1256. * appropriate driver notification, if already present) */
  1257. dbg("%s: configure linux pci_dev structure\n", __FUNCTION__);
  1258. index = 0;
  1259. do {
  1260. new_slot = cpqhp_slot_find(ctrl->bus, func->device, index++);
  1261. if (new_slot && !new_slot->pci_dev) {
  1262. cpqhp_configure_device(ctrl, new_slot);
  1263. }
  1264. } while (new_slot);
  1265. mutex_lock(&ctrl->crit_sect);
  1266. green_LED_on (ctrl, hp_slot);
  1267. set_SOGO(ctrl);
  1268. /* Wait for SOBS to be unset */
  1269. wait_for_ctrl_irq (ctrl);
  1270. mutex_unlock(&ctrl->crit_sect);
  1271. } else {
  1272. mutex_lock(&ctrl->crit_sect);
  1273. amber_LED_on (ctrl, hp_slot);
  1274. green_LED_off (ctrl, hp_slot);
  1275. slot_disable (ctrl, hp_slot);
  1276. set_SOGO(ctrl);
  1277. /* Wait for SOBS to be unset */
  1278. wait_for_ctrl_irq (ctrl);
  1279. mutex_unlock(&ctrl->crit_sect);
  1280. return rc;
  1281. }
  1282. return 0;
  1283. }
  1284. /**
  1285. * remove_board - Turns off slot and LED's
  1286. *
  1287. */
  1288. static u32 remove_board(struct pci_func * func, u32 replace_flag, struct controller * ctrl)
  1289. {
  1290. int index;
  1291. u8 skip = 0;
  1292. u8 device;
  1293. u8 hp_slot;
  1294. u8 temp_byte;
  1295. u32 rc;
  1296. struct resource_lists res_lists;
  1297. struct pci_func *temp_func;
  1298. if (cpqhp_unconfigure_device(func))
  1299. return 1;
  1300. device = func->device;
  1301. hp_slot = func->device - ctrl->slot_device_offset;
  1302. dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
  1303. /* When we get here, it is safe to change base address registers.
  1304. * We will attempt to save the base address register lengths */
  1305. if (replace_flag || !ctrl->add_support)
  1306. rc = cpqhp_save_base_addr_length(ctrl, func);
  1307. else if (!func->bus_head && !func->mem_head &&
  1308. !func->p_mem_head && !func->io_head) {
  1309. /* Here we check to see if we've saved any of the board's
  1310. * resources already. If so, we'll skip the attempt to
  1311. * determine what's being used. */
  1312. index = 0;
  1313. temp_func = cpqhp_slot_find(func->bus, func->device, index++);
  1314. while (temp_func) {
  1315. if (temp_func->bus_head || temp_func->mem_head
  1316. || temp_func->p_mem_head || temp_func->io_head) {
  1317. skip = 1;
  1318. break;
  1319. }
  1320. temp_func = cpqhp_slot_find(temp_func->bus, temp_func->device, index++);
  1321. }
  1322. if (!skip)
  1323. rc = cpqhp_save_used_resources(ctrl, func);
  1324. }
  1325. /* Change status to shutdown */
  1326. if (func->is_a_board)
  1327. func->status = 0x01;
  1328. func->configured = 0;
  1329. mutex_lock(&ctrl->crit_sect);
  1330. green_LED_off (ctrl, hp_slot);
  1331. slot_disable (ctrl, hp_slot);
  1332. set_SOGO(ctrl);
  1333. /* turn off SERR for slot */
  1334. temp_byte = readb(ctrl->hpc_reg + SLOT_SERR);
  1335. temp_byte &= ~(0x01 << hp_slot);
  1336. writeb(temp_byte, ctrl->hpc_reg + SLOT_SERR);
  1337. /* Wait for SOBS to be unset */
  1338. wait_for_ctrl_irq (ctrl);
  1339. mutex_unlock(&ctrl->crit_sect);
  1340. if (!replace_flag && ctrl->add_support) {
  1341. while (func) {
  1342. res_lists.io_head = ctrl->io_head;
  1343. res_lists.mem_head = ctrl->mem_head;
  1344. res_lists.p_mem_head = ctrl->p_mem_head;
  1345. res_lists.bus_head = ctrl->bus_head;
  1346. cpqhp_return_board_resources(func, &res_lists);
  1347. ctrl->io_head = res_lists.io_head;
  1348. ctrl->mem_head = res_lists.mem_head;
  1349. ctrl->p_mem_head = res_lists.p_mem_head;
  1350. ctrl->bus_head = res_lists.bus_head;
  1351. cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  1352. cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  1353. cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  1354. cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  1355. if (is_bridge(func)) {
  1356. bridge_slot_remove(func);
  1357. } else
  1358. slot_remove(func);
  1359. func = cpqhp_slot_find(ctrl->bus, device, 0);
  1360. }
  1361. /* Setup slot structure with entry for empty slot */
  1362. func = cpqhp_slot_create(ctrl->bus);
  1363. if (func == NULL)
  1364. return 1;
  1365. func->bus = ctrl->bus;
  1366. func->device = device;
  1367. func->function = 0;
  1368. func->configured = 0;
  1369. func->switch_save = 0x10;
  1370. func->is_a_board = 0;
  1371. func->p_task_event = NULL;
  1372. }
  1373. return 0;
  1374. }
  1375. static void pushbutton_helper_thread(unsigned long data)
  1376. {
  1377. pushbutton_pending = data;
  1378. up(&event_semaphore);
  1379. }
  1380. /* this is the main worker thread */
  1381. static int event_thread(void* data)
  1382. {
  1383. struct controller *ctrl;
  1384. daemonize("phpd_event");
  1385. while (1) {
  1386. dbg("!!!!event_thread sleeping\n");
  1387. down_interruptible (&event_semaphore);
  1388. dbg("event_thread woken finished = %d\n", event_finished);
  1389. if (event_finished) break;
  1390. /* Do stuff here */
  1391. if (pushbutton_pending)
  1392. cpqhp_pushbutton_thread(pushbutton_pending);
  1393. else
  1394. for (ctrl = cpqhp_ctrl_list; ctrl; ctrl=ctrl->next)
  1395. interrupt_event_handler(ctrl);
  1396. }
  1397. dbg("event_thread signals exit\n");
  1398. up(&event_exit);
  1399. return 0;
  1400. }
  1401. int cpqhp_event_start_thread(void)
  1402. {
  1403. int pid;
  1404. /* initialize our semaphores */
  1405. init_MUTEX(&delay_sem);
  1406. init_MUTEX_LOCKED(&event_semaphore);
  1407. init_MUTEX_LOCKED(&event_exit);
  1408. event_finished=0;
  1409. pid = kernel_thread(event_thread, NULL, 0);
  1410. if (pid < 0) {
  1411. err ("Can't start up our event thread\n");
  1412. return -1;
  1413. }
  1414. dbg("Our event thread pid = %d\n", pid);
  1415. return 0;
  1416. }
  1417. void cpqhp_event_stop_thread(void)
  1418. {
  1419. event_finished = 1;
  1420. dbg("event_thread finish command given\n");
  1421. up(&event_semaphore);
  1422. dbg("wait for event_thread to exit\n");
  1423. down(&event_exit);
  1424. }
  1425. static int update_slot_info(struct controller *ctrl, struct slot *slot)
  1426. {
  1427. struct hotplug_slot_info *info;
  1428. int result;
  1429. info = kmalloc(sizeof(*info), GFP_KERNEL);
  1430. if (!info)
  1431. return -ENOMEM;
  1432. info->power_status = get_slot_enabled(ctrl, slot);
  1433. info->attention_status = cpq_get_attention_status(ctrl, slot);
  1434. info->latch_status = cpq_get_latch_status(ctrl, slot);
  1435. info->adapter_status = get_presence_status(ctrl, slot);
  1436. result = pci_hp_change_slot_info(slot->hotplug_slot, info);
  1437. kfree (info);
  1438. return result;
  1439. }
  1440. static void interrupt_event_handler(struct controller *ctrl)
  1441. {
  1442. int loop = 0;
  1443. int change = 1;
  1444. struct pci_func *func;
  1445. u8 hp_slot;
  1446. struct slot *p_slot;
  1447. while (change) {
  1448. change = 0;
  1449. for (loop = 0; loop < 10; loop++) {
  1450. /* dbg("loop %d\n", loop); */
  1451. if (ctrl->event_queue[loop].event_type != 0) {
  1452. hp_slot = ctrl->event_queue[loop].hp_slot;
  1453. func = cpqhp_slot_find(ctrl->bus, (hp_slot + ctrl->slot_device_offset), 0);
  1454. if (!func)
  1455. return;
  1456. p_slot = cpqhp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  1457. if (!p_slot)
  1458. return;
  1459. dbg("hp_slot %d, func %p, p_slot %p\n",
  1460. hp_slot, func, p_slot);
  1461. if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
  1462. dbg("button pressed\n");
  1463. } else if (ctrl->event_queue[loop].event_type ==
  1464. INT_BUTTON_CANCEL) {
  1465. dbg("button cancel\n");
  1466. del_timer(&p_slot->task_event);
  1467. mutex_lock(&ctrl->crit_sect);
  1468. if (p_slot->state == BLINKINGOFF_STATE) {
  1469. /* slot is on */
  1470. dbg("turn on green LED\n");
  1471. green_LED_on (ctrl, hp_slot);
  1472. } else if (p_slot->state == BLINKINGON_STATE) {
  1473. /* slot is off */
  1474. dbg("turn off green LED\n");
  1475. green_LED_off (ctrl, hp_slot);
  1476. }
  1477. info(msg_button_cancel, p_slot->number);
  1478. p_slot->state = STATIC_STATE;
  1479. amber_LED_off (ctrl, hp_slot);
  1480. set_SOGO(ctrl);
  1481. /* Wait for SOBS to be unset */
  1482. wait_for_ctrl_irq (ctrl);
  1483. mutex_unlock(&ctrl->crit_sect);
  1484. }
  1485. /*** button Released (No action on press...) */
  1486. else if (ctrl->event_queue[loop].event_type == INT_BUTTON_RELEASE) {
  1487. dbg("button release\n");
  1488. if (is_slot_enabled (ctrl, hp_slot)) {
  1489. dbg("slot is on\n");
  1490. p_slot->state = BLINKINGOFF_STATE;
  1491. info(msg_button_off, p_slot->number);
  1492. } else {
  1493. dbg("slot is off\n");
  1494. p_slot->state = BLINKINGON_STATE;
  1495. info(msg_button_on, p_slot->number);
  1496. }
  1497. mutex_lock(&ctrl->crit_sect);
  1498. dbg("blink green LED and turn off amber\n");
  1499. amber_LED_off (ctrl, hp_slot);
  1500. green_LED_blink (ctrl, hp_slot);
  1501. set_SOGO(ctrl);
  1502. /* Wait for SOBS to be unset */
  1503. wait_for_ctrl_irq (ctrl);
  1504. mutex_unlock(&ctrl->crit_sect);
  1505. init_timer(&p_slot->task_event);
  1506. p_slot->hp_slot = hp_slot;
  1507. p_slot->ctrl = ctrl;
  1508. /* p_slot->physical_slot = physical_slot; */
  1509. p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
  1510. p_slot->task_event.function = pushbutton_helper_thread;
  1511. p_slot->task_event.data = (u32) p_slot;
  1512. dbg("add_timer p_slot = %p\n", p_slot);
  1513. add_timer(&p_slot->task_event);
  1514. }
  1515. /***********POWER FAULT */
  1516. else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
  1517. dbg("power fault\n");
  1518. } else {
  1519. /* refresh notification */
  1520. if (p_slot)
  1521. update_slot_info(ctrl, p_slot);
  1522. }
  1523. ctrl->event_queue[loop].event_type = 0;
  1524. change = 1;
  1525. }
  1526. } /* End of FOR loop */
  1527. }
  1528. return;
  1529. }
  1530. /**
  1531. * cpqhp_pushbutton_thread
  1532. *
  1533. * Scheduled procedure to handle blocking stuff for the pushbuttons
  1534. * Handles all pending events and exits.
  1535. *
  1536. */
  1537. void cpqhp_pushbutton_thread(unsigned long slot)
  1538. {
  1539. u8 hp_slot;
  1540. u8 device;
  1541. struct pci_func *func;
  1542. struct slot *p_slot = (struct slot *) slot;
  1543. struct controller *ctrl = (struct controller *) p_slot->ctrl;
  1544. pushbutton_pending = 0;
  1545. hp_slot = p_slot->hp_slot;
  1546. device = p_slot->device;
  1547. if (is_slot_enabled(ctrl, hp_slot)) {
  1548. p_slot->state = POWEROFF_STATE;
  1549. /* power Down board */
  1550. func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
  1551. dbg("In power_down_board, func = %p, ctrl = %p\n", func, ctrl);
  1552. if (!func) {
  1553. dbg("Error! func NULL in %s\n", __FUNCTION__);
  1554. return ;
  1555. }
  1556. if (func != NULL && ctrl != NULL) {
  1557. if (cpqhp_process_SS(ctrl, func) != 0) {
  1558. amber_LED_on (ctrl, hp_slot);
  1559. green_LED_on (ctrl, hp_slot);
  1560. set_SOGO(ctrl);
  1561. /* Wait for SOBS to be unset */
  1562. wait_for_ctrl_irq (ctrl);
  1563. }
  1564. }
  1565. p_slot->state = STATIC_STATE;
  1566. } else {
  1567. p_slot->state = POWERON_STATE;
  1568. /* slot is off */
  1569. func = cpqhp_slot_find(p_slot->bus, p_slot->device, 0);
  1570. dbg("In add_board, func = %p, ctrl = %p\n", func, ctrl);
  1571. if (!func) {
  1572. dbg("Error! func NULL in %s\n", __FUNCTION__);
  1573. return ;
  1574. }
  1575. if (func != NULL && ctrl != NULL) {
  1576. if (cpqhp_process_SI(ctrl, func) != 0) {
  1577. amber_LED_on(ctrl, hp_slot);
  1578. green_LED_off(ctrl, hp_slot);
  1579. set_SOGO(ctrl);
  1580. /* Wait for SOBS to be unset */
  1581. wait_for_ctrl_irq (ctrl);
  1582. }
  1583. }
  1584. p_slot->state = STATIC_STATE;
  1585. }
  1586. return;
  1587. }
  1588. int cpqhp_process_SI(struct controller *ctrl, struct pci_func *func)
  1589. {
  1590. u8 device, hp_slot;
  1591. u16 temp_word;
  1592. u32 tempdword;
  1593. int rc;
  1594. struct slot* p_slot;
  1595. int physical_slot = 0;
  1596. tempdword = 0;
  1597. device = func->device;
  1598. hp_slot = device - ctrl->slot_device_offset;
  1599. p_slot = cpqhp_find_slot(ctrl, device);
  1600. if (p_slot)
  1601. physical_slot = p_slot->number;
  1602. /* Check to see if the interlock is closed */
  1603. tempdword = readl(ctrl->hpc_reg + INT_INPUT_CLEAR);
  1604. if (tempdword & (0x01 << hp_slot)) {
  1605. return 1;
  1606. }
  1607. if (func->is_a_board) {
  1608. rc = board_replaced(func, ctrl);
  1609. } else {
  1610. /* add board */
  1611. slot_remove(func);
  1612. func = cpqhp_slot_create(ctrl->bus);
  1613. if (func == NULL)
  1614. return 1;
  1615. func->bus = ctrl->bus;
  1616. func->device = device;
  1617. func->function = 0;
  1618. func->configured = 0;
  1619. func->is_a_board = 1;
  1620. /* We have to save the presence info for these slots */
  1621. temp_word = ctrl->ctrl_int_comp >> 16;
  1622. func->presence_save = (temp_word >> hp_slot) & 0x01;
  1623. func->presence_save |= (temp_word >> (hp_slot + 7)) & 0x02;
  1624. if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
  1625. func->switch_save = 0;
  1626. } else {
  1627. func->switch_save = 0x10;
  1628. }
  1629. rc = board_added(func, ctrl);
  1630. if (rc) {
  1631. if (is_bridge(func)) {
  1632. bridge_slot_remove(func);
  1633. } else
  1634. slot_remove(func);
  1635. /* Setup slot structure with entry for empty slot */
  1636. func = cpqhp_slot_create(ctrl->bus);
  1637. if (func == NULL)
  1638. return 1;
  1639. func->bus = ctrl->bus;
  1640. func->device = device;
  1641. func->function = 0;
  1642. func->configured = 0;
  1643. func->is_a_board = 0;
  1644. /* We have to save the presence info for these slots */
  1645. temp_word = ctrl->ctrl_int_comp >> 16;
  1646. func->presence_save = (temp_word >> hp_slot) & 0x01;
  1647. func->presence_save |=
  1648. (temp_word >> (hp_slot + 7)) & 0x02;
  1649. if (ctrl->ctrl_int_comp & (0x1L << hp_slot)) {
  1650. func->switch_save = 0;
  1651. } else {
  1652. func->switch_save = 0x10;
  1653. }
  1654. }
  1655. }
  1656. if (rc) {
  1657. dbg("%s: rc = %d\n", __FUNCTION__, rc);
  1658. }
  1659. if (p_slot)
  1660. update_slot_info(ctrl, p_slot);
  1661. return rc;
  1662. }
  1663. int cpqhp_process_SS(struct controller *ctrl, struct pci_func *func)
  1664. {
  1665. u8 device, class_code, header_type, BCR;
  1666. u8 index = 0;
  1667. u8 replace_flag;
  1668. u32 rc = 0;
  1669. unsigned int devfn;
  1670. struct slot* p_slot;
  1671. struct pci_bus *pci_bus = ctrl->pci_bus;
  1672. int physical_slot=0;
  1673. device = func->device;
  1674. func = cpqhp_slot_find(ctrl->bus, device, index++);
  1675. p_slot = cpqhp_find_slot(ctrl, device);
  1676. if (p_slot) {
  1677. physical_slot = p_slot->number;
  1678. }
  1679. /* Make sure there are no video controllers here */
  1680. while (func && !rc) {
  1681. pci_bus->number = func->bus;
  1682. devfn = PCI_DEVFN(func->device, func->function);
  1683. /* Check the Class Code */
  1684. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  1685. if (rc)
  1686. return rc;
  1687. if (class_code == PCI_BASE_CLASS_DISPLAY) {
  1688. /* Display/Video adapter (not supported) */
  1689. rc = REMOVE_NOT_SUPPORTED;
  1690. } else {
  1691. /* See if it's a bridge */
  1692. rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
  1693. if (rc)
  1694. return rc;
  1695. /* If it's a bridge, check the VGA Enable bit */
  1696. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  1697. rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
  1698. if (rc)
  1699. return rc;
  1700. /* If the VGA Enable bit is set, remove isn't
  1701. * supported */
  1702. if (BCR & PCI_BRIDGE_CTL_VGA) {
  1703. rc = REMOVE_NOT_SUPPORTED;
  1704. }
  1705. }
  1706. }
  1707. func = cpqhp_slot_find(ctrl->bus, device, index++);
  1708. }
  1709. func = cpqhp_slot_find(ctrl->bus, device, 0);
  1710. if ((func != NULL) && !rc) {
  1711. /* FIXME: Replace flag should be passed into process_SS */
  1712. replace_flag = !(ctrl->add_support);
  1713. rc = remove_board(func, replace_flag, ctrl);
  1714. } else if (!rc) {
  1715. rc = 1;
  1716. }
  1717. if (p_slot)
  1718. update_slot_info(ctrl, p_slot);
  1719. return rc;
  1720. }
  1721. /**
  1722. * switch_leds: switch the leds, go from one site to the other.
  1723. * @ctrl: controller to use
  1724. * @num_of_slots: number of slots to use
  1725. * @direction: 1 to start from the left side, 0 to start right.
  1726. */
  1727. static void switch_leds(struct controller *ctrl, const int num_of_slots,
  1728. u32 *work_LED, const int direction)
  1729. {
  1730. int loop;
  1731. for (loop = 0; loop < num_of_slots; loop++) {
  1732. if (direction)
  1733. *work_LED = *work_LED >> 1;
  1734. else
  1735. *work_LED = *work_LED << 1;
  1736. writel(*work_LED, ctrl->hpc_reg + LED_CONTROL);
  1737. set_SOGO(ctrl);
  1738. /* Wait for SOGO interrupt */
  1739. wait_for_ctrl_irq(ctrl);
  1740. /* Get ready for next iteration */
  1741. long_delay((2*HZ)/10);
  1742. }
  1743. }
  1744. /**
  1745. * hardware_test - runs hardware tests
  1746. *
  1747. * For hot plug ctrl folks to play with.
  1748. * test_num is the number written to the "test" file in sysfs
  1749. *
  1750. */
  1751. int cpqhp_hardware_test(struct controller *ctrl, int test_num)
  1752. {
  1753. u32 save_LED;
  1754. u32 work_LED;
  1755. int loop;
  1756. int num_of_slots;
  1757. num_of_slots = readb(ctrl->hpc_reg + SLOT_MASK) & 0x0f;
  1758. switch (test_num) {
  1759. case 1:
  1760. /* Do stuff here! */
  1761. /* Do that funky LED thing */
  1762. /* so we can restore them later */
  1763. save_LED = readl(ctrl->hpc_reg + LED_CONTROL);
  1764. work_LED = 0x01010101;
  1765. switch_leds(ctrl, num_of_slots, &work_LED, 0);
  1766. switch_leds(ctrl, num_of_slots, &work_LED, 1);
  1767. switch_leds(ctrl, num_of_slots, &work_LED, 0);
  1768. switch_leds(ctrl, num_of_slots, &work_LED, 1);
  1769. work_LED = 0x01010000;
  1770. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1771. switch_leds(ctrl, num_of_slots, &work_LED, 0);
  1772. switch_leds(ctrl, num_of_slots, &work_LED, 1);
  1773. work_LED = 0x00000101;
  1774. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1775. switch_leds(ctrl, num_of_slots, &work_LED, 0);
  1776. switch_leds(ctrl, num_of_slots, &work_LED, 1);
  1777. work_LED = 0x01010000;
  1778. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1779. for (loop = 0; loop < num_of_slots; loop++) {
  1780. set_SOGO(ctrl);
  1781. /* Wait for SOGO interrupt */
  1782. wait_for_ctrl_irq (ctrl);
  1783. /* Get ready for next iteration */
  1784. long_delay((3*HZ)/10);
  1785. work_LED = work_LED >> 16;
  1786. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1787. set_SOGO(ctrl);
  1788. /* Wait for SOGO interrupt */
  1789. wait_for_ctrl_irq (ctrl);
  1790. /* Get ready for next iteration */
  1791. long_delay((3*HZ)/10);
  1792. work_LED = work_LED << 16;
  1793. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1794. work_LED = work_LED << 1;
  1795. writel(work_LED, ctrl->hpc_reg + LED_CONTROL);
  1796. }
  1797. /* put it back the way it was */
  1798. writel(save_LED, ctrl->hpc_reg + LED_CONTROL);
  1799. set_SOGO(ctrl);
  1800. /* Wait for SOBS to be unset */
  1801. wait_for_ctrl_irq (ctrl);
  1802. break;
  1803. case 2:
  1804. /* Do other stuff here! */
  1805. break;
  1806. case 3:
  1807. /* and more... */
  1808. break;
  1809. }
  1810. return 0;
  1811. }
  1812. /**
  1813. * configure_new_device - Configures the PCI header information of one board.
  1814. *
  1815. * @ctrl: pointer to controller structure
  1816. * @func: pointer to function structure
  1817. * @behind_bridge: 1 if this is a recursive call, 0 if not
  1818. * @resources: pointer to set of resource lists
  1819. *
  1820. * Returns 0 if success
  1821. *
  1822. */
  1823. static u32 configure_new_device(struct controller * ctrl, struct pci_func * func,
  1824. u8 behind_bridge, struct resource_lists * resources)
  1825. {
  1826. u8 temp_byte, function, max_functions, stop_it;
  1827. int rc;
  1828. u32 ID;
  1829. struct pci_func *new_slot;
  1830. int index;
  1831. new_slot = func;
  1832. dbg("%s\n", __FUNCTION__);
  1833. /* Check for Multi-function device */
  1834. ctrl->pci_bus->number = func->bus;
  1835. rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(func->device, func->function), 0x0E, &temp_byte);
  1836. if (rc) {
  1837. dbg("%s: rc = %d\n", __FUNCTION__, rc);
  1838. return rc;
  1839. }
  1840. if (temp_byte & 0x80) /* Multi-function device */
  1841. max_functions = 8;
  1842. else
  1843. max_functions = 1;
  1844. function = 0;
  1845. do {
  1846. rc = configure_new_function(ctrl, new_slot, behind_bridge, resources);
  1847. if (rc) {
  1848. dbg("configure_new_function failed %d\n",rc);
  1849. index = 0;
  1850. while (new_slot) {
  1851. new_slot = cpqhp_slot_find(new_slot->bus, new_slot->device, index++);
  1852. if (new_slot)
  1853. cpqhp_return_board_resources(new_slot, resources);
  1854. }
  1855. return rc;
  1856. }
  1857. function++;
  1858. stop_it = 0;
  1859. /* The following loop skips to the next present function
  1860. * and creates a board structure */
  1861. while ((function < max_functions) && (!stop_it)) {
  1862. pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(func->device, function), 0x00, &ID);
  1863. if (ID == 0xFFFFFFFF) { /* There's nothing there. */
  1864. function++;
  1865. } else { /* There's something there */
  1866. /* Setup slot structure. */
  1867. new_slot = cpqhp_slot_create(func->bus);
  1868. if (new_slot == NULL)
  1869. return 1;
  1870. new_slot->bus = func->bus;
  1871. new_slot->device = func->device;
  1872. new_slot->function = function;
  1873. new_slot->is_a_board = 1;
  1874. new_slot->status = 0;
  1875. stop_it++;
  1876. }
  1877. }
  1878. } while (function < max_functions);
  1879. dbg("returning from configure_new_device\n");
  1880. return 0;
  1881. }
  1882. /*
  1883. Configuration logic that involves the hotplug data structures and
  1884. their bookkeeping
  1885. */
  1886. /**
  1887. * configure_new_function - Configures the PCI header information of one device
  1888. *
  1889. * @ctrl: pointer to controller structure
  1890. * @func: pointer to function structure
  1891. * @behind_bridge: 1 if this is a recursive call, 0 if not
  1892. * @resources: pointer to set of resource lists
  1893. *
  1894. * Calls itself recursively for bridged devices.
  1895. * Returns 0 if success
  1896. *
  1897. */
  1898. static int configure_new_function(struct controller *ctrl, struct pci_func *func,
  1899. u8 behind_bridge,
  1900. struct resource_lists *resources)
  1901. {
  1902. int cloop;
  1903. u8 IRQ = 0;
  1904. u8 temp_byte;
  1905. u8 device;
  1906. u8 class_code;
  1907. u16 command;
  1908. u16 temp_word;
  1909. u32 temp_dword;
  1910. u32 rc;
  1911. u32 temp_register;
  1912. u32 base;
  1913. u32 ID;
  1914. unsigned int devfn;
  1915. struct pci_resource *mem_node;
  1916. struct pci_resource *p_mem_node;
  1917. struct pci_resource *io_node;
  1918. struct pci_resource *bus_node;
  1919. struct pci_resource *hold_mem_node;
  1920. struct pci_resource *hold_p_mem_node;
  1921. struct pci_resource *hold_IO_node;
  1922. struct pci_resource *hold_bus_node;
  1923. struct irq_mapping irqs;
  1924. struct pci_func *new_slot;
  1925. struct pci_bus *pci_bus;
  1926. struct resource_lists temp_resources;
  1927. pci_bus = ctrl->pci_bus;
  1928. pci_bus->number = func->bus;
  1929. devfn = PCI_DEVFN(func->device, func->function);
  1930. /* Check for Bridge */
  1931. rc = pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &temp_byte);
  1932. if (rc)
  1933. return rc;
  1934. if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
  1935. /* set Primary bus */
  1936. dbg("set Primary bus = %d\n", func->bus);
  1937. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_PRIMARY_BUS, func->bus);
  1938. if (rc)
  1939. return rc;
  1940. /* find range of busses to use */
  1941. dbg("find ranges of buses to use\n");
  1942. bus_node = get_max_resource(&(resources->bus_head), 1);
  1943. /* If we don't have any busses to allocate, we can't continue */
  1944. if (!bus_node)
  1945. return -ENOMEM;
  1946. /* set Secondary bus */
  1947. temp_byte = bus_node->base;
  1948. dbg("set Secondary bus = %d\n", bus_node->base);
  1949. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, temp_byte);
  1950. if (rc)
  1951. return rc;
  1952. /* set subordinate bus */
  1953. temp_byte = bus_node->base + bus_node->length - 1;
  1954. dbg("set subordinate bus = %d\n", bus_node->base + bus_node->length - 1);
  1955. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
  1956. if (rc)
  1957. return rc;
  1958. /* set subordinate Latency Timer and base Latency Timer */
  1959. temp_byte = 0x40;
  1960. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
  1961. if (rc)
  1962. return rc;
  1963. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
  1964. if (rc)
  1965. return rc;
  1966. /* set Cache Line size */
  1967. temp_byte = 0x08;
  1968. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
  1969. if (rc)
  1970. return rc;
  1971. /* Setup the IO, memory, and prefetchable windows */
  1972. io_node = get_max_resource(&(resources->io_head), 0x1000);
  1973. if (!io_node)
  1974. return -ENOMEM;
  1975. mem_node = get_max_resource(&(resources->mem_head), 0x100000);
  1976. if (!mem_node)
  1977. return -ENOMEM;
  1978. p_mem_node = get_max_resource(&(resources->p_mem_head), 0x100000);
  1979. if (!p_mem_node)
  1980. return -ENOMEM;
  1981. dbg("Setup the IO, memory, and prefetchable windows\n");
  1982. dbg("io_node\n");
  1983. dbg("(base, len, next) (%x, %x, %p)\n", io_node->base,
  1984. io_node->length, io_node->next);
  1985. dbg("mem_node\n");
  1986. dbg("(base, len, next) (%x, %x, %p)\n", mem_node->base,
  1987. mem_node->length, mem_node->next);
  1988. dbg("p_mem_node\n");
  1989. dbg("(base, len, next) (%x, %x, %p)\n", p_mem_node->base,
  1990. p_mem_node->length, p_mem_node->next);
  1991. /* set up the IRQ info */
  1992. if (!resources->irqs) {
  1993. irqs.barber_pole = 0;
  1994. irqs.interrupt[0] = 0;
  1995. irqs.interrupt[1] = 0;
  1996. irqs.interrupt[2] = 0;
  1997. irqs.interrupt[3] = 0;
  1998. irqs.valid_INT = 0;
  1999. } else {
  2000. irqs.barber_pole = resources->irqs->barber_pole;
  2001. irqs.interrupt[0] = resources->irqs->interrupt[0];
  2002. irqs.interrupt[1] = resources->irqs->interrupt[1];
  2003. irqs.interrupt[2] = resources->irqs->interrupt[2];
  2004. irqs.interrupt[3] = resources->irqs->interrupt[3];
  2005. irqs.valid_INT = resources->irqs->valid_INT;
  2006. }
  2007. /* set up resource lists that are now aligned on top and bottom
  2008. * for anything behind the bridge. */
  2009. temp_resources.bus_head = bus_node;
  2010. temp_resources.io_head = io_node;
  2011. temp_resources.mem_head = mem_node;
  2012. temp_resources.p_mem_head = p_mem_node;
  2013. temp_resources.irqs = &irqs;
  2014. /* Make copies of the nodes we are going to pass down so that
  2015. * if there is a problem,we can just use these to free resources */
  2016. hold_bus_node = kmalloc(sizeof(*hold_bus_node), GFP_KERNEL);
  2017. hold_IO_node = kmalloc(sizeof(*hold_IO_node), GFP_KERNEL);
  2018. hold_mem_node = kmalloc(sizeof(*hold_mem_node), GFP_KERNEL);
  2019. hold_p_mem_node = kmalloc(sizeof(*hold_p_mem_node), GFP_KERNEL);
  2020. if (!hold_bus_node || !hold_IO_node || !hold_mem_node || !hold_p_mem_node) {
  2021. kfree(hold_bus_node);
  2022. kfree(hold_IO_node);
  2023. kfree(hold_mem_node);
  2024. kfree(hold_p_mem_node);
  2025. return 1;
  2026. }
  2027. memcpy(hold_bus_node, bus_node, sizeof(struct pci_resource));
  2028. bus_node->base += 1;
  2029. bus_node->length -= 1;
  2030. bus_node->next = NULL;
  2031. /* If we have IO resources copy them and fill in the bridge's
  2032. * IO range registers */
  2033. if (io_node) {
  2034. memcpy(hold_IO_node, io_node, sizeof(struct pci_resource));
  2035. io_node->next = NULL;
  2036. /* set IO base and Limit registers */
  2037. temp_byte = io_node->base >> 8;
  2038. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_BASE, temp_byte);
  2039. temp_byte = (io_node->base + io_node->length - 1) >> 8;
  2040. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  2041. } else {
  2042. kfree(hold_IO_node);
  2043. hold_IO_node = NULL;
  2044. }
  2045. /* If we have memory resources copy them and fill in the
  2046. * bridge's memory range registers. Otherwise, fill in the
  2047. * range registers with values that disable them. */
  2048. if (mem_node) {
  2049. memcpy(hold_mem_node, mem_node, sizeof(struct pci_resource));
  2050. mem_node->next = NULL;
  2051. /* set Mem base and Limit registers */
  2052. temp_word = mem_node->base >> 16;
  2053. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2054. temp_word = (mem_node->base + mem_node->length - 1) >> 16;
  2055. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2056. } else {
  2057. temp_word = 0xFFFF;
  2058. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2059. temp_word = 0x0000;
  2060. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2061. kfree(hold_mem_node);
  2062. hold_mem_node = NULL;
  2063. }
  2064. memcpy(hold_p_mem_node, p_mem_node, sizeof(struct pci_resource));
  2065. p_mem_node->next = NULL;
  2066. /* set Pre Mem base and Limit registers */
  2067. temp_word = p_mem_node->base >> 16;
  2068. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  2069. temp_word = (p_mem_node->base + p_mem_node->length - 1) >> 16;
  2070. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2071. /* Adjust this to compensate for extra adjustment in first loop */
  2072. irqs.barber_pole--;
  2073. rc = 0;
  2074. /* Here we actually find the devices and configure them */
  2075. for (device = 0; (device <= 0x1F) && !rc; device++) {
  2076. irqs.barber_pole = (irqs.barber_pole + 1) & 0x03;
  2077. ID = 0xFFFFFFFF;
  2078. pci_bus->number = hold_bus_node->base;
  2079. pci_bus_read_config_dword (pci_bus, PCI_DEVFN(device, 0), 0x00, &ID);
  2080. pci_bus->number = func->bus;
  2081. if (ID != 0xFFFFFFFF) { /* device present */
  2082. /* Setup slot structure. */
  2083. new_slot = cpqhp_slot_create(hold_bus_node->base);
  2084. if (new_slot == NULL) {
  2085. rc = -ENOMEM;
  2086. continue;
  2087. }
  2088. new_slot->bus = hold_bus_node->base;
  2089. new_slot->device = device;
  2090. new_slot->function = 0;
  2091. new_slot->is_a_board = 1;
  2092. new_slot->status = 0;
  2093. rc = configure_new_device(ctrl, new_slot, 1, &temp_resources);
  2094. dbg("configure_new_device rc=0x%x\n",rc);
  2095. } /* End of IF (device in slot?) */
  2096. } /* End of FOR loop */
  2097. if (rc)
  2098. goto free_and_out;
  2099. /* save the interrupt routing information */
  2100. if (resources->irqs) {
  2101. resources->irqs->interrupt[0] = irqs.interrupt[0];
  2102. resources->irqs->interrupt[1] = irqs.interrupt[1];
  2103. resources->irqs->interrupt[2] = irqs.interrupt[2];
  2104. resources->irqs->interrupt[3] = irqs.interrupt[3];
  2105. resources->irqs->valid_INT = irqs.valid_INT;
  2106. } else if (!behind_bridge) {
  2107. /* We need to hook up the interrupts here */
  2108. for (cloop = 0; cloop < 4; cloop++) {
  2109. if (irqs.valid_INT & (0x01 << cloop)) {
  2110. rc = cpqhp_set_irq(func->bus, func->device,
  2111. 0x0A + cloop, irqs.interrupt[cloop]);
  2112. if (rc)
  2113. goto free_and_out;
  2114. }
  2115. } /* end of for loop */
  2116. }
  2117. /* Return unused bus resources
  2118. * First use the temporary node to store information for
  2119. * the board */
  2120. if (hold_bus_node && bus_node && temp_resources.bus_head) {
  2121. hold_bus_node->length = bus_node->base - hold_bus_node->base;
  2122. hold_bus_node->next = func->bus_head;
  2123. func->bus_head = hold_bus_node;
  2124. temp_byte = temp_resources.bus_head->base - 1;
  2125. /* set subordinate bus */
  2126. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_SUBORDINATE_BUS, temp_byte);
  2127. if (temp_resources.bus_head->length == 0) {
  2128. kfree(temp_resources.bus_head);
  2129. temp_resources.bus_head = NULL;
  2130. } else {
  2131. return_resource(&(resources->bus_head), temp_resources.bus_head);
  2132. }
  2133. }
  2134. /* If we have IO space available and there is some left,
  2135. * return the unused portion */
  2136. if (hold_IO_node && temp_resources.io_head) {
  2137. io_node = do_pre_bridge_resource_split(&(temp_resources.io_head),
  2138. &hold_IO_node, 0x1000);
  2139. /* Check if we were able to split something off */
  2140. if (io_node) {
  2141. hold_IO_node->base = io_node->base + io_node->length;
  2142. temp_byte = (hold_IO_node->base) >> 8;
  2143. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_BASE, temp_byte);
  2144. return_resource(&(resources->io_head), io_node);
  2145. }
  2146. io_node = do_bridge_resource_split(&(temp_resources.io_head), 0x1000);
  2147. /* Check if we were able to split something off */
  2148. if (io_node) {
  2149. /* First use the temporary node to store
  2150. * information for the board */
  2151. hold_IO_node->length = io_node->base - hold_IO_node->base;
  2152. /* If we used any, add it to the board's list */
  2153. if (hold_IO_node->length) {
  2154. hold_IO_node->next = func->io_head;
  2155. func->io_head = hold_IO_node;
  2156. temp_byte = (io_node->base - 1) >> 8;
  2157. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_IO_LIMIT, temp_byte);
  2158. return_resource(&(resources->io_head), io_node);
  2159. } else {
  2160. /* it doesn't need any IO */
  2161. temp_word = 0x0000;
  2162. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_IO_LIMIT, temp_word);
  2163. return_resource(&(resources->io_head), io_node);
  2164. kfree(hold_IO_node);
  2165. }
  2166. } else {
  2167. /* it used most of the range */
  2168. hold_IO_node->next = func->io_head;
  2169. func->io_head = hold_IO_node;
  2170. }
  2171. } else if (hold_IO_node) {
  2172. /* it used the whole range */
  2173. hold_IO_node->next = func->io_head;
  2174. func->io_head = hold_IO_node;
  2175. }
  2176. /* If we have memory space available and there is some left,
  2177. * return the unused portion */
  2178. if (hold_mem_node && temp_resources.mem_head) {
  2179. mem_node = do_pre_bridge_resource_split(&(temp_resources. mem_head),
  2180. &hold_mem_node, 0x100000);
  2181. /* Check if we were able to split something off */
  2182. if (mem_node) {
  2183. hold_mem_node->base = mem_node->base + mem_node->length;
  2184. temp_word = (hold_mem_node->base) >> 16;
  2185. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_BASE, temp_word);
  2186. return_resource(&(resources->mem_head), mem_node);
  2187. }
  2188. mem_node = do_bridge_resource_split(&(temp_resources.mem_head), 0x100000);
  2189. /* Check if we were able to split something off */
  2190. if (mem_node) {
  2191. /* First use the temporary node to store
  2192. * information for the board */
  2193. hold_mem_node->length = mem_node->base - hold_mem_node->base;
  2194. if (hold_mem_node->length) {
  2195. hold_mem_node->next = func->mem_head;
  2196. func->mem_head = hold_mem_node;
  2197. /* configure end address */
  2198. temp_word = (mem_node->base - 1) >> 16;
  2199. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2200. /* Return unused resources to the pool */
  2201. return_resource(&(resources->mem_head), mem_node);
  2202. } else {
  2203. /* it doesn't need any Mem */
  2204. temp_word = 0x0000;
  2205. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_MEMORY_LIMIT, temp_word);
  2206. return_resource(&(resources->mem_head), mem_node);
  2207. kfree(hold_mem_node);
  2208. }
  2209. } else {
  2210. /* it used most of the range */
  2211. hold_mem_node->next = func->mem_head;
  2212. func->mem_head = hold_mem_node;
  2213. }
  2214. } else if (hold_mem_node) {
  2215. /* it used the whole range */
  2216. hold_mem_node->next = func->mem_head;
  2217. func->mem_head = hold_mem_node;
  2218. }
  2219. /* If we have prefetchable memory space available and there
  2220. * is some left at the end, return the unused portion */
  2221. if (hold_p_mem_node && temp_resources.p_mem_head) {
  2222. p_mem_node = do_pre_bridge_resource_split(&(temp_resources.p_mem_head),
  2223. &hold_p_mem_node, 0x100000);
  2224. /* Check if we were able to split something off */
  2225. if (p_mem_node) {
  2226. hold_p_mem_node->base = p_mem_node->base + p_mem_node->length;
  2227. temp_word = (hold_p_mem_node->base) >> 16;
  2228. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_BASE, temp_word);
  2229. return_resource(&(resources->p_mem_head), p_mem_node);
  2230. }
  2231. p_mem_node = do_bridge_resource_split(&(temp_resources.p_mem_head), 0x100000);
  2232. /* Check if we were able to split something off */
  2233. if (p_mem_node) {
  2234. /* First use the temporary node to store
  2235. * information for the board */
  2236. hold_p_mem_node->length = p_mem_node->base - hold_p_mem_node->base;
  2237. /* If we used any, add it to the board's list */
  2238. if (hold_p_mem_node->length) {
  2239. hold_p_mem_node->next = func->p_mem_head;
  2240. func->p_mem_head = hold_p_mem_node;
  2241. temp_word = (p_mem_node->base - 1) >> 16;
  2242. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2243. return_resource(&(resources->p_mem_head), p_mem_node);
  2244. } else {
  2245. /* it doesn't need any PMem */
  2246. temp_word = 0x0000;
  2247. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, temp_word);
  2248. return_resource(&(resources->p_mem_head), p_mem_node);
  2249. kfree(hold_p_mem_node);
  2250. }
  2251. } else {
  2252. /* it used the most of the range */
  2253. hold_p_mem_node->next = func->p_mem_head;
  2254. func->p_mem_head = hold_p_mem_node;
  2255. }
  2256. } else if (hold_p_mem_node) {
  2257. /* it used the whole range */
  2258. hold_p_mem_node->next = func->p_mem_head;
  2259. func->p_mem_head = hold_p_mem_node;
  2260. }
  2261. /* We should be configuring an IRQ and the bridge's base address
  2262. * registers if it needs them. Although we have never seen such
  2263. * a device */
  2264. /* enable card */
  2265. command = 0x0157; /* = PCI_COMMAND_IO |
  2266. * PCI_COMMAND_MEMORY |
  2267. * PCI_COMMAND_MASTER |
  2268. * PCI_COMMAND_INVALIDATE |
  2269. * PCI_COMMAND_PARITY |
  2270. * PCI_COMMAND_SERR */
  2271. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_COMMAND, command);
  2272. /* set Bridge Control Register */
  2273. command = 0x07; /* = PCI_BRIDGE_CTL_PARITY |
  2274. * PCI_BRIDGE_CTL_SERR |
  2275. * PCI_BRIDGE_CTL_NO_ISA */
  2276. rc = pci_bus_write_config_word (pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
  2277. } else if ((temp_byte & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
  2278. /* Standard device */
  2279. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  2280. if (class_code == PCI_BASE_CLASS_DISPLAY) {
  2281. /* Display (video) adapter (not supported) */
  2282. return DEVICE_TYPE_NOT_SUPPORTED;
  2283. }
  2284. /* Figure out IO and memory needs */
  2285. for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
  2286. temp_register = 0xFFFFFFFF;
  2287. dbg("CND: bus=%d, devfn=%d, offset=%d\n", pci_bus->number, devfn, cloop);
  2288. rc = pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
  2289. rc = pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp_register);
  2290. dbg("CND: base = 0x%x\n", temp_register);
  2291. if (temp_register) { /* If this register is implemented */
  2292. if ((temp_register & 0x03L) == 0x01) {
  2293. /* Map IO */
  2294. /* set base = amount of IO space */
  2295. base = temp_register & 0xFFFFFFFC;
  2296. base = ~base + 1;
  2297. dbg("CND: length = 0x%x\n", base);
  2298. io_node = get_io_resource(&(resources->io_head), base);
  2299. dbg("Got io_node start = %8.8x, length = %8.8x next (%p)\n",
  2300. io_node->base, io_node->length, io_node->next);
  2301. dbg("func (%p) io_head (%p)\n", func, func->io_head);
  2302. /* allocate the resource to the board */
  2303. if (io_node) {
  2304. base = io_node->base;
  2305. io_node->next = func->io_head;
  2306. func->io_head = io_node;
  2307. } else
  2308. return -ENOMEM;
  2309. } else if ((temp_register & 0x0BL) == 0x08) {
  2310. /* Map prefetchable memory */
  2311. base = temp_register & 0xFFFFFFF0;
  2312. base = ~base + 1;
  2313. dbg("CND: length = 0x%x\n", base);
  2314. p_mem_node = get_resource(&(resources->p_mem_head), base);
  2315. /* allocate the resource to the board */
  2316. if (p_mem_node) {
  2317. base = p_mem_node->base;
  2318. p_mem_node->next = func->p_mem_head;
  2319. func->p_mem_head = p_mem_node;
  2320. } else
  2321. return -ENOMEM;
  2322. } else if ((temp_register & 0x0BL) == 0x00) {
  2323. /* Map memory */
  2324. base = temp_register & 0xFFFFFFF0;
  2325. base = ~base + 1;
  2326. dbg("CND: length = 0x%x\n", base);
  2327. mem_node = get_resource(&(resources->mem_head), base);
  2328. /* allocate the resource to the board */
  2329. if (mem_node) {
  2330. base = mem_node->base;
  2331. mem_node->next = func->mem_head;
  2332. func->mem_head = mem_node;
  2333. } else
  2334. return -ENOMEM;
  2335. } else if ((temp_register & 0x0BL) == 0x04) {
  2336. /* Map memory */
  2337. base = temp_register & 0xFFFFFFF0;
  2338. base = ~base + 1;
  2339. dbg("CND: length = 0x%x\n", base);
  2340. mem_node = get_resource(&(resources->mem_head), base);
  2341. /* allocate the resource to the board */
  2342. if (mem_node) {
  2343. base = mem_node->base;
  2344. mem_node->next = func->mem_head;
  2345. func->mem_head = mem_node;
  2346. } else
  2347. return -ENOMEM;
  2348. } else if ((temp_register & 0x0BL) == 0x06) {
  2349. /* Those bits are reserved, we can't handle this */
  2350. return 1;
  2351. } else {
  2352. /* Requesting space below 1M */
  2353. return NOT_ENOUGH_RESOURCES;
  2354. }
  2355. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
  2356. /* Check for 64-bit base */
  2357. if ((temp_register & 0x07L) == 0x04) {
  2358. cloop += 4;
  2359. /* Upper 32 bits of address always zero
  2360. * on today's systems */
  2361. /* FIXME this is probably not true on
  2362. * Alpha and ia64??? */
  2363. base = 0;
  2364. rc = pci_bus_write_config_dword(pci_bus, devfn, cloop, base);
  2365. }
  2366. }
  2367. } /* End of base register loop */
  2368. if (cpqhp_legacy_mode) {
  2369. /* Figure out which interrupt pin this function uses */
  2370. rc = pci_bus_read_config_byte (pci_bus, devfn,
  2371. PCI_INTERRUPT_PIN, &temp_byte);
  2372. /* If this function needs an interrupt and we are behind
  2373. * a bridge and the pin is tied to something that's
  2374. * alread mapped, set this one the same */
  2375. if (temp_byte && resources->irqs &&
  2376. (resources->irqs->valid_INT &
  2377. (0x01 << ((temp_byte + resources->irqs->barber_pole - 1) & 0x03)))) {
  2378. /* We have to share with something already set up */
  2379. IRQ = resources->irqs->interrupt[(temp_byte +
  2380. resources->irqs->barber_pole - 1) & 0x03];
  2381. } else {
  2382. /* Program IRQ based on card type */
  2383. rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
  2384. if (class_code == PCI_BASE_CLASS_STORAGE) {
  2385. IRQ = cpqhp_disk_irq;
  2386. } else {
  2387. IRQ = cpqhp_nic_irq;
  2388. }
  2389. }
  2390. /* IRQ Line */
  2391. rc = pci_bus_write_config_byte (pci_bus, devfn, PCI_INTERRUPT_LINE, IRQ);
  2392. }
  2393. if (!behind_bridge) {
  2394. rc = cpqhp_set_irq(func->bus, func->device, temp_byte + 0x09, IRQ);
  2395. if (rc)
  2396. return 1;
  2397. } else {
  2398. /* TBD - this code may also belong in the other clause
  2399. * of this If statement */
  2400. resources->irqs->interrupt[(temp_byte + resources->irqs->barber_pole - 1) & 0x03] = IRQ;
  2401. resources->irqs->valid_INT |= 0x01 << (temp_byte + resources->irqs->barber_pole - 1) & 0x03;
  2402. }
  2403. /* Latency Timer */
  2404. temp_byte = 0x40;
  2405. rc = pci_bus_write_config_byte(pci_bus, devfn,
  2406. PCI_LATENCY_TIMER, temp_byte);
  2407. /* Cache Line size */
  2408. temp_byte = 0x08;
  2409. rc = pci_bus_write_config_byte(pci_bus, devfn,
  2410. PCI_CACHE_LINE_SIZE, temp_byte);
  2411. /* disable ROM base Address */
  2412. temp_dword = 0x00L;
  2413. rc = pci_bus_write_config_word(pci_bus, devfn,
  2414. PCI_ROM_ADDRESS, temp_dword);
  2415. /* enable card */
  2416. temp_word = 0x0157; /* = PCI_COMMAND_IO |
  2417. * PCI_COMMAND_MEMORY |
  2418. * PCI_COMMAND_MASTER |
  2419. * PCI_COMMAND_INVALIDATE |
  2420. * PCI_COMMAND_PARITY |
  2421. * PCI_COMMAND_SERR */
  2422. rc = pci_bus_write_config_word (pci_bus, devfn,
  2423. PCI_COMMAND, temp_word);
  2424. } else { /* End of Not-A-Bridge else */
  2425. /* It's some strange type of PCI adapter (Cardbus?) */
  2426. return DEVICE_TYPE_NOT_SUPPORTED;
  2427. }
  2428. func->configured = 1;
  2429. return 0;
  2430. free_and_out:
  2431. cpqhp_destroy_resource_list (&temp_resources);
  2432. return_resource(&(resources-> bus_head), hold_bus_node);
  2433. return_resource(&(resources-> io_head), hold_IO_node);
  2434. return_resource(&(resources-> mem_head), hold_mem_node);
  2435. return_resource(&(resources-> p_mem_head), hold_p_mem_node);
  2436. return rc;
  2437. }