pciehp_hpc.c 24 KB

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