pciehp_hpc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI Express PCI Hot Plug Driver
  4. *
  5. * Copyright (C) 1995,2001 Compaq Computer Corporation
  6. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2003-2004 Intel Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/signal.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/timer.h>
  21. #include <linux/pci.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/time.h>
  24. #include <linux/slab.h>
  25. #include "../pci.h"
  26. #include "pciehp.h"
  27. static inline struct pci_dev *ctrl_dev(struct controller *ctrl)
  28. {
  29. return ctrl->pcie->port;
  30. }
  31. static irqreturn_t pcie_isr(int irq, void *dev_id);
  32. static void start_int_poll_timer(struct controller *ctrl, int sec);
  33. /* This is the interrupt polling timeout function. */
  34. static void int_poll_timeout(struct timer_list *t)
  35. {
  36. struct controller *ctrl = from_timer(ctrl, t, poll_timer);
  37. /* Poll for interrupt events. regs == NULL => polling */
  38. pcie_isr(0, ctrl);
  39. if (!pciehp_poll_time)
  40. pciehp_poll_time = 2; /* default polling interval is 2 sec */
  41. start_int_poll_timer(ctrl, pciehp_poll_time);
  42. }
  43. /* This function starts the interrupt polling timer. */
  44. static void start_int_poll_timer(struct controller *ctrl, int sec)
  45. {
  46. /* Clamp to sane value */
  47. if ((sec <= 0) || (sec > 60))
  48. sec = 2;
  49. ctrl->poll_timer.expires = jiffies + sec * HZ;
  50. add_timer(&ctrl->poll_timer);
  51. }
  52. static inline int pciehp_request_irq(struct controller *ctrl)
  53. {
  54. int retval, irq = ctrl->pcie->irq;
  55. /* Install interrupt polling timer. Start with 10 sec delay */
  56. if (pciehp_poll_mode) {
  57. timer_setup(&ctrl->poll_timer, int_poll_timeout, 0);
  58. start_int_poll_timer(ctrl, 10);
  59. return 0;
  60. }
  61. /* Installs the interrupt handler */
  62. retval = request_irq(irq, pcie_isr, IRQF_SHARED, MY_NAME, ctrl);
  63. if (retval)
  64. ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
  65. irq);
  66. return retval;
  67. }
  68. static inline void pciehp_free_irq(struct controller *ctrl)
  69. {
  70. if (pciehp_poll_mode)
  71. del_timer_sync(&ctrl->poll_timer);
  72. else
  73. free_irq(ctrl->pcie->irq, ctrl);
  74. }
  75. static int pcie_poll_cmd(struct controller *ctrl, int timeout)
  76. {
  77. struct pci_dev *pdev = ctrl_dev(ctrl);
  78. u16 slot_status;
  79. while (true) {
  80. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  81. if (slot_status == (u16) ~0) {
  82. ctrl_info(ctrl, "%s: no response from device\n",
  83. __func__);
  84. return 0;
  85. }
  86. if (slot_status & PCI_EXP_SLTSTA_CC) {
  87. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  88. PCI_EXP_SLTSTA_CC);
  89. return 1;
  90. }
  91. if (timeout < 0)
  92. break;
  93. msleep(10);
  94. timeout -= 10;
  95. }
  96. return 0; /* timeout */
  97. }
  98. static void pcie_wait_cmd(struct controller *ctrl)
  99. {
  100. unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
  101. unsigned long duration = msecs_to_jiffies(msecs);
  102. unsigned long cmd_timeout = ctrl->cmd_started + duration;
  103. unsigned long now, timeout;
  104. int rc;
  105. /*
  106. * If the controller does not generate notifications for command
  107. * completions, we never need to wait between writes.
  108. */
  109. if (NO_CMD_CMPL(ctrl))
  110. return;
  111. if (!ctrl->cmd_busy)
  112. return;
  113. /*
  114. * Even if the command has already timed out, we want to call
  115. * pcie_poll_cmd() so it can clear PCI_EXP_SLTSTA_CC.
  116. */
  117. now = jiffies;
  118. if (time_before_eq(cmd_timeout, now))
  119. timeout = 1;
  120. else
  121. timeout = cmd_timeout - now;
  122. if (ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE &&
  123. ctrl->slot_ctrl & PCI_EXP_SLTCTL_CCIE)
  124. rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
  125. else
  126. rc = pcie_poll_cmd(ctrl, jiffies_to_msecs(timeout));
  127. /*
  128. * Controllers with errata like Intel CF118 don't generate
  129. * completion notifications unless the power/indicator/interlock
  130. * control bits are changed. On such controllers, we'll emit this
  131. * timeout message when we wait for completion of commands that
  132. * don't change those bits, e.g., commands that merely enable
  133. * interrupts.
  134. */
  135. if (!rc)
  136. ctrl_info(ctrl, "Timeout on hotplug command %#06x (issued %u msec ago)\n",
  137. ctrl->slot_ctrl,
  138. jiffies_to_msecs(jiffies - ctrl->cmd_started));
  139. }
  140. static void pcie_do_write_cmd(struct controller *ctrl, u16 cmd,
  141. u16 mask, bool wait)
  142. {
  143. struct pci_dev *pdev = ctrl_dev(ctrl);
  144. u16 slot_ctrl;
  145. mutex_lock(&ctrl->ctrl_lock);
  146. /*
  147. * Always wait for any previous command that might still be in progress
  148. */
  149. pcie_wait_cmd(ctrl);
  150. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  151. if (slot_ctrl == (u16) ~0) {
  152. ctrl_info(ctrl, "%s: no response from device\n", __func__);
  153. goto out;
  154. }
  155. slot_ctrl &= ~mask;
  156. slot_ctrl |= (cmd & mask);
  157. ctrl->cmd_busy = 1;
  158. smp_mb();
  159. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, slot_ctrl);
  160. ctrl->cmd_started = jiffies;
  161. ctrl->slot_ctrl = slot_ctrl;
  162. /*
  163. * Optionally wait for the hardware to be ready for a new command,
  164. * indicating completion of the above issued command.
  165. */
  166. if (wait)
  167. pcie_wait_cmd(ctrl);
  168. out:
  169. mutex_unlock(&ctrl->ctrl_lock);
  170. }
  171. /**
  172. * pcie_write_cmd - Issue controller command
  173. * @ctrl: controller to which the command is issued
  174. * @cmd: command value written to slot control register
  175. * @mask: bitmask of slot control register to be modified
  176. */
  177. static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
  178. {
  179. pcie_do_write_cmd(ctrl, cmd, mask, true);
  180. }
  181. /* Same as above without waiting for the hardware to latch */
  182. static void pcie_write_cmd_nowait(struct controller *ctrl, u16 cmd, u16 mask)
  183. {
  184. pcie_do_write_cmd(ctrl, cmd, mask, false);
  185. }
  186. bool pciehp_check_link_active(struct controller *ctrl)
  187. {
  188. struct pci_dev *pdev = ctrl_dev(ctrl);
  189. u16 lnk_status;
  190. bool ret;
  191. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  192. ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
  193. if (ret)
  194. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  195. return ret;
  196. }
  197. static void __pcie_wait_link_active(struct controller *ctrl, bool active)
  198. {
  199. int timeout = 1000;
  200. if (pciehp_check_link_active(ctrl) == active)
  201. return;
  202. while (timeout > 0) {
  203. msleep(10);
  204. timeout -= 10;
  205. if (pciehp_check_link_active(ctrl) == active)
  206. return;
  207. }
  208. ctrl_dbg(ctrl, "Data Link Layer Link Active not %s in 1000 msec\n",
  209. active ? "set" : "cleared");
  210. }
  211. static void pcie_wait_link_active(struct controller *ctrl)
  212. {
  213. __pcie_wait_link_active(ctrl, true);
  214. }
  215. static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
  216. {
  217. u32 l;
  218. int count = 0;
  219. int delay = 1000, step = 20;
  220. bool found = false;
  221. do {
  222. found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
  223. count++;
  224. if (found)
  225. break;
  226. msleep(step);
  227. delay -= step;
  228. } while (delay > 0);
  229. if (count > 1 && pciehp_debug)
  230. printk(KERN_DEBUG "pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
  231. pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
  232. PCI_FUNC(devfn), count, step, l);
  233. return found;
  234. }
  235. int pciehp_check_link_status(struct controller *ctrl)
  236. {
  237. struct pci_dev *pdev = ctrl_dev(ctrl);
  238. bool found;
  239. u16 lnk_status;
  240. /*
  241. * Data Link Layer Link Active Reporting must be capable for
  242. * hot-plug capable downstream port. But old controller might
  243. * not implement it. In this case, we wait for 1000 ms.
  244. */
  245. if (ctrl->link_active_reporting)
  246. pcie_wait_link_active(ctrl);
  247. else
  248. msleep(1000);
  249. /* wait 100ms before read pci conf, and try in 1s */
  250. msleep(100);
  251. found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
  252. PCI_DEVFN(0, 0));
  253. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  254. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  255. if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
  256. !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
  257. ctrl_err(ctrl, "link training error: status %#06x\n",
  258. lnk_status);
  259. return -1;
  260. }
  261. pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);
  262. if (!found)
  263. return -1;
  264. return 0;
  265. }
  266. static int __pciehp_link_set(struct controller *ctrl, bool enable)
  267. {
  268. struct pci_dev *pdev = ctrl_dev(ctrl);
  269. u16 lnk_ctrl;
  270. pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &lnk_ctrl);
  271. if (enable)
  272. lnk_ctrl &= ~PCI_EXP_LNKCTL_LD;
  273. else
  274. lnk_ctrl |= PCI_EXP_LNKCTL_LD;
  275. pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, lnk_ctrl);
  276. ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
  277. return 0;
  278. }
  279. static int pciehp_link_enable(struct controller *ctrl)
  280. {
  281. return __pciehp_link_set(ctrl, true);
  282. }
  283. int pciehp_get_raw_indicator_status(struct hotplug_slot *hotplug_slot,
  284. u8 *status)
  285. {
  286. struct slot *slot = hotplug_slot->private;
  287. struct pci_dev *pdev = ctrl_dev(slot->ctrl);
  288. u16 slot_ctrl;
  289. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  290. *status = (slot_ctrl & (PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC)) >> 6;
  291. return 0;
  292. }
  293. void pciehp_get_attention_status(struct slot *slot, u8 *status)
  294. {
  295. struct controller *ctrl = slot->ctrl;
  296. struct pci_dev *pdev = ctrl_dev(ctrl);
  297. u16 slot_ctrl;
  298. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  299. ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
  300. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  301. switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
  302. case PCI_EXP_SLTCTL_ATTN_IND_ON:
  303. *status = 1; /* On */
  304. break;
  305. case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
  306. *status = 2; /* Blink */
  307. break;
  308. case PCI_EXP_SLTCTL_ATTN_IND_OFF:
  309. *status = 0; /* Off */
  310. break;
  311. default:
  312. *status = 0xFF;
  313. break;
  314. }
  315. }
  316. void pciehp_get_power_status(struct slot *slot, u8 *status)
  317. {
  318. struct controller *ctrl = slot->ctrl;
  319. struct pci_dev *pdev = ctrl_dev(ctrl);
  320. u16 slot_ctrl;
  321. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  322. ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
  323. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  324. switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
  325. case PCI_EXP_SLTCTL_PWR_ON:
  326. *status = 1; /* On */
  327. break;
  328. case PCI_EXP_SLTCTL_PWR_OFF:
  329. *status = 0; /* Off */
  330. break;
  331. default:
  332. *status = 0xFF;
  333. break;
  334. }
  335. }
  336. void pciehp_get_latch_status(struct slot *slot, u8 *status)
  337. {
  338. struct pci_dev *pdev = ctrl_dev(slot->ctrl);
  339. u16 slot_status;
  340. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  341. *status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
  342. }
  343. void pciehp_get_adapter_status(struct slot *slot, u8 *status)
  344. {
  345. struct pci_dev *pdev = ctrl_dev(slot->ctrl);
  346. u16 slot_status;
  347. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  348. *status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
  349. }
  350. int pciehp_query_power_fault(struct slot *slot)
  351. {
  352. struct pci_dev *pdev = ctrl_dev(slot->ctrl);
  353. u16 slot_status;
  354. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  355. return !!(slot_status & PCI_EXP_SLTSTA_PFD);
  356. }
  357. int pciehp_set_raw_indicator_status(struct hotplug_slot *hotplug_slot,
  358. u8 status)
  359. {
  360. struct slot *slot = hotplug_slot->private;
  361. struct controller *ctrl = slot->ctrl;
  362. pcie_write_cmd_nowait(ctrl, status << 6,
  363. PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC);
  364. return 0;
  365. }
  366. void pciehp_set_attention_status(struct slot *slot, u8 value)
  367. {
  368. struct controller *ctrl = slot->ctrl;
  369. u16 slot_cmd;
  370. if (!ATTN_LED(ctrl))
  371. return;
  372. switch (value) {
  373. case 0: /* turn off */
  374. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
  375. break;
  376. case 1: /* turn on */
  377. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_ON;
  378. break;
  379. case 2: /* turn blink */
  380. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_BLINK;
  381. break;
  382. default:
  383. return;
  384. }
  385. pcie_write_cmd_nowait(ctrl, slot_cmd, PCI_EXP_SLTCTL_AIC);
  386. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  387. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  388. }
  389. void pciehp_green_led_on(struct slot *slot)
  390. {
  391. struct controller *ctrl = slot->ctrl;
  392. if (!PWR_LED(ctrl))
  393. return;
  394. pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON,
  395. PCI_EXP_SLTCTL_PIC);
  396. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  397. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  398. PCI_EXP_SLTCTL_PWR_IND_ON);
  399. }
  400. void pciehp_green_led_off(struct slot *slot)
  401. {
  402. struct controller *ctrl = slot->ctrl;
  403. if (!PWR_LED(ctrl))
  404. return;
  405. pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
  406. PCI_EXP_SLTCTL_PIC);
  407. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  408. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  409. PCI_EXP_SLTCTL_PWR_IND_OFF);
  410. }
  411. void pciehp_green_led_blink(struct slot *slot)
  412. {
  413. struct controller *ctrl = slot->ctrl;
  414. if (!PWR_LED(ctrl))
  415. return;
  416. pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK,
  417. PCI_EXP_SLTCTL_PIC);
  418. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  419. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  420. PCI_EXP_SLTCTL_PWR_IND_BLINK);
  421. }
  422. int pciehp_power_on_slot(struct slot *slot)
  423. {
  424. struct controller *ctrl = slot->ctrl;
  425. struct pci_dev *pdev = ctrl_dev(ctrl);
  426. u16 slot_status;
  427. int retval;
  428. /* Clear sticky power-fault bit from previous power failures */
  429. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  430. if (slot_status & PCI_EXP_SLTSTA_PFD)
  431. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  432. PCI_EXP_SLTSTA_PFD);
  433. ctrl->power_fault_detected = 0;
  434. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
  435. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  436. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  437. PCI_EXP_SLTCTL_PWR_ON);
  438. retval = pciehp_link_enable(ctrl);
  439. if (retval)
  440. ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
  441. return retval;
  442. }
  443. void pciehp_power_off_slot(struct slot *slot)
  444. {
  445. struct controller *ctrl = slot->ctrl;
  446. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
  447. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  448. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  449. PCI_EXP_SLTCTL_PWR_OFF);
  450. }
  451. static irqreturn_t pciehp_isr(int irq, void *dev_id)
  452. {
  453. struct controller *ctrl = (struct controller *)dev_id;
  454. struct pci_dev *pdev = ctrl_dev(ctrl);
  455. struct pci_bus *subordinate = pdev->subordinate;
  456. struct pci_dev *dev;
  457. struct slot *slot = ctrl->slot;
  458. u16 status, events;
  459. u8 present;
  460. bool link;
  461. /* Interrupts cannot originate from a controller that's asleep */
  462. if (pdev->current_state == PCI_D3cold)
  463. return IRQ_NONE;
  464. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &status);
  465. if (status == (u16) ~0) {
  466. ctrl_info(ctrl, "%s: no response from device\n", __func__);
  467. return IRQ_NONE;
  468. }
  469. /*
  470. * Slot Status contains plain status bits as well as event
  471. * notification bits; right now we only want the event bits.
  472. */
  473. events = status & (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  474. PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_CC |
  475. PCI_EXP_SLTSTA_DLLSC);
  476. /*
  477. * If we've already reported a power fault, don't report it again
  478. * until we've done something to handle it.
  479. */
  480. if (ctrl->power_fault_detected)
  481. events &= ~PCI_EXP_SLTSTA_PFD;
  482. if (!events)
  483. return IRQ_NONE;
  484. /* Capture link status before clearing interrupts */
  485. if (events & PCI_EXP_SLTSTA_DLLSC)
  486. link = pciehp_check_link_active(ctrl);
  487. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, events);
  488. ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", events);
  489. /* Check Command Complete Interrupt Pending */
  490. if (events & PCI_EXP_SLTSTA_CC) {
  491. ctrl->cmd_busy = 0;
  492. smp_mb();
  493. wake_up(&ctrl->queue);
  494. }
  495. if (subordinate) {
  496. list_for_each_entry(dev, &subordinate->devices, bus_list) {
  497. if (dev->ignore_hotplug) {
  498. ctrl_dbg(ctrl, "ignoring hotplug event %#06x (%s requested no hotplug)\n",
  499. events, pci_name(dev));
  500. return IRQ_HANDLED;
  501. }
  502. }
  503. }
  504. /* Check Attention Button Pressed */
  505. if (events & PCI_EXP_SLTSTA_ABP) {
  506. ctrl_info(ctrl, "Slot(%s): Attention button pressed\n",
  507. slot_name(slot));
  508. pciehp_queue_interrupt_event(slot, INT_BUTTON_PRESS);
  509. }
  510. /*
  511. * Check Link Status Changed at higher precedence than Presence
  512. * Detect Changed. The PDS value may be set to "card present" from
  513. * out-of-band detection, which may be in conflict with a Link Down
  514. * and cause the wrong event to queue.
  515. */
  516. if (events & PCI_EXP_SLTSTA_DLLSC) {
  517. ctrl_info(ctrl, "Slot(%s): Link %s\n", slot_name(slot),
  518. link ? "Up" : "Down");
  519. pciehp_queue_interrupt_event(slot, link ? INT_LINK_UP :
  520. INT_LINK_DOWN);
  521. } else if (events & PCI_EXP_SLTSTA_PDC) {
  522. present = !!(status & PCI_EXP_SLTSTA_PDS);
  523. ctrl_info(ctrl, "Slot(%s): Card %spresent\n", slot_name(slot),
  524. present ? "" : "not ");
  525. pciehp_queue_interrupt_event(slot, present ? INT_PRESENCE_ON :
  526. INT_PRESENCE_OFF);
  527. }
  528. /* Check Power Fault Detected */
  529. if ((events & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
  530. ctrl->power_fault_detected = 1;
  531. ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(slot));
  532. pciehp_queue_interrupt_event(slot, INT_POWER_FAULT);
  533. }
  534. return IRQ_HANDLED;
  535. }
  536. static irqreturn_t pcie_isr(int irq, void *dev_id)
  537. {
  538. irqreturn_t rc, handled = IRQ_NONE;
  539. /*
  540. * To guarantee that all interrupt events are serviced, we need to
  541. * re-inspect Slot Status register after clearing what is presumed
  542. * to be the last pending interrupt.
  543. */
  544. do {
  545. rc = pciehp_isr(irq, dev_id);
  546. if (rc == IRQ_HANDLED)
  547. handled = IRQ_HANDLED;
  548. } while (rc == IRQ_HANDLED);
  549. /* Return IRQ_HANDLED if we handled one or more events */
  550. return handled;
  551. }
  552. void pcie_enable_notification(struct controller *ctrl)
  553. {
  554. u16 cmd, mask;
  555. /*
  556. * TBD: Power fault detected software notification support.
  557. *
  558. * Power fault detected software notification is not enabled
  559. * now, because it caused power fault detected interrupt storm
  560. * on some machines. On those machines, power fault detected
  561. * bit in the slot status register was set again immediately
  562. * when it is cleared in the interrupt service routine, and
  563. * next power fault detected interrupt was notified again.
  564. */
  565. /*
  566. * Always enable link events: thus link-up and link-down shall
  567. * always be treated as hotplug and unplug respectively. Enable
  568. * presence detect only if Attention Button is not present.
  569. */
  570. cmd = PCI_EXP_SLTCTL_DLLSCE;
  571. if (ATTN_BUTTN(ctrl))
  572. cmd |= PCI_EXP_SLTCTL_ABPE;
  573. else
  574. cmd |= PCI_EXP_SLTCTL_PDCE;
  575. if (!pciehp_poll_mode)
  576. cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
  577. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  578. PCI_EXP_SLTCTL_PFDE |
  579. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  580. PCI_EXP_SLTCTL_DLLSCE);
  581. pcie_write_cmd_nowait(ctrl, cmd, mask);
  582. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  583. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
  584. }
  585. static void pcie_disable_notification(struct controller *ctrl)
  586. {
  587. u16 mask;
  588. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  589. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  590. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  591. PCI_EXP_SLTCTL_DLLSCE);
  592. pcie_write_cmd(ctrl, 0, mask);
  593. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  594. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
  595. }
  596. /*
  597. * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
  598. * bus reset of the bridge, but at the same time we want to ensure that it is
  599. * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
  600. * disable link state notification and presence detection change notification
  601. * momentarily, if we see that they could interfere. Also, clear any spurious
  602. * events after.
  603. */
  604. int pciehp_reset_slot(struct slot *slot, int probe)
  605. {
  606. struct controller *ctrl = slot->ctrl;
  607. struct pci_dev *pdev = ctrl_dev(ctrl);
  608. u16 stat_mask = 0, ctrl_mask = 0;
  609. if (probe)
  610. return 0;
  611. if (!ATTN_BUTTN(ctrl)) {
  612. ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
  613. stat_mask |= PCI_EXP_SLTSTA_PDC;
  614. }
  615. ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
  616. stat_mask |= PCI_EXP_SLTSTA_DLLSC;
  617. pcie_write_cmd(ctrl, 0, ctrl_mask);
  618. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  619. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
  620. if (pciehp_poll_mode)
  621. del_timer_sync(&ctrl->poll_timer);
  622. pci_reset_bridge_secondary_bus(ctrl->pcie->port);
  623. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
  624. pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
  625. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  626. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
  627. if (pciehp_poll_mode)
  628. int_poll_timeout(&ctrl->poll_timer);
  629. return 0;
  630. }
  631. int pcie_init_notification(struct controller *ctrl)
  632. {
  633. if (pciehp_request_irq(ctrl))
  634. return -1;
  635. pcie_enable_notification(ctrl);
  636. ctrl->notification_enabled = 1;
  637. return 0;
  638. }
  639. static void pcie_shutdown_notification(struct controller *ctrl)
  640. {
  641. if (ctrl->notification_enabled) {
  642. pcie_disable_notification(ctrl);
  643. pciehp_free_irq(ctrl);
  644. ctrl->notification_enabled = 0;
  645. }
  646. }
  647. static int pcie_init_slot(struct controller *ctrl)
  648. {
  649. struct slot *slot;
  650. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  651. if (!slot)
  652. return -ENOMEM;
  653. slot->wq = alloc_ordered_workqueue("pciehp-%u", 0, PSN(ctrl));
  654. if (!slot->wq)
  655. goto abort;
  656. slot->ctrl = ctrl;
  657. mutex_init(&slot->lock);
  658. mutex_init(&slot->hotplug_lock);
  659. INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
  660. ctrl->slot = slot;
  661. return 0;
  662. abort:
  663. kfree(slot);
  664. return -ENOMEM;
  665. }
  666. static void pcie_cleanup_slot(struct controller *ctrl)
  667. {
  668. struct slot *slot = ctrl->slot;
  669. cancel_delayed_work(&slot->work);
  670. destroy_workqueue(slot->wq);
  671. kfree(slot);
  672. }
  673. static inline void dbg_ctrl(struct controller *ctrl)
  674. {
  675. struct pci_dev *pdev = ctrl->pcie->port;
  676. u16 reg16;
  677. if (!pciehp_debug)
  678. return;
  679. ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
  680. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &reg16);
  681. ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
  682. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &reg16);
  683. ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
  684. }
  685. #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
  686. struct controller *pcie_init(struct pcie_device *dev)
  687. {
  688. struct controller *ctrl;
  689. u32 slot_cap, link_cap;
  690. struct pci_dev *pdev = dev->port;
  691. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  692. if (!ctrl)
  693. goto abort;
  694. ctrl->pcie = dev;
  695. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
  696. if (pdev->hotplug_user_indicators)
  697. slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);
  698. /*
  699. * We assume no Thunderbolt controllers support Command Complete events,
  700. * but some controllers falsely claim they do.
  701. */
  702. if (pdev->is_thunderbolt)
  703. slot_cap |= PCI_EXP_SLTCAP_NCCS;
  704. ctrl->slot_cap = slot_cap;
  705. mutex_init(&ctrl->ctrl_lock);
  706. init_waitqueue_head(&ctrl->queue);
  707. dbg_ctrl(ctrl);
  708. /* Check if Data Link Layer Link Active Reporting is implemented */
  709. pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
  710. if (link_cap & PCI_EXP_LNKCAP_DLLLARC)
  711. ctrl->link_active_reporting = 1;
  712. /*
  713. * Clear all remaining event bits in Slot Status register except
  714. * Presence Detect Changed. We want to make sure possible
  715. * hotplug event is triggered when the interrupt is unmasked so
  716. * that we don't lose that event.
  717. */
  718. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  719. PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  720. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_CC |
  721. PCI_EXP_SLTSTA_DLLSC);
  722. ctrl_info(ctrl, "Slot #%d AttnBtn%c PwrCtrl%c MRL%c AttnInd%c PwrInd%c HotPlug%c Surprise%c Interlock%c NoCompl%c LLActRep%c\n",
  723. (slot_cap & PCI_EXP_SLTCAP_PSN) >> 19,
  724. FLAG(slot_cap, PCI_EXP_SLTCAP_ABP),
  725. FLAG(slot_cap, PCI_EXP_SLTCAP_PCP),
  726. FLAG(slot_cap, PCI_EXP_SLTCAP_MRLSP),
  727. FLAG(slot_cap, PCI_EXP_SLTCAP_AIP),
  728. FLAG(slot_cap, PCI_EXP_SLTCAP_PIP),
  729. FLAG(slot_cap, PCI_EXP_SLTCAP_HPC),
  730. FLAG(slot_cap, PCI_EXP_SLTCAP_HPS),
  731. FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
  732. FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
  733. FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC));
  734. if (pcie_init_slot(ctrl))
  735. goto abort_ctrl;
  736. return ctrl;
  737. abort_ctrl:
  738. kfree(ctrl);
  739. abort:
  740. return NULL;
  741. }
  742. void pciehp_release_ctrl(struct controller *ctrl)
  743. {
  744. pcie_shutdown_notification(ctrl);
  745. pcie_cleanup_slot(ctrl);
  746. kfree(ctrl);
  747. }