cpqphp_ctrl.c 78 KB

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