/drivers/edac/i7core_edac.c

https://bitbucket.org/imoseyon/lean-gbs · C · 2105 lines · 1411 code · 337 blank · 357 comment · 175 complexity · 48bf1ef3b055ea856cc1aed5feeea9a9 MD5 · raw file

  1. /* Intel i7 core/Nehalem Memory Controller kernel module
  2. *
  3. * This driver supports yhe memory controllers found on the Intel
  4. * processor families i7core, i7core 7xx/8xx, i5core, Xeon 35xx,
  5. * Xeon 55xx and Xeon 56xx also known as Nehalem, Nehalem-EP, Lynnfield
  6. * and Westmere-EP.
  7. *
  8. * This file may be distributed under the terms of the
  9. * GNU General Public License version 2 only.
  10. *
  11. * Copyright (c) 2009-2010 by:
  12. * Mauro Carvalho Chehab <mchehab@redhat.com>
  13. *
  14. * Red Hat Inc. http://www.redhat.com
  15. *
  16. * Forked and adapted from the i5400_edac driver
  17. *
  18. * Based on the following public Intel datasheets:
  19. * Intel Core i7 Processor Extreme Edition and Intel Core i7 Processor
  20. * Datasheet, Volume 2:
  21. * http://download.intel.com/design/processor/datashts/320835.pdf
  22. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  23. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  24. * also available at:
  25. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <linux/pci_ids.h>
  31. #include <linux/slab.h>
  32. #include <linux/delay.h>
  33. #include <linux/edac.h>
  34. #include <linux/mmzone.h>
  35. #include <linux/edac_mce.h>
  36. #include <linux/smp.h>
  37. #include <asm/processor.h>
  38. #include "edac_core.h"
  39. /*
  40. * This is used for Nehalem-EP and Nehalem-EX devices, where the non-core
  41. * registers start at bus 255, and are not reported by BIOS.
  42. * We currently find devices with only 2 sockets. In order to support more QPI
  43. * Quick Path Interconnect, just increment this number.
  44. */
  45. #define MAX_SOCKET_BUSES 2
  46. /*
  47. * Alter this version for the module when modifications are made
  48. */
  49. #define I7CORE_REVISION " Ver: 1.0.0 " __DATE__
  50. #define EDAC_MOD_STR "i7core_edac"
  51. /*
  52. * Debug macros
  53. */
  54. #define i7core_printk(level, fmt, arg...) \
  55. edac_printk(level, "i7core", fmt, ##arg)
  56. #define i7core_mc_printk(mci, level, fmt, arg...) \
  57. edac_mc_chipset_printk(mci, level, "i7core", fmt, ##arg)
  58. /*
  59. * i7core Memory Controller Registers
  60. */
  61. /* OFFSETS for Device 0 Function 0 */
  62. #define MC_CFG_CONTROL 0x90
  63. /* OFFSETS for Device 3 Function 0 */
  64. #define MC_CONTROL 0x48
  65. #define MC_STATUS 0x4c
  66. #define MC_MAX_DOD 0x64
  67. /*
  68. * OFFSETS for Device 3 Function 4, as inicated on Xeon 5500 datasheet:
  69. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  70. */
  71. #define MC_TEST_ERR_RCV1 0x60
  72. #define DIMM2_COR_ERR(r) ((r) & 0x7fff)
  73. #define MC_TEST_ERR_RCV0 0x64
  74. #define DIMM1_COR_ERR(r) (((r) >> 16) & 0x7fff)
  75. #define DIMM0_COR_ERR(r) ((r) & 0x7fff)
  76. /* OFFSETS for Device 3 Function 2, as inicated on Xeon 5500 datasheet */
  77. #define MC_COR_ECC_CNT_0 0x80
  78. #define MC_COR_ECC_CNT_1 0x84
  79. #define MC_COR_ECC_CNT_2 0x88
  80. #define MC_COR_ECC_CNT_3 0x8c
  81. #define MC_COR_ECC_CNT_4 0x90
  82. #define MC_COR_ECC_CNT_5 0x94
  83. #define DIMM_TOP_COR_ERR(r) (((r) >> 16) & 0x7fff)
  84. #define DIMM_BOT_COR_ERR(r) ((r) & 0x7fff)
  85. /* OFFSETS for Devices 4,5 and 6 Function 0 */
  86. #define MC_CHANNEL_DIMM_INIT_PARAMS 0x58
  87. #define THREE_DIMMS_PRESENT (1 << 24)
  88. #define SINGLE_QUAD_RANK_PRESENT (1 << 23)
  89. #define QUAD_RANK_PRESENT (1 << 22)
  90. #define REGISTERED_DIMM (1 << 15)
  91. #define MC_CHANNEL_MAPPER 0x60
  92. #define RDLCH(r, ch) ((((r) >> (3 + (ch * 6))) & 0x07) - 1)
  93. #define WRLCH(r, ch) ((((r) >> (ch * 6)) & 0x07) - 1)
  94. #define MC_CHANNEL_RANK_PRESENT 0x7c
  95. #define RANK_PRESENT_MASK 0xffff
  96. #define MC_CHANNEL_ADDR_MATCH 0xf0
  97. #define MC_CHANNEL_ERROR_MASK 0xf8
  98. #define MC_CHANNEL_ERROR_INJECT 0xfc
  99. #define INJECT_ADDR_PARITY 0x10
  100. #define INJECT_ECC 0x08
  101. #define MASK_CACHELINE 0x06
  102. #define MASK_FULL_CACHELINE 0x06
  103. #define MASK_MSB32_CACHELINE 0x04
  104. #define MASK_LSB32_CACHELINE 0x02
  105. #define NO_MASK_CACHELINE 0x00
  106. #define REPEAT_EN 0x01
  107. /* OFFSETS for Devices 4,5 and 6 Function 1 */
  108. #define MC_DOD_CH_DIMM0 0x48
  109. #define MC_DOD_CH_DIMM1 0x4c
  110. #define MC_DOD_CH_DIMM2 0x50
  111. #define RANKOFFSET_MASK ((1 << 12) | (1 << 11) | (1 << 10))
  112. #define RANKOFFSET(x) ((x & RANKOFFSET_MASK) >> 10)
  113. #define DIMM_PRESENT_MASK (1 << 9)
  114. #define DIMM_PRESENT(x) (((x) & DIMM_PRESENT_MASK) >> 9)
  115. #define MC_DOD_NUMBANK_MASK ((1 << 8) | (1 << 7))
  116. #define MC_DOD_NUMBANK(x) (((x) & MC_DOD_NUMBANK_MASK) >> 7)
  117. #define MC_DOD_NUMRANK_MASK ((1 << 6) | (1 << 5))
  118. #define MC_DOD_NUMRANK(x) (((x) & MC_DOD_NUMRANK_MASK) >> 5)
  119. #define MC_DOD_NUMROW_MASK ((1 << 4) | (1 << 3) | (1 << 2))
  120. #define MC_DOD_NUMROW(x) (((x) & MC_DOD_NUMROW_MASK) >> 2)
  121. #define MC_DOD_NUMCOL_MASK 3
  122. #define MC_DOD_NUMCOL(x) ((x) & MC_DOD_NUMCOL_MASK)
  123. #define MC_RANK_PRESENT 0x7c
  124. #define MC_SAG_CH_0 0x80
  125. #define MC_SAG_CH_1 0x84
  126. #define MC_SAG_CH_2 0x88
  127. #define MC_SAG_CH_3 0x8c
  128. #define MC_SAG_CH_4 0x90
  129. #define MC_SAG_CH_5 0x94
  130. #define MC_SAG_CH_6 0x98
  131. #define MC_SAG_CH_7 0x9c
  132. #define MC_RIR_LIMIT_CH_0 0x40
  133. #define MC_RIR_LIMIT_CH_1 0x44
  134. #define MC_RIR_LIMIT_CH_2 0x48
  135. #define MC_RIR_LIMIT_CH_3 0x4C
  136. #define MC_RIR_LIMIT_CH_4 0x50
  137. #define MC_RIR_LIMIT_CH_5 0x54
  138. #define MC_RIR_LIMIT_CH_6 0x58
  139. #define MC_RIR_LIMIT_CH_7 0x5C
  140. #define MC_RIR_LIMIT_MASK ((1 << 10) - 1)
  141. #define MC_RIR_WAY_CH 0x80
  142. #define MC_RIR_WAY_OFFSET_MASK (((1 << 14) - 1) & ~0x7)
  143. #define MC_RIR_WAY_RANK_MASK 0x7
  144. /*
  145. * i7core structs
  146. */
  147. #define NUM_CHANS 3
  148. #define MAX_DIMMS 3 /* Max DIMMS per channel */
  149. #define MAX_MCR_FUNC 4
  150. #define MAX_CHAN_FUNC 3
  151. struct i7core_info {
  152. u32 mc_control;
  153. u32 mc_status;
  154. u32 max_dod;
  155. u32 ch_map;
  156. };
  157. struct i7core_inject {
  158. int enable;
  159. u32 section;
  160. u32 type;
  161. u32 eccmask;
  162. /* Error address mask */
  163. int channel, dimm, rank, bank, page, col;
  164. };
  165. struct i7core_channel {
  166. u32 ranks;
  167. u32 dimms;
  168. };
  169. struct pci_id_descr {
  170. int dev;
  171. int func;
  172. int dev_id;
  173. int optional;
  174. };
  175. struct pci_id_table {
  176. struct pci_id_descr *descr;
  177. int n_devs;
  178. };
  179. struct i7core_dev {
  180. struct list_head list;
  181. u8 socket;
  182. struct pci_dev **pdev;
  183. int n_devs;
  184. struct mem_ctl_info *mci;
  185. };
  186. struct i7core_pvt {
  187. struct pci_dev *pci_noncore;
  188. struct pci_dev *pci_mcr[MAX_MCR_FUNC + 1];
  189. struct pci_dev *pci_ch[NUM_CHANS][MAX_CHAN_FUNC + 1];
  190. struct i7core_dev *i7core_dev;
  191. struct i7core_info info;
  192. struct i7core_inject inject;
  193. struct i7core_channel channel[NUM_CHANS];
  194. int channels; /* Number of active channels */
  195. int ce_count_available;
  196. int csrow_map[NUM_CHANS][MAX_DIMMS];
  197. /* ECC corrected errors counts per udimm */
  198. unsigned long udimm_ce_count[MAX_DIMMS];
  199. int udimm_last_ce_count[MAX_DIMMS];
  200. /* ECC corrected errors counts per rdimm */
  201. unsigned long rdimm_ce_count[NUM_CHANS][MAX_DIMMS];
  202. int rdimm_last_ce_count[NUM_CHANS][MAX_DIMMS];
  203. unsigned int is_registered;
  204. /* mcelog glue */
  205. struct edac_mce edac_mce;
  206. /* Fifo double buffers */
  207. struct mce mce_entry[MCE_LOG_LEN];
  208. struct mce mce_outentry[MCE_LOG_LEN];
  209. /* Fifo in/out counters */
  210. unsigned mce_in, mce_out;
  211. /* Count indicator to show errors not got */
  212. unsigned mce_overrun;
  213. };
  214. /* Static vars */
  215. static LIST_HEAD(i7core_edac_list);
  216. static DEFINE_MUTEX(i7core_edac_lock);
  217. #define PCI_DESCR(device, function, device_id) \
  218. .dev = (device), \
  219. .func = (function), \
  220. .dev_id = (device_id)
  221. struct pci_id_descr pci_dev_descr_i7core_nehalem[] = {
  222. /* Memory controller */
  223. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_I7_MCR) },
  224. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_I7_MC_TAD) },
  225. /* Exists only for RDIMM */
  226. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_I7_MC_RAS), .optional = 1 },
  227. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_I7_MC_TEST) },
  228. /* Channel 0 */
  229. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH0_CTRL) },
  230. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH0_ADDR) },
  231. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH0_RANK) },
  232. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH0_TC) },
  233. /* Channel 1 */
  234. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH1_CTRL) },
  235. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH1_ADDR) },
  236. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH1_RANK) },
  237. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH1_TC) },
  238. /* Channel 2 */
  239. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_I7_MC_CH2_CTRL) },
  240. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_I7_MC_CH2_ADDR) },
  241. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_I7_MC_CH2_RANK) },
  242. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_I7_MC_CH2_TC) },
  243. /* Generic Non-core registers */
  244. /*
  245. * This is the PCI device on i7core and on Xeon 35xx (8086:2c41)
  246. * On Xeon 55xx, however, it has a different id (8086:2c40). So,
  247. * the probing code needs to test for the other address in case of
  248. * failure of this one
  249. */
  250. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_I7_NONCORE) },
  251. };
  252. struct pci_id_descr pci_dev_descr_lynnfield[] = {
  253. { PCI_DESCR( 3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR) },
  254. { PCI_DESCR( 3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD) },
  255. { PCI_DESCR( 3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST) },
  256. { PCI_DESCR( 4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL) },
  257. { PCI_DESCR( 4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR) },
  258. { PCI_DESCR( 4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK) },
  259. { PCI_DESCR( 4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC) },
  260. { PCI_DESCR( 5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL) },
  261. { PCI_DESCR( 5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR) },
  262. { PCI_DESCR( 5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK) },
  263. { PCI_DESCR( 5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC) },
  264. /*
  265. * This is the PCI device has an alternate address on some
  266. * processors like Core i7 860
  267. */
  268. { PCI_DESCR( 0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE) },
  269. };
  270. struct pci_id_descr pci_dev_descr_i7core_westmere[] = {
  271. /* Memory controller */
  272. { PCI_DESCR(3, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MCR_REV2) },
  273. { PCI_DESCR(3, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TAD_REV2) },
  274. /* Exists only for RDIMM */
  275. { PCI_DESCR(3, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_RAS_REV2), .optional = 1 },
  276. { PCI_DESCR(3, 4, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_TEST_REV2) },
  277. /* Channel 0 */
  278. { PCI_DESCR(4, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_CTRL_REV2) },
  279. { PCI_DESCR(4, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_ADDR_REV2) },
  280. { PCI_DESCR(4, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_RANK_REV2) },
  281. { PCI_DESCR(4, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH0_TC_REV2) },
  282. /* Channel 1 */
  283. { PCI_DESCR(5, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_CTRL_REV2) },
  284. { PCI_DESCR(5, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_ADDR_REV2) },
  285. { PCI_DESCR(5, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_RANK_REV2) },
  286. { PCI_DESCR(5, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH1_TC_REV2) },
  287. /* Channel 2 */
  288. { PCI_DESCR(6, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_CTRL_REV2) },
  289. { PCI_DESCR(6, 1, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_ADDR_REV2) },
  290. { PCI_DESCR(6, 2, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_RANK_REV2) },
  291. { PCI_DESCR(6, 3, PCI_DEVICE_ID_INTEL_LYNNFIELD_MC_CH2_TC_REV2) },
  292. /* Generic Non-core registers */
  293. { PCI_DESCR(0, 0, PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_REV2) },
  294. };
  295. #define PCI_ID_TABLE_ENTRY(A) { A, ARRAY_SIZE(A) }
  296. struct pci_id_table pci_dev_table[] = {
  297. PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_nehalem),
  298. PCI_ID_TABLE_ENTRY(pci_dev_descr_lynnfield),
  299. PCI_ID_TABLE_ENTRY(pci_dev_descr_i7core_westmere),
  300. };
  301. /*
  302. * pci_device_id table for which devices we are looking for
  303. */
  304. static const struct pci_device_id i7core_pci_tbl[] __devinitdata = {
  305. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58_HUB_MGMT)},
  306. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_LYNNFIELD_QPI_LINK0)},
  307. {0,} /* 0 terminated list. */
  308. };
  309. static struct edac_pci_ctl_info *i7core_pci;
  310. /****************************************************************************
  311. Anciliary status routines
  312. ****************************************************************************/
  313. /* MC_CONTROL bits */
  314. #define CH_ACTIVE(pvt, ch) ((pvt)->info.mc_control & (1 << (8 + ch)))
  315. #define ECCx8(pvt) ((pvt)->info.mc_control & (1 << 1))
  316. /* MC_STATUS bits */
  317. #define ECC_ENABLED(pvt) ((pvt)->info.mc_status & (1 << 4))
  318. #define CH_DISABLED(pvt, ch) ((pvt)->info.mc_status & (1 << ch))
  319. /* MC_MAX_DOD read functions */
  320. static inline int numdimms(u32 dimms)
  321. {
  322. return (dimms & 0x3) + 1;
  323. }
  324. static inline int numrank(u32 rank)
  325. {
  326. static int ranks[4] = { 1, 2, 4, -EINVAL };
  327. return ranks[rank & 0x3];
  328. }
  329. static inline int numbank(u32 bank)
  330. {
  331. static int banks[4] = { 4, 8, 16, -EINVAL };
  332. return banks[bank & 0x3];
  333. }
  334. static inline int numrow(u32 row)
  335. {
  336. static int rows[8] = {
  337. 1 << 12, 1 << 13, 1 << 14, 1 << 15,
  338. 1 << 16, -EINVAL, -EINVAL, -EINVAL,
  339. };
  340. return rows[row & 0x7];
  341. }
  342. static inline int numcol(u32 col)
  343. {
  344. static int cols[8] = {
  345. 1 << 10, 1 << 11, 1 << 12, -EINVAL,
  346. };
  347. return cols[col & 0x3];
  348. }
  349. static struct i7core_dev *get_i7core_dev(u8 socket)
  350. {
  351. struct i7core_dev *i7core_dev;
  352. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  353. if (i7core_dev->socket == socket)
  354. return i7core_dev;
  355. }
  356. return NULL;
  357. }
  358. /****************************************************************************
  359. Memory check routines
  360. ****************************************************************************/
  361. static struct pci_dev *get_pdev_slot_func(u8 socket, unsigned slot,
  362. unsigned func)
  363. {
  364. struct i7core_dev *i7core_dev = get_i7core_dev(socket);
  365. int i;
  366. if (!i7core_dev)
  367. return NULL;
  368. for (i = 0; i < i7core_dev->n_devs; i++) {
  369. if (!i7core_dev->pdev[i])
  370. continue;
  371. if (PCI_SLOT(i7core_dev->pdev[i]->devfn) == slot &&
  372. PCI_FUNC(i7core_dev->pdev[i]->devfn) == func) {
  373. return i7core_dev->pdev[i];
  374. }
  375. }
  376. return NULL;
  377. }
  378. /**
  379. * i7core_get_active_channels() - gets the number of channels and csrows
  380. * @socket: Quick Path Interconnect socket
  381. * @channels: Number of channels that will be returned
  382. * @csrows: Number of csrows found
  383. *
  384. * Since EDAC core needs to know in advance the number of available channels
  385. * and csrows, in order to allocate memory for csrows/channels, it is needed
  386. * to run two similar steps. At the first step, implemented on this function,
  387. * it checks the number of csrows/channels present at one socket.
  388. * this is used in order to properly allocate the size of mci components.
  389. *
  390. * It should be noticed that none of the current available datasheets explain
  391. * or even mention how csrows are seen by the memory controller. So, we need
  392. * to add a fake description for csrows.
  393. * So, this driver is attributing one DIMM memory for one csrow.
  394. */
  395. static int i7core_get_active_channels(u8 socket, unsigned *channels,
  396. unsigned *csrows)
  397. {
  398. struct pci_dev *pdev = NULL;
  399. int i, j;
  400. u32 status, control;
  401. *channels = 0;
  402. *csrows = 0;
  403. pdev = get_pdev_slot_func(socket, 3, 0);
  404. if (!pdev) {
  405. i7core_printk(KERN_ERR, "Couldn't find socket %d fn 3.0!!!\n",
  406. socket);
  407. return -ENODEV;
  408. }
  409. /* Device 3 function 0 reads */
  410. pci_read_config_dword(pdev, MC_STATUS, &status);
  411. pci_read_config_dword(pdev, MC_CONTROL, &control);
  412. for (i = 0; i < NUM_CHANS; i++) {
  413. u32 dimm_dod[3];
  414. /* Check if the channel is active */
  415. if (!(control & (1 << (8 + i))))
  416. continue;
  417. /* Check if the channel is disabled */
  418. if (status & (1 << i))
  419. continue;
  420. pdev = get_pdev_slot_func(socket, i + 4, 1);
  421. if (!pdev) {
  422. i7core_printk(KERN_ERR, "Couldn't find socket %d "
  423. "fn %d.%d!!!\n",
  424. socket, i + 4, 1);
  425. return -ENODEV;
  426. }
  427. /* Devices 4-6 function 1 */
  428. pci_read_config_dword(pdev,
  429. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  430. pci_read_config_dword(pdev,
  431. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  432. pci_read_config_dword(pdev,
  433. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  434. (*channels)++;
  435. for (j = 0; j < 3; j++) {
  436. if (!DIMM_PRESENT(dimm_dod[j]))
  437. continue;
  438. (*csrows)++;
  439. }
  440. }
  441. debugf0("Number of active channels on socket %d: %d\n",
  442. socket, *channels);
  443. return 0;
  444. }
  445. static int get_dimm_config(struct mem_ctl_info *mci, int *csrow)
  446. {
  447. struct i7core_pvt *pvt = mci->pvt_info;
  448. struct csrow_info *csr;
  449. struct pci_dev *pdev;
  450. int i, j;
  451. unsigned long last_page = 0;
  452. enum edac_type mode;
  453. enum mem_type mtype;
  454. /* Get data from the MC register, function 0 */
  455. pdev = pvt->pci_mcr[0];
  456. if (!pdev)
  457. return -ENODEV;
  458. /* Device 3 function 0 reads */
  459. pci_read_config_dword(pdev, MC_CONTROL, &pvt->info.mc_control);
  460. pci_read_config_dword(pdev, MC_STATUS, &pvt->info.mc_status);
  461. pci_read_config_dword(pdev, MC_MAX_DOD, &pvt->info.max_dod);
  462. pci_read_config_dword(pdev, MC_CHANNEL_MAPPER, &pvt->info.ch_map);
  463. debugf0("QPI %d control=0x%08x status=0x%08x dod=0x%08x map=0x%08x\n",
  464. pvt->i7core_dev->socket, pvt->info.mc_control, pvt->info.mc_status,
  465. pvt->info.max_dod, pvt->info.ch_map);
  466. if (ECC_ENABLED(pvt)) {
  467. debugf0("ECC enabled with x%d SDCC\n", ECCx8(pvt) ? 8 : 4);
  468. if (ECCx8(pvt))
  469. mode = EDAC_S8ECD8ED;
  470. else
  471. mode = EDAC_S4ECD4ED;
  472. } else {
  473. debugf0("ECC disabled\n");
  474. mode = EDAC_NONE;
  475. }
  476. /* FIXME: need to handle the error codes */
  477. debugf0("DOD Max limits: DIMMS: %d, %d-ranked, %d-banked "
  478. "x%x x 0x%x\n",
  479. numdimms(pvt->info.max_dod),
  480. numrank(pvt->info.max_dod >> 2),
  481. numbank(pvt->info.max_dod >> 4),
  482. numrow(pvt->info.max_dod >> 6),
  483. numcol(pvt->info.max_dod >> 9));
  484. for (i = 0; i < NUM_CHANS; i++) {
  485. u32 data, dimm_dod[3], value[8];
  486. if (!pvt->pci_ch[i][0])
  487. continue;
  488. if (!CH_ACTIVE(pvt, i)) {
  489. debugf0("Channel %i is not active\n", i);
  490. continue;
  491. }
  492. if (CH_DISABLED(pvt, i)) {
  493. debugf0("Channel %i is disabled\n", i);
  494. continue;
  495. }
  496. /* Devices 4-6 function 0 */
  497. pci_read_config_dword(pvt->pci_ch[i][0],
  498. MC_CHANNEL_DIMM_INIT_PARAMS, &data);
  499. pvt->channel[i].ranks = (data & QUAD_RANK_PRESENT) ?
  500. 4 : 2;
  501. if (data & REGISTERED_DIMM)
  502. mtype = MEM_RDDR3;
  503. else
  504. mtype = MEM_DDR3;
  505. #if 0
  506. if (data & THREE_DIMMS_PRESENT)
  507. pvt->channel[i].dimms = 3;
  508. else if (data & SINGLE_QUAD_RANK_PRESENT)
  509. pvt->channel[i].dimms = 1;
  510. else
  511. pvt->channel[i].dimms = 2;
  512. #endif
  513. /* Devices 4-6 function 1 */
  514. pci_read_config_dword(pvt->pci_ch[i][1],
  515. MC_DOD_CH_DIMM0, &dimm_dod[0]);
  516. pci_read_config_dword(pvt->pci_ch[i][1],
  517. MC_DOD_CH_DIMM1, &dimm_dod[1]);
  518. pci_read_config_dword(pvt->pci_ch[i][1],
  519. MC_DOD_CH_DIMM2, &dimm_dod[2]);
  520. debugf0("Ch%d phy rd%d, wr%d (0x%08x): "
  521. "%d ranks, %cDIMMs\n",
  522. i,
  523. RDLCH(pvt->info.ch_map, i), WRLCH(pvt->info.ch_map, i),
  524. data,
  525. pvt->channel[i].ranks,
  526. (data & REGISTERED_DIMM) ? 'R' : 'U');
  527. for (j = 0; j < 3; j++) {
  528. u32 banks, ranks, rows, cols;
  529. u32 size, npages;
  530. if (!DIMM_PRESENT(dimm_dod[j]))
  531. continue;
  532. banks = numbank(MC_DOD_NUMBANK(dimm_dod[j]));
  533. ranks = numrank(MC_DOD_NUMRANK(dimm_dod[j]));
  534. rows = numrow(MC_DOD_NUMROW(dimm_dod[j]));
  535. cols = numcol(MC_DOD_NUMCOL(dimm_dod[j]));
  536. /* DDR3 has 8 I/O banks */
  537. size = (rows * cols * banks * ranks) >> (20 - 3);
  538. pvt->channel[i].dimms++;
  539. debugf0("\tdimm %d %d Mb offset: %x, "
  540. "bank: %d, rank: %d, row: %#x, col: %#x\n",
  541. j, size,
  542. RANKOFFSET(dimm_dod[j]),
  543. banks, ranks, rows, cols);
  544. #if PAGE_SHIFT > 20
  545. npages = size >> (PAGE_SHIFT - 20);
  546. #else
  547. npages = size << (20 - PAGE_SHIFT);
  548. #endif
  549. csr = &mci->csrows[*csrow];
  550. csr->first_page = last_page + 1;
  551. last_page += npages;
  552. csr->last_page = last_page;
  553. csr->nr_pages = npages;
  554. csr->page_mask = 0;
  555. csr->grain = 8;
  556. csr->csrow_idx = *csrow;
  557. csr->nr_channels = 1;
  558. csr->channels[0].chan_idx = i;
  559. csr->channels[0].ce_count = 0;
  560. pvt->csrow_map[i][j] = *csrow;
  561. switch (banks) {
  562. case 4:
  563. csr->dtype = DEV_X4;
  564. break;
  565. case 8:
  566. csr->dtype = DEV_X8;
  567. break;
  568. case 16:
  569. csr->dtype = DEV_X16;
  570. break;
  571. default:
  572. csr->dtype = DEV_UNKNOWN;
  573. }
  574. csr->edac_mode = mode;
  575. csr->mtype = mtype;
  576. (*csrow)++;
  577. }
  578. pci_read_config_dword(pdev, MC_SAG_CH_0, &value[0]);
  579. pci_read_config_dword(pdev, MC_SAG_CH_1, &value[1]);
  580. pci_read_config_dword(pdev, MC_SAG_CH_2, &value[2]);
  581. pci_read_config_dword(pdev, MC_SAG_CH_3, &value[3]);
  582. pci_read_config_dword(pdev, MC_SAG_CH_4, &value[4]);
  583. pci_read_config_dword(pdev, MC_SAG_CH_5, &value[5]);
  584. pci_read_config_dword(pdev, MC_SAG_CH_6, &value[6]);
  585. pci_read_config_dword(pdev, MC_SAG_CH_7, &value[7]);
  586. debugf1("\t[%i] DIVBY3\tREMOVED\tOFFSET\n", i);
  587. for (j = 0; j < 8; j++)
  588. debugf1("\t\t%#x\t%#x\t%#x\n",
  589. (value[j] >> 27) & 0x1,
  590. (value[j] >> 24) & 0x7,
  591. (value[j] && ((1 << 24) - 1)));
  592. }
  593. return 0;
  594. }
  595. /****************************************************************************
  596. Error insertion routines
  597. ****************************************************************************/
  598. /* The i7core has independent error injection features per channel.
  599. However, to have a simpler code, we don't allow enabling error injection
  600. on more than one channel.
  601. Also, since a change at an inject parameter will be applied only at enable,
  602. we're disabling error injection on all write calls to the sysfs nodes that
  603. controls the error code injection.
  604. */
  605. static int disable_inject(struct mem_ctl_info *mci)
  606. {
  607. struct i7core_pvt *pvt = mci->pvt_info;
  608. pvt->inject.enable = 0;
  609. if (!pvt->pci_ch[pvt->inject.channel][0])
  610. return -ENODEV;
  611. pci_write_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  612. MC_CHANNEL_ERROR_INJECT, 0);
  613. return 0;
  614. }
  615. /*
  616. * i7core inject inject.section
  617. *
  618. * accept and store error injection inject.section value
  619. * bit 0 - refers to the lower 32-byte half cacheline
  620. * bit 1 - refers to the upper 32-byte half cacheline
  621. */
  622. static ssize_t i7core_inject_section_store(struct mem_ctl_info *mci,
  623. const char *data, size_t count)
  624. {
  625. struct i7core_pvt *pvt = mci->pvt_info;
  626. unsigned long value;
  627. int rc;
  628. if (pvt->inject.enable)
  629. disable_inject(mci);
  630. rc = strict_strtoul(data, 10, &value);
  631. if ((rc < 0) || (value > 3))
  632. return -EIO;
  633. pvt->inject.section = (u32) value;
  634. return count;
  635. }
  636. static ssize_t i7core_inject_section_show(struct mem_ctl_info *mci,
  637. char *data)
  638. {
  639. struct i7core_pvt *pvt = mci->pvt_info;
  640. return sprintf(data, "0x%08x\n", pvt->inject.section);
  641. }
  642. /*
  643. * i7core inject.type
  644. *
  645. * accept and store error injection inject.section value
  646. * bit 0 - repeat enable - Enable error repetition
  647. * bit 1 - inject ECC error
  648. * bit 2 - inject parity error
  649. */
  650. static ssize_t i7core_inject_type_store(struct mem_ctl_info *mci,
  651. const char *data, size_t count)
  652. {
  653. struct i7core_pvt *pvt = mci->pvt_info;
  654. unsigned long value;
  655. int rc;
  656. if (pvt->inject.enable)
  657. disable_inject(mci);
  658. rc = strict_strtoul(data, 10, &value);
  659. if ((rc < 0) || (value > 7))
  660. return -EIO;
  661. pvt->inject.type = (u32) value;
  662. return count;
  663. }
  664. static ssize_t i7core_inject_type_show(struct mem_ctl_info *mci,
  665. char *data)
  666. {
  667. struct i7core_pvt *pvt = mci->pvt_info;
  668. return sprintf(data, "0x%08x\n", pvt->inject.type);
  669. }
  670. /*
  671. * i7core_inject_inject.eccmask_store
  672. *
  673. * The type of error (UE/CE) will depend on the inject.eccmask value:
  674. * Any bits set to a 1 will flip the corresponding ECC bit
  675. * Correctable errors can be injected by flipping 1 bit or the bits within
  676. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  677. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  678. * uncorrectable error to be injected.
  679. */
  680. static ssize_t i7core_inject_eccmask_store(struct mem_ctl_info *mci,
  681. const char *data, size_t count)
  682. {
  683. struct i7core_pvt *pvt = mci->pvt_info;
  684. unsigned long value;
  685. int rc;
  686. if (pvt->inject.enable)
  687. disable_inject(mci);
  688. rc = strict_strtoul(data, 10, &value);
  689. if (rc < 0)
  690. return -EIO;
  691. pvt->inject.eccmask = (u32) value;
  692. return count;
  693. }
  694. static ssize_t i7core_inject_eccmask_show(struct mem_ctl_info *mci,
  695. char *data)
  696. {
  697. struct i7core_pvt *pvt = mci->pvt_info;
  698. return sprintf(data, "0x%08x\n", pvt->inject.eccmask);
  699. }
  700. /*
  701. * i7core_addrmatch
  702. *
  703. * The type of error (UE/CE) will depend on the inject.eccmask value:
  704. * Any bits set to a 1 will flip the corresponding ECC bit
  705. * Correctable errors can be injected by flipping 1 bit or the bits within
  706. * a symbol pair (2 consecutive aligned 8-bit pairs - i.e. 7:0 and 15:8 or
  707. * 23:16 and 31:24). Flipping bits in two symbol pairs will cause an
  708. * uncorrectable error to be injected.
  709. */
  710. #define DECLARE_ADDR_MATCH(param, limit) \
  711. static ssize_t i7core_inject_store_##param( \
  712. struct mem_ctl_info *mci, \
  713. const char *data, size_t count) \
  714. { \
  715. struct i7core_pvt *pvt; \
  716. long value; \
  717. int rc; \
  718. \
  719. debugf1("%s()\n", __func__); \
  720. pvt = mci->pvt_info; \
  721. \
  722. if (pvt->inject.enable) \
  723. disable_inject(mci); \
  724. \
  725. if (!strcasecmp(data, "any") || !strcasecmp(data, "any\n"))\
  726. value = -1; \
  727. else { \
  728. rc = strict_strtoul(data, 10, &value); \
  729. if ((rc < 0) || (value >= limit)) \
  730. return -EIO; \
  731. } \
  732. \
  733. pvt->inject.param = value; \
  734. \
  735. return count; \
  736. } \
  737. \
  738. static ssize_t i7core_inject_show_##param( \
  739. struct mem_ctl_info *mci, \
  740. char *data) \
  741. { \
  742. struct i7core_pvt *pvt; \
  743. \
  744. pvt = mci->pvt_info; \
  745. debugf1("%s() pvt=%p\n", __func__, pvt); \
  746. if (pvt->inject.param < 0) \
  747. return sprintf(data, "any\n"); \
  748. else \
  749. return sprintf(data, "%d\n", pvt->inject.param);\
  750. }
  751. #define ATTR_ADDR_MATCH(param) \
  752. { \
  753. .attr = { \
  754. .name = #param, \
  755. .mode = (S_IRUGO | S_IWUSR) \
  756. }, \
  757. .show = i7core_inject_show_##param, \
  758. .store = i7core_inject_store_##param, \
  759. }
  760. DECLARE_ADDR_MATCH(channel, 3);
  761. DECLARE_ADDR_MATCH(dimm, 3);
  762. DECLARE_ADDR_MATCH(rank, 4);
  763. DECLARE_ADDR_MATCH(bank, 32);
  764. DECLARE_ADDR_MATCH(page, 0x10000);
  765. DECLARE_ADDR_MATCH(col, 0x4000);
  766. static int write_and_test(struct pci_dev *dev, int where, u32 val)
  767. {
  768. u32 read;
  769. int count;
  770. debugf0("setting pci %02x:%02x.%x reg=%02x value=%08x\n",
  771. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  772. where, val);
  773. for (count = 0; count < 10; count++) {
  774. if (count)
  775. msleep(100);
  776. pci_write_config_dword(dev, where, val);
  777. pci_read_config_dword(dev, where, &read);
  778. if (read == val)
  779. return 0;
  780. }
  781. i7core_printk(KERN_ERR, "Error during set pci %02x:%02x.%x reg=%02x "
  782. "write=%08x. Read=%08x\n",
  783. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
  784. where, val, read);
  785. return -EINVAL;
  786. }
  787. /*
  788. * This routine prepares the Memory Controller for error injection.
  789. * The error will be injected when some process tries to write to the
  790. * memory that matches the given criteria.
  791. * The criteria can be set in terms of a mask where dimm, rank, bank, page
  792. * and col can be specified.
  793. * A -1 value for any of the mask items will make the MCU to ignore
  794. * that matching criteria for error injection.
  795. *
  796. * It should be noticed that the error will only happen after a write operation
  797. * on a memory that matches the condition. if REPEAT_EN is not enabled at
  798. * inject mask, then it will produce just one error. Otherwise, it will repeat
  799. * until the injectmask would be cleaned.
  800. *
  801. * FIXME: This routine assumes that MAXNUMDIMMS value of MC_MAX_DOD
  802. * is reliable enough to check if the MC is using the
  803. * three channels. However, this is not clear at the datasheet.
  804. */
  805. static ssize_t i7core_inject_enable_store(struct mem_ctl_info *mci,
  806. const char *data, size_t count)
  807. {
  808. struct i7core_pvt *pvt = mci->pvt_info;
  809. u32 injectmask;
  810. u64 mask = 0;
  811. int rc;
  812. long enable;
  813. if (!pvt->pci_ch[pvt->inject.channel][0])
  814. return 0;
  815. rc = strict_strtoul(data, 10, &enable);
  816. if ((rc < 0))
  817. return 0;
  818. if (enable) {
  819. pvt->inject.enable = 1;
  820. } else {
  821. disable_inject(mci);
  822. return count;
  823. }
  824. /* Sets pvt->inject.dimm mask */
  825. if (pvt->inject.dimm < 0)
  826. mask |= 1LL << 41;
  827. else {
  828. if (pvt->channel[pvt->inject.channel].dimms > 2)
  829. mask |= (pvt->inject.dimm & 0x3LL) << 35;
  830. else
  831. mask |= (pvt->inject.dimm & 0x1LL) << 36;
  832. }
  833. /* Sets pvt->inject.rank mask */
  834. if (pvt->inject.rank < 0)
  835. mask |= 1LL << 40;
  836. else {
  837. if (pvt->channel[pvt->inject.channel].dimms > 2)
  838. mask |= (pvt->inject.rank & 0x1LL) << 34;
  839. else
  840. mask |= (pvt->inject.rank & 0x3LL) << 34;
  841. }
  842. /* Sets pvt->inject.bank mask */
  843. if (pvt->inject.bank < 0)
  844. mask |= 1LL << 39;
  845. else
  846. mask |= (pvt->inject.bank & 0x15LL) << 30;
  847. /* Sets pvt->inject.page mask */
  848. if (pvt->inject.page < 0)
  849. mask |= 1LL << 38;
  850. else
  851. mask |= (pvt->inject.page & 0xffff) << 14;
  852. /* Sets pvt->inject.column mask */
  853. if (pvt->inject.col < 0)
  854. mask |= 1LL << 37;
  855. else
  856. mask |= (pvt->inject.col & 0x3fff);
  857. /*
  858. * bit 0: REPEAT_EN
  859. * bits 1-2: MASK_HALF_CACHELINE
  860. * bit 3: INJECT_ECC
  861. * bit 4: INJECT_ADDR_PARITY
  862. */
  863. injectmask = (pvt->inject.type & 1) |
  864. (pvt->inject.section & 0x3) << 1 |
  865. (pvt->inject.type & 0x6) << (3 - 1);
  866. /* Unlock writes to registers - this register is write only */
  867. pci_write_config_dword(pvt->pci_noncore,
  868. MC_CFG_CONTROL, 0x2);
  869. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  870. MC_CHANNEL_ADDR_MATCH, mask);
  871. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  872. MC_CHANNEL_ADDR_MATCH + 4, mask >> 32L);
  873. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  874. MC_CHANNEL_ERROR_MASK, pvt->inject.eccmask);
  875. write_and_test(pvt->pci_ch[pvt->inject.channel][0],
  876. MC_CHANNEL_ERROR_INJECT, injectmask);
  877. /*
  878. * This is something undocumented, based on my tests
  879. * Without writing 8 to this register, errors aren't injected. Not sure
  880. * why.
  881. */
  882. pci_write_config_dword(pvt->pci_noncore,
  883. MC_CFG_CONTROL, 8);
  884. debugf0("Error inject addr match 0x%016llx, ecc 0x%08x,"
  885. " inject 0x%08x\n",
  886. mask, pvt->inject.eccmask, injectmask);
  887. return count;
  888. }
  889. static ssize_t i7core_inject_enable_show(struct mem_ctl_info *mci,
  890. char *data)
  891. {
  892. struct i7core_pvt *pvt = mci->pvt_info;
  893. u32 injectmask;
  894. if (!pvt->pci_ch[pvt->inject.channel][0])
  895. return 0;
  896. pci_read_config_dword(pvt->pci_ch[pvt->inject.channel][0],
  897. MC_CHANNEL_ERROR_INJECT, &injectmask);
  898. debugf0("Inject error read: 0x%018x\n", injectmask);
  899. if (injectmask & 0x0c)
  900. pvt->inject.enable = 1;
  901. return sprintf(data, "%d\n", pvt->inject.enable);
  902. }
  903. #define DECLARE_COUNTER(param) \
  904. static ssize_t i7core_show_counter_##param( \
  905. struct mem_ctl_info *mci, \
  906. char *data) \
  907. { \
  908. struct i7core_pvt *pvt = mci->pvt_info; \
  909. \
  910. debugf1("%s() \n", __func__); \
  911. if (!pvt->ce_count_available || (pvt->is_registered)) \
  912. return sprintf(data, "data unavailable\n"); \
  913. return sprintf(data, "%lu\n", \
  914. pvt->udimm_ce_count[param]); \
  915. }
  916. #define ATTR_COUNTER(param) \
  917. { \
  918. .attr = { \
  919. .name = __stringify(udimm##param), \
  920. .mode = (S_IRUGO | S_IWUSR) \
  921. }, \
  922. .show = i7core_show_counter_##param \
  923. }
  924. DECLARE_COUNTER(0);
  925. DECLARE_COUNTER(1);
  926. DECLARE_COUNTER(2);
  927. /*
  928. * Sysfs struct
  929. */
  930. static struct mcidev_sysfs_attribute i7core_addrmatch_attrs[] = {
  931. ATTR_ADDR_MATCH(channel),
  932. ATTR_ADDR_MATCH(dimm),
  933. ATTR_ADDR_MATCH(rank),
  934. ATTR_ADDR_MATCH(bank),
  935. ATTR_ADDR_MATCH(page),
  936. ATTR_ADDR_MATCH(col),
  937. { .attr = { .name = NULL } }
  938. };
  939. static struct mcidev_sysfs_group i7core_inject_addrmatch = {
  940. .name = "inject_addrmatch",
  941. .mcidev_attr = i7core_addrmatch_attrs,
  942. };
  943. static struct mcidev_sysfs_attribute i7core_udimm_counters_attrs[] = {
  944. ATTR_COUNTER(0),
  945. ATTR_COUNTER(1),
  946. ATTR_COUNTER(2),
  947. };
  948. static struct mcidev_sysfs_group i7core_udimm_counters = {
  949. .name = "all_channel_counts",
  950. .mcidev_attr = i7core_udimm_counters_attrs,
  951. };
  952. static struct mcidev_sysfs_attribute i7core_sysfs_attrs[] = {
  953. {
  954. .attr = {
  955. .name = "inject_section",
  956. .mode = (S_IRUGO | S_IWUSR)
  957. },
  958. .show = i7core_inject_section_show,
  959. .store = i7core_inject_section_store,
  960. }, {
  961. .attr = {
  962. .name = "inject_type",
  963. .mode = (S_IRUGO | S_IWUSR)
  964. },
  965. .show = i7core_inject_type_show,
  966. .store = i7core_inject_type_store,
  967. }, {
  968. .attr = {
  969. .name = "inject_eccmask",
  970. .mode = (S_IRUGO | S_IWUSR)
  971. },
  972. .show = i7core_inject_eccmask_show,
  973. .store = i7core_inject_eccmask_store,
  974. }, {
  975. .grp = &i7core_inject_addrmatch,
  976. }, {
  977. .attr = {
  978. .name = "inject_enable",
  979. .mode = (S_IRUGO | S_IWUSR)
  980. },
  981. .show = i7core_inject_enable_show,
  982. .store = i7core_inject_enable_store,
  983. },
  984. { .attr = { .name = NULL } }, /* Reserved for udimm counters */
  985. { .attr = { .name = NULL } }
  986. };
  987. /****************************************************************************
  988. Device initialization routines: put/get, init/exit
  989. ****************************************************************************/
  990. /*
  991. * i7core_put_devices 'put' all the devices that we have
  992. * reserved via 'get'
  993. */
  994. static void i7core_put_devices(struct i7core_dev *i7core_dev)
  995. {
  996. int i;
  997. debugf0(__FILE__ ": %s()\n", __func__);
  998. for (i = 0; i < i7core_dev->n_devs; i++) {
  999. struct pci_dev *pdev = i7core_dev->pdev[i];
  1000. if (!pdev)
  1001. continue;
  1002. debugf0("Removing dev %02x:%02x.%d\n",
  1003. pdev->bus->number,
  1004. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1005. pci_dev_put(pdev);
  1006. }
  1007. kfree(i7core_dev->pdev);
  1008. list_del(&i7core_dev->list);
  1009. kfree(i7core_dev);
  1010. }
  1011. static void i7core_put_all_devices(void)
  1012. {
  1013. struct i7core_dev *i7core_dev, *tmp;
  1014. list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list)
  1015. i7core_put_devices(i7core_dev);
  1016. }
  1017. static void __init i7core_xeon_pci_fixup(struct pci_id_table *table)
  1018. {
  1019. struct pci_dev *pdev = NULL;
  1020. int i;
  1021. /*
  1022. * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core pci buses
  1023. * aren't announced by acpi. So, we need to use a legacy scan probing
  1024. * to detect them
  1025. */
  1026. while (table && table->descr) {
  1027. pdev = pci_get_device(PCI_VENDOR_ID_INTEL, table->descr[0].dev_id, NULL);
  1028. if (unlikely(!pdev)) {
  1029. for (i = 0; i < MAX_SOCKET_BUSES; i++)
  1030. pcibios_scan_specific_bus(255-i);
  1031. }
  1032. pci_dev_put(pdev);
  1033. table++;
  1034. }
  1035. }
  1036. static unsigned i7core_pci_lastbus(void)
  1037. {
  1038. int last_bus = 0, bus;
  1039. struct pci_bus *b = NULL;
  1040. while ((b = pci_find_next_bus(b)) != NULL) {
  1041. bus = b->number;
  1042. debugf0("Found bus %d\n", bus);
  1043. if (bus > last_bus)
  1044. last_bus = bus;
  1045. }
  1046. debugf0("Last bus %d\n", last_bus);
  1047. return last_bus;
  1048. }
  1049. /*
  1050. * i7core_get_devices Find and perform 'get' operation on the MCH's
  1051. * device/functions we want to reference for this driver
  1052. *
  1053. * Need to 'get' device 16 func 1 and func 2
  1054. */
  1055. int i7core_get_onedevice(struct pci_dev **prev, int devno,
  1056. struct pci_id_descr *dev_descr, unsigned n_devs,
  1057. unsigned last_bus)
  1058. {
  1059. struct i7core_dev *i7core_dev;
  1060. struct pci_dev *pdev = NULL;
  1061. u8 bus = 0;
  1062. u8 socket = 0;
  1063. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1064. dev_descr->dev_id, *prev);
  1065. /*
  1066. * On Xeon 55xx, the Intel Quckpath Arch Generic Non-core regs
  1067. * is at addr 8086:2c40, instead of 8086:2c41. So, we need
  1068. * to probe for the alternate address in case of failure
  1069. */
  1070. if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_I7_NONCORE && !pdev)
  1071. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1072. PCI_DEVICE_ID_INTEL_I7_NONCORE_ALT, *prev);
  1073. if (dev_descr->dev_id == PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE && !pdev)
  1074. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1075. PCI_DEVICE_ID_INTEL_LYNNFIELD_NONCORE_ALT,
  1076. *prev);
  1077. if (!pdev) {
  1078. if (*prev) {
  1079. *prev = pdev;
  1080. return 0;
  1081. }
  1082. if (dev_descr->optional)
  1083. return 0;
  1084. if (devno == 0)
  1085. return -ENODEV;
  1086. i7core_printk(KERN_INFO,
  1087. "Device not found: dev %02x.%d PCI ID %04x:%04x\n",
  1088. dev_descr->dev, dev_descr->func,
  1089. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1090. /* End of list, leave */
  1091. return -ENODEV;
  1092. }
  1093. bus = pdev->bus->number;
  1094. socket = last_bus - bus;
  1095. i7core_dev = get_i7core_dev(socket);
  1096. if (!i7core_dev) {
  1097. i7core_dev = kzalloc(sizeof(*i7core_dev), GFP_KERNEL);
  1098. if (!i7core_dev)
  1099. return -ENOMEM;
  1100. i7core_dev->pdev = kzalloc(sizeof(*i7core_dev->pdev) * n_devs,
  1101. GFP_KERNEL);
  1102. if (!i7core_dev->pdev) {
  1103. kfree(i7core_dev);
  1104. return -ENOMEM;
  1105. }
  1106. i7core_dev->socket = socket;
  1107. i7core_dev->n_devs = n_devs;
  1108. list_add_tail(&i7core_dev->list, &i7core_edac_list);
  1109. }
  1110. if (i7core_dev->pdev[devno]) {
  1111. i7core_printk(KERN_ERR,
  1112. "Duplicated device for "
  1113. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1114. bus, dev_descr->dev, dev_descr->func,
  1115. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1116. pci_dev_put(pdev);
  1117. return -ENODEV;
  1118. }
  1119. i7core_dev->pdev[devno] = pdev;
  1120. /* Sanity check */
  1121. if (unlikely(PCI_SLOT(pdev->devfn) != dev_descr->dev ||
  1122. PCI_FUNC(pdev->devfn) != dev_descr->func)) {
  1123. i7core_printk(KERN_ERR,
  1124. "Device PCI ID %04x:%04x "
  1125. "has dev %02x:%02x.%d instead of dev %02x:%02x.%d\n",
  1126. PCI_VENDOR_ID_INTEL, dev_descr->dev_id,
  1127. bus, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1128. bus, dev_descr->dev, dev_descr->func);
  1129. return -ENODEV;
  1130. }
  1131. /* Be sure that the device is enabled */
  1132. if (unlikely(pci_enable_device(pdev) < 0)) {
  1133. i7core_printk(KERN_ERR,
  1134. "Couldn't enable "
  1135. "dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1136. bus, dev_descr->dev, dev_descr->func,
  1137. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1138. return -ENODEV;
  1139. }
  1140. debugf0("Detected socket %d dev %02x:%02x.%d PCI ID %04x:%04x\n",
  1141. socket, bus, dev_descr->dev,
  1142. dev_descr->func,
  1143. PCI_VENDOR_ID_INTEL, dev_descr->dev_id);
  1144. *prev = pdev;
  1145. return 0;
  1146. }
  1147. static int i7core_get_devices(struct pci_id_table *table)
  1148. {
  1149. int i, rc, last_bus;
  1150. struct pci_dev *pdev = NULL;
  1151. struct pci_id_descr *dev_descr;
  1152. last_bus = i7core_pci_lastbus();
  1153. while (table && table->descr) {
  1154. dev_descr = table->descr;
  1155. for (i = 0; i < table->n_devs; i++) {
  1156. pdev = NULL;
  1157. do {
  1158. rc = i7core_get_onedevice(&pdev, i,
  1159. &dev_descr[i],
  1160. table->n_devs,
  1161. last_bus);
  1162. if (rc < 0) {
  1163. if (i == 0) {
  1164. i = table->n_devs;
  1165. break;
  1166. }
  1167. i7core_put_all_devices();
  1168. return -ENODEV;
  1169. }
  1170. } while (pdev);
  1171. }
  1172. table++;
  1173. }
  1174. return 0;
  1175. return 0;
  1176. }
  1177. static int mci_bind_devs(struct mem_ctl_info *mci,
  1178. struct i7core_dev *i7core_dev)
  1179. {
  1180. struct i7core_pvt *pvt = mci->pvt_info;
  1181. struct pci_dev *pdev;
  1182. int i, func, slot;
  1183. /* Associates i7core_dev and mci for future usage */
  1184. pvt->i7core_dev = i7core_dev;
  1185. i7core_dev->mci = mci;
  1186. pvt->is_registered = 0;
  1187. for (i = 0; i < i7core_dev->n_devs; i++) {
  1188. pdev = i7core_dev->pdev[i];
  1189. if (!pdev)
  1190. continue;
  1191. func = PCI_FUNC(pdev->devfn);
  1192. slot = PCI_SLOT(pdev->devfn);
  1193. if (slot == 3) {
  1194. if (unlikely(func > MAX_MCR_FUNC))
  1195. goto error;
  1196. pvt->pci_mcr[func] = pdev;
  1197. } else if (likely(slot >= 4 && slot < 4 + NUM_CHANS)) {
  1198. if (unlikely(func > MAX_CHAN_FUNC))
  1199. goto error;
  1200. pvt->pci_ch[slot - 4][func] = pdev;
  1201. } else if (!slot && !func)
  1202. pvt->pci_noncore = pdev;
  1203. else
  1204. goto error;
  1205. debugf0("Associated fn %d.%d, dev = %p, socket %d\n",
  1206. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1207. pdev, i7core_dev->socket);
  1208. if (PCI_SLOT(pdev->devfn) == 3 &&
  1209. PCI_FUNC(pdev->devfn) == 2)
  1210. pvt->is_registered = 1;
  1211. }
  1212. /*
  1213. * Add extra nodes to count errors on udimm
  1214. * For registered memory, this is not needed, since the counters
  1215. * are already displayed at the standard locations
  1216. */
  1217. if (!pvt->is_registered)
  1218. i7core_sysfs_attrs[ARRAY_SIZE(i7core_sysfs_attrs)-2].grp =
  1219. &i7core_udimm_counters;
  1220. return 0;
  1221. error:
  1222. i7core_printk(KERN_ERR, "Device %d, function %d "
  1223. "is out of the expected range\n",
  1224. slot, func);
  1225. return -EINVAL;
  1226. }
  1227. /****************************************************************************
  1228. Error check routines
  1229. ****************************************************************************/
  1230. static void i7core_rdimm_update_csrow(struct mem_ctl_info *mci,
  1231. int chan, int dimm, int add)
  1232. {
  1233. char *msg;
  1234. struct i7core_pvt *pvt = mci->pvt_info;
  1235. int row = pvt->csrow_map[chan][dimm], i;
  1236. for (i = 0; i < add; i++) {
  1237. msg = kasprintf(GFP_KERNEL, "Corrected error "
  1238. "(Socket=%d channel=%d dimm=%d)",
  1239. pvt->i7core_dev->socket, chan, dimm);
  1240. edac_mc_handle_fbd_ce(mci, row, 0, msg);
  1241. kfree (msg);
  1242. }
  1243. }
  1244. static void i7core_rdimm_update_ce_count(struct mem_ctl_info *mci,
  1245. int chan, int new0, int new1, int new2)
  1246. {
  1247. struct i7core_pvt *pvt = mci->pvt_info;
  1248. int add0 = 0, add1 = 0, add2 = 0;
  1249. /* Updates CE counters if it is not the first time here */
  1250. if (pvt->ce_count_available) {
  1251. /* Updates CE counters */
  1252. add2 = new2 - pvt->rdimm_last_ce_count[chan][2];
  1253. add1 = new1 - pvt->rdimm_last_ce_count[chan][1];
  1254. add0 = new0 - pvt->rdimm_last_ce_count[chan][0];
  1255. if (add2 < 0)
  1256. add2 += 0x7fff;
  1257. pvt->rdimm_ce_count[chan][2] += add2;
  1258. if (add1 < 0)
  1259. add1 += 0x7fff;
  1260. pvt->rdimm_ce_count[chan][1] += add1;
  1261. if (add0 < 0)
  1262. add0 += 0x7fff;
  1263. pvt->rdimm_ce_count[chan][0] += add0;
  1264. } else
  1265. pvt->ce_count_available = 1;
  1266. /* Store the new values */
  1267. pvt->rdimm_last_ce_count[chan][2] = new2;
  1268. pvt->rdimm_last_ce_count[chan][1] = new1;
  1269. pvt->rdimm_last_ce_count[chan][0] = new0;
  1270. /*updated the edac core */
  1271. if (add0 != 0)
  1272. i7core_rdimm_update_csrow(mci, chan, 0, add0);
  1273. if (add1 != 0)
  1274. i7core_rdimm_update_csrow(mci, chan, 1, add1);
  1275. if (add2 != 0)
  1276. i7core_rdimm_update_csrow(mci, chan, 2, add2);
  1277. }
  1278. static void i7core_rdimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1279. {
  1280. struct i7core_pvt *pvt = mci->pvt_info;
  1281. u32 rcv[3][2];
  1282. int i, new0, new1, new2;
  1283. /*Read DEV 3: FUN 2: MC_COR_ECC_CNT regs directly*/
  1284. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_0,
  1285. &rcv[0][0]);
  1286. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_1,
  1287. &rcv[0][1]);
  1288. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_2,
  1289. &rcv[1][0]);
  1290. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_3,
  1291. &rcv[1][1]);
  1292. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_4,
  1293. &rcv[2][0]);
  1294. pci_read_config_dword(pvt->pci_mcr[2], MC_COR_ECC_CNT_5,
  1295. &rcv[2][1]);
  1296. for (i = 0 ; i < 3; i++) {
  1297. debugf3("MC_COR_ECC_CNT%d = 0x%x; MC_COR_ECC_CNT%d = 0x%x\n",
  1298. (i * 2), rcv[i][0], (i * 2) + 1, rcv[i][1]);
  1299. /*if the channel has 3 dimms*/
  1300. if (pvt->channel[i].dimms > 2) {
  1301. new0 = DIMM_BOT_COR_ERR(rcv[i][0]);
  1302. new1 = DIMM_TOP_COR_ERR(rcv[i][0]);
  1303. new2 = DIMM_BOT_COR_ERR(rcv[i][1]);
  1304. } else {
  1305. new0 = DIMM_TOP_COR_ERR(rcv[i][0]) +
  1306. DIMM_BOT_COR_ERR(rcv[i][0]);
  1307. new1 = DIMM_TOP_COR_ERR(rcv[i][1]) +
  1308. DIMM_BOT_COR_ERR(rcv[i][1]);
  1309. new2 = 0;
  1310. }
  1311. i7core_rdimm_update_ce_count(mci, i, new0, new1, new2);
  1312. }
  1313. }
  1314. /* This function is based on the device 3 function 4 registers as described on:
  1315. * Intel Xeon Processor 5500 Series Datasheet Volume 2
  1316. * http://www.intel.com/Assets/PDF/datasheet/321322.pdf
  1317. * also available at:
  1318. * http://www.arrownac.com/manufacturers/intel/s/nehalem/5500-datasheet-v2.pdf
  1319. */
  1320. static void i7core_udimm_check_mc_ecc_err(struct mem_ctl_info *mci)
  1321. {
  1322. struct i7core_pvt *pvt = mci->pvt_info;
  1323. u32 rcv1, rcv0;
  1324. int new0, new1, new2;
  1325. if (!pvt->pci_mcr[4]) {
  1326. debugf0("%s MCR registers not found\n", __func__);
  1327. return;
  1328. }
  1329. /* Corrected test errors */
  1330. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV1, &rcv1);
  1331. pci_read_config_dword(pvt->pci_mcr[4], MC_TEST_ERR_RCV0, &rcv0);
  1332. /* Store the new values */
  1333. new2 = DIMM2_COR_ERR(rcv1);
  1334. new1 = DIMM1_COR_ERR(rcv0);
  1335. new0 = DIMM0_COR_ERR(rcv0);
  1336. /* Updates CE counters if it is not the first time here */
  1337. if (pvt->ce_count_available) {
  1338. /* Updates CE counters */
  1339. int add0, add1, add2;
  1340. add2 = new2 - pvt->udimm_last_ce_count[2];
  1341. add1 = new1 - pvt->udimm_last_ce_count[1];
  1342. add0 = new0 - pvt->udimm_last_ce_count[0];
  1343. if (add2 < 0)
  1344. add2 += 0x7fff;
  1345. pvt->udimm_ce_count[2] += add2;
  1346. if (add1 < 0)
  1347. add1 += 0x7fff;
  1348. pvt->udimm_ce_count[1] += add1;
  1349. if (add0 < 0)
  1350. add0 += 0x7fff;
  1351. pvt->udimm_ce_count[0] += add0;
  1352. if (add0 | add1 | add2)
  1353. i7core_printk(KERN_ERR, "New Corrected error(s): "
  1354. "dimm0: +%d, dimm1: +%d, dimm2 +%d\n",
  1355. add0, add1, add2);
  1356. } else
  1357. pvt->ce_count_available = 1;
  1358. /* Store the new values */
  1359. pvt->udimm_last_ce_count[2] = new2;
  1360. pvt->udimm_last_ce_count[1] = new1;
  1361. pvt->udimm_last_ce_count[0] = new0;
  1362. }
  1363. /*
  1364. * According with tables E-11 and E-12 of chapter E.3.3 of Intel 64 and IA-32
  1365. * Architectures Software Developer’s Manual Volume 3B.
  1366. * Nehalem are defined as family 0x06, model 0x1a
  1367. *
  1368. * The MCA registers used here are the following ones:
  1369. * struct mce field MCA Register
  1370. * m->status MSR_IA32_MC8_STATUS
  1371. * m->addr MSR_IA32_MC8_ADDR
  1372. * m->misc MSR_IA32_MC8_MISC
  1373. * In the case of Nehalem, the error information is masked at .status and .misc
  1374. * fields
  1375. */
  1376. static void i7core_mce_output_error(struct mem_ctl_info *mci,
  1377. struct mce *m)
  1378. {
  1379. struct i7core_pvt *pvt = mci->pvt_info;
  1380. char *type, *optype, *err, *msg;
  1381. unsigned long error = m->status & 0x1ff0000l;
  1382. u32 optypenum = (m->status >> 4) & 0x07;
  1383. u32 core_err_cnt = (m->status >> 38) && 0x7fff;
  1384. u32 dimm = (m->misc >> 16) & 0x3;
  1385. u32 channel = (m->misc >> 18) & 0x3;
  1386. u32 syndrome = m->misc >> 32;
  1387. u32 errnum = find_first_bit(&error, 32);
  1388. int csrow;
  1389. if (m->mcgstatus & 1)
  1390. type = "FATAL";
  1391. else
  1392. type = "NON_FATAL";
  1393. switch (optypenum) {
  1394. case 0:
  1395. optype = "generic undef request";
  1396. break;
  1397. case 1:
  1398. optype = "read error";
  1399. break;
  1400. case 2:
  1401. optype = "write error";
  1402. break;
  1403. case 3:
  1404. optype = "addr/cmd error";
  1405. break;
  1406. case 4:
  1407. optype = "scrubbing error";
  1408. break;
  1409. default:
  1410. optype = "reserved";
  1411. break;
  1412. }
  1413. switch (errnum) {
  1414. case 16:
  1415. err = "read ECC error";
  1416. break;
  1417. case 17:
  1418. err = "RAS ECC error";
  1419. break;
  1420. case 18:
  1421. err = "write parity error";
  1422. break;
  1423. case 19:
  1424. err = "redundacy loss";
  1425. break;
  1426. case 20:
  1427. err = "reserved";
  1428. break;
  1429. case 21:
  1430. err = "memory range error";
  1431. break;
  1432. case 22:
  1433. err = "RTID out of range";
  1434. break;
  1435. case 23:
  1436. err = "address parity error";
  1437. break;
  1438. case 24:
  1439. err = "byte enable parity error";
  1440. break;
  1441. default:
  1442. err = "unknown";
  1443. }
  1444. /* FIXME: should convert addr into bank and rank information */
  1445. msg = kasprintf(GFP_ATOMIC,
  1446. "%s (addr = 0x%08llx, cpu=%d, Dimm=%d, Channel=%d, "
  1447. "syndrome=0x%08x, count=%d, Err=%08llx:%08llx (%s: %s))\n",
  1448. type, (long long) m->addr, m->cpu, dimm, channel,
  1449. syndrome, core_err_cnt, (long long)m->status,
  1450. (long long)m->misc, optype, err);
  1451. debugf0("%s", msg);
  1452. csrow = pvt->csrow_map[channel][dimm];
  1453. /* Call the helper to output message */
  1454. if (m->mcgstatus & 1)
  1455. edac_mc_handle_fbd_ue(mci, csrow, 0,
  1456. 0 /* FIXME: should be channel here */, msg);
  1457. else if (!pvt->is_registered)
  1458. edac_mc_handle_fbd_ce(mci, csrow,
  1459. 0 /* FIXME: should be channel here */, msg);
  1460. kfree(msg);
  1461. }
  1462. /*
  1463. * i7core_check_error Retrieve and process errors reported by the
  1464. * hardware. Called by the Core module.
  1465. */
  1466. static void i7core_check_error(struct mem_ctl_info *mci)
  1467. {
  1468. struct i7core_pvt *pvt = mci->pvt_info;
  1469. int i;
  1470. unsigned count = 0;
  1471. struct mce *m;
  1472. /*
  1473. * MCE first step: Copy all mce errors into a temporary buffer
  1474. * We use a double buffering here, to reduce the risk of
  1475. * loosing an error.
  1476. */
  1477. smp_rmb();
  1478. count = (pvt->mce_out + MCE_LOG_LEN - pvt->mce_in)
  1479. % MCE_LOG_LEN;
  1480. if (!count)
  1481. goto check_ce_error;
  1482. m = pvt->mce_outentry;
  1483. if (pvt->mce_in + count > MCE_LOG_LEN) {
  1484. unsigned l = MCE_LOG_LEN - pvt->mce_in;
  1485. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * l);
  1486. smp_wmb();
  1487. pvt->mce_in = 0;
  1488. count -= l;
  1489. m += l;
  1490. }
  1491. memcpy(m, &pvt->mce_entry[pvt->mce_in], sizeof(*m) * count);
  1492. smp_wmb();
  1493. pvt->mce_in += count;
  1494. smp_rmb();
  1495. if (pvt->mce_overrun) {
  1496. i7core_printk(KERN_ERR, "Lost %d memory errors\n",
  1497. pvt->mce_overrun);
  1498. smp_wmb();
  1499. pvt->mce_overrun = 0;
  1500. }
  1501. /*
  1502. * MCE second step: parse errors and display
  1503. */
  1504. for (i = 0; i < count; i++)
  1505. i7core_mce_output_error(mci, &pvt->mce_outentry[i]);
  1506. /*
  1507. * Now, let's increment CE error counts
  1508. */
  1509. check_ce_error:
  1510. if (!pvt->is_registered)
  1511. i7core_udimm_check_mc_ecc_err(mci);
  1512. else
  1513. i7core_rdimm_check_mc_ecc_err(mci);
  1514. }
  1515. /*
  1516. * i7core_mce_check_error Replicates mcelog routine to get errors
  1517. * This routine simply queues mcelog errors, and
  1518. * return. The error itself should be handled later
  1519. * by i7core_check_error.
  1520. * WARNING: As this routine should be called at NMI time, extra care should
  1521. * be taken to avoid deadlocks, and to be as fast as possible.
  1522. */
  1523. static int i7core_mce_check_error(void *priv, struct mce *mce)
  1524. {
  1525. struct mem_ctl_info *mci = priv;
  1526. struct i7core_pvt *pvt = mci->pvt_info;
  1527. /*
  1528. * Just let mcelog handle it if the error is
  1529. * outside the memory controller
  1530. */
  1531. if (((mce->status & 0xffff) >> 7) != 1)
  1532. return 0;
  1533. /* Bank 8 registers are the only ones that we know how to handle */
  1534. if (mce->bank != 8)
  1535. return 0;
  1536. #ifdef CONFIG_SMP
  1537. /* Only handle if it is the right mc controller */
  1538. if (cpu_data(mce->cpu).phys_proc_id != pvt->i7core_dev->socket)
  1539. return 0;
  1540. #endif
  1541. smp_rmb();
  1542. if ((pvt->mce_out + 1) % MCE_LOG_LEN == pvt->mce_in) {
  1543. smp_wmb();
  1544. pvt->mce_overrun++;
  1545. return 0;
  1546. }
  1547. /* Copy memory error at the ringbuffer */
  1548. memcpy(&pvt->mce_entry[pvt->mce_out], mce, sizeof(*mce));
  1549. smp_wmb();
  1550. pvt->mce_out = (pvt->mce_out + 1) % MCE_LOG_LEN;
  1551. /* Handle fatal errors immediately */
  1552. if (mce->mcgstatus & 1)
  1553. i7core_check_error(mci);
  1554. /* Advice mcelog that the error were handled */
  1555. return 1;
  1556. }
  1557. static int i7core_register_mci(struct i7core_dev *i7core_dev,
  1558. int num_channels, int num_csrows)
  1559. {
  1560. struct mem_ctl_info *mci;
  1561. struct i7core_pvt *pvt;
  1562. int csrow = 0;
  1563. int rc;
  1564. /* allocate a new MC control structure */
  1565. mci = edac_mc_alloc(sizeof(*pvt), num_csrows, num_channels,
  1566. i7core_dev->socket);
  1567. if (unlikely(!mci))
  1568. return -ENOMEM;
  1569. debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
  1570. /* record ptr to the generic device */
  1571. mci->dev = &i7core_dev->pdev[0]->dev;
  1572. pvt = mci->pvt_info;
  1573. memset(pvt, 0, sizeof(*pvt));
  1574. /*
  1575. * FIXME: how to handle RDDR3 at MCI level? It is possible to have
  1576. * Mixed RDDR3/UDDR3 with Nehalem, provided that they are on different
  1577. * memory channels
  1578. */
  1579. mci->mtype_cap = MEM_FLAG_DDR3;
  1580. mci->edac_ctl_cap = EDAC_FLAG_NONE;
  1581. mci->edac_cap = EDAC_FLAG_NONE;
  1582. mci->mod_name = "i7core_edac.c";
  1583. mci->mod_ver = I7CORE_REVISION;
  1584. mci->ctl_name = kasprintf(GFP_KERNEL, "i7 core #%d",
  1585. i7core_dev->socket);
  1586. mci->dev_name = pci_name(i7core_dev->pdev[0]);
  1587. mci->ctl_page_to_phys = NULL;
  1588. mci->mc_driver_sysfs_attributes = i7core_sysfs_attrs;
  1589. /* Set the function pointer to an actual operation function */
  1590. mci->edac_check = i7core_check_error;
  1591. /* Store pci devices at mci for faster access */
  1592. rc = mci_bind_devs(mci, i7core_dev);
  1593. if (unlikely(rc < 0))
  1594. goto fail;
  1595. /* Get dimm basic config */
  1596. get_dimm_config(mci, &csrow);
  1597. /* add this new MC control structure to EDAC's list of MCs */
  1598. if (unlikely(edac_mc_add_mc(mci))) {
  1599. debugf0("MC: " __FILE__
  1600. ": %s(): failed edac_mc_add_mc()\n", __func__);
  1601. /* FIXME: perhaps some code should go here that disables error
  1602. * reporting if we just enabled it
  1603. */
  1604. rc = -EINVAL;
  1605. goto fail;
  1606. }
  1607. /* allocating generic PCI control info */
  1608. i7core_pci = edac_pci_create_generic_ctl(&i7core_dev->pdev[0]->dev,
  1609. EDAC_MOD_STR);
  1610. if (unlikely(!i7core_pci)) {
  1611. printk(KERN_WARNING
  1612. "%s(): Unable to create PCI control\n",
  1613. __func__);
  1614. printk(KERN_WARNING
  1615. "%s(): PCI error report via EDAC not setup\n",
  1616. __func__);
  1617. }
  1618. /* Default error mask is any memory */
  1619. pvt->inject.channel = 0;
  1620. pvt->inject.dimm = -1;
  1621. pvt->inject.rank = -1;
  1622. pvt->inject.bank = -1;
  1623. pvt->inject.page = -1;
  1624. pvt->inject.col = -1;
  1625. /* Registers on edac_mce in order to receive memory errors */
  1626. pvt->edac_mce.priv = mci;
  1627. pvt->edac_mce.check_error = i7core_mce_check_error;
  1628. rc = edac_mce_register(&pvt->edac_mce);
  1629. if (unlikely(rc < 0)) {
  1630. debugf0("MC: " __FILE__
  1631. ": %s(): failed edac_mce_register()\n", __func__);
  1632. }
  1633. fail:
  1634. if (rc < 0)
  1635. edac_mc_free(mci);
  1636. return rc;
  1637. }
  1638. /*
  1639. * i7core_probe Probe for ONE instance of device to see if it is
  1640. * present.
  1641. * return:
  1642. * 0 for FOUND a device
  1643. * < 0 for error code
  1644. */
  1645. static int probed = 0;
  1646. static int __devinit i7core_probe(struct pci_dev *pdev,
  1647. const struct pci_device_id *id)
  1648. {
  1649. int rc;
  1650. struct i7core_dev *i7core_dev;
  1651. /* get the pci devices we want to reserve for our use */
  1652. mutex_lock(&i7core_edac_lock);
  1653. /*
  1654. * All memory controllers are allocated at the first pass.
  1655. */
  1656. if (unlikely(probed >= 1)) {
  1657. mutex_unlock(&i7core_edac_lock);
  1658. return -EINVAL;
  1659. }
  1660. probed++;
  1661. rc = i7core_get_devices(pci_dev_table);
  1662. if (unlikely(rc < 0))
  1663. goto fail0;
  1664. list_for_each_entry(i7core_dev, &i7core_edac_list, list) {
  1665. int channels;
  1666. int csrows;
  1667. /* Check the number of active and not disabled channels */
  1668. rc = i7core_get_active_channels(i7core_dev->socket,
  1669. &channels, &csrows);
  1670. if (unlikely(rc < 0))
  1671. goto fail1;
  1672. rc = i7core_register_mci(i7core_dev, channels, csrows);
  1673. if (unlikely(rc < 0))
  1674. goto fail1;
  1675. }
  1676. i7core_printk(KERN_INFO, "Driver loaded.\n");
  1677. mutex_unlock(&i7core_edac_lock);
  1678. return 0;
  1679. fail1:
  1680. i7core_put_all_devices();
  1681. fail0:
  1682. mutex_unlock(&i7core_edac_lock);
  1683. return rc;
  1684. }
  1685. /*
  1686. * i7core_remove destructor for one instance of device
  1687. *
  1688. */
  1689. static void __devexit i7core_remove(struct pci_dev *pdev)
  1690. {
  1691. struct mem_ctl_info *mci;
  1692. struct i7core_dev *i7core_dev, *tmp;
  1693. debugf0(__FILE__ ": %s()\n", __func__);
  1694. if (i7core_pci)
  1695. edac_pci_release_generic_ctl(i7core_pci);
  1696. /*
  1697. * we have a trouble here: pdev value for removal will be wrong, since
  1698. * it will point to the X58 register used to detect that the machine
  1699. * is a Nehalem or upper design. However, due to the way several PCI
  1700. * devices are grouped together to provide MC functionality, we need
  1701. * to use a different method for releasing the devices
  1702. */
  1703. mutex_lock(&i7core_edac_lock);
  1704. list_for_each_entry_safe(i7core_dev, tmp, &i7core_edac_list, list) {
  1705. mci = edac_mc_del_mc(&i7core_dev->pdev[0]->dev);
  1706. if (mci) {
  1707. struct i7core_pvt *pvt = mci->pvt_info;
  1708. i7core_dev = pvt->i7core_dev;
  1709. edac_mce_unregister(&pvt->edac_mce);
  1710. kfree(mci->ctl_name);
  1711. edac_mc_free(mci);
  1712. i7core_put_devices(i7core_dev);
  1713. } else {
  1714. i7core_printk(KERN_ERR,
  1715. "Couldn't find mci for socket %d\n",
  1716. i7core_dev->socket);
  1717. }
  1718. }
  1719. probed--;
  1720. mutex_unlock(&i7core_edac_lock);
  1721. }
  1722. MODULE_DEVICE_TABLE(pci, i7core_pci_tbl);
  1723. /*
  1724. * i7core_driver pci_driver structure for this module
  1725. *
  1726. */
  1727. static struct pci_driver i7core_driver = {
  1728. .name = "i7core_edac",
  1729. .probe = i7core_probe,
  1730. .remove = __devexit_p(i7core_remove),
  1731. .id_table = i7core_pci_tbl,
  1732. };
  1733. /*
  1734. * i7core_init Module entry function
  1735. * Try to initialize this module for its devices
  1736. */
  1737. static int __init i7core_init(void)
  1738. {
  1739. int pci_rc;
  1740. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1741. /* Ensure that the OPSTATE is set correctly for POLL or NMI */
  1742. opstate_init();
  1743. i7core_xeon_pci_fixup(pci_dev_table);
  1744. pci_rc = pci_register_driver(&i7core_driver);
  1745. if (pci_rc >= 0)
  1746. return 0;
  1747. i7core_printk(KERN_ERR, "Failed to register device with error %d.\n",
  1748. pci_rc);
  1749. return pci_rc;
  1750. }
  1751. /*
  1752. * i7core_exit() Module exit function
  1753. * Unregister the driver
  1754. */
  1755. static void __exit i7core_exit(void)
  1756. {
  1757. debugf2("MC: " __FILE__ ": %s()\n", __func__);
  1758. pci_unregister_driver(&i7core_driver);
  1759. }
  1760. module_init(i7core_init);
  1761. module_exit(i7core_exit);
  1762. MODULE_LICENSE("GPL");
  1763. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1764. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  1765. MODULE_DESCRIPTION("MC Driver for Intel i7 Core memory controllers - "
  1766. I7CORE_REVISION);
  1767. module_param(edac_op_state, int, 0444);
  1768. MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");