ef100.c 14 KB

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