xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/sfc/siena_sriov.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /****************************************************************************
3*4882a593Smuzhiyun  * Driver for Solarflare network controllers and boards
4*4882a593Smuzhiyun  * Copyright 2010-2012 Solarflare Communications Inc.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include <linux/pci.h>
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include "net_driver.h"
9*4882a593Smuzhiyun #include "efx.h"
10*4882a593Smuzhiyun #include "efx_channels.h"
11*4882a593Smuzhiyun #include "nic.h"
12*4882a593Smuzhiyun #include "io.h"
13*4882a593Smuzhiyun #include "mcdi.h"
14*4882a593Smuzhiyun #include "filter.h"
15*4882a593Smuzhiyun #include "mcdi_pcol.h"
16*4882a593Smuzhiyun #include "farch_regs.h"
17*4882a593Smuzhiyun #include "siena_sriov.h"
18*4882a593Smuzhiyun #include "vfdi.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /* Number of longs required to track all the VIs in a VF */
21*4882a593Smuzhiyun #define VI_MASK_LENGTH BITS_TO_LONGS(1 << EFX_VI_SCALE_MAX)
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* Maximum number of RX queues supported */
24*4882a593Smuzhiyun #define VF_MAX_RX_QUEUES 63
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /**
27*4882a593Smuzhiyun  * enum efx_vf_tx_filter_mode - TX MAC filtering behaviour
28*4882a593Smuzhiyun  * @VF_TX_FILTER_OFF: Disabled
29*4882a593Smuzhiyun  * @VF_TX_FILTER_AUTO: Enabled if MAC address assigned to VF and only
30*4882a593Smuzhiyun  *	2 TX queues allowed per VF.
31*4882a593Smuzhiyun  * @VF_TX_FILTER_ON: Enabled
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun enum efx_vf_tx_filter_mode {
34*4882a593Smuzhiyun 	VF_TX_FILTER_OFF,
35*4882a593Smuzhiyun 	VF_TX_FILTER_AUTO,
36*4882a593Smuzhiyun 	VF_TX_FILTER_ON,
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /**
40*4882a593Smuzhiyun  * struct siena_vf - Back-end resource and protocol state for a PCI VF
41*4882a593Smuzhiyun  * @efx: The Efx NIC owning this VF
42*4882a593Smuzhiyun  * @pci_rid: The PCI requester ID for this VF
43*4882a593Smuzhiyun  * @pci_name: The PCI name (formatted address) of this VF
44*4882a593Smuzhiyun  * @index: Index of VF within its port and PF.
45*4882a593Smuzhiyun  * @req: VFDI incoming request work item. Incoming USR_EV events are received
46*4882a593Smuzhiyun  *	by the NAPI handler, but must be handled by executing MCDI requests
47*4882a593Smuzhiyun  *	inside a work item.
48*4882a593Smuzhiyun  * @req_addr: VFDI incoming request DMA address (in VF's PCI address space).
49*4882a593Smuzhiyun  * @req_type: Expected next incoming (from VF) %VFDI_EV_TYPE member.
50*4882a593Smuzhiyun  * @req_seqno: Expected next incoming (from VF) %VFDI_EV_SEQ member.
51*4882a593Smuzhiyun  * @msg_seqno: Next %VFDI_EV_SEQ member to reply to VF. Protected by
52*4882a593Smuzhiyun  *	@status_lock
53*4882a593Smuzhiyun  * @busy: VFDI request queued to be processed or being processed. Receiving
54*4882a593Smuzhiyun  *	a VFDI request when @busy is set is an error condition.
55*4882a593Smuzhiyun  * @buf: Incoming VFDI requests are DMA from the VF into this buffer.
56*4882a593Smuzhiyun  * @buftbl_base: Buffer table entries for this VF start at this index.
57*4882a593Smuzhiyun  * @rx_filtering: Receive filtering has been requested by the VF driver.
58*4882a593Smuzhiyun  * @rx_filter_flags: The flags sent in the %VFDI_OP_INSERT_FILTER request.
59*4882a593Smuzhiyun  * @rx_filter_qid: VF relative qid for RX filter requested by VF.
60*4882a593Smuzhiyun  * @rx_filter_id: Receive MAC filter ID. Only one filter per VF is supported.
61*4882a593Smuzhiyun  * @tx_filter_mode: Transmit MAC filtering mode.
62*4882a593Smuzhiyun  * @tx_filter_id: Transmit MAC filter ID.
63*4882a593Smuzhiyun  * @addr: The MAC address and outer vlan tag of the VF.
64*4882a593Smuzhiyun  * @status_addr: VF DMA address of page for &struct vfdi_status updates.
65*4882a593Smuzhiyun  * @status_lock: Mutex protecting @msg_seqno, @status_addr, @addr,
66*4882a593Smuzhiyun  *	@peer_page_addrs and @peer_page_count from simultaneous
67*4882a593Smuzhiyun  *	updates by the VM and consumption by
68*4882a593Smuzhiyun  *	efx_siena_sriov_update_vf_addr()
69*4882a593Smuzhiyun  * @peer_page_addrs: Pointer to an array of guest pages for local addresses.
70*4882a593Smuzhiyun  * @peer_page_count: Number of entries in @peer_page_count.
71*4882a593Smuzhiyun  * @evq0_addrs: Array of guest pages backing evq0.
72*4882a593Smuzhiyun  * @evq0_count: Number of entries in @evq0_addrs.
73*4882a593Smuzhiyun  * @flush_waitq: wait queue used by %VFDI_OP_FINI_ALL_QUEUES handler
74*4882a593Smuzhiyun  *	to wait for flush completions.
75*4882a593Smuzhiyun  * @txq_lock: Mutex for TX queue allocation.
76*4882a593Smuzhiyun  * @txq_mask: Mask of initialized transmit queues.
77*4882a593Smuzhiyun  * @txq_count: Number of initialized transmit queues.
78*4882a593Smuzhiyun  * @rxq_mask: Mask of initialized receive queues.
79*4882a593Smuzhiyun  * @rxq_count: Number of initialized receive queues.
80*4882a593Smuzhiyun  * @rxq_retry_mask: Mask or receive queues that need to be flushed again
81*4882a593Smuzhiyun  *	due to flush failure.
82*4882a593Smuzhiyun  * @rxq_retry_count: Number of receive queues in @rxq_retry_mask.
83*4882a593Smuzhiyun  * @reset_work: Work item to schedule a VF reset.
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun struct siena_vf {
86*4882a593Smuzhiyun 	struct efx_nic *efx;
87*4882a593Smuzhiyun 	unsigned int pci_rid;
88*4882a593Smuzhiyun 	char pci_name[13]; /* dddd:bb:dd.f */
89*4882a593Smuzhiyun 	unsigned int index;
90*4882a593Smuzhiyun 	struct work_struct req;
91*4882a593Smuzhiyun 	u64 req_addr;
92*4882a593Smuzhiyun 	int req_type;
93*4882a593Smuzhiyun 	unsigned req_seqno;
94*4882a593Smuzhiyun 	unsigned msg_seqno;
95*4882a593Smuzhiyun 	bool busy;
96*4882a593Smuzhiyun 	struct efx_buffer buf;
97*4882a593Smuzhiyun 	unsigned buftbl_base;
98*4882a593Smuzhiyun 	bool rx_filtering;
99*4882a593Smuzhiyun 	enum efx_filter_flags rx_filter_flags;
100*4882a593Smuzhiyun 	unsigned rx_filter_qid;
101*4882a593Smuzhiyun 	int rx_filter_id;
102*4882a593Smuzhiyun 	enum efx_vf_tx_filter_mode tx_filter_mode;
103*4882a593Smuzhiyun 	int tx_filter_id;
104*4882a593Smuzhiyun 	struct vfdi_endpoint addr;
105*4882a593Smuzhiyun 	u64 status_addr;
106*4882a593Smuzhiyun 	struct mutex status_lock;
107*4882a593Smuzhiyun 	u64 *peer_page_addrs;
108*4882a593Smuzhiyun 	unsigned peer_page_count;
109*4882a593Smuzhiyun 	u64 evq0_addrs[EFX_MAX_VF_EVQ_SIZE * sizeof(efx_qword_t) /
110*4882a593Smuzhiyun 		       EFX_BUF_SIZE];
111*4882a593Smuzhiyun 	unsigned evq0_count;
112*4882a593Smuzhiyun 	wait_queue_head_t flush_waitq;
113*4882a593Smuzhiyun 	struct mutex txq_lock;
114*4882a593Smuzhiyun 	unsigned long txq_mask[VI_MASK_LENGTH];
115*4882a593Smuzhiyun 	unsigned txq_count;
116*4882a593Smuzhiyun 	unsigned long rxq_mask[VI_MASK_LENGTH];
117*4882a593Smuzhiyun 	unsigned rxq_count;
118*4882a593Smuzhiyun 	unsigned long rxq_retry_mask[VI_MASK_LENGTH];
119*4882a593Smuzhiyun 	atomic_t rxq_retry_count;
120*4882a593Smuzhiyun 	struct work_struct reset_work;
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun struct efx_memcpy_req {
124*4882a593Smuzhiyun 	unsigned int from_rid;
125*4882a593Smuzhiyun 	void *from_buf;
126*4882a593Smuzhiyun 	u64 from_addr;
127*4882a593Smuzhiyun 	unsigned int to_rid;
128*4882a593Smuzhiyun 	u64 to_addr;
129*4882a593Smuzhiyun 	unsigned length;
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun  * struct efx_local_addr - A MAC address on the vswitch without a VF.
134*4882a593Smuzhiyun  *
135*4882a593Smuzhiyun  * Siena does not have a switch, so VFs can't transmit data to each
136*4882a593Smuzhiyun  * other. Instead the VFs must be made aware of the local addresses
137*4882a593Smuzhiyun  * on the vswitch, so that they can arrange for an alternative
138*4882a593Smuzhiyun  * software datapath to be used.
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * @link: List head for insertion into efx->local_addr_list.
141*4882a593Smuzhiyun  * @addr: Ethernet address
142*4882a593Smuzhiyun  */
143*4882a593Smuzhiyun struct efx_local_addr {
144*4882a593Smuzhiyun 	struct list_head link;
145*4882a593Smuzhiyun 	u8 addr[ETH_ALEN];
146*4882a593Smuzhiyun };
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /**
149*4882a593Smuzhiyun  * struct efx_endpoint_page - Page of vfdi_endpoint structures
150*4882a593Smuzhiyun  *
151*4882a593Smuzhiyun  * @link: List head for insertion into efx->local_page_list.
152*4882a593Smuzhiyun  * @ptr: Pointer to page.
153*4882a593Smuzhiyun  * @addr: DMA address of page.
154*4882a593Smuzhiyun  */
155*4882a593Smuzhiyun struct efx_endpoint_page {
156*4882a593Smuzhiyun 	struct list_head link;
157*4882a593Smuzhiyun 	void *ptr;
158*4882a593Smuzhiyun 	dma_addr_t addr;
159*4882a593Smuzhiyun };
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /* Buffer table entries are reserved txq0,rxq0,evq0,txq1,rxq1,evq1 */
162*4882a593Smuzhiyun #define EFX_BUFTBL_TXQ_BASE(_vf, _qid)					\
163*4882a593Smuzhiyun 	((_vf)->buftbl_base + EFX_VF_BUFTBL_PER_VI * (_qid))
164*4882a593Smuzhiyun #define EFX_BUFTBL_RXQ_BASE(_vf, _qid)					\
165*4882a593Smuzhiyun 	(EFX_BUFTBL_TXQ_BASE(_vf, _qid) +				\
166*4882a593Smuzhiyun 	 (EFX_MAX_DMAQ_SIZE * sizeof(efx_qword_t) / EFX_BUF_SIZE))
167*4882a593Smuzhiyun #define EFX_BUFTBL_EVQ_BASE(_vf, _qid)					\
168*4882a593Smuzhiyun 	(EFX_BUFTBL_TXQ_BASE(_vf, _qid) +				\
169*4882a593Smuzhiyun 	 (2 * EFX_MAX_DMAQ_SIZE * sizeof(efx_qword_t) / EFX_BUF_SIZE))
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun #define EFX_FIELD_MASK(_field)			\
172*4882a593Smuzhiyun 	((1 << _field ## _WIDTH) - 1)
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /* VFs can only use this many transmit channels */
175*4882a593Smuzhiyun static unsigned int vf_max_tx_channels = 2;
176*4882a593Smuzhiyun module_param(vf_max_tx_channels, uint, 0444);
177*4882a593Smuzhiyun MODULE_PARM_DESC(vf_max_tx_channels,
178*4882a593Smuzhiyun 		 "Limit the number of TX channels VFs can use");
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun static int max_vfs = -1;
181*4882a593Smuzhiyun module_param(max_vfs, int, 0444);
182*4882a593Smuzhiyun MODULE_PARM_DESC(max_vfs,
183*4882a593Smuzhiyun 		 "Reduce the number of VFs initialized by the driver");
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /* Workqueue used by VFDI communication.  We can't use the global
186*4882a593Smuzhiyun  * workqueue because it may be running the VF driver's probe()
187*4882a593Smuzhiyun  * routine, which will be blocked there waiting for a VFDI response.
188*4882a593Smuzhiyun  */
189*4882a593Smuzhiyun static struct workqueue_struct *vfdi_workqueue;
190*4882a593Smuzhiyun 
abs_index(struct siena_vf * vf,unsigned index)191*4882a593Smuzhiyun static unsigned abs_index(struct siena_vf *vf, unsigned index)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun 	return EFX_VI_BASE + vf->index * efx_vf_size(vf->efx) + index;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun 
efx_siena_sriov_cmd(struct efx_nic * efx,bool enable,unsigned * vi_scale_out,unsigned * vf_total_out)196*4882a593Smuzhiyun static int efx_siena_sriov_cmd(struct efx_nic *efx, bool enable,
197*4882a593Smuzhiyun 			       unsigned *vi_scale_out, unsigned *vf_total_out)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	MCDI_DECLARE_BUF(inbuf, MC_CMD_SRIOV_IN_LEN);
200*4882a593Smuzhiyun 	MCDI_DECLARE_BUF(outbuf, MC_CMD_SRIOV_OUT_LEN);
201*4882a593Smuzhiyun 	unsigned vi_scale, vf_total;
202*4882a593Smuzhiyun 	size_t outlen;
203*4882a593Smuzhiyun 	int rc;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	MCDI_SET_DWORD(inbuf, SRIOV_IN_ENABLE, enable ? 1 : 0);
206*4882a593Smuzhiyun 	MCDI_SET_DWORD(inbuf, SRIOV_IN_VI_BASE, EFX_VI_BASE);
207*4882a593Smuzhiyun 	MCDI_SET_DWORD(inbuf, SRIOV_IN_VF_COUNT, efx->vf_count);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_SRIOV, inbuf, MC_CMD_SRIOV_IN_LEN,
210*4882a593Smuzhiyun 				outbuf, MC_CMD_SRIOV_OUT_LEN, &outlen);
211*4882a593Smuzhiyun 	if (rc)
212*4882a593Smuzhiyun 		return rc;
213*4882a593Smuzhiyun 	if (outlen < MC_CMD_SRIOV_OUT_LEN)
214*4882a593Smuzhiyun 		return -EIO;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	vf_total = MCDI_DWORD(outbuf, SRIOV_OUT_VF_TOTAL);
217*4882a593Smuzhiyun 	vi_scale = MCDI_DWORD(outbuf, SRIOV_OUT_VI_SCALE);
218*4882a593Smuzhiyun 	if (vi_scale > EFX_VI_SCALE_MAX)
219*4882a593Smuzhiyun 		return -EOPNOTSUPP;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (vi_scale_out)
222*4882a593Smuzhiyun 		*vi_scale_out = vi_scale;
223*4882a593Smuzhiyun 	if (vf_total_out)
224*4882a593Smuzhiyun 		*vf_total_out = vf_total;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	return 0;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
efx_siena_sriov_usrev(struct efx_nic * efx,bool enabled)229*4882a593Smuzhiyun static void efx_siena_sriov_usrev(struct efx_nic *efx, bool enabled)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
232*4882a593Smuzhiyun 	efx_oword_t reg;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_2(reg,
235*4882a593Smuzhiyun 			     FRF_CZ_USREV_DIS, enabled ? 0 : 1,
236*4882a593Smuzhiyun 			     FRF_CZ_DFLT_EVQ, nic_data->vfdi_channel->channel);
237*4882a593Smuzhiyun 	efx_writeo(efx, &reg, FR_CZ_USR_EV_CFG);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
efx_siena_sriov_memcpy(struct efx_nic * efx,struct efx_memcpy_req * req,unsigned int count)240*4882a593Smuzhiyun static int efx_siena_sriov_memcpy(struct efx_nic *efx,
241*4882a593Smuzhiyun 				  struct efx_memcpy_req *req,
242*4882a593Smuzhiyun 				  unsigned int count)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	MCDI_DECLARE_BUF(inbuf, MCDI_CTL_SDU_LEN_MAX_V1);
245*4882a593Smuzhiyun 	MCDI_DECLARE_STRUCT_PTR(record);
246*4882a593Smuzhiyun 	unsigned int index, used;
247*4882a593Smuzhiyun 	u64 from_addr;
248*4882a593Smuzhiyun 	u32 from_rid;
249*4882a593Smuzhiyun 	int rc;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	mb();	/* Finish writing source/reading dest before DMA starts */
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	if (WARN_ON(count > MC_CMD_MEMCPY_IN_RECORD_MAXNUM))
254*4882a593Smuzhiyun 		return -ENOBUFS;
255*4882a593Smuzhiyun 	used = MC_CMD_MEMCPY_IN_LEN(count);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	for (index = 0; index < count; index++) {
258*4882a593Smuzhiyun 		record = MCDI_ARRAY_STRUCT_PTR(inbuf, MEMCPY_IN_RECORD, index);
259*4882a593Smuzhiyun 		MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_NUM_RECORDS,
260*4882a593Smuzhiyun 			       count);
261*4882a593Smuzhiyun 		MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_TO_RID,
262*4882a593Smuzhiyun 			       req->to_rid);
263*4882a593Smuzhiyun 		MCDI_SET_QWORD(record, MEMCPY_RECORD_TYPEDEF_TO_ADDR,
264*4882a593Smuzhiyun 			       req->to_addr);
265*4882a593Smuzhiyun 		if (req->from_buf == NULL) {
266*4882a593Smuzhiyun 			from_rid = req->from_rid;
267*4882a593Smuzhiyun 			from_addr = req->from_addr;
268*4882a593Smuzhiyun 		} else {
269*4882a593Smuzhiyun 			if (WARN_ON(used + req->length >
270*4882a593Smuzhiyun 				    MCDI_CTL_SDU_LEN_MAX_V1)) {
271*4882a593Smuzhiyun 				rc = -ENOBUFS;
272*4882a593Smuzhiyun 				goto out;
273*4882a593Smuzhiyun 			}
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 			from_rid = MC_CMD_MEMCPY_RECORD_TYPEDEF_RID_INLINE;
276*4882a593Smuzhiyun 			from_addr = used;
277*4882a593Smuzhiyun 			memcpy(_MCDI_PTR(inbuf, used), req->from_buf,
278*4882a593Smuzhiyun 			       req->length);
279*4882a593Smuzhiyun 			used += req->length;
280*4882a593Smuzhiyun 		}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 		MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_FROM_RID, from_rid);
283*4882a593Smuzhiyun 		MCDI_SET_QWORD(record, MEMCPY_RECORD_TYPEDEF_FROM_ADDR,
284*4882a593Smuzhiyun 			       from_addr);
285*4882a593Smuzhiyun 		MCDI_SET_DWORD(record, MEMCPY_RECORD_TYPEDEF_LENGTH,
286*4882a593Smuzhiyun 			       req->length);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 		++req;
289*4882a593Smuzhiyun 	}
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	rc = efx_mcdi_rpc(efx, MC_CMD_MEMCPY, inbuf, used, NULL, 0, NULL);
292*4882a593Smuzhiyun out:
293*4882a593Smuzhiyun 	mb();	/* Don't write source/read dest before DMA is complete */
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	return rc;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun /* The TX filter is entirely controlled by this driver, and is modified
299*4882a593Smuzhiyun  * underneath the feet of the VF
300*4882a593Smuzhiyun  */
efx_siena_sriov_reset_tx_filter(struct siena_vf * vf)301*4882a593Smuzhiyun static void efx_siena_sriov_reset_tx_filter(struct siena_vf *vf)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
304*4882a593Smuzhiyun 	struct efx_filter_spec filter;
305*4882a593Smuzhiyun 	u16 vlan;
306*4882a593Smuzhiyun 	int rc;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	if (vf->tx_filter_id != -1) {
309*4882a593Smuzhiyun 		efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
310*4882a593Smuzhiyun 					  vf->tx_filter_id);
311*4882a593Smuzhiyun 		netif_dbg(efx, hw, efx->net_dev, "Removed vf %s tx filter %d\n",
312*4882a593Smuzhiyun 			  vf->pci_name, vf->tx_filter_id);
313*4882a593Smuzhiyun 		vf->tx_filter_id = -1;
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	if (is_zero_ether_addr(vf->addr.mac_addr))
317*4882a593Smuzhiyun 		return;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	/* Turn on TX filtering automatically if not explicitly
320*4882a593Smuzhiyun 	 * enabled or disabled.
321*4882a593Smuzhiyun 	 */
322*4882a593Smuzhiyun 	if (vf->tx_filter_mode == VF_TX_FILTER_AUTO && vf_max_tx_channels <= 2)
323*4882a593Smuzhiyun 		vf->tx_filter_mode = VF_TX_FILTER_ON;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	vlan = ntohs(vf->addr.tci) & VLAN_VID_MASK;
326*4882a593Smuzhiyun 	efx_filter_init_tx(&filter, abs_index(vf, 0));
327*4882a593Smuzhiyun 	rc = efx_filter_set_eth_local(&filter,
328*4882a593Smuzhiyun 				      vlan ? vlan : EFX_FILTER_VID_UNSPEC,
329*4882a593Smuzhiyun 				      vf->addr.mac_addr);
330*4882a593Smuzhiyun 	BUG_ON(rc);
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	rc = efx_filter_insert_filter(efx, &filter, true);
333*4882a593Smuzhiyun 	if (rc < 0) {
334*4882a593Smuzhiyun 		netif_warn(efx, hw, efx->net_dev,
335*4882a593Smuzhiyun 			   "Unable to migrate tx filter for vf %s\n",
336*4882a593Smuzhiyun 			   vf->pci_name);
337*4882a593Smuzhiyun 	} else {
338*4882a593Smuzhiyun 		netif_dbg(efx, hw, efx->net_dev, "Inserted vf %s tx filter %d\n",
339*4882a593Smuzhiyun 			  vf->pci_name, rc);
340*4882a593Smuzhiyun 		vf->tx_filter_id = rc;
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /* The RX filter is managed here on behalf of the VF driver */
efx_siena_sriov_reset_rx_filter(struct siena_vf * vf)345*4882a593Smuzhiyun static void efx_siena_sriov_reset_rx_filter(struct siena_vf *vf)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
348*4882a593Smuzhiyun 	struct efx_filter_spec filter;
349*4882a593Smuzhiyun 	u16 vlan;
350*4882a593Smuzhiyun 	int rc;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (vf->rx_filter_id != -1) {
353*4882a593Smuzhiyun 		efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
354*4882a593Smuzhiyun 					  vf->rx_filter_id);
355*4882a593Smuzhiyun 		netif_dbg(efx, hw, efx->net_dev, "Removed vf %s rx filter %d\n",
356*4882a593Smuzhiyun 			  vf->pci_name, vf->rx_filter_id);
357*4882a593Smuzhiyun 		vf->rx_filter_id = -1;
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	if (!vf->rx_filtering || is_zero_ether_addr(vf->addr.mac_addr))
361*4882a593Smuzhiyun 		return;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	vlan = ntohs(vf->addr.tci) & VLAN_VID_MASK;
364*4882a593Smuzhiyun 	efx_filter_init_rx(&filter, EFX_FILTER_PRI_REQUIRED,
365*4882a593Smuzhiyun 			   vf->rx_filter_flags,
366*4882a593Smuzhiyun 			   abs_index(vf, vf->rx_filter_qid));
367*4882a593Smuzhiyun 	rc = efx_filter_set_eth_local(&filter,
368*4882a593Smuzhiyun 				      vlan ? vlan : EFX_FILTER_VID_UNSPEC,
369*4882a593Smuzhiyun 				      vf->addr.mac_addr);
370*4882a593Smuzhiyun 	BUG_ON(rc);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	rc = efx_filter_insert_filter(efx, &filter, true);
373*4882a593Smuzhiyun 	if (rc < 0) {
374*4882a593Smuzhiyun 		netif_warn(efx, hw, efx->net_dev,
375*4882a593Smuzhiyun 			   "Unable to insert rx filter for vf %s\n",
376*4882a593Smuzhiyun 			   vf->pci_name);
377*4882a593Smuzhiyun 	} else {
378*4882a593Smuzhiyun 		netif_dbg(efx, hw, efx->net_dev, "Inserted vf %s rx filter %d\n",
379*4882a593Smuzhiyun 			  vf->pci_name, rc);
380*4882a593Smuzhiyun 		vf->rx_filter_id = rc;
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun 
__efx_siena_sriov_update_vf_addr(struct siena_vf * vf)384*4882a593Smuzhiyun static void __efx_siena_sriov_update_vf_addr(struct siena_vf *vf)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
387*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	efx_siena_sriov_reset_tx_filter(vf);
390*4882a593Smuzhiyun 	efx_siena_sriov_reset_rx_filter(vf);
391*4882a593Smuzhiyun 	queue_work(vfdi_workqueue, &nic_data->peer_work);
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun /* Push the peer list to this VF. The caller must hold status_lock to interlock
395*4882a593Smuzhiyun  * with VFDI requests, and they must be serialised against manipulation of
396*4882a593Smuzhiyun  * local_page_list, either by acquiring local_lock or by running from
397*4882a593Smuzhiyun  * efx_siena_sriov_peer_work()
398*4882a593Smuzhiyun  */
__efx_siena_sriov_push_vf_status(struct siena_vf * vf)399*4882a593Smuzhiyun static void __efx_siena_sriov_push_vf_status(struct siena_vf *vf)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
402*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
403*4882a593Smuzhiyun 	struct vfdi_status *status = nic_data->vfdi_status.addr;
404*4882a593Smuzhiyun 	struct efx_memcpy_req copy[4];
405*4882a593Smuzhiyun 	struct efx_endpoint_page *epp;
406*4882a593Smuzhiyun 	unsigned int pos, count;
407*4882a593Smuzhiyun 	unsigned data_offset;
408*4882a593Smuzhiyun 	efx_qword_t event;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	WARN_ON(!mutex_is_locked(&vf->status_lock));
411*4882a593Smuzhiyun 	WARN_ON(!vf->status_addr);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	status->local = vf->addr;
414*4882a593Smuzhiyun 	status->generation_end = ++status->generation_start;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	memset(copy, '\0', sizeof(copy));
417*4882a593Smuzhiyun 	/* Write generation_start */
418*4882a593Smuzhiyun 	copy[0].from_buf = &status->generation_start;
419*4882a593Smuzhiyun 	copy[0].to_rid = vf->pci_rid;
420*4882a593Smuzhiyun 	copy[0].to_addr = vf->status_addr + offsetof(struct vfdi_status,
421*4882a593Smuzhiyun 						     generation_start);
422*4882a593Smuzhiyun 	copy[0].length = sizeof(status->generation_start);
423*4882a593Smuzhiyun 	/* DMA the rest of the structure (excluding the generations). This
424*4882a593Smuzhiyun 	 * assumes that the non-generation portion of vfdi_status is in
425*4882a593Smuzhiyun 	 * one chunk starting at the version member.
426*4882a593Smuzhiyun 	 */
427*4882a593Smuzhiyun 	data_offset = offsetof(struct vfdi_status, version);
428*4882a593Smuzhiyun 	copy[1].from_rid = efx->pci_dev->devfn;
429*4882a593Smuzhiyun 	copy[1].from_addr = nic_data->vfdi_status.dma_addr + data_offset;
430*4882a593Smuzhiyun 	copy[1].to_rid = vf->pci_rid;
431*4882a593Smuzhiyun 	copy[1].to_addr = vf->status_addr + data_offset;
432*4882a593Smuzhiyun 	copy[1].length =  status->length - data_offset;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/* Copy the peer pages */
435*4882a593Smuzhiyun 	pos = 2;
436*4882a593Smuzhiyun 	count = 0;
437*4882a593Smuzhiyun 	list_for_each_entry(epp, &nic_data->local_page_list, link) {
438*4882a593Smuzhiyun 		if (count == vf->peer_page_count) {
439*4882a593Smuzhiyun 			/* The VF driver will know they need to provide more
440*4882a593Smuzhiyun 			 * pages because peer_addr_count is too large.
441*4882a593Smuzhiyun 			 */
442*4882a593Smuzhiyun 			break;
443*4882a593Smuzhiyun 		}
444*4882a593Smuzhiyun 		copy[pos].from_buf = NULL;
445*4882a593Smuzhiyun 		copy[pos].from_rid = efx->pci_dev->devfn;
446*4882a593Smuzhiyun 		copy[pos].from_addr = epp->addr;
447*4882a593Smuzhiyun 		copy[pos].to_rid = vf->pci_rid;
448*4882a593Smuzhiyun 		copy[pos].to_addr = vf->peer_page_addrs[count];
449*4882a593Smuzhiyun 		copy[pos].length = EFX_PAGE_SIZE;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 		if (++pos == ARRAY_SIZE(copy)) {
452*4882a593Smuzhiyun 			efx_siena_sriov_memcpy(efx, copy, ARRAY_SIZE(copy));
453*4882a593Smuzhiyun 			pos = 0;
454*4882a593Smuzhiyun 		}
455*4882a593Smuzhiyun 		++count;
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	/* Write generation_end */
459*4882a593Smuzhiyun 	copy[pos].from_buf = &status->generation_end;
460*4882a593Smuzhiyun 	copy[pos].to_rid = vf->pci_rid;
461*4882a593Smuzhiyun 	copy[pos].to_addr = vf->status_addr + offsetof(struct vfdi_status,
462*4882a593Smuzhiyun 						       generation_end);
463*4882a593Smuzhiyun 	copy[pos].length = sizeof(status->generation_end);
464*4882a593Smuzhiyun 	efx_siena_sriov_memcpy(efx, copy, pos + 1);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	/* Notify the guest */
467*4882a593Smuzhiyun 	EFX_POPULATE_QWORD_3(event,
468*4882a593Smuzhiyun 			     FSF_AZ_EV_CODE, FSE_CZ_EV_CODE_USER_EV,
469*4882a593Smuzhiyun 			     VFDI_EV_SEQ, (vf->msg_seqno & 0xff),
470*4882a593Smuzhiyun 			     VFDI_EV_TYPE, VFDI_EV_TYPE_STATUS);
471*4882a593Smuzhiyun 	++vf->msg_seqno;
472*4882a593Smuzhiyun 	efx_farch_generate_event(efx,
473*4882a593Smuzhiyun 				 EFX_VI_BASE + vf->index * efx_vf_size(efx),
474*4882a593Smuzhiyun 				 &event);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun 
efx_siena_sriov_bufs(struct efx_nic * efx,unsigned offset,u64 * addr,unsigned count)477*4882a593Smuzhiyun static void efx_siena_sriov_bufs(struct efx_nic *efx, unsigned offset,
478*4882a593Smuzhiyun 				 u64 *addr, unsigned count)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun 	efx_qword_t buf;
481*4882a593Smuzhiyun 	unsigned pos;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	for (pos = 0; pos < count; ++pos) {
484*4882a593Smuzhiyun 		EFX_POPULATE_QWORD_3(buf,
485*4882a593Smuzhiyun 				     FRF_AZ_BUF_ADR_REGION, 0,
486*4882a593Smuzhiyun 				     FRF_AZ_BUF_ADR_FBUF,
487*4882a593Smuzhiyun 				     addr ? addr[pos] >> 12 : 0,
488*4882a593Smuzhiyun 				     FRF_AZ_BUF_OWNER_ID_FBUF, 0);
489*4882a593Smuzhiyun 		efx_sram_writeq(efx, efx->membase + FR_BZ_BUF_FULL_TBL,
490*4882a593Smuzhiyun 				&buf, offset + pos);
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun 
bad_vf_index(struct efx_nic * efx,unsigned index)494*4882a593Smuzhiyun static bool bad_vf_index(struct efx_nic *efx, unsigned index)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	return index >= efx_vf_size(efx);
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun 
bad_buf_count(unsigned buf_count,unsigned max_entry_count)499*4882a593Smuzhiyun static bool bad_buf_count(unsigned buf_count, unsigned max_entry_count)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	unsigned max_buf_count = max_entry_count *
502*4882a593Smuzhiyun 		sizeof(efx_qword_t) / EFX_BUF_SIZE;
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	return ((buf_count & (buf_count - 1)) || buf_count > max_buf_count);
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun /* Check that VI specified by per-port index belongs to a VF.
508*4882a593Smuzhiyun  * Optionally set VF index and VI index within the VF.
509*4882a593Smuzhiyun  */
map_vi_index(struct efx_nic * efx,unsigned abs_index,struct siena_vf ** vf_out,unsigned * rel_index_out)510*4882a593Smuzhiyun static bool map_vi_index(struct efx_nic *efx, unsigned abs_index,
511*4882a593Smuzhiyun 			 struct siena_vf **vf_out, unsigned *rel_index_out)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
514*4882a593Smuzhiyun 	unsigned vf_i;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	if (abs_index < EFX_VI_BASE)
517*4882a593Smuzhiyun 		return true;
518*4882a593Smuzhiyun 	vf_i = (abs_index - EFX_VI_BASE) / efx_vf_size(efx);
519*4882a593Smuzhiyun 	if (vf_i >= efx->vf_init_count)
520*4882a593Smuzhiyun 		return true;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	if (vf_out)
523*4882a593Smuzhiyun 		*vf_out = nic_data->vf + vf_i;
524*4882a593Smuzhiyun 	if (rel_index_out)
525*4882a593Smuzhiyun 		*rel_index_out = abs_index % efx_vf_size(efx);
526*4882a593Smuzhiyun 	return false;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun 
efx_vfdi_init_evq(struct siena_vf * vf)529*4882a593Smuzhiyun static int efx_vfdi_init_evq(struct siena_vf *vf)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
532*4882a593Smuzhiyun 	struct vfdi_req *req = vf->buf.addr;
533*4882a593Smuzhiyun 	unsigned vf_evq = req->u.init_evq.index;
534*4882a593Smuzhiyun 	unsigned buf_count = req->u.init_evq.buf_count;
535*4882a593Smuzhiyun 	unsigned abs_evq = abs_index(vf, vf_evq);
536*4882a593Smuzhiyun 	unsigned buftbl = EFX_BUFTBL_EVQ_BASE(vf, vf_evq);
537*4882a593Smuzhiyun 	efx_oword_t reg;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	if (bad_vf_index(efx, vf_evq) ||
540*4882a593Smuzhiyun 	    bad_buf_count(buf_count, EFX_MAX_VF_EVQ_SIZE)) {
541*4882a593Smuzhiyun 		if (net_ratelimit())
542*4882a593Smuzhiyun 			netif_err(efx, hw, efx->net_dev,
543*4882a593Smuzhiyun 				  "ERROR: Invalid INIT_EVQ from %s: evq %d bufs %d\n",
544*4882a593Smuzhiyun 				  vf->pci_name, vf_evq, buf_count);
545*4882a593Smuzhiyun 		return VFDI_RC_EINVAL;
546*4882a593Smuzhiyun 	}
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	efx_siena_sriov_bufs(efx, buftbl, req->u.init_evq.addr, buf_count);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_3(reg,
551*4882a593Smuzhiyun 			     FRF_CZ_TIMER_Q_EN, 1,
552*4882a593Smuzhiyun 			     FRF_CZ_HOST_NOTIFY_MODE, 0,
553*4882a593Smuzhiyun 			     FRF_CZ_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS);
554*4882a593Smuzhiyun 	efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, abs_evq);
555*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_3(reg,
556*4882a593Smuzhiyun 			     FRF_AZ_EVQ_EN, 1,
557*4882a593Smuzhiyun 			     FRF_AZ_EVQ_SIZE, __ffs(buf_count),
558*4882a593Smuzhiyun 			     FRF_AZ_EVQ_BUF_BASE_ID, buftbl);
559*4882a593Smuzhiyun 	efx_writeo_table(efx, &reg, FR_BZ_EVQ_PTR_TBL, abs_evq);
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	if (vf_evq == 0) {
562*4882a593Smuzhiyun 		memcpy(vf->evq0_addrs, req->u.init_evq.addr,
563*4882a593Smuzhiyun 		       buf_count * sizeof(u64));
564*4882a593Smuzhiyun 		vf->evq0_count = buf_count;
565*4882a593Smuzhiyun 	}
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	return VFDI_RC_SUCCESS;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun 
efx_vfdi_init_rxq(struct siena_vf * vf)570*4882a593Smuzhiyun static int efx_vfdi_init_rxq(struct siena_vf *vf)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
573*4882a593Smuzhiyun 	struct vfdi_req *req = vf->buf.addr;
574*4882a593Smuzhiyun 	unsigned vf_rxq = req->u.init_rxq.index;
575*4882a593Smuzhiyun 	unsigned vf_evq = req->u.init_rxq.evq;
576*4882a593Smuzhiyun 	unsigned buf_count = req->u.init_rxq.buf_count;
577*4882a593Smuzhiyun 	unsigned buftbl = EFX_BUFTBL_RXQ_BASE(vf, vf_rxq);
578*4882a593Smuzhiyun 	unsigned label;
579*4882a593Smuzhiyun 	efx_oword_t reg;
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	if (bad_vf_index(efx, vf_evq) || bad_vf_index(efx, vf_rxq) ||
582*4882a593Smuzhiyun 	    vf_rxq >= VF_MAX_RX_QUEUES ||
583*4882a593Smuzhiyun 	    bad_buf_count(buf_count, EFX_MAX_DMAQ_SIZE)) {
584*4882a593Smuzhiyun 		if (net_ratelimit())
585*4882a593Smuzhiyun 			netif_err(efx, hw, efx->net_dev,
586*4882a593Smuzhiyun 				  "ERROR: Invalid INIT_RXQ from %s: rxq %d evq %d "
587*4882a593Smuzhiyun 				  "buf_count %d\n", vf->pci_name, vf_rxq,
588*4882a593Smuzhiyun 				  vf_evq, buf_count);
589*4882a593Smuzhiyun 		return VFDI_RC_EINVAL;
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 	if (__test_and_set_bit(req->u.init_rxq.index, vf->rxq_mask))
592*4882a593Smuzhiyun 		++vf->rxq_count;
593*4882a593Smuzhiyun 	efx_siena_sriov_bufs(efx, buftbl, req->u.init_rxq.addr, buf_count);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	label = req->u.init_rxq.label & EFX_FIELD_MASK(FRF_AZ_RX_DESCQ_LABEL);
596*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_6(reg,
597*4882a593Smuzhiyun 			     FRF_AZ_RX_DESCQ_BUF_BASE_ID, buftbl,
598*4882a593Smuzhiyun 			     FRF_AZ_RX_DESCQ_EVQ_ID, abs_index(vf, vf_evq),
599*4882a593Smuzhiyun 			     FRF_AZ_RX_DESCQ_LABEL, label,
600*4882a593Smuzhiyun 			     FRF_AZ_RX_DESCQ_SIZE, __ffs(buf_count),
601*4882a593Smuzhiyun 			     FRF_AZ_RX_DESCQ_JUMBO,
602*4882a593Smuzhiyun 			     !!(req->u.init_rxq.flags &
603*4882a593Smuzhiyun 				VFDI_RXQ_FLAG_SCATTER_EN),
604*4882a593Smuzhiyun 			     FRF_AZ_RX_DESCQ_EN, 1);
605*4882a593Smuzhiyun 	efx_writeo_table(efx, &reg, FR_BZ_RX_DESC_PTR_TBL,
606*4882a593Smuzhiyun 			 abs_index(vf, vf_rxq));
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	return VFDI_RC_SUCCESS;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun 
efx_vfdi_init_txq(struct siena_vf * vf)611*4882a593Smuzhiyun static int efx_vfdi_init_txq(struct siena_vf *vf)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
614*4882a593Smuzhiyun 	struct vfdi_req *req = vf->buf.addr;
615*4882a593Smuzhiyun 	unsigned vf_txq = req->u.init_txq.index;
616*4882a593Smuzhiyun 	unsigned vf_evq = req->u.init_txq.evq;
617*4882a593Smuzhiyun 	unsigned buf_count = req->u.init_txq.buf_count;
618*4882a593Smuzhiyun 	unsigned buftbl = EFX_BUFTBL_TXQ_BASE(vf, vf_txq);
619*4882a593Smuzhiyun 	unsigned label, eth_filt_en;
620*4882a593Smuzhiyun 	efx_oword_t reg;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	if (bad_vf_index(efx, vf_evq) || bad_vf_index(efx, vf_txq) ||
623*4882a593Smuzhiyun 	    vf_txq >= vf_max_tx_channels ||
624*4882a593Smuzhiyun 	    bad_buf_count(buf_count, EFX_MAX_DMAQ_SIZE)) {
625*4882a593Smuzhiyun 		if (net_ratelimit())
626*4882a593Smuzhiyun 			netif_err(efx, hw, efx->net_dev,
627*4882a593Smuzhiyun 				  "ERROR: Invalid INIT_TXQ from %s: txq %d evq %d "
628*4882a593Smuzhiyun 				  "buf_count %d\n", vf->pci_name, vf_txq,
629*4882a593Smuzhiyun 				  vf_evq, buf_count);
630*4882a593Smuzhiyun 		return VFDI_RC_EINVAL;
631*4882a593Smuzhiyun 	}
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	mutex_lock(&vf->txq_lock);
634*4882a593Smuzhiyun 	if (__test_and_set_bit(req->u.init_txq.index, vf->txq_mask))
635*4882a593Smuzhiyun 		++vf->txq_count;
636*4882a593Smuzhiyun 	mutex_unlock(&vf->txq_lock);
637*4882a593Smuzhiyun 	efx_siena_sriov_bufs(efx, buftbl, req->u.init_txq.addr, buf_count);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	eth_filt_en = vf->tx_filter_mode == VF_TX_FILTER_ON;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	label = req->u.init_txq.label & EFX_FIELD_MASK(FRF_AZ_TX_DESCQ_LABEL);
642*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_8(reg,
643*4882a593Smuzhiyun 			     FRF_CZ_TX_DPT_Q_MASK_WIDTH, min(efx->vi_scale, 1U),
644*4882a593Smuzhiyun 			     FRF_CZ_TX_DPT_ETH_FILT_EN, eth_filt_en,
645*4882a593Smuzhiyun 			     FRF_AZ_TX_DESCQ_EN, 1,
646*4882a593Smuzhiyun 			     FRF_AZ_TX_DESCQ_BUF_BASE_ID, buftbl,
647*4882a593Smuzhiyun 			     FRF_AZ_TX_DESCQ_EVQ_ID, abs_index(vf, vf_evq),
648*4882a593Smuzhiyun 			     FRF_AZ_TX_DESCQ_LABEL, label,
649*4882a593Smuzhiyun 			     FRF_AZ_TX_DESCQ_SIZE, __ffs(buf_count),
650*4882a593Smuzhiyun 			     FRF_BZ_TX_NON_IP_DROP_DIS, 1);
651*4882a593Smuzhiyun 	efx_writeo_table(efx, &reg, FR_BZ_TX_DESC_PTR_TBL,
652*4882a593Smuzhiyun 			 abs_index(vf, vf_txq));
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	return VFDI_RC_SUCCESS;
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun /* Returns true when efx_vfdi_fini_all_queues should wake */
efx_vfdi_flush_wake(struct siena_vf * vf)658*4882a593Smuzhiyun static bool efx_vfdi_flush_wake(struct siena_vf *vf)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	/* Ensure that all updates are visible to efx_vfdi_fini_all_queues() */
661*4882a593Smuzhiyun 	smp_mb();
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	return (!vf->txq_count && !vf->rxq_count) ||
664*4882a593Smuzhiyun 		atomic_read(&vf->rxq_retry_count);
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun 
efx_vfdi_flush_clear(struct siena_vf * vf)667*4882a593Smuzhiyun static void efx_vfdi_flush_clear(struct siena_vf *vf)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun 	memset(vf->txq_mask, 0, sizeof(vf->txq_mask));
670*4882a593Smuzhiyun 	vf->txq_count = 0;
671*4882a593Smuzhiyun 	memset(vf->rxq_mask, 0, sizeof(vf->rxq_mask));
672*4882a593Smuzhiyun 	vf->rxq_count = 0;
673*4882a593Smuzhiyun 	memset(vf->rxq_retry_mask, 0, sizeof(vf->rxq_retry_mask));
674*4882a593Smuzhiyun 	atomic_set(&vf->rxq_retry_count, 0);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun 
efx_vfdi_fini_all_queues(struct siena_vf * vf)677*4882a593Smuzhiyun static int efx_vfdi_fini_all_queues(struct siena_vf *vf)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
680*4882a593Smuzhiyun 	efx_oword_t reg;
681*4882a593Smuzhiyun 	unsigned count = efx_vf_size(efx);
682*4882a593Smuzhiyun 	unsigned vf_offset = EFX_VI_BASE + vf->index * efx_vf_size(efx);
683*4882a593Smuzhiyun 	unsigned timeout = HZ;
684*4882a593Smuzhiyun 	unsigned index, rxqs_count;
685*4882a593Smuzhiyun 	MCDI_DECLARE_BUF(inbuf, MC_CMD_FLUSH_RX_QUEUES_IN_LENMAX);
686*4882a593Smuzhiyun 	int rc;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	BUILD_BUG_ON(VF_MAX_RX_QUEUES >
689*4882a593Smuzhiyun 		     MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	rtnl_lock();
692*4882a593Smuzhiyun 	siena_prepare_flush(efx);
693*4882a593Smuzhiyun 	rtnl_unlock();
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	/* Flush all the initialized queues */
696*4882a593Smuzhiyun 	rxqs_count = 0;
697*4882a593Smuzhiyun 	for (index = 0; index < count; ++index) {
698*4882a593Smuzhiyun 		if (test_bit(index, vf->txq_mask)) {
699*4882a593Smuzhiyun 			EFX_POPULATE_OWORD_2(reg,
700*4882a593Smuzhiyun 					     FRF_AZ_TX_FLUSH_DESCQ_CMD, 1,
701*4882a593Smuzhiyun 					     FRF_AZ_TX_FLUSH_DESCQ,
702*4882a593Smuzhiyun 					     vf_offset + index);
703*4882a593Smuzhiyun 			efx_writeo(efx, &reg, FR_AZ_TX_FLUSH_DESCQ);
704*4882a593Smuzhiyun 		}
705*4882a593Smuzhiyun 		if (test_bit(index, vf->rxq_mask)) {
706*4882a593Smuzhiyun 			MCDI_SET_ARRAY_DWORD(
707*4882a593Smuzhiyun 				inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
708*4882a593Smuzhiyun 				rxqs_count, vf_offset + index);
709*4882a593Smuzhiyun 			rxqs_count++;
710*4882a593Smuzhiyun 		}
711*4882a593Smuzhiyun 	}
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	atomic_set(&vf->rxq_retry_count, 0);
714*4882a593Smuzhiyun 	while (timeout && (vf->rxq_count || vf->txq_count)) {
715*4882a593Smuzhiyun 		rc = efx_mcdi_rpc(efx, MC_CMD_FLUSH_RX_QUEUES, inbuf,
716*4882a593Smuzhiyun 				  MC_CMD_FLUSH_RX_QUEUES_IN_LEN(rxqs_count),
717*4882a593Smuzhiyun 				  NULL, 0, NULL);
718*4882a593Smuzhiyun 		WARN_ON(rc < 0);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 		timeout = wait_event_timeout(vf->flush_waitq,
721*4882a593Smuzhiyun 					     efx_vfdi_flush_wake(vf),
722*4882a593Smuzhiyun 					     timeout);
723*4882a593Smuzhiyun 		rxqs_count = 0;
724*4882a593Smuzhiyun 		for (index = 0; index < count; ++index) {
725*4882a593Smuzhiyun 			if (test_and_clear_bit(index, vf->rxq_retry_mask)) {
726*4882a593Smuzhiyun 				atomic_dec(&vf->rxq_retry_count);
727*4882a593Smuzhiyun 				MCDI_SET_ARRAY_DWORD(
728*4882a593Smuzhiyun 					inbuf, FLUSH_RX_QUEUES_IN_QID_OFST,
729*4882a593Smuzhiyun 					rxqs_count, vf_offset + index);
730*4882a593Smuzhiyun 				rxqs_count++;
731*4882a593Smuzhiyun 			}
732*4882a593Smuzhiyun 		}
733*4882a593Smuzhiyun 	}
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	rtnl_lock();
736*4882a593Smuzhiyun 	siena_finish_flush(efx);
737*4882a593Smuzhiyun 	rtnl_unlock();
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	/* Irrespective of success/failure, fini the queues */
740*4882a593Smuzhiyun 	EFX_ZERO_OWORD(reg);
741*4882a593Smuzhiyun 	for (index = 0; index < count; ++index) {
742*4882a593Smuzhiyun 		efx_writeo_table(efx, &reg, FR_BZ_RX_DESC_PTR_TBL,
743*4882a593Smuzhiyun 				 vf_offset + index);
744*4882a593Smuzhiyun 		efx_writeo_table(efx, &reg, FR_BZ_TX_DESC_PTR_TBL,
745*4882a593Smuzhiyun 				 vf_offset + index);
746*4882a593Smuzhiyun 		efx_writeo_table(efx, &reg, FR_BZ_EVQ_PTR_TBL,
747*4882a593Smuzhiyun 				 vf_offset + index);
748*4882a593Smuzhiyun 		efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL,
749*4882a593Smuzhiyun 				 vf_offset + index);
750*4882a593Smuzhiyun 	}
751*4882a593Smuzhiyun 	efx_siena_sriov_bufs(efx, vf->buftbl_base, NULL,
752*4882a593Smuzhiyun 			     EFX_VF_BUFTBL_PER_VI * efx_vf_size(efx));
753*4882a593Smuzhiyun 	efx_vfdi_flush_clear(vf);
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	vf->evq0_count = 0;
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	return timeout ? 0 : VFDI_RC_ETIMEDOUT;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun 
efx_vfdi_insert_filter(struct siena_vf * vf)760*4882a593Smuzhiyun static int efx_vfdi_insert_filter(struct siena_vf *vf)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
763*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
764*4882a593Smuzhiyun 	struct vfdi_req *req = vf->buf.addr;
765*4882a593Smuzhiyun 	unsigned vf_rxq = req->u.mac_filter.rxq;
766*4882a593Smuzhiyun 	unsigned flags;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	if (bad_vf_index(efx, vf_rxq) || vf->rx_filtering) {
769*4882a593Smuzhiyun 		if (net_ratelimit())
770*4882a593Smuzhiyun 			netif_err(efx, hw, efx->net_dev,
771*4882a593Smuzhiyun 				  "ERROR: Invalid INSERT_FILTER from %s: rxq %d "
772*4882a593Smuzhiyun 				  "flags 0x%x\n", vf->pci_name, vf_rxq,
773*4882a593Smuzhiyun 				  req->u.mac_filter.flags);
774*4882a593Smuzhiyun 		return VFDI_RC_EINVAL;
775*4882a593Smuzhiyun 	}
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	flags = 0;
778*4882a593Smuzhiyun 	if (req->u.mac_filter.flags & VFDI_MAC_FILTER_FLAG_RSS)
779*4882a593Smuzhiyun 		flags |= EFX_FILTER_FLAG_RX_RSS;
780*4882a593Smuzhiyun 	if (req->u.mac_filter.flags & VFDI_MAC_FILTER_FLAG_SCATTER)
781*4882a593Smuzhiyun 		flags |= EFX_FILTER_FLAG_RX_SCATTER;
782*4882a593Smuzhiyun 	vf->rx_filter_flags = flags;
783*4882a593Smuzhiyun 	vf->rx_filter_qid = vf_rxq;
784*4882a593Smuzhiyun 	vf->rx_filtering = true;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	efx_siena_sriov_reset_rx_filter(vf);
787*4882a593Smuzhiyun 	queue_work(vfdi_workqueue, &nic_data->peer_work);
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	return VFDI_RC_SUCCESS;
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun 
efx_vfdi_remove_all_filters(struct siena_vf * vf)792*4882a593Smuzhiyun static int efx_vfdi_remove_all_filters(struct siena_vf *vf)
793*4882a593Smuzhiyun {
794*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
795*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	vf->rx_filtering = false;
798*4882a593Smuzhiyun 	efx_siena_sriov_reset_rx_filter(vf);
799*4882a593Smuzhiyun 	queue_work(vfdi_workqueue, &nic_data->peer_work);
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	return VFDI_RC_SUCCESS;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun 
efx_vfdi_set_status_page(struct siena_vf * vf)804*4882a593Smuzhiyun static int efx_vfdi_set_status_page(struct siena_vf *vf)
805*4882a593Smuzhiyun {
806*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
807*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
808*4882a593Smuzhiyun 	struct vfdi_req *req = vf->buf.addr;
809*4882a593Smuzhiyun 	u64 page_count = req->u.set_status_page.peer_page_count;
810*4882a593Smuzhiyun 	u64 max_page_count =
811*4882a593Smuzhiyun 		(EFX_PAGE_SIZE -
812*4882a593Smuzhiyun 		 offsetof(struct vfdi_req, u.set_status_page.peer_page_addr[0]))
813*4882a593Smuzhiyun 		/ sizeof(req->u.set_status_page.peer_page_addr[0]);
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	if (!req->u.set_status_page.dma_addr || page_count > max_page_count) {
816*4882a593Smuzhiyun 		if (net_ratelimit())
817*4882a593Smuzhiyun 			netif_err(efx, hw, efx->net_dev,
818*4882a593Smuzhiyun 				  "ERROR: Invalid SET_STATUS_PAGE from %s\n",
819*4882a593Smuzhiyun 				  vf->pci_name);
820*4882a593Smuzhiyun 		return VFDI_RC_EINVAL;
821*4882a593Smuzhiyun 	}
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	mutex_lock(&nic_data->local_lock);
824*4882a593Smuzhiyun 	mutex_lock(&vf->status_lock);
825*4882a593Smuzhiyun 	vf->status_addr = req->u.set_status_page.dma_addr;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	kfree(vf->peer_page_addrs);
828*4882a593Smuzhiyun 	vf->peer_page_addrs = NULL;
829*4882a593Smuzhiyun 	vf->peer_page_count = 0;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	if (page_count) {
832*4882a593Smuzhiyun 		vf->peer_page_addrs = kcalloc(page_count, sizeof(u64),
833*4882a593Smuzhiyun 					      GFP_KERNEL);
834*4882a593Smuzhiyun 		if (vf->peer_page_addrs) {
835*4882a593Smuzhiyun 			memcpy(vf->peer_page_addrs,
836*4882a593Smuzhiyun 			       req->u.set_status_page.peer_page_addr,
837*4882a593Smuzhiyun 			       page_count * sizeof(u64));
838*4882a593Smuzhiyun 			vf->peer_page_count = page_count;
839*4882a593Smuzhiyun 		}
840*4882a593Smuzhiyun 	}
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	__efx_siena_sriov_push_vf_status(vf);
843*4882a593Smuzhiyun 	mutex_unlock(&vf->status_lock);
844*4882a593Smuzhiyun 	mutex_unlock(&nic_data->local_lock);
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	return VFDI_RC_SUCCESS;
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun 
efx_vfdi_clear_status_page(struct siena_vf * vf)849*4882a593Smuzhiyun static int efx_vfdi_clear_status_page(struct siena_vf *vf)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun 	mutex_lock(&vf->status_lock);
852*4882a593Smuzhiyun 	vf->status_addr = 0;
853*4882a593Smuzhiyun 	mutex_unlock(&vf->status_lock);
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	return VFDI_RC_SUCCESS;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun typedef int (*efx_vfdi_op_t)(struct siena_vf *vf);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun static const efx_vfdi_op_t vfdi_ops[VFDI_OP_LIMIT] = {
861*4882a593Smuzhiyun 	[VFDI_OP_INIT_EVQ] = efx_vfdi_init_evq,
862*4882a593Smuzhiyun 	[VFDI_OP_INIT_TXQ] = efx_vfdi_init_txq,
863*4882a593Smuzhiyun 	[VFDI_OP_INIT_RXQ] = efx_vfdi_init_rxq,
864*4882a593Smuzhiyun 	[VFDI_OP_FINI_ALL_QUEUES] = efx_vfdi_fini_all_queues,
865*4882a593Smuzhiyun 	[VFDI_OP_INSERT_FILTER] = efx_vfdi_insert_filter,
866*4882a593Smuzhiyun 	[VFDI_OP_REMOVE_ALL_FILTERS] = efx_vfdi_remove_all_filters,
867*4882a593Smuzhiyun 	[VFDI_OP_SET_STATUS_PAGE] = efx_vfdi_set_status_page,
868*4882a593Smuzhiyun 	[VFDI_OP_CLEAR_STATUS_PAGE] = efx_vfdi_clear_status_page,
869*4882a593Smuzhiyun };
870*4882a593Smuzhiyun 
efx_siena_sriov_vfdi(struct work_struct * work)871*4882a593Smuzhiyun static void efx_siena_sriov_vfdi(struct work_struct *work)
872*4882a593Smuzhiyun {
873*4882a593Smuzhiyun 	struct siena_vf *vf = container_of(work, struct siena_vf, req);
874*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
875*4882a593Smuzhiyun 	struct vfdi_req *req = vf->buf.addr;
876*4882a593Smuzhiyun 	struct efx_memcpy_req copy[2];
877*4882a593Smuzhiyun 	int rc;
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	/* Copy this page into the local address space */
880*4882a593Smuzhiyun 	memset(copy, '\0', sizeof(copy));
881*4882a593Smuzhiyun 	copy[0].from_rid = vf->pci_rid;
882*4882a593Smuzhiyun 	copy[0].from_addr = vf->req_addr;
883*4882a593Smuzhiyun 	copy[0].to_rid = efx->pci_dev->devfn;
884*4882a593Smuzhiyun 	copy[0].to_addr = vf->buf.dma_addr;
885*4882a593Smuzhiyun 	copy[0].length = EFX_PAGE_SIZE;
886*4882a593Smuzhiyun 	rc = efx_siena_sriov_memcpy(efx, copy, 1);
887*4882a593Smuzhiyun 	if (rc) {
888*4882a593Smuzhiyun 		/* If we can't get the request, we can't reply to the caller */
889*4882a593Smuzhiyun 		if (net_ratelimit())
890*4882a593Smuzhiyun 			netif_err(efx, hw, efx->net_dev,
891*4882a593Smuzhiyun 				  "ERROR: Unable to fetch VFDI request from %s rc %d\n",
892*4882a593Smuzhiyun 				  vf->pci_name, -rc);
893*4882a593Smuzhiyun 		vf->busy = false;
894*4882a593Smuzhiyun 		return;
895*4882a593Smuzhiyun 	}
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	if (req->op < VFDI_OP_LIMIT && vfdi_ops[req->op] != NULL) {
898*4882a593Smuzhiyun 		rc = vfdi_ops[req->op](vf);
899*4882a593Smuzhiyun 		if (rc == 0) {
900*4882a593Smuzhiyun 			netif_dbg(efx, hw, efx->net_dev,
901*4882a593Smuzhiyun 				  "vfdi request %d from %s ok\n",
902*4882a593Smuzhiyun 				  req->op, vf->pci_name);
903*4882a593Smuzhiyun 		}
904*4882a593Smuzhiyun 	} else {
905*4882a593Smuzhiyun 		netif_dbg(efx, hw, efx->net_dev,
906*4882a593Smuzhiyun 			  "ERROR: Unrecognised request %d from VF %s addr "
907*4882a593Smuzhiyun 			  "%llx\n", req->op, vf->pci_name,
908*4882a593Smuzhiyun 			  (unsigned long long)vf->req_addr);
909*4882a593Smuzhiyun 		rc = VFDI_RC_EOPNOTSUPP;
910*4882a593Smuzhiyun 	}
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	/* Allow subsequent VF requests */
913*4882a593Smuzhiyun 	vf->busy = false;
914*4882a593Smuzhiyun 	smp_wmb();
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	/* Respond to the request */
917*4882a593Smuzhiyun 	req->rc = rc;
918*4882a593Smuzhiyun 	req->op = VFDI_OP_RESPONSE;
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	memset(copy, '\0', sizeof(copy));
921*4882a593Smuzhiyun 	copy[0].from_buf = &req->rc;
922*4882a593Smuzhiyun 	copy[0].to_rid = vf->pci_rid;
923*4882a593Smuzhiyun 	copy[0].to_addr = vf->req_addr + offsetof(struct vfdi_req, rc);
924*4882a593Smuzhiyun 	copy[0].length = sizeof(req->rc);
925*4882a593Smuzhiyun 	copy[1].from_buf = &req->op;
926*4882a593Smuzhiyun 	copy[1].to_rid = vf->pci_rid;
927*4882a593Smuzhiyun 	copy[1].to_addr = vf->req_addr + offsetof(struct vfdi_req, op);
928*4882a593Smuzhiyun 	copy[1].length = sizeof(req->op);
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	(void)efx_siena_sriov_memcpy(efx, copy, ARRAY_SIZE(copy));
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun /* After a reset the event queues inside the guests no longer exist. Fill the
936*4882a593Smuzhiyun  * event ring in guest memory with VFDI reset events, then (re-initialise) the
937*4882a593Smuzhiyun  * event queue to raise an interrupt. The guest driver will then recover.
938*4882a593Smuzhiyun  */
939*4882a593Smuzhiyun 
efx_siena_sriov_reset_vf(struct siena_vf * vf,struct efx_buffer * buffer)940*4882a593Smuzhiyun static void efx_siena_sriov_reset_vf(struct siena_vf *vf,
941*4882a593Smuzhiyun 				     struct efx_buffer *buffer)
942*4882a593Smuzhiyun {
943*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
944*4882a593Smuzhiyun 	struct efx_memcpy_req copy_req[4];
945*4882a593Smuzhiyun 	efx_qword_t event;
946*4882a593Smuzhiyun 	unsigned int pos, count, k, buftbl, abs_evq;
947*4882a593Smuzhiyun 	efx_oword_t reg;
948*4882a593Smuzhiyun 	efx_dword_t ptr;
949*4882a593Smuzhiyun 	int rc;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	BUG_ON(buffer->len != EFX_PAGE_SIZE);
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	if (!vf->evq0_count)
954*4882a593Smuzhiyun 		return;
955*4882a593Smuzhiyun 	BUG_ON(vf->evq0_count & (vf->evq0_count - 1));
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	mutex_lock(&vf->status_lock);
958*4882a593Smuzhiyun 	EFX_POPULATE_QWORD_3(event,
959*4882a593Smuzhiyun 			     FSF_AZ_EV_CODE, FSE_CZ_EV_CODE_USER_EV,
960*4882a593Smuzhiyun 			     VFDI_EV_SEQ, vf->msg_seqno,
961*4882a593Smuzhiyun 			     VFDI_EV_TYPE, VFDI_EV_TYPE_RESET);
962*4882a593Smuzhiyun 	vf->msg_seqno++;
963*4882a593Smuzhiyun 	for (pos = 0; pos < EFX_PAGE_SIZE; pos += sizeof(event))
964*4882a593Smuzhiyun 		memcpy(buffer->addr + pos, &event, sizeof(event));
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	for (pos = 0; pos < vf->evq0_count; pos += count) {
967*4882a593Smuzhiyun 		count = min_t(unsigned, vf->evq0_count - pos,
968*4882a593Smuzhiyun 			      ARRAY_SIZE(copy_req));
969*4882a593Smuzhiyun 		for (k = 0; k < count; k++) {
970*4882a593Smuzhiyun 			copy_req[k].from_buf = NULL;
971*4882a593Smuzhiyun 			copy_req[k].from_rid = efx->pci_dev->devfn;
972*4882a593Smuzhiyun 			copy_req[k].from_addr = buffer->dma_addr;
973*4882a593Smuzhiyun 			copy_req[k].to_rid = vf->pci_rid;
974*4882a593Smuzhiyun 			copy_req[k].to_addr = vf->evq0_addrs[pos + k];
975*4882a593Smuzhiyun 			copy_req[k].length = EFX_PAGE_SIZE;
976*4882a593Smuzhiyun 		}
977*4882a593Smuzhiyun 		rc = efx_siena_sriov_memcpy(efx, copy_req, count);
978*4882a593Smuzhiyun 		if (rc) {
979*4882a593Smuzhiyun 			if (net_ratelimit())
980*4882a593Smuzhiyun 				netif_err(efx, hw, efx->net_dev,
981*4882a593Smuzhiyun 					  "ERROR: Unable to notify %s of reset"
982*4882a593Smuzhiyun 					  ": %d\n", vf->pci_name, -rc);
983*4882a593Smuzhiyun 			break;
984*4882a593Smuzhiyun 		}
985*4882a593Smuzhiyun 	}
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	/* Reinitialise, arm and trigger evq0 */
988*4882a593Smuzhiyun 	abs_evq = abs_index(vf, 0);
989*4882a593Smuzhiyun 	buftbl = EFX_BUFTBL_EVQ_BASE(vf, 0);
990*4882a593Smuzhiyun 	efx_siena_sriov_bufs(efx, buftbl, vf->evq0_addrs, vf->evq0_count);
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_3(reg,
993*4882a593Smuzhiyun 			     FRF_CZ_TIMER_Q_EN, 1,
994*4882a593Smuzhiyun 			     FRF_CZ_HOST_NOTIFY_MODE, 0,
995*4882a593Smuzhiyun 			     FRF_CZ_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS);
996*4882a593Smuzhiyun 	efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, abs_evq);
997*4882a593Smuzhiyun 	EFX_POPULATE_OWORD_3(reg,
998*4882a593Smuzhiyun 			     FRF_AZ_EVQ_EN, 1,
999*4882a593Smuzhiyun 			     FRF_AZ_EVQ_SIZE, __ffs(vf->evq0_count),
1000*4882a593Smuzhiyun 			     FRF_AZ_EVQ_BUF_BASE_ID, buftbl);
1001*4882a593Smuzhiyun 	efx_writeo_table(efx, &reg, FR_BZ_EVQ_PTR_TBL, abs_evq);
1002*4882a593Smuzhiyun 	EFX_POPULATE_DWORD_1(ptr, FRF_AZ_EVQ_RPTR, 0);
1003*4882a593Smuzhiyun 	efx_writed(efx, &ptr, FR_BZ_EVQ_RPTR + FR_BZ_EVQ_RPTR_STEP * abs_evq);
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun 	mutex_unlock(&vf->status_lock);
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun 
efx_siena_sriov_reset_vf_work(struct work_struct * work)1008*4882a593Smuzhiyun static void efx_siena_sriov_reset_vf_work(struct work_struct *work)
1009*4882a593Smuzhiyun {
1010*4882a593Smuzhiyun 	struct siena_vf *vf = container_of(work, struct siena_vf, req);
1011*4882a593Smuzhiyun 	struct efx_nic *efx = vf->efx;
1012*4882a593Smuzhiyun 	struct efx_buffer buf;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	if (!efx_nic_alloc_buffer(efx, &buf, EFX_PAGE_SIZE, GFP_NOIO)) {
1015*4882a593Smuzhiyun 		efx_siena_sriov_reset_vf(vf, &buf);
1016*4882a593Smuzhiyun 		efx_nic_free_buffer(efx, &buf);
1017*4882a593Smuzhiyun 	}
1018*4882a593Smuzhiyun }
1019*4882a593Smuzhiyun 
efx_siena_sriov_handle_no_channel(struct efx_nic * efx)1020*4882a593Smuzhiyun static void efx_siena_sriov_handle_no_channel(struct efx_nic *efx)
1021*4882a593Smuzhiyun {
1022*4882a593Smuzhiyun 	netif_err(efx, drv, efx->net_dev,
1023*4882a593Smuzhiyun 		  "ERROR: IOV requires MSI-X and 1 additional interrupt"
1024*4882a593Smuzhiyun 		  "vector. IOV disabled\n");
1025*4882a593Smuzhiyun 	efx->vf_count = 0;
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun 
efx_siena_sriov_probe_channel(struct efx_channel * channel)1028*4882a593Smuzhiyun static int efx_siena_sriov_probe_channel(struct efx_channel *channel)
1029*4882a593Smuzhiyun {
1030*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = channel->efx->nic_data;
1031*4882a593Smuzhiyun 	nic_data->vfdi_channel = channel;
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	return 0;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun static void
efx_siena_sriov_get_channel_name(struct efx_channel * channel,char * buf,size_t len)1037*4882a593Smuzhiyun efx_siena_sriov_get_channel_name(struct efx_channel *channel,
1038*4882a593Smuzhiyun 				 char *buf, size_t len)
1039*4882a593Smuzhiyun {
1040*4882a593Smuzhiyun 	snprintf(buf, len, "%s-iov", channel->efx->name);
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun static const struct efx_channel_type efx_siena_sriov_channel_type = {
1044*4882a593Smuzhiyun 	.handle_no_channel	= efx_siena_sriov_handle_no_channel,
1045*4882a593Smuzhiyun 	.pre_probe		= efx_siena_sriov_probe_channel,
1046*4882a593Smuzhiyun 	.post_remove		= efx_channel_dummy_op_void,
1047*4882a593Smuzhiyun 	.get_name		= efx_siena_sriov_get_channel_name,
1048*4882a593Smuzhiyun 	/* no copy operation; channel must not be reallocated */
1049*4882a593Smuzhiyun 	.keep_eventq		= true,
1050*4882a593Smuzhiyun };
1051*4882a593Smuzhiyun 
efx_siena_sriov_probe(struct efx_nic * efx)1052*4882a593Smuzhiyun void efx_siena_sriov_probe(struct efx_nic *efx)
1053*4882a593Smuzhiyun {
1054*4882a593Smuzhiyun 	unsigned count;
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	if (!max_vfs)
1057*4882a593Smuzhiyun 		return;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	if (efx_siena_sriov_cmd(efx, false, &efx->vi_scale, &count)) {
1060*4882a593Smuzhiyun 		pci_info(efx->pci_dev, "no SR-IOV VFs probed\n");
1061*4882a593Smuzhiyun 		return;
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun 	if (count > 0 && count > max_vfs)
1064*4882a593Smuzhiyun 		count = max_vfs;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	/* efx_nic_dimension_resources() will reduce vf_count as appopriate */
1067*4882a593Smuzhiyun 	efx->vf_count = count;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	efx->extra_channel_type[EFX_EXTRA_CHANNEL_IOV] = &efx_siena_sriov_channel_type;
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun /* Copy the list of individual addresses into the vfdi_status.peers
1073*4882a593Smuzhiyun  * array and auxiliary pages, protected by %local_lock. Drop that lock
1074*4882a593Smuzhiyun  * and then broadcast the address list to every VF.
1075*4882a593Smuzhiyun  */
efx_siena_sriov_peer_work(struct work_struct * data)1076*4882a593Smuzhiyun static void efx_siena_sriov_peer_work(struct work_struct *data)
1077*4882a593Smuzhiyun {
1078*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = container_of(data,
1079*4882a593Smuzhiyun 						       struct siena_nic_data,
1080*4882a593Smuzhiyun 						       peer_work);
1081*4882a593Smuzhiyun 	struct efx_nic *efx = nic_data->efx;
1082*4882a593Smuzhiyun 	struct vfdi_status *vfdi_status = nic_data->vfdi_status.addr;
1083*4882a593Smuzhiyun 	struct siena_vf *vf;
1084*4882a593Smuzhiyun 	struct efx_local_addr *local_addr;
1085*4882a593Smuzhiyun 	struct vfdi_endpoint *peer;
1086*4882a593Smuzhiyun 	struct efx_endpoint_page *epp;
1087*4882a593Smuzhiyun 	struct list_head pages;
1088*4882a593Smuzhiyun 	unsigned int peer_space;
1089*4882a593Smuzhiyun 	unsigned int peer_count;
1090*4882a593Smuzhiyun 	unsigned int pos;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	mutex_lock(&nic_data->local_lock);
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	/* Move the existing peer pages off %local_page_list */
1095*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pages);
1096*4882a593Smuzhiyun 	list_splice_tail_init(&nic_data->local_page_list, &pages);
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	/* Populate the VF addresses starting from entry 1 (entry 0 is
1099*4882a593Smuzhiyun 	 * the PF address)
1100*4882a593Smuzhiyun 	 */
1101*4882a593Smuzhiyun 	peer = vfdi_status->peers + 1;
1102*4882a593Smuzhiyun 	peer_space = ARRAY_SIZE(vfdi_status->peers) - 1;
1103*4882a593Smuzhiyun 	peer_count = 1;
1104*4882a593Smuzhiyun 	for (pos = 0; pos < efx->vf_count; ++pos) {
1105*4882a593Smuzhiyun 		vf = nic_data->vf + pos;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 		mutex_lock(&vf->status_lock);
1108*4882a593Smuzhiyun 		if (vf->rx_filtering && !is_zero_ether_addr(vf->addr.mac_addr)) {
1109*4882a593Smuzhiyun 			*peer++ = vf->addr;
1110*4882a593Smuzhiyun 			++peer_count;
1111*4882a593Smuzhiyun 			--peer_space;
1112*4882a593Smuzhiyun 			BUG_ON(peer_space == 0);
1113*4882a593Smuzhiyun 		}
1114*4882a593Smuzhiyun 		mutex_unlock(&vf->status_lock);
1115*4882a593Smuzhiyun 	}
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	/* Fill the remaining addresses */
1118*4882a593Smuzhiyun 	list_for_each_entry(local_addr, &nic_data->local_addr_list, link) {
1119*4882a593Smuzhiyun 		ether_addr_copy(peer->mac_addr, local_addr->addr);
1120*4882a593Smuzhiyun 		peer->tci = 0;
1121*4882a593Smuzhiyun 		++peer;
1122*4882a593Smuzhiyun 		++peer_count;
1123*4882a593Smuzhiyun 		if (--peer_space == 0) {
1124*4882a593Smuzhiyun 			if (list_empty(&pages)) {
1125*4882a593Smuzhiyun 				epp = kmalloc(sizeof(*epp), GFP_KERNEL);
1126*4882a593Smuzhiyun 				if (!epp)
1127*4882a593Smuzhiyun 					break;
1128*4882a593Smuzhiyun 				epp->ptr = dma_alloc_coherent(
1129*4882a593Smuzhiyun 					&efx->pci_dev->dev, EFX_PAGE_SIZE,
1130*4882a593Smuzhiyun 					&epp->addr, GFP_KERNEL);
1131*4882a593Smuzhiyun 				if (!epp->ptr) {
1132*4882a593Smuzhiyun 					kfree(epp);
1133*4882a593Smuzhiyun 					break;
1134*4882a593Smuzhiyun 				}
1135*4882a593Smuzhiyun 			} else {
1136*4882a593Smuzhiyun 				epp = list_first_entry(
1137*4882a593Smuzhiyun 					&pages, struct efx_endpoint_page, link);
1138*4882a593Smuzhiyun 				list_del(&epp->link);
1139*4882a593Smuzhiyun 			}
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 			list_add_tail(&epp->link, &nic_data->local_page_list);
1142*4882a593Smuzhiyun 			peer = (struct vfdi_endpoint *)epp->ptr;
1143*4882a593Smuzhiyun 			peer_space = EFX_PAGE_SIZE / sizeof(struct vfdi_endpoint);
1144*4882a593Smuzhiyun 		}
1145*4882a593Smuzhiyun 	}
1146*4882a593Smuzhiyun 	vfdi_status->peer_count = peer_count;
1147*4882a593Smuzhiyun 	mutex_unlock(&nic_data->local_lock);
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 	/* Free any now unused endpoint pages */
1150*4882a593Smuzhiyun 	while (!list_empty(&pages)) {
1151*4882a593Smuzhiyun 		epp = list_first_entry(
1152*4882a593Smuzhiyun 			&pages, struct efx_endpoint_page, link);
1153*4882a593Smuzhiyun 		list_del(&epp->link);
1154*4882a593Smuzhiyun 		dma_free_coherent(&efx->pci_dev->dev, EFX_PAGE_SIZE,
1155*4882a593Smuzhiyun 				  epp->ptr, epp->addr);
1156*4882a593Smuzhiyun 		kfree(epp);
1157*4882a593Smuzhiyun 	}
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	/* Finally, push the pages */
1160*4882a593Smuzhiyun 	for (pos = 0; pos < efx->vf_count; ++pos) {
1161*4882a593Smuzhiyun 		vf = nic_data->vf + pos;
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 		mutex_lock(&vf->status_lock);
1164*4882a593Smuzhiyun 		if (vf->status_addr)
1165*4882a593Smuzhiyun 			__efx_siena_sriov_push_vf_status(vf);
1166*4882a593Smuzhiyun 		mutex_unlock(&vf->status_lock);
1167*4882a593Smuzhiyun 	}
1168*4882a593Smuzhiyun }
1169*4882a593Smuzhiyun 
efx_siena_sriov_free_local(struct efx_nic * efx)1170*4882a593Smuzhiyun static void efx_siena_sriov_free_local(struct efx_nic *efx)
1171*4882a593Smuzhiyun {
1172*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1173*4882a593Smuzhiyun 	struct efx_local_addr *local_addr;
1174*4882a593Smuzhiyun 	struct efx_endpoint_page *epp;
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	while (!list_empty(&nic_data->local_addr_list)) {
1177*4882a593Smuzhiyun 		local_addr = list_first_entry(&nic_data->local_addr_list,
1178*4882a593Smuzhiyun 					      struct efx_local_addr, link);
1179*4882a593Smuzhiyun 		list_del(&local_addr->link);
1180*4882a593Smuzhiyun 		kfree(local_addr);
1181*4882a593Smuzhiyun 	}
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	while (!list_empty(&nic_data->local_page_list)) {
1184*4882a593Smuzhiyun 		epp = list_first_entry(&nic_data->local_page_list,
1185*4882a593Smuzhiyun 				       struct efx_endpoint_page, link);
1186*4882a593Smuzhiyun 		list_del(&epp->link);
1187*4882a593Smuzhiyun 		dma_free_coherent(&efx->pci_dev->dev, EFX_PAGE_SIZE,
1188*4882a593Smuzhiyun 				  epp->ptr, epp->addr);
1189*4882a593Smuzhiyun 		kfree(epp);
1190*4882a593Smuzhiyun 	}
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun 
efx_siena_sriov_vf_alloc(struct efx_nic * efx)1193*4882a593Smuzhiyun static int efx_siena_sriov_vf_alloc(struct efx_nic *efx)
1194*4882a593Smuzhiyun {
1195*4882a593Smuzhiyun 	unsigned index;
1196*4882a593Smuzhiyun 	struct siena_vf *vf;
1197*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	nic_data->vf = kcalloc(efx->vf_count, sizeof(*nic_data->vf),
1200*4882a593Smuzhiyun 			       GFP_KERNEL);
1201*4882a593Smuzhiyun 	if (!nic_data->vf)
1202*4882a593Smuzhiyun 		return -ENOMEM;
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun 	for (index = 0; index < efx->vf_count; ++index) {
1205*4882a593Smuzhiyun 		vf = nic_data->vf + index;
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 		vf->efx = efx;
1208*4882a593Smuzhiyun 		vf->index = index;
1209*4882a593Smuzhiyun 		vf->rx_filter_id = -1;
1210*4882a593Smuzhiyun 		vf->tx_filter_mode = VF_TX_FILTER_AUTO;
1211*4882a593Smuzhiyun 		vf->tx_filter_id = -1;
1212*4882a593Smuzhiyun 		INIT_WORK(&vf->req, efx_siena_sriov_vfdi);
1213*4882a593Smuzhiyun 		INIT_WORK(&vf->reset_work, efx_siena_sriov_reset_vf_work);
1214*4882a593Smuzhiyun 		init_waitqueue_head(&vf->flush_waitq);
1215*4882a593Smuzhiyun 		mutex_init(&vf->status_lock);
1216*4882a593Smuzhiyun 		mutex_init(&vf->txq_lock);
1217*4882a593Smuzhiyun 	}
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	return 0;
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun 
efx_siena_sriov_vfs_fini(struct efx_nic * efx)1222*4882a593Smuzhiyun static void efx_siena_sriov_vfs_fini(struct efx_nic *efx)
1223*4882a593Smuzhiyun {
1224*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1225*4882a593Smuzhiyun 	struct siena_vf *vf;
1226*4882a593Smuzhiyun 	unsigned int pos;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	for (pos = 0; pos < efx->vf_count; ++pos) {
1229*4882a593Smuzhiyun 		vf = nic_data->vf + pos;
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 		efx_nic_free_buffer(efx, &vf->buf);
1232*4882a593Smuzhiyun 		kfree(vf->peer_page_addrs);
1233*4882a593Smuzhiyun 		vf->peer_page_addrs = NULL;
1234*4882a593Smuzhiyun 		vf->peer_page_count = 0;
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 		vf->evq0_count = 0;
1237*4882a593Smuzhiyun 	}
1238*4882a593Smuzhiyun }
1239*4882a593Smuzhiyun 
efx_siena_sriov_vfs_init(struct efx_nic * efx)1240*4882a593Smuzhiyun static int efx_siena_sriov_vfs_init(struct efx_nic *efx)
1241*4882a593Smuzhiyun {
1242*4882a593Smuzhiyun 	struct pci_dev *pci_dev = efx->pci_dev;
1243*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1244*4882a593Smuzhiyun 	unsigned index, devfn, sriov, buftbl_base;
1245*4882a593Smuzhiyun 	u16 offset, stride;
1246*4882a593Smuzhiyun 	struct siena_vf *vf;
1247*4882a593Smuzhiyun 	int rc;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	sriov = pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV);
1250*4882a593Smuzhiyun 	if (!sriov)
1251*4882a593Smuzhiyun 		return -ENOENT;
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	pci_read_config_word(pci_dev, sriov + PCI_SRIOV_VF_OFFSET, &offset);
1254*4882a593Smuzhiyun 	pci_read_config_word(pci_dev, sriov + PCI_SRIOV_VF_STRIDE, &stride);
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	buftbl_base = nic_data->vf_buftbl_base;
1257*4882a593Smuzhiyun 	devfn = pci_dev->devfn + offset;
1258*4882a593Smuzhiyun 	for (index = 0; index < efx->vf_count; ++index) {
1259*4882a593Smuzhiyun 		vf = nic_data->vf + index;
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun 		/* Reserve buffer entries */
1262*4882a593Smuzhiyun 		vf->buftbl_base = buftbl_base;
1263*4882a593Smuzhiyun 		buftbl_base += EFX_VF_BUFTBL_PER_VI * efx_vf_size(efx);
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 		vf->pci_rid = devfn;
1266*4882a593Smuzhiyun 		snprintf(vf->pci_name, sizeof(vf->pci_name),
1267*4882a593Smuzhiyun 			 "%04x:%02x:%02x.%d",
1268*4882a593Smuzhiyun 			 pci_domain_nr(pci_dev->bus), pci_dev->bus->number,
1269*4882a593Smuzhiyun 			 PCI_SLOT(devfn), PCI_FUNC(devfn));
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 		rc = efx_nic_alloc_buffer(efx, &vf->buf, EFX_PAGE_SIZE,
1272*4882a593Smuzhiyun 					  GFP_KERNEL);
1273*4882a593Smuzhiyun 		if (rc)
1274*4882a593Smuzhiyun 			goto fail;
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 		devfn += stride;
1277*4882a593Smuzhiyun 	}
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	return 0;
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun fail:
1282*4882a593Smuzhiyun 	efx_siena_sriov_vfs_fini(efx);
1283*4882a593Smuzhiyun 	return rc;
1284*4882a593Smuzhiyun }
1285*4882a593Smuzhiyun 
efx_siena_sriov_init(struct efx_nic * efx)1286*4882a593Smuzhiyun int efx_siena_sriov_init(struct efx_nic *efx)
1287*4882a593Smuzhiyun {
1288*4882a593Smuzhiyun 	struct net_device *net_dev = efx->net_dev;
1289*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1290*4882a593Smuzhiyun 	struct vfdi_status *vfdi_status;
1291*4882a593Smuzhiyun 	int rc;
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	/* Ensure there's room for vf_channel */
1294*4882a593Smuzhiyun 	BUILD_BUG_ON(EFX_MAX_CHANNELS + 1 >= EFX_VI_BASE);
1295*4882a593Smuzhiyun 	/* Ensure that VI_BASE is aligned on VI_SCALE */
1296*4882a593Smuzhiyun 	BUILD_BUG_ON(EFX_VI_BASE & ((1 << EFX_VI_SCALE_MAX) - 1));
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	if (efx->vf_count == 0)
1299*4882a593Smuzhiyun 		return 0;
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	rc = efx_siena_sriov_cmd(efx, true, NULL, NULL);
1302*4882a593Smuzhiyun 	if (rc)
1303*4882a593Smuzhiyun 		goto fail_cmd;
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 	rc = efx_nic_alloc_buffer(efx, &nic_data->vfdi_status,
1306*4882a593Smuzhiyun 				  sizeof(*vfdi_status), GFP_KERNEL);
1307*4882a593Smuzhiyun 	if (rc)
1308*4882a593Smuzhiyun 		goto fail_status;
1309*4882a593Smuzhiyun 	vfdi_status = nic_data->vfdi_status.addr;
1310*4882a593Smuzhiyun 	memset(vfdi_status, 0, sizeof(*vfdi_status));
1311*4882a593Smuzhiyun 	vfdi_status->version = 1;
1312*4882a593Smuzhiyun 	vfdi_status->length = sizeof(*vfdi_status);
1313*4882a593Smuzhiyun 	vfdi_status->max_tx_channels = vf_max_tx_channels;
1314*4882a593Smuzhiyun 	vfdi_status->vi_scale = efx->vi_scale;
1315*4882a593Smuzhiyun 	vfdi_status->rss_rxq_count = efx->rss_spread;
1316*4882a593Smuzhiyun 	vfdi_status->peer_count = 1 + efx->vf_count;
1317*4882a593Smuzhiyun 	vfdi_status->timer_quantum_ns = efx->timer_quantum_ns;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	rc = efx_siena_sriov_vf_alloc(efx);
1320*4882a593Smuzhiyun 	if (rc)
1321*4882a593Smuzhiyun 		goto fail_alloc;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	mutex_init(&nic_data->local_lock);
1324*4882a593Smuzhiyun 	INIT_WORK(&nic_data->peer_work, efx_siena_sriov_peer_work);
1325*4882a593Smuzhiyun 	INIT_LIST_HEAD(&nic_data->local_addr_list);
1326*4882a593Smuzhiyun 	INIT_LIST_HEAD(&nic_data->local_page_list);
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	rc = efx_siena_sriov_vfs_init(efx);
1329*4882a593Smuzhiyun 	if (rc)
1330*4882a593Smuzhiyun 		goto fail_vfs;
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	rtnl_lock();
1333*4882a593Smuzhiyun 	ether_addr_copy(vfdi_status->peers[0].mac_addr, net_dev->dev_addr);
1334*4882a593Smuzhiyun 	efx->vf_init_count = efx->vf_count;
1335*4882a593Smuzhiyun 	rtnl_unlock();
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	efx_siena_sriov_usrev(efx, true);
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	/* At this point we must be ready to accept VFDI requests */
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	rc = pci_enable_sriov(efx->pci_dev, efx->vf_count);
1342*4882a593Smuzhiyun 	if (rc)
1343*4882a593Smuzhiyun 		goto fail_pci;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	netif_info(efx, probe, net_dev,
1346*4882a593Smuzhiyun 		   "enabled SR-IOV for %d VFs, %d VI per VF\n",
1347*4882a593Smuzhiyun 		   efx->vf_count, efx_vf_size(efx));
1348*4882a593Smuzhiyun 	return 0;
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun fail_pci:
1351*4882a593Smuzhiyun 	efx_siena_sriov_usrev(efx, false);
1352*4882a593Smuzhiyun 	rtnl_lock();
1353*4882a593Smuzhiyun 	efx->vf_init_count = 0;
1354*4882a593Smuzhiyun 	rtnl_unlock();
1355*4882a593Smuzhiyun 	efx_siena_sriov_vfs_fini(efx);
1356*4882a593Smuzhiyun fail_vfs:
1357*4882a593Smuzhiyun 	cancel_work_sync(&nic_data->peer_work);
1358*4882a593Smuzhiyun 	efx_siena_sriov_free_local(efx);
1359*4882a593Smuzhiyun 	kfree(nic_data->vf);
1360*4882a593Smuzhiyun fail_alloc:
1361*4882a593Smuzhiyun 	efx_nic_free_buffer(efx, &nic_data->vfdi_status);
1362*4882a593Smuzhiyun fail_status:
1363*4882a593Smuzhiyun 	efx_siena_sriov_cmd(efx, false, NULL, NULL);
1364*4882a593Smuzhiyun fail_cmd:
1365*4882a593Smuzhiyun 	return rc;
1366*4882a593Smuzhiyun }
1367*4882a593Smuzhiyun 
efx_siena_sriov_fini(struct efx_nic * efx)1368*4882a593Smuzhiyun void efx_siena_sriov_fini(struct efx_nic *efx)
1369*4882a593Smuzhiyun {
1370*4882a593Smuzhiyun 	struct siena_vf *vf;
1371*4882a593Smuzhiyun 	unsigned int pos;
1372*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 	if (efx->vf_init_count == 0)
1375*4882a593Smuzhiyun 		return;
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 	/* Disable all interfaces to reconfiguration */
1378*4882a593Smuzhiyun 	BUG_ON(nic_data->vfdi_channel->enabled);
1379*4882a593Smuzhiyun 	efx_siena_sriov_usrev(efx, false);
1380*4882a593Smuzhiyun 	rtnl_lock();
1381*4882a593Smuzhiyun 	efx->vf_init_count = 0;
1382*4882a593Smuzhiyun 	rtnl_unlock();
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 	/* Flush all reconfiguration work */
1385*4882a593Smuzhiyun 	for (pos = 0; pos < efx->vf_count; ++pos) {
1386*4882a593Smuzhiyun 		vf = nic_data->vf + pos;
1387*4882a593Smuzhiyun 		cancel_work_sync(&vf->req);
1388*4882a593Smuzhiyun 		cancel_work_sync(&vf->reset_work);
1389*4882a593Smuzhiyun 	}
1390*4882a593Smuzhiyun 	cancel_work_sync(&nic_data->peer_work);
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	pci_disable_sriov(efx->pci_dev);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	/* Tear down back-end state */
1395*4882a593Smuzhiyun 	efx_siena_sriov_vfs_fini(efx);
1396*4882a593Smuzhiyun 	efx_siena_sriov_free_local(efx);
1397*4882a593Smuzhiyun 	kfree(nic_data->vf);
1398*4882a593Smuzhiyun 	efx_nic_free_buffer(efx, &nic_data->vfdi_status);
1399*4882a593Smuzhiyun 	efx_siena_sriov_cmd(efx, false, NULL, NULL);
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun 
efx_siena_sriov_event(struct efx_channel * channel,efx_qword_t * event)1402*4882a593Smuzhiyun void efx_siena_sriov_event(struct efx_channel *channel, efx_qword_t *event)
1403*4882a593Smuzhiyun {
1404*4882a593Smuzhiyun 	struct efx_nic *efx = channel->efx;
1405*4882a593Smuzhiyun 	struct siena_vf *vf;
1406*4882a593Smuzhiyun 	unsigned qid, seq, type, data;
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	qid = EFX_QWORD_FIELD(*event, FSF_CZ_USER_QID);
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun 	/* USR_EV_REG_VALUE is dword0, so access the VFDI_EV fields directly */
1411*4882a593Smuzhiyun 	BUILD_BUG_ON(FSF_CZ_USER_EV_REG_VALUE_LBN != 0);
1412*4882a593Smuzhiyun 	seq = EFX_QWORD_FIELD(*event, VFDI_EV_SEQ);
1413*4882a593Smuzhiyun 	type = EFX_QWORD_FIELD(*event, VFDI_EV_TYPE);
1414*4882a593Smuzhiyun 	data = EFX_QWORD_FIELD(*event, VFDI_EV_DATA);
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	netif_vdbg(efx, hw, efx->net_dev,
1417*4882a593Smuzhiyun 		   "USR_EV event from qid %d seq 0x%x type %d data 0x%x\n",
1418*4882a593Smuzhiyun 		   qid, seq, type, data);
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 	if (map_vi_index(efx, qid, &vf, NULL))
1421*4882a593Smuzhiyun 		return;
1422*4882a593Smuzhiyun 	if (vf->busy)
1423*4882a593Smuzhiyun 		goto error;
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	if (type == VFDI_EV_TYPE_REQ_WORD0) {
1426*4882a593Smuzhiyun 		/* Resynchronise */
1427*4882a593Smuzhiyun 		vf->req_type = VFDI_EV_TYPE_REQ_WORD0;
1428*4882a593Smuzhiyun 		vf->req_seqno = seq + 1;
1429*4882a593Smuzhiyun 		vf->req_addr = 0;
1430*4882a593Smuzhiyun 	} else if (seq != (vf->req_seqno++ & 0xff) || type != vf->req_type)
1431*4882a593Smuzhiyun 		goto error;
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	switch (vf->req_type) {
1434*4882a593Smuzhiyun 	case VFDI_EV_TYPE_REQ_WORD0:
1435*4882a593Smuzhiyun 	case VFDI_EV_TYPE_REQ_WORD1:
1436*4882a593Smuzhiyun 	case VFDI_EV_TYPE_REQ_WORD2:
1437*4882a593Smuzhiyun 		vf->req_addr |= (u64)data << (vf->req_type << 4);
1438*4882a593Smuzhiyun 		++vf->req_type;
1439*4882a593Smuzhiyun 		return;
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	case VFDI_EV_TYPE_REQ_WORD3:
1442*4882a593Smuzhiyun 		vf->req_addr |= (u64)data << 48;
1443*4882a593Smuzhiyun 		vf->req_type = VFDI_EV_TYPE_REQ_WORD0;
1444*4882a593Smuzhiyun 		vf->busy = true;
1445*4882a593Smuzhiyun 		queue_work(vfdi_workqueue, &vf->req);
1446*4882a593Smuzhiyun 		return;
1447*4882a593Smuzhiyun 	}
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun error:
1450*4882a593Smuzhiyun 	if (net_ratelimit())
1451*4882a593Smuzhiyun 		netif_err(efx, hw, efx->net_dev,
1452*4882a593Smuzhiyun 			  "ERROR: Screaming VFDI request from %s\n",
1453*4882a593Smuzhiyun 			  vf->pci_name);
1454*4882a593Smuzhiyun 	/* Reset the request and sequence number */
1455*4882a593Smuzhiyun 	vf->req_type = VFDI_EV_TYPE_REQ_WORD0;
1456*4882a593Smuzhiyun 	vf->req_seqno = seq + 1;
1457*4882a593Smuzhiyun }
1458*4882a593Smuzhiyun 
efx_siena_sriov_flr(struct efx_nic * efx,unsigned vf_i)1459*4882a593Smuzhiyun void efx_siena_sriov_flr(struct efx_nic *efx, unsigned vf_i)
1460*4882a593Smuzhiyun {
1461*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1462*4882a593Smuzhiyun 	struct siena_vf *vf;
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun 	if (vf_i > efx->vf_init_count)
1465*4882a593Smuzhiyun 		return;
1466*4882a593Smuzhiyun 	vf = nic_data->vf + vf_i;
1467*4882a593Smuzhiyun 	netif_info(efx, hw, efx->net_dev,
1468*4882a593Smuzhiyun 		   "FLR on VF %s\n", vf->pci_name);
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 	vf->status_addr = 0;
1471*4882a593Smuzhiyun 	efx_vfdi_remove_all_filters(vf);
1472*4882a593Smuzhiyun 	efx_vfdi_flush_clear(vf);
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	vf->evq0_count = 0;
1475*4882a593Smuzhiyun }
1476*4882a593Smuzhiyun 
efx_siena_sriov_mac_address_changed(struct efx_nic * efx)1477*4882a593Smuzhiyun int efx_siena_sriov_mac_address_changed(struct efx_nic *efx)
1478*4882a593Smuzhiyun {
1479*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1480*4882a593Smuzhiyun 	struct vfdi_status *vfdi_status = nic_data->vfdi_status.addr;
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	if (!efx->vf_init_count)
1483*4882a593Smuzhiyun 		return 0;
1484*4882a593Smuzhiyun 	ether_addr_copy(vfdi_status->peers[0].mac_addr,
1485*4882a593Smuzhiyun 			efx->net_dev->dev_addr);
1486*4882a593Smuzhiyun 	queue_work(vfdi_workqueue, &nic_data->peer_work);
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun 	return 0;
1489*4882a593Smuzhiyun }
1490*4882a593Smuzhiyun 
efx_siena_sriov_tx_flush_done(struct efx_nic * efx,efx_qword_t * event)1491*4882a593Smuzhiyun void efx_siena_sriov_tx_flush_done(struct efx_nic *efx, efx_qword_t *event)
1492*4882a593Smuzhiyun {
1493*4882a593Smuzhiyun 	struct siena_vf *vf;
1494*4882a593Smuzhiyun 	unsigned queue, qid;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	queue = EFX_QWORD_FIELD(*event,  FSF_AZ_DRIVER_EV_SUBDATA);
1497*4882a593Smuzhiyun 	if (map_vi_index(efx, queue, &vf, &qid))
1498*4882a593Smuzhiyun 		return;
1499*4882a593Smuzhiyun 	/* Ignore flush completions triggered by an FLR */
1500*4882a593Smuzhiyun 	if (!test_bit(qid, vf->txq_mask))
1501*4882a593Smuzhiyun 		return;
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	__clear_bit(qid, vf->txq_mask);
1504*4882a593Smuzhiyun 	--vf->txq_count;
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	if (efx_vfdi_flush_wake(vf))
1507*4882a593Smuzhiyun 		wake_up(&vf->flush_waitq);
1508*4882a593Smuzhiyun }
1509*4882a593Smuzhiyun 
efx_siena_sriov_rx_flush_done(struct efx_nic * efx,efx_qword_t * event)1510*4882a593Smuzhiyun void efx_siena_sriov_rx_flush_done(struct efx_nic *efx, efx_qword_t *event)
1511*4882a593Smuzhiyun {
1512*4882a593Smuzhiyun 	struct siena_vf *vf;
1513*4882a593Smuzhiyun 	unsigned ev_failed, queue, qid;
1514*4882a593Smuzhiyun 
1515*4882a593Smuzhiyun 	queue = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
1516*4882a593Smuzhiyun 	ev_failed = EFX_QWORD_FIELD(*event,
1517*4882a593Smuzhiyun 				    FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
1518*4882a593Smuzhiyun 	if (map_vi_index(efx, queue, &vf, &qid))
1519*4882a593Smuzhiyun 		return;
1520*4882a593Smuzhiyun 	if (!test_bit(qid, vf->rxq_mask))
1521*4882a593Smuzhiyun 		return;
1522*4882a593Smuzhiyun 
1523*4882a593Smuzhiyun 	if (ev_failed) {
1524*4882a593Smuzhiyun 		set_bit(qid, vf->rxq_retry_mask);
1525*4882a593Smuzhiyun 		atomic_inc(&vf->rxq_retry_count);
1526*4882a593Smuzhiyun 	} else {
1527*4882a593Smuzhiyun 		__clear_bit(qid, vf->rxq_mask);
1528*4882a593Smuzhiyun 		--vf->rxq_count;
1529*4882a593Smuzhiyun 	}
1530*4882a593Smuzhiyun 	if (efx_vfdi_flush_wake(vf))
1531*4882a593Smuzhiyun 		wake_up(&vf->flush_waitq);
1532*4882a593Smuzhiyun }
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun /* Called from napi. Schedule the reset work item */
efx_siena_sriov_desc_fetch_err(struct efx_nic * efx,unsigned dmaq)1535*4882a593Smuzhiyun void efx_siena_sriov_desc_fetch_err(struct efx_nic *efx, unsigned dmaq)
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun 	struct siena_vf *vf;
1538*4882a593Smuzhiyun 	unsigned int rel;
1539*4882a593Smuzhiyun 
1540*4882a593Smuzhiyun 	if (map_vi_index(efx, dmaq, &vf, &rel))
1541*4882a593Smuzhiyun 		return;
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	if (net_ratelimit())
1544*4882a593Smuzhiyun 		netif_err(efx, hw, efx->net_dev,
1545*4882a593Smuzhiyun 			  "VF %d DMA Q %d reports descriptor fetch error.\n",
1546*4882a593Smuzhiyun 			  vf->index, rel);
1547*4882a593Smuzhiyun 	queue_work(vfdi_workqueue, &vf->reset_work);
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun /* Reset all VFs */
efx_siena_sriov_reset(struct efx_nic * efx)1551*4882a593Smuzhiyun void efx_siena_sriov_reset(struct efx_nic *efx)
1552*4882a593Smuzhiyun {
1553*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1554*4882a593Smuzhiyun 	unsigned int vf_i;
1555*4882a593Smuzhiyun 	struct efx_buffer buf;
1556*4882a593Smuzhiyun 	struct siena_vf *vf;
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 	ASSERT_RTNL();
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	if (efx->vf_init_count == 0)
1561*4882a593Smuzhiyun 		return;
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	efx_siena_sriov_usrev(efx, true);
1564*4882a593Smuzhiyun 	(void)efx_siena_sriov_cmd(efx, true, NULL, NULL);
1565*4882a593Smuzhiyun 
1566*4882a593Smuzhiyun 	if (efx_nic_alloc_buffer(efx, &buf, EFX_PAGE_SIZE, GFP_NOIO))
1567*4882a593Smuzhiyun 		return;
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 	for (vf_i = 0; vf_i < efx->vf_init_count; ++vf_i) {
1570*4882a593Smuzhiyun 		vf = nic_data->vf + vf_i;
1571*4882a593Smuzhiyun 		efx_siena_sriov_reset_vf(vf, &buf);
1572*4882a593Smuzhiyun 	}
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	efx_nic_free_buffer(efx, &buf);
1575*4882a593Smuzhiyun }
1576*4882a593Smuzhiyun 
efx_init_sriov(void)1577*4882a593Smuzhiyun int efx_init_sriov(void)
1578*4882a593Smuzhiyun {
1579*4882a593Smuzhiyun 	/* A single threaded workqueue is sufficient. efx_siena_sriov_vfdi() and
1580*4882a593Smuzhiyun 	 * efx_siena_sriov_peer_work() spend almost all their time sleeping for
1581*4882a593Smuzhiyun 	 * MCDI to complete anyway
1582*4882a593Smuzhiyun 	 */
1583*4882a593Smuzhiyun 	vfdi_workqueue = create_singlethread_workqueue("sfc_vfdi");
1584*4882a593Smuzhiyun 	if (!vfdi_workqueue)
1585*4882a593Smuzhiyun 		return -ENOMEM;
1586*4882a593Smuzhiyun 	return 0;
1587*4882a593Smuzhiyun }
1588*4882a593Smuzhiyun 
efx_fini_sriov(void)1589*4882a593Smuzhiyun void efx_fini_sriov(void)
1590*4882a593Smuzhiyun {
1591*4882a593Smuzhiyun 	destroy_workqueue(vfdi_workqueue);
1592*4882a593Smuzhiyun }
1593*4882a593Smuzhiyun 
efx_siena_sriov_set_vf_mac(struct efx_nic * efx,int vf_i,u8 * mac)1594*4882a593Smuzhiyun int efx_siena_sriov_set_vf_mac(struct efx_nic *efx, int vf_i, u8 *mac)
1595*4882a593Smuzhiyun {
1596*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1597*4882a593Smuzhiyun 	struct siena_vf *vf;
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun 	if (vf_i >= efx->vf_init_count)
1600*4882a593Smuzhiyun 		return -EINVAL;
1601*4882a593Smuzhiyun 	vf = nic_data->vf + vf_i;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	mutex_lock(&vf->status_lock);
1604*4882a593Smuzhiyun 	ether_addr_copy(vf->addr.mac_addr, mac);
1605*4882a593Smuzhiyun 	__efx_siena_sriov_update_vf_addr(vf);
1606*4882a593Smuzhiyun 	mutex_unlock(&vf->status_lock);
1607*4882a593Smuzhiyun 
1608*4882a593Smuzhiyun 	return 0;
1609*4882a593Smuzhiyun }
1610*4882a593Smuzhiyun 
efx_siena_sriov_set_vf_vlan(struct efx_nic * efx,int vf_i,u16 vlan,u8 qos)1611*4882a593Smuzhiyun int efx_siena_sriov_set_vf_vlan(struct efx_nic *efx, int vf_i,
1612*4882a593Smuzhiyun 				u16 vlan, u8 qos)
1613*4882a593Smuzhiyun {
1614*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1615*4882a593Smuzhiyun 	struct siena_vf *vf;
1616*4882a593Smuzhiyun 	u16 tci;
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 	if (vf_i >= efx->vf_init_count)
1619*4882a593Smuzhiyun 		return -EINVAL;
1620*4882a593Smuzhiyun 	vf = nic_data->vf + vf_i;
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	mutex_lock(&vf->status_lock);
1623*4882a593Smuzhiyun 	tci = (vlan & VLAN_VID_MASK) | ((qos & 0x7) << VLAN_PRIO_SHIFT);
1624*4882a593Smuzhiyun 	vf->addr.tci = htons(tci);
1625*4882a593Smuzhiyun 	__efx_siena_sriov_update_vf_addr(vf);
1626*4882a593Smuzhiyun 	mutex_unlock(&vf->status_lock);
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	return 0;
1629*4882a593Smuzhiyun }
1630*4882a593Smuzhiyun 
efx_siena_sriov_set_vf_spoofchk(struct efx_nic * efx,int vf_i,bool spoofchk)1631*4882a593Smuzhiyun int efx_siena_sriov_set_vf_spoofchk(struct efx_nic *efx, int vf_i,
1632*4882a593Smuzhiyun 				    bool spoofchk)
1633*4882a593Smuzhiyun {
1634*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1635*4882a593Smuzhiyun 	struct siena_vf *vf;
1636*4882a593Smuzhiyun 	int rc;
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun 	if (vf_i >= efx->vf_init_count)
1639*4882a593Smuzhiyun 		return -EINVAL;
1640*4882a593Smuzhiyun 	vf = nic_data->vf + vf_i;
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun 	mutex_lock(&vf->txq_lock);
1643*4882a593Smuzhiyun 	if (vf->txq_count == 0) {
1644*4882a593Smuzhiyun 		vf->tx_filter_mode =
1645*4882a593Smuzhiyun 			spoofchk ? VF_TX_FILTER_ON : VF_TX_FILTER_OFF;
1646*4882a593Smuzhiyun 		rc = 0;
1647*4882a593Smuzhiyun 	} else {
1648*4882a593Smuzhiyun 		/* This cannot be changed while TX queues are running */
1649*4882a593Smuzhiyun 		rc = -EBUSY;
1650*4882a593Smuzhiyun 	}
1651*4882a593Smuzhiyun 	mutex_unlock(&vf->txq_lock);
1652*4882a593Smuzhiyun 	return rc;
1653*4882a593Smuzhiyun }
1654*4882a593Smuzhiyun 
efx_siena_sriov_get_vf_config(struct efx_nic * efx,int vf_i,struct ifla_vf_info * ivi)1655*4882a593Smuzhiyun int efx_siena_sriov_get_vf_config(struct efx_nic *efx, int vf_i,
1656*4882a593Smuzhiyun 				  struct ifla_vf_info *ivi)
1657*4882a593Smuzhiyun {
1658*4882a593Smuzhiyun 	struct siena_nic_data *nic_data = efx->nic_data;
1659*4882a593Smuzhiyun 	struct siena_vf *vf;
1660*4882a593Smuzhiyun 	u16 tci;
1661*4882a593Smuzhiyun 
1662*4882a593Smuzhiyun 	if (vf_i >= efx->vf_init_count)
1663*4882a593Smuzhiyun 		return -EINVAL;
1664*4882a593Smuzhiyun 	vf = nic_data->vf + vf_i;
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun 	ivi->vf = vf_i;
1667*4882a593Smuzhiyun 	ether_addr_copy(ivi->mac, vf->addr.mac_addr);
1668*4882a593Smuzhiyun 	ivi->max_tx_rate = 0;
1669*4882a593Smuzhiyun 	ivi->min_tx_rate = 0;
1670*4882a593Smuzhiyun 	tci = ntohs(vf->addr.tci);
1671*4882a593Smuzhiyun 	ivi->vlan = tci & VLAN_VID_MASK;
1672*4882a593Smuzhiyun 	ivi->qos = (tci >> VLAN_PRIO_SHIFT) & 0x7;
1673*4882a593Smuzhiyun 	ivi->spoofchk = vf->tx_filter_mode == VF_TX_FILTER_ON;
1674*4882a593Smuzhiyun 
1675*4882a593Smuzhiyun 	return 0;
1676*4882a593Smuzhiyun }
1677*4882a593Smuzhiyun 
efx_siena_sriov_wanted(struct efx_nic * efx)1678*4882a593Smuzhiyun bool efx_siena_sriov_wanted(struct efx_nic *efx)
1679*4882a593Smuzhiyun {
1680*4882a593Smuzhiyun 	return efx->vf_count != 0;
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun 
efx_siena_sriov_configure(struct efx_nic * efx,int num_vfs)1683*4882a593Smuzhiyun int efx_siena_sriov_configure(struct efx_nic *efx, int num_vfs)
1684*4882a593Smuzhiyun {
1685*4882a593Smuzhiyun 	return 0;
1686*4882a593Smuzhiyun }
1687