cpqphp_ctrl.c 77 KB

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