pcie.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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. pci_read_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0, &dd->pcibar0);
  189. pci_read_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1, &dd->pcibar1);
  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. static void msix_setup(struct hfi1_devdata *dd, int pos, u32 *msixcnt,
  218. struct hfi1_msix_entry *hfi1_msix_entry)
  219. {
  220. int ret;
  221. int nvec = *msixcnt;
  222. struct msix_entry *msix_entry;
  223. int i;
  224. /*
  225. * We can't pass hfi1_msix_entry array to msix_setup
  226. * so use a dummy msix_entry array and copy the allocated
  227. * irq back to the hfi1_msix_entry array.
  228. */
  229. msix_entry = kmalloc_array(nvec, sizeof(*msix_entry), GFP_KERNEL);
  230. if (!msix_entry) {
  231. ret = -ENOMEM;
  232. goto do_intx;
  233. }
  234. for (i = 0; i < nvec; i++)
  235. msix_entry[i] = hfi1_msix_entry[i].msix;
  236. ret = pci_enable_msix_range(dd->pcidev, msix_entry, 1, nvec);
  237. if (ret < 0)
  238. goto free_msix_entry;
  239. nvec = ret;
  240. for (i = 0; i < nvec; i++)
  241. hfi1_msix_entry[i].msix = msix_entry[i];
  242. kfree(msix_entry);
  243. *msixcnt = nvec;
  244. return;
  245. free_msix_entry:
  246. kfree(msix_entry);
  247. do_intx:
  248. dd_dev_err(dd, "pci_enable_msix_range %d vectors failed: %d, falling back to INTx\n",
  249. nvec, ret);
  250. *msixcnt = 0;
  251. hfi1_enable_intx(dd->pcidev);
  252. }
  253. /* return the PCIe link speed from the given link status */
  254. static u32 extract_speed(u16 linkstat)
  255. {
  256. u32 speed;
  257. switch (linkstat & PCI_EXP_LNKSTA_CLS) {
  258. default: /* not defined, assume Gen1 */
  259. case PCI_EXP_LNKSTA_CLS_2_5GB:
  260. speed = 2500; /* Gen 1, 2.5GHz */
  261. break;
  262. case PCI_EXP_LNKSTA_CLS_5_0GB:
  263. speed = 5000; /* Gen 2, 5GHz */
  264. break;
  265. case GEN3_SPEED_VECTOR:
  266. speed = 8000; /* Gen 3, 8GHz */
  267. break;
  268. }
  269. return speed;
  270. }
  271. /* return the PCIe link speed from the given link status */
  272. static u32 extract_width(u16 linkstat)
  273. {
  274. return (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
  275. }
  276. /* read the link status and set dd->{lbus_width,lbus_speed,lbus_info} */
  277. static void update_lbus_info(struct hfi1_devdata *dd)
  278. {
  279. u16 linkstat;
  280. pcie_capability_read_word(dd->pcidev, PCI_EXP_LNKSTA, &linkstat);
  281. dd->lbus_width = extract_width(linkstat);
  282. dd->lbus_speed = extract_speed(linkstat);
  283. snprintf(dd->lbus_info, sizeof(dd->lbus_info),
  284. "PCIe,%uMHz,x%u", dd->lbus_speed, dd->lbus_width);
  285. }
  286. /*
  287. * Read in the current PCIe link width and speed. Find if the link is
  288. * Gen3 capable.
  289. */
  290. int pcie_speeds(struct hfi1_devdata *dd)
  291. {
  292. u32 linkcap;
  293. struct pci_dev *parent = dd->pcidev->bus->self;
  294. if (!pci_is_pcie(dd->pcidev)) {
  295. dd_dev_err(dd, "Can't find PCI Express capability!\n");
  296. return -EINVAL;
  297. }
  298. /* find if our max speed is Gen3 and parent supports Gen3 speeds */
  299. dd->link_gen3_capable = 1;
  300. pcie_capability_read_dword(dd->pcidev, PCI_EXP_LNKCAP, &linkcap);
  301. if ((linkcap & PCI_EXP_LNKCAP_SLS) != GEN3_SPEED_VECTOR) {
  302. dd_dev_info(dd,
  303. "This HFI is not Gen3 capable, max speed 0x%x, need 0x3\n",
  304. linkcap & PCI_EXP_LNKCAP_SLS);
  305. dd->link_gen3_capable = 0;
  306. }
  307. /*
  308. * bus->max_bus_speed is set from the bridge's linkcap Max Link Speed
  309. */
  310. if (parent && dd->pcidev->bus->max_bus_speed != PCIE_SPEED_8_0GT) {
  311. dd_dev_info(dd, "Parent PCIe bridge does not support Gen3\n");
  312. dd->link_gen3_capable = 0;
  313. }
  314. /* obtain the link width and current speed */
  315. update_lbus_info(dd);
  316. dd_dev_info(dd, "%s\n", dd->lbus_info);
  317. return 0;
  318. }
  319. /*
  320. * Returns in *nent:
  321. * - actual number of interrupts allocated
  322. * - 0 if fell back to INTx.
  323. */
  324. void request_msix(struct hfi1_devdata *dd, u32 *nent,
  325. struct hfi1_msix_entry *entry)
  326. {
  327. int pos;
  328. pos = dd->pcidev->msix_cap;
  329. if (*nent && pos) {
  330. msix_setup(dd, pos, nent, entry);
  331. /* did it, either MSI-X or INTx */
  332. } else {
  333. *nent = 0;
  334. hfi1_enable_intx(dd->pcidev);
  335. }
  336. tune_pcie_caps(dd);
  337. }
  338. void hfi1_enable_intx(struct pci_dev *pdev)
  339. {
  340. /* first, turn on INTx */
  341. pci_intx(pdev, 1);
  342. /* then turn off MSI-X */
  343. pci_disable_msix(pdev);
  344. }
  345. /* restore command and BARs after a reset has wiped them out */
  346. void restore_pci_variables(struct hfi1_devdata *dd)
  347. {
  348. pci_write_config_word(dd->pcidev, PCI_COMMAND, dd->pci_command);
  349. pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0, dd->pcibar0);
  350. pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1, dd->pcibar1);
  351. pci_write_config_dword(dd->pcidev, PCI_ROM_ADDRESS, dd->pci_rom);
  352. pcie_capability_write_word(dd->pcidev, PCI_EXP_DEVCTL, dd->pcie_devctl);
  353. pcie_capability_write_word(dd->pcidev, PCI_EXP_LNKCTL, dd->pcie_lnkctl);
  354. pcie_capability_write_word(dd->pcidev, PCI_EXP_DEVCTL2,
  355. dd->pcie_devctl2);
  356. pci_write_config_dword(dd->pcidev, PCI_CFG_MSIX0, dd->pci_msix0);
  357. pci_write_config_dword(dd->pcidev, PCIE_CFG_SPCIE1, dd->pci_lnkctl3);
  358. pci_write_config_dword(dd->pcidev, PCIE_CFG_TPH2, dd->pci_tph2);
  359. }
  360. /*
  361. * BIOS may not set PCIe bus-utilization parameters for best performance.
  362. * Check and optionally adjust them to maximize our throughput.
  363. */
  364. static int hfi1_pcie_caps;
  365. module_param_named(pcie_caps, hfi1_pcie_caps, int, S_IRUGO);
  366. MODULE_PARM_DESC(pcie_caps, "Max PCIe tuning: Payload (0..3), ReadReq (4..7)");
  367. uint aspm_mode = ASPM_MODE_DISABLED;
  368. module_param_named(aspm, aspm_mode, uint, S_IRUGO);
  369. MODULE_PARM_DESC(aspm, "PCIe ASPM: 0: disable, 1: enable, 2: dynamic");
  370. static void tune_pcie_caps(struct hfi1_devdata *dd)
  371. {
  372. struct pci_dev *parent;
  373. u16 rc_mpss, rc_mps, ep_mpss, ep_mps;
  374. u16 rc_mrrs, ep_mrrs, max_mrrs, ectl;
  375. /*
  376. * Turn on extended tags in DevCtl in case the BIOS has turned it off
  377. * to improve WFR SDMA bandwidth
  378. */
  379. pcie_capability_read_word(dd->pcidev, PCI_EXP_DEVCTL, &ectl);
  380. if (!(ectl & PCI_EXP_DEVCTL_EXT_TAG)) {
  381. dd_dev_info(dd, "Enabling PCIe extended tags\n");
  382. ectl |= PCI_EXP_DEVCTL_EXT_TAG;
  383. pcie_capability_write_word(dd->pcidev, PCI_EXP_DEVCTL, ectl);
  384. }
  385. /* Find out supported and configured values for parent (root) */
  386. parent = dd->pcidev->bus->self;
  387. /*
  388. * The driver cannot perform the tuning if it does not have
  389. * access to the upstream component.
  390. */
  391. if (!parent)
  392. return;
  393. if (!pci_is_root_bus(parent->bus)) {
  394. dd_dev_info(dd, "Parent not root\n");
  395. return;
  396. }
  397. if (!pci_is_pcie(parent) || !pci_is_pcie(dd->pcidev))
  398. return;
  399. rc_mpss = parent->pcie_mpss;
  400. rc_mps = ffs(pcie_get_mps(parent)) - 8;
  401. /* Find out supported and configured values for endpoint (us) */
  402. ep_mpss = dd->pcidev->pcie_mpss;
  403. ep_mps = ffs(pcie_get_mps(dd->pcidev)) - 8;
  404. /* Find max payload supported by root, endpoint */
  405. if (rc_mpss > ep_mpss)
  406. rc_mpss = ep_mpss;
  407. /* If Supported greater than limit in module param, limit it */
  408. if (rc_mpss > (hfi1_pcie_caps & 7))
  409. rc_mpss = hfi1_pcie_caps & 7;
  410. /* If less than (allowed, supported), bump root payload */
  411. if (rc_mpss > rc_mps) {
  412. rc_mps = rc_mpss;
  413. pcie_set_mps(parent, 128 << rc_mps);
  414. }
  415. /* If less than (allowed, supported), bump endpoint payload */
  416. if (rc_mpss > ep_mps) {
  417. ep_mps = rc_mpss;
  418. pcie_set_mps(dd->pcidev, 128 << ep_mps);
  419. }
  420. /*
  421. * Now the Read Request size.
  422. * No field for max supported, but PCIe spec limits it to 4096,
  423. * which is code '5' (log2(4096) - 7)
  424. */
  425. max_mrrs = 5;
  426. if (max_mrrs > ((hfi1_pcie_caps >> 4) & 7))
  427. max_mrrs = (hfi1_pcie_caps >> 4) & 7;
  428. max_mrrs = 128 << max_mrrs;
  429. rc_mrrs = pcie_get_readrq(parent);
  430. ep_mrrs = pcie_get_readrq(dd->pcidev);
  431. if (max_mrrs > rc_mrrs) {
  432. rc_mrrs = max_mrrs;
  433. pcie_set_readrq(parent, rc_mrrs);
  434. }
  435. if (max_mrrs > ep_mrrs) {
  436. ep_mrrs = max_mrrs;
  437. pcie_set_readrq(dd->pcidev, ep_mrrs);
  438. }
  439. }
  440. /* End of PCIe capability tuning */
  441. /*
  442. * From here through hfi1_pci_err_handler definition is invoked via
  443. * PCI error infrastructure, registered via pci
  444. */
  445. static pci_ers_result_t
  446. pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  447. {
  448. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  449. pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
  450. switch (state) {
  451. case pci_channel_io_normal:
  452. dd_dev_info(dd, "State Normal, ignoring\n");
  453. break;
  454. case pci_channel_io_frozen:
  455. dd_dev_info(dd, "State Frozen, requesting reset\n");
  456. pci_disable_device(pdev);
  457. ret = PCI_ERS_RESULT_NEED_RESET;
  458. break;
  459. case pci_channel_io_perm_failure:
  460. if (dd) {
  461. dd_dev_info(dd, "State Permanent Failure, disabling\n");
  462. /* no more register accesses! */
  463. dd->flags &= ~HFI1_PRESENT;
  464. hfi1_disable_after_error(dd);
  465. }
  466. /* else early, or other problem */
  467. ret = PCI_ERS_RESULT_DISCONNECT;
  468. break;
  469. default: /* shouldn't happen */
  470. dd_dev_info(dd, "HFI1 PCI errors detected (state %d)\n",
  471. state);
  472. break;
  473. }
  474. return ret;
  475. }
  476. static pci_ers_result_t
  477. pci_mmio_enabled(struct pci_dev *pdev)
  478. {
  479. u64 words = 0U;
  480. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  481. pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
  482. if (dd && dd->pport) {
  483. words = read_port_cntr(dd->pport, C_RX_WORDS, CNTR_INVALID_VL);
  484. if (words == ~0ULL)
  485. ret = PCI_ERS_RESULT_NEED_RESET;
  486. dd_dev_info(dd,
  487. "HFI1 mmio_enabled function called, read wordscntr %llx, returning %d\n",
  488. words, ret);
  489. }
  490. return ret;
  491. }
  492. static pci_ers_result_t
  493. pci_slot_reset(struct pci_dev *pdev)
  494. {
  495. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  496. dd_dev_info(dd, "HFI1 slot_reset function called, ignored\n");
  497. return PCI_ERS_RESULT_CAN_RECOVER;
  498. }
  499. static void
  500. pci_resume(struct pci_dev *pdev)
  501. {
  502. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  503. dd_dev_info(dd, "HFI1 resume function called\n");
  504. pci_cleanup_aer_uncorrect_error_status(pdev);
  505. /*
  506. * Running jobs will fail, since it's asynchronous
  507. * unlike sysfs-requested reset. Better than
  508. * doing nothing.
  509. */
  510. hfi1_init(dd, 1); /* same as re-init after reset */
  511. }
  512. const struct pci_error_handlers hfi1_pci_err_handler = {
  513. .error_detected = pci_error_detected,
  514. .mmio_enabled = pci_mmio_enabled,
  515. .slot_reset = pci_slot_reset,
  516. .resume = pci_resume,
  517. };
  518. /*============================================================================*/
  519. /* PCIe Gen3 support */
  520. /*
  521. * This code is separated out because it is expected to be removed in the
  522. * final shipping product. If not, then it will be revisited and items
  523. * will be moved to more standard locations.
  524. */
  525. /* ASIC_PCI_SD_HOST_STATUS.FW_DNLD_STS field values */
  526. #define DL_STATUS_HFI0 0x1 /* hfi0 firmware download complete */
  527. #define DL_STATUS_HFI1 0x2 /* hfi1 firmware download complete */
  528. #define DL_STATUS_BOTH 0x3 /* hfi0 and hfi1 firmware download complete */
  529. /* ASIC_PCI_SD_HOST_STATUS.FW_DNLD_ERR field values */
  530. #define DL_ERR_NONE 0x0 /* no error */
  531. #define DL_ERR_SWAP_PARITY 0x1 /* parity error in SerDes interrupt */
  532. /* or response data */
  533. #define DL_ERR_DISABLED 0x2 /* hfi disabled */
  534. #define DL_ERR_SECURITY 0x3 /* security check failed */
  535. #define DL_ERR_SBUS 0x4 /* SBus status error */
  536. #define DL_ERR_XFR_PARITY 0x5 /* parity error during ROM transfer*/
  537. /* gasket block secondary bus reset delay */
  538. #define SBR_DELAY_US 200000 /* 200ms */
  539. /* mask for PCIe capability register lnkctl2 target link speed */
  540. #define LNKCTL2_TARGET_LINK_SPEED_MASK 0xf
  541. static uint pcie_target = 3;
  542. module_param(pcie_target, uint, S_IRUGO);
  543. MODULE_PARM_DESC(pcie_target, "PCIe target speed (0 skip, 1-3 Gen1-3)");
  544. static uint pcie_force;
  545. module_param(pcie_force, uint, S_IRUGO);
  546. MODULE_PARM_DESC(pcie_force, "Force driver to do a PCIe firmware download even if already at target speed");
  547. static uint pcie_retry = 5;
  548. module_param(pcie_retry, uint, S_IRUGO);
  549. MODULE_PARM_DESC(pcie_retry, "Driver will try this many times to reach requested speed");
  550. #define UNSET_PSET 255
  551. #define DEFAULT_DISCRETE_PSET 2 /* discrete HFI */
  552. #define DEFAULT_MCP_PSET 6 /* MCP HFI */
  553. static uint pcie_pset = UNSET_PSET;
  554. module_param(pcie_pset, uint, S_IRUGO);
  555. MODULE_PARM_DESC(pcie_pset, "PCIe Eq Pset value to use, range is 0-10");
  556. static uint pcie_ctle = 3; /* discrete on, integrated on */
  557. module_param(pcie_ctle, uint, S_IRUGO);
  558. MODULE_PARM_DESC(pcie_ctle, "PCIe static CTLE mode, bit 0 - discrete on/off, bit 1 - integrated on/off");
  559. /* equalization columns */
  560. #define PREC 0
  561. #define ATTN 1
  562. #define POST 2
  563. /* discrete silicon preliminary equalization values */
  564. static const u8 discrete_preliminary_eq[11][3] = {
  565. /* prec attn post */
  566. { 0x00, 0x00, 0x12 }, /* p0 */
  567. { 0x00, 0x00, 0x0c }, /* p1 */
  568. { 0x00, 0x00, 0x0f }, /* p2 */
  569. { 0x00, 0x00, 0x09 }, /* p3 */
  570. { 0x00, 0x00, 0x00 }, /* p4 */
  571. { 0x06, 0x00, 0x00 }, /* p5 */
  572. { 0x09, 0x00, 0x00 }, /* p6 */
  573. { 0x06, 0x00, 0x0f }, /* p7 */
  574. { 0x09, 0x00, 0x09 }, /* p8 */
  575. { 0x0c, 0x00, 0x00 }, /* p9 */
  576. { 0x00, 0x00, 0x18 }, /* p10 */
  577. };
  578. /* integrated silicon preliminary equalization values */
  579. static const u8 integrated_preliminary_eq[11][3] = {
  580. /* prec attn post */
  581. { 0x00, 0x1e, 0x07 }, /* p0 */
  582. { 0x00, 0x1e, 0x05 }, /* p1 */
  583. { 0x00, 0x1e, 0x06 }, /* p2 */
  584. { 0x00, 0x1e, 0x04 }, /* p3 */
  585. { 0x00, 0x1e, 0x00 }, /* p4 */
  586. { 0x03, 0x1e, 0x00 }, /* p5 */
  587. { 0x04, 0x1e, 0x00 }, /* p6 */
  588. { 0x03, 0x1e, 0x06 }, /* p7 */
  589. { 0x03, 0x1e, 0x04 }, /* p8 */
  590. { 0x05, 0x1e, 0x00 }, /* p9 */
  591. { 0x00, 0x1e, 0x0a }, /* p10 */
  592. };
  593. static const u8 discrete_ctle_tunings[11][4] = {
  594. /* DC LF HF BW */
  595. { 0x48, 0x0b, 0x04, 0x04 }, /* p0 */
  596. { 0x60, 0x05, 0x0f, 0x0a }, /* p1 */
  597. { 0x50, 0x09, 0x06, 0x06 }, /* p2 */
  598. { 0x68, 0x05, 0x0f, 0x0a }, /* p3 */
  599. { 0x80, 0x05, 0x0f, 0x0a }, /* p4 */
  600. { 0x70, 0x05, 0x0f, 0x0a }, /* p5 */
  601. { 0x68, 0x05, 0x0f, 0x0a }, /* p6 */
  602. { 0x38, 0x0f, 0x00, 0x00 }, /* p7 */
  603. { 0x48, 0x09, 0x06, 0x06 }, /* p8 */
  604. { 0x60, 0x05, 0x0f, 0x0a }, /* p9 */
  605. { 0x38, 0x0f, 0x00, 0x00 }, /* p10 */
  606. };
  607. static const u8 integrated_ctle_tunings[11][4] = {
  608. /* DC LF HF BW */
  609. { 0x38, 0x0f, 0x00, 0x00 }, /* p0 */
  610. { 0x38, 0x0f, 0x00, 0x00 }, /* p1 */
  611. { 0x38, 0x0f, 0x00, 0x00 }, /* p2 */
  612. { 0x38, 0x0f, 0x00, 0x00 }, /* p3 */
  613. { 0x58, 0x0a, 0x05, 0x05 }, /* p4 */
  614. { 0x48, 0x0a, 0x05, 0x05 }, /* p5 */
  615. { 0x40, 0x0a, 0x05, 0x05 }, /* p6 */
  616. { 0x38, 0x0f, 0x00, 0x00 }, /* p7 */
  617. { 0x38, 0x0f, 0x00, 0x00 }, /* p8 */
  618. { 0x38, 0x09, 0x06, 0x06 }, /* p9 */
  619. { 0x38, 0x0e, 0x01, 0x01 }, /* p10 */
  620. };
  621. /* helper to format the value to write to hardware */
  622. #define eq_value(pre, curr, post) \
  623. ((((u32)(pre)) << \
  624. PCIE_CFG_REG_PL102_GEN3_EQ_PRE_CURSOR_PSET_SHIFT) \
  625. | (((u32)(curr)) << PCIE_CFG_REG_PL102_GEN3_EQ_CURSOR_PSET_SHIFT) \
  626. | (((u32)(post)) << \
  627. PCIE_CFG_REG_PL102_GEN3_EQ_POST_CURSOR_PSET_SHIFT))
  628. /*
  629. * Load the given EQ preset table into the PCIe hardware.
  630. */
  631. static int load_eq_table(struct hfi1_devdata *dd, const u8 eq[11][3], u8 fs,
  632. u8 div)
  633. {
  634. struct pci_dev *pdev = dd->pcidev;
  635. u32 hit_error = 0;
  636. u32 violation;
  637. u32 i;
  638. u8 c_minus1, c0, c_plus1;
  639. for (i = 0; i < 11; i++) {
  640. /* set index */
  641. pci_write_config_dword(pdev, PCIE_CFG_REG_PL103, i);
  642. /* write the value */
  643. c_minus1 = eq[i][PREC] / div;
  644. c0 = fs - (eq[i][PREC] / div) - (eq[i][POST] / div);
  645. c_plus1 = eq[i][POST] / div;
  646. pci_write_config_dword(pdev, PCIE_CFG_REG_PL102,
  647. eq_value(c_minus1, c0, c_plus1));
  648. /* check if these coefficients violate EQ rules */
  649. pci_read_config_dword(dd->pcidev, PCIE_CFG_REG_PL105,
  650. &violation);
  651. if (violation
  652. & PCIE_CFG_REG_PL105_GEN3_EQ_VIOLATE_COEF_RULES_SMASK){
  653. if (hit_error == 0) {
  654. dd_dev_err(dd,
  655. "Gen3 EQ Table Coefficient rule violations\n");
  656. dd_dev_err(dd, " prec attn post\n");
  657. }
  658. dd_dev_err(dd, " p%02d: %02x %02x %02x\n",
  659. i, (u32)eq[i][0], (u32)eq[i][1],
  660. (u32)eq[i][2]);
  661. dd_dev_err(dd, " %02x %02x %02x\n",
  662. (u32)c_minus1, (u32)c0, (u32)c_plus1);
  663. hit_error = 1;
  664. }
  665. }
  666. if (hit_error)
  667. return -EINVAL;
  668. return 0;
  669. }
  670. /*
  671. * Steps to be done after the PCIe firmware is downloaded and
  672. * before the SBR for the Pcie Gen3.
  673. * The SBus resource is already being held.
  674. */
  675. static void pcie_post_steps(struct hfi1_devdata *dd)
  676. {
  677. int i;
  678. set_sbus_fast_mode(dd);
  679. /*
  680. * Write to the PCIe PCSes to set the G3_LOCKED_NEXT bits to 1.
  681. * This avoids a spurious framing error that can otherwise be
  682. * generated by the MAC layer.
  683. *
  684. * Use individual addresses since no broadcast is set up.
  685. */
  686. for (i = 0; i < NUM_PCIE_SERDES; i++) {
  687. sbus_request(dd, pcie_pcs_addrs[dd->hfi1_id][i],
  688. 0x03, WRITE_SBUS_RECEIVER, 0x00022132);
  689. }
  690. clear_sbus_fast_mode(dd);
  691. }
  692. /*
  693. * Trigger a secondary bus reset (SBR) on ourselves using our parent.
  694. *
  695. * Based on pci_parent_bus_reset() which is not exported by the
  696. * kernel core.
  697. */
  698. static int trigger_sbr(struct hfi1_devdata *dd)
  699. {
  700. struct pci_dev *dev = dd->pcidev;
  701. struct pci_dev *pdev;
  702. /* need a parent */
  703. if (!dev->bus->self) {
  704. dd_dev_err(dd, "%s: no parent device\n", __func__);
  705. return -ENOTTY;
  706. }
  707. /* should not be anyone else on the bus */
  708. list_for_each_entry(pdev, &dev->bus->devices, bus_list)
  709. if (pdev != dev) {
  710. dd_dev_err(dd,
  711. "%s: another device is on the same bus\n",
  712. __func__);
  713. return -ENOTTY;
  714. }
  715. /*
  716. * A secondary bus reset (SBR) issues a hot reset to our device.
  717. * The following routine does a 1s wait after the reset is dropped
  718. * per PCI Trhfa (recovery time). PCIe 3.0 section 6.6.1 -
  719. * Conventional Reset, paragraph 3, line 35 also says that a 1s
  720. * delay after a reset is required. Per spec requirements,
  721. * the link is either working or not after that point.
  722. */
  723. pci_reset_bridge_secondary_bus(dev->bus->self);
  724. return 0;
  725. }
  726. /*
  727. * Write the given gasket interrupt register.
  728. */
  729. static void write_gasket_interrupt(struct hfi1_devdata *dd, int index,
  730. u16 code, u16 data)
  731. {
  732. write_csr(dd, ASIC_PCIE_SD_INTRPT_LIST + (index * 8),
  733. (((u64)code << ASIC_PCIE_SD_INTRPT_LIST_INTRPT_CODE_SHIFT) |
  734. ((u64)data << ASIC_PCIE_SD_INTRPT_LIST_INTRPT_DATA_SHIFT)));
  735. }
  736. /*
  737. * Tell the gasket logic how to react to the reset.
  738. */
  739. static void arm_gasket_logic(struct hfi1_devdata *dd)
  740. {
  741. u64 reg;
  742. reg = (((u64)1 << dd->hfi1_id) <<
  743. ASIC_PCIE_SD_HOST_CMD_INTRPT_CMD_SHIFT) |
  744. ((u64)pcie_serdes_broadcast[dd->hfi1_id] <<
  745. ASIC_PCIE_SD_HOST_CMD_SBUS_RCVR_ADDR_SHIFT |
  746. ASIC_PCIE_SD_HOST_CMD_SBR_MODE_SMASK |
  747. ((u64)SBR_DELAY_US & ASIC_PCIE_SD_HOST_CMD_TIMER_MASK) <<
  748. ASIC_PCIE_SD_HOST_CMD_TIMER_SHIFT);
  749. write_csr(dd, ASIC_PCIE_SD_HOST_CMD, reg);
  750. /* read back to push the write */
  751. read_csr(dd, ASIC_PCIE_SD_HOST_CMD);
  752. }
  753. /*
  754. * CCE_PCIE_CTRL long name helpers
  755. * We redefine these shorter macros to use in the code while leaving
  756. * chip_registers.h to be autogenerated from the hardware spec.
  757. */
  758. #define LANE_BUNDLE_MASK CCE_PCIE_CTRL_PCIE_LANE_BUNDLE_MASK
  759. #define LANE_BUNDLE_SHIFT CCE_PCIE_CTRL_PCIE_LANE_BUNDLE_SHIFT
  760. #define LANE_DELAY_MASK CCE_PCIE_CTRL_PCIE_LANE_DELAY_MASK
  761. #define LANE_DELAY_SHIFT CCE_PCIE_CTRL_PCIE_LANE_DELAY_SHIFT
  762. #define MARGIN_OVERWRITE_ENABLE_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_OVERWRITE_ENABLE_SHIFT
  763. #define MARGIN_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_SHIFT
  764. #define MARGIN_G1_G2_OVERWRITE_MASK CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_OVERWRITE_ENABLE_MASK
  765. #define MARGIN_G1_G2_OVERWRITE_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_OVERWRITE_ENABLE_SHIFT
  766. #define MARGIN_GEN1_GEN2_MASK CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_MASK
  767. #define MARGIN_GEN1_GEN2_SHIFT CCE_PCIE_CTRL_XMT_MARGIN_GEN1_GEN2_SHIFT
  768. /*
  769. * Write xmt_margin for full-swing (WFR-B) or half-swing (WFR-C).
  770. */
  771. static void write_xmt_margin(struct hfi1_devdata *dd, const char *fname)
  772. {
  773. u64 pcie_ctrl;
  774. u64 xmt_margin;
  775. u64 xmt_margin_oe;
  776. u64 lane_delay;
  777. u64 lane_bundle;
  778. pcie_ctrl = read_csr(dd, CCE_PCIE_CTRL);
  779. /*
  780. * For Discrete, use full-swing.
  781. * - PCIe TX defaults to full-swing.
  782. * Leave this register as default.
  783. * For Integrated, use half-swing
  784. * - Copy xmt_margin and xmt_margin_oe
  785. * from Gen1/Gen2 to Gen3.
  786. */
  787. if (dd->pcidev->device == PCI_DEVICE_ID_INTEL1) { /* integrated */
  788. /* extract initial fields */
  789. xmt_margin = (pcie_ctrl >> MARGIN_GEN1_GEN2_SHIFT)
  790. & MARGIN_GEN1_GEN2_MASK;
  791. xmt_margin_oe = (pcie_ctrl >> MARGIN_G1_G2_OVERWRITE_SHIFT)
  792. & MARGIN_G1_G2_OVERWRITE_MASK;
  793. lane_delay = (pcie_ctrl >> LANE_DELAY_SHIFT) & LANE_DELAY_MASK;
  794. lane_bundle = (pcie_ctrl >> LANE_BUNDLE_SHIFT)
  795. & LANE_BUNDLE_MASK;
  796. /*
  797. * For A0, EFUSE values are not set. Override with the
  798. * correct values.
  799. */
  800. if (is_ax(dd)) {
  801. /*
  802. * xmt_margin and OverwiteEnabel should be the
  803. * same for Gen1/Gen2 and Gen3
  804. */
  805. xmt_margin = 0x5;
  806. xmt_margin_oe = 0x1;
  807. lane_delay = 0xF; /* Delay 240ns. */
  808. lane_bundle = 0x0; /* Set to 1 lane. */
  809. }
  810. /* overwrite existing values */
  811. pcie_ctrl = (xmt_margin << MARGIN_GEN1_GEN2_SHIFT)
  812. | (xmt_margin_oe << MARGIN_G1_G2_OVERWRITE_SHIFT)
  813. | (xmt_margin << MARGIN_SHIFT)
  814. | (xmt_margin_oe << MARGIN_OVERWRITE_ENABLE_SHIFT)
  815. | (lane_delay << LANE_DELAY_SHIFT)
  816. | (lane_bundle << LANE_BUNDLE_SHIFT);
  817. write_csr(dd, CCE_PCIE_CTRL, pcie_ctrl);
  818. }
  819. dd_dev_dbg(dd, "%s: program XMT margin, CcePcieCtrl 0x%llx\n",
  820. fname, pcie_ctrl);
  821. }
  822. /*
  823. * Do all the steps needed to transition the PCIe link to Gen3 speed.
  824. */
  825. int do_pcie_gen3_transition(struct hfi1_devdata *dd)
  826. {
  827. struct pci_dev *parent = dd->pcidev->bus->self;
  828. u64 fw_ctrl;
  829. u64 reg, therm;
  830. u32 reg32, fs, lf;
  831. u32 status, err;
  832. int ret;
  833. int do_retry, retry_count = 0;
  834. int intnum = 0;
  835. uint default_pset;
  836. u16 target_vector, target_speed;
  837. u16 lnkctl2, vendor;
  838. u8 div;
  839. const u8 (*eq)[3];
  840. const u8 (*ctle_tunings)[4];
  841. uint static_ctle_mode;
  842. int return_error = 0;
  843. /* PCIe Gen3 is for the ASIC only */
  844. if (dd->icode != ICODE_RTL_SILICON)
  845. return 0;
  846. if (pcie_target == 1) { /* target Gen1 */
  847. target_vector = GEN1_SPEED_VECTOR;
  848. target_speed = 2500;
  849. } else if (pcie_target == 2) { /* target Gen2 */
  850. target_vector = GEN2_SPEED_VECTOR;
  851. target_speed = 5000;
  852. } else if (pcie_target == 3) { /* target Gen3 */
  853. target_vector = GEN3_SPEED_VECTOR;
  854. target_speed = 8000;
  855. } else {
  856. /* off or invalid target - skip */
  857. dd_dev_info(dd, "%s: Skipping PCIe transition\n", __func__);
  858. return 0;
  859. }
  860. /* if already at target speed, done (unless forced) */
  861. if (dd->lbus_speed == target_speed) {
  862. dd_dev_info(dd, "%s: PCIe already at gen%d, %s\n", __func__,
  863. pcie_target,
  864. pcie_force ? "re-doing anyway" : "skipping");
  865. if (!pcie_force)
  866. return 0;
  867. }
  868. /*
  869. * The driver cannot do the transition if it has no access to the
  870. * upstream component
  871. */
  872. if (!parent) {
  873. dd_dev_info(dd, "%s: No upstream, Can't do gen3 transition\n",
  874. __func__);
  875. return 0;
  876. }
  877. /*
  878. * Do the Gen3 transition. Steps are those of the PCIe Gen3
  879. * recipe.
  880. */
  881. /* step 1: pcie link working in gen1/gen2 */
  882. /* step 2: if either side is not capable of Gen3, done */
  883. if (pcie_target == 3 && !dd->link_gen3_capable) {
  884. dd_dev_err(dd, "The PCIe link is not Gen3 capable\n");
  885. ret = -ENOSYS;
  886. goto done_no_mutex;
  887. }
  888. /* hold the SBus resource across the firmware download and SBR */
  889. ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
  890. if (ret) {
  891. dd_dev_err(dd, "%s: unable to acquire SBus resource\n",
  892. __func__);
  893. return ret;
  894. }
  895. /* make sure thermal polling is not causing interrupts */
  896. therm = read_csr(dd, ASIC_CFG_THERM_POLL_EN);
  897. if (therm) {
  898. write_csr(dd, ASIC_CFG_THERM_POLL_EN, 0x0);
  899. msleep(100);
  900. dd_dev_info(dd, "%s: Disabled therm polling\n",
  901. __func__);
  902. }
  903. retry:
  904. /* the SBus download will reset the spico for thermal */
  905. /* step 3: download SBus Master firmware */
  906. /* step 4: download PCIe Gen3 SerDes firmware */
  907. dd_dev_info(dd, "%s: downloading firmware\n", __func__);
  908. ret = load_pcie_firmware(dd);
  909. if (ret) {
  910. /* do not proceed if the firmware cannot be downloaded */
  911. return_error = 1;
  912. goto done;
  913. }
  914. /* step 5: set up device parameter settings */
  915. dd_dev_info(dd, "%s: setting PCIe registers\n", __func__);
  916. /*
  917. * PcieCfgSpcie1 - Link Control 3
  918. * Leave at reset value. No need to set PerfEq - link equalization
  919. * will be performed automatically after the SBR when the target
  920. * speed is 8GT/s.
  921. */
  922. /* clear all 16 per-lane error bits (PCIe: Lane Error Status) */
  923. pci_write_config_dword(dd->pcidev, PCIE_CFG_SPCIE2, 0xffff);
  924. /* step 5a: Set Synopsys Port Logic registers */
  925. /*
  926. * PcieCfgRegPl2 - Port Force Link
  927. *
  928. * Set the low power field to 0x10 to avoid unnecessary power
  929. * management messages. All other fields are zero.
  930. */
  931. reg32 = 0x10ul << PCIE_CFG_REG_PL2_LOW_PWR_ENT_CNT_SHIFT;
  932. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL2, reg32);
  933. /*
  934. * PcieCfgRegPl100 - Gen3 Control
  935. *
  936. * turn off PcieCfgRegPl100.Gen3ZRxDcNonCompl
  937. * turn on PcieCfgRegPl100.EqEieosCnt
  938. * Everything else zero.
  939. */
  940. reg32 = PCIE_CFG_REG_PL100_EQ_EIEOS_CNT_SMASK;
  941. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL100, reg32);
  942. /*
  943. * PcieCfgRegPl101 - Gen3 EQ FS and LF
  944. * PcieCfgRegPl102 - Gen3 EQ Presets to Coefficients Mapping
  945. * PcieCfgRegPl103 - Gen3 EQ Preset Index
  946. * PcieCfgRegPl105 - Gen3 EQ Status
  947. *
  948. * Give initial EQ settings.
  949. */
  950. if (dd->pcidev->device == PCI_DEVICE_ID_INTEL0) { /* discrete */
  951. /* 1000mV, FS=24, LF = 8 */
  952. fs = 24;
  953. lf = 8;
  954. div = 3;
  955. eq = discrete_preliminary_eq;
  956. default_pset = DEFAULT_DISCRETE_PSET;
  957. ctle_tunings = discrete_ctle_tunings;
  958. /* bit 0 - discrete on/off */
  959. static_ctle_mode = pcie_ctle & 0x1;
  960. } else {
  961. /* 400mV, FS=29, LF = 9 */
  962. fs = 29;
  963. lf = 9;
  964. div = 1;
  965. eq = integrated_preliminary_eq;
  966. default_pset = DEFAULT_MCP_PSET;
  967. ctle_tunings = integrated_ctle_tunings;
  968. /* bit 1 - integrated on/off */
  969. static_ctle_mode = (pcie_ctle >> 1) & 0x1;
  970. }
  971. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL101,
  972. (fs <<
  973. PCIE_CFG_REG_PL101_GEN3_EQ_LOCAL_FS_SHIFT) |
  974. (lf <<
  975. PCIE_CFG_REG_PL101_GEN3_EQ_LOCAL_LF_SHIFT));
  976. ret = load_eq_table(dd, eq, fs, div);
  977. if (ret)
  978. goto done;
  979. /*
  980. * PcieCfgRegPl106 - Gen3 EQ Control
  981. *
  982. * Set Gen3EqPsetReqVec, leave other fields 0.
  983. */
  984. if (pcie_pset == UNSET_PSET)
  985. pcie_pset = default_pset;
  986. if (pcie_pset > 10) { /* valid range is 0-10, inclusive */
  987. dd_dev_err(dd, "%s: Invalid Eq Pset %u, setting to %d\n",
  988. __func__, pcie_pset, default_pset);
  989. pcie_pset = default_pset;
  990. }
  991. dd_dev_info(dd, "%s: using EQ Pset %u\n", __func__, pcie_pset);
  992. pci_write_config_dword(dd->pcidev, PCIE_CFG_REG_PL106,
  993. ((1 << pcie_pset) <<
  994. PCIE_CFG_REG_PL106_GEN3_EQ_PSET_REQ_VEC_SHIFT) |
  995. PCIE_CFG_REG_PL106_GEN3_EQ_EVAL2MS_DISABLE_SMASK |
  996. PCIE_CFG_REG_PL106_GEN3_EQ_PHASE23_EXIT_MODE_SMASK);
  997. /*
  998. * step 5b: Do post firmware download steps via SBus
  999. */
  1000. dd_dev_info(dd, "%s: doing pcie post steps\n", __func__);
  1001. pcie_post_steps(dd);
  1002. /*
  1003. * step 5c: Program gasket interrupts
  1004. */
  1005. /* set the Rx Bit Rate to REFCLK ratio */
  1006. write_gasket_interrupt(dd, intnum++, 0x0006, 0x0050);
  1007. /* disable pCal for PCIe Gen3 RX equalization */
  1008. /* select adaptive or static CTLE */
  1009. write_gasket_interrupt(dd, intnum++, 0x0026,
  1010. 0x5b01 | (static_ctle_mode << 3));
  1011. /*
  1012. * Enable iCal for PCIe Gen3 RX equalization, and set which
  1013. * evaluation of RX_EQ_EVAL will launch the iCal procedure.
  1014. */
  1015. write_gasket_interrupt(dd, intnum++, 0x0026, 0x5202);
  1016. if (static_ctle_mode) {
  1017. /* apply static CTLE tunings */
  1018. u8 pcie_dc, pcie_lf, pcie_hf, pcie_bw;
  1019. pcie_dc = ctle_tunings[pcie_pset][0];
  1020. pcie_lf = ctle_tunings[pcie_pset][1];
  1021. pcie_hf = ctle_tunings[pcie_pset][2];
  1022. pcie_bw = ctle_tunings[pcie_pset][3];
  1023. write_gasket_interrupt(dd, intnum++, 0x0026, 0x0200 | pcie_dc);
  1024. write_gasket_interrupt(dd, intnum++, 0x0026, 0x0100 | pcie_lf);
  1025. write_gasket_interrupt(dd, intnum++, 0x0026, 0x0000 | pcie_hf);
  1026. write_gasket_interrupt(dd, intnum++, 0x0026, 0x5500 | pcie_bw);
  1027. }
  1028. /* terminate list */
  1029. write_gasket_interrupt(dd, intnum++, 0x0000, 0x0000);
  1030. /*
  1031. * step 5d: program XMT margin
  1032. */
  1033. write_xmt_margin(dd, __func__);
  1034. /*
  1035. * step 5e: disable active state power management (ASPM). It
  1036. * will be enabled if required later
  1037. */
  1038. dd_dev_info(dd, "%s: clearing ASPM\n", __func__);
  1039. aspm_hw_disable_l1(dd);
  1040. /*
  1041. * step 5f: clear DirectSpeedChange
  1042. * PcieCfgRegPl67.DirectSpeedChange must be zero to prevent the
  1043. * change in the speed target from starting before we are ready.
  1044. * This field defaults to 0 and we are not changing it, so nothing
  1045. * needs to be done.
  1046. */
  1047. /* step 5g: Set target link speed */
  1048. /*
  1049. * Set target link speed to be target on both device and parent.
  1050. * On setting the parent: Some system BIOSs "helpfully" set the
  1051. * parent target speed to Gen2 to match the ASIC's initial speed.
  1052. * We can set the target Gen3 because we have already checked
  1053. * that it is Gen3 capable earlier.
  1054. */
  1055. dd_dev_info(dd, "%s: setting parent target link speed\n", __func__);
  1056. pcie_capability_read_word(parent, PCI_EXP_LNKCTL2, &lnkctl2);
  1057. dd_dev_info(dd, "%s: ..old link control2: 0x%x\n", __func__,
  1058. (u32)lnkctl2);
  1059. /* only write to parent if target is not as high as ours */
  1060. if ((lnkctl2 & LNKCTL2_TARGET_LINK_SPEED_MASK) < target_vector) {
  1061. lnkctl2 &= ~LNKCTL2_TARGET_LINK_SPEED_MASK;
  1062. lnkctl2 |= target_vector;
  1063. dd_dev_info(dd, "%s: ..new link control2: 0x%x\n", __func__,
  1064. (u32)lnkctl2);
  1065. pcie_capability_write_word(parent, PCI_EXP_LNKCTL2, lnkctl2);
  1066. } else {
  1067. dd_dev_info(dd, "%s: ..target speed is OK\n", __func__);
  1068. }
  1069. dd_dev_info(dd, "%s: setting target link speed\n", __func__);
  1070. pcie_capability_read_word(dd->pcidev, PCI_EXP_LNKCTL2, &lnkctl2);
  1071. dd_dev_info(dd, "%s: ..old link control2: 0x%x\n", __func__,
  1072. (u32)lnkctl2);
  1073. lnkctl2 &= ~LNKCTL2_TARGET_LINK_SPEED_MASK;
  1074. lnkctl2 |= target_vector;
  1075. dd_dev_info(dd, "%s: ..new link control2: 0x%x\n", __func__,
  1076. (u32)lnkctl2);
  1077. pcie_capability_write_word(dd->pcidev, PCI_EXP_LNKCTL2, lnkctl2);
  1078. /* step 5h: arm gasket logic */
  1079. /* hold DC in reset across the SBR */
  1080. write_csr(dd, CCE_DC_CTRL, CCE_DC_CTRL_DC_RESET_SMASK);
  1081. (void)read_csr(dd, CCE_DC_CTRL); /* DC reset hold */
  1082. /* save firmware control across the SBR */
  1083. fw_ctrl = read_csr(dd, MISC_CFG_FW_CTRL);
  1084. dd_dev_info(dd, "%s: arming gasket logic\n", __func__);
  1085. arm_gasket_logic(dd);
  1086. /*
  1087. * step 6: quiesce PCIe link
  1088. * The chip has already been reset, so there will be no traffic
  1089. * from the chip. Linux has no easy way to enforce that it will
  1090. * not try to access the device, so we just need to hope it doesn't
  1091. * do it while we are doing the reset.
  1092. */
  1093. /*
  1094. * step 7: initiate the secondary bus reset (SBR)
  1095. * step 8: hardware brings the links back up
  1096. * step 9: wait for link speed transition to be complete
  1097. */
  1098. dd_dev_info(dd, "%s: calling trigger_sbr\n", __func__);
  1099. ret = trigger_sbr(dd);
  1100. if (ret)
  1101. goto done;
  1102. /* step 10: decide what to do next */
  1103. /* check if we can read PCI space */
  1104. ret = pci_read_config_word(dd->pcidev, PCI_VENDOR_ID, &vendor);
  1105. if (ret) {
  1106. dd_dev_info(dd,
  1107. "%s: read of VendorID failed after SBR, err %d\n",
  1108. __func__, ret);
  1109. return_error = 1;
  1110. goto done;
  1111. }
  1112. if (vendor == 0xffff) {
  1113. dd_dev_info(dd, "%s: VendorID is all 1s after SBR\n", __func__);
  1114. return_error = 1;
  1115. ret = -EIO;
  1116. goto done;
  1117. }
  1118. /* restore PCI space registers we know were reset */
  1119. dd_dev_info(dd, "%s: calling restore_pci_variables\n", __func__);
  1120. restore_pci_variables(dd);
  1121. /* restore firmware control */
  1122. write_csr(dd, MISC_CFG_FW_CTRL, fw_ctrl);
  1123. /*
  1124. * Check the gasket block status.
  1125. *
  1126. * This is the first CSR read after the SBR. If the read returns
  1127. * all 1s (fails), the link did not make it back.
  1128. *
  1129. * Once we're sure we can read and write, clear the DC reset after
  1130. * the SBR. Then check for any per-lane errors. Then look over
  1131. * the status.
  1132. */
  1133. reg = read_csr(dd, ASIC_PCIE_SD_HOST_STATUS);
  1134. dd_dev_info(dd, "%s: gasket block status: 0x%llx\n", __func__, reg);
  1135. if (reg == ~0ull) { /* PCIe read failed/timeout */
  1136. dd_dev_err(dd, "SBR failed - unable to read from device\n");
  1137. return_error = 1;
  1138. ret = -ENOSYS;
  1139. goto done;
  1140. }
  1141. /* clear the DC reset */
  1142. write_csr(dd, CCE_DC_CTRL, 0);
  1143. /* Set the LED off */
  1144. setextled(dd, 0);
  1145. /* check for any per-lane errors */
  1146. pci_read_config_dword(dd->pcidev, PCIE_CFG_SPCIE2, &reg32);
  1147. dd_dev_info(dd, "%s: per-lane errors: 0x%x\n", __func__, reg32);
  1148. /* extract status, look for our HFI */
  1149. status = (reg >> ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_STS_SHIFT)
  1150. & ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_STS_MASK;
  1151. if ((status & (1 << dd->hfi1_id)) == 0) {
  1152. dd_dev_err(dd,
  1153. "%s: gasket status 0x%x, expecting 0x%x\n",
  1154. __func__, status, 1 << dd->hfi1_id);
  1155. ret = -EIO;
  1156. goto done;
  1157. }
  1158. /* extract error */
  1159. err = (reg >> ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_ERR_SHIFT)
  1160. & ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_ERR_MASK;
  1161. if (err) {
  1162. dd_dev_err(dd, "%s: gasket error %d\n", __func__, err);
  1163. ret = -EIO;
  1164. goto done;
  1165. }
  1166. /* update our link information cache */
  1167. update_lbus_info(dd);
  1168. dd_dev_info(dd, "%s: new speed and width: %s\n", __func__,
  1169. dd->lbus_info);
  1170. if (dd->lbus_speed != target_speed) { /* not target */
  1171. /* maybe retry */
  1172. do_retry = retry_count < pcie_retry;
  1173. dd_dev_err(dd, "PCIe link speed did not switch to Gen%d%s\n",
  1174. pcie_target, do_retry ? ", retrying" : "");
  1175. retry_count++;
  1176. if (do_retry) {
  1177. msleep(100); /* allow time to settle */
  1178. goto retry;
  1179. }
  1180. ret = -EIO;
  1181. }
  1182. done:
  1183. if (therm) {
  1184. write_csr(dd, ASIC_CFG_THERM_POLL_EN, 0x1);
  1185. msleep(100);
  1186. dd_dev_info(dd, "%s: Re-enable therm polling\n",
  1187. __func__);
  1188. }
  1189. release_chip_resource(dd, CR_SBUS);
  1190. done_no_mutex:
  1191. /* return no error if it is OK to be at current speed */
  1192. if (ret && !return_error) {
  1193. dd_dev_err(dd, "Proceeding at current speed PCIe speed\n");
  1194. ret = 0;
  1195. }
  1196. dd_dev_info(dd, "%s: done\n", __func__);
  1197. return ret;
  1198. }