pciehp_hpc.c 22 KB

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