1be1f1ffaSAriel Elior /* bnx2x_vfpf.c: Broadcom Everest network driver.
2be1f1ffaSAriel Elior  *
3be1f1ffaSAriel Elior  * Copyright 2009-2012 Broadcom Corporation
4be1f1ffaSAriel Elior  *
5be1f1ffaSAriel Elior  * Unless you and Broadcom execute a separate written software license
6be1f1ffaSAriel Elior  * agreement governing use of this software, this software is licensed to you
7be1f1ffaSAriel Elior  * under the terms of the GNU General Public License version 2, available
8be1f1ffaSAriel Elior  * at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html (the "GPL").
9be1f1ffaSAriel Elior  *
10be1f1ffaSAriel Elior  * Notwithstanding the above, under no circumstances may you combine this
11be1f1ffaSAriel Elior  * software in any way with any other Broadcom software provided under a
12be1f1ffaSAriel Elior  * license other than the GPL, without Broadcom's express prior written
13be1f1ffaSAriel Elior  * consent.
14be1f1ffaSAriel Elior  *
15be1f1ffaSAriel Elior  * Maintained by: Eilon Greenstein <eilong@broadcom.com>
16be1f1ffaSAriel Elior  * Written by: Shmulik Ravid <shmulikr@broadcom.com>
17be1f1ffaSAriel Elior  *	       Ariel Elior <ariele@broadcom.com>
18be1f1ffaSAriel Elior  */
19be1f1ffaSAriel Elior 
20be1f1ffaSAriel Elior #include "bnx2x.h"
21be1f1ffaSAriel Elior #include "bnx2x_sriov.h"
22be1f1ffaSAriel Elior 
23be1f1ffaSAriel Elior /* place a given tlv on the tlv buffer at a given offset */
24be1f1ffaSAriel Elior void bnx2x_add_tlv(struct bnx2x *bp, void *tlvs_list, u16 offset, u16 type,
25be1f1ffaSAriel Elior 		   u16 length)
26be1f1ffaSAriel Elior {
27be1f1ffaSAriel Elior 	struct channel_tlv *tl =
28be1f1ffaSAriel Elior 		(struct channel_tlv *)(tlvs_list + offset);
29be1f1ffaSAriel Elior 
30be1f1ffaSAriel Elior 	tl->type = type;
31be1f1ffaSAriel Elior 	tl->length = length;
32be1f1ffaSAriel Elior }
33be1f1ffaSAriel Elior 
34be1f1ffaSAriel Elior /* Clear the mailbox and init the header of the first tlv */
35be1f1ffaSAriel Elior void bnx2x_vfpf_prep(struct bnx2x *bp, struct vfpf_first_tlv *first_tlv,
36be1f1ffaSAriel Elior 		     u16 type, u16 length)
37be1f1ffaSAriel Elior {
38be1f1ffaSAriel Elior 	DP(BNX2X_MSG_IOV, "preparing to send %d tlv over vf pf channel\n",
39be1f1ffaSAriel Elior 	   type);
40be1f1ffaSAriel Elior 
41be1f1ffaSAriel Elior 	/* Clear mailbox */
42be1f1ffaSAriel Elior 	memset(bp->vf2pf_mbox, 0, sizeof(struct bnx2x_vf_mbx_msg));
43be1f1ffaSAriel Elior 
44be1f1ffaSAriel Elior 	/* init type and length */
45be1f1ffaSAriel Elior 	bnx2x_add_tlv(bp, &first_tlv->tl, 0, type, length);
46be1f1ffaSAriel Elior 
47be1f1ffaSAriel Elior 	/* init first tlv header */
48be1f1ffaSAriel Elior 	first_tlv->resp_msg_offset = sizeof(bp->vf2pf_mbox->req);
49be1f1ffaSAriel Elior }
50be1f1ffaSAriel Elior 
51be1f1ffaSAriel Elior /* list the types and lengths of the tlvs on the buffer */
52be1f1ffaSAriel Elior void bnx2x_dp_tlv_list(struct bnx2x *bp, void *tlvs_list)
53be1f1ffaSAriel Elior {
54be1f1ffaSAriel Elior 	int i = 1;
55be1f1ffaSAriel Elior 	struct channel_tlv *tlv = (struct channel_tlv *)tlvs_list;
56be1f1ffaSAriel Elior 
57be1f1ffaSAriel Elior 	while (tlv->type != CHANNEL_TLV_LIST_END) {
58be1f1ffaSAriel Elior 		/* output tlv */
59be1f1ffaSAriel Elior 		DP(BNX2X_MSG_IOV, "TLV number %d: type %d, length %d\n", i,
60be1f1ffaSAriel Elior 		   tlv->type, tlv->length);
61be1f1ffaSAriel Elior 
62be1f1ffaSAriel Elior 		/* advance to next tlv */
63be1f1ffaSAriel Elior 		tlvs_list += tlv->length;
64be1f1ffaSAriel Elior 
65be1f1ffaSAriel Elior 		/* cast general tlv list pointer to channel tlv header*/
66be1f1ffaSAriel Elior 		tlv = (struct channel_tlv *)tlvs_list;
67be1f1ffaSAriel Elior 
68be1f1ffaSAriel Elior 		i++;
69be1f1ffaSAriel Elior 
70be1f1ffaSAriel Elior 		/* break condition for this loop */
71be1f1ffaSAriel Elior 		if (i > MAX_TLVS_IN_LIST) {
72be1f1ffaSAriel Elior 			WARN(true, "corrupt tlvs");
73be1f1ffaSAriel Elior 			return;
74be1f1ffaSAriel Elior 		}
75be1f1ffaSAriel Elior 	}
76be1f1ffaSAriel Elior 
77be1f1ffaSAriel Elior 	/* output last tlv */
78be1f1ffaSAriel Elior 	DP(BNX2X_MSG_IOV, "TLV number %d: type %d, length %d\n", i,
79be1f1ffaSAriel Elior 	   tlv->type, tlv->length);
80be1f1ffaSAriel Elior }
81b56e9670SAriel Elior 
82fd1fc79dSAriel Elior /* test whether we support a tlv type */
83fd1fc79dSAriel Elior bool bnx2x_tlv_supported(u16 tlvtype)
84fd1fc79dSAriel Elior {
85fd1fc79dSAriel Elior 	return CHANNEL_TLV_NONE < tlvtype && tlvtype < CHANNEL_TLV_MAX;
86fd1fc79dSAriel Elior }
87fd1fc79dSAriel Elior 
88fd1fc79dSAriel Elior static inline int bnx2x_pfvf_status_codes(int rc)
89fd1fc79dSAriel Elior {
90fd1fc79dSAriel Elior 	switch (rc) {
91fd1fc79dSAriel Elior 	case 0:
92fd1fc79dSAriel Elior 		return PFVF_STATUS_SUCCESS;
93fd1fc79dSAriel Elior 	case -ENOMEM:
94fd1fc79dSAriel Elior 		return PFVF_STATUS_NO_RESOURCE;
95fd1fc79dSAriel Elior 	default:
96fd1fc79dSAriel Elior 		return PFVF_STATUS_FAILURE;
97fd1fc79dSAriel Elior 	}
98fd1fc79dSAriel Elior }
99fd1fc79dSAriel Elior 
100b56e9670SAriel Elior /* General service functions */
101b56e9670SAriel Elior static void storm_memset_vf_mbx_ack(struct bnx2x *bp, u16 abs_fid)
102b56e9670SAriel Elior {
103b56e9670SAriel Elior 	u32 addr = BAR_CSTRORM_INTMEM +
104b56e9670SAriel Elior 		   CSTORM_VF_PF_CHANNEL_STATE_OFFSET(abs_fid);
105b56e9670SAriel Elior 
106b56e9670SAriel Elior 	REG_WR8(bp, addr, VF_PF_CHANNEL_STATE_READY);
107b56e9670SAriel Elior }
108b56e9670SAriel Elior 
109b56e9670SAriel Elior static void storm_memset_vf_mbx_valid(struct bnx2x *bp, u16 abs_fid)
110b56e9670SAriel Elior {
111b56e9670SAriel Elior 	u32 addr = BAR_CSTRORM_INTMEM +
112b56e9670SAriel Elior 		   CSTORM_VF_PF_CHANNEL_VALID_OFFSET(abs_fid);
113b56e9670SAriel Elior 
114b56e9670SAriel Elior 	REG_WR8(bp, addr, 1);
115b56e9670SAriel Elior }
116b56e9670SAriel Elior 
117b56e9670SAriel Elior static inline void bnx2x_set_vf_mbxs_valid(struct bnx2x *bp)
118b56e9670SAriel Elior {
119b56e9670SAriel Elior 	int i;
120b56e9670SAriel Elior 
121b56e9670SAriel Elior 	for_each_vf(bp, i)
122b56e9670SAriel Elior 		storm_memset_vf_mbx_valid(bp, bnx2x_vf(bp, i, abs_vfid));
123b56e9670SAriel Elior }
124b56e9670SAriel Elior 
125b56e9670SAriel Elior /* enable vf_pf mailbox (aka vf-pf-chanell) */
126b56e9670SAriel Elior void bnx2x_vf_enable_mbx(struct bnx2x *bp, u8 abs_vfid)
127b56e9670SAriel Elior {
128b56e9670SAriel Elior 	bnx2x_vf_flr_clnup_epilog(bp, abs_vfid);
129b56e9670SAriel Elior 
130b56e9670SAriel Elior 	/* enable the mailbox in the FW */
131b56e9670SAriel Elior 	storm_memset_vf_mbx_ack(bp, abs_vfid);
132b56e9670SAriel Elior 	storm_memset_vf_mbx_valid(bp, abs_vfid);
133b56e9670SAriel Elior 
134b56e9670SAriel Elior 	/* enable the VF access to the mailbox */
135b56e9670SAriel Elior 	bnx2x_vf_enable_access(bp, abs_vfid);
136b56e9670SAriel Elior }
137fd1fc79dSAriel Elior 
138fd1fc79dSAriel Elior /* this works only on !E1h */
139fd1fc79dSAriel Elior static int bnx2x_copy32_vf_dmae(struct bnx2x *bp, u8 from_vf,
140fd1fc79dSAriel Elior 				dma_addr_t pf_addr, u8 vfid, u32 vf_addr_hi,
141fd1fc79dSAriel Elior 				u32 vf_addr_lo, u32 len32)
142fd1fc79dSAriel Elior {
143fd1fc79dSAriel Elior 	struct dmae_command dmae;
144fd1fc79dSAriel Elior 
145fd1fc79dSAriel Elior 	if (CHIP_IS_E1x(bp)) {
146fd1fc79dSAriel Elior 		BNX2X_ERR("Chip revision does not support VFs\n");
147fd1fc79dSAriel Elior 		return DMAE_NOT_RDY;
148fd1fc79dSAriel Elior 	}
149fd1fc79dSAriel Elior 
150fd1fc79dSAriel Elior 	if (!bp->dmae_ready) {
151fd1fc79dSAriel Elior 		BNX2X_ERR("DMAE is not ready, can not copy\n");
152fd1fc79dSAriel Elior 		return DMAE_NOT_RDY;
153fd1fc79dSAriel Elior 	}
154fd1fc79dSAriel Elior 
155fd1fc79dSAriel Elior 	/* set opcode and fixed command fields */
156fd1fc79dSAriel Elior 	bnx2x_prep_dmae_with_comp(bp, &dmae, DMAE_SRC_PCI, DMAE_DST_PCI);
157fd1fc79dSAriel Elior 
158fd1fc79dSAriel Elior 	if (from_vf) {
159fd1fc79dSAriel Elior 		dmae.opcode_iov = (vfid << DMAE_COMMAND_SRC_VFID_SHIFT) |
160fd1fc79dSAriel Elior 			(DMAE_SRC_VF << DMAE_COMMAND_SRC_VFPF_SHIFT) |
161fd1fc79dSAriel Elior 			(DMAE_DST_PF << DMAE_COMMAND_DST_VFPF_SHIFT);
162fd1fc79dSAriel Elior 
163fd1fc79dSAriel Elior 		dmae.opcode |= (DMAE_C_DST << DMAE_COMMAND_C_FUNC_SHIFT);
164fd1fc79dSAriel Elior 
165fd1fc79dSAriel Elior 		dmae.src_addr_lo = vf_addr_lo;
166fd1fc79dSAriel Elior 		dmae.src_addr_hi = vf_addr_hi;
167fd1fc79dSAriel Elior 		dmae.dst_addr_lo = U64_LO(pf_addr);
168fd1fc79dSAriel Elior 		dmae.dst_addr_hi = U64_HI(pf_addr);
169fd1fc79dSAriel Elior 	} else {
170fd1fc79dSAriel Elior 		dmae.opcode_iov = (vfid << DMAE_COMMAND_DST_VFID_SHIFT) |
171fd1fc79dSAriel Elior 			(DMAE_DST_VF << DMAE_COMMAND_DST_VFPF_SHIFT) |
172fd1fc79dSAriel Elior 			(DMAE_SRC_PF << DMAE_COMMAND_SRC_VFPF_SHIFT);
173fd1fc79dSAriel Elior 
174fd1fc79dSAriel Elior 		dmae.opcode |= (DMAE_C_SRC << DMAE_COMMAND_C_FUNC_SHIFT);
175fd1fc79dSAriel Elior 
176fd1fc79dSAriel Elior 		dmae.src_addr_lo = U64_LO(pf_addr);
177fd1fc79dSAriel Elior 		dmae.src_addr_hi = U64_HI(pf_addr);
178fd1fc79dSAriel Elior 		dmae.dst_addr_lo = vf_addr_lo;
179fd1fc79dSAriel Elior 		dmae.dst_addr_hi = vf_addr_hi;
180fd1fc79dSAriel Elior 	}
181fd1fc79dSAriel Elior 	dmae.len = len32;
182fd1fc79dSAriel Elior 	bnx2x_dp_dmae(bp, &dmae, BNX2X_MSG_DMAE);
183fd1fc79dSAriel Elior 
184fd1fc79dSAriel Elior 	/* issue the command and wait for completion */
185fd1fc79dSAriel Elior 	return bnx2x_issue_dmae_with_comp(bp, &dmae);
186fd1fc79dSAriel Elior }
187fd1fc79dSAriel Elior 
188fd1fc79dSAriel Elior /* dispatch request */
189fd1fc79dSAriel Elior static void bnx2x_vf_mbx_request(struct bnx2x *bp, struct bnx2x_virtf *vf,
190fd1fc79dSAriel Elior 				  struct bnx2x_vf_mbx *mbx)
191fd1fc79dSAriel Elior {
192fd1fc79dSAriel Elior 	int i;
193fd1fc79dSAriel Elior 
194fd1fc79dSAriel Elior 	/* check if tlv type is known */
195fd1fc79dSAriel Elior 	if (bnx2x_tlv_supported(mbx->first_tlv.tl.type)) {
196fd1fc79dSAriel Elior 		/* switch on the opcode */
197fd1fc79dSAriel Elior 		switch (mbx->first_tlv.tl.type) {
198fd1fc79dSAriel Elior 		}
199fd1fc79dSAriel Elior 	} else {
200fd1fc79dSAriel Elior 		/* unknown TLV - this may belong to a VF driver from the future
201fd1fc79dSAriel Elior 		 * - a version written after this PF driver was written, which
202fd1fc79dSAriel Elior 		 * supports features unknown as of yet. Too bad since we don't
203fd1fc79dSAriel Elior 		 * support them. Or this may be because someone wrote a crappy
204fd1fc79dSAriel Elior 		 * VF driver and is sending garbage over the channel.
205fd1fc79dSAriel Elior 		 */
206fd1fc79dSAriel Elior 		BNX2X_ERR("unknown TLV. type %d length %d. first 20 bytes of mailbox buffer:\n",
207fd1fc79dSAriel Elior 			  mbx->first_tlv.tl.type, mbx->first_tlv.tl.length);
208fd1fc79dSAriel Elior 		for (i = 0; i < 20; i++)
209fd1fc79dSAriel Elior 			DP_CONT(BNX2X_MSG_IOV, "%x ",
210fd1fc79dSAriel Elior 				mbx->msg->req.tlv_buf_size.tlv_buffer[i]);
211fd1fc79dSAriel Elior 	}
212fd1fc79dSAriel Elior }
213fd1fc79dSAriel Elior 
214fd1fc79dSAriel Elior /* handle new vf-pf message */
215fd1fc79dSAriel Elior void bnx2x_vf_mbx(struct bnx2x *bp, struct vf_pf_event_data *vfpf_event)
216fd1fc79dSAriel Elior {
217fd1fc79dSAriel Elior 	struct bnx2x_virtf *vf;
218fd1fc79dSAriel Elior 	struct bnx2x_vf_mbx *mbx;
219fd1fc79dSAriel Elior 	u8 vf_idx;
220fd1fc79dSAriel Elior 	int rc;
221fd1fc79dSAriel Elior 
222fd1fc79dSAriel Elior 	DP(BNX2X_MSG_IOV,
223fd1fc79dSAriel Elior 	   "vf pf event received: vfid %d, address_hi %x, address lo %x",
224fd1fc79dSAriel Elior 	   vfpf_event->vf_id, vfpf_event->msg_addr_hi, vfpf_event->msg_addr_lo);
225fd1fc79dSAriel Elior 	/* Sanity checks consider removing later */
226fd1fc79dSAriel Elior 
227fd1fc79dSAriel Elior 	/* check if the vf_id is valid */
228fd1fc79dSAriel Elior 	if (vfpf_event->vf_id - BP_VFDB(bp)->sriov.first_vf_in_pf >
229fd1fc79dSAriel Elior 	    BNX2X_NR_VIRTFN(bp)) {
230fd1fc79dSAriel Elior 		BNX2X_ERR("Illegal vf_id %d max allowed: %d\n",
231fd1fc79dSAriel Elior 			  vfpf_event->vf_id, BNX2X_NR_VIRTFN(bp));
232fd1fc79dSAriel Elior 		goto mbx_done;
233fd1fc79dSAriel Elior 	}
234fd1fc79dSAriel Elior 	vf_idx = bnx2x_vf_idx_by_abs_fid(bp, vfpf_event->vf_id);
235fd1fc79dSAriel Elior 	mbx = BP_VF_MBX(bp, vf_idx);
236fd1fc79dSAriel Elior 
237fd1fc79dSAriel Elior 	/* verify an event is not currently being processed -
238fd1fc79dSAriel Elior 	 * debug failsafe only
239fd1fc79dSAriel Elior 	 */
240fd1fc79dSAriel Elior 	if (mbx->flags & VF_MSG_INPROCESS) {
241fd1fc79dSAriel Elior 		BNX2X_ERR("Previous message is still being processed, vf_id %d\n",
242fd1fc79dSAriel Elior 			  vfpf_event->vf_id);
243fd1fc79dSAriel Elior 		goto mbx_done;
244fd1fc79dSAriel Elior 	}
245fd1fc79dSAriel Elior 	vf = BP_VF(bp, vf_idx);
246fd1fc79dSAriel Elior 
247fd1fc79dSAriel Elior 	/* save the VF message address */
248fd1fc79dSAriel Elior 	mbx->vf_addr_hi = vfpf_event->msg_addr_hi;
249fd1fc79dSAriel Elior 	mbx->vf_addr_lo = vfpf_event->msg_addr_lo;
250fd1fc79dSAriel Elior 	DP(BNX2X_MSG_IOV, "mailbox vf address hi 0x%x, lo 0x%x, offset 0x%x\n",
251fd1fc79dSAriel Elior 	   mbx->vf_addr_hi, mbx->vf_addr_lo, mbx->first_tlv.resp_msg_offset);
252fd1fc79dSAriel Elior 
253fd1fc79dSAriel Elior 	/* dmae to get the VF request */
254fd1fc79dSAriel Elior 	rc = bnx2x_copy32_vf_dmae(bp, true, mbx->msg_mapping, vf->abs_vfid,
255fd1fc79dSAriel Elior 				  mbx->vf_addr_hi, mbx->vf_addr_lo,
256fd1fc79dSAriel Elior 				  sizeof(union vfpf_tlvs)/4);
257fd1fc79dSAriel Elior 	if (rc) {
258fd1fc79dSAriel Elior 		BNX2X_ERR("Failed to copy request VF %d\n", vf->abs_vfid);
259fd1fc79dSAriel Elior 		goto mbx_error;
260fd1fc79dSAriel Elior 	}
261fd1fc79dSAriel Elior 
262fd1fc79dSAriel Elior 	/* process the VF message header */
263fd1fc79dSAriel Elior 	mbx->first_tlv = mbx->msg->req.first_tlv;
264fd1fc79dSAriel Elior 
265fd1fc79dSAriel Elior 	/* dispatch the request (will prepare the response) */
266fd1fc79dSAriel Elior 	bnx2x_vf_mbx_request(bp, vf, mbx);
267fd1fc79dSAriel Elior 	goto mbx_done;
268fd1fc79dSAriel Elior 
269fd1fc79dSAriel Elior mbx_error:
270fd1fc79dSAriel Elior mbx_done:
271fd1fc79dSAriel Elior 	return;
272fd1fc79dSAriel Elior }
273