pciehp_hpc.c 22 KB

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