pcie.c 41 KB

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