pcie.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/pci.h>
  48. #include <linux/io.h>
  49. #include <linux/delay.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/aer.h>
  52. #include <linux/module.h>
  53. #include "hfi.h"
  54. #include "chip_registers.h"
  55. #include "aspm.h"
  56. /* link speed vector for Gen3 speed - not in Linux headers */
  57. #define GEN1_SPEED_VECTOR 0x1
  58. #define GEN2_SPEED_VECTOR 0x2
  59. #define GEN3_SPEED_VECTOR 0x3
  60. /*
  61. * This file contains PCIe utility routines.
  62. */
  63. /*
  64. * Code to adjust PCIe capabilities.
  65. */
  66. static void tune_pcie_caps(struct hfi1_devdata *);
  67. /*
  68. * Do all the common PCIe setup and initialization.
  69. * devdata is not yet allocated, and is not allocated until after this
  70. * routine returns success. Therefore dd_dev_err() can't be used for error
  71. * printing.
  72. */
  73. int hfi1_pcie_init(struct pci_dev *pdev, const struct pci_device_id *ent)
  74. {
  75. int ret;
  76. ret = pci_enable_device(pdev);
  77. if (ret) {
  78. /*
  79. * This can happen (in theory) iff:
  80. * We did a chip reset, and then failed to reprogram the
  81. * BAR, or the chip reset due to an internal error. We then
  82. * unloaded the driver and reloaded it.
  83. *
  84. * Both reset cases set the BAR back to initial state. For
  85. * the latter case, the AER sticky error bit at offset 0x718
  86. * should be set, but the Linux kernel doesn't yet know
  87. * about that, it appears. If the original BAR was retained
  88. * in the kernel data structures, this may be OK.
  89. */
  90. hfi1_early_err(&pdev->dev, "pci enable failed: error %d\n",
  91. -ret);
  92. goto done;
  93. }
  94. ret = pci_request_regions(pdev, DRIVER_NAME);
  95. if (ret) {
  96. hfi1_early_err(&pdev->dev,
  97. "pci_request_regions fails: err %d\n", -ret);
  98. goto bail;
  99. }
  100. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  101. if (ret) {
  102. /*
  103. * If the 64 bit setup fails, try 32 bit. Some systems
  104. * do not setup 64 bit maps on systems with 2GB or less
  105. * memory installed.
  106. */
  107. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  108. if (ret) {
  109. hfi1_early_err(&pdev->dev,
  110. "Unable to set DMA mask: %d\n", ret);
  111. goto bail;
  112. }
  113. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  114. } else {
  115. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  116. }
  117. if (ret) {
  118. hfi1_early_err(&pdev->dev,
  119. "Unable to set DMA consistent mask: %d\n", ret);
  120. goto bail;
  121. }
  122. pci_set_master(pdev);
  123. (void)pci_enable_pcie_error_reporting(pdev);
  124. goto done;
  125. bail:
  126. hfi1_pcie_cleanup(pdev);
  127. done:
  128. return ret;
  129. }
  130. /*
  131. * Clean what was done in hfi1_pcie_init()
  132. */
  133. void hfi1_pcie_cleanup(struct pci_dev *pdev)
  134. {
  135. pci_disable_device(pdev);
  136. /*
  137. * Release regions should be called after the disable. OK to
  138. * call if request regions has not been called or failed.
  139. */
  140. pci_release_regions(pdev);
  141. }
  142. /*
  143. * Do remaining PCIe setup, once dd is allocated, and save away
  144. * fields required to re-initialize after a chip reset, or for
  145. * various other purposes
  146. */
  147. int hfi1_pcie_ddinit(struct hfi1_devdata *dd, struct pci_dev *pdev)
  148. {
  149. unsigned long len;
  150. resource_size_t addr;
  151. dd->pcidev = pdev;
  152. pci_set_drvdata(pdev, dd);
  153. addr = pci_resource_start(pdev, 0);
  154. len = pci_resource_len(pdev, 0);
  155. /*
  156. * The TXE PIO buffers are at the tail end of the chip space.
  157. * Cut them off and map them separately.
  158. */
  159. /* sanity check vs expectations */
  160. if (len != TXE_PIO_SEND + TXE_PIO_SIZE) {
  161. dd_dev_err(dd, "chip PIO range does not match\n");
  162. return -EINVAL;
  163. }
  164. dd->kregbase = ioremap_nocache(addr, TXE_PIO_SEND);
  165. if (!dd->kregbase)
  166. return -ENOMEM;
  167. dd->piobase = ioremap_wc(addr + TXE_PIO_SEND, TXE_PIO_SIZE);
  168. if (!dd->piobase) {
  169. iounmap(dd->kregbase);
  170. return -ENOMEM;
  171. }
  172. dd->flags |= HFI1_PRESENT; /* now register routines work */
  173. dd->kregend = dd->kregbase + TXE_PIO_SEND;
  174. dd->physaddr = addr; /* used for io_remap, etc. */
  175. /*
  176. * Re-map the chip's RcvArray as write-combining to allow us
  177. * to write an entire cacheline worth of entries in one shot.
  178. * If this re-map fails, just continue - the RcvArray programming
  179. * function will handle both cases.
  180. */
  181. dd->chip_rcv_array_count = read_csr(dd, RCV_ARRAY_CNT);
  182. dd->rcvarray_wc = ioremap_wc(addr + RCV_ARRAY,
  183. dd->chip_rcv_array_count * 8);
  184. dd_dev_info(dd, "WC Remapped RcvArray: %p\n", dd->rcvarray_wc);
  185. /*
  186. * Save BARs and command to rewrite after device reset.
  187. */
  188. dd->pcibar0 = addr;
  189. dd->pcibar1 = addr >> 32;
  190. pci_read_config_dword(dd->pcidev, PCI_ROM_ADDRESS, &dd->pci_rom);
  191. pci_read_config_word(dd->pcidev, PCI_COMMAND, &dd->pci_command);
  192. pcie_capability_read_word(dd->pcidev, PCI_EXP_DEVCTL, &dd->pcie_devctl);
  193. pcie_capability_read_word(dd->pcidev, PCI_EXP_LNKCTL, &dd->pcie_lnkctl);
  194. pcie_capability_read_word(dd->pcidev, PCI_EXP_DEVCTL2,
  195. &dd->pcie_devctl2);
  196. pci_read_config_dword(dd->pcidev, PCI_CFG_MSIX0, &dd->pci_msix0);
  197. pci_read_config_dword(dd->pcidev, PCIE_CFG_SPCIE1, &dd->pci_lnkctl3);
  198. pci_read_config_dword(dd->pcidev, PCIE_CFG_TPH2, &dd->pci_tph2);
  199. return 0;
  200. }
  201. /*
  202. * Do PCIe cleanup related to dd, after chip-specific cleanup, etc. Just prior
  203. * to releasing the dd memory.
  204. * Void because all of the core pcie cleanup functions are void.
  205. */
  206. void hfi1_pcie_ddcleanup(struct hfi1_devdata *dd)
  207. {
  208. u64 __iomem *base = (void __iomem *)dd->kregbase;
  209. dd->flags &= ~HFI1_PRESENT;
  210. dd->kregbase = NULL;
  211. iounmap(base);
  212. if (dd->rcvarray_wc)
  213. iounmap(dd->rcvarray_wc);
  214. if (dd->piobase)
  215. iounmap(dd->piobase);
  216. }
  217. /*
  218. * Do a Function Level Reset (FLR) on the device.
  219. * Based on static function drivers/pci/pci.c:pcie_flr().
  220. */
  221. void hfi1_pcie_flr(struct hfi1_devdata *dd)
  222. {
  223. int i;
  224. u16 status;
  225. /* no need to check for the capability - we know the device has it */
  226. /* wait for Transaction Pending bit to clear, at most a few ms */
  227. for (i = 0; i < 4; i++) {
  228. if (i)
  229. msleep((1 << (i - 1)) * 100);
  230. pcie_capability_read_word(dd->pcidev, PCI_EXP_DEVSTA, &status);
  231. if (!(status & PCI_EXP_DEVSTA_TRPND))
  232. goto clear;
  233. }
  234. dd_dev_err(dd, "Transaction Pending bit is not clearing, proceeding with reset anyway\n");
  235. clear:
  236. pcie_capability_set_word(dd->pcidev, PCI_EXP_DEVCTL,
  237. PCI_EXP_DEVCTL_BCR_FLR);
  238. /* PCIe spec requires the function to be back within 100ms */
  239. msleep(100);
  240. }
  241. static void msix_setup(struct hfi1_devdata *dd, int pos, u32 *msixcnt,
  242. struct hfi1_msix_entry *hfi1_msix_entry)
  243. {
  244. int ret;
  245. int nvec = *msixcnt;
  246. struct msix_entry *msix_entry;
  247. int i;
  248. /*
  249. * We can't pass hfi1_msix_entry array to msix_setup
  250. * so use a dummy msix_entry array and copy the allocated
  251. * irq back to the hfi1_msix_entry array.
  252. */
  253. msix_entry = kmalloc_array(nvec, sizeof(*msix_entry), GFP_KERNEL);
  254. if (!msix_entry) {
  255. ret = -ENOMEM;
  256. goto do_intx;
  257. }
  258. for (i = 0; i < nvec; i++)
  259. msix_entry[i] = hfi1_msix_entry[i].msix;
  260. ret = pci_enable_msix_range(dd->pcidev, msix_entry, 1, nvec);
  261. if (ret < 0)
  262. goto free_msix_entry;
  263. nvec = ret;
  264. for (i = 0; i < nvec; i++)
  265. hfi1_msix_entry[i].msix = msix_entry[i];
  266. kfree(msix_entry);
  267. *msixcnt = nvec;
  268. return;
  269. free_msix_entry:
  270. kfree(msix_entry);
  271. do_intx:
  272. dd_dev_err(dd, "pci_enable_msix_range %d vectors failed: %d, falling back to INTx\n",
  273. nvec, ret);
  274. *msixcnt = 0;
  275. hfi1_enable_intx(dd->pcidev);
  276. }
  277. /* return the PCIe link speed from the given link status */
  278. static u32 extract_speed(u16 linkstat)
  279. {
  280. u32 speed;
  281. switch (linkstat & PCI_EXP_LNKSTA_CLS) {
  282. default: /* not defined, assume Gen1 */
  283. case PCI_EXP_LNKSTA_CLS_2_5GB:
  284. speed = 2500; /* Gen 1, 2.5GHz */
  285. break;
  286. case PCI_EXP_LNKSTA_CLS_5_0GB:
  287. speed = 5000; /* Gen 2, 5GHz */
  288. break;
  289. case GEN3_SPEED_VECTOR:
  290. speed = 8000; /* Gen 3, 8GHz */
  291. break;
  292. }
  293. return speed;
  294. }
  295. /* return the PCIe link speed from the given link status */
  296. static u32 extract_width(u16 linkstat)
  297. {
  298. return (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
  299. }
  300. /* read the link status and set dd->{lbus_width,lbus_speed,lbus_info} */
  301. static void update_lbus_info(struct hfi1_devdata *dd)
  302. {
  303. u16 linkstat;
  304. pcie_capability_read_word(dd->pcidev, PCI_EXP_LNKSTA, &linkstat);
  305. dd->lbus_width = extract_width(linkstat);
  306. dd->lbus_speed = extract_speed(linkstat);
  307. snprintf(dd->lbus_info, sizeof(dd->lbus_info),
  308. "PCIe,%uMHz,x%u", dd->lbus_speed, dd->lbus_width);
  309. }
  310. /*
  311. * Read in the current PCIe link width and speed. Find if the link is
  312. * Gen3 capable.
  313. */
  314. int pcie_speeds(struct hfi1_devdata *dd)
  315. {
  316. u32 linkcap;
  317. struct pci_dev *parent = dd->pcidev->bus->self;
  318. if (!pci_is_pcie(dd->pcidev)) {
  319. dd_dev_err(dd, "Can't find PCI Express capability!\n");
  320. return -EINVAL;
  321. }
  322. /* find if our max speed is Gen3 and parent supports Gen3 speeds */
  323. dd->link_gen3_capable = 1;
  324. pcie_capability_read_dword(dd->pcidev, PCI_EXP_LNKCAP, &linkcap);
  325. if ((linkcap & PCI_EXP_LNKCAP_SLS) != GEN3_SPEED_VECTOR) {
  326. dd_dev_info(dd,
  327. "This HFI is not Gen3 capable, max speed 0x%x, need 0x3\n",
  328. linkcap & PCI_EXP_LNKCAP_SLS);
  329. dd->link_gen3_capable = 0;
  330. }
  331. /*
  332. * bus->max_bus_speed is set from the bridge's linkcap Max Link Speed
  333. */
  334. if (parent && dd->pcidev->bus->max_bus_speed != PCIE_SPEED_8_0GT) {
  335. dd_dev_info(dd, "Parent PCIe bridge does not support Gen3\n");
  336. dd->link_gen3_capable = 0;
  337. }
  338. /* obtain the link width and current speed */
  339. update_lbus_info(dd);
  340. dd_dev_info(dd, "%s\n", dd->lbus_info);
  341. return 0;
  342. }
  343. /*
  344. * Returns in *nent:
  345. * - actual number of interrupts allocated
  346. * - 0 if fell back to INTx.
  347. */
  348. void request_msix(struct hfi1_devdata *dd, u32 *nent,
  349. struct hfi1_msix_entry *entry)
  350. {
  351. int pos;
  352. pos = dd->pcidev->msix_cap;
  353. if (*nent && pos) {
  354. msix_setup(dd, pos, nent, entry);
  355. /* did it, either MSI-X or INTx */
  356. } else {
  357. *nent = 0;
  358. hfi1_enable_intx(dd->pcidev);
  359. }
  360. tune_pcie_caps(dd);
  361. }
  362. void hfi1_enable_intx(struct pci_dev *pdev)
  363. {
  364. /* first, turn on INTx */
  365. pci_intx(pdev, 1);
  366. /* then turn off MSI-X */
  367. pci_disable_msix(pdev);
  368. }
  369. /* restore command and BARs after a reset has wiped them out */
  370. void restore_pci_variables(struct hfi1_devdata *dd)
  371. {
  372. pci_write_config_word(dd->pcidev, PCI_COMMAND, dd->pci_command);
  373. pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0, dd->pcibar0);
  374. pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1, dd->pcibar1);
  375. pci_write_config_dword(dd->pcidev, PCI_ROM_ADDRESS, dd->pci_rom);
  376. pcie_capability_write_word(dd->pcidev, PCI_EXP_DEVCTL, dd->pcie_devctl);
  377. pcie_capability_write_word(dd->pcidev, PCI_EXP_LNKCTL, dd->pcie_lnkctl);
  378. pcie_capability_write_word(dd->pcidev, PCI_EXP_DEVCTL2,
  379. dd->pcie_devctl2);
  380. pci_write_config_dword(dd->pcidev, PCI_CFG_MSIX0, dd->pci_msix0);
  381. pci_write_config_dword(dd->pcidev, PCIE_CFG_SPCIE1, dd->pci_lnkctl3);
  382. pci_write_config_dword(dd->pcidev, PCIE_CFG_TPH2, dd->pci_tph2);
  383. }
  384. /*
  385. * BIOS may not set PCIe bus-utilization parameters for best performance.
  386. * Check and optionally adjust them to maximize our throughput.
  387. */
  388. static int hfi1_pcie_caps;
  389. module_param_named(pcie_caps, hfi1_pcie_caps, int, S_IRUGO);
  390. MODULE_PARM_DESC(pcie_caps, "Max PCIe tuning: Payload (0..3), ReadReq (4..7)");
  391. uint aspm_mode = ASPM_MODE_DISABLED;
  392. module_param_named(aspm, aspm_mode, uint, S_IRUGO);
  393. MODULE_PARM_DESC(aspm, "PCIe ASPM: 0: disable, 1: enable, 2: dynamic");
  394. static void tune_pcie_caps(struct hfi1_devdata *dd)
  395. {
  396. struct pci_dev *parent;
  397. u16 rc_mpss, rc_mps, ep_mpss, ep_mps;
  398. u16 rc_mrrs, ep_mrrs, max_mrrs, ectl;
  399. /*
  400. * Turn on extended tags in DevCtl in case the BIOS has turned it off
  401. * to improve WFR SDMA bandwidth
  402. */
  403. pcie_capability_read_word(dd->pcidev, PCI_EXP_DEVCTL, &ectl);
  404. if (!(ectl & PCI_EXP_DEVCTL_EXT_TAG)) {
  405. dd_dev_info(dd, "Enabling PCIe extended tags\n");
  406. ectl |= PCI_EXP_DEVCTL_EXT_TAG;
  407. pcie_capability_write_word(dd->pcidev, PCI_EXP_DEVCTL, ectl);
  408. }
  409. /* Find out supported and configured values for parent (root) */
  410. parent = dd->pcidev->bus->self;
  411. /*
  412. * The driver cannot perform the tuning if it does not have
  413. * access to the upstream component.
  414. */
  415. if (!parent)
  416. return;
  417. if (!pci_is_root_bus(parent->bus)) {
  418. dd_dev_info(dd, "Parent not root\n");
  419. return;
  420. }
  421. if (!pci_is_pcie(parent) || !pci_is_pcie(dd->pcidev))
  422. return;
  423. rc_mpss = parent->pcie_mpss;
  424. rc_mps = ffs(pcie_get_mps(parent)) - 8;
  425. /* Find out supported and configured values for endpoint (us) */
  426. ep_mpss = dd->pcidev->pcie_mpss;
  427. ep_mps = ffs(pcie_get_mps(dd->pcidev)) - 8;
  428. /* Find max payload supported by root, endpoint */
  429. if (rc_mpss > ep_mpss)
  430. rc_mpss = ep_mpss;
  431. /* If Supported greater than limit in module param, limit it */
  432. if (rc_mpss > (hfi1_pcie_caps & 7))
  433. rc_mpss = hfi1_pcie_caps & 7;
  434. /* If less than (allowed, supported), bump root payload */
  435. if (rc_mpss > rc_mps) {
  436. rc_mps = rc_mpss;
  437. pcie_set_mps(parent, 128 << rc_mps);
  438. }
  439. /* If less than (allowed, supported), bump endpoint payload */
  440. if (rc_mpss > ep_mps) {
  441. ep_mps = rc_mpss;
  442. pcie_set_mps(dd->pcidev, 128 << ep_mps);
  443. }
  444. /*
  445. * Now the Read Request size.
  446. * No field for max supported, but PCIe spec limits it to 4096,
  447. * which is code '5' (log2(4096) - 7)
  448. */
  449. max_mrrs = 5;
  450. if (max_mrrs > ((hfi1_pcie_caps >> 4) & 7))
  451. max_mrrs = (hfi1_pcie_caps >> 4) & 7;
  452. max_mrrs = 128 << max_mrrs;
  453. rc_mrrs = pcie_get_readrq(parent);
  454. ep_mrrs = pcie_get_readrq(dd->pcidev);
  455. if (max_mrrs > rc_mrrs) {
  456. rc_mrrs = max_mrrs;
  457. pcie_set_readrq(parent, rc_mrrs);
  458. }
  459. if (max_mrrs > ep_mrrs) {
  460. ep_mrrs = max_mrrs;
  461. pcie_set_readrq(dd->pcidev, ep_mrrs);
  462. }
  463. }
  464. /* End of PCIe capability tuning */
  465. /*
  466. * From here through hfi1_pci_err_handler definition is invoked via
  467. * PCI error infrastructure, registered via pci
  468. */
  469. static pci_ers_result_t
  470. pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  471. {
  472. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  473. pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
  474. switch (state) {
  475. case pci_channel_io_normal:
  476. dd_dev_info(dd, "State Normal, ignoring\n");
  477. break;
  478. case pci_channel_io_frozen:
  479. dd_dev_info(dd, "State Frozen, requesting reset\n");
  480. pci_disable_device(pdev);
  481. ret = PCI_ERS_RESULT_NEED_RESET;
  482. break;
  483. case pci_channel_io_perm_failure:
  484. if (dd) {
  485. dd_dev_info(dd, "State Permanent Failure, disabling\n");
  486. /* no more register accesses! */
  487. dd->flags &= ~HFI1_PRESENT;
  488. hfi1_disable_after_error(dd);
  489. }
  490. /* else early, or other problem */
  491. ret = PCI_ERS_RESULT_DISCONNECT;
  492. break;
  493. default: /* shouldn't happen */
  494. dd_dev_info(dd, "HFI1 PCI errors detected (state %d)\n",
  495. state);
  496. break;
  497. }
  498. return ret;
  499. }
  500. static pci_ers_result_t
  501. pci_mmio_enabled(struct pci_dev *pdev)
  502. {
  503. u64 words = 0U;
  504. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  505. pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
  506. if (dd && dd->pport) {
  507. words = read_port_cntr(dd->pport, C_RX_WORDS, CNTR_INVALID_VL);
  508. if (words == ~0ULL)
  509. ret = PCI_ERS_RESULT_NEED_RESET;
  510. dd_dev_info(dd,
  511. "HFI1 mmio_enabled function called, read wordscntr %Lx, returning %d\n",
  512. words, ret);
  513. }
  514. return ret;
  515. }
  516. static pci_ers_result_t
  517. pci_slot_reset(struct pci_dev *pdev)
  518. {
  519. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  520. dd_dev_info(dd, "HFI1 slot_reset function called, ignored\n");
  521. return PCI_ERS_RESULT_CAN_RECOVER;
  522. }
  523. static void
  524. pci_resume(struct pci_dev *pdev)
  525. {
  526. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  527. dd_dev_info(dd, "HFI1 resume function called\n");
  528. pci_cleanup_aer_uncorrect_error_status(pdev);
  529. /*
  530. * Running jobs will fail, since it's asynchronous
  531. * unlike sysfs-requested reset. Better than
  532. * doing nothing.
  533. */
  534. hfi1_init(dd, 1); /* same as re-init after reset */
  535. }
  536. const struct pci_error_handlers hfi1_pci_err_handler = {
  537. .error_detected = pci_error_detected,
  538. .mmio_enabled = pci_mmio_enabled,
  539. .slot_reset = pci_slot_reset,
  540. .resume = pci_resume,
  541. };
  542. /*============================================================================*/
  543. /* PCIe Gen3 support */
  544. /*
  545. * This code is separated out because it is expected to be removed in the
  546. * final shipping product. If not, then it will be revisited and items
  547. * will be moved to more standard locations.
  548. */
  549. /* ASIC_PCI_SD_HOST_STATUS.FW_DNLD_STS field values */
  550. #define DL_STATUS_HFI0 0x1 /* hfi0 firmware download complete */
  551. #define DL_STATUS_HFI1 0x2 /* hfi1 firmware download complete */
  552. #define DL_STATUS_BOTH 0x3 /* hfi0 and hfi1 firmware download complete */
  553. /* ASIC_PCI_SD_HOST_STATUS.FW_DNLD_ERR field values */
  554. #define DL_ERR_NONE 0x0 /* no error */
  555. #define DL_ERR_SWAP_PARITY 0x1 /* parity error in SerDes interrupt */
  556. /* or response data */
  557. #define DL_ERR_DISABLED 0x2 /* hfi disabled */
  558. #define DL_ERR_SECURITY 0x3 /* security check failed */
  559. #define DL_ERR_SBUS 0x4 /* SBus status error */
  560. #define DL_ERR_XFR_PARITY 0x5 /* parity error during ROM transfer*/
  561. /* gasket block secondary bus reset delay */
  562. #define SBR_DELAY_US 200000 /* 200ms */
  563. /* mask for PCIe capability register lnkctl2 target link speed */
  564. #define LNKCTL2_TARGET_LINK_SPEED_MASK 0xf
  565. static uint pcie_target = 3;
  566. module_param(pcie_target, uint, S_IRUGO);
  567. MODULE_PARM_DESC(pcie_target, "PCIe target speed (0 skip, 1-3 Gen1-3)");
  568. static uint pcie_force;
  569. module_param(pcie_force, uint, S_IRUGO);
  570. MODULE_PARM_DESC(pcie_force, "Force driver to do a PCIe firmware download even if already at target speed");
  571. static uint pcie_retry = 5;
  572. module_param(pcie_retry, uint, S_IRUGO);
  573. MODULE_PARM_DESC(pcie_retry, "Driver will try this many times to reach requested speed");
  574. #define UNSET_PSET 255
  575. #define DEFAULT_DISCRETE_PSET 2 /* discrete HFI */
  576. #define DEFAULT_MCP_PSET 6 /* MCP HFI */
  577. static uint pcie_pset = UNSET_PSET;
  578. module_param(pcie_pset, uint, S_IRUGO);
  579. MODULE_PARM_DESC(pcie_pset, "PCIe Eq Pset value to use, range is 0-10");
  580. static uint pcie_ctle = 3; /* discrete on, integrated on */
  581. module_param(pcie_ctle, uint, S_IRUGO);
  582. MODULE_PARM_DESC(pcie_ctle, "PCIe static CTLE mode, bit 0 - discrete on/off, bit 1 - integrated on/off");
  583. /* equalization columns */
  584. #define PREC 0
  585. #define ATTN 1
  586. #define POST 2
  587. /* discrete silicon preliminary equalization values */
  588. static const u8 discrete_preliminary_eq[11][3] = {
  589. /* prec attn post */
  590. { 0x00, 0x00, 0x12 }, /* p0 */
  591. { 0x00, 0x00, 0x0c }, /* p1 */
  592. { 0x00, 0x00, 0x0f }, /* p2 */
  593. { 0x00, 0x00, 0x09 }, /* p3 */
  594. { 0x00, 0x00, 0x00 }, /* p4 */
  595. { 0x06, 0x00, 0x00 }, /* p5 */
  596. { 0x09, 0x00, 0x00 }, /* p6 */
  597. { 0x06, 0x00, 0x0f }, /* p7 */
  598. { 0x09, 0x00, 0x09 }, /* p8 */
  599. { 0x0c, 0x00, 0x00 }, /* p9 */
  600. { 0x00, 0x00, 0x18 }, /* p10 */
  601. };
  602. /* integrated silicon preliminary equalization values */
  603. static const u8 integrated_preliminary_eq[11][3] = {
  604. /* prec attn post */
  605. { 0x00, 0x1e, 0x07 }, /* p0 */
  606. { 0x00, 0x1e, 0x05 }, /* p1 */
  607. { 0x00, 0x1e, 0x06 }, /* p2 */
  608. { 0x00, 0x1e, 0x04 }, /* p3 */
  609. { 0x00, 0x1e, 0x00 }, /* p4 */
  610. { 0x03, 0x1e, 0x00 }, /* p5 */
  611. { 0x04, 0x1e, 0x00 }, /* p6 */
  612. { 0x03, 0x1e, 0x06 }, /* p7 */
  613. { 0x03, 0x1e, 0x04 }, /* p8 */
  614. { 0x05, 0x1e, 0x00 }, /* p9 */
  615. { 0x00, 0x1e, 0x0a }, /* p10 */
  616. };
  617. static const u8 discrete_ctle_tunings[11][4] = {
  618. /* DC LF HF BW */
  619. { 0x48, 0x0b, 0x04, 0x04 }, /* p0 */
  620. { 0x60, 0x05, 0x0f, 0x0a }, /* p1 */
  621. { 0x50, 0x09, 0x06, 0x06 }, /* p2 */
  622. { 0x68, 0x05, 0x0f, 0x0a }, /* p3 */
  623. { 0x80, 0x05, 0x0f, 0x0a }, /* p4 */
  624. { 0x70, 0x05, 0x0f, 0x0a }, /* p5 */
  625. { 0x68, 0x05, 0x0f, 0x0a }, /* p6 */
  626. { 0x38, 0x0f, 0x00, 0x00 }, /* p7 */
  627. { 0x48, 0x09, 0x06, 0x06 }, /* p8 */
  628. { 0x60, 0x05, 0x0f, 0x0a }, /* p9 */
  629. { 0x38, 0x0f, 0x00, 0x00 }, /* p10 */
  630. };
  631. static const u8 integrated_ctle_tunings[11][4] = {
  632. /* DC LF HF BW */
  633. { 0x38, 0x0f, 0x00, 0x00 }, /* p0 */
  634. { 0x38, 0x0f, 0x00, 0x00 }, /* p1 */
  635. { 0x38, 0x0f, 0x00, 0x00 }, /* p2 */
  636. { 0x38, 0x0f, 0x00, 0x00 }, /* p3 */
  637. { 0x58, 0x0a, 0x05, 0x05 }, /* p4 */
  638. { 0x48, 0x0a, 0x05, 0x05 }, /* p5 */
  639. { 0x40, 0x0a, 0x05, 0x05 }, /* p6 */
  640. { 0x38, 0x0f, 0x00, 0x00 }, /* p7 */
  641. { 0x38, 0x0f, 0x00, 0x00 }, /* p8 */
  642. { 0x38, 0x09, 0x06, 0x06 }, /* p9 */
  643. { 0x38, 0x0e, 0x01, 0x01 }, /* p10 */
  644. };
  645. /* helper to format the value to write to hardware */
  646. #define eq_value(pre, curr, post) \
  647. ((((u32)(pre)) << \
  648. PCIE_CFG_REG_PL102_GEN3_EQ_PRE_CURSOR_PSET_SHIFT) \
  649. | (((u32)(curr)) << PCIE_CFG_REG_PL102_GEN3_EQ_CURSOR_PSET_SHIFT) \
  650. | (((u32)(post)) << \
  651. PCIE_CFG_REG_PL102_GEN3_EQ_POST_CURSOR_PSET_SHIFT))
  652. /*
  653. * Load the given EQ preset table into the PCIe hardware.
  654. */
  655. static int load_eq_table(struct hfi1_devdata *dd, const u8 eq[11][3], u8 fs,
  656. u8 div)
  657. {
  658. struct pci_dev *pdev = dd->pcidev;
  659. u32 hit_error = 0;
  660. u32 violation;
  661. u32 i;
  662. u8 c_minus1, c0, c_plus1;
  663. for (i = 0; i < 11; i++) {
  664. /* set index */
  665. pci_write_config_dword(pdev, PCIE_CFG_REG_PL103, i);
  666. /* write the value */
  667. c_minus1 = eq[i][PREC] / div;
  668. c0 = fs - (eq[i][PREC] / div) - (eq[i][POST] / div);
  669. c_plus1 = eq[i][POST] / div;
  670. pci_write_config_dword(pdev, PCIE_CFG_REG_PL102,
  671. eq_value(c_minus1, c0, c_plus1));
  672. /* check if these coefficients violate EQ rules */
  673. pci_read_config_dword(dd->pcidev, PCIE_CFG_REG_PL105,
  674. &violation);
  675. if (violation
  676. & PCIE_CFG_REG_PL105_GEN3_EQ_VIOLATE_COEF_RULES_SMASK){
  677. if (hit_error == 0) {
  678. dd_dev_err(dd,
  679. "Gen3 EQ Table Coefficient rule violations\n");
  680. dd_dev_err(dd, " prec attn post\n");
  681. }
  682. dd_dev_err(dd, " p%02d: %02x %02x %02x\n",
  683. i, (u32)eq[i][0], (u32)eq[i][1],
  684. (u32)eq[i][2]);
  685. dd_dev_err(dd, " %02x %02x %02x\n",
  686. (u32)c_minus1, (u32)c0, (u32)c_plus1);
  687. hit_error = 1;
  688. }
  689. }
  690. if (hit_error)
  691. return -EINVAL;
  692. return 0;
  693. }
  694. /*
  695. * Steps to be done after the PCIe firmware is downloaded and
  696. * before the SBR for the Pcie Gen3.
  697. * The SBus resource is already being held.
  698. */
  699. static void pcie_post_steps(struct hfi1_devdata *dd)
  700. {
  701. int i;
  702. set_sbus_fast_mode(dd);
  703. /*
  704. * Write to the PCIe PCSes to set the G3_LOCKED_NEXT bits to 1.
  705. * This avoids a spurious framing error that can otherwise be
  706. * generated by the MAC layer.
  707. *
  708. * Use individual addresses since no broadcast is set up.
  709. */
  710. for (i = 0; i < NUM_PCIE_SERDES; i++) {
  711. sbus_request(dd, pcie_pcs_addrs[dd->hfi1_id][i],
  712. 0x03, WRITE_SBUS_RECEIVER, 0x00022132);
  713. }
  714. clear_sbus_fast_mode(dd);
  715. }
  716. /*
  717. * Trigger a secondary bus reset (SBR) on ourselves using our parent.
  718. *
  719. * Based on pci_parent_bus_reset() which is not exported by the
  720. * kernel core.
  721. */
  722. static int trigger_sbr(struct hfi1_devdata *dd)
  723. {
  724. struct pci_dev *dev = dd->pcidev;
  725. struct pci_dev *pdev;
  726. /* need a parent */
  727. if (!dev->bus->self) {
  728. dd_dev_err(dd, "%s: no parent device\n", __func__);
  729. return -ENOTTY;
  730. }
  731. /* should not be anyone else on the bus */
  732. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  733. if (pdev != dev) {
  734. dd_dev_err(dd,
  735. "%s: another device is on the same bus\n",
  736. __func__);
  737. return -ENOTTY;
  738. }
  739. /*
  740. * A secondary bus reset (SBR) issues a hot reset to our device.
  741. * The following routine does a 1s wait after the reset is dropped
  742. * per PCI Trhfa (recovery time). PCIe 3.0 section 6.6.1 -
  743. * Conventional Reset, paragraph 3, line 35 also says that a 1s
  744. * delay after a reset is required. Per spec requirements,
  745. * the link is either working or not after that point.
  746. */
  747. pci_reset_bridge_secondary_bus(dev->bus->self);
  748. return 0;
  749. }
  750. /*
  751. * Write the given gasket interrupt register.
  752. */
  753. static void write_gasket_interrupt(struct hfi1_devdata *dd, int index,
  754. u16 code, u16 data)
  755. {
  756. write_csr(dd, ASIC_PCIE_SD_INTRPT_LIST + (index * 8),
  757. (((u64)code << ASIC_PCIE_SD_INTRPT_LIST_INTRPT_CODE_SHIFT) |
  758. ((u64)data << ASIC_PCIE_SD_INTRPT_LIST_INTRPT_DATA_SHIFT)));
  759. }
  760. /*
  761. * Tell the gasket logic how to react to the reset.
  762. */
  763. static void arm_gasket_logic(struct hfi1_devdata *dd)
  764. {
  765. u64 reg;
  766. reg = (((u64)1 << dd->hfi1_id) <<
  767. ASIC_PCIE_SD_HOST_CMD_INTRPT_CMD_SHIFT) |
  768. ((u64)pcie_serdes_broadcast[dd->hfi1_id] <<
  769. ASIC_PCIE_SD_HOST_CMD_SBUS_RCVR_ADDR_SHIFT |
  770. ASIC_PCIE_SD_HOST_CMD_SBR_MODE_SMASK |
  771. ((u64)SBR_DELAY_US & ASIC_PCIE_SD_HOST_CMD_TIMER_MASK) <<
  772. ASIC_PCIE_SD_HOST_CMD_TIMER_SHIFT);
  773. write_csr(dd, ASIC_PCIE_SD_HOST_CMD, reg);
  774. /* read back to push the write */
  775. read_csr(dd, ASIC_PCIE_SD_HOST_CMD);
  776. }
  777. /*
  778. * CCE_PCIE_CTRL long name helpers
  779. * We redefine these shorter macros to use in the code while leaving
  780. * chip_registers.h to be autogenerated from the hardware spec.
  781. */
  782. #define LANE_BUNDLE_MASK CCE_PCIE_CTRL_PCIE_LANE_BUNDLE_MASK
  783. #define LANE_BUNDLE_SHIFT CCE_PCIE_CTRL_PCIE_LANE_BUNDLE_SHIFT
  784. #define LANE_DELAY_MASK CCE_PCIE_CTRL_PCIE_LANE_DELAY_MASK
  785. #define LANE_DELAY_SHIFT CCE_PCIE_CTRL_PCIE_LANE_DELAY_SHIFT
  786. #define MARGIN_OVERWRITE_ENABLE_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_OVERWRITE_ENABLE_SHIFT
  787. #define MARGIN_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_SHIFT
  788. #define MARGIN_G1_G2_OVERWRITE_MASK CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_OVERWRITE_ENABLE_MASK
  789. #define MARGIN_G1_G2_OVERWRITE_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_OVERWRITE_ENABLE_SHIFT
  790. #define MARGIN_GEN1_GEN2_MASK CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_MASK
  791. #define MARGIN_GEN1_GEN2_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_SHIFT
  792. /*
  793. * Write xmt_margin for full-swing (WFR-B) or half-swing (WFR-C).
  794. */
  795. static void write_xmt_margin(struct hfi1_devdata *dd, const char *fname)
  796. {
  797. u64 pcie_ctrl;
  798. u64 xmt_margin;
  799. u64 xmt_margin_oe;
  800. u64 lane_delay;
  801. u64 lane_bundle;
  802. pcie_ctrl = read_csr(dd, CCE_PCIE_CTRL);
  803. /*
  804. * For Discrete, use full-swing.
  805. * - PCIe TX defaults to full-swing.
  806. * Leave this register as default.
  807. * For Integrated, use half-swing
  808. * - Copy xmt_margin and xmt_margin_oe
  809. * from Gen1/Gen2 to Gen3.
  810. */
  811. if (dd->pcidev->device == PCI_DEVICE_ID_INTEL1) { /* integrated */
  812. /* extract initial fields */
  813. xmt_margin = (pcie_ctrl >> MARGIN_GEN1_GEN2_SHIFT)
  814. & MARGIN_GEN1_GEN2_MASK;
  815. xmt_margin_oe = (pcie_ctrl >> MARGIN_G1_G2_OVERWRITE_SHIFT)
  816. & MARGIN_G1_G2_OVERWRITE_MASK;
  817. lane_delay = (pcie_ctrl >> LANE_DELAY_SHIFT) & LANE_DELAY_MASK;
  818. lane_bundle = (pcie_ctrl >> LANE_BUNDLE_SHIFT)
  819. & LANE_BUNDLE_MASK;
  820. /*
  821. * For A0, EFUSE values are not set. Override with the
  822. * correct values.
  823. */
  824. if (is_ax(dd)) {
  825. /*
  826. * xmt_margin and OverwiteEnabel should be the
  827. * same for Gen1/Gen2 and Gen3
  828. */
  829. xmt_margin = 0x5;
  830. xmt_margin_oe = 0x1;
  831. lane_delay = 0xF; /* Delay 240ns. */
  832. lane_bundle = 0x0; /* Set to 1 lane. */
  833. }
  834. /* overwrite existing values */
  835. pcie_ctrl = (xmt_margin << MARGIN_GEN1_GEN2_SHIFT)
  836. | (xmt_margin_oe << MARGIN_G1_G2_OVERWRITE_SHIFT)
  837. | (xmt_margin << MARGIN_SHIFT)
  838. | (xmt_margin_oe << MARGIN_OVERWRITE_ENABLE_SHIFT)
  839. | (lane_delay << LANE_DELAY_SHIFT)
  840. | (lane_bundle << LANE_BUNDLE_SHIFT);
  841. write_csr(dd, CCE_PCIE_CTRL, pcie_ctrl);
  842. }
  843. dd_dev_dbg(dd, "%s: program XMT margin, CcePcieCtrl 0x%llx\n",
  844. fname, pcie_ctrl);
  845. }
  846. /*
  847. * Do all the steps needed to transition the PCIe link to Gen3 speed.
  848. */
  849. int do_pcie_gen3_transition(struct hfi1_devdata *dd)
  850. {
  851. struct pci_dev *parent = dd->pcidev->bus->self;
  852. u64 fw_ctrl;
  853. u64 reg, therm;
  854. u32 reg32, fs, lf;
  855. u32 status, err;
  856. int ret;
  857. int do_retry, retry_count = 0;
  858. int intnum = 0;
  859. uint default_pset;
  860. u16 target_vector, target_speed;
  861. u16 lnkctl2, vendor;
  862. u8 div;
  863. const u8 (*eq)[3];
  864. const u8 (*ctle_tunings)[4];
  865. uint static_ctle_mode;
  866. int return_error = 0;
  867. /* PCIe Gen3 is for the ASIC only */
  868. if (dd->icode != ICODE_RTL_SILICON)
  869. return 0;
  870. if (pcie_target == 1) { /* target Gen1 */
  871. target_vector = GEN1_SPEED_VECTOR;
  872. target_speed = 2500;
  873. } else if (pcie_target == 2) { /* target Gen2 */
  874. target_vector = GEN2_SPEED_VECTOR;
  875. target_speed = 5000;
  876. } else if (pcie_target == 3) { /* target Gen3 */
  877. target_vector = GEN3_SPEED_VECTOR;
  878. target_speed = 8000;
  879. } else {
  880. /* off or invalid target - skip */
  881. dd_dev_info(dd, "%s: Skipping PCIe transition\n", __func__);
  882. return 0;
  883. }
  884. /* if already at target speed, done (unless forced) */
  885. if (dd->lbus_speed == target_speed) {
  886. dd_dev_info(dd, "%s: PCIe already at gen%d, %s\n", __func__,
  887. pcie_target,
  888. pcie_force ? "re-doing anyway" : "skipping");
  889. if (!pcie_force)
  890. return 0;
  891. }
  892. /*
  893. * The driver cannot do the transition if it has no access to the
  894. * upstream component
  895. */
  896. if (!parent) {
  897. dd_dev_info(dd, "%s: No upstream, Can't do gen3 transition\n",
  898. __func__);
  899. return 0;
  900. }
  901. /*
  902. * Do the Gen3 transition. Steps are those of the PCIe Gen3
  903. * recipe.
  904. */
  905. /* step 1: pcie link working in gen1/gen2 */
  906. /* step 2: if either side is not capable of Gen3, done */
  907. if (pcie_target == 3 && !dd->link_gen3_capable) {
  908. dd_dev_err(dd, "The PCIe link is not Gen3 capable\n");
  909. ret = -ENOSYS;
  910. goto done_no_mutex;
  911. }
  912. /* hold the SBus resource across the firmware download and SBR */
  913. ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
  914. if (ret) {
  915. dd_dev_err(dd, "%s: unable to acquire SBus resource\n",
  916. __func__);
  917. return ret;
  918. }
  919. /* make sure thermal polling is not causing interrupts */
  920. therm = read_csr(dd, ASIC_CFG_THERM_POLL_EN);
  921. if (therm) {
  922. write_csr(dd, ASIC_CFG_THERM_POLL_EN, 0x0);
  923. msleep(100);
  924. dd_dev_info(dd, "%s: Disabled therm polling\n",
  925. __func__);
  926. }
  927. retry:
  928. /* the SBus download will reset the spico for thermal */
  929. /* step 3: download SBus Master firmware */
  930. /* step 4: download PCIe Gen3 SerDes firmware */
  931. dd_dev_info(dd, "%s: downloading firmware\n", __func__);
  932. ret = load_pcie_firmware(dd);
  933. if (ret) {
  934. /* do not proceed if the firmware cannot be downloaded */
  935. return_error = 1;
  936. goto done;
  937. }
  938. /* step 5: set up device parameter settings */
  939. dd_dev_info(dd, "%s: setting PCIe registers\n", __func__);
  940. /*
  941. * PcieCfgSpcie1 - Link Control 3
  942. * Leave at reset value. No need to set PerfEq - link equalization
  943. * will be performed automatically after the SBR when the target
  944. * speed is 8GT/s.
  945. */
  946. /* clear all 16 per-lane error bits (PCIe: Lane Error Status) */
  947. pci_write_config_dword(dd->pcidev, PCIE_CFG_SPCIE2, 0xffff);
  948. /* step 5a: Set Synopsys Port Logic registers */
  949. /*
  950. * PcieCfgRegPl2 - Port Force Link
  951. *
  952. * Set the low power field to 0x10 to avoid unnecessary power
  953. * management messages. All other fields are zero.
  954. */
  955. reg32 = 0x10ul << PCIE_CFG_REG_PL2_LOW_PWR_ENT_CNT_SHIFT;
  956. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL2, reg32);
  957. /*
  958. * PcieCfgRegPl100 - Gen3 Control
  959. *
  960. * turn off PcieCfgRegPl100.Gen3ZRxDcNonCompl
  961. * turn on PcieCfgRegPl100.EqEieosCnt
  962. * Everything else zero.
  963. */
  964. reg32 = PCIE_CFG_REG_PL100_EQ_EIEOS_CNT_SMASK;
  965. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL100, reg32);
  966. /*
  967. * PcieCfgRegPl101 - Gen3 EQ FS and LF
  968. * PcieCfgRegPl102 - Gen3 EQ Presets to Coefficients Mapping
  969. * PcieCfgRegPl103 - Gen3 EQ Preset Index
  970. * PcieCfgRegPl105 - Gen3 EQ Status
  971. *
  972. * Give initial EQ settings.
  973. */
  974. if (dd->pcidev->device == PCI_DEVICE_ID_INTEL0) { /* discrete */
  975. /* 1000mV, FS=24, LF = 8 */
  976. fs = 24;
  977. lf = 8;
  978. div = 3;
  979. eq = discrete_preliminary_eq;
  980. default_pset = DEFAULT_DISCRETE_PSET;
  981. ctle_tunings = discrete_ctle_tunings;
  982. /* bit 0 - discrete on/off */
  983. static_ctle_mode = pcie_ctle & 0x1;
  984. } else {
  985. /* 400mV, FS=29, LF = 9 */
  986. fs = 29;
  987. lf = 9;
  988. div = 1;
  989. eq = integrated_preliminary_eq;
  990. default_pset = DEFAULT_MCP_PSET;
  991. ctle_tunings = integrated_ctle_tunings;
  992. /* bit 1 - integrated on/off */
  993. static_ctle_mode = (pcie_ctle >> 1) & 0x1;
  994. }
  995. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL101,
  996. (fs <<
  997. PCIE_CFG_REG_PL101_GEN3_EQ_LOCAL_FS_SHIFT) |
  998. (lf <<
  999. PCIE_CFG_REG_PL101_GEN3_EQ_LOCAL_LF_SHIFT));
  1000. ret = load_eq_table(dd, eq, fs, div);
  1001. if (ret)
  1002. goto done;
  1003. /*
  1004. * PcieCfgRegPl106 - Gen3 EQ Control
  1005. *
  1006. * Set Gen3EqPsetReqVec, leave other fields 0.
  1007. */
  1008. if (pcie_pset == UNSET_PSET)
  1009. pcie_pset = default_pset;
  1010. if (pcie_pset > 10) { /* valid range is 0-10, inclusive */
  1011. dd_dev_err(dd, "%s: Invalid Eq Pset %u, setting to %d\n",
  1012. __func__, pcie_pset, default_pset);
  1013. pcie_pset = default_pset;
  1014. }
  1015. dd_dev_info(dd, "%s: using EQ Pset %u\n", __func__, pcie_pset);
  1016. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL106,
  1017. ((1 << pcie_pset) <<
  1018. PCIE_CFG_REG_PL106_GEN3_EQ_PSET_REQ_VEC_SHIFT) |
  1019. PCIE_CFG_REG_PL106_GEN3_EQ_EVAL2MS_DISABLE_SMASK |
  1020. PCIE_CFG_REG_PL106_GEN3_EQ_PHASE23_EXIT_MODE_SMASK);
  1021. /*
  1022. * step 5b: Do post firmware download steps via SBus
  1023. */
  1024. dd_dev_info(dd, "%s: doing pcie post steps\n", __func__);
  1025. pcie_post_steps(dd);
  1026. /*
  1027. * step 5c: Program gasket interrupts
  1028. */
  1029. /* set the Rx Bit Rate to REFCLK ratio */
  1030. write_gasket_interrupt(dd, intnum++, 0x0006, 0x0050);
  1031. /* disable pCal for PCIe Gen3 RX equalization */
  1032. /* select adaptive or static CTLE */
  1033. write_gasket_interrupt(dd, intnum++, 0x0026,
  1034. 0x5b01 | (static_ctle_mode << 3));
  1035. /*
  1036. * Enable iCal for PCIe Gen3 RX equalization, and set which
  1037. * evaluation of RX_EQ_EVAL will launch the iCal procedure.
  1038. */
  1039. write_gasket_interrupt(dd, intnum++, 0x0026, 0x5202);
  1040. if (static_ctle_mode) {
  1041. /* apply static CTLE tunings */
  1042. u8 pcie_dc, pcie_lf, pcie_hf, pcie_bw;
  1043. pcie_dc = ctle_tunings[pcie_pset][0];
  1044. pcie_lf = ctle_tunings[pcie_pset][1];
  1045. pcie_hf = ctle_tunings[pcie_pset][2];
  1046. pcie_bw = ctle_tunings[pcie_pset][3];
  1047. write_gasket_interrupt(dd, intnum++, 0x0026, 0x0200 | pcie_dc);
  1048. write_gasket_interrupt(dd, intnum++, 0x0026, 0x0100 | pcie_lf);
  1049. write_gasket_interrupt(dd, intnum++, 0x0026, 0x0000 | pcie_hf);
  1050. write_gasket_interrupt(dd, intnum++, 0x0026, 0x5500 | pcie_bw);
  1051. }
  1052. /* terminate list */
  1053. write_gasket_interrupt(dd, intnum++, 0x0000, 0x0000);
  1054. /*
  1055. * step 5d: program XMT margin
  1056. */
  1057. write_xmt_margin(dd, __func__);
  1058. /*
  1059. * step 5e: disable active state power management (ASPM). It
  1060. * will be enabled if required later
  1061. */
  1062. dd_dev_info(dd, "%s: clearing ASPM\n", __func__);
  1063. aspm_hw_disable_l1(dd);
  1064. /*
  1065. * step 5f: clear DirectSpeedChange
  1066. * PcieCfgRegPl67.DirectSpeedChange must be zero to prevent the
  1067. * change in the speed target from starting before we are ready.
  1068. * This field defaults to 0 and we are not changing it, so nothing
  1069. * needs to be done.
  1070. */
  1071. /* step 5g: Set target link speed */
  1072. /*
  1073. * Set target link speed to be target on both device and parent.
  1074. * On setting the parent: Some system BIOSs "helpfully" set the
  1075. * parent target speed to Gen2 to match the ASIC's initial speed.
  1076. * We can set the target Gen3 because we have already checked
  1077. * that it is Gen3 capable earlier.
  1078. */
  1079. dd_dev_info(dd, "%s: setting parent target link speed\n", __func__);
  1080. pcie_capability_read_word(parent, PCI_EXP_LNKCTL2, &lnkctl2);
  1081. dd_dev_info(dd, "%s: ..old link control2: 0x%x\n", __func__,
  1082. (u32)lnkctl2);
  1083. /* only write to parent if target is not as high as ours */
  1084. if ((lnkctl2 & LNKCTL2_TARGET_LINK_SPEED_MASK) < target_vector) {
  1085. lnkctl2 &= ~LNKCTL2_TARGET_LINK_SPEED_MASK;
  1086. lnkctl2 |= target_vector;
  1087. dd_dev_info(dd, "%s: ..new link control2: 0x%x\n", __func__,
  1088. (u32)lnkctl2);
  1089. pcie_capability_write_word(parent, PCI_EXP_LNKCTL2, lnkctl2);
  1090. } else {
  1091. dd_dev_info(dd, "%s: ..target speed is OK\n", __func__);
  1092. }
  1093. dd_dev_info(dd, "%s: setting target link speed\n", __func__);
  1094. pcie_capability_read_word(dd->pcidev, PCI_EXP_LNKCTL2, &lnkctl2);
  1095. dd_dev_info(dd, "%s: ..old link control2: 0x%x\n", __func__,
  1096. (u32)lnkctl2);
  1097. lnkctl2 &= ~LNKCTL2_TARGET_LINK_SPEED_MASK;
  1098. lnkctl2 |= target_vector;
  1099. dd_dev_info(dd, "%s: ..new link control2: 0x%x\n", __func__,
  1100. (u32)lnkctl2);
  1101. pcie_capability_write_word(dd->pcidev, PCI_EXP_LNKCTL2, lnkctl2);
  1102. /* step 5h: arm gasket logic */
  1103. /* hold DC in reset across the SBR */
  1104. write_csr(dd, CCE_DC_CTRL, CCE_DC_CTRL_DC_RESET_SMASK);
  1105. (void)read_csr(dd, CCE_DC_CTRL); /* DC reset hold */
  1106. /* save firmware control across the SBR */
  1107. fw_ctrl = read_csr(dd, MISC_CFG_FW_CTRL);
  1108. dd_dev_info(dd, "%s: arming gasket logic\n", __func__);
  1109. arm_gasket_logic(dd);
  1110. /*
  1111. * step 6: quiesce PCIe link
  1112. * The chip has already been reset, so there will be no traffic
  1113. * from the chip. Linux has no easy way to enforce that it will
  1114. * not try to access the device, so we just need to hope it doesn't
  1115. * do it while we are doing the reset.
  1116. */
  1117. /*
  1118. * step 7: initiate the secondary bus reset (SBR)
  1119. * step 8: hardware brings the links back up
  1120. * step 9: wait for link speed transition to be complete
  1121. */
  1122. dd_dev_info(dd, "%s: calling trigger_sbr\n", __func__);
  1123. ret = trigger_sbr(dd);
  1124. if (ret)
  1125. goto done;
  1126. /* step 10: decide what to do next */
  1127. /* check if we can read PCI space */
  1128. ret = pci_read_config_word(dd->pcidev, PCI_VENDOR_ID, &vendor);
  1129. if (ret) {
  1130. dd_dev_info(dd,
  1131. "%s: read of VendorID failed after SBR, err %d\n",
  1132. __func__, ret);
  1133. return_error = 1;
  1134. goto done;
  1135. }
  1136. if (vendor == 0xffff) {
  1137. dd_dev_info(dd, "%s: VendorID is all 1s after SBR\n", __func__);
  1138. return_error = 1;
  1139. ret = -EIO;
  1140. goto done;
  1141. }
  1142. /* restore PCI space registers we know were reset */
  1143. dd_dev_info(dd, "%s: calling restore_pci_variables\n", __func__);
  1144. restore_pci_variables(dd);
  1145. /* restore firmware control */
  1146. write_csr(dd, MISC_CFG_FW_CTRL, fw_ctrl);
  1147. /*
  1148. * Check the gasket block status.
  1149. *
  1150. * This is the first CSR read after the SBR. If the read returns
  1151. * all 1s (fails), the link did not make it back.
  1152. *
  1153. * Once we're sure we can read and write, clear the DC reset after
  1154. * the SBR. Then check for any per-lane errors. Then look over
  1155. * the status.
  1156. */
  1157. reg = read_csr(dd, ASIC_PCIE_SD_HOST_STATUS);
  1158. dd_dev_info(dd, "%s: gasket block status: 0x%llx\n", __func__, reg);
  1159. if (reg == ~0ull) { /* PCIe read failed/timeout */
  1160. dd_dev_err(dd, "SBR failed - unable to read from device\n");
  1161. return_error = 1;
  1162. ret = -ENOSYS;
  1163. goto done;
  1164. }
  1165. /* clear the DC reset */
  1166. write_csr(dd, CCE_DC_CTRL, 0);
  1167. /* Set the LED off */
  1168. setextled(dd, 0);
  1169. /* check for any per-lane errors */
  1170. pci_read_config_dword(dd->pcidev, PCIE_CFG_SPCIE2, &reg32);
  1171. dd_dev_info(dd, "%s: per-lane errors: 0x%x\n", __func__, reg32);
  1172. /* extract status, look for our HFI */
  1173. status = (reg >> ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_STS_SHIFT)
  1174. & ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_STS_MASK;
  1175. if ((status & (1 << dd->hfi1_id)) == 0) {
  1176. dd_dev_err(dd,
  1177. "%s: gasket status 0x%x, expecting 0x%x\n",
  1178. __func__, status, 1 << dd->hfi1_id);
  1179. ret = -EIO;
  1180. goto done;
  1181. }
  1182. /* extract error */
  1183. err = (reg >> ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_ERR_SHIFT)
  1184. & ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_ERR_MASK;
  1185. if (err) {
  1186. dd_dev_err(dd, "%s: gasket error %d\n", __func__, err);
  1187. ret = -EIO;
  1188. goto done;
  1189. }
  1190. /* update our link information cache */
  1191. update_lbus_info(dd);
  1192. dd_dev_info(dd, "%s: new speed and width: %s\n", __func__,
  1193. dd->lbus_info);
  1194. if (dd->lbus_speed != target_speed) { /* not target */
  1195. /* maybe retry */
  1196. do_retry = retry_count < pcie_retry;
  1197. dd_dev_err(dd, "PCIe link speed did not switch to Gen%d%s\n",
  1198. pcie_target, do_retry ? ", retrying" : "");
  1199. retry_count++;
  1200. if (do_retry) {
  1201. msleep(100); /* allow time to settle */
  1202. goto retry;
  1203. }
  1204. ret = -EIO;
  1205. }
  1206. done:
  1207. if (therm) {
  1208. write_csr(dd, ASIC_CFG_THERM_POLL_EN, 0x1);
  1209. msleep(100);
  1210. dd_dev_info(dd, "%s: Re-enable therm polling\n",
  1211. __func__);
  1212. }
  1213. release_chip_resource(dd, CR_SBUS);
  1214. done_no_mutex:
  1215. /* return no error if it is OK to be at current speed */
  1216. if (ret && !return_error) {
  1217. dd_dev_err(dd, "Proceeding at current speed PCIe speed\n");
  1218. ret = 0;
  1219. }
  1220. dd_dev_info(dd, "%s: done\n", __func__);
  1221. return ret;
  1222. }