nfp_main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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_main.c
  35. * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
  36. * Alejandro Lucero <alejandro.lucero@netronome.com>
  37. * Jason McMullan <jason.mcmullan@netronome.com>
  38. * Rolf Neugebauer <rolf.neugebauer@netronome.com>
  39. */
  40. #include <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/mutex.h>
  43. #include <linux/pci.h>
  44. #include <linux/firmware.h>
  45. #include <linux/vermagic.h>
  46. #include <linux/vmalloc.h>
  47. #include <net/devlink.h>
  48. #include "nfpcore/nfp.h"
  49. #include "nfpcore/nfp_cpp.h"
  50. #include "nfpcore/nfp_nffw.h"
  51. #include "nfpcore/nfp_nsp.h"
  52. #include "nfpcore/nfp6000_pcie.h"
  53. #include "nfp_abi.h"
  54. #include "nfp_app.h"
  55. #include "nfp_main.h"
  56. #include "nfp_net.h"
  57. static const char nfp_driver_name[] = "nfp";
  58. const char nfp_driver_version[] = VERMAGIC_STRING;
  59. static const struct pci_device_id nfp_pci_device_ids[] = {
  60. { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
  61. PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
  62. PCI_ANY_ID, 0,
  63. },
  64. { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
  65. PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
  66. PCI_ANY_ID, 0,
  67. },
  68. { 0, } /* Required last entry. */
  69. };
  70. MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
  71. int nfp_pf_rtsym_read_optional(struct nfp_pf *pf, const char *format,
  72. unsigned int default_val)
  73. {
  74. char name[256];
  75. int err = 0;
  76. u64 val;
  77. snprintf(name, sizeof(name), format, nfp_cppcore_pcie_unit(pf->cpp));
  78. val = nfp_rtsym_read_le(pf->rtbl, name, &err);
  79. if (err) {
  80. if (err == -ENOENT)
  81. return default_val;
  82. nfp_err(pf->cpp, "Unable to read symbol %s\n", name);
  83. return err;
  84. }
  85. return val;
  86. }
  87. u8 __iomem *
  88. nfp_pf_map_rtsym(struct nfp_pf *pf, const char *name, const char *sym_fmt,
  89. unsigned int min_size, struct nfp_cpp_area **area)
  90. {
  91. char pf_symbol[256];
  92. snprintf(pf_symbol, sizeof(pf_symbol), sym_fmt,
  93. nfp_cppcore_pcie_unit(pf->cpp));
  94. return nfp_rtsym_map(pf->rtbl, pf_symbol, name, min_size, area);
  95. }
  96. /* Callers should hold the devlink instance lock */
  97. int nfp_mbox_cmd(struct nfp_pf *pf, u32 cmd, void *in_data, u64 in_length,
  98. void *out_data, u64 out_length)
  99. {
  100. unsigned long long addr;
  101. unsigned long err_at;
  102. u64 max_data_sz;
  103. u32 val = 0;
  104. u32 cpp_id;
  105. int n, err;
  106. if (!pf->mbox)
  107. return -EOPNOTSUPP;
  108. cpp_id = NFP_CPP_ISLAND_ID(pf->mbox->target, NFP_CPP_ACTION_RW, 0,
  109. pf->mbox->domain);
  110. addr = pf->mbox->addr;
  111. max_data_sz = pf->mbox->size - NFP_MBOX_SYM_MIN_SIZE;
  112. /* Check if cmd field is clear */
  113. err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, &val);
  114. if (err || val) {
  115. nfp_warn(pf->cpp, "failed to issue command (%u): %u, err: %d\n",
  116. cmd, val, err);
  117. return err ?: -EBUSY;
  118. }
  119. in_length = min(in_length, max_data_sz);
  120. n = nfp_cpp_write(pf->cpp, cpp_id, addr + NFP_MBOX_DATA,
  121. in_data, in_length);
  122. if (n != in_length)
  123. return -EIO;
  124. /* Write data_len and wipe reserved */
  125. err = nfp_cpp_writeq(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN,
  126. in_length);
  127. if (err)
  128. return err;
  129. /* Read back for ordering */
  130. err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN, &val);
  131. if (err)
  132. return err;
  133. /* Write cmd and wipe return value */
  134. err = nfp_cpp_writeq(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, cmd);
  135. if (err)
  136. return err;
  137. err_at = jiffies + 5 * HZ;
  138. while (true) {
  139. /* Wait for command to go to 0 (NFP_MBOX_NO_CMD) */
  140. err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_CMD, &val);
  141. if (err)
  142. return err;
  143. if (!val)
  144. break;
  145. if (time_is_before_eq_jiffies(err_at))
  146. return -ETIMEDOUT;
  147. msleep(5);
  148. }
  149. /* Copy output if any (could be error info, do it before reading ret) */
  150. err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_DATA_LEN, &val);
  151. if (err)
  152. return err;
  153. out_length = min_t(u32, val, min(out_length, max_data_sz));
  154. n = nfp_cpp_read(pf->cpp, cpp_id, addr + NFP_MBOX_DATA,
  155. out_data, out_length);
  156. if (n != out_length)
  157. return -EIO;
  158. /* Check if there is an error */
  159. err = nfp_cpp_readl(pf->cpp, cpp_id, addr + NFP_MBOX_RET, &val);
  160. if (err)
  161. return err;
  162. if (val)
  163. return -val;
  164. return out_length;
  165. }
  166. static bool nfp_board_ready(struct nfp_pf *pf)
  167. {
  168. const char *cp;
  169. long state;
  170. int err;
  171. cp = nfp_hwinfo_lookup(pf->hwinfo, "board.state");
  172. if (!cp)
  173. return false;
  174. err = kstrtol(cp, 0, &state);
  175. if (err < 0)
  176. return false;
  177. return state == 15;
  178. }
  179. static int nfp_pf_board_state_wait(struct nfp_pf *pf)
  180. {
  181. const unsigned long wait_until = jiffies + 10 * HZ;
  182. while (!nfp_board_ready(pf)) {
  183. if (time_is_before_eq_jiffies(wait_until)) {
  184. nfp_err(pf->cpp, "NFP board initialization timeout\n");
  185. return -EINVAL;
  186. }
  187. nfp_info(pf->cpp, "waiting for board initialization\n");
  188. if (msleep_interruptible(500))
  189. return -ERESTARTSYS;
  190. /* Refresh cached information */
  191. kfree(pf->hwinfo);
  192. pf->hwinfo = nfp_hwinfo_read(pf->cpp);
  193. }
  194. return 0;
  195. }
  196. static int nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
  197. {
  198. int err;
  199. pf->limit_vfs = nfp_rtsym_read_le(pf->rtbl, "nfd_vf_cfg_max_vfs", &err);
  200. if (err) {
  201. /* For backwards compatibility if symbol not found allow all */
  202. pf->limit_vfs = ~0;
  203. if (err == -ENOENT)
  204. return 0;
  205. nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
  206. return err;
  207. }
  208. err = pci_sriov_set_totalvfs(pf->pdev, pf->limit_vfs);
  209. if (err)
  210. nfp_warn(pf->cpp, "Failed to set VF count in sysfs: %d\n", err);
  211. return 0;
  212. }
  213. static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
  214. {
  215. #ifdef CONFIG_PCI_IOV
  216. struct nfp_pf *pf = pci_get_drvdata(pdev);
  217. int err;
  218. if (num_vfs > pf->limit_vfs) {
  219. nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
  220. pf->limit_vfs);
  221. return -EINVAL;
  222. }
  223. err = pci_enable_sriov(pdev, num_vfs);
  224. if (err) {
  225. dev_warn(&pdev->dev, "Failed to enable PCI SR-IOV: %d\n", err);
  226. return err;
  227. }
  228. mutex_lock(&pf->lock);
  229. err = nfp_app_sriov_enable(pf->app, num_vfs);
  230. if (err) {
  231. dev_warn(&pdev->dev,
  232. "App specific PCI SR-IOV configuration failed: %d\n",
  233. err);
  234. goto err_sriov_disable;
  235. }
  236. pf->num_vfs = num_vfs;
  237. dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
  238. mutex_unlock(&pf->lock);
  239. return num_vfs;
  240. err_sriov_disable:
  241. mutex_unlock(&pf->lock);
  242. pci_disable_sriov(pdev);
  243. return err;
  244. #endif
  245. return 0;
  246. }
  247. static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
  248. {
  249. #ifdef CONFIG_PCI_IOV
  250. struct nfp_pf *pf = pci_get_drvdata(pdev);
  251. mutex_lock(&pf->lock);
  252. /* If the VFs are assigned we cannot shut down SR-IOV without
  253. * causing issues, so just leave the hardware available but
  254. * disabled
  255. */
  256. if (pci_vfs_assigned(pdev)) {
  257. dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
  258. mutex_unlock(&pf->lock);
  259. return -EPERM;
  260. }
  261. nfp_app_sriov_disable(pf->app);
  262. pf->num_vfs = 0;
  263. mutex_unlock(&pf->lock);
  264. pci_disable_sriov(pdev);
  265. dev_dbg(&pdev->dev, "Removed VFs.\n");
  266. #endif
  267. return 0;
  268. }
  269. static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
  270. {
  271. if (num_vfs == 0)
  272. return nfp_pcie_sriov_disable(pdev);
  273. else
  274. return nfp_pcie_sriov_enable(pdev, num_vfs);
  275. }
  276. static const struct firmware *
  277. nfp_net_fw_request(struct pci_dev *pdev, struct nfp_pf *pf, const char *name)
  278. {
  279. const struct firmware *fw = NULL;
  280. int err;
  281. err = request_firmware_direct(&fw, name, &pdev->dev);
  282. nfp_info(pf->cpp, " %s: %s\n",
  283. name, err ? "not found" : "found, loading...");
  284. if (err)
  285. return NULL;
  286. return fw;
  287. }
  288. /**
  289. * nfp_net_fw_find() - Find the correct firmware image for netdev mode
  290. * @pdev: PCI Device structure
  291. * @pf: NFP PF Device structure
  292. *
  293. * Return: firmware if found and requested successfully.
  294. */
  295. static const struct firmware *
  296. nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
  297. {
  298. struct nfp_eth_table_port *port;
  299. const struct firmware *fw;
  300. const char *fw_model;
  301. char fw_name[256];
  302. const u8 *serial;
  303. u16 interface;
  304. int spc, i, j;
  305. nfp_info(pf->cpp, "Looking for firmware file in order of priority:\n");
  306. /* First try to find a firmware image specific for this device */
  307. interface = nfp_cpp_interface(pf->cpp);
  308. nfp_cpp_serial(pf->cpp, &serial);
  309. sprintf(fw_name, "netronome/serial-%pMF-%02hhx-%02hhx.nffw",
  310. serial, interface >> 8, interface & 0xff);
  311. fw = nfp_net_fw_request(pdev, pf, fw_name);
  312. if (fw)
  313. return fw;
  314. /* Then try the PCI name */
  315. sprintf(fw_name, "netronome/pci-%s.nffw", pci_name(pdev));
  316. fw = nfp_net_fw_request(pdev, pf, fw_name);
  317. if (fw)
  318. return fw;
  319. /* Finally try the card type and media */
  320. if (!pf->eth_tbl) {
  321. dev_err(&pdev->dev, "Error: can't identify media config\n");
  322. return NULL;
  323. }
  324. fw_model = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno");
  325. if (!fw_model) {
  326. dev_err(&pdev->dev, "Error: can't read part number\n");
  327. return NULL;
  328. }
  329. spc = ARRAY_SIZE(fw_name);
  330. spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
  331. for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
  332. port = &pf->eth_tbl->ports[i];
  333. j = 1;
  334. while (i + j < pf->eth_tbl->count &&
  335. port->speed == port[j].speed)
  336. j++;
  337. spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
  338. "_%dx%d", j, port->speed / 1000);
  339. }
  340. if (spc <= 0)
  341. return NULL;
  342. spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
  343. if (spc <= 0)
  344. return NULL;
  345. return nfp_net_fw_request(pdev, pf, fw_name);
  346. }
  347. /**
  348. * nfp_net_fw_load() - Load the firmware image
  349. * @pdev: PCI Device structure
  350. * @pf: NFP PF Device structure
  351. * @nsp: NFP SP handle
  352. *
  353. * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
  354. */
  355. static int
  356. nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
  357. {
  358. const struct firmware *fw;
  359. u16 interface;
  360. int err;
  361. interface = nfp_cpp_interface(pf->cpp);
  362. if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
  363. /* Only Unit 0 should reset or load firmware */
  364. dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
  365. return 0;
  366. }
  367. fw = nfp_net_fw_find(pdev, pf);
  368. if (!fw)
  369. return 0;
  370. dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
  371. err = nfp_nsp_device_soft_reset(nsp);
  372. if (err < 0) {
  373. dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
  374. err);
  375. goto exit_release_fw;
  376. }
  377. err = nfp_nsp_load_fw(nsp, fw);
  378. if (err < 0) {
  379. dev_err(&pdev->dev, "FW loading failed: %d\n", err);
  380. goto exit_release_fw;
  381. }
  382. dev_info(&pdev->dev, "Finished loading FW image\n");
  383. exit_release_fw:
  384. release_firmware(fw);
  385. return err < 0 ? err : 1;
  386. }
  387. static void
  388. nfp_nsp_init_ports(struct pci_dev *pdev, struct nfp_pf *pf,
  389. struct nfp_nsp *nsp)
  390. {
  391. bool needs_reinit = false;
  392. int i;
  393. pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
  394. if (!pf->eth_tbl)
  395. return;
  396. if (!nfp_nsp_has_mac_reinit(nsp))
  397. return;
  398. for (i = 0; i < pf->eth_tbl->count; i++)
  399. needs_reinit |= pf->eth_tbl->ports[i].override_changed;
  400. if (!needs_reinit)
  401. return;
  402. kfree(pf->eth_tbl);
  403. if (nfp_nsp_mac_reinit(nsp))
  404. dev_warn(&pdev->dev, "MAC reinit failed\n");
  405. pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
  406. }
  407. static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
  408. {
  409. struct nfp_nsp *nsp;
  410. int err;
  411. err = nfp_resource_wait(pf->cpp, NFP_RESOURCE_NSP, 30);
  412. if (err)
  413. return err;
  414. nsp = nfp_nsp_open(pf->cpp);
  415. if (IS_ERR(nsp)) {
  416. err = PTR_ERR(nsp);
  417. dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
  418. return err;
  419. }
  420. err = nfp_nsp_wait(nsp);
  421. if (err < 0)
  422. goto exit_close_nsp;
  423. nfp_nsp_init_ports(pdev, pf, nsp);
  424. pf->nspi = __nfp_nsp_identify(nsp);
  425. if (pf->nspi)
  426. dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
  427. err = nfp_fw_load(pdev, pf, nsp);
  428. if (err < 0) {
  429. kfree(pf->nspi);
  430. kfree(pf->eth_tbl);
  431. dev_err(&pdev->dev, "Failed to load FW\n");
  432. goto exit_close_nsp;
  433. }
  434. pf->fw_loaded = !!err;
  435. err = 0;
  436. exit_close_nsp:
  437. nfp_nsp_close(nsp);
  438. return err;
  439. }
  440. static void nfp_fw_unload(struct nfp_pf *pf)
  441. {
  442. struct nfp_nsp *nsp;
  443. int err;
  444. nsp = nfp_nsp_open(pf->cpp);
  445. if (IS_ERR(nsp)) {
  446. nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
  447. return;
  448. }
  449. err = nfp_nsp_device_soft_reset(nsp);
  450. if (err < 0)
  451. dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
  452. else
  453. dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
  454. nfp_nsp_close(nsp);
  455. }
  456. static int nfp_pf_find_rtsyms(struct nfp_pf *pf)
  457. {
  458. char pf_symbol[256];
  459. unsigned int pf_id;
  460. pf_id = nfp_cppcore_pcie_unit(pf->cpp);
  461. /* Optional per-PCI PF mailbox */
  462. snprintf(pf_symbol, sizeof(pf_symbol), NFP_MBOX_SYM_NAME, pf_id);
  463. pf->mbox = nfp_rtsym_lookup(pf->rtbl, pf_symbol);
  464. if (pf->mbox && pf->mbox->size < NFP_MBOX_SYM_MIN_SIZE) {
  465. nfp_err(pf->cpp, "PF mailbox symbol too small: %llu < %d\n",
  466. pf->mbox->size, NFP_MBOX_SYM_MIN_SIZE);
  467. return -EINVAL;
  468. }
  469. return 0;
  470. }
  471. static int nfp_pci_probe(struct pci_dev *pdev,
  472. const struct pci_device_id *pci_id)
  473. {
  474. struct devlink *devlink;
  475. struct nfp_pf *pf;
  476. int err;
  477. err = pci_enable_device(pdev);
  478. if (err < 0)
  479. return err;
  480. pci_set_master(pdev);
  481. err = dma_set_mask_and_coherent(&pdev->dev,
  482. DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
  483. if (err)
  484. goto err_pci_disable;
  485. err = pci_request_regions(pdev, nfp_driver_name);
  486. if (err < 0) {
  487. dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
  488. goto err_pci_disable;
  489. }
  490. devlink = devlink_alloc(&nfp_devlink_ops, sizeof(*pf));
  491. if (!devlink) {
  492. err = -ENOMEM;
  493. goto err_rel_regions;
  494. }
  495. pf = devlink_priv(devlink);
  496. INIT_LIST_HEAD(&pf->vnics);
  497. INIT_LIST_HEAD(&pf->ports);
  498. mutex_init(&pf->lock);
  499. pci_set_drvdata(pdev, pf);
  500. pf->pdev = pdev;
  501. pf->wq = alloc_workqueue("nfp-%s", 0, 2, pci_name(pdev));
  502. if (!pf->wq) {
  503. err = -ENOMEM;
  504. goto err_pci_priv_unset;
  505. }
  506. pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
  507. if (IS_ERR_OR_NULL(pf->cpp)) {
  508. err = PTR_ERR(pf->cpp);
  509. if (err >= 0)
  510. err = -ENOMEM;
  511. goto err_disable_msix;
  512. }
  513. err = nfp_resource_table_init(pf->cpp);
  514. if (err)
  515. goto err_cpp_free;
  516. pf->hwinfo = nfp_hwinfo_read(pf->cpp);
  517. dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
  518. nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"),
  519. nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"),
  520. nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"),
  521. nfp_hwinfo_lookup(pf->hwinfo, "assembly.revision"),
  522. nfp_hwinfo_lookup(pf->hwinfo, "cpld.version"));
  523. err = nfp_pf_board_state_wait(pf);
  524. if (err)
  525. goto err_hwinfo_free;
  526. err = nfp_nsp_init(pdev, pf);
  527. if (err)
  528. goto err_hwinfo_free;
  529. pf->mip = nfp_mip_open(pf->cpp);
  530. pf->rtbl = __nfp_rtsym_table_read(pf->cpp, pf->mip);
  531. err = nfp_pf_find_rtsyms(pf);
  532. if (err)
  533. goto err_fw_unload;
  534. pf->dump_flag = NFP_DUMP_NSP_DIAG;
  535. pf->dumpspec = nfp_net_dump_load_dumpspec(pf->cpp, pf->rtbl);
  536. err = nfp_pcie_sriov_read_nfd_limit(pf);
  537. if (err)
  538. goto err_fw_unload;
  539. pf->num_vfs = pci_num_vf(pdev);
  540. if (pf->num_vfs > pf->limit_vfs) {
  541. dev_err(&pdev->dev,
  542. "Error: %d VFs already enabled, but loaded FW can only support %d\n",
  543. pf->num_vfs, pf->limit_vfs);
  544. err = -EINVAL;
  545. goto err_fw_unload;
  546. }
  547. err = nfp_net_pci_probe(pf);
  548. if (err)
  549. goto err_fw_unload;
  550. err = nfp_hwmon_register(pf);
  551. if (err) {
  552. dev_err(&pdev->dev, "Failed to register hwmon info\n");
  553. goto err_net_remove;
  554. }
  555. return 0;
  556. err_net_remove:
  557. nfp_net_pci_remove(pf);
  558. err_fw_unload:
  559. kfree(pf->rtbl);
  560. nfp_mip_close(pf->mip);
  561. if (pf->fw_loaded)
  562. nfp_fw_unload(pf);
  563. kfree(pf->eth_tbl);
  564. kfree(pf->nspi);
  565. vfree(pf->dumpspec);
  566. err_hwinfo_free:
  567. kfree(pf->hwinfo);
  568. err_cpp_free:
  569. nfp_cpp_free(pf->cpp);
  570. err_disable_msix:
  571. destroy_workqueue(pf->wq);
  572. err_pci_priv_unset:
  573. pci_set_drvdata(pdev, NULL);
  574. mutex_destroy(&pf->lock);
  575. devlink_free(devlink);
  576. err_rel_regions:
  577. pci_release_regions(pdev);
  578. err_pci_disable:
  579. pci_disable_device(pdev);
  580. return err;
  581. }
  582. static void nfp_pci_remove(struct pci_dev *pdev)
  583. {
  584. struct nfp_pf *pf = pci_get_drvdata(pdev);
  585. nfp_hwmon_unregister(pf);
  586. nfp_pcie_sriov_disable(pdev);
  587. nfp_net_pci_remove(pf);
  588. vfree(pf->dumpspec);
  589. kfree(pf->rtbl);
  590. nfp_mip_close(pf->mip);
  591. if (pf->fw_loaded)
  592. nfp_fw_unload(pf);
  593. destroy_workqueue(pf->wq);
  594. pci_set_drvdata(pdev, NULL);
  595. kfree(pf->hwinfo);
  596. nfp_cpp_free(pf->cpp);
  597. kfree(pf->eth_tbl);
  598. kfree(pf->nspi);
  599. mutex_destroy(&pf->lock);
  600. devlink_free(priv_to_devlink(pf));
  601. pci_release_regions(pdev);
  602. pci_disable_device(pdev);
  603. }
  604. static struct pci_driver nfp_pci_driver = {
  605. .name = nfp_driver_name,
  606. .id_table = nfp_pci_device_ids,
  607. .probe = nfp_pci_probe,
  608. .remove = nfp_pci_remove,
  609. .sriov_configure = nfp_pcie_sriov_configure,
  610. };
  611. static int __init nfp_main_init(void)
  612. {
  613. int err;
  614. pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
  615. nfp_driver_name);
  616. nfp_net_debugfs_create();
  617. err = pci_register_driver(&nfp_pci_driver);
  618. if (err < 0)
  619. goto err_destroy_debugfs;
  620. err = pci_register_driver(&nfp_netvf_pci_driver);
  621. if (err)
  622. goto err_unreg_pf;
  623. return err;
  624. err_unreg_pf:
  625. pci_unregister_driver(&nfp_pci_driver);
  626. err_destroy_debugfs:
  627. nfp_net_debugfs_destroy();
  628. return err;
  629. }
  630. static void __exit nfp_main_exit(void)
  631. {
  632. pci_unregister_driver(&nfp_netvf_pci_driver);
  633. pci_unregister_driver(&nfp_pci_driver);
  634. nfp_net_debugfs_destroy();
  635. }
  636. module_init(nfp_main_init);
  637. module_exit(nfp_main_exit);
  638. MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
  639. MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
  640. MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
  641. MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
  642. MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
  643. MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
  644. MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
  645. MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
  646. MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_1x10_1x25.nffw");
  647. MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
  648. MODULE_LICENSE("GPL");
  649. MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");
  650. MODULE_VERSION(UTS_RELEASE);