aerdrv_core.c 19 KB

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