1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Admin Function driver
3  *
4  * Copyright (C) 2021 Marvell.
5  *
6  */
7 
8 #include <linux/pci.h>
9 #include "rvu.h"
10 
11 /* SDP PF device id */
12 #define PCI_DEVID_OTX2_SDP_PF   0xA0F6
13 
14 /* Maximum SDP blocks in a chip */
15 #define MAX_SDP		2
16 
17 /* SDP PF number */
18 static int sdp_pf_num[MAX_SDP] = {-1, -1};
19 
20 bool is_sdp_pfvf(u16 pcifunc)
21 {
22 	u16 pf = rvu_get_pf(pcifunc);
23 	u32 found = 0, i = 0;
24 
25 	while (i < MAX_SDP) {
26 		if (pf == sdp_pf_num[i])
27 			found = 1;
28 		i++;
29 	}
30 
31 	if (!found)
32 		return false;
33 
34 	return true;
35 }
36 
37 bool is_sdp_pf(u16 pcifunc)
38 {
39 	return (is_sdp_pfvf(pcifunc) &&
40 		!(pcifunc & RVU_PFVF_FUNC_MASK));
41 }
42 
43 bool is_sdp_vf(u16 pcifunc)
44 {
45 	return (is_sdp_pfvf(pcifunc) &&
46 		!!(pcifunc & RVU_PFVF_FUNC_MASK));
47 }
48 
49 int rvu_sdp_init(struct rvu *rvu)
50 {
51 	struct pci_dev *pdev = NULL;
52 	struct rvu_pfvf *pfvf;
53 	u32 i = 0;
54 
55 	while ((i < MAX_SDP) && (pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
56 						       PCI_DEVID_OTX2_SDP_PF,
57 						       pdev)) != NULL) {
58 		/* The RVU PF number is one less than bus number */
59 		sdp_pf_num[i] = pdev->bus->number - 1;
60 		pfvf = &rvu->pf[sdp_pf_num[i]];
61 
62 		pfvf->sdp_info = devm_kzalloc(rvu->dev,
63 					      sizeof(struct sdp_node_info),
64 					      GFP_KERNEL);
65 		if (!pfvf->sdp_info) {
66 			pci_dev_put(pdev);
67 			return -ENOMEM;
68 		}
69 
70 		dev_info(rvu->dev, "SDP PF number:%d\n", sdp_pf_num[i]);
71 
72 		i++;
73 	}
74 
75 	pci_dev_put(pdev);
76 
77 	return 0;
78 }
79 
80 int
81 rvu_mbox_handler_set_sdp_chan_info(struct rvu *rvu,
82 				   struct sdp_chan_info_msg *req,
83 				   struct msg_rsp *rsp)
84 {
85 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
86 
87 	memcpy(pfvf->sdp_info, &req->info, sizeof(struct sdp_node_info));
88 	dev_info(rvu->dev, "AF: SDP%d max_vfs %d num_pf_rings %d pf_srn %d\n",
89 		 req->info.node_id, req->info.max_vfs, req->info.num_pf_rings,
90 		 req->info.pf_srn);
91 	return 0;
92 }
93 
94 int
95 rvu_mbox_handler_get_sdp_chan_info(struct rvu *rvu, struct msg_req *req,
96 				   struct sdp_get_chan_info_msg *rsp)
97 {
98 	struct rvu_hwinfo *hw = rvu->hw;
99 	int blkaddr;
100 
101 	if (!hw->cap.programmable_chans) {
102 		rsp->chan_base = NIX_CHAN_SDP_CH_START;
103 		rsp->num_chan = NIX_CHAN_SDP_NUM_CHANS;
104 	} else {
105 		blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, 0);
106 		rsp->chan_base = hw->sdp_chan_base;
107 		rsp->num_chan = rvu_read64(rvu, blkaddr, NIX_AF_CONST1) & 0xFFFUL;
108 	}
109 
110 	return 0;
111 }
112