aerdrv_core.c 19 KB

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