pciehp_hpc.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 "../pci.h"
  39. #include "pciehp.h"
  40. static atomic_t pciehp_num_controllers = ATOMIC_INIT(0);
  41. static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
  42. {
  43. struct pci_dev *dev = ctrl->pci_dev;
  44. return pci_read_config_word(dev, ctrl->cap_base + reg, value);
  45. }
  46. static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
  47. {
  48. struct pci_dev *dev = ctrl->pci_dev;
  49. return pci_read_config_dword(dev, ctrl->cap_base + reg, value);
  50. }
  51. static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
  52. {
  53. struct pci_dev *dev = ctrl->pci_dev;
  54. return pci_write_config_word(dev, ctrl->cap_base + reg, value);
  55. }
  56. static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
  57. {
  58. struct pci_dev *dev = ctrl->pci_dev;
  59. return pci_write_config_dword(dev, ctrl->cap_base + 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 inline int check_link_active(struct controller *ctrl)
  215. {
  216. u16 link_status;
  217. if (pciehp_readw(ctrl, PCI_EXP_LNKSTA, &link_status))
  218. return 0;
  219. return !!(link_status & PCI_EXP_LNKSTA_DLLLA);
  220. }
  221. static void pcie_wait_link_active(struct controller *ctrl)
  222. {
  223. int timeout = 1000;
  224. if (check_link_active(ctrl))
  225. return;
  226. while (timeout > 0) {
  227. msleep(10);
  228. timeout -= 10;
  229. if (check_link_active(ctrl))
  230. return;
  231. }
  232. ctrl_dbg(ctrl, "Data Link Layer Link Active not set in 1000 msec\n");
  233. }
  234. static int hpc_check_lnk_status(struct controller *ctrl)
  235. {
  236. u16 lnk_status;
  237. int retval = 0;
  238. /*
  239. * Data Link Layer Link Active Reporting must be capable for
  240. * hot-plug capable downstream port. But old controller might
  241. * not implement it. In this case, we wait for 1000 ms.
  242. */
  243. if (ctrl->link_active_reporting){
  244. /* Wait for Data Link Layer Link Active bit to be set */
  245. pcie_wait_link_active(ctrl);
  246. /*
  247. * We must wait for 100 ms after the Data Link Layer
  248. * Link Active bit reads 1b before initiating a
  249. * configuration access to the hot added device.
  250. */
  251. msleep(100);
  252. } else
  253. msleep(1000);
  254. retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status);
  255. if (retval) {
  256. ctrl_err(ctrl, "Cannot read LNKSTATUS register\n");
  257. return retval;
  258. }
  259. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  260. if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
  261. !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
  262. ctrl_err(ctrl, "Link Training Error occurs \n");
  263. retval = -1;
  264. return retval;
  265. }
  266. return retval;
  267. }
  268. static int hpc_get_attention_status(struct slot *slot, u8 *status)
  269. {
  270. struct controller *ctrl = slot->ctrl;
  271. u16 slot_ctrl;
  272. u8 atten_led_state;
  273. int retval = 0;
  274. retval = pciehp_readw(ctrl, PCI_EXP_SLTCTL, &slot_ctrl);
  275. if (retval) {
  276. ctrl_err(ctrl, "%s: Cannot read SLOTCTRL register\n", __func__);
  277. return retval;
  278. }
  279. ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n",
  280. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_ctrl);
  281. atten_led_state = (slot_ctrl & PCI_EXP_SLTCTL_AIC) >> 6;
  282. switch (atten_led_state) {
  283. case 0:
  284. *status = 0xFF; /* Reserved */
  285. break;
  286. case 1:
  287. *status = 1; /* On */
  288. break;
  289. case 2:
  290. *status = 2; /* Blink */
  291. break;
  292. case 3:
  293. *status = 0; /* Off */
  294. break;
  295. default:
  296. *status = 0xFF;
  297. break;
  298. }
  299. return 0;
  300. }
  301. static int hpc_get_power_status(struct slot *slot, u8 *status)
  302. {
  303. struct controller *ctrl = slot->ctrl;
  304. u16 slot_ctrl;
  305. u8 pwr_state;
  306. int retval = 0;
  307. retval = pciehp_readw(ctrl, PCI_EXP_SLTCTL, &slot_ctrl);
  308. if (retval) {
  309. ctrl_err(ctrl, "%s: Cannot read SLOTCTRL register\n", __func__);
  310. return retval;
  311. }
  312. ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n",
  313. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_ctrl);
  314. pwr_state = (slot_ctrl & PCI_EXP_SLTCTL_PCC) >> 10;
  315. switch (pwr_state) {
  316. case 0:
  317. *status = 1;
  318. break;
  319. case 1:
  320. *status = 0;
  321. break;
  322. default:
  323. *status = 0xFF;
  324. break;
  325. }
  326. return retval;
  327. }
  328. static int hpc_get_latch_status(struct slot *slot, u8 *status)
  329. {
  330. struct controller *ctrl = slot->ctrl;
  331. u16 slot_status;
  332. int retval;
  333. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  334. if (retval) {
  335. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  336. __func__);
  337. return retval;
  338. }
  339. *status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
  340. return 0;
  341. }
  342. static int hpc_get_adapter_status(struct slot *slot, u8 *status)
  343. {
  344. struct controller *ctrl = slot->ctrl;
  345. u16 slot_status;
  346. int retval;
  347. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  348. if (retval) {
  349. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  350. __func__);
  351. return retval;
  352. }
  353. *status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
  354. return 0;
  355. }
  356. static int hpc_query_power_fault(struct slot *slot)
  357. {
  358. struct controller *ctrl = slot->ctrl;
  359. u16 slot_status;
  360. int retval;
  361. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  362. if (retval) {
  363. ctrl_err(ctrl, "Cannot check for power fault\n");
  364. return retval;
  365. }
  366. return !!(slot_status & PCI_EXP_SLTSTA_PFD);
  367. }
  368. static int hpc_get_emi_status(struct slot *slot, u8 *status)
  369. {
  370. struct controller *ctrl = slot->ctrl;
  371. u16 slot_status;
  372. int retval;
  373. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  374. if (retval) {
  375. ctrl_err(ctrl, "Cannot check EMI status\n");
  376. return retval;
  377. }
  378. *status = !!(slot_status & PCI_EXP_SLTSTA_EIS);
  379. return retval;
  380. }
  381. static int hpc_toggle_emi(struct slot *slot)
  382. {
  383. u16 slot_cmd;
  384. u16 cmd_mask;
  385. int rc;
  386. slot_cmd = PCI_EXP_SLTCTL_EIC;
  387. cmd_mask = PCI_EXP_SLTCTL_EIC;
  388. rc = pcie_write_cmd(slot->ctrl, slot_cmd, cmd_mask);
  389. slot->last_emi_toggle = get_seconds();
  390. return rc;
  391. }
  392. static int hpc_set_attention_status(struct slot *slot, u8 value)
  393. {
  394. struct controller *ctrl = slot->ctrl;
  395. u16 slot_cmd;
  396. u16 cmd_mask;
  397. int rc;
  398. cmd_mask = PCI_EXP_SLTCTL_AIC;
  399. switch (value) {
  400. case 0 : /* turn off */
  401. slot_cmd = 0x00C0;
  402. break;
  403. case 1: /* turn on */
  404. slot_cmd = 0x0040;
  405. break;
  406. case 2: /* turn blink */
  407. slot_cmd = 0x0080;
  408. break;
  409. default:
  410. return -1;
  411. }
  412. rc = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  413. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  414. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_cmd);
  415. return rc;
  416. }
  417. static void hpc_set_green_led_on(struct slot *slot)
  418. {
  419. struct controller *ctrl = slot->ctrl;
  420. u16 slot_cmd;
  421. u16 cmd_mask;
  422. slot_cmd = 0x0100;
  423. cmd_mask = PCI_EXP_SLTCTL_PIC;
  424. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  425. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  426. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_cmd);
  427. }
  428. static void hpc_set_green_led_off(struct slot *slot)
  429. {
  430. struct controller *ctrl = slot->ctrl;
  431. u16 slot_cmd;
  432. u16 cmd_mask;
  433. slot_cmd = 0x0300;
  434. cmd_mask = PCI_EXP_SLTCTL_PIC;
  435. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  436. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  437. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_cmd);
  438. }
  439. static void hpc_set_green_led_blink(struct slot *slot)
  440. {
  441. struct controller *ctrl = slot->ctrl;
  442. u16 slot_cmd;
  443. u16 cmd_mask;
  444. slot_cmd = 0x0200;
  445. cmd_mask = PCI_EXP_SLTCTL_PIC;
  446. pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  447. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  448. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_cmd);
  449. }
  450. static int hpc_power_on_slot(struct slot * slot)
  451. {
  452. struct controller *ctrl = slot->ctrl;
  453. u16 slot_cmd;
  454. u16 cmd_mask;
  455. u16 slot_status;
  456. int retval = 0;
  457. ctrl_dbg(ctrl, "%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  458. /* Clear sticky power-fault bit from previous power failures */
  459. retval = pciehp_readw(ctrl, PCI_EXP_SLTSTA, &slot_status);
  460. if (retval) {
  461. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS register\n",
  462. __func__);
  463. return retval;
  464. }
  465. slot_status &= PCI_EXP_SLTSTA_PFD;
  466. if (slot_status) {
  467. retval = pciehp_writew(ctrl, PCI_EXP_SLTSTA, slot_status);
  468. if (retval) {
  469. ctrl_err(ctrl,
  470. "%s: Cannot write to SLOTSTATUS register\n",
  471. __func__);
  472. return retval;
  473. }
  474. }
  475. slot_cmd = POWER_ON;
  476. cmd_mask = PCI_EXP_SLTCTL_PCC;
  477. /* Enable detection that we turned off at slot power-off time */
  478. if (!pciehp_poll_mode) {
  479. slot_cmd |= (PCI_EXP_SLTCTL_PFDE | PCI_EXP_SLTCTL_MRLSCE |
  480. PCI_EXP_SLTCTL_PDCE);
  481. cmd_mask |= (PCI_EXP_SLTCTL_PFDE | PCI_EXP_SLTCTL_MRLSCE |
  482. PCI_EXP_SLTCTL_PDCE);
  483. }
  484. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  485. if (retval) {
  486. ctrl_err(ctrl, "Write %x command failed!\n", slot_cmd);
  487. return -1;
  488. }
  489. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  490. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_cmd);
  491. return retval;
  492. }
  493. static inline int pcie_mask_bad_dllp(struct controller *ctrl)
  494. {
  495. struct pci_dev *dev = ctrl->pci_dev;
  496. int pos;
  497. u32 reg;
  498. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  499. if (!pos)
  500. return 0;
  501. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  502. if (reg & PCI_ERR_COR_BAD_DLLP)
  503. return 0;
  504. reg |= PCI_ERR_COR_BAD_DLLP;
  505. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  506. return 1;
  507. }
  508. static inline void pcie_unmask_bad_dllp(struct controller *ctrl)
  509. {
  510. struct pci_dev *dev = ctrl->pci_dev;
  511. u32 reg;
  512. int pos;
  513. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  514. if (!pos)
  515. return;
  516. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  517. if (!(reg & PCI_ERR_COR_BAD_DLLP))
  518. return;
  519. reg &= ~PCI_ERR_COR_BAD_DLLP;
  520. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  521. }
  522. static int hpc_power_off_slot(struct slot * slot)
  523. {
  524. struct controller *ctrl = slot->ctrl;
  525. u16 slot_cmd;
  526. u16 cmd_mask;
  527. int retval = 0;
  528. int changed;
  529. ctrl_dbg(ctrl, "%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  530. /*
  531. * Set Bad DLLP Mask bit in Correctable Error Mask
  532. * Register. This is the workaround against Bad DLLP error
  533. * that sometimes happens during turning power off the slot
  534. * which conforms to PCI Express 1.0a spec.
  535. */
  536. changed = pcie_mask_bad_dllp(ctrl);
  537. slot_cmd = POWER_OFF;
  538. cmd_mask = PCI_EXP_SLTCTL_PCC;
  539. /*
  540. * If we get MRL or presence detect interrupts now, the isr
  541. * will notice the sticky power-fault bit too and issue power
  542. * indicator change commands. This will lead to an endless loop
  543. * of command completions, since the power-fault bit remains on
  544. * till the slot is powered on again.
  545. */
  546. if (!pciehp_poll_mode) {
  547. slot_cmd &= ~(PCI_EXP_SLTCTL_PFDE | PCI_EXP_SLTCTL_MRLSCE |
  548. PCI_EXP_SLTCTL_PDCE);
  549. cmd_mask |= (PCI_EXP_SLTCTL_PFDE | PCI_EXP_SLTCTL_MRLSCE |
  550. PCI_EXP_SLTCTL_PDCE);
  551. }
  552. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  553. if (retval) {
  554. ctrl_err(ctrl, "Write command failed!\n");
  555. retval = -1;
  556. goto out;
  557. }
  558. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  559. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_cmd);
  560. out:
  561. if (changed)
  562. pcie_unmask_bad_dllp(ctrl);
  563. return retval;
  564. }
  565. static irqreturn_t pcie_isr(int irq, void *dev_id)
  566. {
  567. struct controller *ctrl = (struct controller *)dev_id;
  568. u16 detected, intr_loc;
  569. struct slot *p_slot;
  570. /*
  571. * In order to guarantee that all interrupt events are
  572. * serviced, we need to re-inspect Slot Status register after
  573. * clearing what is presumed to be the last pending interrupt.
  574. */
  575. intr_loc = 0;
  576. do {
  577. if (pciehp_readw(ctrl, PCI_EXP_SLTSTA, &detected)) {
  578. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS\n",
  579. __func__);
  580. return IRQ_NONE;
  581. }
  582. detected &= (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  583. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
  584. PCI_EXP_SLTSTA_CC);
  585. intr_loc |= detected;
  586. if (!intr_loc)
  587. return IRQ_NONE;
  588. if (detected && pciehp_writew(ctrl, PCI_EXP_SLTSTA, detected)) {
  589. ctrl_err(ctrl, "%s: Cannot write to SLOTSTATUS\n",
  590. __func__);
  591. return IRQ_NONE;
  592. }
  593. } while (detected);
  594. ctrl_dbg(ctrl, "%s: intr_loc %x\n", __func__, intr_loc);
  595. /* Check Command Complete Interrupt Pending */
  596. if (intr_loc & PCI_EXP_SLTSTA_CC) {
  597. ctrl->cmd_busy = 0;
  598. smp_mb();
  599. wake_up(&ctrl->queue);
  600. }
  601. if (!(intr_loc & ~PCI_EXP_SLTSTA_CC))
  602. return IRQ_HANDLED;
  603. p_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  604. /* Check MRL Sensor Changed */
  605. if (intr_loc & PCI_EXP_SLTSTA_MRLSC)
  606. pciehp_handle_switch_change(p_slot);
  607. /* Check Attention Button Pressed */
  608. if (intr_loc & PCI_EXP_SLTSTA_ABP)
  609. pciehp_handle_attention_button(p_slot);
  610. /* Check Presence Detect Changed */
  611. if (intr_loc & PCI_EXP_SLTSTA_PDC)
  612. pciehp_handle_presence_change(p_slot);
  613. /* Check Power Fault Detected */
  614. if (intr_loc & PCI_EXP_SLTSTA_PFD)
  615. pciehp_handle_power_fault(p_slot);
  616. return IRQ_HANDLED;
  617. }
  618. static int hpc_get_max_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  619. {
  620. struct controller *ctrl = slot->ctrl;
  621. enum pcie_link_speed lnk_speed;
  622. u32 lnk_cap;
  623. int retval = 0;
  624. retval = pciehp_readl(ctrl, PCI_EXP_LNKCAP, &lnk_cap);
  625. if (retval) {
  626. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  627. return retval;
  628. }
  629. switch (lnk_cap & 0x000F) {
  630. case 1:
  631. lnk_speed = PCIE_2PT5GB;
  632. break;
  633. default:
  634. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  635. break;
  636. }
  637. *value = lnk_speed;
  638. ctrl_dbg(ctrl, "Max link speed = %d\n", lnk_speed);
  639. return retval;
  640. }
  641. static int hpc_get_max_lnk_width(struct slot *slot,
  642. enum pcie_link_width *value)
  643. {
  644. struct controller *ctrl = slot->ctrl;
  645. enum pcie_link_width lnk_wdth;
  646. u32 lnk_cap;
  647. int retval = 0;
  648. retval = pciehp_readl(ctrl, PCI_EXP_LNKCAP, &lnk_cap);
  649. if (retval) {
  650. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  651. return retval;
  652. }
  653. switch ((lnk_cap & PCI_EXP_LNKSTA_NLW) >> 4){
  654. case 0:
  655. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  656. break;
  657. case 1:
  658. lnk_wdth = PCIE_LNK_X1;
  659. break;
  660. case 2:
  661. lnk_wdth = PCIE_LNK_X2;
  662. break;
  663. case 4:
  664. lnk_wdth = PCIE_LNK_X4;
  665. break;
  666. case 8:
  667. lnk_wdth = PCIE_LNK_X8;
  668. break;
  669. case 12:
  670. lnk_wdth = PCIE_LNK_X12;
  671. break;
  672. case 16:
  673. lnk_wdth = PCIE_LNK_X16;
  674. break;
  675. case 32:
  676. lnk_wdth = PCIE_LNK_X32;
  677. break;
  678. default:
  679. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  680. break;
  681. }
  682. *value = lnk_wdth;
  683. ctrl_dbg(ctrl, "Max link width = %d\n", lnk_wdth);
  684. return retval;
  685. }
  686. static int hpc_get_cur_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  687. {
  688. struct controller *ctrl = slot->ctrl;
  689. enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
  690. int retval = 0;
  691. u16 lnk_status;
  692. retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status);
  693. if (retval) {
  694. ctrl_err(ctrl, "%s: Cannot read LNKSTATUS register\n",
  695. __func__);
  696. return retval;
  697. }
  698. switch (lnk_status & PCI_EXP_LNKSTA_CLS) {
  699. case 1:
  700. lnk_speed = PCIE_2PT5GB;
  701. break;
  702. default:
  703. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  704. break;
  705. }
  706. *value = lnk_speed;
  707. ctrl_dbg(ctrl, "Current link speed = %d\n", lnk_speed);
  708. return retval;
  709. }
  710. static int hpc_get_cur_lnk_width(struct slot *slot,
  711. enum pcie_link_width *value)
  712. {
  713. struct controller *ctrl = slot->ctrl;
  714. enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  715. int retval = 0;
  716. u16 lnk_status;
  717. retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status);
  718. if (retval) {
  719. ctrl_err(ctrl, "%s: Cannot read LNKSTATUS register\n",
  720. __func__);
  721. return retval;
  722. }
  723. switch ((lnk_status & PCI_EXP_LNKSTA_NLW) >> 4){
  724. case 0:
  725. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  726. break;
  727. case 1:
  728. lnk_wdth = PCIE_LNK_X1;
  729. break;
  730. case 2:
  731. lnk_wdth = PCIE_LNK_X2;
  732. break;
  733. case 4:
  734. lnk_wdth = PCIE_LNK_X4;
  735. break;
  736. case 8:
  737. lnk_wdth = PCIE_LNK_X8;
  738. break;
  739. case 12:
  740. lnk_wdth = PCIE_LNK_X12;
  741. break;
  742. case 16:
  743. lnk_wdth = PCIE_LNK_X16;
  744. break;
  745. case 32:
  746. lnk_wdth = PCIE_LNK_X32;
  747. break;
  748. default:
  749. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  750. break;
  751. }
  752. *value = lnk_wdth;
  753. ctrl_dbg(ctrl, "Current link width = %d\n", lnk_wdth);
  754. return retval;
  755. }
  756. static void pcie_release_ctrl(struct controller *ctrl);
  757. static struct hpc_ops pciehp_hpc_ops = {
  758. .power_on_slot = hpc_power_on_slot,
  759. .power_off_slot = hpc_power_off_slot,
  760. .set_attention_status = hpc_set_attention_status,
  761. .get_power_status = hpc_get_power_status,
  762. .get_attention_status = hpc_get_attention_status,
  763. .get_latch_status = hpc_get_latch_status,
  764. .get_adapter_status = hpc_get_adapter_status,
  765. .get_emi_status = hpc_get_emi_status,
  766. .toggle_emi = hpc_toggle_emi,
  767. .get_max_bus_speed = hpc_get_max_lnk_speed,
  768. .get_cur_bus_speed = hpc_get_cur_lnk_speed,
  769. .get_max_lnk_width = hpc_get_max_lnk_width,
  770. .get_cur_lnk_width = hpc_get_cur_lnk_width,
  771. .query_power_fault = hpc_query_power_fault,
  772. .green_led_on = hpc_set_green_led_on,
  773. .green_led_off = hpc_set_green_led_off,
  774. .green_led_blink = hpc_set_green_led_blink,
  775. .release_ctlr = pcie_release_ctrl,
  776. .check_lnk_status = hpc_check_lnk_status,
  777. };
  778. int pcie_enable_notification(struct controller *ctrl)
  779. {
  780. u16 cmd, mask;
  781. cmd = PCI_EXP_SLTCTL_PDCE;
  782. if (ATTN_BUTTN(ctrl))
  783. cmd |= PCI_EXP_SLTCTL_ABPE;
  784. if (POWER_CTRL(ctrl))
  785. cmd |= PCI_EXP_SLTCTL_PFDE;
  786. if (MRL_SENS(ctrl))
  787. cmd |= PCI_EXP_SLTCTL_MRLSCE;
  788. if (!pciehp_poll_mode)
  789. cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
  790. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  791. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  792. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE);
  793. if (pcie_write_cmd(ctrl, cmd, mask)) {
  794. ctrl_err(ctrl, "Cannot enable software notification\n");
  795. return -1;
  796. }
  797. return 0;
  798. }
  799. static void pcie_disable_notification(struct controller *ctrl)
  800. {
  801. u16 mask;
  802. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  803. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  804. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE);
  805. if (pcie_write_cmd(ctrl, 0, mask))
  806. ctrl_warn(ctrl, "Cannot disable software notification\n");
  807. }
  808. int pcie_init_notification(struct controller *ctrl)
  809. {
  810. if (pciehp_request_irq(ctrl))
  811. return -1;
  812. if (pcie_enable_notification(ctrl)) {
  813. pciehp_free_irq(ctrl);
  814. return -1;
  815. }
  816. ctrl->notification_enabled = 1;
  817. return 0;
  818. }
  819. static void pcie_shutdown_notification(struct controller *ctrl)
  820. {
  821. if (ctrl->notification_enabled) {
  822. pcie_disable_notification(ctrl);
  823. pciehp_free_irq(ctrl);
  824. ctrl->notification_enabled = 0;
  825. }
  826. }
  827. static int pcie_init_slot(struct controller *ctrl)
  828. {
  829. struct slot *slot;
  830. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  831. if (!slot)
  832. return -ENOMEM;
  833. slot->hp_slot = 0;
  834. slot->ctrl = ctrl;
  835. slot->bus = ctrl->pci_dev->subordinate->number;
  836. slot->device = ctrl->slot_device_offset + slot->hp_slot;
  837. slot->hpc_ops = ctrl->hpc_ops;
  838. slot->number = ctrl->first_slot;
  839. mutex_init(&slot->lock);
  840. INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
  841. list_add(&slot->slot_list, &ctrl->slot_list);
  842. return 0;
  843. }
  844. static void pcie_cleanup_slot(struct controller *ctrl)
  845. {
  846. struct slot *slot;
  847. slot = list_first_entry(&ctrl->slot_list, struct slot, slot_list);
  848. list_del(&slot->slot_list);
  849. cancel_delayed_work(&slot->work);
  850. flush_scheduled_work();
  851. flush_workqueue(pciehp_wq);
  852. kfree(slot);
  853. }
  854. static inline void dbg_ctrl(struct controller *ctrl)
  855. {
  856. int i;
  857. u16 reg16;
  858. struct pci_dev *pdev = ctrl->pci_dev;
  859. if (!pciehp_debug)
  860. return;
  861. ctrl_info(ctrl, "Hotplug Controller:\n");
  862. ctrl_info(ctrl, " Seg/Bus/Dev/Func/IRQ : %s IRQ %d\n",
  863. pci_name(pdev), pdev->irq);
  864. ctrl_info(ctrl, " Vendor ID : 0x%04x\n", pdev->vendor);
  865. ctrl_info(ctrl, " Device ID : 0x%04x\n", pdev->device);
  866. ctrl_info(ctrl, " Subsystem ID : 0x%04x\n",
  867. pdev->subsystem_device);
  868. ctrl_info(ctrl, " Subsystem Vendor ID : 0x%04x\n",
  869. pdev->subsystem_vendor);
  870. ctrl_info(ctrl, " PCIe Cap offset : 0x%02x\n", ctrl->cap_base);
  871. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  872. if (!pci_resource_len(pdev, i))
  873. continue;
  874. ctrl_info(ctrl, " PCI resource [%d] : 0x%llx@0x%llx\n",
  875. i, (unsigned long long)pci_resource_len(pdev, i),
  876. (unsigned long long)pci_resource_start(pdev, i));
  877. }
  878. ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
  879. ctrl_info(ctrl, " Physical Slot Number : %d\n", ctrl->first_slot);
  880. ctrl_info(ctrl, " Attention Button : %3s\n",
  881. ATTN_BUTTN(ctrl) ? "yes" : "no");
  882. ctrl_info(ctrl, " Power Controller : %3s\n",
  883. POWER_CTRL(ctrl) ? "yes" : "no");
  884. ctrl_info(ctrl, " MRL Sensor : %3s\n",
  885. MRL_SENS(ctrl) ? "yes" : "no");
  886. ctrl_info(ctrl, " Attention Indicator : %3s\n",
  887. ATTN_LED(ctrl) ? "yes" : "no");
  888. ctrl_info(ctrl, " Power Indicator : %3s\n",
  889. PWR_LED(ctrl) ? "yes" : "no");
  890. ctrl_info(ctrl, " Hot-Plug Surprise : %3s\n",
  891. HP_SUPR_RM(ctrl) ? "yes" : "no");
  892. ctrl_info(ctrl, " EMI Present : %3s\n",
  893. EMI(ctrl) ? "yes" : "no");
  894. ctrl_info(ctrl, " Command Completed : %3s\n",
  895. NO_CMD_CMPL(ctrl) ? "no" : "yes");
  896. pciehp_readw(ctrl, PCI_EXP_SLTSTA, &reg16);
  897. ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
  898. pciehp_readw(ctrl, PCI_EXP_SLTCTL, &reg16);
  899. ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
  900. }
  901. struct controller *pcie_init(struct pcie_device *dev)
  902. {
  903. struct controller *ctrl;
  904. u32 slot_cap, link_cap;
  905. struct pci_dev *pdev = dev->port;
  906. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  907. if (!ctrl) {
  908. dev_err(&dev->device, "%s: Out of memory\n", __func__);
  909. goto abort;
  910. }
  911. INIT_LIST_HEAD(&ctrl->slot_list);
  912. ctrl->pcie = dev;
  913. ctrl->pci_dev = pdev;
  914. ctrl->cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  915. if (!ctrl->cap_base) {
  916. ctrl_err(ctrl, "Cannot find PCI Express capability\n");
  917. goto abort_ctrl;
  918. }
  919. if (pciehp_readl(ctrl, PCI_EXP_SLTCAP, &slot_cap)) {
  920. ctrl_err(ctrl, "Cannot read SLOTCAP register\n");
  921. goto abort_ctrl;
  922. }
  923. ctrl->slot_cap = slot_cap;
  924. ctrl->first_slot = slot_cap >> 19;
  925. ctrl->slot_device_offset = 0;
  926. ctrl->num_slots = 1;
  927. ctrl->hpc_ops = &pciehp_hpc_ops;
  928. mutex_init(&ctrl->crit_sect);
  929. mutex_init(&ctrl->ctrl_lock);
  930. init_waitqueue_head(&ctrl->queue);
  931. dbg_ctrl(ctrl);
  932. /*
  933. * Controller doesn't notify of command completion if the "No
  934. * Command Completed Support" bit is set in Slot Capability
  935. * register or the controller supports none of power
  936. * controller, attention led, power led and EMI.
  937. */
  938. if (NO_CMD_CMPL(ctrl) ||
  939. !(POWER_CTRL(ctrl) | ATTN_LED(ctrl) | PWR_LED(ctrl) | EMI(ctrl)))
  940. ctrl->no_cmd_complete = 1;
  941. /* Check if Data Link Layer Link Active Reporting is implemented */
  942. if (pciehp_readl(ctrl, PCI_EXP_LNKCAP, &link_cap)) {
  943. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  944. goto abort_ctrl;
  945. }
  946. if (link_cap & PCI_EXP_LNKCAP_DLLLARC) {
  947. ctrl_dbg(ctrl, "Link Active Reporting supported\n");
  948. ctrl->link_active_reporting = 1;
  949. }
  950. /* Clear all remaining event bits in Slot Status register */
  951. if (pciehp_writew(ctrl, PCI_EXP_SLTSTA, 0x1f))
  952. goto abort_ctrl;
  953. /* Disable sotfware notification */
  954. pcie_disable_notification(ctrl);
  955. /*
  956. * If this is the first controller to be initialized,
  957. * initialize the pciehp work queue
  958. */
  959. if (atomic_add_return(1, &pciehp_num_controllers) == 1) {
  960. pciehp_wq = create_singlethread_workqueue("pciehpd");
  961. if (!pciehp_wq)
  962. goto abort_ctrl;
  963. }
  964. ctrl_info(ctrl, "HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
  965. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  966. pdev->subsystem_device);
  967. if (pcie_init_slot(ctrl))
  968. goto abort_ctrl;
  969. return ctrl;
  970. abort_ctrl:
  971. kfree(ctrl);
  972. abort:
  973. return NULL;
  974. }
  975. void pcie_release_ctrl(struct controller *ctrl)
  976. {
  977. pcie_shutdown_notification(ctrl);
  978. pcie_cleanup_slot(ctrl);
  979. /*
  980. * If this is the last controller to be released, destroy the
  981. * pciehp work queue
  982. */
  983. if (atomic_dec_and_test(&pciehp_num_controllers))
  984. destroy_workqueue(pciehp_wq);
  985. kfree(ctrl);
  986. }