pciehp_hpc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. if (!ctrl->no_cmd_complete) {
  142. /*
  143. * After 1 sec and CMD_COMPLETED still not set, just
  144. * proceed forward to issue the next command according
  145. * to spec. Just print out the error message.
  146. */
  147. ctrl_dbg(ctrl, "CMD_COMPLETED not clear after 1 sec\n");
  148. } else if (!NO_CMD_CMPL(ctrl)) {
  149. /*
  150. * This controller seems to notify of command completed
  151. * event even though it supports none of power
  152. * controller, attention led, power led and EMI.
  153. */
  154. ctrl_dbg(ctrl, "Unexpected CMD_COMPLETED. Need to "
  155. "wait for command completed event.\n");
  156. ctrl->no_cmd_complete = 0;
  157. } else {
  158. ctrl_dbg(ctrl, "Unexpected CMD_COMPLETED. Maybe "
  159. "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. static bool 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 (check_link_active(ctrl) == active)
  200. return;
  201. while (timeout > 0) {
  202. msleep(10);
  203. timeout -= 10;
  204. if (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 void pcie_wait_link_not_active(struct controller *ctrl)
  215. {
  216. __pcie_wait_link_active(ctrl, false);
  217. }
  218. static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
  219. {
  220. u32 l;
  221. int count = 0;
  222. int delay = 1000, step = 20;
  223. bool found = false;
  224. do {
  225. found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
  226. count++;
  227. if (found)
  228. break;
  229. msleep(step);
  230. delay -= step;
  231. } while (delay > 0);
  232. if (count > 1 && pciehp_debug)
  233. printk(KERN_DEBUG "pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
  234. pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
  235. PCI_FUNC(devfn), count, step, l);
  236. return found;
  237. }
  238. int pciehp_check_link_status(struct controller *ctrl)
  239. {
  240. struct pci_dev *pdev = ctrl_dev(ctrl);
  241. bool found;
  242. u16 lnk_status;
  243. /*
  244. * Data Link Layer Link Active Reporting must be capable for
  245. * hot-plug capable downstream port. But old controller might
  246. * not implement it. In this case, we wait for 1000 ms.
  247. */
  248. if (ctrl->link_active_reporting)
  249. pcie_wait_link_active(ctrl);
  250. else
  251. msleep(1000);
  252. /* wait 100ms before read pci conf, and try in 1s */
  253. msleep(100);
  254. found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
  255. PCI_DEVFN(0, 0));
  256. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  257. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  258. if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
  259. !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
  260. ctrl_err(ctrl, "Link Training Error occurs \n");
  261. return -1;
  262. }
  263. pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);
  264. if (!found)
  265. return -1;
  266. return 0;
  267. }
  268. static int __pciehp_link_set(struct controller *ctrl, bool enable)
  269. {
  270. struct pci_dev *pdev = ctrl_dev(ctrl);
  271. u16 lnk_ctrl;
  272. pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &lnk_ctrl);
  273. if (enable)
  274. lnk_ctrl &= ~PCI_EXP_LNKCTL_LD;
  275. else
  276. lnk_ctrl |= PCI_EXP_LNKCTL_LD;
  277. pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, lnk_ctrl);
  278. ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
  279. return 0;
  280. }
  281. static int pciehp_link_enable(struct controller *ctrl)
  282. {
  283. return __pciehp_link_set(ctrl, true);
  284. }
  285. static int pciehp_link_disable(struct controller *ctrl)
  286. {
  287. return __pciehp_link_set(ctrl, false);
  288. }
  289. void pciehp_get_attention_status(struct slot *slot, u8 *status)
  290. {
  291. struct controller *ctrl = slot->ctrl;
  292. struct pci_dev *pdev = ctrl_dev(ctrl);
  293. u16 slot_ctrl;
  294. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  295. ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
  296. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  297. switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
  298. case PCI_EXP_SLTCTL_ATTN_IND_ON:
  299. *status = 1; /* On */
  300. break;
  301. case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
  302. *status = 2; /* Blink */
  303. break;
  304. case PCI_EXP_SLTCTL_ATTN_IND_OFF:
  305. *status = 0; /* Off */
  306. break;
  307. default:
  308. *status = 0xFF;
  309. break;
  310. }
  311. }
  312. void pciehp_get_power_status(struct slot *slot, u8 *status)
  313. {
  314. struct controller *ctrl = slot->ctrl;
  315. struct pci_dev *pdev = ctrl_dev(ctrl);
  316. u16 slot_ctrl;
  317. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  318. ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
  319. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  320. switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
  321. case PCI_EXP_SLTCTL_PWR_ON:
  322. *status = 1; /* On */
  323. break;
  324. case PCI_EXP_SLTCTL_PWR_OFF:
  325. *status = 0; /* Off */
  326. break;
  327. default:
  328. *status = 0xFF;
  329. break;
  330. }
  331. }
  332. void pciehp_get_latch_status(struct slot *slot, u8 *status)
  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. *status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
  338. }
  339. void pciehp_get_adapter_status(struct slot *slot, u8 *status)
  340. {
  341. struct pci_dev *pdev = ctrl_dev(slot->ctrl);
  342. u16 slot_status;
  343. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  344. *status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
  345. }
  346. int pciehp_query_power_fault(struct slot *slot)
  347. {
  348. struct pci_dev *pdev = ctrl_dev(slot->ctrl);
  349. u16 slot_status;
  350. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  351. return !!(slot_status & PCI_EXP_SLTSTA_PFD);
  352. }
  353. void pciehp_set_attention_status(struct slot *slot, u8 value)
  354. {
  355. struct controller *ctrl = slot->ctrl;
  356. u16 slot_cmd;
  357. if (!ATTN_LED(ctrl))
  358. return;
  359. switch (value) {
  360. case 0 : /* turn off */
  361. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
  362. break;
  363. case 1: /* turn on */
  364. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_ON;
  365. break;
  366. case 2: /* turn blink */
  367. slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_BLINK;
  368. break;
  369. default:
  370. return;
  371. }
  372. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  373. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  374. pcie_write_cmd(ctrl, slot_cmd, PCI_EXP_SLTCTL_AIC);
  375. }
  376. void pciehp_green_led_on(struct slot *slot)
  377. {
  378. struct controller *ctrl = slot->ctrl;
  379. if (!PWR_LED(ctrl))
  380. return;
  381. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON, PCI_EXP_SLTCTL_PIC);
  382. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  383. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  384. PCI_EXP_SLTCTL_PWR_IND_ON);
  385. }
  386. void pciehp_green_led_off(struct slot *slot)
  387. {
  388. struct controller *ctrl = slot->ctrl;
  389. if (!PWR_LED(ctrl))
  390. return;
  391. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF, PCI_EXP_SLTCTL_PIC);
  392. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  393. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  394. PCI_EXP_SLTCTL_PWR_IND_OFF);
  395. }
  396. void pciehp_green_led_blink(struct slot *slot)
  397. {
  398. struct controller *ctrl = slot->ctrl;
  399. if (!PWR_LED(ctrl))
  400. return;
  401. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK, PCI_EXP_SLTCTL_PIC);
  402. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  403. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  404. PCI_EXP_SLTCTL_PWR_IND_BLINK);
  405. }
  406. int pciehp_power_on_slot(struct slot * slot)
  407. {
  408. struct controller *ctrl = slot->ctrl;
  409. struct pci_dev *pdev = ctrl_dev(ctrl);
  410. u16 slot_status;
  411. int retval;
  412. /* Clear sticky power-fault bit from previous power failures */
  413. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  414. if (slot_status & PCI_EXP_SLTSTA_PFD)
  415. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  416. PCI_EXP_SLTSTA_PFD);
  417. ctrl->power_fault_detected = 0;
  418. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
  419. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  420. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  421. PCI_EXP_SLTCTL_PWR_ON);
  422. retval = pciehp_link_enable(ctrl);
  423. if (retval)
  424. ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
  425. return retval;
  426. }
  427. void pciehp_power_off_slot(struct slot * slot)
  428. {
  429. struct controller *ctrl = slot->ctrl;
  430. /* Disable the link at first */
  431. pciehp_link_disable(ctrl);
  432. /* wait the link is down */
  433. if (ctrl->link_active_reporting)
  434. pcie_wait_link_not_active(ctrl);
  435. else
  436. msleep(1000);
  437. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
  438. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  439. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  440. PCI_EXP_SLTCTL_PWR_OFF);
  441. }
  442. static irqreturn_t pcie_isr(int irq, void *dev_id)
  443. {
  444. struct controller *ctrl = (struct controller *)dev_id;
  445. struct pci_dev *pdev = ctrl_dev(ctrl);
  446. struct slot *slot = ctrl->slot;
  447. u16 detected, intr_loc;
  448. /*
  449. * In order to guarantee that all interrupt events are
  450. * serviced, we need to re-inspect Slot Status register after
  451. * clearing what is presumed to be the last pending interrupt.
  452. */
  453. intr_loc = 0;
  454. do {
  455. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &detected);
  456. detected &= (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  457. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
  458. PCI_EXP_SLTSTA_CC);
  459. detected &= ~intr_loc;
  460. intr_loc |= detected;
  461. if (!intr_loc)
  462. return IRQ_NONE;
  463. if (detected)
  464. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  465. intr_loc);
  466. } while (detected);
  467. ctrl_dbg(ctrl, "%s: intr_loc %x\n", __func__, intr_loc);
  468. /* Check Command Complete Interrupt Pending */
  469. if (intr_loc & PCI_EXP_SLTSTA_CC) {
  470. ctrl->cmd_busy = 0;
  471. smp_mb();
  472. wake_up(&ctrl->queue);
  473. }
  474. if (!(intr_loc & ~PCI_EXP_SLTSTA_CC))
  475. return IRQ_HANDLED;
  476. /* Check MRL Sensor Changed */
  477. if (intr_loc & PCI_EXP_SLTSTA_MRLSC)
  478. pciehp_handle_switch_change(slot);
  479. /* Check Attention Button Pressed */
  480. if (intr_loc & PCI_EXP_SLTSTA_ABP)
  481. pciehp_handle_attention_button(slot);
  482. /* Check Presence Detect Changed */
  483. if (intr_loc & PCI_EXP_SLTSTA_PDC)
  484. pciehp_handle_presence_change(slot);
  485. /* Check Power Fault Detected */
  486. if ((intr_loc & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
  487. ctrl->power_fault_detected = 1;
  488. pciehp_handle_power_fault(slot);
  489. }
  490. return IRQ_HANDLED;
  491. }
  492. void pcie_enable_notification(struct controller *ctrl)
  493. {
  494. u16 cmd, mask;
  495. /*
  496. * TBD: Power fault detected software notification support.
  497. *
  498. * Power fault detected software notification is not enabled
  499. * now, because it caused power fault detected interrupt storm
  500. * on some machines. On those machines, power fault detected
  501. * bit in the slot status register was set again immediately
  502. * when it is cleared in the interrupt service routine, and
  503. * next power fault detected interrupt was notified again.
  504. */
  505. cmd = PCI_EXP_SLTCTL_PDCE;
  506. if (ATTN_BUTTN(ctrl))
  507. cmd |= PCI_EXP_SLTCTL_ABPE;
  508. if (MRL_SENS(ctrl))
  509. cmd |= PCI_EXP_SLTCTL_MRLSCE;
  510. if (!pciehp_poll_mode)
  511. cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
  512. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  513. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  514. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE);
  515. pcie_write_cmd(ctrl, cmd, mask);
  516. }
  517. static void pcie_disable_notification(struct controller *ctrl)
  518. {
  519. u16 mask;
  520. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  521. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  522. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  523. PCI_EXP_SLTCTL_DLLSCE);
  524. pcie_write_cmd(ctrl, 0, mask);
  525. }
  526. /*
  527. * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
  528. * bus reset of the bridge, but if the slot supports surprise removal we need
  529. * to disable presence detection around the bus reset and clear any spurious
  530. * events after.
  531. */
  532. int pciehp_reset_slot(struct slot *slot, int probe)
  533. {
  534. struct controller *ctrl = slot->ctrl;
  535. struct pci_dev *pdev = ctrl_dev(ctrl);
  536. if (probe)
  537. return 0;
  538. if (HP_SUPR_RM(ctrl)) {
  539. pcie_write_cmd(ctrl, 0, PCI_EXP_SLTCTL_PDCE);
  540. if (pciehp_poll_mode)
  541. del_timer_sync(&ctrl->poll_timer);
  542. }
  543. pci_reset_bridge_secondary_bus(ctrl->pcie->port);
  544. if (HP_SUPR_RM(ctrl)) {
  545. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  546. PCI_EXP_SLTSTA_PDC);
  547. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PDCE, PCI_EXP_SLTCTL_PDCE);
  548. if (pciehp_poll_mode)
  549. int_poll_timeout(ctrl->poll_timer.data);
  550. }
  551. return 0;
  552. }
  553. int pcie_init_notification(struct controller *ctrl)
  554. {
  555. if (pciehp_request_irq(ctrl))
  556. return -1;
  557. pcie_enable_notification(ctrl);
  558. ctrl->notification_enabled = 1;
  559. return 0;
  560. }
  561. static void pcie_shutdown_notification(struct controller *ctrl)
  562. {
  563. if (ctrl->notification_enabled) {
  564. pcie_disable_notification(ctrl);
  565. pciehp_free_irq(ctrl);
  566. ctrl->notification_enabled = 0;
  567. }
  568. }
  569. static int pcie_init_slot(struct controller *ctrl)
  570. {
  571. struct slot *slot;
  572. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  573. if (!slot)
  574. return -ENOMEM;
  575. slot->wq = alloc_workqueue("pciehp-%u", 0, 0, PSN(ctrl));
  576. if (!slot->wq)
  577. goto abort;
  578. slot->ctrl = ctrl;
  579. mutex_init(&slot->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. }