pcie.c 41 KB

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