nfp_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/pci.h>
  43. #include <linux/firmware.h>
  44. #include <linux/vermagic.h>
  45. #include "nfpcore/nfp.h"
  46. #include "nfpcore/nfp_cpp.h"
  47. #include "nfpcore/nfp_nffw.h"
  48. #include "nfpcore/nfp_nsp.h"
  49. #include "nfpcore/nfp6000_pcie.h"
  50. #include "nfp_main.h"
  51. #include "nfp_net.h"
  52. static const char nfp_driver_name[] = "nfp";
  53. const char nfp_driver_version[] = VERMAGIC_STRING;
  54. static const struct pci_device_id nfp_pci_device_ids[] = {
  55. { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000,
  56. PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
  57. PCI_ANY_ID, 0,
  58. },
  59. { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP4000,
  60. PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
  61. PCI_ANY_ID, 0,
  62. },
  63. { 0, } /* Required last entry. */
  64. };
  65. MODULE_DEVICE_TABLE(pci, nfp_pci_device_ids);
  66. static void nfp_pcie_sriov_read_nfd_limit(struct nfp_pf *pf)
  67. {
  68. #ifdef CONFIG_PCI_IOV
  69. int err;
  70. pf->limit_vfs = nfp_rtsym_read_le(pf->cpp, "nfd_vf_cfg_max_vfs", &err);
  71. if (!err)
  72. return;
  73. pf->limit_vfs = ~0;
  74. /* Allow any setting for backwards compatibility if symbol not found */
  75. if (err != -ENOENT)
  76. nfp_warn(pf->cpp, "Warning: VF limit read failed: %d\n", err);
  77. #endif
  78. }
  79. static int nfp_pcie_sriov_enable(struct pci_dev *pdev, int num_vfs)
  80. {
  81. #ifdef CONFIG_PCI_IOV
  82. struct nfp_pf *pf = pci_get_drvdata(pdev);
  83. int err;
  84. if (num_vfs > pf->limit_vfs) {
  85. nfp_info(pf->cpp, "Firmware limits number of VFs to %u\n",
  86. pf->limit_vfs);
  87. return -EINVAL;
  88. }
  89. err = pci_enable_sriov(pdev, num_vfs);
  90. if (err) {
  91. dev_warn(&pdev->dev, "Failed to enable PCI sriov: %d\n", err);
  92. return err;
  93. }
  94. pf->num_vfs = num_vfs;
  95. dev_dbg(&pdev->dev, "Created %d VFs.\n", pf->num_vfs);
  96. return num_vfs;
  97. #endif
  98. return 0;
  99. }
  100. static int nfp_pcie_sriov_disable(struct pci_dev *pdev)
  101. {
  102. #ifdef CONFIG_PCI_IOV
  103. struct nfp_pf *pf = pci_get_drvdata(pdev);
  104. /* If the VFs are assigned we cannot shut down SR-IOV without
  105. * causing issues, so just leave the hardware available but
  106. * disabled
  107. */
  108. if (pci_vfs_assigned(pdev)) {
  109. dev_warn(&pdev->dev, "Disabling while VFs assigned - VFs will not be deallocated\n");
  110. return -EPERM;
  111. }
  112. pf->num_vfs = 0;
  113. pci_disable_sriov(pdev);
  114. dev_dbg(&pdev->dev, "Removed VFs.\n");
  115. #endif
  116. return 0;
  117. }
  118. static int nfp_pcie_sriov_configure(struct pci_dev *pdev, int num_vfs)
  119. {
  120. if (num_vfs == 0)
  121. return nfp_pcie_sriov_disable(pdev);
  122. else
  123. return nfp_pcie_sriov_enable(pdev, num_vfs);
  124. }
  125. /**
  126. * nfp_net_fw_find() - Find the correct firmware image for netdev mode
  127. * @pdev: PCI Device structure
  128. * @pf: NFP PF Device structure
  129. *
  130. * Return: firmware if found and requested successfully.
  131. */
  132. static const struct firmware *
  133. nfp_net_fw_find(struct pci_dev *pdev, struct nfp_pf *pf)
  134. {
  135. const struct firmware *fw = NULL;
  136. struct nfp_eth_table_port *port;
  137. const char *fw_model;
  138. char fw_name[256];
  139. int spc, err = 0;
  140. int i, j;
  141. if (!pf->eth_tbl) {
  142. dev_err(&pdev->dev, "Error: can't identify media config\n");
  143. return NULL;
  144. }
  145. fw_model = nfp_hwinfo_lookup(pf->cpp, "assembly.partno");
  146. if (!fw_model) {
  147. dev_err(&pdev->dev, "Error: can't read part number\n");
  148. return NULL;
  149. }
  150. spc = ARRAY_SIZE(fw_name);
  151. spc -= snprintf(fw_name, spc, "netronome/nic_%s", fw_model);
  152. for (i = 0; spc > 0 && i < pf->eth_tbl->count; i += j) {
  153. port = &pf->eth_tbl->ports[i];
  154. j = 1;
  155. while (i + j < pf->eth_tbl->count &&
  156. port->speed == port[j].speed)
  157. j++;
  158. spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc,
  159. "_%dx%d", j, port->speed / 1000);
  160. }
  161. if (spc <= 0)
  162. return NULL;
  163. spc -= snprintf(&fw_name[ARRAY_SIZE(fw_name) - spc], spc, ".nffw");
  164. if (spc <= 0)
  165. return NULL;
  166. err = request_firmware(&fw, fw_name, &pdev->dev);
  167. if (err)
  168. return NULL;
  169. dev_info(&pdev->dev, "Loading FW image: %s\n", fw_name);
  170. return fw;
  171. }
  172. /**
  173. * nfp_net_fw_load() - Load the firmware image
  174. * @pdev: PCI Device structure
  175. * @pf: NFP PF Device structure
  176. * @nsp: NFP SP handle
  177. *
  178. * Return: -ERRNO, 0 for no firmware loaded, 1 for firmware loaded
  179. */
  180. static int
  181. nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
  182. {
  183. const struct firmware *fw;
  184. u16 interface;
  185. int err;
  186. interface = nfp_cpp_interface(pf->cpp);
  187. if (NFP_CPP_INTERFACE_UNIT_of(interface) != 0) {
  188. /* Only Unit 0 should reset or load firmware */
  189. dev_info(&pdev->dev, "Firmware will be loaded by partner\n");
  190. return 0;
  191. }
  192. fw = nfp_net_fw_find(pdev, pf);
  193. if (!fw)
  194. return 0;
  195. dev_info(&pdev->dev, "Soft-reset, loading FW image\n");
  196. err = nfp_nsp_device_soft_reset(nsp);
  197. if (err < 0) {
  198. dev_err(&pdev->dev, "Failed to soft reset the NFP: %d\n",
  199. err);
  200. goto exit_release_fw;
  201. }
  202. err = nfp_nsp_load_fw(nsp, fw);
  203. if (err < 0) {
  204. dev_err(&pdev->dev, "FW loading failed: %d\n", err);
  205. goto exit_release_fw;
  206. }
  207. dev_info(&pdev->dev, "Finished loading FW image\n");
  208. exit_release_fw:
  209. release_firmware(fw);
  210. return err < 0 ? err : 1;
  211. }
  212. static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
  213. {
  214. struct nfp_nsp_identify *nspi;
  215. struct nfp_nsp *nsp;
  216. int err;
  217. nsp = nfp_nsp_open(pf->cpp);
  218. if (IS_ERR(nsp)) {
  219. err = PTR_ERR(nsp);
  220. dev_err(&pdev->dev, "Failed to access the NSP: %d\n", err);
  221. return err;
  222. }
  223. err = nfp_nsp_wait(nsp);
  224. if (err < 0)
  225. goto exit_close_nsp;
  226. pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
  227. nspi = __nfp_nsp_identify(nsp);
  228. if (nspi) {
  229. dev_info(&pdev->dev, "BSP: %s\n", nspi->version);
  230. kfree(nspi);
  231. }
  232. err = nfp_fw_load(pdev, pf, nsp);
  233. if (err < 0) {
  234. kfree(pf->eth_tbl);
  235. dev_err(&pdev->dev, "Failed to load FW\n");
  236. goto exit_close_nsp;
  237. }
  238. pf->fw_loaded = !!err;
  239. err = 0;
  240. exit_close_nsp:
  241. nfp_nsp_close(nsp);
  242. return err;
  243. }
  244. static void nfp_fw_unload(struct nfp_pf *pf)
  245. {
  246. struct nfp_nsp *nsp;
  247. int err;
  248. nsp = nfp_nsp_open(pf->cpp);
  249. if (IS_ERR(nsp)) {
  250. nfp_err(pf->cpp, "Reset failed, can't open NSP\n");
  251. return;
  252. }
  253. err = nfp_nsp_device_soft_reset(nsp);
  254. if (err < 0)
  255. dev_warn(&pf->pdev->dev, "Couldn't unload firmware: %d\n", err);
  256. else
  257. dev_info(&pf->pdev->dev, "Firmware safely unloaded\n");
  258. nfp_nsp_close(nsp);
  259. }
  260. static int nfp_pci_probe(struct pci_dev *pdev,
  261. const struct pci_device_id *pci_id)
  262. {
  263. struct nfp_pf *pf;
  264. int err;
  265. err = pci_enable_device(pdev);
  266. if (err < 0)
  267. return err;
  268. pci_set_master(pdev);
  269. err = dma_set_mask_and_coherent(&pdev->dev,
  270. DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
  271. if (err)
  272. goto err_pci_disable;
  273. err = pci_request_regions(pdev, nfp_driver_name);
  274. if (err < 0) {
  275. dev_err(&pdev->dev, "Unable to reserve pci resources.\n");
  276. goto err_pci_disable;
  277. }
  278. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  279. if (!pf) {
  280. err = -ENOMEM;
  281. goto err_rel_regions;
  282. }
  283. INIT_LIST_HEAD(&pf->ports);
  284. pci_set_drvdata(pdev, pf);
  285. pf->pdev = pdev;
  286. pf->cpp = nfp_cpp_from_nfp6000_pcie(pdev);
  287. if (IS_ERR_OR_NULL(pf->cpp)) {
  288. err = PTR_ERR(pf->cpp);
  289. if (err >= 0)
  290. err = -ENOMEM;
  291. goto err_disable_msix;
  292. }
  293. dev_info(&pdev->dev, "Assembly: %s%s%s-%s CPLD: %s\n",
  294. nfp_hwinfo_lookup(pf->cpp, "assembly.vendor"),
  295. nfp_hwinfo_lookup(pf->cpp, "assembly.partno"),
  296. nfp_hwinfo_lookup(pf->cpp, "assembly.serial"),
  297. nfp_hwinfo_lookup(pf->cpp, "assembly.revision"),
  298. nfp_hwinfo_lookup(pf->cpp, "cpld.version"));
  299. err = nfp_nsp_init(pdev, pf);
  300. if (err)
  301. goto err_cpp_free;
  302. nfp_pcie_sriov_read_nfd_limit(pf);
  303. err = nfp_net_pci_probe(pf);
  304. if (err)
  305. goto err_fw_unload;
  306. return 0;
  307. err_fw_unload:
  308. if (pf->fw_loaded)
  309. nfp_fw_unload(pf);
  310. kfree(pf->eth_tbl);
  311. err_cpp_free:
  312. nfp_cpp_free(pf->cpp);
  313. err_disable_msix:
  314. pci_set_drvdata(pdev, NULL);
  315. kfree(pf);
  316. err_rel_regions:
  317. pci_release_regions(pdev);
  318. err_pci_disable:
  319. pci_disable_device(pdev);
  320. return err;
  321. }
  322. static void nfp_pci_remove(struct pci_dev *pdev)
  323. {
  324. struct nfp_pf *pf = pci_get_drvdata(pdev);
  325. nfp_net_pci_remove(pf);
  326. nfp_pcie_sriov_disable(pdev);
  327. if (pf->fw_loaded)
  328. nfp_fw_unload(pf);
  329. pci_set_drvdata(pdev, NULL);
  330. nfp_cpp_free(pf->cpp);
  331. kfree(pf->eth_tbl);
  332. kfree(pf);
  333. pci_release_regions(pdev);
  334. pci_disable_device(pdev);
  335. }
  336. static struct pci_driver nfp_pci_driver = {
  337. .name = nfp_driver_name,
  338. .id_table = nfp_pci_device_ids,
  339. .probe = nfp_pci_probe,
  340. .remove = nfp_pci_remove,
  341. .sriov_configure = nfp_pcie_sriov_configure,
  342. };
  343. static int __init nfp_main_init(void)
  344. {
  345. int err;
  346. pr_info("%s: NFP PCIe Driver, Copyright (C) 2014-2017 Netronome Systems\n",
  347. nfp_driver_name);
  348. nfp_net_debugfs_create();
  349. err = pci_register_driver(&nfp_pci_driver);
  350. if (err < 0)
  351. goto err_destroy_debugfs;
  352. err = pci_register_driver(&nfp_netvf_pci_driver);
  353. if (err)
  354. goto err_unreg_pf;
  355. return err;
  356. err_unreg_pf:
  357. pci_unregister_driver(&nfp_pci_driver);
  358. err_destroy_debugfs:
  359. nfp_net_debugfs_destroy();
  360. return err;
  361. }
  362. static void __exit nfp_main_exit(void)
  363. {
  364. pci_unregister_driver(&nfp_netvf_pci_driver);
  365. pci_unregister_driver(&nfp_pci_driver);
  366. nfp_net_debugfs_destroy();
  367. }
  368. module_init(nfp_main_init);
  369. module_exit(nfp_main_exit);
  370. MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_1x40.nffw");
  371. MODULE_FIRMWARE("netronome/nic_AMDA0081-0001_4x10.nffw");
  372. MODULE_FIRMWARE("netronome/nic_AMDA0096-0001_2x10.nffw");
  373. MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_2x40.nffw");
  374. MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_4x10_1x40.nffw");
  375. MODULE_FIRMWARE("netronome/nic_AMDA0097-0001_8x10.nffw");
  376. MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x10.nffw");
  377. MODULE_FIRMWARE("netronome/nic_AMDA0099-0001_2x25.nffw");
  378. MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
  379. MODULE_LICENSE("GPL");
  380. MODULE_DESCRIPTION("The Netronome Flow Processor (NFP) driver.");