pciehp_hpc.c 22 KB

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