aerdrv_core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /*
  2. * drivers/pci/pcie/aer/aerdrv_core.c
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * This file implements the core part of PCI-Express AER. When an pci-express
  9. * error is delivered, an error message will be collected and printed to
  10. * console, then, an error recovery procedure will be executed by following
  11. * the pci error recovery rules.
  12. *
  13. * Copyright (C) 2006 Intel Corp.
  14. * Tom Long Nguyen (tom.l.nguyen@intel.com)
  15. * Zhang Yanmin (yanmin.zhang@intel.com)
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/pm.h>
  23. #include <linux/suspend.h>
  24. #include <linux/delay.h>
  25. #include "aerdrv.h"
  26. static int forceload;
  27. static int nosourceid;
  28. module_param(forceload, bool, 0);
  29. module_param(nosourceid, bool, 0);
  30. int pci_enable_pcie_error_reporting(struct pci_dev *dev)
  31. {
  32. u16 reg16 = 0;
  33. int pos;
  34. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  35. if (!pos)
  36. return -EIO;
  37. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  38. if (!pos)
  39. return -EIO;
  40. pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, &reg16);
  41. reg16 = reg16 |
  42. PCI_EXP_DEVCTL_CERE |
  43. PCI_EXP_DEVCTL_NFERE |
  44. PCI_EXP_DEVCTL_FERE |
  45. PCI_EXP_DEVCTL_URRE;
  46. pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
  47. return 0;
  48. }
  49. EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
  50. int pci_disable_pcie_error_reporting(struct pci_dev *dev)
  51. {
  52. u16 reg16 = 0;
  53. int pos;
  54. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  55. if (!pos)
  56. return -EIO;
  57. pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, &reg16);
  58. reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE |
  59. PCI_EXP_DEVCTL_NFERE |
  60. PCI_EXP_DEVCTL_FERE |
  61. PCI_EXP_DEVCTL_URRE);
  62. pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
  63. return 0;
  64. }
  65. EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
  66. int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
  67. {
  68. int pos;
  69. u32 status, mask;
  70. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  71. if (!pos)
  72. return -EIO;
  73. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  74. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  75. if (dev->error_state == pci_channel_io_normal)
  76. status &= ~mask; /* Clear corresponding nonfatal bits */
  77. else
  78. status &= mask; /* Clear corresponding fatal bits */
  79. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
  83. #if 0
  84. int pci_cleanup_aer_correct_error_status(struct pci_dev *dev)
  85. {
  86. int pos;
  87. u32 status;
  88. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  89. if (!pos)
  90. return -EIO;
  91. pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
  92. pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
  93. return 0;
  94. }
  95. #endif /* 0 */
  96. static int set_device_error_reporting(struct pci_dev *dev, void *data)
  97. {
  98. bool enable = *((bool *)data);
  99. if (dev->pcie_type == PCIE_RC_PORT ||
  100. dev->pcie_type == PCIE_SW_UPSTREAM_PORT ||
  101. dev->pcie_type == PCIE_SW_DOWNSTREAM_PORT) {
  102. if (enable)
  103. pci_enable_pcie_error_reporting(dev);
  104. else
  105. pci_disable_pcie_error_reporting(dev);
  106. }
  107. if (enable)
  108. pcie_set_ecrc_checking(dev);
  109. return 0;
  110. }
  111. /**
  112. * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
  113. * @dev: pointer to root port's pci_dev data structure
  114. * @enable: true = enable error reporting, false = disable error reporting.
  115. */
  116. static void set_downstream_devices_error_reporting(struct pci_dev *dev,
  117. bool enable)
  118. {
  119. set_device_error_reporting(dev, &enable);
  120. if (!dev->subordinate)
  121. return;
  122. pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
  123. }
  124. static inline int compare_device_id(struct pci_dev *dev,
  125. struct aer_err_info *e_info)
  126. {
  127. if (e_info->id == ((dev->bus->number << 8) | dev->devfn)) {
  128. /*
  129. * Device ID match
  130. */
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
  136. {
  137. if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
  138. e_info->dev[e_info->error_dev_num] = dev;
  139. e_info->error_dev_num++;
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. #define PCI_BUS(x) (((x) >> 8) & 0xff)
  145. static int find_device_iter(struct pci_dev *dev, void *data)
  146. {
  147. int pos;
  148. u32 status;
  149. u32 mask;
  150. u16 reg16;
  151. int result;
  152. struct aer_err_info *e_info = (struct aer_err_info *)data;
  153. /*
  154. * When bus id is equal to 0, it might be a bad id
  155. * reported by root port.
  156. */
  157. if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
  158. result = compare_device_id(dev, e_info);
  159. if (result)
  160. add_error_device(e_info, dev);
  161. /*
  162. * If there is no multiple error, we stop
  163. * or continue based on the id comparing.
  164. */
  165. if (!(e_info->flags & AER_MULTI_ERROR_VALID_FLAG))
  166. return result;
  167. /*
  168. * If there are multiple errors and id does match,
  169. * We need continue to search other devices under
  170. * the root port. Return 0 means that.
  171. */
  172. if (result)
  173. return 0;
  174. }
  175. /*
  176. * When either
  177. * 1) nosourceid==y;
  178. * 2) bus id is equal to 0. Some ports might lose the bus
  179. * id of error source id;
  180. * 3) There are multiple errors and prior id comparing fails;
  181. * We check AER status registers to find the initial reporter.
  182. */
  183. if (atomic_read(&dev->enable_cnt) == 0)
  184. return 0;
  185. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  186. if (!pos)
  187. return 0;
  188. /* Check if AER is enabled */
  189. pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, &reg16);
  190. if (!(reg16 & (
  191. PCI_EXP_DEVCTL_CERE |
  192. PCI_EXP_DEVCTL_NFERE |
  193. PCI_EXP_DEVCTL_FERE |
  194. PCI_EXP_DEVCTL_URRE)))
  195. return 0;
  196. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  197. if (!pos)
  198. return 0;
  199. status = 0;
  200. mask = 0;
  201. if (e_info->severity == AER_CORRECTABLE) {
  202. pci_read_config_dword(dev,
  203. pos + PCI_ERR_COR_STATUS,
  204. &status);
  205. pci_read_config_dword(dev,
  206. pos + PCI_ERR_COR_MASK,
  207. &mask);
  208. if (status & ERR_CORRECTABLE_ERROR_MASK & ~mask) {
  209. add_error_device(e_info, dev);
  210. goto added;
  211. }
  212. } else {
  213. pci_read_config_dword(dev,
  214. pos + PCI_ERR_UNCOR_STATUS,
  215. &status);
  216. pci_read_config_dword(dev,
  217. pos + PCI_ERR_UNCOR_MASK,
  218. &mask);
  219. if (status & ERR_UNCORRECTABLE_ERROR_MASK & ~mask) {
  220. add_error_device(e_info, dev);
  221. goto added;
  222. }
  223. }
  224. return 0;
  225. added:
  226. if (e_info->flags & AER_MULTI_ERROR_VALID_FLAG)
  227. return 0;
  228. else
  229. return 1;
  230. }
  231. /**
  232. * find_source_device - search through device hierarchy for source device
  233. * @parent: pointer to Root Port pci_dev data structure
  234. * @err_info: including detailed error information such like id
  235. *
  236. * Invoked when error is detected at the Root Port.
  237. */
  238. static void find_source_device(struct pci_dev *parent,
  239. struct aer_err_info *e_info)
  240. {
  241. struct pci_dev *dev = parent;
  242. int result;
  243. /* Is Root Port an agent that sends error message? */
  244. result = find_device_iter(dev, e_info);
  245. if (result)
  246. return;
  247. pci_walk_bus(parent->subordinate, find_device_iter, e_info);
  248. }
  249. static int report_error_detected(struct pci_dev *dev, void *data)
  250. {
  251. pci_ers_result_t vote;
  252. struct pci_error_handlers *err_handler;
  253. struct aer_broadcast_data *result_data;
  254. result_data = (struct aer_broadcast_data *) data;
  255. dev->error_state = result_data->state;
  256. if (!dev->driver ||
  257. !dev->driver->err_handler ||
  258. !dev->driver->err_handler->error_detected) {
  259. if (result_data->state == pci_channel_io_frozen &&
  260. !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
  261. /*
  262. * In case of fatal recovery, if one of down-
  263. * stream device has no driver. We might be
  264. * unable to recover because a later insmod
  265. * of a driver for this device is unaware of
  266. * its hw state.
  267. */
  268. dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
  269. dev->driver ?
  270. "no AER-aware driver" : "no driver");
  271. }
  272. return 0;
  273. }
  274. err_handler = dev->driver->err_handler;
  275. vote = err_handler->error_detected(dev, result_data->state);
  276. result_data->result = merge_result(result_data->result, vote);
  277. return 0;
  278. }
  279. static int report_mmio_enabled(struct pci_dev *dev, void *data)
  280. {
  281. pci_ers_result_t vote;
  282. struct pci_error_handlers *err_handler;
  283. struct aer_broadcast_data *result_data;
  284. result_data = (struct aer_broadcast_data *) data;
  285. if (!dev->driver ||
  286. !dev->driver->err_handler ||
  287. !dev->driver->err_handler->mmio_enabled)
  288. return 0;
  289. err_handler = dev->driver->err_handler;
  290. vote = err_handler->mmio_enabled(dev);
  291. result_data->result = merge_result(result_data->result, vote);
  292. return 0;
  293. }
  294. static int report_slot_reset(struct pci_dev *dev, void *data)
  295. {
  296. pci_ers_result_t vote;
  297. struct pci_error_handlers *err_handler;
  298. struct aer_broadcast_data *result_data;
  299. result_data = (struct aer_broadcast_data *) data;
  300. if (!dev->driver ||
  301. !dev->driver->err_handler ||
  302. !dev->driver->err_handler->slot_reset)
  303. return 0;
  304. err_handler = dev->driver->err_handler;
  305. vote = err_handler->slot_reset(dev);
  306. result_data->result = merge_result(result_data->result, vote);
  307. return 0;
  308. }
  309. static int report_resume(struct pci_dev *dev, void *data)
  310. {
  311. struct pci_error_handlers *err_handler;
  312. dev->error_state = pci_channel_io_normal;
  313. if (!dev->driver ||
  314. !dev->driver->err_handler ||
  315. !dev->driver->err_handler->resume)
  316. return 0;
  317. err_handler = dev->driver->err_handler;
  318. err_handler->resume(dev);
  319. return 0;
  320. }
  321. /**
  322. * broadcast_error_message - handle message broadcast to downstream drivers
  323. * @dev: pointer to from where in a hierarchy message is broadcasted down
  324. * @state: error state
  325. * @error_mesg: message to print
  326. * @cb: callback to be broadcasted
  327. *
  328. * Invoked during error recovery process. Once being invoked, the content
  329. * of error severity will be broadcasted to all downstream drivers in a
  330. * hierarchy in question.
  331. */
  332. static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
  333. enum pci_channel_state state,
  334. char *error_mesg,
  335. int (*cb)(struct pci_dev *, void *))
  336. {
  337. struct aer_broadcast_data result_data;
  338. dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
  339. result_data.state = state;
  340. if (cb == report_error_detected)
  341. result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
  342. else
  343. result_data.result = PCI_ERS_RESULT_RECOVERED;
  344. if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
  345. /*
  346. * If the error is reported by a bridge, we think this error
  347. * is related to the downstream link of the bridge, so we
  348. * do error recovery on all subordinates of the bridge instead
  349. * of the bridge and clear the error status of the bridge.
  350. */
  351. if (cb == report_error_detected)
  352. dev->error_state = state;
  353. pci_walk_bus(dev->subordinate, cb, &result_data);
  354. if (cb == report_resume) {
  355. pci_cleanup_aer_uncorrect_error_status(dev);
  356. dev->error_state = pci_channel_io_normal;
  357. }
  358. } else {
  359. /*
  360. * If the error is reported by an end point, we think this
  361. * error is related to the upstream link of the end point.
  362. */
  363. pci_walk_bus(dev->bus, cb, &result_data);
  364. }
  365. return result_data.result;
  366. }
  367. struct find_aer_service_data {
  368. struct pcie_port_service_driver *aer_driver;
  369. int is_downstream;
  370. };
  371. static int find_aer_service_iter(struct device *device, void *data)
  372. {
  373. struct device_driver *driver;
  374. struct pcie_port_service_driver *service_driver;
  375. struct find_aer_service_data *result;
  376. result = (struct find_aer_service_data *) data;
  377. if (device->bus == &pcie_port_bus_type) {
  378. struct pcie_port_data *port_data;
  379. port_data = pci_get_drvdata(to_pcie_device(device)->port);
  380. if (port_data->port_type == PCIE_SW_DOWNSTREAM_PORT)
  381. result->is_downstream = 1;
  382. driver = device->driver;
  383. if (driver) {
  384. service_driver = to_service_driver(driver);
  385. if (service_driver->service == PCIE_PORT_SERVICE_AER) {
  386. result->aer_driver = service_driver;
  387. return 1;
  388. }
  389. }
  390. }
  391. return 0;
  392. }
  393. static void find_aer_service(struct pci_dev *dev,
  394. struct find_aer_service_data *data)
  395. {
  396. int retval;
  397. retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
  398. }
  399. static pci_ers_result_t reset_link(struct pcie_device *aerdev,
  400. struct pci_dev *dev)
  401. {
  402. struct pci_dev *udev;
  403. pci_ers_result_t status;
  404. struct find_aer_service_data data;
  405. if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
  406. udev = dev;
  407. else
  408. udev = dev->bus->self;
  409. data.is_downstream = 0;
  410. data.aer_driver = NULL;
  411. find_aer_service(udev, &data);
  412. /*
  413. * Use the aer driver of the error agent firstly.
  414. * If it hasn't the aer driver, use the root port's
  415. */
  416. if (!data.aer_driver || !data.aer_driver->reset_link) {
  417. if (data.is_downstream &&
  418. aerdev->device.driver &&
  419. to_service_driver(aerdev->device.driver)->reset_link) {
  420. data.aer_driver =
  421. to_service_driver(aerdev->device.driver);
  422. } else {
  423. dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
  424. "support\n");
  425. return PCI_ERS_RESULT_DISCONNECT;
  426. }
  427. }
  428. status = data.aer_driver->reset_link(udev);
  429. if (status != PCI_ERS_RESULT_RECOVERED) {
  430. dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
  431. "device %s failed\n", pci_name(udev));
  432. return PCI_ERS_RESULT_DISCONNECT;
  433. }
  434. return status;
  435. }
  436. /**
  437. * do_recovery - handle nonfatal/fatal error recovery process
  438. * @aerdev: pointer to a pcie_device data structure of root port
  439. * @dev: pointer to a pci_dev data structure of agent detecting an error
  440. * @severity: error severity type
  441. *
  442. * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
  443. * error detected message to all downstream drivers within a hierarchy in
  444. * question and return the returned code.
  445. */
  446. static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
  447. struct pci_dev *dev,
  448. int severity)
  449. {
  450. pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
  451. enum pci_channel_state state;
  452. if (severity == AER_FATAL)
  453. state = pci_channel_io_frozen;
  454. else
  455. state = pci_channel_io_normal;
  456. status = broadcast_error_message(dev,
  457. state,
  458. "error_detected",
  459. report_error_detected);
  460. if (severity == AER_FATAL) {
  461. result = reset_link(aerdev, dev);
  462. if (result != PCI_ERS_RESULT_RECOVERED) {
  463. /* TODO: Should panic here? */
  464. return result;
  465. }
  466. }
  467. if (status == PCI_ERS_RESULT_CAN_RECOVER)
  468. status = broadcast_error_message(dev,
  469. state,
  470. "mmio_enabled",
  471. report_mmio_enabled);
  472. if (status == PCI_ERS_RESULT_NEED_RESET) {
  473. /*
  474. * TODO: Should call platform-specific
  475. * functions to reset slot before calling
  476. * drivers' slot_reset callbacks?
  477. */
  478. status = broadcast_error_message(dev,
  479. state,
  480. "slot_reset",
  481. report_slot_reset);
  482. }
  483. if (status == PCI_ERS_RESULT_RECOVERED)
  484. broadcast_error_message(dev,
  485. state,
  486. "resume",
  487. report_resume);
  488. return status;
  489. }
  490. /**
  491. * handle_error_source - handle logging error into an event log
  492. * @aerdev: pointer to pcie_device data structure of the root port
  493. * @dev: pointer to pci_dev data structure of error source device
  494. * @info: comprehensive error information
  495. *
  496. * Invoked when an error being detected by Root Port.
  497. */
  498. static void handle_error_source(struct pcie_device *aerdev,
  499. struct pci_dev *dev,
  500. struct aer_err_info *info)
  501. {
  502. pci_ers_result_t status = 0;
  503. int pos;
  504. if (info->severity == AER_CORRECTABLE) {
  505. /*
  506. * Correctable error does not need software intevention.
  507. * No need to go through error recovery process.
  508. */
  509. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  510. if (pos)
  511. pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
  512. info->status);
  513. } else {
  514. status = do_recovery(aerdev, dev, info->severity);
  515. if (status == PCI_ERS_RESULT_RECOVERED) {
  516. dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
  517. "successfully recovered\n");
  518. } else {
  519. /* TODO: Should kernel panic here? */
  520. dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
  521. "recover\n");
  522. }
  523. }
  524. }
  525. /**
  526. * aer_enable_rootport - enable Root Port's interrupts when receiving messages
  527. * @rpc: pointer to a Root Port data structure
  528. *
  529. * Invoked when PCIE bus loads AER service driver.
  530. */
  531. void aer_enable_rootport(struct aer_rpc *rpc)
  532. {
  533. struct pci_dev *pdev = rpc->rpd->port;
  534. int pos, aer_pos;
  535. u16 reg16;
  536. u32 reg32;
  537. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  538. /* Clear PCIE Capability's Device Status */
  539. pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, &reg16);
  540. pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
  541. /* Disable system error generation in response to error messages */
  542. pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, &reg16);
  543. reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
  544. pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
  545. aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  546. /* Clear error status */
  547. pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
  548. pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
  549. pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
  550. pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
  551. pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
  552. pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
  553. /*
  554. * Enable error reporting for the root port device and downstream port
  555. * devices.
  556. */
  557. set_downstream_devices_error_reporting(pdev, true);
  558. /* Enable Root Port's interrupt in response to error messages */
  559. pci_write_config_dword(pdev,
  560. aer_pos + PCI_ERR_ROOT_COMMAND,
  561. ROOT_PORT_INTR_ON_MESG_MASK);
  562. }
  563. /**
  564. * disable_root_aer - disable Root Port's interrupts when receiving messages
  565. * @rpc: pointer to a Root Port data structure
  566. *
  567. * Invoked when PCIE bus unloads AER service driver.
  568. */
  569. static void disable_root_aer(struct aer_rpc *rpc)
  570. {
  571. struct pci_dev *pdev = rpc->rpd->port;
  572. u32 reg32;
  573. int pos;
  574. /*
  575. * Disable error reporting for the root port device and downstream port
  576. * devices.
  577. */
  578. set_downstream_devices_error_reporting(pdev, false);
  579. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  580. /* Disable Root's interrupt in response to error messages */
  581. pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, 0);
  582. /* Clear Root's error status reg */
  583. pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
  584. pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
  585. }
  586. /**
  587. * get_e_source - retrieve an error source
  588. * @rpc: pointer to the root port which holds an error
  589. *
  590. * Invoked by DPC handler to consume an error.
  591. */
  592. static struct aer_err_source *get_e_source(struct aer_rpc *rpc)
  593. {
  594. struct aer_err_source *e_source;
  595. unsigned long flags;
  596. /* Lock access to Root error producer/consumer index */
  597. spin_lock_irqsave(&rpc->e_lock, flags);
  598. if (rpc->prod_idx == rpc->cons_idx) {
  599. spin_unlock_irqrestore(&rpc->e_lock, flags);
  600. return NULL;
  601. }
  602. e_source = &rpc->e_sources[rpc->cons_idx];
  603. rpc->cons_idx++;
  604. if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
  605. rpc->cons_idx = 0;
  606. spin_unlock_irqrestore(&rpc->e_lock, flags);
  607. return e_source;
  608. }
  609. static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
  610. {
  611. int pos;
  612. info->status = 0;
  613. info->flags &= ~AER_TLP_HEADER_VALID_FLAG;
  614. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  615. /* The device might not support AER */
  616. if (!pos)
  617. return AER_SUCCESS;
  618. if (info->severity == AER_CORRECTABLE) {
  619. pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
  620. &info->status);
  621. if (!(info->status & ERR_CORRECTABLE_ERROR_MASK))
  622. return AER_UNSUCCESS;
  623. } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
  624. info->severity == AER_NONFATAL) {
  625. /* Link is still healthy for IO reads */
  626. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
  627. &info->status);
  628. if (!(info->status & ERR_UNCORRECTABLE_ERROR_MASK))
  629. return AER_UNSUCCESS;
  630. if (info->status & AER_LOG_TLP_MASKS) {
  631. info->flags |= AER_TLP_HEADER_VALID_FLAG;
  632. pci_read_config_dword(dev,
  633. pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
  634. pci_read_config_dword(dev,
  635. pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
  636. pci_read_config_dword(dev,
  637. pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
  638. pci_read_config_dword(dev,
  639. pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
  640. }
  641. }
  642. return AER_SUCCESS;
  643. }
  644. static inline void aer_process_err_devices(struct pcie_device *p_device,
  645. struct aer_err_info *e_info)
  646. {
  647. int i;
  648. if (!e_info->dev[0]) {
  649. dev_printk(KERN_DEBUG, &p_device->port->dev,
  650. "can't find device of ID%04x\n",
  651. e_info->id);
  652. }
  653. for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
  654. if (get_device_error_info(e_info->dev[i], e_info) ==
  655. AER_SUCCESS) {
  656. aer_print_error(e_info->dev[i], e_info);
  657. handle_error_source(p_device,
  658. e_info->dev[i],
  659. e_info);
  660. }
  661. }
  662. }
  663. /**
  664. * aer_isr_one_error - consume an error detected by root port
  665. * @p_device: pointer to error root port service device
  666. * @e_src: pointer to an error source
  667. */
  668. static void aer_isr_one_error(struct pcie_device *p_device,
  669. struct aer_err_source *e_src)
  670. {
  671. struct aer_err_info *e_info;
  672. int i;
  673. /* struct aer_err_info might be big, so we allocate it with slab */
  674. e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
  675. if (e_info == NULL) {
  676. dev_printk(KERN_DEBUG, &p_device->port->dev,
  677. "Can't allocate mem when processing AER errors\n");
  678. return;
  679. }
  680. /*
  681. * There is a possibility that both correctable error and
  682. * uncorrectable error being logged. Report correctable error first.
  683. */
  684. for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
  685. if (i > 4)
  686. break;
  687. if (!(e_src->status & i))
  688. continue;
  689. memset(e_info, 0, sizeof(struct aer_err_info));
  690. /* Init comprehensive error information */
  691. if (i & PCI_ERR_ROOT_COR_RCV) {
  692. e_info->id = ERR_COR_ID(e_src->id);
  693. e_info->severity = AER_CORRECTABLE;
  694. } else {
  695. e_info->id = ERR_UNCOR_ID(e_src->id);
  696. e_info->severity = ((e_src->status >> 6) & 1);
  697. }
  698. if (e_src->status &
  699. (PCI_ERR_ROOT_MULTI_COR_RCV |
  700. PCI_ERR_ROOT_MULTI_UNCOR_RCV))
  701. e_info->flags |= AER_MULTI_ERROR_VALID_FLAG;
  702. find_source_device(p_device->port, e_info);
  703. aer_process_err_devices(p_device, e_info);
  704. }
  705. kfree(e_info);
  706. }
  707. /**
  708. * aer_isr - consume errors detected by root port
  709. * @work: definition of this work item
  710. *
  711. * Invoked, as DPC, when root port records new detected error
  712. */
  713. void aer_isr(struct work_struct *work)
  714. {
  715. struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
  716. struct pcie_device *p_device = rpc->rpd;
  717. struct aer_err_source *e_src;
  718. mutex_lock(&rpc->rpc_mutex);
  719. e_src = get_e_source(rpc);
  720. while (e_src) {
  721. aer_isr_one_error(p_device, e_src);
  722. e_src = get_e_source(rpc);
  723. }
  724. mutex_unlock(&rpc->rpc_mutex);
  725. wake_up(&rpc->wait_release);
  726. }
  727. /**
  728. * aer_delete_rootport - disable root port aer and delete service data
  729. * @rpc: pointer to a root port device being deleted
  730. *
  731. * Invoked when AER service unloaded on a specific Root Port
  732. */
  733. void aer_delete_rootport(struct aer_rpc *rpc)
  734. {
  735. /* Disable root port AER itself */
  736. disable_root_aer(rpc);
  737. kfree(rpc);
  738. }
  739. /**
  740. * aer_init - provide AER initialization
  741. * @dev: pointer to AER pcie device
  742. *
  743. * Invoked when AER service driver is loaded.
  744. */
  745. int aer_init(struct pcie_device *dev)
  746. {
  747. if (aer_osc_setup(dev) && !forceload)
  748. return -ENXIO;
  749. return AER_SUCCESS;
  750. }