aerdrv_core.c 22 KB

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