pciehp_hpc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. /*
  494. * If we've already reported a power fault, don't report it again
  495. * until we've done something to handle it.
  496. */
  497. if (ctrl->power_fault_detected)
  498. events &= ~PCI_EXP_SLTSTA_PFD;
  499. if (!events)
  500. return IRQ_NONE;
  501. /* Capture link status before clearing interrupts */
  502. if (events & PCI_EXP_SLTSTA_DLLSC)
  503. link = pciehp_check_link_active(ctrl);
  504. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, events);
  505. ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", events);
  506. /* Check Command Complete Interrupt Pending */
  507. if (events & PCI_EXP_SLTSTA_CC) {
  508. ctrl->cmd_busy = 0;
  509. smp_mb();
  510. wake_up(&ctrl->queue);
  511. }
  512. if (subordinate) {
  513. list_for_each_entry(dev, &subordinate->devices, bus_list) {
  514. if (dev->ignore_hotplug) {
  515. ctrl_dbg(ctrl, "ignoring hotplug event %#06x (%s requested no hotplug)\n",
  516. events, pci_name(dev));
  517. return IRQ_HANDLED;
  518. }
  519. }
  520. }
  521. /* Check Attention Button Pressed */
  522. if (events & PCI_EXP_SLTSTA_ABP) {
  523. ctrl_info(ctrl, "Slot(%s): Attention button pressed\n",
  524. slot_name(slot));
  525. pciehp_queue_interrupt_event(slot, INT_BUTTON_PRESS);
  526. }
  527. /* Check Presence Detect Changed */
  528. if (events & PCI_EXP_SLTSTA_PDC) {
  529. present = !!(status & PCI_EXP_SLTSTA_PDS);
  530. ctrl_info(ctrl, "Slot(%s): Card %spresent\n", slot_name(slot),
  531. present ? "" : "not ");
  532. pciehp_queue_interrupt_event(slot, present ? INT_PRESENCE_ON :
  533. INT_PRESENCE_OFF);
  534. }
  535. /* Check Power Fault Detected */
  536. if ((events & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
  537. ctrl->power_fault_detected = 1;
  538. ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(slot));
  539. pciehp_queue_interrupt_event(slot, INT_POWER_FAULT);
  540. }
  541. if (events & PCI_EXP_SLTSTA_DLLSC) {
  542. ctrl_info(ctrl, "Slot(%s): Link %s\n", slot_name(slot),
  543. link ? "Up" : "Down");
  544. pciehp_queue_interrupt_event(slot, link ? INT_LINK_UP :
  545. INT_LINK_DOWN);
  546. }
  547. return IRQ_HANDLED;
  548. }
  549. static irqreturn_t pcie_isr(int irq, void *dev_id)
  550. {
  551. irqreturn_t rc, handled = IRQ_NONE;
  552. /*
  553. * To guarantee that all interrupt events are serviced, we need to
  554. * re-inspect Slot Status register after clearing what is presumed
  555. * to be the last pending interrupt.
  556. */
  557. do {
  558. rc = pciehp_isr(irq, dev_id);
  559. if (rc == IRQ_HANDLED)
  560. handled = IRQ_HANDLED;
  561. } while (rc == IRQ_HANDLED);
  562. /* Return IRQ_HANDLED if we handled one or more events */
  563. return handled;
  564. }
  565. void pcie_enable_notification(struct controller *ctrl)
  566. {
  567. u16 cmd, mask;
  568. /*
  569. * TBD: Power fault detected software notification support.
  570. *
  571. * Power fault detected software notification is not enabled
  572. * now, because it caused power fault detected interrupt storm
  573. * on some machines. On those machines, power fault detected
  574. * bit in the slot status register was set again immediately
  575. * when it is cleared in the interrupt service routine, and
  576. * next power fault detected interrupt was notified again.
  577. */
  578. /*
  579. * Always enable link events: thus link-up and link-down shall
  580. * always be treated as hotplug and unplug respectively. Enable
  581. * presence detect only if Attention Button is not present.
  582. */
  583. cmd = PCI_EXP_SLTCTL_DLLSCE;
  584. if (ATTN_BUTTN(ctrl))
  585. cmd |= PCI_EXP_SLTCTL_ABPE;
  586. else
  587. cmd |= PCI_EXP_SLTCTL_PDCE;
  588. if (!pciehp_poll_mode)
  589. cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
  590. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  591. PCI_EXP_SLTCTL_PFDE |
  592. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  593. PCI_EXP_SLTCTL_DLLSCE);
  594. pcie_write_cmd_nowait(ctrl, cmd, mask);
  595. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  596. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
  597. }
  598. static void pcie_disable_notification(struct controller *ctrl)
  599. {
  600. u16 mask;
  601. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  602. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  603. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  604. PCI_EXP_SLTCTL_DLLSCE);
  605. pcie_write_cmd(ctrl, 0, mask);
  606. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  607. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
  608. }
  609. /*
  610. * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
  611. * bus reset of the bridge, but at the same time we want to ensure that it is
  612. * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
  613. * disable link state notification and presence detection change notification
  614. * momentarily, if we see that they could interfere. Also, clear any spurious
  615. * events after.
  616. */
  617. int pciehp_reset_slot(struct slot *slot, int probe)
  618. {
  619. struct controller *ctrl = slot->ctrl;
  620. struct pci_dev *pdev = ctrl_dev(ctrl);
  621. u16 stat_mask = 0, ctrl_mask = 0;
  622. if (probe)
  623. return 0;
  624. if (!ATTN_BUTTN(ctrl)) {
  625. ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
  626. stat_mask |= PCI_EXP_SLTSTA_PDC;
  627. }
  628. ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
  629. stat_mask |= PCI_EXP_SLTSTA_DLLSC;
  630. pcie_write_cmd(ctrl, 0, ctrl_mask);
  631. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  632. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
  633. if (pciehp_poll_mode)
  634. del_timer_sync(&ctrl->poll_timer);
  635. pci_reset_bridge_secondary_bus(ctrl->pcie->port);
  636. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
  637. pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
  638. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  639. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
  640. if (pciehp_poll_mode)
  641. int_poll_timeout(ctrl->poll_timer.data);
  642. return 0;
  643. }
  644. int pcie_init_notification(struct controller *ctrl)
  645. {
  646. if (pciehp_request_irq(ctrl))
  647. return -1;
  648. pcie_enable_notification(ctrl);
  649. ctrl->notification_enabled = 1;
  650. return 0;
  651. }
  652. static void pcie_shutdown_notification(struct controller *ctrl)
  653. {
  654. if (ctrl->notification_enabled) {
  655. pcie_disable_notification(ctrl);
  656. pciehp_free_irq(ctrl);
  657. ctrl->notification_enabled = 0;
  658. }
  659. }
  660. static int pcie_init_slot(struct controller *ctrl)
  661. {
  662. struct slot *slot;
  663. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  664. if (!slot)
  665. return -ENOMEM;
  666. slot->wq = alloc_workqueue("pciehp-%u", 0, 0, PSN(ctrl));
  667. if (!slot->wq)
  668. goto abort;
  669. slot->ctrl = ctrl;
  670. mutex_init(&slot->lock);
  671. mutex_init(&slot->hotplug_lock);
  672. INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
  673. ctrl->slot = slot;
  674. return 0;
  675. abort:
  676. kfree(slot);
  677. return -ENOMEM;
  678. }
  679. static void pcie_cleanup_slot(struct controller *ctrl)
  680. {
  681. struct slot *slot = ctrl->slot;
  682. cancel_delayed_work(&slot->work);
  683. destroy_workqueue(slot->wq);
  684. kfree(slot);
  685. }
  686. static inline void dbg_ctrl(struct controller *ctrl)
  687. {
  688. struct pci_dev *pdev = ctrl->pcie->port;
  689. u16 reg16;
  690. if (!pciehp_debug)
  691. return;
  692. ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
  693. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &reg16);
  694. ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
  695. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &reg16);
  696. ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
  697. }
  698. #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
  699. struct controller *pcie_init(struct pcie_device *dev)
  700. {
  701. struct controller *ctrl;
  702. u32 slot_cap, link_cap;
  703. struct pci_dev *pdev = dev->port;
  704. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  705. if (!ctrl) {
  706. dev_err(&dev->device, "%s: Out of memory\n", __func__);
  707. goto abort;
  708. }
  709. ctrl->pcie = dev;
  710. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
  711. if (pdev->hotplug_user_indicators)
  712. slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);
  713. ctrl->slot_cap = slot_cap;
  714. mutex_init(&ctrl->ctrl_lock);
  715. init_waitqueue_head(&ctrl->queue);
  716. dbg_ctrl(ctrl);
  717. /* Check if Data Link Layer Link Active Reporting is implemented */
  718. pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
  719. if (link_cap & PCI_EXP_LNKCAP_DLLLARC)
  720. ctrl->link_active_reporting = 1;
  721. /* Clear all remaining event bits in Slot Status register */
  722. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  723. PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  724. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
  725. PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC);
  726. 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",
  727. (slot_cap & PCI_EXP_SLTCAP_PSN) >> 19,
  728. FLAG(slot_cap, PCI_EXP_SLTCAP_ABP),
  729. FLAG(slot_cap, PCI_EXP_SLTCAP_PCP),
  730. FLAG(slot_cap, PCI_EXP_SLTCAP_MRLSP),
  731. FLAG(slot_cap, PCI_EXP_SLTCAP_AIP),
  732. FLAG(slot_cap, PCI_EXP_SLTCAP_PIP),
  733. FLAG(slot_cap, PCI_EXP_SLTCAP_HPC),
  734. FLAG(slot_cap, PCI_EXP_SLTCAP_HPS),
  735. FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
  736. FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
  737. FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC));
  738. if (pcie_init_slot(ctrl))
  739. goto abort_ctrl;
  740. return ctrl;
  741. abort_ctrl:
  742. kfree(ctrl);
  743. abort:
  744. return NULL;
  745. }
  746. void pciehp_release_ctrl(struct controller *ctrl)
  747. {
  748. pcie_shutdown_notification(ctrl);
  749. pcie_cleanup_slot(ctrl);
  750. kfree(ctrl);
  751. }