pciehp_hpc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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 int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
  42. {
  43. struct pci_dev *dev = ctrl->pcie->port;
  44. return pci_read_config_word(dev, pci_pcie_cap(dev) + reg, value);
  45. }
  46. static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
  47. {
  48. struct pci_dev *dev = ctrl->pcie->port;
  49. return pci_read_config_dword(dev, pci_pcie_cap(dev) + reg, value);
  50. }
  51. static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
  52. {
  53. struct pci_dev *dev = ctrl->pcie->port;
  54. return pci_write_config_word(dev, pci_pcie_cap(dev) + reg, value);
  55. }
  56. static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
  57. {
  58. struct pci_dev *dev = ctrl->pcie->port;
  59. return pci_write_config_dword(dev, pci_pcie_cap(dev) + reg, value);
  60. }
  61. /* Power Control Command */
  62. #define POWER_ON 0
  63. #define POWER_OFF PCI_EXP_SLTCTL_PCC
  64. static irqreturn_t pcie_isr(int irq, void *dev_id);
  65. static void start_int_poll_timer(struct controller *ctrl, int sec);
  66. /* This is the interrupt polling timeout function. */
  67. static void int_poll_timeout(unsigned long data)
  68. {
  69. struct controller *ctrl = (struct controller *)data;
  70. /* Poll for interrupt events. regs == NULL => polling */
  71. pcie_isr(0, ctrl);
  72. init_timer(&ctrl->poll_timer);
  73. if (!pciehp_poll_time)
  74. pciehp_poll_time = 2; /* default polling interval is 2 sec */
  75. start_int_poll_timer(ctrl, pciehp_poll_time);
  76. }
  77. /* This function starts the interrupt polling timer. */
  78. static void start_int_poll_timer(struct controller *ctrl, int sec)
  79. {
  80. /* Clamp to sane value */
  81. if ((sec <= 0) || (sec > 60))
  82. sec = 2;
  83. ctrl->poll_timer.function = &int_poll_timeout;
  84. ctrl->poll_timer.data = (unsigned long)ctrl;
  85. ctrl->poll_timer.expires = jiffies + sec * HZ;
  86. add_timer(&ctrl->poll_timer);
  87. }
  88. static inline int pciehp_request_irq(struct controller *ctrl)
  89. {
  90. int retval, irq = ctrl->pcie->irq;
  91. /* Install interrupt polling timer. Start with 10 sec delay */
  92. if (pciehp_poll_mode) {
  93. init_timer(&ctrl->poll_timer);
  94. start_int_poll_timer(ctrl, 10);
  95. return 0;
  96. }
  97. /* Installs the interrupt handler */
  98. retval = request_irq(irq, pcie_isr, IRQF_SHARED, MY_NAME, ctrl);
  99. if (retval)
  100. ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
  101. irq);
  102. return retval;
  103. }
  104. static inline void pciehp_free_irq(struct controller *ctrl)
  105. {
  106. if (pciehp_poll_mode)
  107. del_timer_sync(&ctrl->poll_timer);
  108. else
  109. free_irq(ctrl->pcie->irq, ctrl);
  110. }
  111. static int pcie_poll_cmd(struct controller *ctrl)
  112. {
  113. u16 slot_status;
  114. int err, timeout = 1000;
  115. err = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  116. if (!err && (slot_status & PCI_EXP_SLTSTA_CC)) {
  117. pciehp_writew(ctrl, PCI_EXP_SLTSTA, PCI_EXP_SLTSTA_CC);
  118. return 1;
  119. }
  120. while (timeout > 0) {
  121. msleep(10);
  122. timeout -= 10;
  123. err = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  124. if (!err && (slot_status & PCI_EXP_SLTSTA_CC)) {
  125. pciehp_writew(ctrl, PCI_EXP_SLTSTA, PCI_EXP_SLTSTA_CC);
  126. return 1;
  127. }
  128. }
  129. return 0; /* timeout */
  130. }
  131. static void pcie_wait_cmd(struct controller *ctrl, int poll)
  132. {
  133. unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
  134. unsigned long timeout = msecs_to_jiffies(msecs);
  135. int rc;
  136. if (poll)
  137. rc = pcie_poll_cmd(ctrl);
  138. else
  139. rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
  140. if (!rc)
  141. ctrl_dbg(ctrl, "Command not completed in 1000 msec\n");
  142. }
  143. /**
  144. * pcie_write_cmd - Issue controller command
  145. * @ctrl: controller to which the command is issued
  146. * @cmd: command value written to slot control register
  147. * @mask: bitmask of slot control register to be modified
  148. */
  149. static int pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
  150. {
  151. int retval = 0;
  152. u16 slot_status;
  153. u16 slot_ctrl;
  154. mutex_lock(&ctrl->ctrl_lock);
  155. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  156. if (retval) {
  157. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  158. __func__);
  159. goto out;
  160. }
  161. if (slot_status & PCI_EXP_SLTSTA_CC) {
  162. if (!ctrl->no_cmd_complete) {
  163. /*
  164. * After 1 sec and CMD_COMPLETED still not set, just
  165. * proceed forward to issue the next command according
  166. * to spec. Just print out the error message.
  167. */
  168. ctrl_dbg(ctrl, "CMD_COMPLETED not clear after 1 sec\n");
  169. } else if (!NO_CMD_CMPL(ctrl)) {
  170. /*
  171. * This controller semms to notify of command completed
  172. * event even though it supports none of power
  173. * controller, attention led, power led and EMI.
  174. */
  175. ctrl_dbg(ctrl, "Unexpected CMD_COMPLETED. Need to "
  176. "wait for command completed event.\n");
  177. ctrl->no_cmd_complete = 0;
  178. } else {
  179. ctrl_dbg(ctrl, "Unexpected CMD_COMPLETED. Maybe "
  180. "the controller is broken.\n");
  181. }
  182. }
  183. retval = pciehp_readw(ctrl, PCI_EXP_SLTCTL, &slot_ctrl);
  184. if (retval) {
  185. ctrl_err(ctrl, "%s: Cannot read SLOTCTRL register\n", __func__);
  186. goto out;
  187. }
  188. slot_ctrl &= ~mask;
  189. slot_ctrl |= (cmd & mask);
  190. ctrl->cmd_busy = 1;
  191. smp_mb();
  192. retval = pciehp_writew(ctrl, PCI_EXP_SLTCTL, slot_ctrl);
  193. if (retval)
  194. ctrl_err(ctrl, "Cannot write to SLOTCTRL register\n");
  195. /*
  196. * Wait for command completion.
  197. */
  198. if (!retval && !ctrl->no_cmd_complete) {
  199. int poll = 0;
  200. /*
  201. * if hotplug interrupt is not enabled or command
  202. * completed interrupt is not enabled, we need to poll
  203. * command completed event.
  204. */
  205. if (!(slot_ctrl & PCI_EXP_SLTCTL_HPIE) ||
  206. !(slot_ctrl & PCI_EXP_SLTCTL_CCIE))
  207. poll = 1;
  208. pcie_wait_cmd(ctrl, poll);
  209. }
  210. out:
  211. mutex_unlock(&ctrl->ctrl_lock);
  212. return retval;
  213. }
  214. static bool check_link_active(struct controller *ctrl)
  215. {
  216. bool ret = false;
  217. u16 lnk_status;
  218. if (pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status))
  219. return ret;
  220. ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
  221. if (ret)
  222. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  223. return ret;
  224. }
  225. static void __pcie_wait_link_active(struct controller *ctrl, bool active)
  226. {
  227. int timeout = 1000;
  228. if (check_link_active(ctrl) == active)
  229. return;
  230. while (timeout > 0) {
  231. msleep(10);
  232. timeout -= 10;
  233. if (check_link_active(ctrl) == active)
  234. return;
  235. }
  236. ctrl_dbg(ctrl, "Data Link Layer Link Active not %s in 1000 msec\n",
  237. active ? "set" : "cleared");
  238. }
  239. static void pcie_wait_link_active(struct controller *ctrl)
  240. {
  241. __pcie_wait_link_active(ctrl, true);
  242. }
  243. static void pcie_wait_link_not_active(struct controller *ctrl)
  244. {
  245. __pcie_wait_link_active(ctrl, false);
  246. }
  247. static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
  248. {
  249. u32 l;
  250. int count = 0;
  251. int delay = 1000, step = 20;
  252. bool found = false;
  253. do {
  254. found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
  255. count++;
  256. if (found)
  257. break;
  258. msleep(step);
  259. delay -= step;
  260. } while (delay > 0);
  261. if (count > 1 && pciehp_debug)
  262. printk(KERN_DEBUG "pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
  263. pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
  264. PCI_FUNC(devfn), count, step, l);
  265. return found;
  266. }
  267. int pciehp_check_link_status(struct controller *ctrl)
  268. {
  269. u16 lnk_status;
  270. int retval = 0;
  271. bool found = false;
  272. /*
  273. * Data Link Layer Link Active Reporting must be capable for
  274. * hot-plug capable downstream port. But old controller might
  275. * not implement it. In this case, we wait for 1000 ms.
  276. */
  277. if (ctrl->link_active_reporting)
  278. pcie_wait_link_active(ctrl);
  279. else
  280. msleep(1000);
  281. /* wait 100ms before read pci conf, and try in 1s */
  282. msleep(100);
  283. found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
  284. PCI_DEVFN(0, 0));
  285. retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status);
  286. if (retval) {
  287. ctrl_err(ctrl, "Cannot read LNKSTATUS register\n");
  288. return retval;
  289. }
  290. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  291. if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
  292. !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
  293. ctrl_err(ctrl, "Link Training Error occurs \n");
  294. retval = -1;
  295. return retval;
  296. }
  297. pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);
  298. if (!found && !retval)
  299. retval = -1;
  300. return retval;
  301. }
  302. static int __pciehp_link_set(struct controller *ctrl, bool enable)
  303. {
  304. u16 lnk_ctrl;
  305. int retval = 0;
  306. retval = pciehp_readw(ctrl, PCI_EXP_LNKCTL, &lnk_ctrl);
  307. if (retval) {
  308. ctrl_err(ctrl, "Cannot read LNKCTRL register\n");
  309. return retval;
  310. }
  311. if (enable)
  312. lnk_ctrl &= ~PCI_EXP_LNKCTL_LD;
  313. else
  314. lnk_ctrl |= PCI_EXP_LNKCTL_LD;
  315. retval = pciehp_writew(ctrl, PCI_EXP_LNKCTL, lnk_ctrl);
  316. if (retval) {
  317. ctrl_err(ctrl, "Cannot write LNKCTRL register\n");
  318. return retval;
  319. }
  320. ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
  321. return retval;
  322. }
  323. static int pciehp_link_enable(struct controller *ctrl)
  324. {
  325. return __pciehp_link_set(ctrl, true);
  326. }
  327. static int pciehp_link_disable(struct controller *ctrl)
  328. {
  329. return __pciehp_link_set(ctrl, false);
  330. }
  331. int pciehp_get_attention_status(struct slot *slot, u8 *status)
  332. {
  333. struct controller *ctrl = slot->ctrl;
  334. u16 slot_ctrl;
  335. u8 atten_led_state;
  336. int retval = 0;
  337. retval = pciehp_readw(ctrl, PCI_EXP_SLTCTL, &slot_ctrl);
  338. if (retval) {
  339. ctrl_err(ctrl, "%s: Cannot read SLOTCTRL register\n", __func__);
  340. return retval;
  341. }
  342. ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
  343. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  344. atten_led_state = (slot_ctrl & PCI_EXP_SLTCTL_AIC) >> 6;
  345. switch (atten_led_state) {
  346. case 0:
  347. *status = 0xFF; /* Reserved */
  348. break;
  349. case 1:
  350. *status = 1; /* On */
  351. break;
  352. case 2:
  353. *status = 2; /* Blink */
  354. break;
  355. case 3:
  356. *status = 0; /* Off */
  357. break;
  358. default:
  359. *status = 0xFF;
  360. break;
  361. }
  362. return 0;
  363. }
  364. int pciehp_get_power_status(struct slot *slot, u8 *status)
  365. {
  366. struct controller *ctrl = slot->ctrl;
  367. u16 slot_ctrl;
  368. u8 pwr_state;
  369. int retval = 0;
  370. retval = pciehp_readw(ctrl, PCI_EXP_SLTCTL, &slot_ctrl);
  371. if (retval) {
  372. ctrl_err(ctrl, "%s: Cannot read SLOTCTRL register\n", __func__);
  373. return retval;
  374. }
  375. ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
  376. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  377. pwr_state = (slot_ctrl & PCI_EXP_SLTCTL_PCC) >> 10;
  378. switch (pwr_state) {
  379. case 0:
  380. *status = 1;
  381. break;
  382. case 1:
  383. *status = 0;
  384. break;
  385. default:
  386. *status = 0xFF;
  387. break;
  388. }
  389. return retval;
  390. }
  391. int pciehp_get_latch_status(struct slot *slot, u8 *status)
  392. {
  393. struct controller *ctrl = slot->ctrl;
  394. u16 slot_status;
  395. int retval;
  396. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  397. if (retval) {
  398. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  399. __func__);
  400. return retval;
  401. }
  402. *status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
  403. return 0;
  404. }
  405. int pciehp_get_adapter_status(struct slot *slot, u8 *status)
  406. {
  407. struct controller *ctrl = slot->ctrl;
  408. u16 slot_status;
  409. int retval;
  410. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  411. if (retval) {
  412. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  413. __func__);
  414. return retval;
  415. }
  416. *status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
  417. return 0;
  418. }
  419. int pciehp_query_power_fault(struct slot *slot)
  420. {
  421. struct controller *ctrl = slot->ctrl;
  422. u16 slot_status;
  423. int retval;
  424. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  425. if (retval) {
  426. ctrl_err(ctrl, "Cannot check for power fault\n");
  427. return retval;
  428. }
  429. return !!(slot_status & PCI_EXP_SLTSTA_PFD);
  430. }
  431. int pciehp_set_attention_status(struct slot *slot, u8 value)
  432. {
  433. struct controller *ctrl = slot->ctrl;
  434. u16 slot_cmd;
  435. u16 cmd_mask;
  436. cmd_mask = PCI_EXP_SLTCTL_AIC;
  437. switch (value) {
  438. case 0 : /* turn off */
  439. slot_cmd = 0x00C0;
  440. break;
  441. case 1: /* turn on */
  442. slot_cmd = 0x0040;
  443. break;
  444. case 2: /* turn blink */
  445. slot_cmd = 0x0080;
  446. break;
  447. default:
  448. return -EINVAL;
  449. }
  450. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  451. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  452. return pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  453. }
  454. void pciehp_green_led_on(struct slot *slot)
  455. {
  456. struct controller *ctrl = slot->ctrl;
  457. u16 slot_cmd;
  458. u16 cmd_mask;
  459. slot_cmd = 0x0100;
  460. cmd_mask = PCI_EXP_SLTCTL_PIC;
  461. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  462. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  463. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  464. }
  465. void pciehp_green_led_off(struct slot *slot)
  466. {
  467. struct controller *ctrl = slot->ctrl;
  468. u16 slot_cmd;
  469. u16 cmd_mask;
  470. slot_cmd = 0x0300;
  471. cmd_mask = PCI_EXP_SLTCTL_PIC;
  472. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  473. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  474. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  475. }
  476. void pciehp_green_led_blink(struct slot *slot)
  477. {
  478. struct controller *ctrl = slot->ctrl;
  479. u16 slot_cmd;
  480. u16 cmd_mask;
  481. slot_cmd = 0x0200;
  482. cmd_mask = PCI_EXP_SLTCTL_PIC;
  483. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  484. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  485. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  486. }
  487. int pciehp_power_on_slot(struct slot * slot)
  488. {
  489. struct controller *ctrl = slot->ctrl;
  490. u16 slot_cmd;
  491. u16 cmd_mask;
  492. u16 slot_status;
  493. int retval = 0;
  494. /* Clear sticky power-fault bit from previous power failures */
  495. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  496. if (retval) {
  497. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  498. __func__);
  499. return retval;
  500. }
  501. slot_status &= PCI_EXP_SLTSTA_PFD;
  502. if (slot_status) {
  503. retval = pciehp_writew(ctrl, PCI_EXP_SLTSTA, slot_status);
  504. if (retval) {
  505. ctrl_err(ctrl,
  506. "%s: Cannot write to SLOTSTATUS register\n",
  507. __func__);
  508. return retval;
  509. }
  510. }
  511. ctrl->power_fault_detected = 0;
  512. slot_cmd = POWER_ON;
  513. cmd_mask = PCI_EXP_SLTCTL_PCC;
  514. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  515. if (retval) {
  516. ctrl_err(ctrl, "Write %x command failed!\n", slot_cmd);
  517. return retval;
  518. }
  519. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  520. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  521. retval = pciehp_link_enable(ctrl);
  522. if (retval)
  523. ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
  524. return retval;
  525. }
  526. int pciehp_power_off_slot(struct slot * slot)
  527. {
  528. struct controller *ctrl = slot->ctrl;
  529. u16 slot_cmd;
  530. u16 cmd_mask;
  531. int retval;
  532. /* Disable the link at first */
  533. pciehp_link_disable(ctrl);
  534. /* wait the link is down */
  535. if (ctrl->link_active_reporting)
  536. pcie_wait_link_not_active(ctrl);
  537. else
  538. msleep(1000);
  539. slot_cmd = POWER_OFF;
  540. cmd_mask = PCI_EXP_SLTCTL_PCC;
  541. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  542. if (retval) {
  543. ctrl_err(ctrl, "Write command failed!\n");
  544. return retval;
  545. }
  546. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  547. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
  548. return 0;
  549. }
  550. static irqreturn_t pcie_isr(int irq, void *dev_id)
  551. {
  552. struct controller *ctrl = (struct controller *)dev_id;
  553. struct slot *slot = ctrl->slot;
  554. u16 detected, intr_loc;
  555. /*
  556. * In order to guarantee that all interrupt events are
  557. * serviced, we need to re-inspect Slot Status register after
  558. * clearing what is presumed to be the last pending interrupt.
  559. */
  560. intr_loc = 0;
  561. do {
  562. if (pciehp_readw(ctrl, PCI_EXP_SLTSTA, &detected)) {
  563. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS\n",
  564. __func__);
  565. return IRQ_NONE;
  566. }
  567. detected &= (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  568. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
  569. PCI_EXP_SLTSTA_CC);
  570. detected &= ~intr_loc;
  571. intr_loc |= detected;
  572. if (!intr_loc)
  573. return IRQ_NONE;
  574. if (detected && pciehp_writew(ctrl, PCI_EXP_SLTSTA, intr_loc)) {
  575. ctrl_err(ctrl, "%s: Cannot write to SLOTSTATUS\n",
  576. __func__);
  577. return IRQ_NONE;
  578. }
  579. } while (detected);
  580. ctrl_dbg(ctrl, "%s: intr_loc %x\n", __func__, intr_loc);
  581. /* Check Command Complete Interrupt Pending */
  582. if (intr_loc & PCI_EXP_SLTSTA_CC) {
  583. ctrl->cmd_busy = 0;
  584. smp_mb();
  585. wake_up(&ctrl->queue);
  586. }
  587. if (!(intr_loc & ~PCI_EXP_SLTSTA_CC))
  588. return IRQ_HANDLED;
  589. /* Check MRL Sensor Changed */
  590. if (intr_loc & PCI_EXP_SLTSTA_MRLSC)
  591. pciehp_handle_switch_change(slot);
  592. /* Check Attention Button Pressed */
  593. if (intr_loc & PCI_EXP_SLTSTA_ABP)
  594. pciehp_handle_attention_button(slot);
  595. /* Check Presence Detect Changed */
  596. if (intr_loc & PCI_EXP_SLTSTA_PDC)
  597. pciehp_handle_presence_change(slot);
  598. /* Check Power Fault Detected */
  599. if ((intr_loc & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
  600. ctrl->power_fault_detected = 1;
  601. pciehp_handle_power_fault(slot);
  602. }
  603. return IRQ_HANDLED;
  604. }
  605. int pciehp_get_max_lnk_width(struct slot *slot,
  606. enum pcie_link_width *value)
  607. {
  608. struct controller *ctrl = slot->ctrl;
  609. enum pcie_link_width lnk_wdth;
  610. u32 lnk_cap;
  611. int retval = 0;
  612. retval = pciehp_readl(ctrl, PCI_EXP_LNKCAP, &lnk_cap);
  613. if (retval) {
  614. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  615. return retval;
  616. }
  617. switch ((lnk_cap & PCI_EXP_LNKSTA_NLW) >> 4){
  618. case 0:
  619. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  620. break;
  621. case 1:
  622. lnk_wdth = PCIE_LNK_X1;
  623. break;
  624. case 2:
  625. lnk_wdth = PCIE_LNK_X2;
  626. break;
  627. case 4:
  628. lnk_wdth = PCIE_LNK_X4;
  629. break;
  630. case 8:
  631. lnk_wdth = PCIE_LNK_X8;
  632. break;
  633. case 12:
  634. lnk_wdth = PCIE_LNK_X12;
  635. break;
  636. case 16:
  637. lnk_wdth = PCIE_LNK_X16;
  638. break;
  639. case 32:
  640. lnk_wdth = PCIE_LNK_X32;
  641. break;
  642. default:
  643. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  644. break;
  645. }
  646. *value = lnk_wdth;
  647. ctrl_dbg(ctrl, "Max link width = %d\n", lnk_wdth);
  648. return retval;
  649. }
  650. int pciehp_get_cur_lnk_width(struct slot *slot,
  651. enum pcie_link_width *value)
  652. {
  653. struct controller *ctrl = slot->ctrl;
  654. enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  655. int retval = 0;
  656. u16 lnk_status;
  657. retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status);
  658. if (retval) {
  659. ctrl_err(ctrl, "%s: Cannot read LNKSTATUS register\n",
  660. __func__);
  661. return retval;
  662. }
  663. switch ((lnk_status & PCI_EXP_LNKSTA_NLW) >> 4){
  664. case 0:
  665. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  666. break;
  667. case 1:
  668. lnk_wdth = PCIE_LNK_X1;
  669. break;
  670. case 2:
  671. lnk_wdth = PCIE_LNK_X2;
  672. break;
  673. case 4:
  674. lnk_wdth = PCIE_LNK_X4;
  675. break;
  676. case 8:
  677. lnk_wdth = PCIE_LNK_X8;
  678. break;
  679. case 12:
  680. lnk_wdth = PCIE_LNK_X12;
  681. break;
  682. case 16:
  683. lnk_wdth = PCIE_LNK_X16;
  684. break;
  685. case 32:
  686. lnk_wdth = PCIE_LNK_X32;
  687. break;
  688. default:
  689. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  690. break;
  691. }
  692. *value = lnk_wdth;
  693. ctrl_dbg(ctrl, "Current link width = %d\n", lnk_wdth);
  694. return retval;
  695. }
  696. int pcie_enable_notification(struct controller *ctrl)
  697. {
  698. u16 cmd, mask;
  699. /*
  700. * TBD: Power fault detected software notification support.
  701. *
  702. * Power fault detected software notification is not enabled
  703. * now, because it caused power fault detected interrupt storm
  704. * on some machines. On those machines, power fault detected
  705. * bit in the slot status register was set again immediately
  706. * when it is cleared in the interrupt service routine, and
  707. * next power fault detected interrupt was notified again.
  708. */
  709. cmd = PCI_EXP_SLTCTL_PDCE;
  710. if (ATTN_BUTTN(ctrl))
  711. cmd |= PCI_EXP_SLTCTL_ABPE;
  712. if (MRL_SENS(ctrl))
  713. cmd |= PCI_EXP_SLTCTL_MRLSCE;
  714. if (!pciehp_poll_mode)
  715. cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
  716. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  717. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  718. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE);
  719. if (pcie_write_cmd(ctrl, cmd, mask)) {
  720. ctrl_err(ctrl, "Cannot enable software notification\n");
  721. return -1;
  722. }
  723. return 0;
  724. }
  725. static void pcie_disable_notification(struct controller *ctrl)
  726. {
  727. u16 mask;
  728. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  729. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  730. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  731. PCI_EXP_SLTCTL_DLLSCE);
  732. if (pcie_write_cmd(ctrl, 0, mask))
  733. ctrl_warn(ctrl, "Cannot disable software notification\n");
  734. }
  735. int pcie_init_notification(struct controller *ctrl)
  736. {
  737. if (pciehp_request_irq(ctrl))
  738. return -1;
  739. if (pcie_enable_notification(ctrl)) {
  740. pciehp_free_irq(ctrl);
  741. return -1;
  742. }
  743. ctrl->notification_enabled = 1;
  744. return 0;
  745. }
  746. static void pcie_shutdown_notification(struct controller *ctrl)
  747. {
  748. if (ctrl->notification_enabled) {
  749. pcie_disable_notification(ctrl);
  750. pciehp_free_irq(ctrl);
  751. ctrl->notification_enabled = 0;
  752. }
  753. }
  754. static int pcie_init_slot(struct controller *ctrl)
  755. {
  756. struct slot *slot;
  757. char name[32];
  758. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  759. if (!slot)
  760. return -ENOMEM;
  761. snprintf(name, sizeof(name), "pciehp-%u", PSN(ctrl));
  762. slot->wq = alloc_workqueue(name, 0, 0);
  763. if (!slot->wq)
  764. goto abort;
  765. slot->ctrl = ctrl;
  766. mutex_init(&slot->lock);
  767. INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
  768. ctrl->slot = slot;
  769. return 0;
  770. abort:
  771. kfree(slot);
  772. return -ENOMEM;
  773. }
  774. static void pcie_cleanup_slot(struct controller *ctrl)
  775. {
  776. struct slot *slot = ctrl->slot;
  777. cancel_delayed_work(&slot->work);
  778. destroy_workqueue(slot->wq);
  779. kfree(slot);
  780. }
  781. static inline void dbg_ctrl(struct controller *ctrl)
  782. {
  783. int i;
  784. u16 reg16;
  785. struct pci_dev *pdev = ctrl->pcie->port;
  786. if (!pciehp_debug)
  787. return;
  788. ctrl_info(ctrl, "Hotplug Controller:\n");
  789. ctrl_info(ctrl, " Seg/Bus/Dev/Func/IRQ : %s IRQ %d\n",
  790. pci_name(pdev), pdev->irq);
  791. ctrl_info(ctrl, " Vendor ID : 0x%04x\n", pdev->vendor);
  792. ctrl_info(ctrl, " Device ID : 0x%04x\n", pdev->device);
  793. ctrl_info(ctrl, " Subsystem ID : 0x%04x\n",
  794. pdev->subsystem_device);
  795. ctrl_info(ctrl, " Subsystem Vendor ID : 0x%04x\n",
  796. pdev->subsystem_vendor);
  797. ctrl_info(ctrl, " PCIe Cap offset : 0x%02x\n",
  798. pci_pcie_cap(pdev));
  799. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  800. if (!pci_resource_len(pdev, i))
  801. continue;
  802. ctrl_info(ctrl, " PCI resource [%d] : %pR\n",
  803. i, &pdev->resource[i]);
  804. }
  805. ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
  806. ctrl_info(ctrl, " Physical Slot Number : %d\n", PSN(ctrl));
  807. ctrl_info(ctrl, " Attention Button : %3s\n",
  808. ATTN_BUTTN(ctrl) ? "yes" : "no");
  809. ctrl_info(ctrl, " Power Controller : %3s\n",
  810. POWER_CTRL(ctrl) ? "yes" : "no");
  811. ctrl_info(ctrl, " MRL Sensor : %3s\n",
  812. MRL_SENS(ctrl) ? "yes" : "no");
  813. ctrl_info(ctrl, " Attention Indicator : %3s\n",
  814. ATTN_LED(ctrl) ? "yes" : "no");
  815. ctrl_info(ctrl, " Power Indicator : %3s\n",
  816. PWR_LED(ctrl) ? "yes" : "no");
  817. ctrl_info(ctrl, " Hot-Plug Surprise : %3s\n",
  818. HP_SUPR_RM(ctrl) ? "yes" : "no");
  819. ctrl_info(ctrl, " EMI Present : %3s\n",
  820. EMI(ctrl) ? "yes" : "no");
  821. ctrl_info(ctrl, " Command Completed : %3s\n",
  822. NO_CMD_CMPL(ctrl) ? "no" : "yes");
  823. pciehp_readw(ctrl, PCI_EXP_SLTSTA, &reg16);
  824. ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
  825. pciehp_readw(ctrl, PCI_EXP_SLTCTL, &reg16);
  826. ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
  827. }
  828. struct controller *pcie_init(struct pcie_device *dev)
  829. {
  830. struct controller *ctrl;
  831. u32 slot_cap, link_cap;
  832. struct pci_dev *pdev = dev->port;
  833. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  834. if (!ctrl) {
  835. dev_err(&dev->device, "%s: Out of memory\n", __func__);
  836. goto abort;
  837. }
  838. ctrl->pcie = dev;
  839. if (!pci_pcie_cap(pdev)) {
  840. ctrl_err(ctrl, "Cannot find PCI Express capability\n");
  841. goto abort_ctrl;
  842. }
  843. if (pciehp_readl(ctrl, PCI_EXP_SLTCAP, &slot_cap)) {
  844. ctrl_err(ctrl, "Cannot read SLOTCAP register\n");
  845. goto abort_ctrl;
  846. }
  847. ctrl->slot_cap = slot_cap;
  848. mutex_init(&ctrl->ctrl_lock);
  849. init_waitqueue_head(&ctrl->queue);
  850. dbg_ctrl(ctrl);
  851. /*
  852. * Controller doesn't notify of command completion if the "No
  853. * Command Completed Support" bit is set in Slot Capability
  854. * register or the controller supports none of power
  855. * controller, attention led, power led and EMI.
  856. */
  857. if (NO_CMD_CMPL(ctrl) ||
  858. !(POWER_CTRL(ctrl) | ATTN_LED(ctrl) | PWR_LED(ctrl) | EMI(ctrl)))
  859. ctrl->no_cmd_complete = 1;
  860. /* Check if Data Link Layer Link Active Reporting is implemented */
  861. if (pciehp_readl(ctrl, PCI_EXP_LNKCAP, &link_cap)) {
  862. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  863. goto abort_ctrl;
  864. }
  865. if (link_cap & PCI_EXP_LNKCAP_DLLLARC) {
  866. ctrl_dbg(ctrl, "Link Active Reporting supported\n");
  867. ctrl->link_active_reporting = 1;
  868. }
  869. /* Clear all remaining event bits in Slot Status register */
  870. if (pciehp_writew(ctrl, PCI_EXP_SLTSTA, 0x1f))
  871. goto abort_ctrl;
  872. /* Disable sotfware notification */
  873. pcie_disable_notification(ctrl);
  874. ctrl_info(ctrl, "HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
  875. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  876. pdev->subsystem_device);
  877. if (pcie_init_slot(ctrl))
  878. goto abort_ctrl;
  879. return ctrl;
  880. abort_ctrl:
  881. kfree(ctrl);
  882. abort:
  883. return NULL;
  884. }
  885. void pciehp_release_ctrl(struct controller *ctrl)
  886. {
  887. pcie_shutdown_notification(ctrl);
  888. pcie_cleanup_slot(ctrl);
  889. kfree(ctrl);
  890. }