aerdrv_core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. int is_downstream;
  326. };
  327. static int find_aer_service_iter(struct device *device, void *data)
  328. {
  329. struct device_driver *driver;
  330. struct pcie_port_service_driver *service_driver;
  331. struct find_aer_service_data *result;
  332. result = (struct find_aer_service_data *) data;
  333. if (device->bus == &pcie_port_bus_type) {
  334. struct pcie_device *pcie = to_pcie_device(device);
  335. if (pcie->port->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  336. result->is_downstream = 1;
  337. driver = device->driver;
  338. if (driver) {
  339. service_driver = to_service_driver(driver);
  340. if (service_driver->service == PCIE_PORT_SERVICE_AER) {
  341. result->aer_driver = service_driver;
  342. return 1;
  343. }
  344. }
  345. }
  346. return 0;
  347. }
  348. static void find_aer_service(struct pci_dev *dev,
  349. struct find_aer_service_data *data)
  350. {
  351. int retval;
  352. retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
  353. }
  354. static pci_ers_result_t reset_link(struct pcie_device *aerdev,
  355. struct pci_dev *dev)
  356. {
  357. struct pci_dev *udev;
  358. pci_ers_result_t status;
  359. struct find_aer_service_data data;
  360. if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
  361. udev = dev;
  362. else
  363. udev = dev->bus->self;
  364. data.is_downstream = 0;
  365. data.aer_driver = NULL;
  366. find_aer_service(udev, &data);
  367. /*
  368. * Use the aer driver of the error agent firstly.
  369. * If it hasn't the aer driver, use the root port's
  370. */
  371. if (!data.aer_driver || !data.aer_driver->reset_link) {
  372. if (data.is_downstream &&
  373. aerdev->device.driver &&
  374. to_service_driver(aerdev->device.driver)->reset_link) {
  375. data.aer_driver =
  376. to_service_driver(aerdev->device.driver);
  377. } else {
  378. dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
  379. "support\n");
  380. return PCI_ERS_RESULT_DISCONNECT;
  381. }
  382. }
  383. status = data.aer_driver->reset_link(udev);
  384. if (status != PCI_ERS_RESULT_RECOVERED) {
  385. dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
  386. "device %s failed\n", pci_name(udev));
  387. return PCI_ERS_RESULT_DISCONNECT;
  388. }
  389. return status;
  390. }
  391. /**
  392. * do_recovery - handle nonfatal/fatal error recovery process
  393. * @aerdev: pointer to a pcie_device data structure of root port
  394. * @dev: pointer to a pci_dev data structure of agent detecting an error
  395. * @severity: error severity type
  396. *
  397. * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
  398. * error detected message to all downstream drivers within a hierarchy in
  399. * question and return the returned code.
  400. */
  401. static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
  402. struct pci_dev *dev,
  403. int severity)
  404. {
  405. pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
  406. enum pci_channel_state state;
  407. if (severity == AER_FATAL)
  408. state = pci_channel_io_frozen;
  409. else
  410. state = pci_channel_io_normal;
  411. status = broadcast_error_message(dev,
  412. state,
  413. "error_detected",
  414. report_error_detected);
  415. if (severity == AER_FATAL) {
  416. result = reset_link(aerdev, dev);
  417. if (result != PCI_ERS_RESULT_RECOVERED) {
  418. /* TODO: Should panic here? */
  419. return result;
  420. }
  421. }
  422. if (status == PCI_ERS_RESULT_CAN_RECOVER)
  423. status = broadcast_error_message(dev,
  424. state,
  425. "mmio_enabled",
  426. report_mmio_enabled);
  427. if (status == PCI_ERS_RESULT_NEED_RESET) {
  428. /*
  429. * TODO: Should call platform-specific
  430. * functions to reset slot before calling
  431. * drivers' slot_reset callbacks?
  432. */
  433. status = broadcast_error_message(dev,
  434. state,
  435. "slot_reset",
  436. report_slot_reset);
  437. }
  438. if (status == PCI_ERS_RESULT_RECOVERED)
  439. broadcast_error_message(dev,
  440. state,
  441. "resume",
  442. report_resume);
  443. return status;
  444. }
  445. /**
  446. * handle_error_source - handle logging error into an event log
  447. * @aerdev: pointer to pcie_device data structure of the root port
  448. * @dev: pointer to pci_dev data structure of error source device
  449. * @info: comprehensive error information
  450. *
  451. * Invoked when an error being detected by Root Port.
  452. */
  453. static void handle_error_source(struct pcie_device *aerdev,
  454. struct pci_dev *dev,
  455. struct aer_err_info *info)
  456. {
  457. pci_ers_result_t status = 0;
  458. int pos;
  459. if (info->severity == AER_CORRECTABLE) {
  460. /*
  461. * Correctable error does not need software intevention.
  462. * No need to go through error recovery process.
  463. */
  464. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  465. if (pos)
  466. pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
  467. info->status);
  468. } else {
  469. status = do_recovery(aerdev, dev, info->severity);
  470. if (status == PCI_ERS_RESULT_RECOVERED) {
  471. dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
  472. "successfully recovered\n");
  473. } else {
  474. /* TODO: Should kernel panic here? */
  475. dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
  476. "recover\n");
  477. }
  478. }
  479. }
  480. /**
  481. * get_device_error_info - read error status from dev and store it to info
  482. * @dev: pointer to the device expected to have a error record
  483. * @info: pointer to structure to store the error record
  484. *
  485. * Return 1 on success, 0 on error.
  486. *
  487. * Note that @info is reused among all error devices. Clear fields properly.
  488. */
  489. static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
  490. {
  491. int pos, temp;
  492. /* Must reset in this function */
  493. info->status = 0;
  494. info->tlp_header_valid = 0;
  495. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  496. /* The device might not support AER */
  497. if (!pos)
  498. return 1;
  499. if (info->severity == AER_CORRECTABLE) {
  500. pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
  501. &info->status);
  502. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
  503. &info->mask);
  504. if (!(info->status & ~info->mask))
  505. return 0;
  506. } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
  507. info->severity == AER_NONFATAL) {
  508. /* Link is still healthy for IO reads */
  509. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
  510. &info->status);
  511. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
  512. &info->mask);
  513. if (!(info->status & ~info->mask))
  514. return 0;
  515. /* Get First Error Pointer */
  516. pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
  517. info->first_error = PCI_ERR_CAP_FEP(temp);
  518. if (info->status & AER_LOG_TLP_MASKS) {
  519. info->tlp_header_valid = 1;
  520. pci_read_config_dword(dev,
  521. pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
  522. pci_read_config_dword(dev,
  523. pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
  524. pci_read_config_dword(dev,
  525. pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
  526. pci_read_config_dword(dev,
  527. pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
  528. }
  529. }
  530. return 1;
  531. }
  532. static inline void aer_process_err_devices(struct pcie_device *p_device,
  533. struct aer_err_info *e_info)
  534. {
  535. int i;
  536. /* Report all before handle them, not to lost records by reset etc. */
  537. for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
  538. if (get_device_error_info(e_info->dev[i], e_info))
  539. aer_print_error(e_info->dev[i], e_info);
  540. }
  541. for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
  542. if (get_device_error_info(e_info->dev[i], e_info))
  543. handle_error_source(p_device, e_info->dev[i], e_info);
  544. }
  545. }
  546. /**
  547. * aer_isr_one_error - consume an error detected by root port
  548. * @p_device: pointer to error root port service device
  549. * @e_src: pointer to an error source
  550. */
  551. static void aer_isr_one_error(struct pcie_device *p_device,
  552. struct aer_err_source *e_src)
  553. {
  554. struct aer_err_info *e_info;
  555. /* struct aer_err_info might be big, so we allocate it with slab */
  556. e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
  557. if (!e_info) {
  558. dev_printk(KERN_DEBUG, &p_device->port->dev,
  559. "Can't allocate mem when processing AER errors\n");
  560. return;
  561. }
  562. /*
  563. * There is a possibility that both correctable error and
  564. * uncorrectable error being logged. Report correctable error first.
  565. */
  566. if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
  567. e_info->id = ERR_COR_ID(e_src->id);
  568. e_info->severity = AER_CORRECTABLE;
  569. if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
  570. e_info->multi_error_valid = 1;
  571. else
  572. e_info->multi_error_valid = 0;
  573. aer_print_port_info(p_device->port, e_info);
  574. if (find_source_device(p_device->port, e_info))
  575. aer_process_err_devices(p_device, e_info);
  576. }
  577. if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
  578. e_info->id = ERR_UNCOR_ID(e_src->id);
  579. if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
  580. e_info->severity = AER_FATAL;
  581. else
  582. e_info->severity = AER_NONFATAL;
  583. if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
  584. e_info->multi_error_valid = 1;
  585. else
  586. e_info->multi_error_valid = 0;
  587. aer_print_port_info(p_device->port, e_info);
  588. if (find_source_device(p_device->port, e_info))
  589. aer_process_err_devices(p_device, e_info);
  590. }
  591. kfree(e_info);
  592. }
  593. /**
  594. * get_e_source - retrieve an error source
  595. * @rpc: pointer to the root port which holds an error
  596. * @e_src: pointer to store retrieved error source
  597. *
  598. * Return 1 if an error source is retrieved, otherwise 0.
  599. *
  600. * Invoked by DPC handler to consume an error.
  601. */
  602. static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
  603. {
  604. unsigned long flags;
  605. int ret = 0;
  606. /* Lock access to Root error producer/consumer index */
  607. spin_lock_irqsave(&rpc->e_lock, flags);
  608. if (rpc->prod_idx != rpc->cons_idx) {
  609. *e_src = rpc->e_sources[rpc->cons_idx];
  610. rpc->cons_idx++;
  611. if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
  612. rpc->cons_idx = 0;
  613. ret = 1;
  614. }
  615. spin_unlock_irqrestore(&rpc->e_lock, flags);
  616. return ret;
  617. }
  618. /**
  619. * aer_isr - consume errors detected by root port
  620. * @work: definition of this work item
  621. *
  622. * Invoked, as DPC, when root port records new detected error
  623. */
  624. void aer_isr(struct work_struct *work)
  625. {
  626. struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
  627. struct pcie_device *p_device = rpc->rpd;
  628. struct aer_err_source e_src;
  629. mutex_lock(&rpc->rpc_mutex);
  630. while (get_e_source(rpc, &e_src))
  631. aer_isr_one_error(p_device, &e_src);
  632. mutex_unlock(&rpc->rpc_mutex);
  633. wake_up(&rpc->wait_release);
  634. }
  635. /**
  636. * aer_init - provide AER initialization
  637. * @dev: pointer to AER pcie device
  638. *
  639. * Invoked when AER service driver is loaded.
  640. */
  641. int aer_init(struct pcie_device *dev)
  642. {
  643. if (dev->port->aer_firmware_first) {
  644. dev_printk(KERN_DEBUG, &dev->device,
  645. "PCIe errors handled by platform firmware.\n");
  646. goto out;
  647. }
  648. if (aer_osc_setup(dev))
  649. goto out;
  650. return 0;
  651. out:
  652. if (forceload) {
  653. dev_printk(KERN_DEBUG, &dev->device,
  654. "aerdrv forceload requested.\n");
  655. dev->port->aer_firmware_first = 0;
  656. return 0;
  657. }
  658. return -ENXIO;
  659. }