cpqphp_ctrl.c 75 KB

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