ef100.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2005-2018 Solarflare Communications Inc.
  5. * Copyright 2019-2022 Xilinx Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation, incorporated herein by reference.
  10. */
  11. #include "net_driver.h"
  12. #include <linux/module.h>
  13. #include <linux/aer.h>
  14. #include "efx_common.h"
  15. #include "efx_channels.h"
  16. #include "io.h"
  17. #include "ef100_nic.h"
  18. #include "ef100_netdev.h"
  19. #include "ef100_sriov.h"
  20. #include "ef100_regs.h"
  21. #include "ef100.h"
  22. #define EFX_EF100_PCI_DEFAULT_BAR 2
  23. /* Number of bytes at start of vendor specified extended capability that indicate
  24. * that the capability is vendor specified. i.e. offset from value returned by
  25. * pci_find_next_ext_capability() to beginning of vendor specified capability
  26. * header.
  27. */
  28. #define PCI_EXT_CAP_HDR_LENGTH 4
  29. /* Expected size of a Xilinx continuation address table entry. */
  30. #define ESE_GZ_CFGBAR_CONT_CAP_MIN_LENGTH 16
  31. struct ef100_func_ctl_window {
  32. bool valid;
  33. unsigned int bar;
  34. u64 offset;
  35. };
  36. static int ef100_pci_walk_xilinx_table(struct efx_nic *efx, u64 offset,
  37. struct ef100_func_ctl_window *result);
  38. /* Number of bytes to offset when reading bit position x with dword accessors. */
  39. #define ROUND_DOWN_TO_DWORD(x) (((x) & (~31)) >> 3)
  40. #define EXTRACT_BITS(x, lbn, width) \
  41. (((x) >> ((lbn) & 31)) & ((1ull << (width)) - 1))
  42. static u32 _ef100_pci_get_bar_bits_with_width(struct efx_nic *efx,
  43. int structure_start,
  44. int lbn, int width)
  45. {
  46. efx_dword_t dword;
  47. efx_readd(efx, &dword, structure_start + ROUND_DOWN_TO_DWORD(lbn));
  48. return EXTRACT_BITS(le32_to_cpu(dword.u32[0]), lbn, width);
  49. }
  50. #define ef100_pci_get_bar_bits(efx, entry_location, bitdef) \
  51. _ef100_pci_get_bar_bits_with_width(efx, entry_location, \
  52. ESF_GZ_CFGBAR_ ## bitdef ## _LBN, \
  53. ESF_GZ_CFGBAR_ ## bitdef ## _WIDTH)
  54. static int ef100_pci_parse_ef100_entry(struct efx_nic *efx, int entry_location,
  55. struct ef100_func_ctl_window *result)
  56. {
  57. u64 offset = ef100_pci_get_bar_bits(efx, entry_location, EF100_FUNC_CTL_WIN_OFF) <<
  58. ESE_GZ_EF100_FUNC_CTL_WIN_OFF_SHIFT;
  59. u32 bar = ef100_pci_get_bar_bits(efx, entry_location, EF100_BAR);
  60. netif_dbg(efx, probe, efx->net_dev,
  61. "Found EF100 function control window bar=%d offset=0x%llx\n",
  62. bar, offset);
  63. if (result->valid) {
  64. netif_err(efx, probe, efx->net_dev,
  65. "Duplicated EF100 table entry.\n");
  66. return -EINVAL;
  67. }
  68. if (bar == ESE_GZ_CFGBAR_EF100_BAR_NUM_EXPANSION_ROM ||
  69. bar == ESE_GZ_CFGBAR_EF100_BAR_NUM_INVALID) {
  70. netif_err(efx, probe, efx->net_dev,
  71. "Bad BAR value of %d in Xilinx capabilities EF100 entry.\n",
  72. bar);
  73. return -EINVAL;
  74. }
  75. result->bar = bar;
  76. result->offset = offset;
  77. result->valid = true;
  78. return 0;
  79. }
  80. static bool ef100_pci_does_bar_overflow(struct efx_nic *efx, int bar,
  81. u64 next_entry)
  82. {
  83. return next_entry + ESE_GZ_CFGBAR_ENTRY_HEADER_SIZE >
  84. pci_resource_len(efx->pci_dev, bar);
  85. }
  86. /* Parse a Xilinx capabilities table entry describing a continuation to a new
  87. * sub-table.
  88. */
  89. static int ef100_pci_parse_continue_entry(struct efx_nic *efx, int entry_location,
  90. struct ef100_func_ctl_window *result)
  91. {
  92. unsigned int previous_bar;
  93. efx_oword_t entry;
  94. u64 offset;
  95. int rc = 0;
  96. u32 bar;
  97. efx_reado(efx, &entry, entry_location);
  98. bar = EFX_OWORD_FIELD32(entry, ESF_GZ_CFGBAR_CONT_CAP_BAR);
  99. offset = EFX_OWORD_FIELD64(entry, ESF_GZ_CFGBAR_CONT_CAP_OFFSET) <<
  100. ESE_GZ_CONT_CAP_OFFSET_BYTES_SHIFT;
  101. previous_bar = efx->mem_bar;
  102. if (bar == ESE_GZ_VSEC_BAR_NUM_EXPANSION_ROM ||
  103. bar == ESE_GZ_VSEC_BAR_NUM_INVALID) {
  104. netif_err(efx, probe, efx->net_dev,
  105. "Bad BAR value of %d in Xilinx capabilities sub-table.\n",
  106. bar);
  107. return -EINVAL;
  108. }
  109. if (bar != previous_bar) {
  110. efx_fini_io(efx);
  111. if (ef100_pci_does_bar_overflow(efx, bar, offset)) {
  112. netif_err(efx, probe, efx->net_dev,
  113. "Xilinx table will overrun BAR[%d] offset=0x%llx\n",
  114. bar, offset);
  115. return -EINVAL;
  116. }
  117. /* Temporarily map new BAR. */
  118. rc = efx_init_io(efx, bar,
  119. (dma_addr_t)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH),
  120. pci_resource_len(efx->pci_dev, bar));
  121. if (rc) {
  122. netif_err(efx, probe, efx->net_dev,
  123. "Mapping new BAR for Xilinx table failed, rc=%d\n", rc);
  124. return rc;
  125. }
  126. }
  127. rc = ef100_pci_walk_xilinx_table(efx, offset, result);
  128. if (rc)
  129. return rc;
  130. if (bar != previous_bar) {
  131. efx_fini_io(efx);
  132. /* Put old BAR back. */
  133. rc = efx_init_io(efx, previous_bar,
  134. (dma_addr_t)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH),
  135. pci_resource_len(efx->pci_dev, previous_bar));
  136. if (rc) {
  137. netif_err(efx, probe, efx->net_dev,
  138. "Putting old BAR back failed, rc=%d\n", rc);
  139. return rc;
  140. }
  141. }
  142. return 0;
  143. }
  144. /* Iterate over the Xilinx capabilities table in the currently mapped BAR and
  145. * call ef100_pci_parse_ef100_entry() on any EF100 entries and
  146. * ef100_pci_parse_continue_entry() on any table continuations.
  147. */
  148. static int ef100_pci_walk_xilinx_table(struct efx_nic *efx, u64 offset,
  149. struct ef100_func_ctl_window *result)
  150. {
  151. u64 current_entry = offset;
  152. int rc = 0;
  153. while (true) {
  154. u32 id = ef100_pci_get_bar_bits(efx, current_entry, ENTRY_FORMAT);
  155. u32 last = ef100_pci_get_bar_bits(efx, current_entry, ENTRY_LAST);
  156. u32 rev = ef100_pci_get_bar_bits(efx, current_entry, ENTRY_REV);
  157. u32 entry_size;
  158. if (id == ESE_GZ_CFGBAR_ENTRY_LAST)
  159. return 0;
  160. entry_size = ef100_pci_get_bar_bits(efx, current_entry, ENTRY_SIZE);
  161. netif_dbg(efx, probe, efx->net_dev,
  162. "Seen Xilinx table entry 0x%x size 0x%x at 0x%llx in BAR[%d]\n",
  163. id, entry_size, current_entry, efx->mem_bar);
  164. if (entry_size < sizeof(u32) * 2) {
  165. netif_err(efx, probe, efx->net_dev,
  166. "Xilinx table entry too short len=0x%x\n", entry_size);
  167. return -EINVAL;
  168. }
  169. switch (id) {
  170. case ESE_GZ_CFGBAR_ENTRY_EF100:
  171. if (rev != ESE_GZ_CFGBAR_ENTRY_REV_EF100 ||
  172. entry_size < ESE_GZ_CFGBAR_ENTRY_SIZE_EF100) {
  173. netif_err(efx, probe, efx->net_dev,
  174. "Bad length or rev for EF100 entry in Xilinx capabilities table. entry_size=%d rev=%d.\n",
  175. entry_size, rev);
  176. return -EINVAL;
  177. }
  178. rc = ef100_pci_parse_ef100_entry(efx, current_entry,
  179. result);
  180. if (rc)
  181. return rc;
  182. break;
  183. case ESE_GZ_CFGBAR_ENTRY_CONT_CAP_ADDR:
  184. if (rev != 0 || entry_size < ESE_GZ_CFGBAR_CONT_CAP_MIN_LENGTH) {
  185. netif_err(efx, probe, efx->net_dev,
  186. "Bad length or rev for continue entry in Xilinx capabilities table. entry_size=%d rev=%d.\n",
  187. entry_size, rev);
  188. return -EINVAL;
  189. }
  190. rc = ef100_pci_parse_continue_entry(efx, current_entry, result);
  191. if (rc)
  192. return rc;
  193. break;
  194. default:
  195. /* Ignore unknown table entries. */
  196. break;
  197. }
  198. if (last)
  199. return 0;
  200. current_entry += entry_size;
  201. if (ef100_pci_does_bar_overflow(efx, efx->mem_bar, current_entry)) {
  202. netif_err(efx, probe, efx->net_dev,
  203. "Xilinx table overrun at position=0x%llx.\n",
  204. current_entry);
  205. return -EINVAL;
  206. }
  207. }
  208. }
  209. static int _ef100_pci_get_config_bits_with_width(struct efx_nic *efx,
  210. int structure_start, int lbn,
  211. int width, u32 *result)
  212. {
  213. int rc, pos = structure_start + ROUND_DOWN_TO_DWORD(lbn);
  214. u32 temp;
  215. rc = pci_read_config_dword(efx->pci_dev, pos, &temp);
  216. if (rc) {
  217. netif_err(efx, probe, efx->net_dev,
  218. "Failed to read PCI config dword at %d\n",
  219. pos);
  220. return rc;
  221. }
  222. *result = EXTRACT_BITS(temp, lbn, width);
  223. return 0;
  224. }
  225. #define ef100_pci_get_config_bits(efx, entry_location, bitdef, result) \
  226. _ef100_pci_get_config_bits_with_width(efx, entry_location, \
  227. ESF_GZ_VSEC_ ## bitdef ## _LBN, \
  228. ESF_GZ_VSEC_ ## bitdef ## _WIDTH, result)
  229. /* Call ef100_pci_walk_xilinx_table() for the Xilinx capabilities table pointed
  230. * to by this PCI_EXT_CAP_ID_VNDR.
  231. */
  232. static int ef100_pci_parse_xilinx_cap(struct efx_nic *efx, int vndr_cap,
  233. bool has_offset_hi,
  234. struct ef100_func_ctl_window *result)
  235. {
  236. u32 offset_high = 0;
  237. u32 offset_lo = 0;
  238. u64 offset = 0;
  239. u32 bar = 0;
  240. int rc = 0;
  241. rc = ef100_pci_get_config_bits(efx, vndr_cap, TBL_BAR, &bar);
  242. if (rc) {
  243. netif_err(efx, probe, efx->net_dev,
  244. "Failed to read ESF_GZ_VSEC_TBL_BAR, rc=%d\n",
  245. rc);
  246. return rc;
  247. }
  248. if (bar == ESE_GZ_CFGBAR_CONT_CAP_BAR_NUM_EXPANSION_ROM ||
  249. bar == ESE_GZ_CFGBAR_CONT_CAP_BAR_NUM_INVALID) {
  250. netif_err(efx, probe, efx->net_dev,
  251. "Bad BAR value of %d in Xilinx capabilities sub-table.\n",
  252. bar);
  253. return -EINVAL;
  254. }
  255. rc = ef100_pci_get_config_bits(efx, vndr_cap, TBL_OFF_LO, &offset_lo);
  256. if (rc) {
  257. netif_err(efx, probe, efx->net_dev,
  258. "Failed to read ESF_GZ_VSEC_TBL_OFF_LO, rc=%d\n",
  259. rc);
  260. return rc;
  261. }
  262. /* Get optional extension to 64bit offset. */
  263. if (has_offset_hi) {
  264. rc = ef100_pci_get_config_bits(efx, vndr_cap, TBL_OFF_HI, &offset_high);
  265. if (rc) {
  266. netif_err(efx, probe, efx->net_dev,
  267. "Failed to read ESF_GZ_VSEC_TBL_OFF_HI, rc=%d\n",
  268. rc);
  269. return rc;
  270. }
  271. }
  272. offset = (((u64)offset_lo) << ESE_GZ_VSEC_TBL_OFF_LO_BYTES_SHIFT) |
  273. (((u64)offset_high) << ESE_GZ_VSEC_TBL_OFF_HI_BYTES_SHIFT);
  274. if (offset > pci_resource_len(efx->pci_dev, bar) - sizeof(u32) * 2) {
  275. netif_err(efx, probe, efx->net_dev,
  276. "Xilinx table will overrun BAR[%d] offset=0x%llx\n",
  277. bar, offset);
  278. return -EINVAL;
  279. }
  280. /* Temporarily map BAR. */
  281. rc = efx_init_io(efx, bar,
  282. (dma_addr_t)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH),
  283. pci_resource_len(efx->pci_dev, bar));
  284. if (rc) {
  285. netif_err(efx, probe, efx->net_dev,
  286. "efx_init_io failed, rc=%d\n", rc);
  287. return rc;
  288. }
  289. rc = ef100_pci_walk_xilinx_table(efx, offset, result);
  290. /* Unmap temporarily mapped BAR. */
  291. efx_fini_io(efx);
  292. return rc;
  293. }
  294. /* Call ef100_pci_parse_ef100_entry() for each Xilinx PCI_EXT_CAP_ID_VNDR
  295. * capability.
  296. */
  297. static int ef100_pci_find_func_ctrl_window(struct efx_nic *efx,
  298. struct ef100_func_ctl_window *result)
  299. {
  300. int num_xilinx_caps = 0;
  301. int cap = 0;
  302. result->valid = false;
  303. while ((cap = pci_find_next_ext_capability(efx->pci_dev, cap, PCI_EXT_CAP_ID_VNDR)) != 0) {
  304. int vndr_cap = cap + PCI_EXT_CAP_HDR_LENGTH;
  305. u32 vsec_ver = 0;
  306. u32 vsec_len = 0;
  307. u32 vsec_id = 0;
  308. int rc = 0;
  309. num_xilinx_caps++;
  310. rc = ef100_pci_get_config_bits(efx, vndr_cap, ID, &vsec_id);
  311. if (rc) {
  312. netif_err(efx, probe, efx->net_dev,
  313. "Failed to read ESF_GZ_VSEC_ID, rc=%d\n",
  314. rc);
  315. return rc;
  316. }
  317. rc = ef100_pci_get_config_bits(efx, vndr_cap, VER, &vsec_ver);
  318. if (rc) {
  319. netif_err(efx, probe, efx->net_dev,
  320. "Failed to read ESF_GZ_VSEC_VER, rc=%d\n",
  321. rc);
  322. return rc;
  323. }
  324. /* Get length of whole capability - i.e. starting at cap */
  325. rc = ef100_pci_get_config_bits(efx, vndr_cap, LEN, &vsec_len);
  326. if (rc) {
  327. netif_err(efx, probe, efx->net_dev,
  328. "Failed to read ESF_GZ_VSEC_LEN, rc=%d\n",
  329. rc);
  330. return rc;
  331. }
  332. if (vsec_id == ESE_GZ_XILINX_VSEC_ID &&
  333. vsec_ver == ESE_GZ_VSEC_VER_XIL_CFGBAR &&
  334. vsec_len >= ESE_GZ_VSEC_LEN_MIN) {
  335. bool has_offset_hi = (vsec_len >= ESE_GZ_VSEC_LEN_HIGH_OFFT);
  336. rc = ef100_pci_parse_xilinx_cap(efx, vndr_cap,
  337. has_offset_hi, result);
  338. if (rc)
  339. return rc;
  340. }
  341. }
  342. if (num_xilinx_caps && !result->valid) {
  343. netif_err(efx, probe, efx->net_dev,
  344. "Seen %d Xilinx tables, but no EF100 entry.\n",
  345. num_xilinx_caps);
  346. return -EINVAL;
  347. }
  348. return 0;
  349. }
  350. /* Final NIC shutdown
  351. * This is called only at module unload (or hotplug removal). A PF can call
  352. * this on its VFs to ensure they are unbound first.
  353. */
  354. static void ef100_pci_remove(struct pci_dev *pci_dev)
  355. {
  356. struct efx_nic *efx = pci_get_drvdata(pci_dev);
  357. struct efx_probe_data *probe_data;
  358. if (!efx)
  359. return;
  360. probe_data = container_of(efx, struct efx_probe_data, efx);
  361. ef100_remove_netdev(probe_data);
  362. #ifdef CONFIG_SFC_SRIOV
  363. efx_fini_struct_tc(efx);
  364. #endif
  365. ef100_remove(efx);
  366. efx_fini_io(efx);
  367. pci_dbg(pci_dev, "shutdown successful\n");
  368. pci_disable_pcie_error_reporting(pci_dev);
  369. pci_set_drvdata(pci_dev, NULL);
  370. efx_fini_struct(efx);
  371. kfree(probe_data);
  372. };
  373. static int ef100_pci_probe(struct pci_dev *pci_dev,
  374. const struct pci_device_id *entry)
  375. {
  376. struct ef100_func_ctl_window fcw = { 0 };
  377. struct efx_probe_data *probe_data;
  378. struct efx_nic *efx;
  379. int rc;
  380. /* Allocate probe data and struct efx_nic */
  381. probe_data = kzalloc(sizeof(*probe_data), GFP_KERNEL);
  382. if (!probe_data)
  383. return -ENOMEM;
  384. probe_data->pci_dev = pci_dev;
  385. efx = &probe_data->efx;
  386. efx->type = (const struct efx_nic_type *)entry->driver_data;
  387. efx->pci_dev = pci_dev;
  388. pci_set_drvdata(pci_dev, efx);
  389. rc = efx_init_struct(efx, pci_dev);
  390. if (rc)
  391. goto fail;
  392. efx->vi_stride = EF100_DEFAULT_VI_STRIDE;
  393. pci_info(pci_dev, "Solarflare EF100 NIC detected\n");
  394. rc = ef100_pci_find_func_ctrl_window(efx, &fcw);
  395. if (rc) {
  396. pci_err(pci_dev,
  397. "Error looking for ef100 function control window, rc=%d\n",
  398. rc);
  399. goto fail;
  400. }
  401. if (!fcw.valid) {
  402. /* Extended capability not found - use defaults. */
  403. fcw.bar = EFX_EF100_PCI_DEFAULT_BAR;
  404. fcw.offset = 0;
  405. fcw.valid = true;
  406. }
  407. if (fcw.offset > pci_resource_len(efx->pci_dev, fcw.bar) - ESE_GZ_FCW_LEN) {
  408. pci_err(pci_dev, "Func control window overruns BAR\n");
  409. rc = -EIO;
  410. goto fail;
  411. }
  412. /* Set up basic I/O (BAR mappings etc) */
  413. rc = efx_init_io(efx, fcw.bar,
  414. (dma_addr_t)DMA_BIT_MASK(ESF_GZ_TX_SEND_ADDR_WIDTH),
  415. pci_resource_len(efx->pci_dev, fcw.bar));
  416. if (rc)
  417. goto fail;
  418. efx->reg_base = fcw.offset;
  419. rc = efx->type->probe(efx);
  420. if (rc)
  421. goto fail;
  422. efx->state = STATE_PROBED;
  423. rc = ef100_probe_netdev(probe_data);
  424. if (rc)
  425. goto fail;
  426. pci_dbg(pci_dev, "initialisation successful\n");
  427. return 0;
  428. fail:
  429. ef100_pci_remove(pci_dev);
  430. return rc;
  431. }
  432. #ifdef CONFIG_SFC_SRIOV
  433. static int ef100_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
  434. {
  435. struct efx_nic *efx = pci_get_drvdata(dev);
  436. int rc;
  437. if (efx->type->sriov_configure) {
  438. rc = efx->type->sriov_configure(efx, num_vfs);
  439. if (rc)
  440. return rc;
  441. else
  442. return num_vfs;
  443. }
  444. return -ENOENT;
  445. }
  446. #endif
  447. /* PCI device ID table */
  448. static const struct pci_device_id ef100_pci_table[] = {
  449. {PCI_DEVICE(PCI_VENDOR_ID_XILINX, 0x0100), /* Riverhead PF */
  450. .driver_data = (unsigned long) &ef100_pf_nic_type },
  451. {PCI_DEVICE(PCI_VENDOR_ID_XILINX, 0x1100), /* Riverhead VF */
  452. .driver_data = (unsigned long) &ef100_vf_nic_type },
  453. {0} /* end of list */
  454. };
  455. struct pci_driver ef100_pci_driver = {
  456. .name = "sfc_ef100",
  457. .id_table = ef100_pci_table,
  458. .probe = ef100_pci_probe,
  459. .remove = ef100_pci_remove,
  460. #ifdef CONFIG_SFC_SRIOV
  461. .sriov_configure = ef100_pci_sriov_configure,
  462. #endif
  463. .err_handler = &efx_err_handlers,
  464. };
  465. MODULE_DEVICE_TABLE(pci, ef100_pci_table);