cpqphp_ctrl.c 77 KB

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