nitrox_main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. #include <linux/aer.h>
  2. #include <linux/delay.h>
  3. #include <linux/debugfs.h>
  4. #include <linux/firmware.h>
  5. #include <linux/list.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/pci.h>
  9. #include <linux/pci_ids.h>
  10. #include "nitrox_dev.h"
  11. #include "nitrox_common.h"
  12. #include "nitrox_csr.h"
  13. #define CNN55XX_DEV_ID 0x12
  14. #define MAX_PF_QUEUES 64
  15. #define UCODE_HLEN 48
  16. #define SE_GROUP 0
  17. #define DRIVER_VERSION "1.0"
  18. #define FW_DIR "cavium/"
  19. /* SE microcode */
  20. #define SE_FW FW_DIR "cnn55xx_se.fw"
  21. static const char nitrox_driver_name[] = "CNN55XX";
  22. static LIST_HEAD(ndevlist);
  23. static DEFINE_MUTEX(devlist_lock);
  24. static unsigned int num_devices;
  25. /**
  26. * nitrox_pci_tbl - PCI Device ID Table
  27. */
  28. static const struct pci_device_id nitrox_pci_tbl[] = {
  29. {PCI_VDEVICE(CAVIUM, CNN55XX_DEV_ID), 0},
  30. /* required last entry */
  31. {0, }
  32. };
  33. MODULE_DEVICE_TABLE(pci, nitrox_pci_tbl);
  34. static unsigned int qlen = DEFAULT_CMD_QLEN;
  35. module_param(qlen, uint, 0644);
  36. MODULE_PARM_DESC(qlen, "Command queue length - default 2048");
  37. /**
  38. * struct ucode - Firmware Header
  39. * @id: microcode ID
  40. * @version: firmware version
  41. * @code_size: code section size
  42. * @raz: alignment
  43. * @code: code section
  44. */
  45. struct ucode {
  46. u8 id;
  47. char version[VERSION_LEN - 1];
  48. __be32 code_size;
  49. u8 raz[12];
  50. u64 code[0];
  51. };
  52. /**
  53. * write_to_ucd_unit - Write Firmware to NITROX UCD unit
  54. */
  55. static void write_to_ucd_unit(struct nitrox_device *ndev,
  56. struct ucode *ucode)
  57. {
  58. u32 code_size = be32_to_cpu(ucode->code_size) * 2;
  59. u64 offset, data;
  60. int i = 0;
  61. /*
  62. * UCD structure
  63. *
  64. * -------------
  65. * | BLK 7 |
  66. * -------------
  67. * | BLK 6 |
  68. * -------------
  69. * | ... |
  70. * -------------
  71. * | BLK 0 |
  72. * -------------
  73. * Total of 8 blocks, each size 32KB
  74. */
  75. /* set the block number */
  76. offset = UCD_UCODE_LOAD_BLOCK_NUM;
  77. nitrox_write_csr(ndev, offset, 0);
  78. code_size = roundup(code_size, 8);
  79. while (code_size) {
  80. data = ucode->code[i];
  81. /* write 8 bytes at a time */
  82. offset = UCD_UCODE_LOAD_IDX_DATAX(i);
  83. nitrox_write_csr(ndev, offset, data);
  84. code_size -= 8;
  85. i++;
  86. }
  87. /* put all SE cores in group 0 */
  88. offset = POM_GRP_EXECMASKX(SE_GROUP);
  89. nitrox_write_csr(ndev, offset, (~0ULL));
  90. for (i = 0; i < ndev->hw.se_cores; i++) {
  91. /*
  92. * write block number and firware length
  93. * bit:<2:0> block number
  94. * bit:3 is set SE uses 32KB microcode
  95. * bit:3 is clear SE uses 64KB microcode
  96. */
  97. offset = UCD_SE_EID_UCODE_BLOCK_NUMX(i);
  98. nitrox_write_csr(ndev, offset, 0x8);
  99. }
  100. usleep_range(300, 400);
  101. }
  102. static int nitrox_load_fw(struct nitrox_device *ndev, const char *fw_name)
  103. {
  104. const struct firmware *fw;
  105. struct ucode *ucode;
  106. int ret;
  107. dev_info(DEV(ndev), "Loading firmware \"%s\"\n", fw_name);
  108. ret = request_firmware(&fw, fw_name, DEV(ndev));
  109. if (ret < 0) {
  110. dev_err(DEV(ndev), "failed to get firmware %s\n", fw_name);
  111. return ret;
  112. }
  113. ucode = (struct ucode *)fw->data;
  114. /* copy the firmware version */
  115. memcpy(ndev->hw.fw_name, ucode->version, (VERSION_LEN - 2));
  116. ndev->hw.fw_name[VERSION_LEN - 1] = '\0';
  117. write_to_ucd_unit(ndev, ucode);
  118. release_firmware(fw);
  119. set_bit(NITROX_UCODE_LOADED, &ndev->status);
  120. /* barrier to sync with other cpus */
  121. smp_mb__after_atomic();
  122. return 0;
  123. }
  124. /**
  125. * nitrox_add_to_devlist - add NITROX device to global device list
  126. * @ndev: NITROX device
  127. */
  128. static int nitrox_add_to_devlist(struct nitrox_device *ndev)
  129. {
  130. struct nitrox_device *dev;
  131. int ret = 0;
  132. INIT_LIST_HEAD(&ndev->list);
  133. refcount_set(&ndev->refcnt, 1);
  134. mutex_lock(&devlist_lock);
  135. list_for_each_entry(dev, &ndevlist, list) {
  136. if (dev == ndev) {
  137. ret = -EEXIST;
  138. goto unlock;
  139. }
  140. }
  141. ndev->idx = num_devices++;
  142. list_add_tail(&ndev->list, &ndevlist);
  143. unlock:
  144. mutex_unlock(&devlist_lock);
  145. return ret;
  146. }
  147. /**
  148. * nitrox_remove_from_devlist - remove NITROX device from
  149. * global device list
  150. * @ndev: NITROX device
  151. */
  152. static void nitrox_remove_from_devlist(struct nitrox_device *ndev)
  153. {
  154. mutex_lock(&devlist_lock);
  155. list_del(&ndev->list);
  156. num_devices--;
  157. mutex_unlock(&devlist_lock);
  158. }
  159. struct nitrox_device *nitrox_get_first_device(void)
  160. {
  161. struct nitrox_device *ndev;
  162. mutex_lock(&devlist_lock);
  163. list_for_each_entry(ndev, &ndevlist, list) {
  164. if (nitrox_ready(ndev))
  165. break;
  166. }
  167. mutex_unlock(&devlist_lock);
  168. if (&ndev->list == &ndevlist)
  169. return NULL;
  170. refcount_inc(&ndev->refcnt);
  171. /* barrier to sync with other cpus */
  172. smp_mb__after_atomic();
  173. return ndev;
  174. }
  175. void nitrox_put_device(struct nitrox_device *ndev)
  176. {
  177. if (!ndev)
  178. return;
  179. refcount_dec(&ndev->refcnt);
  180. /* barrier to sync with other cpus */
  181. smp_mb__after_atomic();
  182. }
  183. static int nitrox_reset_device(struct pci_dev *pdev)
  184. {
  185. int pos = 0;
  186. pos = pci_save_state(pdev);
  187. if (pos) {
  188. dev_err(&pdev->dev, "Failed to save pci state\n");
  189. return -ENOMEM;
  190. }
  191. pos = pci_pcie_cap(pdev);
  192. if (!pos)
  193. return -ENOTTY;
  194. if (!pci_wait_for_pending_transaction(pdev))
  195. dev_err(&pdev->dev, "waiting for pending transaction\n");
  196. pcie_capability_set_word(pdev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
  197. msleep(100);
  198. pci_restore_state(pdev);
  199. return 0;
  200. }
  201. static int nitrox_pf_sw_init(struct nitrox_device *ndev)
  202. {
  203. int err;
  204. err = nitrox_common_sw_init(ndev);
  205. if (err)
  206. return err;
  207. err = nitrox_pf_init_isr(ndev);
  208. if (err)
  209. nitrox_common_sw_cleanup(ndev);
  210. return err;
  211. }
  212. static void nitrox_pf_sw_cleanup(struct nitrox_device *ndev)
  213. {
  214. nitrox_pf_cleanup_isr(ndev);
  215. nitrox_common_sw_cleanup(ndev);
  216. }
  217. /**
  218. * nitrox_bist_check - Check NITORX BIST registers status
  219. * @ndev: NITROX device
  220. */
  221. static int nitrox_bist_check(struct nitrox_device *ndev)
  222. {
  223. u64 value = 0;
  224. int i;
  225. for (i = 0; i < NR_CLUSTERS; i++) {
  226. value += nitrox_read_csr(ndev, EMU_BIST_STATUSX(i));
  227. value += nitrox_read_csr(ndev, EFL_CORE_BIST_REGX(i));
  228. }
  229. value += nitrox_read_csr(ndev, UCD_BIST_STATUS);
  230. value += nitrox_read_csr(ndev, NPS_CORE_BIST_REG);
  231. value += nitrox_read_csr(ndev, NPS_CORE_NPC_BIST_REG);
  232. value += nitrox_read_csr(ndev, NPS_PKT_SLC_BIST_REG);
  233. value += nitrox_read_csr(ndev, NPS_PKT_IN_BIST_REG);
  234. value += nitrox_read_csr(ndev, POM_BIST_REG);
  235. value += nitrox_read_csr(ndev, BMI_BIST_REG);
  236. value += nitrox_read_csr(ndev, EFL_TOP_BIST_STAT);
  237. value += nitrox_read_csr(ndev, BMO_BIST_REG);
  238. value += nitrox_read_csr(ndev, LBC_BIST_STATUS);
  239. value += nitrox_read_csr(ndev, PEM_BIST_STATUSX(0));
  240. if (value)
  241. return -EIO;
  242. return 0;
  243. }
  244. static void nitrox_get_hwinfo(struct nitrox_device *ndev)
  245. {
  246. union emu_fuse_map emu_fuse;
  247. u64 offset;
  248. int i;
  249. for (i = 0; i < NR_CLUSTERS; i++) {
  250. u8 dead_cores;
  251. offset = EMU_FUSE_MAPX(i);
  252. emu_fuse.value = nitrox_read_csr(ndev, offset);
  253. if (emu_fuse.s.valid) {
  254. dead_cores = hweight32(emu_fuse.s.ae_fuse);
  255. ndev->hw.ae_cores += AE_CORES_PER_CLUSTER - dead_cores;
  256. dead_cores = hweight16(emu_fuse.s.se_fuse);
  257. ndev->hw.se_cores += SE_CORES_PER_CLUSTER - dead_cores;
  258. }
  259. }
  260. }
  261. static int nitrox_pf_hw_init(struct nitrox_device *ndev)
  262. {
  263. int err;
  264. err = nitrox_bist_check(ndev);
  265. if (err) {
  266. dev_err(&ndev->pdev->dev, "BIST check failed\n");
  267. return err;
  268. }
  269. /* get cores information */
  270. nitrox_get_hwinfo(ndev);
  271. nitrox_config_nps_unit(ndev);
  272. nitrox_config_pom_unit(ndev);
  273. nitrox_config_efl_unit(ndev);
  274. /* configure IO units */
  275. nitrox_config_bmi_unit(ndev);
  276. nitrox_config_bmo_unit(ndev);
  277. /* configure Local Buffer Cache */
  278. nitrox_config_lbc_unit(ndev);
  279. nitrox_config_rand_unit(ndev);
  280. /* load firmware on SE cores */
  281. err = nitrox_load_fw(ndev, SE_FW);
  282. if (err)
  283. return err;
  284. nitrox_config_emu_unit(ndev);
  285. return 0;
  286. }
  287. #if IS_ENABLED(CONFIG_DEBUG_FS)
  288. static int registers_show(struct seq_file *s, void *v)
  289. {
  290. struct nitrox_device *ndev = s->private;
  291. u64 offset;
  292. /* NPS DMA stats */
  293. offset = NPS_STATS_PKT_DMA_RD_CNT;
  294. seq_printf(s, "NPS_STATS_PKT_DMA_RD_CNT 0x%016llx\n",
  295. nitrox_read_csr(ndev, offset));
  296. offset = NPS_STATS_PKT_DMA_WR_CNT;
  297. seq_printf(s, "NPS_STATS_PKT_DMA_WR_CNT 0x%016llx\n",
  298. nitrox_read_csr(ndev, offset));
  299. /* BMI/BMO stats */
  300. offset = BMI_NPS_PKT_CNT;
  301. seq_printf(s, "BMI_NPS_PKT_CNT 0x%016llx\n",
  302. nitrox_read_csr(ndev, offset));
  303. offset = BMO_NPS_SLC_PKT_CNT;
  304. seq_printf(s, "BMO_NPS_PKT_CNT 0x%016llx\n",
  305. nitrox_read_csr(ndev, offset));
  306. return 0;
  307. }
  308. static int registers_open(struct inode *inode, struct file *file)
  309. {
  310. return single_open(file, registers_show, inode->i_private);
  311. }
  312. static const struct file_operations register_fops = {
  313. .owner = THIS_MODULE,
  314. .open = registers_open,
  315. .read = seq_read,
  316. .llseek = seq_lseek,
  317. .release = single_release,
  318. };
  319. static int firmware_show(struct seq_file *s, void *v)
  320. {
  321. struct nitrox_device *ndev = s->private;
  322. seq_printf(s, "Version: %s\n", ndev->hw.fw_name);
  323. return 0;
  324. }
  325. static int firmware_open(struct inode *inode, struct file *file)
  326. {
  327. return single_open(file, firmware_show, inode->i_private);
  328. }
  329. static const struct file_operations firmware_fops = {
  330. .owner = THIS_MODULE,
  331. .open = firmware_open,
  332. .read = seq_read,
  333. .llseek = seq_lseek,
  334. .release = single_release,
  335. };
  336. static int nitrox_show(struct seq_file *s, void *v)
  337. {
  338. struct nitrox_device *ndev = s->private;
  339. seq_printf(s, "NITROX-5 [idx: %d]\n", ndev->idx);
  340. seq_printf(s, " Revision ID: 0x%0x\n", ndev->hw.revision_id);
  341. seq_printf(s, " Cores [AE: %u SE: %u]\n",
  342. ndev->hw.ae_cores, ndev->hw.se_cores);
  343. seq_printf(s, " Number of Queues: %u\n", ndev->nr_queues);
  344. seq_printf(s, " Queue length: %u\n", ndev->qlen);
  345. seq_printf(s, " Node: %u\n", ndev->node);
  346. return 0;
  347. }
  348. static int nitrox_open(struct inode *inode, struct file *file)
  349. {
  350. return single_open(file, nitrox_show, inode->i_private);
  351. }
  352. static const struct file_operations nitrox_fops = {
  353. .owner = THIS_MODULE,
  354. .open = nitrox_open,
  355. .read = seq_read,
  356. .llseek = seq_lseek,
  357. .release = single_release,
  358. };
  359. static void nitrox_debugfs_exit(struct nitrox_device *ndev)
  360. {
  361. debugfs_remove_recursive(ndev->debugfs_dir);
  362. ndev->debugfs_dir = NULL;
  363. }
  364. static int nitrox_debugfs_init(struct nitrox_device *ndev)
  365. {
  366. struct dentry *dir, *f;
  367. dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  368. if (!dir)
  369. return -ENOMEM;
  370. ndev->debugfs_dir = dir;
  371. f = debugfs_create_file("counters", 0400, dir, ndev, &register_fops);
  372. if (!f)
  373. goto err;
  374. f = debugfs_create_file("firmware", 0400, dir, ndev, &firmware_fops);
  375. if (!f)
  376. goto err;
  377. f = debugfs_create_file("nitrox", 0400, dir, ndev, &nitrox_fops);
  378. if (!f)
  379. goto err;
  380. return 0;
  381. err:
  382. nitrox_debugfs_exit(ndev);
  383. return -ENODEV;
  384. }
  385. #else
  386. static int nitrox_debugfs_init(struct nitrox_device *ndev)
  387. {
  388. return 0;
  389. }
  390. static void nitrox_debugfs_exit(struct nitrox_device *ndev)
  391. {
  392. }
  393. #endif
  394. /**
  395. * nitrox_probe - NITROX Initialization function.
  396. * @pdev: PCI device information struct
  397. * @id: entry in nitrox_pci_tbl
  398. *
  399. * Return: 0, if the driver is bound to the device, or
  400. * a negative error if there is failure.
  401. */
  402. static int nitrox_probe(struct pci_dev *pdev,
  403. const struct pci_device_id *id)
  404. {
  405. struct nitrox_device *ndev;
  406. int err;
  407. dev_info_once(&pdev->dev, "%s driver version %s\n",
  408. nitrox_driver_name, DRIVER_VERSION);
  409. err = pci_enable_device_mem(pdev);
  410. if (err)
  411. return err;
  412. /* do FLR */
  413. err = nitrox_reset_device(pdev);
  414. if (err) {
  415. dev_err(&pdev->dev, "FLR failed\n");
  416. pci_disable_device(pdev);
  417. return err;
  418. }
  419. if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
  420. dev_dbg(&pdev->dev, "DMA to 64-BIT address\n");
  421. } else {
  422. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  423. if (err) {
  424. dev_err(&pdev->dev, "DMA configuration failed\n");
  425. pci_disable_device(pdev);
  426. return err;
  427. }
  428. }
  429. err = pci_request_mem_regions(pdev, nitrox_driver_name);
  430. if (err) {
  431. pci_disable_device(pdev);
  432. return err;
  433. }
  434. pci_set_master(pdev);
  435. ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
  436. if (!ndev) {
  437. err = -ENOMEM;
  438. goto ndev_fail;
  439. }
  440. pci_set_drvdata(pdev, ndev);
  441. ndev->pdev = pdev;
  442. /* add to device list */
  443. nitrox_add_to_devlist(ndev);
  444. ndev->hw.vendor_id = pdev->vendor;
  445. ndev->hw.device_id = pdev->device;
  446. ndev->hw.revision_id = pdev->revision;
  447. /* command timeout in jiffies */
  448. ndev->timeout = msecs_to_jiffies(CMD_TIMEOUT);
  449. ndev->node = dev_to_node(&pdev->dev);
  450. if (ndev->node == NUMA_NO_NODE)
  451. ndev->node = 0;
  452. ndev->bar_addr = ioremap(pci_resource_start(pdev, 0),
  453. pci_resource_len(pdev, 0));
  454. if (!ndev->bar_addr) {
  455. err = -EIO;
  456. goto ioremap_err;
  457. }
  458. /* allocate command queus based on cpus, max queues are 64 */
  459. ndev->nr_queues = min_t(u32, MAX_PF_QUEUES, num_online_cpus());
  460. ndev->qlen = qlen;
  461. err = nitrox_pf_sw_init(ndev);
  462. if (err)
  463. goto ioremap_err;
  464. err = nitrox_pf_hw_init(ndev);
  465. if (err)
  466. goto pf_hw_fail;
  467. err = nitrox_debugfs_init(ndev);
  468. if (err)
  469. goto pf_hw_fail;
  470. set_bit(NITROX_READY, &ndev->status);
  471. /* barrier to sync with other cpus */
  472. smp_mb__after_atomic();
  473. err = nitrox_crypto_register();
  474. if (err)
  475. goto crypto_fail;
  476. return 0;
  477. crypto_fail:
  478. nitrox_debugfs_exit(ndev);
  479. clear_bit(NITROX_READY, &ndev->status);
  480. /* barrier to sync with other cpus */
  481. smp_mb__after_atomic();
  482. pf_hw_fail:
  483. nitrox_pf_sw_cleanup(ndev);
  484. ioremap_err:
  485. nitrox_remove_from_devlist(ndev);
  486. kfree(ndev);
  487. pci_set_drvdata(pdev, NULL);
  488. ndev_fail:
  489. pci_release_mem_regions(pdev);
  490. pci_disable_device(pdev);
  491. return err;
  492. }
  493. /**
  494. * nitrox_remove - Unbind the driver from the device.
  495. * @pdev: PCI device information struct
  496. */
  497. static void nitrox_remove(struct pci_dev *pdev)
  498. {
  499. struct nitrox_device *ndev = pci_get_drvdata(pdev);
  500. if (!ndev)
  501. return;
  502. if (!refcount_dec_and_test(&ndev->refcnt)) {
  503. dev_err(DEV(ndev), "Device refcnt not zero (%d)\n",
  504. refcount_read(&ndev->refcnt));
  505. return;
  506. }
  507. dev_info(DEV(ndev), "Removing Device %x:%x\n",
  508. ndev->hw.vendor_id, ndev->hw.device_id);
  509. clear_bit(NITROX_READY, &ndev->status);
  510. /* barrier to sync with other cpus */
  511. smp_mb__after_atomic();
  512. nitrox_remove_from_devlist(ndev);
  513. nitrox_crypto_unregister();
  514. nitrox_debugfs_exit(ndev);
  515. nitrox_pf_sw_cleanup(ndev);
  516. iounmap(ndev->bar_addr);
  517. kfree(ndev);
  518. pci_set_drvdata(pdev, NULL);
  519. pci_release_mem_regions(pdev);
  520. pci_disable_device(pdev);
  521. }
  522. static void nitrox_shutdown(struct pci_dev *pdev)
  523. {
  524. pci_set_drvdata(pdev, NULL);
  525. pci_release_mem_regions(pdev);
  526. pci_disable_device(pdev);
  527. }
  528. static struct pci_driver nitrox_driver = {
  529. .name = nitrox_driver_name,
  530. .id_table = nitrox_pci_tbl,
  531. .probe = nitrox_probe,
  532. .remove = nitrox_remove,
  533. .shutdown = nitrox_shutdown,
  534. };
  535. module_pci_driver(nitrox_driver);
  536. MODULE_AUTHOR("Srikanth Jampala <Jampala.Srikanth@cavium.com>");
  537. MODULE_DESCRIPTION("Cavium CNN55XX PF Driver" DRIVER_VERSION " ");
  538. MODULE_LICENSE("GPL");
  539. MODULE_VERSION(DRIVER_VERSION);
  540. MODULE_FIRMWARE(SE_FW);