pciehp_hpc.c 24 KB

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