pcie.c 42 KB

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