pcie.c 41 KB

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