cpqphp_ctrl.c 74 KB

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