pciehp_hpc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. if (!pciehp_poll_mode) {
  478. /* Enable power fault detection turned off at power off time */
  479. slot_cmd |= PCI_EXP_SLTCTL_PFDE;
  480. cmd_mask |= PCI_EXP_SLTCTL_PFDE;
  481. }
  482. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  483. if (retval) {
  484. ctrl_err(ctrl, "Write %x command failed!\n", slot_cmd);
  485. return retval;
  486. }
  487. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  488. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_cmd);
  489. ctrl->power_fault_detected = 0;
  490. return retval;
  491. }
  492. static inline int pcie_mask_bad_dllp(struct controller *ctrl)
  493. {
  494. struct pci_dev *dev = ctrl->pci_dev;
  495. int pos;
  496. u32 reg;
  497. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  498. if (!pos)
  499. return 0;
  500. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  501. if (reg & PCI_ERR_COR_BAD_DLLP)
  502. return 0;
  503. reg |= PCI_ERR_COR_BAD_DLLP;
  504. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  505. return 1;
  506. }
  507. static inline void pcie_unmask_bad_dllp(struct controller *ctrl)
  508. {
  509. struct pci_dev *dev = ctrl->pci_dev;
  510. u32 reg;
  511. int pos;
  512. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  513. if (!pos)
  514. return;
  515. pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &reg);
  516. if (!(reg & PCI_ERR_COR_BAD_DLLP))
  517. return;
  518. reg &= ~PCI_ERR_COR_BAD_DLLP;
  519. pci_write_config_dword(dev, pos + PCI_ERR_COR_MASK, reg);
  520. }
  521. static int hpc_power_off_slot(struct slot * slot)
  522. {
  523. struct controller *ctrl = slot->ctrl;
  524. u16 slot_cmd;
  525. u16 cmd_mask;
  526. int retval = 0;
  527. int changed;
  528. ctrl_dbg(ctrl, "%s: slot->hp_slot %x\n", __func__, slot->hp_slot);
  529. /*
  530. * Set Bad DLLP Mask bit in Correctable Error Mask
  531. * Register. This is the workaround against Bad DLLP error
  532. * that sometimes happens during turning power off the slot
  533. * which conforms to PCI Express 1.0a spec.
  534. */
  535. changed = pcie_mask_bad_dllp(ctrl);
  536. slot_cmd = POWER_OFF;
  537. cmd_mask = PCI_EXP_SLTCTL_PCC;
  538. if (!pciehp_poll_mode) {
  539. /* Disable power fault detection */
  540. slot_cmd &= ~PCI_EXP_SLTCTL_PFDE;
  541. cmd_mask |= PCI_EXP_SLTCTL_PFDE;
  542. }
  543. retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
  544. if (retval) {
  545. ctrl_err(ctrl, "Write command failed!\n");
  546. retval = -1;
  547. goto out;
  548. }
  549. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n",
  550. __func__, ctrl->cap_base + PCI_EXP_SLTCTL, slot_cmd);
  551. out:
  552. if (changed)
  553. pcie_unmask_bad_dllp(ctrl);
  554. return retval;
  555. }
  556. static irqreturn_t pcie_isr(int irq, void *dev_id)
  557. {
  558. struct controller *ctrl = (struct controller *)dev_id;
  559. u16 detected, intr_loc;
  560. struct slot *p_slot;
  561. /*
  562. * In order to guarantee that all interrupt events are
  563. * serviced, we need to re-inspect Slot Status register after
  564. * clearing what is presumed to be the last pending interrupt.
  565. */
  566. intr_loc = 0;
  567. do {
  568. if (pciehp_readw(ctrl, PCI_EXP_SLTSTA, &detected)) {
  569. ctrl_err(ctrl, "%s: Cannot read SLOTSTATUS\n",
  570. __func__);
  571. return IRQ_NONE;
  572. }
  573. detected &= (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  574. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC |
  575. PCI_EXP_SLTSTA_CC);
  576. detected &= ~intr_loc;
  577. intr_loc |= detected;
  578. if (!intr_loc)
  579. return IRQ_NONE;
  580. if (detected && pciehp_writew(ctrl, PCI_EXP_SLTSTA, intr_loc)) {
  581. ctrl_err(ctrl, "%s: Cannot write to SLOTSTATUS\n",
  582. __func__);
  583. return IRQ_NONE;
  584. }
  585. } while (detected);
  586. ctrl_dbg(ctrl, "%s: intr_loc %x\n", __func__, intr_loc);
  587. /* Check Command Complete Interrupt Pending */
  588. if (intr_loc & PCI_EXP_SLTSTA_CC) {
  589. ctrl->cmd_busy = 0;
  590. smp_mb();
  591. wake_up(&ctrl->queue);
  592. }
  593. if (!(intr_loc & ~PCI_EXP_SLTSTA_CC))
  594. return IRQ_HANDLED;
  595. p_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  596. /* Check MRL Sensor Changed */
  597. if (intr_loc & PCI_EXP_SLTSTA_MRLSC)
  598. pciehp_handle_switch_change(p_slot);
  599. /* Check Attention Button Pressed */
  600. if (intr_loc & PCI_EXP_SLTSTA_ABP)
  601. pciehp_handle_attention_button(p_slot);
  602. /* Check Presence Detect Changed */
  603. if (intr_loc & PCI_EXP_SLTSTA_PDC)
  604. pciehp_handle_presence_change(p_slot);
  605. /* Check Power Fault Detected */
  606. if ((intr_loc & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
  607. ctrl->power_fault_detected = 1;
  608. pciehp_handle_power_fault(p_slot);
  609. }
  610. return IRQ_HANDLED;
  611. }
  612. static int hpc_get_max_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  613. {
  614. struct controller *ctrl = slot->ctrl;
  615. enum pcie_link_speed lnk_speed;
  616. u32 lnk_cap;
  617. int retval = 0;
  618. retval = pciehp_readl(ctrl, PCI_EXP_LNKCAP, &lnk_cap);
  619. if (retval) {
  620. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  621. return retval;
  622. }
  623. switch (lnk_cap & 0x000F) {
  624. case 1:
  625. lnk_speed = PCIE_2PT5GB;
  626. break;
  627. default:
  628. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  629. break;
  630. }
  631. *value = lnk_speed;
  632. ctrl_dbg(ctrl, "Max link speed = %d\n", lnk_speed);
  633. return retval;
  634. }
  635. static int hpc_get_max_lnk_width(struct slot *slot,
  636. enum pcie_link_width *value)
  637. {
  638. struct controller *ctrl = slot->ctrl;
  639. enum pcie_link_width lnk_wdth;
  640. u32 lnk_cap;
  641. int retval = 0;
  642. retval = pciehp_readl(ctrl, PCI_EXP_LNKCAP, &lnk_cap);
  643. if (retval) {
  644. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  645. return retval;
  646. }
  647. switch ((lnk_cap & PCI_EXP_LNKSTA_NLW) >> 4){
  648. case 0:
  649. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  650. break;
  651. case 1:
  652. lnk_wdth = PCIE_LNK_X1;
  653. break;
  654. case 2:
  655. lnk_wdth = PCIE_LNK_X2;
  656. break;
  657. case 4:
  658. lnk_wdth = PCIE_LNK_X4;
  659. break;
  660. case 8:
  661. lnk_wdth = PCIE_LNK_X8;
  662. break;
  663. case 12:
  664. lnk_wdth = PCIE_LNK_X12;
  665. break;
  666. case 16:
  667. lnk_wdth = PCIE_LNK_X16;
  668. break;
  669. case 32:
  670. lnk_wdth = PCIE_LNK_X32;
  671. break;
  672. default:
  673. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  674. break;
  675. }
  676. *value = lnk_wdth;
  677. ctrl_dbg(ctrl, "Max link width = %d\n", lnk_wdth);
  678. return retval;
  679. }
  680. static int hpc_get_cur_lnk_speed(struct slot *slot, enum pci_bus_speed *value)
  681. {
  682. struct controller *ctrl = slot->ctrl;
  683. enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
  684. int retval = 0;
  685. u16 lnk_status;
  686. retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status);
  687. if (retval) {
  688. ctrl_err(ctrl, "%s: Cannot read LNKSTATUS register\n",
  689. __func__);
  690. return retval;
  691. }
  692. switch (lnk_status & PCI_EXP_LNKSTA_CLS) {
  693. case 1:
  694. lnk_speed = PCIE_2PT5GB;
  695. break;
  696. default:
  697. lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
  698. break;
  699. }
  700. *value = lnk_speed;
  701. ctrl_dbg(ctrl, "Current link speed = %d\n", lnk_speed);
  702. return retval;
  703. }
  704. static int hpc_get_cur_lnk_width(struct slot *slot,
  705. enum pcie_link_width *value)
  706. {
  707. struct controller *ctrl = slot->ctrl;
  708. enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  709. int retval = 0;
  710. u16 lnk_status;
  711. retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status);
  712. if (retval) {
  713. ctrl_err(ctrl, "%s: Cannot read LNKSTATUS register\n",
  714. __func__);
  715. return retval;
  716. }
  717. switch ((lnk_status & PCI_EXP_LNKSTA_NLW) >> 4){
  718. case 0:
  719. lnk_wdth = PCIE_LNK_WIDTH_RESRV;
  720. break;
  721. case 1:
  722. lnk_wdth = PCIE_LNK_X1;
  723. break;
  724. case 2:
  725. lnk_wdth = PCIE_LNK_X2;
  726. break;
  727. case 4:
  728. lnk_wdth = PCIE_LNK_X4;
  729. break;
  730. case 8:
  731. lnk_wdth = PCIE_LNK_X8;
  732. break;
  733. case 12:
  734. lnk_wdth = PCIE_LNK_X12;
  735. break;
  736. case 16:
  737. lnk_wdth = PCIE_LNK_X16;
  738. break;
  739. case 32:
  740. lnk_wdth = PCIE_LNK_X32;
  741. break;
  742. default:
  743. lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
  744. break;
  745. }
  746. *value = lnk_wdth;
  747. ctrl_dbg(ctrl, "Current link width = %d\n", lnk_wdth);
  748. return retval;
  749. }
  750. static void pcie_release_ctrl(struct controller *ctrl);
  751. static struct hpc_ops pciehp_hpc_ops = {
  752. .power_on_slot = hpc_power_on_slot,
  753. .power_off_slot = hpc_power_off_slot,
  754. .set_attention_status = hpc_set_attention_status,
  755. .get_power_status = hpc_get_power_status,
  756. .get_attention_status = hpc_get_attention_status,
  757. .get_latch_status = hpc_get_latch_status,
  758. .get_adapter_status = hpc_get_adapter_status,
  759. .get_emi_status = hpc_get_emi_status,
  760. .toggle_emi = hpc_toggle_emi,
  761. .get_max_bus_speed = hpc_get_max_lnk_speed,
  762. .get_cur_bus_speed = hpc_get_cur_lnk_speed,
  763. .get_max_lnk_width = hpc_get_max_lnk_width,
  764. .get_cur_lnk_width = hpc_get_cur_lnk_width,
  765. .query_power_fault = hpc_query_power_fault,
  766. .green_led_on = hpc_set_green_led_on,
  767. .green_led_off = hpc_set_green_led_off,
  768. .green_led_blink = hpc_set_green_led_blink,
  769. .release_ctlr = pcie_release_ctrl,
  770. .check_lnk_status = hpc_check_lnk_status,
  771. };
  772. int pcie_enable_notification(struct controller *ctrl)
  773. {
  774. u16 cmd, mask;
  775. cmd = PCI_EXP_SLTCTL_PDCE;
  776. if (ATTN_BUTTN(ctrl))
  777. cmd |= PCI_EXP_SLTCTL_ABPE;
  778. if (POWER_CTRL(ctrl))
  779. cmd |= PCI_EXP_SLTCTL_PFDE;
  780. if (MRL_SENS(ctrl))
  781. cmd |= PCI_EXP_SLTCTL_MRLSCE;
  782. if (!pciehp_poll_mode)
  783. cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
  784. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  785. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  786. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE);
  787. if (pcie_write_cmd(ctrl, cmd, mask)) {
  788. ctrl_err(ctrl, "Cannot enable software notification\n");
  789. return -1;
  790. }
  791. return 0;
  792. }
  793. static void pcie_disable_notification(struct controller *ctrl)
  794. {
  795. u16 mask;
  796. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  797. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  798. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE);
  799. if (pcie_write_cmd(ctrl, 0, mask))
  800. ctrl_warn(ctrl, "Cannot disable software notification\n");
  801. }
  802. int pcie_init_notification(struct controller *ctrl)
  803. {
  804. if (pciehp_request_irq(ctrl))
  805. return -1;
  806. if (pcie_enable_notification(ctrl)) {
  807. pciehp_free_irq(ctrl);
  808. return -1;
  809. }
  810. ctrl->notification_enabled = 1;
  811. return 0;
  812. }
  813. static void pcie_shutdown_notification(struct controller *ctrl)
  814. {
  815. if (ctrl->notification_enabled) {
  816. pcie_disable_notification(ctrl);
  817. pciehp_free_irq(ctrl);
  818. ctrl->notification_enabled = 0;
  819. }
  820. }
  821. static int pcie_init_slot(struct controller *ctrl)
  822. {
  823. struct slot *slot;
  824. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  825. if (!slot)
  826. return -ENOMEM;
  827. slot->hp_slot = 0;
  828. slot->ctrl = ctrl;
  829. slot->bus = ctrl->pci_dev->subordinate->number;
  830. slot->device = ctrl->slot_device_offset + slot->hp_slot;
  831. slot->hpc_ops = ctrl->hpc_ops;
  832. slot->number = ctrl->first_slot;
  833. mutex_init(&slot->lock);
  834. INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
  835. list_add(&slot->slot_list, &ctrl->slot_list);
  836. return 0;
  837. }
  838. static void pcie_cleanup_slot(struct controller *ctrl)
  839. {
  840. struct slot *slot;
  841. slot = list_first_entry(&ctrl->slot_list, struct slot, slot_list);
  842. list_del(&slot->slot_list);
  843. cancel_delayed_work(&slot->work);
  844. flush_scheduled_work();
  845. flush_workqueue(pciehp_wq);
  846. kfree(slot);
  847. }
  848. static inline void dbg_ctrl(struct controller *ctrl)
  849. {
  850. int i;
  851. u16 reg16;
  852. struct pci_dev *pdev = ctrl->pci_dev;
  853. if (!pciehp_debug)
  854. return;
  855. ctrl_info(ctrl, "Hotplug Controller:\n");
  856. ctrl_info(ctrl, " Seg/Bus/Dev/Func/IRQ : %s IRQ %d\n",
  857. pci_name(pdev), pdev->irq);
  858. ctrl_info(ctrl, " Vendor ID : 0x%04x\n", pdev->vendor);
  859. ctrl_info(ctrl, " Device ID : 0x%04x\n", pdev->device);
  860. ctrl_info(ctrl, " Subsystem ID : 0x%04x\n",
  861. pdev->subsystem_device);
  862. ctrl_info(ctrl, " Subsystem Vendor ID : 0x%04x\n",
  863. pdev->subsystem_vendor);
  864. ctrl_info(ctrl, " PCIe Cap offset : 0x%02x\n", ctrl->cap_base);
  865. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  866. if (!pci_resource_len(pdev, i))
  867. continue;
  868. ctrl_info(ctrl, " PCI resource [%d] : 0x%llx@0x%llx\n",
  869. i, (unsigned long long)pci_resource_len(pdev, i),
  870. (unsigned long long)pci_resource_start(pdev, i));
  871. }
  872. ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
  873. ctrl_info(ctrl, " Physical Slot Number : %d\n", ctrl->first_slot);
  874. ctrl_info(ctrl, " Attention Button : %3s\n",
  875. ATTN_BUTTN(ctrl) ? "yes" : "no");
  876. ctrl_info(ctrl, " Power Controller : %3s\n",
  877. POWER_CTRL(ctrl) ? "yes" : "no");
  878. ctrl_info(ctrl, " MRL Sensor : %3s\n",
  879. MRL_SENS(ctrl) ? "yes" : "no");
  880. ctrl_info(ctrl, " Attention Indicator : %3s\n",
  881. ATTN_LED(ctrl) ? "yes" : "no");
  882. ctrl_info(ctrl, " Power Indicator : %3s\n",
  883. PWR_LED(ctrl) ? "yes" : "no");
  884. ctrl_info(ctrl, " Hot-Plug Surprise : %3s\n",
  885. HP_SUPR_RM(ctrl) ? "yes" : "no");
  886. ctrl_info(ctrl, " EMI Present : %3s\n",
  887. EMI(ctrl) ? "yes" : "no");
  888. ctrl_info(ctrl, " Command Completed : %3s\n",
  889. NO_CMD_CMPL(ctrl) ? "no" : "yes");
  890. pciehp_readw(ctrl, PCI_EXP_SLTSTA, &reg16);
  891. ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
  892. pciehp_readw(ctrl, PCI_EXP_SLTCTL, &reg16);
  893. ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
  894. }
  895. struct controller *pcie_init(struct pcie_device *dev)
  896. {
  897. struct controller *ctrl;
  898. u32 slot_cap, link_cap;
  899. struct pci_dev *pdev = dev->port;
  900. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  901. if (!ctrl) {
  902. dev_err(&dev->device, "%s: Out of memory\n", __func__);
  903. goto abort;
  904. }
  905. INIT_LIST_HEAD(&ctrl->slot_list);
  906. ctrl->pcie = dev;
  907. ctrl->pci_dev = pdev;
  908. ctrl->cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  909. if (!ctrl->cap_base) {
  910. ctrl_err(ctrl, "Cannot find PCI Express capability\n");
  911. goto abort_ctrl;
  912. }
  913. if (pciehp_readl(ctrl, PCI_EXP_SLTCAP, &slot_cap)) {
  914. ctrl_err(ctrl, "Cannot read SLOTCAP register\n");
  915. goto abort_ctrl;
  916. }
  917. ctrl->slot_cap = slot_cap;
  918. ctrl->first_slot = slot_cap >> 19;
  919. ctrl->slot_device_offset = 0;
  920. ctrl->num_slots = 1;
  921. ctrl->hpc_ops = &pciehp_hpc_ops;
  922. mutex_init(&ctrl->crit_sect);
  923. mutex_init(&ctrl->ctrl_lock);
  924. init_waitqueue_head(&ctrl->queue);
  925. dbg_ctrl(ctrl);
  926. /*
  927. * Controller doesn't notify of command completion if the "No
  928. * Command Completed Support" bit is set in Slot Capability
  929. * register or the controller supports none of power
  930. * controller, attention led, power led and EMI.
  931. */
  932. if (NO_CMD_CMPL(ctrl) ||
  933. !(POWER_CTRL(ctrl) | ATTN_LED(ctrl) | PWR_LED(ctrl) | EMI(ctrl)))
  934. ctrl->no_cmd_complete = 1;
  935. /* Check if Data Link Layer Link Active Reporting is implemented */
  936. if (pciehp_readl(ctrl, PCI_EXP_LNKCAP, &link_cap)) {
  937. ctrl_err(ctrl, "%s: Cannot read LNKCAP register\n", __func__);
  938. goto abort_ctrl;
  939. }
  940. if (link_cap & PCI_EXP_LNKCAP_DLLLARC) {
  941. ctrl_dbg(ctrl, "Link Active Reporting supported\n");
  942. ctrl->link_active_reporting = 1;
  943. }
  944. /* Clear all remaining event bits in Slot Status register */
  945. if (pciehp_writew(ctrl, PCI_EXP_SLTSTA, 0x1f))
  946. goto abort_ctrl;
  947. /* Disable sotfware notification */
  948. pcie_disable_notification(ctrl);
  949. /*
  950. * If this is the first controller to be initialized,
  951. * initialize the pciehp work queue
  952. */
  953. if (atomic_add_return(1, &pciehp_num_controllers) == 1) {
  954. pciehp_wq = create_singlethread_workqueue("pciehpd");
  955. if (!pciehp_wq)
  956. goto abort_ctrl;
  957. }
  958. ctrl_info(ctrl, "HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
  959. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  960. pdev->subsystem_device);
  961. if (pcie_init_slot(ctrl))
  962. goto abort_ctrl;
  963. return ctrl;
  964. abort_ctrl:
  965. kfree(ctrl);
  966. abort:
  967. return NULL;
  968. }
  969. void pcie_release_ctrl(struct controller *ctrl)
  970. {
  971. pcie_shutdown_notification(ctrl);
  972. pcie_cleanup_slot(ctrl);
  973. /*
  974. * If this is the last controller to be released, destroy the
  975. * pciehp work queue
  976. */
  977. if (atomic_dec_and_test(&pciehp_num_controllers))
  978. destroy_workqueue(pciehp_wq);
  979. kfree(ctrl);
  980. }