pciehp_hpc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. void pciehp_get_attention_status(struct slot *slot, u8 *status)
  301. {
  302. struct controller *ctrl = slot->ctrl;
  303. struct pci_dev *pdev = ctrl_dev(ctrl);
  304. u16 slot_ctrl;
  305. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  306. ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
  307. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  308. switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
  309. case PCI_EXP_SLTCTL_ATTN_IND_ON:
  310. *status = 1; /* On */
  311. break;
  312. case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
  313. *status = 2; /* Blink */
  314. break;
  315. case PCI_EXP_SLTCTL_ATTN_IND_OFF:
  316. *status = 0; /* Off */
  317. break;
  318. default:
  319. *status = 0xFF;
  320. break;
  321. }
  322. }
  323. void pciehp_get_power_status(struct slot *slot, u8 *status)
  324. {
  325. struct controller *ctrl = slot->ctrl;
  326. struct pci_dev *pdev = ctrl_dev(ctrl);
  327. u16 slot_ctrl;
  328. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  329. ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
  330. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  331. switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
  332. case PCI_EXP_SLTCTL_PWR_ON:
  333. *status = 1; /* On */
  334. break;
  335. case PCI_EXP_SLTCTL_PWR_OFF:
  336. *status = 0; /* Off */
  337. break;
  338. default:
  339. *status = 0xFF;
  340. break;
  341. }
  342. }
  343. void pciehp_get_latch_status(struct slot *slot, u8 *status)
  344. {
  345. struct pci_dev *pdev = ctrl_dev(slot->ctrl);
  346. u16 slot_status;
  347. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  348. *status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
  349. }
  350. void pciehp_get_adapter_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_PDS);
  356. }
  357. int pciehp_query_power_fault(struct slot *slot)
  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. return !!(slot_status & PCI_EXP_SLTSTA_PFD);
  363. }
  364. void pciehp_set_attention_status(struct slot *slot, u8 value)
  365. {
  366. struct controller *ctrl = slot->ctrl;
  367. u16 slot_cmd;
  368. if (!ATTN_LED(ctrl))
  369. return;
  370. switch (value) {
  371. case 0: /* turn off */
  372. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
  373. break;
  374. case 1: /* turn on */
  375. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_ON;
  376. break;
  377. case 2: /* turn blink */
  378. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_BLINK;
  379. break;
  380. default:
  381. return;
  382. }
  383. pcie_write_cmd_nowait(ctrl, slot_cmd, PCI_EXP_SLTCTL_AIC);
  384. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  385. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  386. }
  387. void pciehp_green_led_on(struct slot *slot)
  388. {
  389. struct controller *ctrl = slot->ctrl;
  390. if (!PWR_LED(ctrl))
  391. return;
  392. pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON,
  393. PCI_EXP_SLTCTL_PIC);
  394. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  395. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  396. PCI_EXP_SLTCTL_PWR_IND_ON);
  397. }
  398. void pciehp_green_led_off(struct slot *slot)
  399. {
  400. struct controller *ctrl = slot->ctrl;
  401. if (!PWR_LED(ctrl))
  402. return;
  403. pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
  404. PCI_EXP_SLTCTL_PIC);
  405. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  406. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  407. PCI_EXP_SLTCTL_PWR_IND_OFF);
  408. }
  409. void pciehp_green_led_blink(struct slot *slot)
  410. {
  411. struct controller *ctrl = slot->ctrl;
  412. if (!PWR_LED(ctrl))
  413. return;
  414. pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK,
  415. PCI_EXP_SLTCTL_PIC);
  416. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  417. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  418. PCI_EXP_SLTCTL_PWR_IND_BLINK);
  419. }
  420. int pciehp_power_on_slot(struct slot *slot)
  421. {
  422. struct controller *ctrl = slot->ctrl;
  423. struct pci_dev *pdev = ctrl_dev(ctrl);
  424. u16 slot_status;
  425. int retval;
  426. /* Clear sticky power-fault bit from previous power failures */
  427. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  428. if (slot_status & PCI_EXP_SLTSTA_PFD)
  429. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  430. PCI_EXP_SLTSTA_PFD);
  431. ctrl->power_fault_detected = 0;
  432. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
  433. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  434. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  435. PCI_EXP_SLTCTL_PWR_ON);
  436. retval = pciehp_link_enable(ctrl);
  437. if (retval)
  438. ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
  439. return retval;
  440. }
  441. void pciehp_power_off_slot(struct slot *slot)
  442. {
  443. struct controller *ctrl = slot->ctrl;
  444. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
  445. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  446. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  447. PCI_EXP_SLTCTL_PWR_OFF);
  448. }
  449. static irqreturn_t pcie_isr(int irq, void *dev_id)
  450. {
  451. struct controller *ctrl = (struct controller *)dev_id;
  452. struct pci_dev *pdev = ctrl_dev(ctrl);
  453. struct pci_bus *subordinate = pdev->subordinate;
  454. struct pci_dev *dev;
  455. struct slot *slot = ctrl->slot;
  456. u16 detected, intr_loc;
  457. u8 present;
  458. bool link;
  459. /*
  460. * In order to guarantee that all interrupt events are
  461. * serviced, we need to re-inspect Slot Status register after
  462. * clearing what is presumed to be the last pending interrupt.
  463. */
  464. intr_loc = 0;
  465. do {
  466. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &detected);
  467. if (detected == (u16) ~0) {
  468. ctrl_info(ctrl, "%s: no response from device\n",
  469. __func__);
  470. return IRQ_HANDLED;
  471. }
  472. detected &= (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  473. PCI_EXP_SLTSTA_PDC |
  474. PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC);
  475. detected &= ~intr_loc;
  476. intr_loc |= detected;
  477. if (!intr_loc)
  478. return IRQ_NONE;
  479. if (detected)
  480. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  481. intr_loc);
  482. } while (detected);
  483. ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", intr_loc);
  484. /* Check Command Complete Interrupt Pending */
  485. if (intr_loc & PCI_EXP_SLTSTA_CC) {
  486. ctrl->cmd_busy = 0;
  487. smp_mb();
  488. wake_up(&ctrl->queue);
  489. }
  490. if (subordinate) {
  491. list_for_each_entry(dev, &subordinate->devices, bus_list) {
  492. if (dev->ignore_hotplug) {
  493. ctrl_dbg(ctrl, "ignoring hotplug event %#06x (%s requested no hotplug)\n",
  494. intr_loc, pci_name(dev));
  495. return IRQ_HANDLED;
  496. }
  497. }
  498. }
  499. if (!(intr_loc & ~PCI_EXP_SLTSTA_CC))
  500. return IRQ_HANDLED;
  501. /* Check Attention Button Pressed */
  502. if (intr_loc & PCI_EXP_SLTSTA_ABP) {
  503. ctrl_info(ctrl, "Button pressed on Slot(%s)\n",
  504. slot_name(slot));
  505. pciehp_queue_interrupt_event(slot, INT_BUTTON_PRESS);
  506. }
  507. /* Check Presence Detect Changed */
  508. if (intr_loc & PCI_EXP_SLTSTA_PDC) {
  509. pciehp_get_adapter_status(slot, &present);
  510. ctrl_info(ctrl, "Card %spresent on Slot(%s)\n",
  511. present ? "" : "not ", slot_name(slot));
  512. pciehp_queue_interrupt_event(slot, present ? INT_PRESENCE_ON :
  513. INT_PRESENCE_OFF);
  514. }
  515. /* Check Power Fault Detected */
  516. if ((intr_loc & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
  517. ctrl->power_fault_detected = 1;
  518. ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(slot));
  519. pciehp_queue_interrupt_event(slot, INT_POWER_FAULT);
  520. }
  521. if (intr_loc & PCI_EXP_SLTSTA_DLLSC) {
  522. link = pciehp_check_link_active(ctrl);
  523. ctrl_info(ctrl, "slot(%s): Link %s event\n",
  524. slot_name(slot), link ? "Up" : "Down");
  525. pciehp_queue_interrupt_event(slot, link ? INT_LINK_UP :
  526. INT_LINK_DOWN);
  527. }
  528. return IRQ_HANDLED;
  529. }
  530. static void pcie_enable_notification(struct controller *ctrl)
  531. {
  532. u16 cmd, mask;
  533. /*
  534. * TBD: Power fault detected software notification support.
  535. *
  536. * Power fault detected software notification is not enabled
  537. * now, because it caused power fault detected interrupt storm
  538. * on some machines. On those machines, power fault detected
  539. * bit in the slot status register was set again immediately
  540. * when it is cleared in the interrupt service routine, and
  541. * next power fault detected interrupt was notified again.
  542. */
  543. /*
  544. * Always enable link events: thus link-up and link-down shall
  545. * always be treated as hotplug and unplug respectively. Enable
  546. * presence detect only if Attention Button is not present.
  547. */
  548. cmd = PCI_EXP_SLTCTL_DLLSCE;
  549. if (ATTN_BUTTN(ctrl))
  550. cmd |= PCI_EXP_SLTCTL_ABPE;
  551. else
  552. cmd |= PCI_EXP_SLTCTL_PDCE;
  553. if (!pciehp_poll_mode)
  554. cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
  555. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  556. PCI_EXP_SLTCTL_PFDE |
  557. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  558. PCI_EXP_SLTCTL_DLLSCE);
  559. pcie_write_cmd_nowait(ctrl, cmd, mask);
  560. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  561. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
  562. }
  563. void pcie_reenable_notification(struct controller *ctrl)
  564. {
  565. /*
  566. * Clear both Presence and Data Link Layer Changed to make sure
  567. * those events still fire after we have re-enabled them.
  568. */
  569. pcie_capability_write_word(ctrl->pcie->port, PCI_EXP_SLTSTA,
  570. PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  571. pcie_enable_notification(ctrl);
  572. }
  573. static void pcie_disable_notification(struct controller *ctrl)
  574. {
  575. u16 mask;
  576. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  577. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  578. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  579. PCI_EXP_SLTCTL_DLLSCE);
  580. pcie_write_cmd(ctrl, 0, mask);
  581. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  582. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
  583. }
  584. /*
  585. * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
  586. * bus reset of the bridge, but at the same time we want to ensure that it is
  587. * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
  588. * disable link state notification and presence detection change notification
  589. * momentarily, if we see that they could interfere. Also, clear any spurious
  590. * events after.
  591. */
  592. int pciehp_reset_slot(struct slot *slot, int probe)
  593. {
  594. struct controller *ctrl = slot->ctrl;
  595. struct pci_dev *pdev = ctrl_dev(ctrl);
  596. u16 stat_mask = 0, ctrl_mask = 0;
  597. if (probe)
  598. return 0;
  599. if (!ATTN_BUTTN(ctrl)) {
  600. ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
  601. stat_mask |= PCI_EXP_SLTSTA_PDC;
  602. }
  603. ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
  604. stat_mask |= PCI_EXP_SLTSTA_DLLSC;
  605. pcie_write_cmd(ctrl, 0, ctrl_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. if (pciehp_poll_mode)
  609. del_timer_sync(&ctrl->poll_timer);
  610. pci_reset_bridge_secondary_bus(ctrl->pcie->port);
  611. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
  612. pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
  613. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  614. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
  615. if (pciehp_poll_mode)
  616. int_poll_timeout(ctrl->poll_timer.data);
  617. return 0;
  618. }
  619. int pcie_init_notification(struct controller *ctrl)
  620. {
  621. if (pciehp_request_irq(ctrl))
  622. return -1;
  623. pcie_enable_notification(ctrl);
  624. ctrl->notification_enabled = 1;
  625. return 0;
  626. }
  627. void pcie_shutdown_notification(struct controller *ctrl)
  628. {
  629. if (ctrl->notification_enabled) {
  630. pcie_disable_notification(ctrl);
  631. pciehp_free_irq(ctrl);
  632. ctrl->notification_enabled = 0;
  633. }
  634. }
  635. static int pcie_init_slot(struct controller *ctrl)
  636. {
  637. struct slot *slot;
  638. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  639. if (!slot)
  640. return -ENOMEM;
  641. slot->wq = alloc_workqueue("pciehp-%u", 0, 0, PSN(ctrl));
  642. if (!slot->wq)
  643. goto abort;
  644. slot->ctrl = ctrl;
  645. mutex_init(&slot->lock);
  646. mutex_init(&slot->hotplug_lock);
  647. INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
  648. ctrl->slot = slot;
  649. return 0;
  650. abort:
  651. kfree(slot);
  652. return -ENOMEM;
  653. }
  654. static void pcie_cleanup_slot(struct controller *ctrl)
  655. {
  656. struct slot *slot = ctrl->slot;
  657. destroy_workqueue(slot->wq);
  658. kfree(slot);
  659. }
  660. static inline void dbg_ctrl(struct controller *ctrl)
  661. {
  662. struct pci_dev *pdev = ctrl->pcie->port;
  663. u16 reg16;
  664. if (!pciehp_debug)
  665. return;
  666. ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
  667. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &reg16);
  668. ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
  669. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &reg16);
  670. ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
  671. }
  672. #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
  673. struct controller *pcie_init(struct pcie_device *dev)
  674. {
  675. struct controller *ctrl;
  676. u32 slot_cap, link_cap;
  677. struct pci_dev *pdev = dev->port;
  678. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  679. if (!ctrl) {
  680. dev_err(&dev->device, "%s: Out of memory\n", __func__);
  681. goto abort;
  682. }
  683. ctrl->pcie = dev;
  684. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
  685. ctrl->slot_cap = slot_cap;
  686. mutex_init(&ctrl->ctrl_lock);
  687. init_waitqueue_head(&ctrl->queue);
  688. dbg_ctrl(ctrl);
  689. /* Check if Data Link Layer Link Active Reporting is implemented */
  690. pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
  691. if (link_cap & PCI_EXP_LNKCAP_DLLLARC)
  692. ctrl->link_active_reporting = 1;
  693. /* Clear all remaining event bits in Slot Status register */
  694. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  695. PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  696. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
  697. PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC);
  698. 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",
  699. (slot_cap & PCI_EXP_SLTCAP_PSN) >> 19,
  700. FLAG(slot_cap, PCI_EXP_SLTCAP_ABP),
  701. FLAG(slot_cap, PCI_EXP_SLTCAP_PCP),
  702. FLAG(slot_cap, PCI_EXP_SLTCAP_MRLSP),
  703. FLAG(slot_cap, PCI_EXP_SLTCAP_AIP),
  704. FLAG(slot_cap, PCI_EXP_SLTCAP_PIP),
  705. FLAG(slot_cap, PCI_EXP_SLTCAP_HPC),
  706. FLAG(slot_cap, PCI_EXP_SLTCAP_HPS),
  707. FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
  708. FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
  709. FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC));
  710. if (pcie_init_slot(ctrl))
  711. goto abort_ctrl;
  712. return ctrl;
  713. abort_ctrl:
  714. kfree(ctrl);
  715. abort:
  716. return NULL;
  717. }
  718. void pciehp_release_ctrl(struct controller *ctrl)
  719. {
  720. pcie_cleanup_slot(ctrl);
  721. kfree(ctrl);
  722. }