pcie.c 41 KB

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