aerdrv_core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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 <linux/slab.h>
  26. #include "aerdrv.h"
  27. static int forceload;
  28. static int nosourceid;
  29. module_param(forceload, bool, 0);
  30. module_param(nosourceid, bool, 0);
  31. int pci_enable_pcie_error_reporting(struct pci_dev *dev)
  32. {
  33. u16 reg16 = 0;
  34. int pos;
  35. if (dev->aer_firmware_first)
  36. return -EIO;
  37. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  38. if (!pos)
  39. return -EIO;
  40. pos = pci_pcie_cap(dev);
  41. if (!pos)
  42. return -EIO;
  43. pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, &reg16);
  44. reg16 = reg16 |
  45. PCI_EXP_DEVCTL_CERE |
  46. PCI_EXP_DEVCTL_NFERE |
  47. PCI_EXP_DEVCTL_FERE |
  48. PCI_EXP_DEVCTL_URRE;
  49. pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
  53. int pci_disable_pcie_error_reporting(struct pci_dev *dev)
  54. {
  55. u16 reg16 = 0;
  56. int pos;
  57. if (dev->aer_firmware_first)
  58. return -EIO;
  59. pos = pci_pcie_cap(dev);
  60. if (!pos)
  61. return -EIO;
  62. pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, &reg16);
  63. reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE |
  64. PCI_EXP_DEVCTL_NFERE |
  65. PCI_EXP_DEVCTL_FERE |
  66. PCI_EXP_DEVCTL_URRE);
  67. pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
  71. int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
  72. {
  73. int pos;
  74. u32 status;
  75. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  76. if (!pos)
  77. return -EIO;
  78. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  79. if (status)
  80. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  81. return 0;
  82. }
  83. EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
  84. /**
  85. * add_error_device - list device to be handled
  86. * @e_info: pointer to error info
  87. * @dev: pointer to pci_dev to be added
  88. */
  89. static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
  90. {
  91. if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
  92. e_info->dev[e_info->error_dev_num] = dev;
  93. e_info->error_dev_num++;
  94. return 0;
  95. }
  96. return -ENOSPC;
  97. }
  98. #define PCI_BUS(x) (((x) >> 8) & 0xff)
  99. /**
  100. * is_error_source - check whether the device is source of reported error
  101. * @dev: pointer to pci_dev to be checked
  102. * @e_info: pointer to reported error info
  103. */
  104. static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
  105. {
  106. int pos;
  107. u32 status, mask;
  108. u16 reg16;
  109. /*
  110. * When bus id is equal to 0, it might be a bad id
  111. * reported by root port.
  112. */
  113. if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
  114. /* Device ID match? */
  115. if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
  116. return true;
  117. /* Continue id comparing if there is no multiple error */
  118. if (!e_info->multi_error_valid)
  119. return false;
  120. }
  121. /*
  122. * When either
  123. * 1) nosourceid==y;
  124. * 2) bus id is equal to 0. Some ports might lose the bus
  125. * id of error source id;
  126. * 3) There are multiple errors and prior id comparing fails;
  127. * We check AER status registers to find possible reporter.
  128. */
  129. if (atomic_read(&dev->enable_cnt) == 0)
  130. return false;
  131. pos = pci_pcie_cap(dev);
  132. if (!pos)
  133. return false;
  134. /* Check if AER is enabled */
  135. pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &reg16);
  136. if (!(reg16 & (
  137. PCI_EXP_DEVCTL_CERE |
  138. PCI_EXP_DEVCTL_NFERE |
  139. PCI_EXP_DEVCTL_FERE |
  140. PCI_EXP_DEVCTL_URRE)))
  141. return false;
  142. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  143. if (!pos)
  144. return false;
  145. /* Check if error is recorded */
  146. if (e_info->severity == AER_CORRECTABLE) {
  147. pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
  148. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
  149. } else {
  150. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  151. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
  152. }
  153. if (status & ~mask)
  154. return true;
  155. return false;
  156. }
  157. static int find_device_iter(struct pci_dev *dev, void *data)
  158. {
  159. struct aer_err_info *e_info = (struct aer_err_info *)data;
  160. if (is_error_source(dev, e_info)) {
  161. /* List this device */
  162. if (add_error_device(e_info, dev)) {
  163. /* We cannot handle more... Stop iteration */
  164. /* TODO: Should print error message here? */
  165. return 1;
  166. }
  167. /* If there is only a single error, stop iteration */
  168. if (!e_info->multi_error_valid)
  169. return 1;
  170. }
  171. return 0;
  172. }
  173. /**
  174. * find_source_device - search through device hierarchy for source device
  175. * @parent: pointer to Root Port pci_dev data structure
  176. * @e_info: including detailed error information such like id
  177. *
  178. * Return true if found.
  179. *
  180. * Invoked by DPC when error is detected at the Root Port.
  181. * Caller of this function must set id, severity, and multi_error_valid of
  182. * struct aer_err_info pointed by @e_info properly. This function must fill
  183. * e_info->error_dev_num and e_info->dev[], based on the given information.
  184. */
  185. static bool find_source_device(struct pci_dev *parent,
  186. struct aer_err_info *e_info)
  187. {
  188. struct pci_dev *dev = parent;
  189. int result;
  190. /* Must reset in this function */
  191. e_info->error_dev_num = 0;
  192. /* Is Root Port an agent that sends error message? */
  193. result = find_device_iter(dev, e_info);
  194. if (result)
  195. return true;
  196. pci_walk_bus(parent->subordinate, find_device_iter, e_info);
  197. if (!e_info->error_dev_num) {
  198. dev_printk(KERN_DEBUG, &parent->dev,
  199. "can't find device of ID%04x\n",
  200. e_info->id);
  201. return false;
  202. }
  203. return true;
  204. }
  205. static int report_error_detected(struct pci_dev *dev, void *data)
  206. {
  207. pci_ers_result_t vote;
  208. struct pci_error_handlers *err_handler;
  209. struct aer_broadcast_data *result_data;
  210. result_data = (struct aer_broadcast_data *) data;
  211. dev->error_state = result_data->state;
  212. if (!dev->driver ||
  213. !dev->driver->err_handler ||
  214. !dev->driver->err_handler->error_detected) {
  215. if (result_data->state == pci_channel_io_frozen &&
  216. !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
  217. /*
  218. * In case of fatal recovery, if one of down-
  219. * stream device has no driver. We might be
  220. * unable to recover because a later insmod
  221. * of a driver for this device is unaware of
  222. * its hw state.
  223. */
  224. dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
  225. dev->driver ?
  226. "no AER-aware driver" : "no driver");
  227. }
  228. return 0;
  229. }
  230. err_handler = dev->driver->err_handler;
  231. vote = err_handler->error_detected(dev, result_data->state);
  232. result_data->result = merge_result(result_data->result, vote);
  233. return 0;
  234. }
  235. static int report_mmio_enabled(struct pci_dev *dev, void *data)
  236. {
  237. pci_ers_result_t vote;
  238. struct pci_error_handlers *err_handler;
  239. struct aer_broadcast_data *result_data;
  240. result_data = (struct aer_broadcast_data *) data;
  241. if (!dev->driver ||
  242. !dev->driver->err_handler ||
  243. !dev->driver->err_handler->mmio_enabled)
  244. return 0;
  245. err_handler = dev->driver->err_handler;
  246. vote = err_handler->mmio_enabled(dev);
  247. result_data->result = merge_result(result_data->result, vote);
  248. return 0;
  249. }
  250. static int report_slot_reset(struct pci_dev *dev, void *data)
  251. {
  252. pci_ers_result_t vote;
  253. struct pci_error_handlers *err_handler;
  254. struct aer_broadcast_data *result_data;
  255. result_data = (struct aer_broadcast_data *) data;
  256. if (!dev->driver ||
  257. !dev->driver->err_handler ||
  258. !dev->driver->err_handler->slot_reset)
  259. return 0;
  260. err_handler = dev->driver->err_handler;
  261. vote = err_handler->slot_reset(dev);
  262. result_data->result = merge_result(result_data->result, vote);
  263. return 0;
  264. }
  265. static int report_resume(struct pci_dev *dev, void *data)
  266. {
  267. struct pci_error_handlers *err_handler;
  268. dev->error_state = pci_channel_io_normal;
  269. if (!dev->driver ||
  270. !dev->driver->err_handler ||
  271. !dev->driver->err_handler->resume)
  272. return 0;
  273. err_handler = dev->driver->err_handler;
  274. err_handler->resume(dev);
  275. return 0;
  276. }
  277. /**
  278. * broadcast_error_message - handle message broadcast to downstream drivers
  279. * @dev: pointer to from where in a hierarchy message is broadcasted down
  280. * @state: error state
  281. * @error_mesg: message to print
  282. * @cb: callback to be broadcasted
  283. *
  284. * Invoked during error recovery process. Once being invoked, the content
  285. * of error severity will be broadcasted to all downstream drivers in a
  286. * hierarchy in question.
  287. */
  288. static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
  289. enum pci_channel_state state,
  290. char *error_mesg,
  291. int (*cb)(struct pci_dev *, void *))
  292. {
  293. struct aer_broadcast_data result_data;
  294. dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
  295. result_data.state = state;
  296. if (cb == report_error_detected)
  297. result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
  298. else
  299. result_data.result = PCI_ERS_RESULT_RECOVERED;
  300. if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
  301. /*
  302. * If the error is reported by a bridge, we think this error
  303. * is related to the downstream link of the bridge, so we
  304. * do error recovery on all subordinates of the bridge instead
  305. * of the bridge and clear the error status of the bridge.
  306. */
  307. if (cb == report_error_detected)
  308. dev->error_state = state;
  309. pci_walk_bus(dev->subordinate, cb, &result_data);
  310. if (cb == report_resume) {
  311. pci_cleanup_aer_uncorrect_error_status(dev);
  312. dev->error_state = pci_channel_io_normal;
  313. }
  314. } else {
  315. /*
  316. * If the error is reported by an end point, we think this
  317. * error is related to the upstream link of the end point.
  318. */
  319. pci_walk_bus(dev->bus, cb, &result_data);
  320. }
  321. return result_data.result;
  322. }
  323. struct find_aer_service_data {
  324. struct pcie_port_service_driver *aer_driver;
  325. };
  326. static int find_aer_service_iter(struct device *device, void *data)
  327. {
  328. struct pcie_port_service_driver *service_driver;
  329. struct find_aer_service_data *result;
  330. result = (struct find_aer_service_data *) data;
  331. if (device->bus == &pcie_port_bus_type && device->driver) {
  332. service_driver = to_service_driver(device->driver);
  333. if (service_driver->service == PCIE_PORT_SERVICE_AER) {
  334. result->aer_driver = service_driver;
  335. return 1;
  336. }
  337. }
  338. return 0;
  339. }
  340. static void find_aer_service(struct pci_dev *dev,
  341. struct find_aer_service_data *data)
  342. {
  343. int retval;
  344. retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
  345. }
  346. static pci_ers_result_t reset_link(struct pcie_device *aerdev,
  347. struct pci_dev *dev)
  348. {
  349. struct pci_dev *udev;
  350. pci_ers_result_t status;
  351. struct find_aer_service_data data;
  352. if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
  353. udev = dev;
  354. else
  355. udev = dev->bus->self;
  356. data.aer_driver = NULL;
  357. find_aer_service(udev, &data);
  358. /*
  359. * Use the aer driver of the error agent firstly.
  360. * If it hasn't the aer driver, use the root port's
  361. */
  362. if (!data.aer_driver || !data.aer_driver->reset_link) {
  363. if (udev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM &&
  364. aerdev->device.driver &&
  365. to_service_driver(aerdev->device.driver)->reset_link) {
  366. data.aer_driver =
  367. to_service_driver(aerdev->device.driver);
  368. } else {
  369. dev_printk(KERN_DEBUG, &dev->dev,
  370. "no link-reset support at upstream device %s\n",
  371. pci_name(udev));
  372. return PCI_ERS_RESULT_DISCONNECT;
  373. }
  374. }
  375. status = data.aer_driver->reset_link(udev);
  376. if (status != PCI_ERS_RESULT_RECOVERED) {
  377. dev_printk(KERN_DEBUG, &dev->dev,
  378. "link reset at upstream device %s failed\n",
  379. pci_name(udev));
  380. return PCI_ERS_RESULT_DISCONNECT;
  381. }
  382. return status;
  383. }
  384. /**
  385. * do_recovery - handle nonfatal/fatal error recovery process
  386. * @aerdev: pointer to a pcie_device data structure of root port
  387. * @dev: pointer to a pci_dev data structure of agent detecting an error
  388. * @severity: error severity type
  389. *
  390. * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
  391. * error detected message to all downstream drivers within a hierarchy in
  392. * question and return the returned code.
  393. */
  394. static void do_recovery(struct pcie_device *aerdev, struct pci_dev *dev,
  395. int severity)
  396. {
  397. pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
  398. enum pci_channel_state state;
  399. if (severity == AER_FATAL)
  400. state = pci_channel_io_frozen;
  401. else
  402. state = pci_channel_io_normal;
  403. status = broadcast_error_message(dev,
  404. state,
  405. "error_detected",
  406. report_error_detected);
  407. if (severity == AER_FATAL) {
  408. result = reset_link(aerdev, dev);
  409. if (result != PCI_ERS_RESULT_RECOVERED)
  410. goto failed;
  411. }
  412. if (status == PCI_ERS_RESULT_CAN_RECOVER)
  413. status = broadcast_error_message(dev,
  414. state,
  415. "mmio_enabled",
  416. report_mmio_enabled);
  417. if (status == PCI_ERS_RESULT_NEED_RESET) {
  418. /*
  419. * TODO: Should call platform-specific
  420. * functions to reset slot before calling
  421. * drivers' slot_reset callbacks?
  422. */
  423. status = broadcast_error_message(dev,
  424. state,
  425. "slot_reset",
  426. report_slot_reset);
  427. }
  428. if (status != PCI_ERS_RESULT_RECOVERED)
  429. goto failed;
  430. broadcast_error_message(dev,
  431. state,
  432. "resume",
  433. report_resume);
  434. dev_printk(KERN_DEBUG, &dev->dev,
  435. "AER driver successfully recovered\n");
  436. return;
  437. failed:
  438. /* TODO: Should kernel panic here? */
  439. dev_printk(KERN_DEBUG, &dev->dev,
  440. "AER driver didn't recover\n");
  441. }
  442. /**
  443. * handle_error_source - handle logging error into an event log
  444. * @aerdev: pointer to pcie_device data structure of the root port
  445. * @dev: pointer to pci_dev data structure of error source device
  446. * @info: comprehensive error information
  447. *
  448. * Invoked when an error being detected by Root Port.
  449. */
  450. static void handle_error_source(struct pcie_device *aerdev,
  451. struct pci_dev *dev,
  452. struct aer_err_info *info)
  453. {
  454. int pos;
  455. if (info->severity == AER_CORRECTABLE) {
  456. /*
  457. * Correctable error does not need software intevention.
  458. * No need to go through error recovery process.
  459. */
  460. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  461. if (pos)
  462. pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
  463. info->status);
  464. } else
  465. do_recovery(aerdev, dev, info->severity);
  466. }
  467. /**
  468. * get_device_error_info - read error status from dev and store it to info
  469. * @dev: pointer to the device expected to have a error record
  470. * @info: pointer to structure to store the error record
  471. *
  472. * Return 1 on success, 0 on error.
  473. *
  474. * Note that @info is reused among all error devices. Clear fields properly.
  475. */
  476. static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
  477. {
  478. int pos, temp;
  479. /* Must reset in this function */
  480. info->status = 0;
  481. info->tlp_header_valid = 0;
  482. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  483. /* The device might not support AER */
  484. if (!pos)
  485. return 1;
  486. if (info->severity == AER_CORRECTABLE) {
  487. pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
  488. &info->status);
  489. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
  490. &info->mask);
  491. if (!(info->status & ~info->mask))
  492. return 0;
  493. } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
  494. info->severity == AER_NONFATAL) {
  495. /* Link is still healthy for IO reads */
  496. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
  497. &info->status);
  498. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
  499. &info->mask);
  500. if (!(info->status & ~info->mask))
  501. return 0;
  502. /* Get First Error Pointer */
  503. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
  504. info->first_error = PCI_ERR_CAP_FEP(temp);
  505. if (info->status & AER_LOG_TLP_MASKS) {
  506. info->tlp_header_valid = 1;
  507. pci_read_config_dword(dev,
  508. pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
  509. pci_read_config_dword(dev,
  510. pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
  511. pci_read_config_dword(dev,
  512. pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
  513. pci_read_config_dword(dev,
  514. pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
  515. }
  516. }
  517. return 1;
  518. }
  519. static inline void aer_process_err_devices(struct pcie_device *p_device,
  520. struct aer_err_info *e_info)
  521. {
  522. int i;
  523. /* Report all before handle them, not to lost records by reset etc. */
  524. for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
  525. if (get_device_error_info(e_info->dev[i], e_info))
  526. aer_print_error(e_info->dev[i], e_info);
  527. }
  528. for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
  529. if (get_device_error_info(e_info->dev[i], e_info))
  530. handle_error_source(p_device, e_info->dev[i], e_info);
  531. }
  532. }
  533. /**
  534. * aer_isr_one_error - consume an error detected by root port
  535. * @p_device: pointer to error root port service device
  536. * @e_src: pointer to an error source
  537. */
  538. static void aer_isr_one_error(struct pcie_device *p_device,
  539. struct aer_err_source *e_src)
  540. {
  541. struct aer_err_info *e_info;
  542. /* struct aer_err_info might be big, so we allocate it with slab */
  543. e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
  544. if (!e_info) {
  545. dev_printk(KERN_DEBUG, &p_device->port->dev,
  546. "Can't allocate mem when processing AER errors\n");
  547. return;
  548. }
  549. /*
  550. * There is a possibility that both correctable error and
  551. * uncorrectable error being logged. Report correctable error first.
  552. */
  553. if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
  554. e_info->id = ERR_COR_ID(e_src->id);
  555. e_info->severity = AER_CORRECTABLE;
  556. if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
  557. e_info->multi_error_valid = 1;
  558. else
  559. e_info->multi_error_valid = 0;
  560. aer_print_port_info(p_device->port, e_info);
  561. if (find_source_device(p_device->port, e_info))
  562. aer_process_err_devices(p_device, e_info);
  563. }
  564. if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
  565. e_info->id = ERR_UNCOR_ID(e_src->id);
  566. if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
  567. e_info->severity = AER_FATAL;
  568. else
  569. e_info->severity = AER_NONFATAL;
  570. if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
  571. e_info->multi_error_valid = 1;
  572. else
  573. e_info->multi_error_valid = 0;
  574. aer_print_port_info(p_device->port, e_info);
  575. if (find_source_device(p_device->port, e_info))
  576. aer_process_err_devices(p_device, e_info);
  577. }
  578. kfree(e_info);
  579. }
  580. /**
  581. * get_e_source - retrieve an error source
  582. * @rpc: pointer to the root port which holds an error
  583. * @e_src: pointer to store retrieved error source
  584. *
  585. * Return 1 if an error source is retrieved, otherwise 0.
  586. *
  587. * Invoked by DPC handler to consume an error.
  588. */
  589. static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
  590. {
  591. unsigned long flags;
  592. int ret = 0;
  593. /* Lock access to Root error producer/consumer index */
  594. spin_lock_irqsave(&rpc->e_lock, flags);
  595. if (rpc->prod_idx != rpc->cons_idx) {
  596. *e_src = rpc->e_sources[rpc->cons_idx];
  597. rpc->cons_idx++;
  598. if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
  599. rpc->cons_idx = 0;
  600. ret = 1;
  601. }
  602. spin_unlock_irqrestore(&rpc->e_lock, flags);
  603. return ret;
  604. }
  605. /**
  606. * aer_isr - consume errors detected by root port
  607. * @work: definition of this work item
  608. *
  609. * Invoked, as DPC, when root port records new detected error
  610. */
  611. void aer_isr(struct work_struct *work)
  612. {
  613. struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
  614. struct pcie_device *p_device = rpc->rpd;
  615. struct aer_err_source e_src;
  616. mutex_lock(&rpc->rpc_mutex);
  617. while (get_e_source(rpc, &e_src))
  618. aer_isr_one_error(p_device, &e_src);
  619. mutex_unlock(&rpc->rpc_mutex);
  620. wake_up(&rpc->wait_release);
  621. }
  622. /**
  623. * aer_init - provide AER initialization
  624. * @dev: pointer to AER pcie device
  625. *
  626. * Invoked when AER service driver is loaded.
  627. */
  628. int aer_init(struct pcie_device *dev)
  629. {
  630. if (dev->port->aer_firmware_first) {
  631. dev_printk(KERN_DEBUG, &dev->device,
  632. "PCIe errors handled by platform firmware.\n");
  633. goto out;
  634. }
  635. if (aer_osc_setup(dev))
  636. goto out;
  637. return 0;
  638. out:
  639. if (forceload) {
  640. dev_printk(KERN_DEBUG, &dev->device,
  641. "aerdrv forceload requested.\n");
  642. dev->port->aer_firmware_first = 0;
  643. return 0;
  644. }
  645. return -ENXIO;
  646. }