pciehp_hpc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI Express PCI Hot Plug Driver
  4. *
  5. * Copyright (C) 1995,2001 Compaq Computer Corporation
  6. * Copyright (C) 2001 Greg Kroah-Hartman ([email protected])
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2003-2004 Intel Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <[email protected]>,<[email protected]>
  13. */
  14. #define dev_fmt(fmt) "pciehp: " fmt
  15. #include <linux/dmi.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/kthread.h>
  20. #include <linux/pci.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/slab.h>
  24. #include "../pci.h"
  25. #include "pciehp.h"
  26. static const struct dmi_system_id inband_presence_disabled_dmi_table[] = {
  27. /*
  28. * Match all Dell systems, as some Dell systems have inband
  29. * presence disabled on NVMe slots (but don't support the bit to
  30. * report it). Setting inband presence disabled should have no
  31. * negative effect, except on broken hotplug slots that never
  32. * assert presence detect--and those will still work, they will
  33. * just have a bit of extra delay before being probed.
  34. */
  35. {
  36. .ident = "Dell System",
  37. .matches = {
  38. DMI_MATCH(DMI_OEM_STRING, "Dell System"),
  39. },
  40. },
  41. {}
  42. };
  43. static inline struct pci_dev *ctrl_dev(struct controller *ctrl)
  44. {
  45. return ctrl->pcie->port;
  46. }
  47. static irqreturn_t pciehp_isr(int irq, void *dev_id);
  48. static irqreturn_t pciehp_ist(int irq, void *dev_id);
  49. static int pciehp_poll(void *data);
  50. static inline int pciehp_request_irq(struct controller *ctrl)
  51. {
  52. int retval, irq = ctrl->pcie->irq;
  53. if (pciehp_poll_mode) {
  54. ctrl->poll_thread = kthread_run(&pciehp_poll, ctrl,
  55. "pciehp_poll-%s",
  56. slot_name(ctrl));
  57. return PTR_ERR_OR_ZERO(ctrl->poll_thread);
  58. }
  59. /* Installs the interrupt handler */
  60. retval = request_threaded_irq(irq, pciehp_isr, pciehp_ist,
  61. IRQF_SHARED, "pciehp", ctrl);
  62. if (retval)
  63. ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
  64. irq);
  65. return retval;
  66. }
  67. static inline void pciehp_free_irq(struct controller *ctrl)
  68. {
  69. if (pciehp_poll_mode)
  70. kthread_stop(ctrl->poll_thread);
  71. else
  72. free_irq(ctrl->pcie->irq, ctrl);
  73. }
  74. static int pcie_poll_cmd(struct controller *ctrl, int timeout)
  75. {
  76. struct pci_dev *pdev = ctrl_dev(ctrl);
  77. u16 slot_status;
  78. do {
  79. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  80. if (PCI_POSSIBLE_ERROR(slot_status)) {
  81. ctrl_info(ctrl, "%s: no response from device\n",
  82. __func__);
  83. return 0;
  84. }
  85. if (slot_status & PCI_EXP_SLTSTA_CC) {
  86. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  87. PCI_EXP_SLTSTA_CC);
  88. ctrl->cmd_busy = 0;
  89. smp_mb();
  90. return 1;
  91. }
  92. msleep(10);
  93. timeout -= 10;
  94. } while (timeout >= 0);
  95. return 0; /* timeout */
  96. }
  97. static void pcie_wait_cmd(struct controller *ctrl)
  98. {
  99. unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
  100. unsigned long duration = msecs_to_jiffies(msecs);
  101. unsigned long cmd_timeout = ctrl->cmd_started + duration;
  102. unsigned long now, timeout;
  103. int rc;
  104. /*
  105. * If the controller does not generate notifications for command
  106. * completions, we never need to wait between writes.
  107. */
  108. if (NO_CMD_CMPL(ctrl))
  109. return;
  110. if (!ctrl->cmd_busy)
  111. return;
  112. /*
  113. * Even if the command has already timed out, we want to call
  114. * pcie_poll_cmd() so it can clear PCI_EXP_SLTSTA_CC.
  115. */
  116. now = jiffies;
  117. if (time_before_eq(cmd_timeout, now))
  118. timeout = 1;
  119. else
  120. timeout = cmd_timeout - now;
  121. if (ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE &&
  122. ctrl->slot_ctrl & PCI_EXP_SLTCTL_CCIE)
  123. rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
  124. else
  125. rc = pcie_poll_cmd(ctrl, jiffies_to_msecs(timeout));
  126. if (!rc)
  127. ctrl_info(ctrl, "Timeout on hotplug command %#06x (issued %u msec ago)\n",
  128. ctrl->slot_ctrl,
  129. jiffies_to_msecs(jiffies - ctrl->cmd_started));
  130. }
  131. #define CC_ERRATUM_MASK (PCI_EXP_SLTCTL_PCC | \
  132. PCI_EXP_SLTCTL_PIC | \
  133. PCI_EXP_SLTCTL_AIC | \
  134. PCI_EXP_SLTCTL_EIC)
  135. static void pcie_do_write_cmd(struct controller *ctrl, u16 cmd,
  136. u16 mask, bool wait)
  137. {
  138. struct pci_dev *pdev = ctrl_dev(ctrl);
  139. u16 slot_ctrl_orig, slot_ctrl;
  140. mutex_lock(&ctrl->ctrl_lock);
  141. /*
  142. * Always wait for any previous command that might still be in progress
  143. */
  144. pcie_wait_cmd(ctrl);
  145. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  146. if (PCI_POSSIBLE_ERROR(slot_ctrl)) {
  147. ctrl_info(ctrl, "%s: no response from device\n", __func__);
  148. goto out;
  149. }
  150. slot_ctrl_orig = slot_ctrl;
  151. slot_ctrl &= ~mask;
  152. slot_ctrl |= (cmd & mask);
  153. ctrl->cmd_busy = 1;
  154. smp_mb();
  155. ctrl->slot_ctrl = slot_ctrl;
  156. pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, slot_ctrl);
  157. ctrl->cmd_started = jiffies;
  158. /*
  159. * Controllers with the Intel CF118 and similar errata advertise
  160. * Command Completed support, but they only set Command Completed
  161. * if we change the "Control" bits for power, power indicator,
  162. * attention indicator, or interlock. If we only change the
  163. * "Enable" bits, they never set the Command Completed bit.
  164. */
  165. if (pdev->broken_cmd_compl &&
  166. (slot_ctrl_orig & CC_ERRATUM_MASK) == (slot_ctrl & CC_ERRATUM_MASK))
  167. ctrl->cmd_busy = 0;
  168. /*
  169. * Optionally wait for the hardware to be ready for a new command,
  170. * indicating completion of the above issued command.
  171. */
  172. if (wait)
  173. pcie_wait_cmd(ctrl);
  174. out:
  175. mutex_unlock(&ctrl->ctrl_lock);
  176. }
  177. /**
  178. * pcie_write_cmd - Issue controller command
  179. * @ctrl: controller to which the command is issued
  180. * @cmd: command value written to slot control register
  181. * @mask: bitmask of slot control register to be modified
  182. */
  183. static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
  184. {
  185. pcie_do_write_cmd(ctrl, cmd, mask, true);
  186. }
  187. /* Same as above without waiting for the hardware to latch */
  188. static void pcie_write_cmd_nowait(struct controller *ctrl, u16 cmd, u16 mask)
  189. {
  190. pcie_do_write_cmd(ctrl, cmd, mask, false);
  191. }
  192. /**
  193. * pciehp_check_link_active() - Is the link active
  194. * @ctrl: PCIe hotplug controller
  195. *
  196. * Check whether the downstream link is currently active. Note it is
  197. * possible that the card is removed immediately after this so the
  198. * caller may need to take it into account.
  199. *
  200. * If the hotplug controller itself is not available anymore returns
  201. * %-ENODEV.
  202. */
  203. int pciehp_check_link_active(struct controller *ctrl)
  204. {
  205. struct pci_dev *pdev = ctrl_dev(ctrl);
  206. u16 lnk_status;
  207. int ret;
  208. ret = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  209. if (ret == PCIBIOS_DEVICE_NOT_FOUND || PCI_POSSIBLE_ERROR(lnk_status))
  210. return -ENODEV;
  211. ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
  212. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  213. return ret;
  214. }
  215. static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
  216. {
  217. u32 l;
  218. int count = 0;
  219. int delay = 1000, step = 20;
  220. bool found = false;
  221. do {
  222. found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
  223. count++;
  224. if (found)
  225. break;
  226. msleep(step);
  227. delay -= step;
  228. } while (delay > 0);
  229. if (count > 1)
  230. pr_debug("pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
  231. pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
  232. PCI_FUNC(devfn), count, step, l);
  233. return found;
  234. }
  235. static void pcie_wait_for_presence(struct pci_dev *pdev)
  236. {
  237. int timeout = 1250;
  238. u16 slot_status;
  239. do {
  240. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  241. if (slot_status & PCI_EXP_SLTSTA_PDS)
  242. return;
  243. msleep(10);
  244. timeout -= 10;
  245. } while (timeout > 0);
  246. }
  247. int pciehp_check_link_status(struct controller *ctrl)
  248. {
  249. struct pci_dev *pdev = ctrl_dev(ctrl);
  250. bool found;
  251. u16 lnk_status;
  252. if (!pcie_wait_for_link(pdev, true)) {
  253. ctrl_info(ctrl, "Slot(%s): No link\n", slot_name(ctrl));
  254. return -1;
  255. }
  256. if (ctrl->inband_presence_disabled)
  257. pcie_wait_for_presence(pdev);
  258. found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
  259. PCI_DEVFN(0, 0));
  260. /* ignore link or presence changes up to this point */
  261. if (found)
  262. atomic_and(~(PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC),
  263. &ctrl->pending_events);
  264. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
  265. ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
  266. if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
  267. !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
  268. ctrl_info(ctrl, "Slot(%s): Cannot train link: status %#06x\n",
  269. slot_name(ctrl), lnk_status);
  270. return -1;
  271. }
  272. pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);
  273. if (!found) {
  274. ctrl_info(ctrl, "Slot(%s): No device found\n",
  275. slot_name(ctrl));
  276. return -1;
  277. }
  278. return 0;
  279. }
  280. static int __pciehp_link_set(struct controller *ctrl, bool enable)
  281. {
  282. struct pci_dev *pdev = ctrl_dev(ctrl);
  283. pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
  284. PCI_EXP_LNKCTL_LD,
  285. enable ? 0 : PCI_EXP_LNKCTL_LD);
  286. return 0;
  287. }
  288. static int pciehp_link_enable(struct controller *ctrl)
  289. {
  290. return __pciehp_link_set(ctrl, true);
  291. }
  292. int pciehp_get_raw_indicator_status(struct hotplug_slot *hotplug_slot,
  293. u8 *status)
  294. {
  295. struct controller *ctrl = to_ctrl(hotplug_slot);
  296. struct pci_dev *pdev = ctrl_dev(ctrl);
  297. u16 slot_ctrl;
  298. pci_config_pm_runtime_get(pdev);
  299. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  300. pci_config_pm_runtime_put(pdev);
  301. *status = (slot_ctrl & (PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC)) >> 6;
  302. return 0;
  303. }
  304. int pciehp_get_attention_status(struct hotplug_slot *hotplug_slot, u8 *status)
  305. {
  306. struct controller *ctrl = to_ctrl(hotplug_slot);
  307. struct pci_dev *pdev = ctrl_dev(ctrl);
  308. u16 slot_ctrl;
  309. pci_config_pm_runtime_get(pdev);
  310. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  311. pci_config_pm_runtime_put(pdev);
  312. ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
  313. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  314. switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
  315. case PCI_EXP_SLTCTL_ATTN_IND_ON:
  316. *status = 1; /* On */
  317. break;
  318. case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
  319. *status = 2; /* Blink */
  320. break;
  321. case PCI_EXP_SLTCTL_ATTN_IND_OFF:
  322. *status = 0; /* Off */
  323. break;
  324. default:
  325. *status = 0xFF;
  326. break;
  327. }
  328. return 0;
  329. }
  330. void pciehp_get_power_status(struct controller *ctrl, u8 *status)
  331. {
  332. struct pci_dev *pdev = ctrl_dev(ctrl);
  333. u16 slot_ctrl;
  334. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
  335. ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
  336. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
  337. switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
  338. case PCI_EXP_SLTCTL_PWR_ON:
  339. *status = 1; /* On */
  340. break;
  341. case PCI_EXP_SLTCTL_PWR_OFF:
  342. *status = 0; /* Off */
  343. break;
  344. default:
  345. *status = 0xFF;
  346. break;
  347. }
  348. }
  349. void pciehp_get_latch_status(struct controller *ctrl, u8 *status)
  350. {
  351. struct pci_dev *pdev = ctrl_dev(ctrl);
  352. u16 slot_status;
  353. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  354. *status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
  355. }
  356. /**
  357. * pciehp_card_present() - Is the card present
  358. * @ctrl: PCIe hotplug controller
  359. *
  360. * Function checks whether the card is currently present in the slot and
  361. * in that case returns true. Note it is possible that the card is
  362. * removed immediately after the check so the caller may need to take
  363. * this into account.
  364. *
  365. * It the hotplug controller itself is not available anymore returns
  366. * %-ENODEV.
  367. */
  368. int pciehp_card_present(struct controller *ctrl)
  369. {
  370. struct pci_dev *pdev = ctrl_dev(ctrl);
  371. u16 slot_status;
  372. int ret;
  373. ret = pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  374. if (ret == PCIBIOS_DEVICE_NOT_FOUND || PCI_POSSIBLE_ERROR(slot_status))
  375. return -ENODEV;
  376. return !!(slot_status & PCI_EXP_SLTSTA_PDS);
  377. }
  378. /**
  379. * pciehp_card_present_or_link_active() - whether given slot is occupied
  380. * @ctrl: PCIe hotplug controller
  381. *
  382. * Unlike pciehp_card_present(), which determines presence solely from the
  383. * Presence Detect State bit, this helper also returns true if the Link Active
  384. * bit is set. This is a concession to broken hotplug ports which hardwire
  385. * Presence Detect State to zero, such as Wilocity's [1ae9:0200].
  386. *
  387. * Returns: %1 if the slot is occupied and %0 if it is not. If the hotplug
  388. * port is not present anymore returns %-ENODEV.
  389. */
  390. int pciehp_card_present_or_link_active(struct controller *ctrl)
  391. {
  392. int ret;
  393. ret = pciehp_card_present(ctrl);
  394. if (ret)
  395. return ret;
  396. return pciehp_check_link_active(ctrl);
  397. }
  398. int pciehp_query_power_fault(struct controller *ctrl)
  399. {
  400. struct pci_dev *pdev = ctrl_dev(ctrl);
  401. u16 slot_status;
  402. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  403. return !!(slot_status & PCI_EXP_SLTSTA_PFD);
  404. }
  405. int pciehp_set_raw_indicator_status(struct hotplug_slot *hotplug_slot,
  406. u8 status)
  407. {
  408. struct controller *ctrl = to_ctrl(hotplug_slot);
  409. struct pci_dev *pdev = ctrl_dev(ctrl);
  410. pci_config_pm_runtime_get(pdev);
  411. pcie_write_cmd_nowait(ctrl, status << 6,
  412. PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC);
  413. pci_config_pm_runtime_put(pdev);
  414. return 0;
  415. }
  416. /**
  417. * pciehp_set_indicators() - set attention indicator, power indicator, or both
  418. * @ctrl: PCIe hotplug controller
  419. * @pwr: one of:
  420. * PCI_EXP_SLTCTL_PWR_IND_ON
  421. * PCI_EXP_SLTCTL_PWR_IND_BLINK
  422. * PCI_EXP_SLTCTL_PWR_IND_OFF
  423. * @attn: one of:
  424. * PCI_EXP_SLTCTL_ATTN_IND_ON
  425. * PCI_EXP_SLTCTL_ATTN_IND_BLINK
  426. * PCI_EXP_SLTCTL_ATTN_IND_OFF
  427. *
  428. * Either @pwr or @attn can also be INDICATOR_NOOP to leave that indicator
  429. * unchanged.
  430. */
  431. void pciehp_set_indicators(struct controller *ctrl, int pwr, int attn)
  432. {
  433. u16 cmd = 0, mask = 0;
  434. if (PWR_LED(ctrl) && pwr != INDICATOR_NOOP) {
  435. cmd |= (pwr & PCI_EXP_SLTCTL_PIC);
  436. mask |= PCI_EXP_SLTCTL_PIC;
  437. }
  438. if (ATTN_LED(ctrl) && attn != INDICATOR_NOOP) {
  439. cmd |= (attn & PCI_EXP_SLTCTL_AIC);
  440. mask |= PCI_EXP_SLTCTL_AIC;
  441. }
  442. if (cmd) {
  443. pcie_write_cmd_nowait(ctrl, cmd, mask);
  444. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  445. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
  446. }
  447. }
  448. int pciehp_power_on_slot(struct controller *ctrl)
  449. {
  450. struct pci_dev *pdev = ctrl_dev(ctrl);
  451. u16 slot_status;
  452. int retval;
  453. /* Clear power-fault bit from previous power failures */
  454. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
  455. if (slot_status & PCI_EXP_SLTSTA_PFD)
  456. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  457. PCI_EXP_SLTSTA_PFD);
  458. ctrl->power_fault_detected = 0;
  459. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
  460. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  461. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  462. PCI_EXP_SLTCTL_PWR_ON);
  463. retval = pciehp_link_enable(ctrl);
  464. if (retval)
  465. ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
  466. return retval;
  467. }
  468. void pciehp_power_off_slot(struct controller *ctrl)
  469. {
  470. pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
  471. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  472. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
  473. PCI_EXP_SLTCTL_PWR_OFF);
  474. }
  475. static void pciehp_ignore_dpc_link_change(struct controller *ctrl,
  476. struct pci_dev *pdev, int irq)
  477. {
  478. /*
  479. * Ignore link changes which occurred while waiting for DPC recovery.
  480. * Could be several if DPC triggered multiple times consecutively.
  481. */
  482. synchronize_hardirq(irq);
  483. atomic_and(~PCI_EXP_SLTSTA_DLLSC, &ctrl->pending_events);
  484. if (pciehp_poll_mode)
  485. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  486. PCI_EXP_SLTSTA_DLLSC);
  487. ctrl_info(ctrl, "Slot(%s): Link Down/Up ignored (recovered by DPC)\n",
  488. slot_name(ctrl));
  489. /*
  490. * If the link is unexpectedly down after successful recovery,
  491. * the corresponding link change may have been ignored above.
  492. * Synthesize it to ensure that it is acted on.
  493. */
  494. down_read_nested(&ctrl->reset_lock, ctrl->depth);
  495. if (!pciehp_check_link_active(ctrl))
  496. pciehp_request(ctrl, PCI_EXP_SLTSTA_DLLSC);
  497. up_read(&ctrl->reset_lock);
  498. }
  499. static irqreturn_t pciehp_isr(int irq, void *dev_id)
  500. {
  501. struct controller *ctrl = (struct controller *)dev_id;
  502. struct pci_dev *pdev = ctrl_dev(ctrl);
  503. struct device *parent = pdev->dev.parent;
  504. u16 status, events = 0;
  505. /*
  506. * Interrupts only occur in D3hot or shallower and only if enabled
  507. * in the Slot Control register (PCIe r4.0, sec 6.7.3.4).
  508. */
  509. if (pdev->current_state == PCI_D3cold ||
  510. (!(ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE) && !pciehp_poll_mode))
  511. return IRQ_NONE;
  512. /*
  513. * Keep the port accessible by holding a runtime PM ref on its parent.
  514. * Defer resume of the parent to the IRQ thread if it's suspended.
  515. * Mask the interrupt until then.
  516. */
  517. if (parent) {
  518. pm_runtime_get_noresume(parent);
  519. if (!pm_runtime_active(parent)) {
  520. pm_runtime_put(parent);
  521. disable_irq_nosync(irq);
  522. atomic_or(RERUN_ISR, &ctrl->pending_events);
  523. return IRQ_WAKE_THREAD;
  524. }
  525. }
  526. read_status:
  527. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &status);
  528. if (PCI_POSSIBLE_ERROR(status)) {
  529. ctrl_info(ctrl, "%s: no response from device\n", __func__);
  530. if (parent)
  531. pm_runtime_put(parent);
  532. return IRQ_NONE;
  533. }
  534. /*
  535. * Slot Status contains plain status bits as well as event
  536. * notification bits; right now we only want the event bits.
  537. */
  538. status &= PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  539. PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_CC |
  540. PCI_EXP_SLTSTA_DLLSC;
  541. /*
  542. * If we've already reported a power fault, don't report it again
  543. * until we've done something to handle it.
  544. */
  545. if (ctrl->power_fault_detected)
  546. status &= ~PCI_EXP_SLTSTA_PFD;
  547. else if (status & PCI_EXP_SLTSTA_PFD)
  548. ctrl->power_fault_detected = true;
  549. events |= status;
  550. if (!events) {
  551. if (parent)
  552. pm_runtime_put(parent);
  553. return IRQ_NONE;
  554. }
  555. if (status) {
  556. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, status);
  557. /*
  558. * In MSI mode, all event bits must be zero before the port
  559. * will send a new interrupt (PCIe Base Spec r5.0 sec 6.7.3.4).
  560. * So re-read the Slot Status register in case a bit was set
  561. * between read and write.
  562. */
  563. if (pci_dev_msi_enabled(pdev) && !pciehp_poll_mode)
  564. goto read_status;
  565. }
  566. ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", events);
  567. if (parent)
  568. pm_runtime_put(parent);
  569. /*
  570. * Command Completed notifications are not deferred to the
  571. * IRQ thread because it may be waiting for their arrival.
  572. */
  573. if (events & PCI_EXP_SLTSTA_CC) {
  574. ctrl->cmd_busy = 0;
  575. smp_mb();
  576. wake_up(&ctrl->queue);
  577. if (events == PCI_EXP_SLTSTA_CC)
  578. return IRQ_HANDLED;
  579. events &= ~PCI_EXP_SLTSTA_CC;
  580. }
  581. if (pdev->ignore_hotplug) {
  582. ctrl_dbg(ctrl, "ignoring hotplug event %#06x\n", events);
  583. return IRQ_HANDLED;
  584. }
  585. /* Save pending events for consumption by IRQ thread. */
  586. atomic_or(events, &ctrl->pending_events);
  587. return IRQ_WAKE_THREAD;
  588. }
  589. static irqreturn_t pciehp_ist(int irq, void *dev_id)
  590. {
  591. struct controller *ctrl = (struct controller *)dev_id;
  592. struct pci_dev *pdev = ctrl_dev(ctrl);
  593. irqreturn_t ret;
  594. u32 events;
  595. ctrl->ist_running = true;
  596. pci_config_pm_runtime_get(pdev);
  597. /* rerun pciehp_isr() if the port was inaccessible on interrupt */
  598. if (atomic_fetch_and(~RERUN_ISR, &ctrl->pending_events) & RERUN_ISR) {
  599. ret = pciehp_isr(irq, dev_id);
  600. enable_irq(irq);
  601. if (ret != IRQ_WAKE_THREAD)
  602. goto out;
  603. }
  604. synchronize_hardirq(irq);
  605. events = atomic_xchg(&ctrl->pending_events, 0);
  606. if (!events) {
  607. ret = IRQ_NONE;
  608. goto out;
  609. }
  610. /* Check Attention Button Pressed */
  611. if (events & PCI_EXP_SLTSTA_ABP) {
  612. ctrl_info(ctrl, "Slot(%s): Attention button pressed\n",
  613. slot_name(ctrl));
  614. pciehp_handle_button_press(ctrl);
  615. }
  616. /* Check Power Fault Detected */
  617. if (events & PCI_EXP_SLTSTA_PFD) {
  618. ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(ctrl));
  619. pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
  620. PCI_EXP_SLTCTL_ATTN_IND_ON);
  621. }
  622. /*
  623. * Ignore Link Down/Up events caused by Downstream Port Containment
  624. * if recovery from the error succeeded.
  625. */
  626. if ((events & PCI_EXP_SLTSTA_DLLSC) && pci_dpc_recovered(pdev) &&
  627. ctrl->state == ON_STATE) {
  628. events &= ~PCI_EXP_SLTSTA_DLLSC;
  629. pciehp_ignore_dpc_link_change(ctrl, pdev, irq);
  630. }
  631. /*
  632. * Disable requests have higher priority than Presence Detect Changed
  633. * or Data Link Layer State Changed events.
  634. */
  635. down_read_nested(&ctrl->reset_lock, ctrl->depth);
  636. if (events & DISABLE_SLOT)
  637. pciehp_handle_disable_request(ctrl);
  638. else if (events & (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC))
  639. pciehp_handle_presence_or_link_change(ctrl, events);
  640. up_read(&ctrl->reset_lock);
  641. ret = IRQ_HANDLED;
  642. out:
  643. pci_config_pm_runtime_put(pdev);
  644. ctrl->ist_running = false;
  645. wake_up(&ctrl->requester);
  646. return ret;
  647. }
  648. static int pciehp_poll(void *data)
  649. {
  650. struct controller *ctrl = data;
  651. schedule_timeout_idle(10 * HZ); /* start with 10 sec delay */
  652. while (!kthread_should_stop()) {
  653. /* poll for interrupt events or user requests */
  654. while (pciehp_isr(IRQ_NOTCONNECTED, ctrl) == IRQ_WAKE_THREAD ||
  655. atomic_read(&ctrl->pending_events))
  656. pciehp_ist(IRQ_NOTCONNECTED, ctrl);
  657. if (pciehp_poll_time <= 0 || pciehp_poll_time > 60)
  658. pciehp_poll_time = 2; /* clamp to sane value */
  659. schedule_timeout_idle(pciehp_poll_time * HZ);
  660. }
  661. return 0;
  662. }
  663. static void pcie_enable_notification(struct controller *ctrl)
  664. {
  665. u16 cmd, mask;
  666. /*
  667. * TBD: Power fault detected software notification support.
  668. *
  669. * Power fault detected software notification is not enabled
  670. * now, because it caused power fault detected interrupt storm
  671. * on some machines. On those machines, power fault detected
  672. * bit in the slot status register was set again immediately
  673. * when it is cleared in the interrupt service routine, and
  674. * next power fault detected interrupt was notified again.
  675. */
  676. /*
  677. * Always enable link events: thus link-up and link-down shall
  678. * always be treated as hotplug and unplug respectively. Enable
  679. * presence detect only if Attention Button is not present.
  680. */
  681. cmd = PCI_EXP_SLTCTL_DLLSCE;
  682. if (ATTN_BUTTN(ctrl))
  683. cmd |= PCI_EXP_SLTCTL_ABPE;
  684. else
  685. cmd |= PCI_EXP_SLTCTL_PDCE;
  686. if (!pciehp_poll_mode)
  687. cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
  688. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  689. PCI_EXP_SLTCTL_PFDE |
  690. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  691. PCI_EXP_SLTCTL_DLLSCE);
  692. pcie_write_cmd_nowait(ctrl, cmd, mask);
  693. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  694. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
  695. }
  696. static void pcie_disable_notification(struct controller *ctrl)
  697. {
  698. u16 mask;
  699. mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
  700. PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
  701. PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
  702. PCI_EXP_SLTCTL_DLLSCE);
  703. pcie_write_cmd(ctrl, 0, mask);
  704. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  705. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
  706. }
  707. void pcie_clear_hotplug_events(struct controller *ctrl)
  708. {
  709. pcie_capability_write_word(ctrl_dev(ctrl), PCI_EXP_SLTSTA,
  710. PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
  711. }
  712. void pcie_enable_interrupt(struct controller *ctrl)
  713. {
  714. u16 mask;
  715. mask = PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_DLLSCE;
  716. pcie_write_cmd(ctrl, mask, mask);
  717. }
  718. void pcie_disable_interrupt(struct controller *ctrl)
  719. {
  720. u16 mask;
  721. /*
  722. * Mask hot-plug interrupt to prevent it triggering immediately
  723. * when the link goes inactive (we still get PME when any of the
  724. * enabled events is detected). Same goes with Link Layer State
  725. * changed event which generates PME immediately when the link goes
  726. * inactive so mask it as well.
  727. */
  728. mask = PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_DLLSCE;
  729. pcie_write_cmd(ctrl, 0, mask);
  730. }
  731. /**
  732. * pciehp_slot_reset() - ignore link event caused by error-induced hot reset
  733. * @dev: PCI Express port service device
  734. *
  735. * Called from pcie_portdrv_slot_reset() after AER or DPC initiated a reset
  736. * further up in the hierarchy to recover from an error. The reset was
  737. * propagated down to this hotplug port. Ignore the resulting link flap.
  738. * If the link failed to retrain successfully, synthesize the ignored event.
  739. * Surprise removal during reset is detected through Presence Detect Changed.
  740. */
  741. int pciehp_slot_reset(struct pcie_device *dev)
  742. {
  743. struct controller *ctrl = get_service_data(dev);
  744. if (ctrl->state != ON_STATE)
  745. return 0;
  746. pcie_capability_write_word(dev->port, PCI_EXP_SLTSTA,
  747. PCI_EXP_SLTSTA_DLLSC);
  748. if (!pciehp_check_link_active(ctrl))
  749. pciehp_request(ctrl, PCI_EXP_SLTSTA_DLLSC);
  750. return 0;
  751. }
  752. /*
  753. * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
  754. * bus reset of the bridge, but at the same time we want to ensure that it is
  755. * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
  756. * disable link state notification and presence detection change notification
  757. * momentarily, if we see that they could interfere. Also, clear any spurious
  758. * events after.
  759. */
  760. int pciehp_reset_slot(struct hotplug_slot *hotplug_slot, bool probe)
  761. {
  762. struct controller *ctrl = to_ctrl(hotplug_slot);
  763. struct pci_dev *pdev = ctrl_dev(ctrl);
  764. u16 stat_mask = 0, ctrl_mask = 0;
  765. int rc;
  766. if (probe)
  767. return 0;
  768. down_write_nested(&ctrl->reset_lock, ctrl->depth);
  769. if (!ATTN_BUTTN(ctrl)) {
  770. ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
  771. stat_mask |= PCI_EXP_SLTSTA_PDC;
  772. }
  773. ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
  774. stat_mask |= PCI_EXP_SLTSTA_DLLSC;
  775. pcie_write_cmd(ctrl, 0, ctrl_mask);
  776. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  777. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
  778. rc = pci_bridge_secondary_bus_reset(ctrl->pcie->port);
  779. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
  780. pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
  781. ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
  782. pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
  783. up_write(&ctrl->reset_lock);
  784. return rc;
  785. }
  786. int pcie_init_notification(struct controller *ctrl)
  787. {
  788. if (pciehp_request_irq(ctrl))
  789. return -1;
  790. pcie_enable_notification(ctrl);
  791. ctrl->notification_enabled = 1;
  792. return 0;
  793. }
  794. void pcie_shutdown_notification(struct controller *ctrl)
  795. {
  796. if (ctrl->notification_enabled) {
  797. pcie_disable_notification(ctrl);
  798. pciehp_free_irq(ctrl);
  799. ctrl->notification_enabled = 0;
  800. }
  801. }
  802. static inline void dbg_ctrl(struct controller *ctrl)
  803. {
  804. struct pci_dev *pdev = ctrl->pcie->port;
  805. u16 reg16;
  806. ctrl_dbg(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
  807. pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &reg16);
  808. ctrl_dbg(ctrl, "Slot Status : 0x%04x\n", reg16);
  809. pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &reg16);
  810. ctrl_dbg(ctrl, "Slot Control : 0x%04x\n", reg16);
  811. }
  812. #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
  813. static inline int pcie_hotplug_depth(struct pci_dev *dev)
  814. {
  815. struct pci_bus *bus = dev->bus;
  816. int depth = 0;
  817. while (bus->parent) {
  818. bus = bus->parent;
  819. if (bus->self && bus->self->is_hotplug_bridge)
  820. depth++;
  821. }
  822. return depth;
  823. }
  824. struct controller *pcie_init(struct pcie_device *dev)
  825. {
  826. struct controller *ctrl;
  827. u32 slot_cap, slot_cap2, link_cap;
  828. u8 poweron;
  829. struct pci_dev *pdev = dev->port;
  830. struct pci_bus *subordinate = pdev->subordinate;
  831. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  832. if (!ctrl)
  833. return NULL;
  834. ctrl->pcie = dev;
  835. ctrl->depth = pcie_hotplug_depth(dev->port);
  836. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
  837. if (pdev->hotplug_user_indicators)
  838. slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);
  839. /*
  840. * We assume no Thunderbolt controllers support Command Complete events,
  841. * but some controllers falsely claim they do.
  842. */
  843. if (pdev->is_thunderbolt)
  844. slot_cap |= PCI_EXP_SLTCAP_NCCS;
  845. ctrl->slot_cap = slot_cap;
  846. mutex_init(&ctrl->ctrl_lock);
  847. mutex_init(&ctrl->state_lock);
  848. init_rwsem(&ctrl->reset_lock);
  849. init_waitqueue_head(&ctrl->requester);
  850. init_waitqueue_head(&ctrl->queue);
  851. INIT_DELAYED_WORK(&ctrl->button_work, pciehp_queue_pushbutton_work);
  852. dbg_ctrl(ctrl);
  853. down_read(&pci_bus_sem);
  854. ctrl->state = list_empty(&subordinate->devices) ? OFF_STATE : ON_STATE;
  855. up_read(&pci_bus_sem);
  856. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP2, &slot_cap2);
  857. if (slot_cap2 & PCI_EXP_SLTCAP2_IBPD) {
  858. pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_IBPD_DISABLE,
  859. PCI_EXP_SLTCTL_IBPD_DISABLE);
  860. ctrl->inband_presence_disabled = 1;
  861. }
  862. if (dmi_first_match(inband_presence_disabled_dmi_table))
  863. ctrl->inband_presence_disabled = 1;
  864. /* Check if Data Link Layer Link Active Reporting is implemented */
  865. pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
  866. /* Clear all remaining event bits in Slot Status register. */
  867. pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
  868. PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
  869. PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_CC |
  870. PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC);
  871. ctrl_info(ctrl, "Slot #%d AttnBtn%c PwrCtrl%c MRL%c AttnInd%c PwrInd%c HotPlug%c Surprise%c Interlock%c NoCompl%c IbPresDis%c LLActRep%c%s\n",
  872. (slot_cap & PCI_EXP_SLTCAP_PSN) >> 19,
  873. FLAG(slot_cap, PCI_EXP_SLTCAP_ABP),
  874. FLAG(slot_cap, PCI_EXP_SLTCAP_PCP),
  875. FLAG(slot_cap, PCI_EXP_SLTCAP_MRLSP),
  876. FLAG(slot_cap, PCI_EXP_SLTCAP_AIP),
  877. FLAG(slot_cap, PCI_EXP_SLTCAP_PIP),
  878. FLAG(slot_cap, PCI_EXP_SLTCAP_HPC),
  879. FLAG(slot_cap, PCI_EXP_SLTCAP_HPS),
  880. FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
  881. FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
  882. FLAG(slot_cap2, PCI_EXP_SLTCAP2_IBPD),
  883. FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC),
  884. pdev->broken_cmd_compl ? " (with Cmd Compl erratum)" : "");
  885. /*
  886. * If empty slot's power status is on, turn power off. The IRQ isn't
  887. * requested yet, so avoid triggering a notification with this command.
  888. */
  889. if (POWER_CTRL(ctrl)) {
  890. pciehp_get_power_status(ctrl, &poweron);
  891. if (!pciehp_card_present_or_link_active(ctrl) && poweron) {
  892. pcie_disable_notification(ctrl);
  893. pciehp_power_off_slot(ctrl);
  894. }
  895. }
  896. return ctrl;
  897. }
  898. void pciehp_release_ctrl(struct controller *ctrl)
  899. {
  900. cancel_delayed_work_sync(&ctrl->button_work);
  901. kfree(ctrl);
  902. }
  903. static void quirk_cmd_compl(struct pci_dev *pdev)
  904. {
  905. u32 slot_cap;
  906. if (pci_is_pcie(pdev)) {
  907. pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
  908. if (slot_cap & PCI_EXP_SLTCAP_HPC &&
  909. !(slot_cap & PCI_EXP_SLTCAP_NCCS))
  910. pdev->broken_cmd_compl = 1;
  911. }
  912. }
  913. DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
  914. PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
  915. DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x010e,
  916. PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
  917. DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0110,
  918. PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
  919. DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0400,
  920. PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
  921. DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0401,
  922. PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
  923. DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_HXT, 0x0401,
  924. PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);