nfp_net_main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * Copyright (C) 2015-2017 Netronome Systems, Inc.
  3. *
  4. * This software is dual licensed under the GNU General License Version 2,
  5. * June 1991 as shown in the file COPYING in the top-level directory of this
  6. * source tree or the BSD 2-Clause License provided below. You have the
  7. * option to license this software under the complete terms of either license.
  8. *
  9. * The BSD 2-Clause License:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /*
  34. * nfp_net_main.c
  35. * Netronome network device driver: Main entry point
  36. * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
  37. * Alejandro Lucero <alejandro.lucero@netronome.com>
  38. * Jason McMullan <jason.mcmullan@netronome.com>
  39. * Rolf Neugebauer <rolf.neugebauer@netronome.com>
  40. */
  41. #include <linux/etherdevice.h>
  42. #include <linux/kernel.h>
  43. #include <linux/init.h>
  44. #include <linux/pci.h>
  45. #include <linux/pci_regs.h>
  46. #include <linux/msi.h>
  47. #include <linux/random.h>
  48. #include <linux/rtnetlink.h>
  49. #include "nfpcore/nfp.h"
  50. #include "nfpcore/nfp_cpp.h"
  51. #include "nfpcore/nfp_nffw.h"
  52. #include "nfpcore/nfp_nsp.h"
  53. #include "nfpcore/nfp6000_pcie.h"
  54. #include "nfp_net_ctrl.h"
  55. #include "nfp_net.h"
  56. #include "nfp_main.h"
  57. #define NFP_PF_CSR_SLICE_SIZE (32 * 1024)
  58. static int nfp_is_ready(struct nfp_cpp *cpp)
  59. {
  60. const char *cp;
  61. long state;
  62. int err;
  63. cp = nfp_hwinfo_lookup(cpp, "board.state");
  64. if (!cp)
  65. return 0;
  66. err = kstrtol(cp, 0, &state);
  67. if (err < 0)
  68. return 0;
  69. return state == 15;
  70. }
  71. /**
  72. * nfp_net_map_area() - Help function to map an area
  73. * @cpp: NFP CPP handler
  74. * @name: Name for the area
  75. * @target: CPP target
  76. * @addr: CPP address
  77. * @size: Size of the area
  78. * @area: Area handle (returned).
  79. *
  80. * This function is primarily to simplify the code in the main probe
  81. * function. To undo the effect of this functions call
  82. * @nfp_cpp_area_release_free(*area);
  83. *
  84. * Return: Pointer to memory mapped area or ERR_PTR
  85. */
  86. static u8 __iomem *nfp_net_map_area(struct nfp_cpp *cpp,
  87. const char *name, int isl, int target,
  88. unsigned long long addr, unsigned long size,
  89. struct nfp_cpp_area **area)
  90. {
  91. u8 __iomem *res;
  92. u32 dest;
  93. int err;
  94. dest = NFP_CPP_ISLAND_ID(target, NFP_CPP_ACTION_RW, 0, isl);
  95. *area = nfp_cpp_area_alloc_with_name(cpp, dest, name, addr, size);
  96. if (!*area) {
  97. err = -EIO;
  98. goto err_area;
  99. }
  100. err = nfp_cpp_area_acquire(*area);
  101. if (err < 0)
  102. goto err_acquire;
  103. res = nfp_cpp_area_iomem(*area);
  104. if (!res) {
  105. err = -EIO;
  106. goto err_map;
  107. }
  108. return res;
  109. err_map:
  110. nfp_cpp_area_release(*area);
  111. err_acquire:
  112. nfp_cpp_area_free(*area);
  113. err_area:
  114. return (u8 __iomem *)ERR_PTR(err);
  115. }
  116. /**
  117. * nfp_net_get_mac_addr() - Get the MAC address.
  118. * @nn: NFP Network structure
  119. * @cpp: NFP CPP handle
  120. * @id: NFP port id
  121. *
  122. * First try to get the MAC address from NSP ETH table. If that
  123. * fails try HWInfo. As a last resort generate a random address.
  124. */
  125. static void
  126. nfp_net_get_mac_addr(struct nfp_net *nn, struct nfp_cpp *cpp, unsigned int id)
  127. {
  128. struct nfp_net_dp *dp = &nn->dp;
  129. u8 mac_addr[ETH_ALEN];
  130. const char *mac_str;
  131. char name[32];
  132. if (nn->eth_port) {
  133. ether_addr_copy(dp->netdev->dev_addr, nn->eth_port->mac_addr);
  134. ether_addr_copy(dp->netdev->perm_addr, nn->eth_port->mac_addr);
  135. return;
  136. }
  137. snprintf(name, sizeof(name), "eth%d.mac", id);
  138. mac_str = nfp_hwinfo_lookup(cpp, name);
  139. if (!mac_str) {
  140. dev_warn(dp->dev, "Can't lookup MAC address. Generate\n");
  141. eth_hw_addr_random(dp->netdev);
  142. return;
  143. }
  144. if (sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
  145. &mac_addr[0], &mac_addr[1], &mac_addr[2],
  146. &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
  147. dev_warn(dp->dev,
  148. "Can't parse MAC address (%s). Generate.\n", mac_str);
  149. eth_hw_addr_random(dp->netdev);
  150. return;
  151. }
  152. ether_addr_copy(dp->netdev->dev_addr, mac_addr);
  153. ether_addr_copy(dp->netdev->perm_addr, mac_addr);
  154. }
  155. static struct nfp_eth_table_port *
  156. nfp_net_find_port(struct nfp_eth_table *eth_tbl, unsigned int id)
  157. {
  158. int i;
  159. for (i = 0; eth_tbl && i < eth_tbl->count; i++)
  160. if (eth_tbl->ports[i].eth_index == id)
  161. return &eth_tbl->ports[i];
  162. return NULL;
  163. }
  164. static unsigned int nfp_net_pf_get_num_ports(struct nfp_pf *pf)
  165. {
  166. char name[256];
  167. u16 interface;
  168. int pcie_pf;
  169. int err = 0;
  170. u64 val;
  171. interface = nfp_cpp_interface(pf->cpp);
  172. pcie_pf = NFP_CPP_INTERFACE_UNIT_of(interface);
  173. snprintf(name, sizeof(name), "nfd_cfg_pf%d_num_ports", pcie_pf);
  174. val = nfp_rtsym_read_le(pf->cpp, name, &err);
  175. /* Default to one port */
  176. if (err) {
  177. if (err != -ENOENT)
  178. nfp_err(pf->cpp, "Unable to read adapter port count\n");
  179. val = 1;
  180. }
  181. return val;
  182. }
  183. static unsigned int
  184. nfp_net_pf_total_qcs(struct nfp_pf *pf, void __iomem *ctrl_bar,
  185. unsigned int stride, u32 start_off, u32 num_off)
  186. {
  187. unsigned int i, min_qc, max_qc;
  188. min_qc = readl(ctrl_bar + start_off);
  189. max_qc = min_qc;
  190. for (i = 0; i < pf->num_ports; i++) {
  191. /* To make our lives simpler only accept configuration where
  192. * queues are allocated to PFs in order (queues of PFn all have
  193. * indexes lower than PFn+1).
  194. */
  195. if (max_qc > readl(ctrl_bar + start_off))
  196. return 0;
  197. max_qc = readl(ctrl_bar + start_off);
  198. max_qc += readl(ctrl_bar + num_off) * stride;
  199. ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
  200. }
  201. return max_qc - min_qc;
  202. }
  203. static u8 __iomem *nfp_net_pf_map_ctrl_bar(struct nfp_pf *pf)
  204. {
  205. const struct nfp_rtsym *ctrl_sym;
  206. u8 __iomem *ctrl_bar;
  207. char pf_symbol[256];
  208. u16 interface;
  209. int pcie_pf;
  210. interface = nfp_cpp_interface(pf->cpp);
  211. pcie_pf = NFP_CPP_INTERFACE_UNIT_of(interface);
  212. snprintf(pf_symbol, sizeof(pf_symbol), "_pf%d_net_bar0", pcie_pf);
  213. ctrl_sym = nfp_rtsym_lookup(pf->cpp, pf_symbol);
  214. if (!ctrl_sym) {
  215. dev_err(&pf->pdev->dev,
  216. "Failed to find PF BAR0 symbol %s\n", pf_symbol);
  217. return NULL;
  218. }
  219. if (ctrl_sym->size < pf->num_ports * NFP_PF_CSR_SLICE_SIZE) {
  220. dev_err(&pf->pdev->dev,
  221. "PF BAR0 too small to contain %d ports\n",
  222. pf->num_ports);
  223. return NULL;
  224. }
  225. ctrl_bar = nfp_net_map_area(pf->cpp, "net.ctrl",
  226. ctrl_sym->domain, ctrl_sym->target,
  227. ctrl_sym->addr, ctrl_sym->size,
  228. &pf->ctrl_area);
  229. if (IS_ERR(ctrl_bar)) {
  230. dev_err(&pf->pdev->dev, "Failed to map PF BAR0: %ld\n",
  231. PTR_ERR(ctrl_bar));
  232. return NULL;
  233. }
  234. return ctrl_bar;
  235. }
  236. static void nfp_net_pf_free_netdevs(struct nfp_pf *pf)
  237. {
  238. struct nfp_net *nn;
  239. while (!list_empty(&pf->ports)) {
  240. nn = list_first_entry(&pf->ports, struct nfp_net, port_list);
  241. list_del(&nn->port_list);
  242. pf->num_netdevs--;
  243. nfp_net_netdev_free(nn);
  244. }
  245. }
  246. static struct nfp_net *
  247. nfp_net_pf_alloc_port_netdev(struct nfp_pf *pf, void __iomem *ctrl_bar,
  248. void __iomem *tx_bar, void __iomem *rx_bar,
  249. int stride, struct nfp_net_fw_version *fw_ver,
  250. struct nfp_eth_table_port *eth_port)
  251. {
  252. u32 n_tx_rings, n_rx_rings;
  253. struct nfp_net *nn;
  254. n_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
  255. n_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
  256. /* Allocate and initialise the netdev */
  257. nn = nfp_net_netdev_alloc(pf->pdev, n_tx_rings, n_rx_rings);
  258. if (IS_ERR(nn))
  259. return nn;
  260. nn->cpp = pf->cpp;
  261. nn->fw_ver = *fw_ver;
  262. nn->dp.ctrl_bar = ctrl_bar;
  263. nn->tx_bar = tx_bar;
  264. nn->rx_bar = rx_bar;
  265. nn->dp.is_vf = 0;
  266. nn->stride_rx = stride;
  267. nn->stride_tx = stride;
  268. nn->eth_port = eth_port;
  269. return nn;
  270. }
  271. static int
  272. nfp_net_pf_init_port_netdev(struct nfp_pf *pf, struct nfp_net *nn,
  273. unsigned int id)
  274. {
  275. int err;
  276. /* Get MAC address */
  277. nfp_net_get_mac_addr(nn, pf->cpp, id);
  278. /* Get ME clock frequency from ctrl BAR
  279. * XXX for now frequency is hardcoded until we figure out how
  280. * to get the value from nfp-hwinfo into ctrl bar
  281. */
  282. nn->me_freq_mhz = 1200;
  283. err = nfp_net_netdev_init(nn->dp.netdev);
  284. if (err)
  285. return err;
  286. nfp_net_debugfs_port_add(nn, pf->ddir, id);
  287. nfp_net_info(nn);
  288. return 0;
  289. }
  290. static int
  291. nfp_net_pf_alloc_netdevs(struct nfp_pf *pf, void __iomem *ctrl_bar,
  292. void __iomem *tx_bar, void __iomem *rx_bar,
  293. int stride, struct nfp_net_fw_version *fw_ver)
  294. {
  295. u32 prev_tx_base, prev_rx_base, tgt_tx_base, tgt_rx_base;
  296. struct nfp_eth_table_port *eth_port;
  297. struct nfp_net *nn;
  298. unsigned int i;
  299. int err;
  300. prev_tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
  301. prev_rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
  302. for (i = 0; i < pf->num_ports; i++) {
  303. tgt_tx_base = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
  304. tgt_rx_base = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
  305. tx_bar += (tgt_tx_base - prev_tx_base) * NFP_QCP_QUEUE_ADDR_SZ;
  306. rx_bar += (tgt_rx_base - prev_rx_base) * NFP_QCP_QUEUE_ADDR_SZ;
  307. prev_tx_base = tgt_tx_base;
  308. prev_rx_base = tgt_rx_base;
  309. eth_port = nfp_net_find_port(pf->eth_tbl, i);
  310. if (eth_port && eth_port->override_changed) {
  311. nfp_warn(pf->cpp, "Config changed for port #%d, reboot required before port will be operational\n", i);
  312. } else {
  313. nn = nfp_net_pf_alloc_port_netdev(pf, ctrl_bar, tx_bar,
  314. rx_bar, stride,
  315. fw_ver, eth_port);
  316. if (IS_ERR(nn)) {
  317. err = PTR_ERR(nn);
  318. goto err_free_prev;
  319. }
  320. list_add_tail(&nn->port_list, &pf->ports);
  321. pf->num_netdevs++;
  322. }
  323. ctrl_bar += NFP_PF_CSR_SLICE_SIZE;
  324. }
  325. if (list_empty(&pf->ports))
  326. return -ENODEV;
  327. return 0;
  328. err_free_prev:
  329. nfp_net_pf_free_netdevs(pf);
  330. return err;
  331. }
  332. static int
  333. nfp_net_pf_spawn_netdevs(struct nfp_pf *pf,
  334. void __iomem *ctrl_bar, void __iomem *tx_bar,
  335. void __iomem *rx_bar, int stride,
  336. struct nfp_net_fw_version *fw_ver)
  337. {
  338. unsigned int id, wanted_irqs, num_irqs, ports_left, irqs_left;
  339. struct nfp_net *nn;
  340. int err;
  341. /* Allocate the netdevs and do basic init */
  342. err = nfp_net_pf_alloc_netdevs(pf, ctrl_bar, tx_bar, rx_bar,
  343. stride, fw_ver);
  344. if (err)
  345. return err;
  346. /* Get MSI-X vectors */
  347. wanted_irqs = 0;
  348. list_for_each_entry(nn, &pf->ports, port_list)
  349. wanted_irqs += NFP_NET_NON_Q_VECTORS + nn->dp.num_r_vecs;
  350. pf->irq_entries = kcalloc(wanted_irqs, sizeof(*pf->irq_entries),
  351. GFP_KERNEL);
  352. if (!pf->irq_entries) {
  353. err = -ENOMEM;
  354. goto err_nn_free;
  355. }
  356. num_irqs = nfp_net_irqs_alloc(pf->pdev, pf->irq_entries,
  357. NFP_NET_MIN_PORT_IRQS * pf->num_netdevs,
  358. wanted_irqs);
  359. if (!num_irqs) {
  360. nn_warn(nn, "Unable to allocate MSI-X Vectors. Exiting\n");
  361. err = -ENOMEM;
  362. goto err_vec_free;
  363. }
  364. /* Distribute IRQs to ports */
  365. irqs_left = num_irqs;
  366. ports_left = pf->num_netdevs;
  367. list_for_each_entry(nn, &pf->ports, port_list) {
  368. unsigned int n;
  369. n = DIV_ROUND_UP(irqs_left, ports_left);
  370. nfp_net_irqs_assign(nn, &pf->irq_entries[num_irqs - irqs_left],
  371. n);
  372. irqs_left -= n;
  373. ports_left--;
  374. }
  375. /* Finish netdev init and register */
  376. id = 0;
  377. list_for_each_entry(nn, &pf->ports, port_list) {
  378. err = nfp_net_pf_init_port_netdev(pf, nn, id);
  379. if (err)
  380. goto err_prev_deinit;
  381. id++;
  382. }
  383. return 0;
  384. err_prev_deinit:
  385. list_for_each_entry_continue_reverse(nn, &pf->ports, port_list) {
  386. nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
  387. nfp_net_netdev_clean(nn->dp.netdev);
  388. }
  389. nfp_net_irqs_disable(pf->pdev);
  390. err_vec_free:
  391. kfree(pf->irq_entries);
  392. err_nn_free:
  393. nfp_net_pf_free_netdevs(pf);
  394. return err;
  395. }
  396. static void nfp_net_pci_remove_finish(struct nfp_pf *pf)
  397. {
  398. nfp_net_debugfs_dir_clean(&pf->ddir);
  399. nfp_net_irqs_disable(pf->pdev);
  400. kfree(pf->irq_entries);
  401. nfp_cpp_area_release_free(pf->rx_area);
  402. nfp_cpp_area_release_free(pf->tx_area);
  403. nfp_cpp_area_release_free(pf->ctrl_area);
  404. }
  405. static void nfp_net_refresh_netdevs(struct work_struct *work)
  406. {
  407. struct nfp_pf *pf = container_of(work, struct nfp_pf,
  408. port_refresh_work);
  409. struct nfp_eth_table *eth_table;
  410. struct nfp_net *nn, *next;
  411. mutex_lock(&pf->port_lock);
  412. /* Check for nfp_net_pci_remove() racing against us */
  413. if (list_empty(&pf->ports))
  414. goto out;
  415. list_for_each_entry(nn, &pf->ports, port_list)
  416. nfp_net_link_changed_read_clear(nn);
  417. eth_table = nfp_eth_read_ports(pf->cpp);
  418. if (!eth_table) {
  419. nfp_err(pf->cpp, "Error refreshing port config!\n");
  420. goto out;
  421. }
  422. rtnl_lock();
  423. list_for_each_entry(nn, &pf->ports, port_list) {
  424. if (!nn->eth_port)
  425. continue;
  426. nn->eth_port = nfp_net_find_port(eth_table,
  427. nn->eth_port->eth_index);
  428. }
  429. rtnl_unlock();
  430. kfree(pf->eth_tbl);
  431. pf->eth_tbl = eth_table;
  432. list_for_each_entry_safe(nn, next, &pf->ports, port_list) {
  433. if (!nn->eth_port) {
  434. nfp_warn(pf->cpp, "Warning: port not present after reconfig\n");
  435. continue;
  436. }
  437. if (!nn->eth_port->override_changed)
  438. continue;
  439. nn_warn(nn, "Port config changed, unregistering. Reboot required before port will be operational again.\n");
  440. nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
  441. nfp_net_netdev_clean(nn->dp.netdev);
  442. list_del(&nn->port_list);
  443. pf->num_netdevs--;
  444. nfp_net_netdev_free(nn);
  445. }
  446. if (list_empty(&pf->ports))
  447. nfp_net_pci_remove_finish(pf);
  448. out:
  449. mutex_unlock(&pf->port_lock);
  450. }
  451. void nfp_net_refresh_port_table(struct nfp_net *nn)
  452. {
  453. struct nfp_pf *pf = pci_get_drvdata(nn->pdev);
  454. schedule_work(&pf->port_refresh_work);
  455. }
  456. int nfp_net_refresh_eth_port(struct nfp_net *nn)
  457. {
  458. struct nfp_eth_table_port *eth_port;
  459. struct nfp_eth_table *eth_table;
  460. eth_table = nfp_eth_read_ports(nn->cpp);
  461. if (!eth_table) {
  462. nn_err(nn, "Error refreshing port state table!\n");
  463. return -EIO;
  464. }
  465. eth_port = nfp_net_find_port(eth_table, nn->eth_port->eth_index);
  466. if (!eth_port) {
  467. nn_err(nn, "Error finding state of the port!\n");
  468. kfree(eth_table);
  469. return -EIO;
  470. }
  471. memcpy(nn->eth_port, eth_port, sizeof(*eth_port));
  472. kfree(eth_table);
  473. return 0;
  474. }
  475. /*
  476. * PCI device functions
  477. */
  478. int nfp_net_pci_probe(struct nfp_pf *pf)
  479. {
  480. u8 __iomem *ctrl_bar, *tx_bar, *rx_bar;
  481. u32 total_tx_qcs, total_rx_qcs;
  482. struct nfp_net_fw_version fw_ver;
  483. u32 tx_area_sz, rx_area_sz;
  484. u32 start_q;
  485. int stride;
  486. int err;
  487. INIT_WORK(&pf->port_refresh_work, nfp_net_refresh_netdevs);
  488. mutex_init(&pf->port_lock);
  489. /* Verify that the board has completed initialization */
  490. if (!nfp_is_ready(pf->cpp)) {
  491. nfp_err(pf->cpp, "NFP is not ready for NIC operation.\n");
  492. return -EINVAL;
  493. }
  494. mutex_lock(&pf->port_lock);
  495. pf->num_ports = nfp_net_pf_get_num_ports(pf);
  496. ctrl_bar = nfp_net_pf_map_ctrl_bar(pf);
  497. if (!ctrl_bar) {
  498. err = pf->fw_loaded ? -EINVAL : -EPROBE_DEFER;
  499. goto err_unlock;
  500. }
  501. nfp_net_get_fw_version(&fw_ver, ctrl_bar);
  502. if (fw_ver.resv || fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
  503. nfp_err(pf->cpp, "Unknown Firmware ABI %d.%d.%d.%d\n",
  504. fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor);
  505. err = -EINVAL;
  506. goto err_ctrl_unmap;
  507. }
  508. /* Determine stride */
  509. if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
  510. stride = 2;
  511. nfp_warn(pf->cpp, "OBSOLETE Firmware detected - VF isolation not available\n");
  512. } else {
  513. switch (fw_ver.major) {
  514. case 1 ... 4:
  515. stride = 4;
  516. break;
  517. default:
  518. nfp_err(pf->cpp, "Unsupported Firmware ABI %d.%d.%d.%d\n",
  519. fw_ver.resv, fw_ver.class,
  520. fw_ver.major, fw_ver.minor);
  521. err = -EINVAL;
  522. goto err_ctrl_unmap;
  523. }
  524. }
  525. /* Find how many QC structs need to be mapped */
  526. total_tx_qcs = nfp_net_pf_total_qcs(pf, ctrl_bar, stride,
  527. NFP_NET_CFG_START_TXQ,
  528. NFP_NET_CFG_MAX_TXRINGS);
  529. total_rx_qcs = nfp_net_pf_total_qcs(pf, ctrl_bar, stride,
  530. NFP_NET_CFG_START_RXQ,
  531. NFP_NET_CFG_MAX_RXRINGS);
  532. if (!total_tx_qcs || !total_rx_qcs) {
  533. nfp_err(pf->cpp, "Invalid PF QC configuration [%d,%d]\n",
  534. total_tx_qcs, total_rx_qcs);
  535. err = -EINVAL;
  536. goto err_ctrl_unmap;
  537. }
  538. tx_area_sz = NFP_QCP_QUEUE_ADDR_SZ * total_tx_qcs;
  539. rx_area_sz = NFP_QCP_QUEUE_ADDR_SZ * total_rx_qcs;
  540. /* Map TX queues */
  541. start_q = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
  542. tx_bar = nfp_net_map_area(pf->cpp, "net.tx", 0, 0,
  543. NFP_PCIE_QUEUE(start_q),
  544. tx_area_sz, &pf->tx_area);
  545. if (IS_ERR(tx_bar)) {
  546. nfp_err(pf->cpp, "Failed to map TX area.\n");
  547. err = PTR_ERR(tx_bar);
  548. goto err_ctrl_unmap;
  549. }
  550. /* Map RX queues */
  551. start_q = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
  552. rx_bar = nfp_net_map_area(pf->cpp, "net.rx", 0, 0,
  553. NFP_PCIE_QUEUE(start_q),
  554. rx_area_sz, &pf->rx_area);
  555. if (IS_ERR(rx_bar)) {
  556. nfp_err(pf->cpp, "Failed to map RX area.\n");
  557. err = PTR_ERR(rx_bar);
  558. goto err_unmap_tx;
  559. }
  560. pf->ddir = nfp_net_debugfs_device_add(pf->pdev);
  561. err = nfp_net_pf_spawn_netdevs(pf, ctrl_bar, tx_bar, rx_bar,
  562. stride, &fw_ver);
  563. if (err)
  564. goto err_clean_ddir;
  565. mutex_unlock(&pf->port_lock);
  566. return 0;
  567. err_clean_ddir:
  568. nfp_net_debugfs_dir_clean(&pf->ddir);
  569. nfp_cpp_area_release_free(pf->rx_area);
  570. err_unmap_tx:
  571. nfp_cpp_area_release_free(pf->tx_area);
  572. err_ctrl_unmap:
  573. nfp_cpp_area_release_free(pf->ctrl_area);
  574. err_unlock:
  575. mutex_unlock(&pf->port_lock);
  576. return err;
  577. }
  578. void nfp_net_pci_remove(struct nfp_pf *pf)
  579. {
  580. struct nfp_net *nn;
  581. mutex_lock(&pf->port_lock);
  582. if (list_empty(&pf->ports))
  583. goto out;
  584. list_for_each_entry(nn, &pf->ports, port_list) {
  585. nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
  586. nfp_net_netdev_clean(nn->dp.netdev);
  587. }
  588. nfp_net_pf_free_netdevs(pf);
  589. nfp_net_pci_remove_finish(pf);
  590. out:
  591. mutex_unlock(&pf->port_lock);
  592. cancel_work_sync(&pf->port_refresh_work);
  593. }