pciehp_hpc.c 22 KB

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