aerdrv_core.c 19 KB

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