cpqphp_ctrl.c 73 KB

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