sb_edac.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. /* Intel Sandy Bridge -EN/-EP/-EX Memory Controller kernel module
  2. *
  3. * This driver supports the memory controllers found on the Intel
  4. * processor family Sandy Bridge.
  5. *
  6. * This file may be distributed under the terms of the
  7. * GNU General Public License version 2 only.
  8. *
  9. * Copyright (c) 2011 by:
  10. * Mauro Carvalho Chehab <mchehab@redhat.com>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_ids.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/edac.h>
  19. #include <linux/mmzone.h>
  20. #include <linux/smp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/math64.h>
  23. #include <asm/processor.h>
  24. #include <asm/mce.h>
  25. #include "edac_core.h"
  26. /* Static vars */
  27. static LIST_HEAD(sbridge_edac_list);
  28. static DEFINE_MUTEX(sbridge_edac_lock);
  29. static int probed;
  30. /*
  31. * Alter this version for the module when modifications are made
  32. */
  33. #define SBRIDGE_REVISION " Ver: 1.0.0 "
  34. #define EDAC_MOD_STR "sbridge_edac"
  35. /*
  36. * Debug macros
  37. */
  38. #define sbridge_printk(level, fmt, arg...) \
  39. edac_printk(level, "sbridge", fmt, ##arg)
  40. #define sbridge_mc_printk(mci, level, fmt, arg...) \
  41. edac_mc_chipset_printk(mci, level, "sbridge", fmt, ##arg)
  42. /*
  43. * Get a bit field at register value <v>, from bit <lo> to bit <hi>
  44. */
  45. #define GET_BITFIELD(v, lo, hi) \
  46. (((v) & ((1ULL << ((hi) - (lo) + 1)) - 1) << (lo)) >> (lo))
  47. /*
  48. * sbridge Memory Controller Registers
  49. */
  50. /*
  51. * FIXME: For now, let's order by device function, as it makes
  52. * easier for driver's development process. This table should be
  53. * moved to pci_id.h when submitted upstream
  54. */
  55. #define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0 0x3cf4 /* 12.6 */
  56. #define PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1 0x3cf6 /* 12.7 */
  57. #define PCI_DEVICE_ID_INTEL_SBRIDGE_BR 0x3cf5 /* 13.6 */
  58. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0 0x3ca0 /* 14.0 */
  59. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA 0x3ca8 /* 15.0 */
  60. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS 0x3c71 /* 15.1 */
  61. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0 0x3caa /* 15.2 */
  62. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1 0x3cab /* 15.3 */
  63. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2 0x3cac /* 15.4 */
  64. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3 0x3cad /* 15.5 */
  65. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO 0x3cb8 /* 17.0 */
  66. /*
  67. * Currently, unused, but will be needed in the future
  68. * implementations, as they hold the error counters
  69. */
  70. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR0 0x3c72 /* 16.2 */
  71. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR1 0x3c73 /* 16.3 */
  72. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR2 0x3c76 /* 16.6 */
  73. #define PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_ERR3 0x3c77 /* 16.7 */
  74. /* Devices 12 Function 6, Offsets 0x80 to 0xcc */
  75. static const u32 sbridge_dram_rule[] = {
  76. 0x80, 0x88, 0x90, 0x98, 0xa0,
  77. 0xa8, 0xb0, 0xb8, 0xc0, 0xc8,
  78. };
  79. #define SAD_LIMIT(reg) ((GET_BITFIELD(reg, 6, 25) << 26) | 0x3ffffff)
  80. #define DRAM_ATTR(reg) GET_BITFIELD(reg, 2, 3)
  81. #define INTERLEAVE_MODE(reg) GET_BITFIELD(reg, 1, 1)
  82. #define DRAM_RULE_ENABLE(reg) GET_BITFIELD(reg, 0, 0)
  83. static char *get_dram_attr(u32 reg)
  84. {
  85. switch(DRAM_ATTR(reg)) {
  86. case 0:
  87. return "DRAM";
  88. case 1:
  89. return "MMCFG";
  90. case 2:
  91. return "NXM";
  92. default:
  93. return "unknown";
  94. }
  95. }
  96. static const u32 sbridge_interleave_list[] = {
  97. 0x84, 0x8c, 0x94, 0x9c, 0xa4,
  98. 0xac, 0xb4, 0xbc, 0xc4, 0xcc,
  99. };
  100. #define SAD_PKG0(reg) GET_BITFIELD(reg, 0, 2)
  101. #define SAD_PKG1(reg) GET_BITFIELD(reg, 3, 5)
  102. #define SAD_PKG2(reg) GET_BITFIELD(reg, 8, 10)
  103. #define SAD_PKG3(reg) GET_BITFIELD(reg, 11, 13)
  104. #define SAD_PKG4(reg) GET_BITFIELD(reg, 16, 18)
  105. #define SAD_PKG5(reg) GET_BITFIELD(reg, 19, 21)
  106. #define SAD_PKG6(reg) GET_BITFIELD(reg, 24, 26)
  107. #define SAD_PKG7(reg) GET_BITFIELD(reg, 27, 29)
  108. static inline int sad_pkg(u32 reg, int interleave)
  109. {
  110. switch (interleave) {
  111. case 0:
  112. return SAD_PKG0(reg);
  113. case 1:
  114. return SAD_PKG1(reg);
  115. case 2:
  116. return SAD_PKG2(reg);
  117. case 3:
  118. return SAD_PKG3(reg);
  119. case 4:
  120. return SAD_PKG4(reg);
  121. case 5:
  122. return SAD_PKG5(reg);
  123. case 6:
  124. return SAD_PKG6(reg);
  125. case 7:
  126. return SAD_PKG7(reg);
  127. default:
  128. return -EINVAL;
  129. }
  130. }
  131. /* Devices 12 Function 7 */
  132. #define TOLM 0x80
  133. #define TOHM 0x84
  134. #define GET_TOLM(reg) ((GET_BITFIELD(reg, 0, 3) << 28) | 0x3ffffff)
  135. #define GET_TOHM(reg) ((GET_BITFIELD(reg, 0, 20) << 25) | 0x3ffffff)
  136. /* Device 13 Function 6 */
  137. #define SAD_TARGET 0xf0
  138. #define SOURCE_ID(reg) GET_BITFIELD(reg, 9, 11)
  139. #define SAD_CONTROL 0xf4
  140. #define NODE_ID(reg) GET_BITFIELD(reg, 0, 2)
  141. /* Device 14 function 0 */
  142. static const u32 tad_dram_rule[] = {
  143. 0x40, 0x44, 0x48, 0x4c,
  144. 0x50, 0x54, 0x58, 0x5c,
  145. 0x60, 0x64, 0x68, 0x6c,
  146. };
  147. #define MAX_TAD ARRAY_SIZE(tad_dram_rule)
  148. #define TAD_LIMIT(reg) ((GET_BITFIELD(reg, 12, 31) << 26) | 0x3ffffff)
  149. #define TAD_SOCK(reg) GET_BITFIELD(reg, 10, 11)
  150. #define TAD_CH(reg) GET_BITFIELD(reg, 8, 9)
  151. #define TAD_TGT3(reg) GET_BITFIELD(reg, 6, 7)
  152. #define TAD_TGT2(reg) GET_BITFIELD(reg, 4, 5)
  153. #define TAD_TGT1(reg) GET_BITFIELD(reg, 2, 3)
  154. #define TAD_TGT0(reg) GET_BITFIELD(reg, 0, 1)
  155. /* Device 15, function 0 */
  156. #define MCMTR 0x7c
  157. #define IS_ECC_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 2, 2)
  158. #define IS_LOCKSTEP_ENABLED(mcmtr) GET_BITFIELD(mcmtr, 1, 1)
  159. #define IS_CLOSE_PG(mcmtr) GET_BITFIELD(mcmtr, 0, 0)
  160. /* Device 15, function 1 */
  161. #define RASENABLES 0xac
  162. #define IS_MIRROR_ENABLED(reg) GET_BITFIELD(reg, 0, 0)
  163. /* Device 15, functions 2-5 */
  164. static const int mtr_regs[] = {
  165. 0x80, 0x84, 0x88,
  166. };
  167. #define RANK_DISABLE(mtr) GET_BITFIELD(mtr, 16, 19)
  168. #define IS_DIMM_PRESENT(mtr) GET_BITFIELD(mtr, 14, 14)
  169. #define RANK_CNT_BITS(mtr) GET_BITFIELD(mtr, 12, 13)
  170. #define RANK_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 2, 4)
  171. #define COL_WIDTH_BITS(mtr) GET_BITFIELD(mtr, 0, 1)
  172. static const u32 tad_ch_nilv_offset[] = {
  173. 0x90, 0x94, 0x98, 0x9c,
  174. 0xa0, 0xa4, 0xa8, 0xac,
  175. 0xb0, 0xb4, 0xb8, 0xbc,
  176. };
  177. #define CHN_IDX_OFFSET(reg) GET_BITFIELD(reg, 28, 29)
  178. #define TAD_OFFSET(reg) (GET_BITFIELD(reg, 6, 25) << 26)
  179. static const u32 rir_way_limit[] = {
  180. 0x108, 0x10c, 0x110, 0x114, 0x118,
  181. };
  182. #define MAX_RIR_RANGES ARRAY_SIZE(rir_way_limit)
  183. #define IS_RIR_VALID(reg) GET_BITFIELD(reg, 31, 31)
  184. #define RIR_WAY(reg) GET_BITFIELD(reg, 28, 29)
  185. #define RIR_LIMIT(reg) ((GET_BITFIELD(reg, 1, 10) << 29)| 0x1fffffff)
  186. #define MAX_RIR_WAY 8
  187. static const u32 rir_offset[MAX_RIR_RANGES][MAX_RIR_WAY] = {
  188. { 0x120, 0x124, 0x128, 0x12c, 0x130, 0x134, 0x138, 0x13c },
  189. { 0x140, 0x144, 0x148, 0x14c, 0x150, 0x154, 0x158, 0x15c },
  190. { 0x160, 0x164, 0x168, 0x16c, 0x170, 0x174, 0x178, 0x17c },
  191. { 0x180, 0x184, 0x188, 0x18c, 0x190, 0x194, 0x198, 0x19c },
  192. { 0x1a0, 0x1a4, 0x1a8, 0x1ac, 0x1b0, 0x1b4, 0x1b8, 0x1bc },
  193. };
  194. #define RIR_RNK_TGT(reg) GET_BITFIELD(reg, 16, 19)
  195. #define RIR_OFFSET(reg) GET_BITFIELD(reg, 2, 14)
  196. /* Device 16, functions 2-7 */
  197. /*
  198. * FIXME: Implement the error count reads directly
  199. */
  200. static const u32 correrrcnt[] = {
  201. 0x104, 0x108, 0x10c, 0x110,
  202. };
  203. #define RANK_ODD_OV(reg) GET_BITFIELD(reg, 31, 31)
  204. #define RANK_ODD_ERR_CNT(reg) GET_BITFIELD(reg, 16, 30)
  205. #define RANK_EVEN_OV(reg) GET_BITFIELD(reg, 15, 15)
  206. #define RANK_EVEN_ERR_CNT(reg) GET_BITFIELD(reg, 0, 14)
  207. static const u32 correrrthrsld[] = {
  208. 0x11c, 0x120, 0x124, 0x128,
  209. };
  210. #define RANK_ODD_ERR_THRSLD(reg) GET_BITFIELD(reg, 16, 30)
  211. #define RANK_EVEN_ERR_THRSLD(reg) GET_BITFIELD(reg, 0, 14)
  212. /* Device 17, function 0 */
  213. #define SB_RANK_CFG_A 0x0328
  214. #define IS_RDIMM_ENABLED(reg) GET_BITFIELD(reg, 11, 11)
  215. /*
  216. * sbridge structs
  217. */
  218. #define NUM_CHANNELS 4
  219. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  220. struct sbridge_pvt;
  221. struct sbridge_info {
  222. u32 mcmtr;
  223. u32 rankcfgr;
  224. u64 (*get_tolm)(struct sbridge_pvt *pvt);
  225. u64 (*get_tohm)(struct sbridge_pvt *pvt);
  226. const u32 *dram_rule;
  227. const u32 *interleave_list;
  228. u8 max_sad;
  229. u8 max_interleave;
  230. };
  231. struct sbridge_channel {
  232. u32 ranks;
  233. u32 dimms;
  234. };
  235. struct pci_id_descr {
  236. int dev;
  237. int func;
  238. int dev_id;
  239. int optional;
  240. };
  241. struct pci_id_table {
  242. const struct pci_id_descr *descr;
  243. int n_devs;
  244. };
  245. struct sbridge_dev {
  246. struct list_head list;
  247. u8 bus, mc;
  248. u8 node_id, source_id;
  249. struct pci_dev **pdev;
  250. int n_devs;
  251. struct mem_ctl_info *mci;
  252. };
  253. struct sbridge_pvt {
  254. struct pci_dev *pci_ta, *pci_ddrio, *pci_ras;
  255. struct pci_dev *pci_sad0, *pci_sad1, *pci_ha0;
  256. struct pci_dev *pci_br0;
  257. struct pci_dev *pci_tad[NUM_CHANNELS];
  258. struct sbridge_dev *sbridge_dev;
  259. struct sbridge_info info;
  260. struct sbridge_channel channel[NUM_CHANNELS];
  261. /* Memory type detection */
  262. bool is_mirrored, is_lockstep, is_close_pg;
  263. /* Fifo double buffers */
  264. struct mce mce_entry[MCE_LOG_LEN];
  265. struct mce mce_outentry[MCE_LOG_LEN];
  266. /* Fifo in/out counters */
  267. unsigned mce_in, mce_out;
  268. /* Count indicator to show errors not got */
  269. unsigned mce_overrun;
  270. /* Memory description */
  271. u64 tolm, tohm;
  272. };
  273. #define PCI_DESCR(device, function, device_id, opt) \
  274. .dev = (device), \
  275. .func = (function), \
  276. .dev_id = (device_id), \
  277. .optional = opt
  278. static const struct pci_id_descr pci_dev_descr_sbridge[] = {
  279. /* Processor Home Agent */
  280. { PCI_DESCR(14, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_HA0, 0) },
  281. /* Memory controller */
  282. { PCI_DESCR(15, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA, 0) },
  283. { PCI_DESCR(15, 1, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_RAS, 0) },
  284. { PCI_DESCR(15, 2, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD0, 0) },
  285. { PCI_DESCR(15, 3, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD1, 0) },
  286. { PCI_DESCR(15, 4, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD2, 0) },
  287. { PCI_DESCR(15, 5, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TAD3, 0) },
  288. { PCI_DESCR(17, 0, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_DDRIO, 1) },
  289. /* System Address Decoder */
  290. { PCI_DESCR(12, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD0, 0) },
  291. { PCI_DESCR(12, 7, PCI_DEVICE_ID_INTEL_SBRIDGE_SAD1, 0) },
  292. /* Broadcast Registers */
  293. { PCI_DESCR(13, 6, PCI_DEVICE_ID_INTEL_SBRIDGE_BR, 0) },
  294. };
  295. #define PCI_ID_TABLE_ENTRY(A) { .descr=A, .n_devs = ARRAY_SIZE(A) }
  296. static const struct pci_id_table pci_dev_descr_sbridge_table[] = {
  297. PCI_ID_TABLE_ENTRY(pci_dev_descr_sbridge),
  298. {0,} /* 0 terminated list. */
  299. };
  300. /*
  301. * pci_device_id table for which devices we are looking for
  302. */
  303. static DEFINE_PCI_DEVICE_TABLE(sbridge_pci_tbl) = {
  304. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SBRIDGE_IMC_TA)},
  305. {0,} /* 0 terminated list. */
  306. };
  307. /****************************************************************************
  308. Ancillary status routines
  309. ****************************************************************************/
  310. static inline int numrank(u32 mtr)
  311. {
  312. int ranks = (1 << RANK_CNT_BITS(mtr));
  313. if (ranks > 4) {
  314. edac_dbg(0, "Invalid number of ranks: %d (max = 4) raw value = %x (%04x)\n",
  315. ranks, (unsigned int)RANK_CNT_BITS(mtr), mtr);
  316. return -EINVAL;
  317. }
  318. return ranks;
  319. }
  320. static inline int numrow(u32 mtr)
  321. {
  322. int rows = (RANK_WIDTH_BITS(mtr) + 12);
  323. if (rows < 13 || rows > 18) {
  324. edac_dbg(0, "Invalid number of rows: %d (should be between 14 and 17) raw value = %x (%04x)\n",
  325. rows, (unsigned int)RANK_WIDTH_BITS(mtr), mtr);
  326. return -EINVAL;
  327. }
  328. return 1 << rows;
  329. }
  330. static inline int numcol(u32 mtr)
  331. {
  332. int cols = (COL_WIDTH_BITS(mtr) + 10);
  333. if (cols > 12) {
  334. edac_dbg(0, "Invalid number of cols: %d (max = 4) raw value = %x (%04x)\n",
  335. cols, (unsigned int)COL_WIDTH_BITS(mtr), mtr);
  336. return -EINVAL;
  337. }
  338. return 1 << cols;
  339. }
  340. static struct sbridge_dev *get_sbridge_dev(u8 bus)
  341. {
  342. struct sbridge_dev *sbridge_dev;
  343. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  344. if (sbridge_dev->bus == bus)
  345. return sbridge_dev;
  346. }
  347. return NULL;
  348. }
  349. static struct sbridge_dev *alloc_sbridge_dev(u8 bus,
  350. const struct pci_id_table *table)
  351. {
  352. struct sbridge_dev *sbridge_dev;
  353. sbridge_dev = kzalloc(sizeof(*sbridge_dev), GFP_KERNEL);
  354. if (!sbridge_dev)
  355. return NULL;
  356. sbridge_dev->pdev = kzalloc(sizeof(*sbridge_dev->pdev) * table->n_devs,
  357. GFP_KERNEL);
  358. if (!sbridge_dev->pdev) {
  359. kfree(sbridge_dev);
  360. return NULL;
  361. }
  362. sbridge_dev->bus = bus;
  363. sbridge_dev->n_devs = table->n_devs;
  364. list_add_tail(&sbridge_dev->list, &sbridge_edac_list);
  365. return sbridge_dev;
  366. }
  367. static void free_sbridge_dev(struct sbridge_dev *sbridge_dev)
  368. {
  369. list_del(&sbridge_dev->list);
  370. kfree(sbridge_dev->pdev);
  371. kfree(sbridge_dev);
  372. }
  373. static u64 sbridge_get_tolm(struct sbridge_pvt *pvt)
  374. {
  375. u32 reg;
  376. /* Address range is 32:28 */
  377. pci_read_config_dword(pvt->pci_sad1, TOLM, &reg);
  378. return GET_TOLM(reg);
  379. }
  380. static u64 sbridge_get_tohm(struct sbridge_pvt *pvt)
  381. {
  382. u32 reg;
  383. pci_read_config_dword(pvt->pci_sad1, TOHM, &reg);
  384. return GET_TOHM(reg);
  385. }
  386. /****************************************************************************
  387. Memory check routines
  388. ****************************************************************************/
  389. static struct pci_dev *get_pdev_slot_func(u8 bus, unsigned slot,
  390. unsigned func)
  391. {
  392. struct sbridge_dev *sbridge_dev = get_sbridge_dev(bus);
  393. int i;
  394. if (!sbridge_dev)
  395. return NULL;
  396. for (i = 0; i < sbridge_dev->n_devs; i++) {
  397. if (!sbridge_dev->pdev[i])
  398. continue;
  399. if (PCI_SLOT(sbridge_dev->pdev[i]->devfn) == slot &&
  400. PCI_FUNC(sbridge_dev->pdev[i]->devfn) == func) {
  401. edac_dbg(1, "Associated %02x.%02x.%d with %p\n",
  402. bus, slot, func, sbridge_dev->pdev[i]);
  403. return sbridge_dev->pdev[i];
  404. }
  405. }
  406. return NULL;
  407. }
  408. /**
  409. * check_if_ecc_is_active() - Checks if ECC is active
  410. * bus: Device bus
  411. */
  412. static int check_if_ecc_is_active(const u8 bus)
  413. {
  414. struct pci_dev *pdev = NULL;
  415. u32 mcmtr;
  416. pdev = get_pdev_slot_func(bus, 15, 0);
  417. if (!pdev) {
  418. sbridge_printk(KERN_ERR, "Couldn't find PCI device "
  419. "%2x.%02d.%d!!!\n",
  420. bus, 15, 0);
  421. return -ENODEV;
  422. }
  423. pci_read_config_dword(pdev, MCMTR, &mcmtr);
  424. if (!IS_ECC_ENABLED(mcmtr)) {
  425. sbridge_printk(KERN_ERR, "ECC is disabled. Aborting\n");
  426. return -ENODEV;
  427. }
  428. return 0;
  429. }
  430. static int get_dimm_config(struct mem_ctl_info *mci)
  431. {
  432. struct sbridge_pvt *pvt = mci->pvt_info;
  433. struct dimm_info *dimm;
  434. unsigned i, j, banks, ranks, rows, cols, npages;
  435. u64 size;
  436. u32 reg;
  437. enum edac_type mode;
  438. enum mem_type mtype;
  439. pvt->info.rankcfgr = SB_RANK_CFG_A;
  440. pci_read_config_dword(pvt->pci_br0, SAD_TARGET, &reg);
  441. pvt->sbridge_dev->source_id = SOURCE_ID(reg);
  442. pci_read_config_dword(pvt->pci_br0, SAD_CONTROL, &reg);
  443. pvt->sbridge_dev->node_id = NODE_ID(reg);
  444. edac_dbg(0, "mc#%d: Node ID: %d, source ID: %d\n",
  445. pvt->sbridge_dev->mc,
  446. pvt->sbridge_dev->node_id,
  447. pvt->sbridge_dev->source_id);
  448. pci_read_config_dword(pvt->pci_ras, RASENABLES, &reg);
  449. if (IS_MIRROR_ENABLED(reg)) {
  450. edac_dbg(0, "Memory mirror is enabled\n");
  451. pvt->is_mirrored = true;
  452. } else {
  453. edac_dbg(0, "Memory mirror is disabled\n");
  454. pvt->is_mirrored = false;
  455. }
  456. pci_read_config_dword(pvt->pci_ta, MCMTR, &pvt->info.mcmtr);
  457. if (IS_LOCKSTEP_ENABLED(pvt->info.mcmtr)) {
  458. edac_dbg(0, "Lockstep is enabled\n");
  459. mode = EDAC_S8ECD8ED;
  460. pvt->is_lockstep = true;
  461. } else {
  462. edac_dbg(0, "Lockstep is disabled\n");
  463. mode = EDAC_S4ECD4ED;
  464. pvt->is_lockstep = false;
  465. }
  466. if (IS_CLOSE_PG(pvt->info.mcmtr)) {
  467. edac_dbg(0, "address map is on closed page mode\n");
  468. pvt->is_close_pg = true;
  469. } else {
  470. edac_dbg(0, "address map is on open page mode\n");
  471. pvt->is_close_pg = false;
  472. }
  473. if (pvt->pci_ddrio) {
  474. pci_read_config_dword(pvt->pci_ddrio, pvt->info.rankcfgr,
  475. &reg);
  476. if (IS_RDIMM_ENABLED(reg)) {
  477. /* FIXME: Can also be LRDIMM */
  478. edac_dbg(0, "Memory is registered\n");
  479. mtype = MEM_RDDR3;
  480. } else {
  481. edac_dbg(0, "Memory is unregistered\n");
  482. mtype = MEM_DDR3;
  483. }
  484. } else {
  485. edac_dbg(0, "Cannot determine memory type\n");
  486. mtype = MEM_UNKNOWN;
  487. }
  488. /* On all supported DDR3 DIMM types, there are 8 banks available */
  489. banks = 8;
  490. for (i = 0; i < NUM_CHANNELS; i++) {
  491. u32 mtr;
  492. for (j = 0; j < ARRAY_SIZE(mtr_regs); j++) {
  493. dimm = EDAC_DIMM_PTR(mci->layers, mci->dimms, mci->n_layers,
  494. i, j, 0);
  495. pci_read_config_dword(pvt->pci_tad[i],
  496. mtr_regs[j], &mtr);
  497. edac_dbg(4, "Channel #%d MTR%d = %x\n", i, j, mtr);
  498. if (IS_DIMM_PRESENT(mtr)) {
  499. pvt->channel[i].dimms++;
  500. ranks = numrank(mtr);
  501. rows = numrow(mtr);
  502. cols = numcol(mtr);
  503. /* DDR3 has 8 I/O banks */
  504. size = ((u64)rows * cols * banks * ranks) >> (20 - 3);
  505. npages = MiB_TO_PAGES(size);
  506. edac_dbg(0, "mc#%d: channel %d, dimm %d, %Ld Mb (%d pages) bank: %d, rank: %d, row: %#x, col: %#x\n",
  507. pvt->sbridge_dev->mc, i, j,
  508. size, npages,
  509. banks, ranks, rows, cols);
  510. dimm->nr_pages = npages;
  511. dimm->grain = 32;
  512. dimm->dtype = (banks == 8) ? DEV_X8 : DEV_X4;
  513. dimm->mtype = mtype;
  514. dimm->edac_mode = mode;
  515. snprintf(dimm->label, sizeof(dimm->label),
  516. "CPU_SrcID#%u_Channel#%u_DIMM#%u",
  517. pvt->sbridge_dev->source_id, i, j);
  518. }
  519. }
  520. }
  521. return 0;
  522. }
  523. static void get_memory_layout(const struct mem_ctl_info *mci)
  524. {
  525. struct sbridge_pvt *pvt = mci->pvt_info;
  526. int i, j, k, n_sads, n_tads, sad_interl;
  527. u32 reg;
  528. u64 limit, prv = 0;
  529. u64 tmp_mb;
  530. u32 mb, kb;
  531. u32 rir_way;
  532. /*
  533. * Step 1) Get TOLM/TOHM ranges
  534. */
  535. pvt->tolm = pvt->info.get_tolm(pvt);
  536. tmp_mb = (1 + pvt->tolm) >> 20;
  537. mb = div_u64_rem(tmp_mb, 1000, &kb);
  538. edac_dbg(0, "TOLM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tolm);
  539. /* Address range is already 45:25 */
  540. pvt->tohm = pvt->info.get_tohm(pvt);
  541. tmp_mb = (1 + pvt->tohm) >> 20;
  542. mb = div_u64_rem(tmp_mb, 1000, &kb);
  543. edac_dbg(0, "TOHM: %u.%03u GB (0x%016Lx)\n", mb, kb, (u64)pvt->tohm);
  544. /*
  545. * Step 2) Get SAD range and SAD Interleave list
  546. * TAD registers contain the interleave wayness. However, it
  547. * seems simpler to just discover it indirectly, with the
  548. * algorithm bellow.
  549. */
  550. prv = 0;
  551. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  552. /* SAD_LIMIT Address range is 45:26 */
  553. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  554. &reg);
  555. limit = SAD_LIMIT(reg);
  556. if (!DRAM_RULE_ENABLE(reg))
  557. continue;
  558. if (limit <= prv)
  559. break;
  560. tmp_mb = (limit + 1) >> 20;
  561. mb = div_u64_rem(tmp_mb, 1000, &kb);
  562. edac_dbg(0, "SAD#%d %s up to %u.%03u GB (0x%016Lx) Interleave: %s reg=0x%08x\n",
  563. n_sads,
  564. get_dram_attr(reg),
  565. mb, kb,
  566. ((u64)tmp_mb) << 20L,
  567. INTERLEAVE_MODE(reg) ? "8:6" : "[8:6]XOR[18:16]",
  568. reg);
  569. prv = limit;
  570. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  571. &reg);
  572. sad_interl = sad_pkg(reg, 0);
  573. for (j = 0; j < 8; j++) {
  574. if (j > 0 && sad_interl == sad_pkg(reg, j))
  575. break;
  576. edac_dbg(0, "SAD#%d, interleave #%d: %d\n",
  577. n_sads, j, sad_pkg(reg, j));
  578. }
  579. }
  580. /*
  581. * Step 3) Get TAD range
  582. */
  583. prv = 0;
  584. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  585. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  586. &reg);
  587. limit = TAD_LIMIT(reg);
  588. if (limit <= prv)
  589. break;
  590. tmp_mb = (limit + 1) >> 20;
  591. mb = div_u64_rem(tmp_mb, 1000, &kb);
  592. edac_dbg(0, "TAD#%d: up to %u.%03u GB (0x%016Lx), socket interleave %d, memory interleave %d, TGT: %d, %d, %d, %d, reg=0x%08x\n",
  593. n_tads, mb, kb,
  594. ((u64)tmp_mb) << 20L,
  595. (u32)TAD_SOCK(reg),
  596. (u32)TAD_CH(reg),
  597. (u32)TAD_TGT0(reg),
  598. (u32)TAD_TGT1(reg),
  599. (u32)TAD_TGT2(reg),
  600. (u32)TAD_TGT3(reg),
  601. reg);
  602. prv = limit;
  603. }
  604. /*
  605. * Step 4) Get TAD offsets, per each channel
  606. */
  607. for (i = 0; i < NUM_CHANNELS; i++) {
  608. if (!pvt->channel[i].dimms)
  609. continue;
  610. for (j = 0; j < n_tads; j++) {
  611. pci_read_config_dword(pvt->pci_tad[i],
  612. tad_ch_nilv_offset[j],
  613. &reg);
  614. tmp_mb = TAD_OFFSET(reg) >> 20;
  615. mb = div_u64_rem(tmp_mb, 1000, &kb);
  616. edac_dbg(0, "TAD CH#%d, offset #%d: %u.%03u GB (0x%016Lx), reg=0x%08x\n",
  617. i, j,
  618. mb, kb,
  619. ((u64)tmp_mb) << 20L,
  620. reg);
  621. }
  622. }
  623. /*
  624. * Step 6) Get RIR Wayness/Limit, per each channel
  625. */
  626. for (i = 0; i < NUM_CHANNELS; i++) {
  627. if (!pvt->channel[i].dimms)
  628. continue;
  629. for (j = 0; j < MAX_RIR_RANGES; j++) {
  630. pci_read_config_dword(pvt->pci_tad[i],
  631. rir_way_limit[j],
  632. &reg);
  633. if (!IS_RIR_VALID(reg))
  634. continue;
  635. tmp_mb = RIR_LIMIT(reg) >> 20;
  636. rir_way = 1 << RIR_WAY(reg);
  637. mb = div_u64_rem(tmp_mb, 1000, &kb);
  638. edac_dbg(0, "CH#%d RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d, reg=0x%08x\n",
  639. i, j,
  640. mb, kb,
  641. ((u64)tmp_mb) << 20L,
  642. rir_way,
  643. reg);
  644. for (k = 0; k < rir_way; k++) {
  645. pci_read_config_dword(pvt->pci_tad[i],
  646. rir_offset[j][k],
  647. &reg);
  648. tmp_mb = RIR_OFFSET(reg) << 6;
  649. mb = div_u64_rem(tmp_mb, 1000, &kb);
  650. edac_dbg(0, "CH#%d RIR#%d INTL#%d, offset %u.%03u GB (0x%016Lx), tgt: %d, reg=0x%08x\n",
  651. i, j, k,
  652. mb, kb,
  653. ((u64)tmp_mb) << 20L,
  654. (u32)RIR_RNK_TGT(reg),
  655. reg);
  656. }
  657. }
  658. }
  659. }
  660. struct mem_ctl_info *get_mci_for_node_id(u8 node_id)
  661. {
  662. struct sbridge_dev *sbridge_dev;
  663. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  664. if (sbridge_dev->node_id == node_id)
  665. return sbridge_dev->mci;
  666. }
  667. return NULL;
  668. }
  669. static int get_memory_error_data(struct mem_ctl_info *mci,
  670. u64 addr,
  671. u8 *socket,
  672. long *channel_mask,
  673. u8 *rank,
  674. char **area_type, char *msg)
  675. {
  676. struct mem_ctl_info *new_mci;
  677. struct sbridge_pvt *pvt = mci->pvt_info;
  678. int n_rir, n_sads, n_tads, sad_way, sck_xch;
  679. int sad_interl, idx, base_ch;
  680. int interleave_mode;
  681. unsigned sad_interleave[pvt->info.max_interleave];
  682. u32 reg;
  683. u8 ch_way,sck_way;
  684. u32 tad_offset;
  685. u32 rir_way;
  686. u32 mb, kb;
  687. u64 ch_addr, offset, limit, prv = 0;
  688. /*
  689. * Step 0) Check if the address is at special memory ranges
  690. * The check bellow is probably enough to fill all cases where
  691. * the error is not inside a memory, except for the legacy
  692. * range (e. g. VGA addresses). It is unlikely, however, that the
  693. * memory controller would generate an error on that range.
  694. */
  695. if ((addr > (u64) pvt->tolm) && (addr < (1LL << 32))) {
  696. sprintf(msg, "Error at TOLM area, on addr 0x%08Lx", addr);
  697. return -EINVAL;
  698. }
  699. if (addr >= (u64)pvt->tohm) {
  700. sprintf(msg, "Error at MMIOH area, on addr 0x%016Lx", addr);
  701. return -EINVAL;
  702. }
  703. /*
  704. * Step 1) Get socket
  705. */
  706. for (n_sads = 0; n_sads < pvt->info.max_sad; n_sads++) {
  707. pci_read_config_dword(pvt->pci_sad0, pvt->info.dram_rule[n_sads],
  708. &reg);
  709. if (!DRAM_RULE_ENABLE(reg))
  710. continue;
  711. limit = SAD_LIMIT(reg);
  712. if (limit <= prv) {
  713. sprintf(msg, "Can't discover the memory socket");
  714. return -EINVAL;
  715. }
  716. if (addr <= limit)
  717. break;
  718. prv = limit;
  719. }
  720. if (n_sads == pvt->info.max_sad) {
  721. sprintf(msg, "Can't discover the memory socket");
  722. return -EINVAL;
  723. }
  724. *area_type = get_dram_attr(reg);
  725. interleave_mode = INTERLEAVE_MODE(reg);
  726. pci_read_config_dword(pvt->pci_sad0, pvt->info.interleave_list[n_sads],
  727. &reg);
  728. sad_interl = sad_pkg(reg, 0);
  729. for (sad_way = 0; sad_way < 8; sad_way++) {
  730. if (sad_way > 0 && sad_interl == sad_pkg(reg, sad_way))
  731. break;
  732. sad_interleave[sad_way] = sad_pkg(reg, sad_way);
  733. edac_dbg(0, "SAD interleave #%d: %d\n",
  734. sad_way, sad_interleave[sad_way]);
  735. }
  736. edac_dbg(0, "mc#%d: Error detected on SAD#%d: address 0x%016Lx < 0x%016Lx, Interleave [%d:6]%s\n",
  737. pvt->sbridge_dev->mc,
  738. n_sads,
  739. addr,
  740. limit,
  741. sad_way + 7,
  742. interleave_mode ? "" : "XOR[18:16]");
  743. if (interleave_mode)
  744. idx = ((addr >> 6) ^ (addr >> 16)) & 7;
  745. else
  746. idx = (addr >> 6) & 7;
  747. switch (sad_way) {
  748. case 1:
  749. idx = 0;
  750. break;
  751. case 2:
  752. idx = idx & 1;
  753. break;
  754. case 4:
  755. idx = idx & 3;
  756. break;
  757. case 8:
  758. break;
  759. default:
  760. sprintf(msg, "Can't discover socket interleave");
  761. return -EINVAL;
  762. }
  763. *socket = sad_interleave[idx];
  764. edac_dbg(0, "SAD interleave index: %d (wayness %d) = CPU socket %d\n",
  765. idx, sad_way, *socket);
  766. /*
  767. * Move to the proper node structure, in order to access the
  768. * right PCI registers
  769. */
  770. new_mci = get_mci_for_node_id(*socket);
  771. if (!new_mci) {
  772. sprintf(msg, "Struct for socket #%u wasn't initialized",
  773. *socket);
  774. return -EINVAL;
  775. }
  776. mci = new_mci;
  777. pvt = mci->pvt_info;
  778. /*
  779. * Step 2) Get memory channel
  780. */
  781. prv = 0;
  782. for (n_tads = 0; n_tads < MAX_TAD; n_tads++) {
  783. pci_read_config_dword(pvt->pci_ha0, tad_dram_rule[n_tads],
  784. &reg);
  785. limit = TAD_LIMIT(reg);
  786. if (limit <= prv) {
  787. sprintf(msg, "Can't discover the memory channel");
  788. return -EINVAL;
  789. }
  790. if (addr <= limit)
  791. break;
  792. prv = limit;
  793. }
  794. ch_way = TAD_CH(reg) + 1;
  795. sck_way = TAD_SOCK(reg) + 1;
  796. /*
  797. * FIXME: Is it right to always use channel 0 for offsets?
  798. */
  799. pci_read_config_dword(pvt->pci_tad[0],
  800. tad_ch_nilv_offset[n_tads],
  801. &tad_offset);
  802. if (ch_way == 3)
  803. idx = addr >> 6;
  804. else
  805. idx = addr >> (6 + sck_way);
  806. idx = idx % ch_way;
  807. /*
  808. * FIXME: Shouldn't we use CHN_IDX_OFFSET() here, when ch_way == 3 ???
  809. */
  810. switch (idx) {
  811. case 0:
  812. base_ch = TAD_TGT0(reg);
  813. break;
  814. case 1:
  815. base_ch = TAD_TGT1(reg);
  816. break;
  817. case 2:
  818. base_ch = TAD_TGT2(reg);
  819. break;
  820. case 3:
  821. base_ch = TAD_TGT3(reg);
  822. break;
  823. default:
  824. sprintf(msg, "Can't discover the TAD target");
  825. return -EINVAL;
  826. }
  827. *channel_mask = 1 << base_ch;
  828. if (pvt->is_mirrored) {
  829. *channel_mask |= 1 << ((base_ch + 2) % 4);
  830. switch(ch_way) {
  831. case 2:
  832. case 4:
  833. sck_xch = 1 << sck_way * (ch_way >> 1);
  834. break;
  835. default:
  836. sprintf(msg, "Invalid mirror set. Can't decode addr");
  837. return -EINVAL;
  838. }
  839. } else
  840. sck_xch = (1 << sck_way) * ch_way;
  841. if (pvt->is_lockstep)
  842. *channel_mask |= 1 << ((base_ch + 1) % 4);
  843. offset = TAD_OFFSET(tad_offset);
  844. edac_dbg(0, "TAD#%d: address 0x%016Lx < 0x%016Lx, socket interleave %d, channel interleave %d (offset 0x%08Lx), index %d, base ch: %d, ch mask: 0x%02lx\n",
  845. n_tads,
  846. addr,
  847. limit,
  848. (u32)TAD_SOCK(reg),
  849. ch_way,
  850. offset,
  851. idx,
  852. base_ch,
  853. *channel_mask);
  854. /* Calculate channel address */
  855. /* Remove the TAD offset */
  856. if (offset > addr) {
  857. sprintf(msg, "Can't calculate ch addr: TAD offset 0x%08Lx is too high for addr 0x%08Lx!",
  858. offset, addr);
  859. return -EINVAL;
  860. }
  861. addr -= offset;
  862. /* Store the low bits [0:6] of the addr */
  863. ch_addr = addr & 0x7f;
  864. /* Remove socket wayness and remove 6 bits */
  865. addr >>= 6;
  866. addr = div_u64(addr, sck_xch);
  867. #if 0
  868. /* Divide by channel way */
  869. addr = addr / ch_way;
  870. #endif
  871. /* Recover the last 6 bits */
  872. ch_addr |= addr << 6;
  873. /*
  874. * Step 3) Decode rank
  875. */
  876. for (n_rir = 0; n_rir < MAX_RIR_RANGES; n_rir++) {
  877. pci_read_config_dword(pvt->pci_tad[base_ch],
  878. rir_way_limit[n_rir],
  879. &reg);
  880. if (!IS_RIR_VALID(reg))
  881. continue;
  882. limit = RIR_LIMIT(reg);
  883. mb = div_u64_rem(limit >> 20, 1000, &kb);
  884. edac_dbg(0, "RIR#%d, limit: %u.%03u GB (0x%016Lx), way: %d\n",
  885. n_rir,
  886. mb, kb,
  887. limit,
  888. 1 << RIR_WAY(reg));
  889. if (ch_addr <= limit)
  890. break;
  891. }
  892. if (n_rir == MAX_RIR_RANGES) {
  893. sprintf(msg, "Can't discover the memory rank for ch addr 0x%08Lx",
  894. ch_addr);
  895. return -EINVAL;
  896. }
  897. rir_way = RIR_WAY(reg);
  898. if (pvt->is_close_pg)
  899. idx = (ch_addr >> 6);
  900. else
  901. idx = (ch_addr >> 13); /* FIXME: Datasheet says to shift by 15 */
  902. idx %= 1 << rir_way;
  903. pci_read_config_dword(pvt->pci_tad[base_ch],
  904. rir_offset[n_rir][idx],
  905. &reg);
  906. *rank = RIR_RNK_TGT(reg);
  907. edac_dbg(0, "RIR#%d: channel address 0x%08Lx < 0x%08Lx, RIR interleave %d, index %d\n",
  908. n_rir,
  909. ch_addr,
  910. limit,
  911. rir_way,
  912. idx);
  913. return 0;
  914. }
  915. /****************************************************************************
  916. Device initialization routines: put/get, init/exit
  917. ****************************************************************************/
  918. /*
  919. * sbridge_put_all_devices 'put' all the devices that we have
  920. * reserved via 'get'
  921. */
  922. static void sbridge_put_devices(struct sbridge_dev *sbridge_dev)
  923. {
  924. int i;
  925. edac_dbg(0, "\n");
  926. for (i = 0; i < sbridge_dev->n_devs; i++) {
  927. struct pci_dev *pdev = sbridge_dev->pdev[i];
  928. if (!pdev)
  929. continue;
  930. edac_dbg(0, "Removing dev %02x:%02x.%d\n",
  931. pdev->bus->number,
  932. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  933. pci_dev_put(pdev);
  934. }
  935. }
  936. static void sbridge_put_all_devices(void)
  937. {
  938. struct sbridge_dev *sbridge_dev, *tmp;
  939. list_for_each_entry_safe(sbridge_dev, tmp, &sbridge_edac_list, list) {
  940. sbridge_put_devices(sbridge_dev);
  941. free_sbridge_dev(sbridge_dev);
  942. }
  943. }
  944. /*
  945. * sbridge_get_all_devices Find and perform 'get' operation on the MCH's
  946. * device/functions we want to reference for this driver
  947. *
  948. * Need to 'get' device 16 func 1 and func 2
  949. */
  950. static int sbridge_get_onedevice(struct pci_dev **prev,
  951. u8 *num_mc,
  952. const struct pci_id_table *table,
  953. const unsigned devno)
  954. {
  955. struct sbridge_dev *sbridge_dev;
  956. const struct pci_id_descr *dev_descr = &table->descr[devno];
  957. struct pci_dev *pdev = NULL;
  958. u8 bus = 0;
  959. sbridge_printk(KERN_INFO,
  960. "Seeking for: dev %02x.%d PCI ID %04x:%04x\n",
  961. dev_descr->dev, dev_descr->func,
  962. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  963. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  964. dev_descr->dev_id, *prev);
  965. if (!pdev) {
  966. if (*prev) {
  967. *prev = pdev;
  968. return 0;
  969. }
  970. if (dev_descr->optional)
  971. return 0;
  972. if (devno == 0)
  973. return -ENODEV;
  974. sbridge_printk(KERN_INFO,
  975. "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
  976. dev_descr->dev, dev_descr->func,
  977. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  978. /* End of list, leave */
  979. return -ENODEV;
  980. }
  981. bus = pdev->bus->number;
  982. sbridge_dev = get_sbridge_dev(bus);
  983. if (!sbridge_dev) {
  984. sbridge_dev = alloc_sbridge_dev(bus, table);
  985. if (!sbridge_dev) {
  986. pci_dev_put(pdev);
  987. return -ENOMEM;
  988. }
  989. (*num_mc)++;
  990. }
  991. if (sbridge_dev->pdev[devno]) {
  992. sbridge_printk(KERN_ERR,
  993. "Duplicated device for "
  994. "dev %02x:%d.%d PCI ID %04x:%04x\n",
  995. bus, dev_descr->dev, dev_descr->func,
  996. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  997. pci_dev_put(pdev);
  998. return -ENODEV;
  999. }
  1000. sbridge_dev->pdev[devno] = pdev;
  1001. /* Sanity check */
  1002. if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
  1003. PCI_FUNC(pdev->devfn) != dev_descr->func)) {
  1004. sbridge_printk(KERN_ERR,
  1005. "Device PCI ID %04x:%04x "
  1006. "has dev %02x:%d.%d instead of dev %02x:%02x.%d\n",
  1007. PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
  1008. bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1009. bus, dev_descr->dev, dev_descr->func);
  1010. return -ENODEV;
  1011. }
  1012. /* Be sure that the device is enabled */
  1013. if (unlikely(pci_enable_device(pdev) < 0)) {
  1014. sbridge_printk(KERN_ERR,
  1015. "Couldn't enable "
  1016. "dev %02x:%d.%d PCI ID %04x:%04x\n",
  1017. bus, dev_descr->dev, dev_descr->func,
  1018. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1019. return -ENODEV;
  1020. }
  1021. edac_dbg(0, "Detected dev %02x:%d.%d PCI ID %04x:%04x\n",
  1022. bus, dev_descr->dev, dev_descr->func,
  1023. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1024. /*
  1025. * As stated on drivers/pci/search.c, the reference count for
  1026. * @from is always decremented if it is not %NULL. So, as we need
  1027. * to get all devices up to null, we need to do a get for the device
  1028. */
  1029. pci_dev_get(pdev);
  1030. *prev = pdev;
  1031. return 0;
  1032. }
  1033. static int sbridge_get_all_devices(u8 *num_mc)
  1034. {
  1035. int i, rc;
  1036. struct pci_dev *pdev = NULL;
  1037. const struct pci_id_table *table = pci_dev_descr_sbridge_table;
  1038. while (table && table->descr) {
  1039. for (i = 0; i < table->n_devs; i++) {
  1040. pdev = NULL;
  1041. do {
  1042. rc = sbridge_get_onedevice(&pdev, num_mc,
  1043. table, i);
  1044. if (rc < 0) {
  1045. if (i == 0) {
  1046. i = table->n_devs;
  1047. break;
  1048. }
  1049. sbridge_put_all_devices();
  1050. return -ENODEV;
  1051. }
  1052. } while (pdev);
  1053. }
  1054. table++;
  1055. }
  1056. return 0;
  1057. }
  1058. static int mci_bind_devs(struct mem_ctl_info *mci,
  1059. struct sbridge_dev *sbridge_dev)
  1060. {
  1061. struct sbridge_pvt *pvt = mci->pvt_info;
  1062. struct pci_dev *pdev;
  1063. int i, func, slot;
  1064. for (i = 0; i < sbridge_dev->n_devs; i++) {
  1065. pdev = sbridge_dev->pdev[i];
  1066. if (!pdev)
  1067. continue;
  1068. slot = PCI_SLOT(pdev->devfn);
  1069. func = PCI_FUNC(pdev->devfn);
  1070. switch (slot) {
  1071. case 12:
  1072. switch (func) {
  1073. case 6:
  1074. pvt->pci_sad0 = pdev;
  1075. break;
  1076. case 7:
  1077. pvt->pci_sad1 = pdev;
  1078. break;
  1079. default:
  1080. goto error;
  1081. }
  1082. break;
  1083. case 13:
  1084. switch (func) {
  1085. case 6:
  1086. pvt->pci_br0 = pdev;
  1087. break;
  1088. default:
  1089. goto error;
  1090. }
  1091. break;
  1092. case 14:
  1093. switch (func) {
  1094. case 0:
  1095. pvt->pci_ha0 = pdev;
  1096. break;
  1097. default:
  1098. goto error;
  1099. }
  1100. break;
  1101. case 15:
  1102. switch (func) {
  1103. case 0:
  1104. pvt->pci_ta = pdev;
  1105. break;
  1106. case 1:
  1107. pvt->pci_ras = pdev;
  1108. break;
  1109. case 2:
  1110. case 3:
  1111. case 4:
  1112. case 5:
  1113. pvt->pci_tad[func - 2] = pdev;
  1114. break;
  1115. default:
  1116. goto error;
  1117. }
  1118. break;
  1119. case 17:
  1120. switch (func) {
  1121. case 0:
  1122. pvt->pci_ddrio = pdev;
  1123. break;
  1124. default:
  1125. goto error;
  1126. }
  1127. break;
  1128. default:
  1129. goto error;
  1130. }
  1131. edac_dbg(0, "Associated PCI %02x.%02d.%d with dev = %p\n",
  1132. sbridge_dev->bus,
  1133. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1134. pdev);
  1135. }
  1136. /* Check if everything were registered */
  1137. if (!pvt->pci_sad0 || !pvt->pci_sad1 || !pvt->pci_ha0 ||
  1138. !pvt-> pci_tad || !pvt->pci_ras || !pvt->pci_ta)
  1139. goto enodev;
  1140. for (i = 0; i < NUM_CHANNELS; i++) {
  1141. if (!pvt->pci_tad[i])
  1142. goto enodev;
  1143. }
  1144. return 0;
  1145. enodev:
  1146. sbridge_printk(KERN_ERR, "Some needed devices are missing\n");
  1147. return -ENODEV;
  1148. error:
  1149. sbridge_printk(KERN_ERR, "Device %d, function %d "
  1150. "is out of the expected range\n",
  1151. slot, func);
  1152. return -EINVAL;
  1153. }
  1154. /****************************************************************************
  1155. Error check routines
  1156. ****************************************************************************/
  1157. /*
  1158. * While Sandy Bridge has error count registers, SMI BIOS read values from
  1159. * and resets the counters. So, they are not reliable for the OS to read
  1160. * from them. So, we have no option but to just trust on whatever MCE is
  1161. * telling us about the errors.
  1162. */
  1163. static void sbridge_mce_output_error(struct mem_ctl_info *mci,
  1164. const struct mce *m)
  1165. {
  1166. struct mem_ctl_info *new_mci;
  1167. struct sbridge_pvt *pvt = mci->pvt_info;
  1168. enum hw_event_mc_err_type tp_event;
  1169. char *type, *optype, msg[256];
  1170. bool ripv = GET_BITFIELD(m->mcgstatus, 0, 0);
  1171. bool overflow = GET_BITFIELD(m->status, 62, 62);
  1172. bool uncorrected_error = GET_BITFIELD(m->status, 61, 61);
  1173. bool recoverable = GET_BITFIELD(m->status, 56, 56);
  1174. u32 core_err_cnt = GET_BITFIELD(m->status, 38, 52);
  1175. u32 mscod = GET_BITFIELD(m->status, 16, 31);
  1176. u32 errcode = GET_BITFIELD(m->status, 0, 15);
  1177. u32 channel = GET_BITFIELD(m->status, 0, 3);
  1178. u32 optypenum = GET_BITFIELD(m->status, 4, 6);
  1179. long channel_mask, first_channel;
  1180. u8 rank, socket;
  1181. int rc, dimm;
  1182. char *area_type = NULL;
  1183. if (uncorrected_error) {
  1184. if (ripv) {
  1185. type = "FATAL";
  1186. tp_event = HW_EVENT_ERR_FATAL;
  1187. } else {
  1188. type = "NON_FATAL";
  1189. tp_event = HW_EVENT_ERR_UNCORRECTED;
  1190. }
  1191. } else {
  1192. type = "CORRECTED";
  1193. tp_event = HW_EVENT_ERR_CORRECTED;
  1194. }
  1195. /*
  1196. * According with Table 15-9 of the Intel Architecture spec vol 3A,
  1197. * memory errors should fit in this mask:
  1198. * 000f 0000 1mmm cccc (binary)
  1199. * where:
  1200. * f = Correction Report Filtering Bit. If 1, subsequent errors
  1201. * won't be shown
  1202. * mmm = error type
  1203. * cccc = channel
  1204. * If the mask doesn't match, report an error to the parsing logic
  1205. */
  1206. if (! ((errcode & 0xef80) == 0x80)) {
  1207. optype = "Can't parse: it is not a mem";
  1208. } else {
  1209. switch (optypenum) {
  1210. case 0:
  1211. optype = "generic undef request error";
  1212. break;
  1213. case 1:
  1214. optype = "memory read error";
  1215. break;
  1216. case 2:
  1217. optype = "memory write error";
  1218. break;
  1219. case 3:
  1220. optype = "addr/cmd error";
  1221. break;
  1222. case 4:
  1223. optype = "memory scrubbing error";
  1224. break;
  1225. default:
  1226. optype = "reserved";
  1227. break;
  1228. }
  1229. }
  1230. rc = get_memory_error_data(mci, m->addr, &socket,
  1231. &channel_mask, &rank, &area_type, msg);
  1232. if (rc < 0)
  1233. goto err_parsing;
  1234. new_mci = get_mci_for_node_id(socket);
  1235. if (!new_mci) {
  1236. strcpy(msg, "Error: socket got corrupted!");
  1237. goto err_parsing;
  1238. }
  1239. mci = new_mci;
  1240. pvt = mci->pvt_info;
  1241. first_channel = find_first_bit(&channel_mask, NUM_CHANNELS);
  1242. if (rank < 4)
  1243. dimm = 0;
  1244. else if (rank < 8)
  1245. dimm = 1;
  1246. else
  1247. dimm = 2;
  1248. /*
  1249. * FIXME: On some memory configurations (mirror, lockstep), the
  1250. * Memory Controller can't point the error to a single DIMM. The
  1251. * EDAC core should be handling the channel mask, in order to point
  1252. * to the group of dimm's where the error may be happening.
  1253. */
  1254. snprintf(msg, sizeof(msg),
  1255. "%s%s area:%s err_code:%04x:%04x socket:%d channel_mask:%ld rank:%d",
  1256. overflow ? " OVERFLOW" : "",
  1257. (uncorrected_error && recoverable) ? " recoverable" : "",
  1258. area_type,
  1259. mscod, errcode,
  1260. socket,
  1261. channel_mask,
  1262. rank);
  1263. edac_dbg(0, "%s\n", msg);
  1264. /* FIXME: need support for channel mask */
  1265. /* Call the helper to output message */
  1266. edac_mc_handle_error(tp_event, mci, core_err_cnt,
  1267. m->addr >> PAGE_SHIFT, m->addr & ~PAGE_MASK, 0,
  1268. channel, dimm, -1,
  1269. optype, msg);
  1270. return;
  1271. err_parsing:
  1272. edac_mc_handle_error(tp_event, mci, core_err_cnt, 0, 0, 0,
  1273. -1, -1, -1,
  1274. msg, "");
  1275. }
  1276. /*
  1277. * sbridge_check_error Retrieve and process errors reported by the
  1278. * hardware. Called by the Core module.
  1279. */
  1280. static void sbridge_check_error(struct mem_ctl_info *mci)
  1281. {
  1282. struct sbridge_pvt *pvt = mci->pvt_info;
  1283. int i;
  1284. unsigned count = 0;
  1285. struct mce *m;
  1286. /*
  1287. * MCE first step: Copy all mce errors into a temporary buffer
  1288. * We use a double buffering here, to reduce the risk of
  1289. * loosing an error.
  1290. */
  1291. smp_rmb();
  1292. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1293. % MCE_LOG_LEN;
  1294. if (!count)
  1295. return;
  1296. m = pvt->mce_outentry;
  1297. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1298. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1299. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1300. smp_wmb();
  1301. pvt->mce_in = 0;
  1302. count -= l;
  1303. m += l;
  1304. }
  1305. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1306. smp_wmb();
  1307. pvt->mce_in += count;
  1308. smp_rmb();
  1309. if (pvt->mce_overrun) {
  1310. sbridge_printk(KERN_ERR, "Lost %d memory errors\n",
  1311. pvt->mce_overrun);
  1312. smp_wmb();
  1313. pvt->mce_overrun = 0;
  1314. }
  1315. /*
  1316. * MCE second step: parse errors and display
  1317. */
  1318. for (i = 0; i < count; i++)
  1319. sbridge_mce_output_error(mci, &pvt->mce_outentry[i]);
  1320. }
  1321. /*
  1322. * sbridge_mce_check_error Replicates mcelog routine to get errors
  1323. * This routine simply queues mcelog errors, and
  1324. * return. The error itself should be handled later
  1325. * by sbridge_check_error.
  1326. * WARNING: As this routine should be called at NMI time, extra care should
  1327. * be taken to avoid deadlocks, and to be as fast as possible.
  1328. */
  1329. static int sbridge_mce_check_error(struct notifier_block *nb, unsigned long val,
  1330. void *data)
  1331. {
  1332. struct mce *mce = (struct mce *)data;
  1333. struct mem_ctl_info *mci;
  1334. struct sbridge_pvt *pvt;
  1335. mci = get_mci_for_node_id(mce->socketid);
  1336. if (!mci)
  1337. return NOTIFY_BAD;
  1338. pvt = mci->pvt_info;
  1339. /*
  1340. * Just let mcelog handle it if the error is
  1341. * outside the memory controller. A memory error
  1342. * is indicated by bit 7 = 1 and bits = 8-11,13-15 = 0.
  1343. * bit 12 has an special meaning.
  1344. */
  1345. if ((mce->status & 0xefff) >> 7 != 1)
  1346. return NOTIFY_DONE;
  1347. printk("sbridge: HANDLING MCE MEMORY ERROR\n");
  1348. printk("CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
  1349. mce->extcpu, mce->mcgstatus, mce->bank, mce->status);
  1350. printk("TSC %llx ", mce->tsc);
  1351. printk("ADDR %llx ", mce->addr);
  1352. printk("MISC %llx ", mce->misc);
  1353. printk("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  1354. mce->cpuvendor, mce->cpuid, mce->time,
  1355. mce->socketid, mce->apicid);
  1356. /* Only handle if it is the right mc controller */
  1357. if (cpu_data(mce->cpu).phys_proc_id != pvt->sbridge_dev->mc)
  1358. return NOTIFY_DONE;
  1359. smp_rmb();
  1360. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1361. smp_wmb();
  1362. pvt->mce_overrun++;
  1363. return NOTIFY_DONE;
  1364. }
  1365. /* Copy memory error at the ringbuffer */
  1366. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1367. smp_wmb();
  1368. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1369. /* Handle fatal errors immediately */
  1370. if (mce->mcgstatus & 1)
  1371. sbridge_check_error(mci);
  1372. /* Advice mcelog that the error were handled */
  1373. return NOTIFY_STOP;
  1374. }
  1375. static struct notifier_block sbridge_mce_dec = {
  1376. .notifier_call = sbridge_mce_check_error,
  1377. };
  1378. /****************************************************************************
  1379. EDAC register/unregister logic
  1380. ****************************************************************************/
  1381. static void sbridge_unregister_mci(struct sbridge_dev *sbridge_dev)
  1382. {
  1383. struct mem_ctl_info *mci = sbridge_dev->mci;
  1384. struct sbridge_pvt *pvt;
  1385. if (unlikely(!mci || !mci->pvt_info)) {
  1386. edac_dbg(0, "MC: dev = %p\n", &sbridge_dev->pdev[0]->dev);
  1387. sbridge_printk(KERN_ERR, "Couldn't find mci handler\n");
  1388. return;
  1389. }
  1390. pvt = mci->pvt_info;
  1391. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  1392. mci, &sbridge_dev->pdev[0]->dev);
  1393. /* Remove MC sysfs nodes */
  1394. edac_mc_del_mc(mci->pdev);
  1395. edac_dbg(1, "%s: free mci struct\n", mci->ctl_name);
  1396. kfree(mci->ctl_name);
  1397. edac_mc_free(mci);
  1398. sbridge_dev->mci = NULL;
  1399. }
  1400. static int sbridge_register_mci(struct sbridge_dev *sbridge_dev)
  1401. {
  1402. struct mem_ctl_info *mci;
  1403. struct edac_mc_layer layers[2];
  1404. struct sbridge_pvt *pvt;
  1405. int rc;
  1406. /* Check the number of active and not disabled channels */
  1407. rc = check_if_ecc_is_active(sbridge_dev->bus);
  1408. if (unlikely(rc < 0))
  1409. return rc;
  1410. /* allocate a new MC control structure */
  1411. layers[0].type = EDAC_MC_LAYER_CHANNEL;
  1412. layers[0].size = NUM_CHANNELS;
  1413. layers[0].is_virt_csrow = false;
  1414. layers[1].type = EDAC_MC_LAYER_SLOT;
  1415. layers[1].size = MAX_DIMMS;
  1416. layers[1].is_virt_csrow = true;
  1417. mci = edac_mc_alloc(sbridge_dev->mc, ARRAY_SIZE(layers), layers,
  1418. sizeof(*pvt));
  1419. if (unlikely(!mci))
  1420. return -ENOMEM;
  1421. edac_dbg(0, "MC: mci = %p, dev = %p\n",
  1422. mci, &sbridge_dev->pdev[0]->dev);
  1423. pvt = mci->pvt_info;
  1424. memset(pvt, 0, sizeof(*pvt));
  1425. /* Associate sbridge_dev and mci for future usage */
  1426. pvt->sbridge_dev = sbridge_dev;
  1427. sbridge_dev->mci = mci;
  1428. mci->mtype_cap = MEM_FLAG_DDR3;
  1429. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1430. mci->edac_cap = EDAC_FLAG_NONE;
  1431. mci->mod_name = "sbridge_edac.c";
  1432. mci->mod_ver = SBRIDGE_REVISION;
  1433. mci->ctl_name = kasprintf(GFP_KERNEL, "Sandy Bridge Socket#%d", mci->mc_idx);
  1434. mci->dev_name = pci_name(sbridge_dev->pdev[0]);
  1435. mci->ctl_page_to_phys = NULL;
  1436. pvt->info.get_tolm = sbridge_get_tolm;
  1437. pvt->info.get_tohm = sbridge_get_tohm;
  1438. pvt->info.dram_rule = sbridge_dram_rule;
  1439. pvt->info.max_sad = ARRAY_SIZE(sbridge_dram_rule);
  1440. pvt->info.interleave_list = sbridge_interleave_list;
  1441. pvt->info.max_interleave = ARRAY_SIZE(sbridge_interleave_list);
  1442. /* Set the function pointer to an actual operation function */
  1443. mci->edac_check = sbridge_check_error;
  1444. /* Store pci devices at mci for faster access */
  1445. rc = mci_bind_devs(mci, sbridge_dev);
  1446. if (unlikely(rc < 0))
  1447. goto fail0;
  1448. /* Get dimm basic config and the memory layout */
  1449. get_dimm_config(mci);
  1450. get_memory_layout(mci);
  1451. /* record ptr to the generic device */
  1452. mci->pdev = &sbridge_dev->pdev[0]->dev;
  1453. /* add this new MC control structure to EDAC's list of MCs */
  1454. if (unlikely(edac_mc_add_mc(mci))) {
  1455. edac_dbg(0, "MC: failed edac_mc_add_mc()\n");
  1456. rc = -EINVAL;
  1457. goto fail0;
  1458. }
  1459. return 0;
  1460. fail0:
  1461. kfree(mci->ctl_name);
  1462. edac_mc_free(mci);
  1463. sbridge_dev->mci = NULL;
  1464. return rc;
  1465. }
  1466. /*
  1467. * sbridge_probe Probe for ONE instance of device to see if it is
  1468. * present.
  1469. * return:
  1470. * 0 for FOUND a device
  1471. * < 0 for error code
  1472. */
  1473. static int sbridge_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1474. {
  1475. int rc;
  1476. u8 mc, num_mc = 0;
  1477. struct sbridge_dev *sbridge_dev;
  1478. /* get the pci devices we want to reserve for our use */
  1479. mutex_lock(&sbridge_edac_lock);
  1480. /*
  1481. * All memory controllers are allocated at the first pass.
  1482. */
  1483. if (unlikely(probed >= 1)) {
  1484. mutex_unlock(&sbridge_edac_lock);
  1485. return -ENODEV;
  1486. }
  1487. probed++;
  1488. rc = sbridge_get_all_devices(&num_mc);
  1489. if (unlikely(rc < 0))
  1490. goto fail0;
  1491. mc = 0;
  1492. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list) {
  1493. edac_dbg(0, "Registering MC#%d (%d of %d)\n",
  1494. mc, mc + 1, num_mc);
  1495. sbridge_dev->mc = mc++;
  1496. rc = sbridge_register_mci(sbridge_dev);
  1497. if (unlikely(rc < 0))
  1498. goto fail1;
  1499. }
  1500. sbridge_printk(KERN_INFO, "Driver loaded.\n");
  1501. mutex_unlock(&sbridge_edac_lock);
  1502. return 0;
  1503. fail1:
  1504. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1505. sbridge_unregister_mci(sbridge_dev);
  1506. sbridge_put_all_devices();
  1507. fail0:
  1508. mutex_unlock(&sbridge_edac_lock);
  1509. return rc;
  1510. }
  1511. /*
  1512. * sbridge_remove destructor for one instance of device
  1513. *
  1514. */
  1515. static void sbridge_remove(struct pci_dev *pdev)
  1516. {
  1517. struct sbridge_dev *sbridge_dev;
  1518. edac_dbg(0, "\n");
  1519. /*
  1520. * we have a trouble here: pdev value for removal will be wrong, since
  1521. * it will point to the X58 register used to detect that the machine
  1522. * is a Nehalem or upper design. However, due to the way several PCI
  1523. * devices are grouped together to provide MC functionality, we need
  1524. * to use a different method for releasing the devices
  1525. */
  1526. mutex_lock(&sbridge_edac_lock);
  1527. if (unlikely(!probed)) {
  1528. mutex_unlock(&sbridge_edac_lock);
  1529. return;
  1530. }
  1531. list_for_each_entry(sbridge_dev, &sbridge_edac_list, list)
  1532. sbridge_unregister_mci(sbridge_dev);
  1533. /* Release PCI resources */
  1534. sbridge_put_all_devices();
  1535. probed--;
  1536. mutex_unlock(&sbridge_edac_lock);
  1537. }
  1538. MODULE_DEVICE_TABLE(pci, sbridge_pci_tbl);
  1539. /*
  1540. * sbridge_driver pci_driver structure for this module
  1541. *
  1542. */
  1543. static struct pci_driver sbridge_driver = {
  1544. .name = "sbridge_edac",
  1545. .probe = sbridge_probe,
  1546. .remove = sbridge_remove,
  1547. .id_table = sbridge_pci_tbl,
  1548. };
  1549. /*
  1550. * sbridge_init Module entry function
  1551. * Try to initialize this module for its devices
  1552. */
  1553. static int __init sbridge_init(void)
  1554. {
  1555. int pci_rc;
  1556. edac_dbg(2, "\n");
  1557. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1558. opstate_init();
  1559. pci_rc = pci_register_driver(&sbridge_driver);
  1560. if (pci_rc >= 0) {
  1561. mce_register_decode_chain(&sbridge_mce_dec);
  1562. return 0;
  1563. }
  1564. sbridge_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1565. pci_rc);
  1566. return pci_rc;
  1567. }
  1568. /*
  1569. * sbridge_exit() Module exit function
  1570. * Unregister the driver
  1571. */
  1572. static void __exit sbridge_exit(void)
  1573. {
  1574. edac_dbg(2, "\n");
  1575. pci_unregister_driver(&sbridge_driver);
  1576. mce_unregister_decode_chain(&sbridge_mce_dec);
  1577. }
  1578. module_init(sbridge_init);
  1579. module_exit(sbridge_exit);
  1580. module_param(edac_op_state, int, 0444);
  1581. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
  1582. MODULE_LICENSE("GPL");
  1583. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1584. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1585. MODULE_DESCRIPTION("MC Driver for Intel Sandy Bridge memory controllers - "
  1586. SBRIDGE_REVISION);