pcie.c 42 KB

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