1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2010-2012 Solarflare Communications Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation, incorporated herein by reference.
10 #include <linux/module.h>
11 #include "net_driver.h"
17 #include "mcdi_pcol.h"
18 #include "farch_regs.h"
19 #include "siena_sriov.h"
22 /* Number of longs required to track all the VIs in a VF */
23 #define VI_MASK_LENGTH BITS_TO_LONGS(1 << EFX_VI_SCALE_MAX)
25 /* Maximum number of RX queues supported */
26 #define VF_MAX_RX_QUEUES 63
29 * enum efx_vf_tx_filter_mode - TX MAC filtering behaviour
30 * @VF_TX_FILTER_OFF: Disabled
31 * @VF_TX_FILTER_AUTO: Enabled if MAC address assigned to VF and only
32 * 2 TX queues allowed per VF.
33 * @VF_TX_FILTER_ON: Enabled
35 enum efx_vf_tx_filter_mode
{
42 * struct siena_vf - Back-end resource and protocol state for a PCI VF
43 * @efx: The Efx NIC owning this VF
44 * @pci_rid: The PCI requester ID for this VF
45 * @pci_name: The PCI name (formatted address) of this VF
46 * @index: Index of VF within its port and PF.
47 * @req: VFDI incoming request work item. Incoming USR_EV events are received
48 * by the NAPI handler, but must be handled by executing MCDI requests
50 * @req_addr: VFDI incoming request DMA address (in VF's PCI address space).
51 * @req_type: Expected next incoming (from VF) %VFDI_EV_TYPE member.
52 * @req_seqno: Expected next incoming (from VF) %VFDI_EV_SEQ member.
53 * @msg_seqno: Next %VFDI_EV_SEQ member to reply to VF. Protected by
55 * @busy: VFDI request queued to be processed or being processed. Receiving
56 * a VFDI request when @busy is set is an error condition.
57 * @buf: Incoming VFDI requests are DMA from the VF into this buffer.
58 * @buftbl_base: Buffer table entries for this VF start at this index.
59 * @rx_filtering: Receive filtering has been requested by the VF driver.
60 * @rx_filter_flags: The flags sent in the %VFDI_OP_INSERT_FILTER request.
61 * @rx_filter_qid: VF relative qid for RX filter requested by VF.
62 * @rx_filter_id: Receive MAC filter ID. Only one filter per VF is supported.
63 * @tx_filter_mode: Transmit MAC filtering mode.
64 * @tx_filter_id: Transmit MAC filter ID.
65 * @addr: The MAC address and outer vlan tag of the VF.
66 * @status_addr: VF DMA address of page for &struct vfdi_status updates.
67 * @status_lock: Mutex protecting @msg_seqno, @status_addr, @addr,
68 * @peer_page_addrs and @peer_page_count from simultaneous
69 * updates by the VM and consumption by
70 * efx_siena_sriov_update_vf_addr()
71 * @peer_page_addrs: Pointer to an array of guest pages for local addresses.
72 * @peer_page_count: Number of entries in @peer_page_count.
73 * @evq0_addrs: Array of guest pages backing evq0.
74 * @evq0_count: Number of entries in @evq0_addrs.
75 * @flush_waitq: wait queue used by %VFDI_OP_FINI_ALL_QUEUES handler
76 * to wait for flush completions.
77 * @txq_lock: Mutex for TX queue allocation.
78 * @txq_mask: Mask of initialized transmit queues.
79 * @txq_count: Number of initialized transmit queues.
80 * @rxq_mask: Mask of initialized receive queues.
81 * @rxq_count: Number of initialized receive queues.
82 * @rxq_retry_mask: Mask or receive queues that need to be flushed again
83 * due to flush failure.
84 * @rxq_retry_count: Number of receive queues in @rxq_retry_mask.
85 * @reset_work: Work item to schedule a VF reset.
90 char pci_name
[13]; /* dddd:bb:dd.f */
92 struct work_struct req
;
98 struct efx_buffer buf
;
101 enum efx_filter_flags rx_filter_flags
;
102 unsigned rx_filter_qid
;
104 enum efx_vf_tx_filter_mode tx_filter_mode
;
106 struct vfdi_endpoint addr
;
108 struct mutex status_lock
;
109 u64
*peer_page_addrs
;
110 unsigned peer_page_count
;
111 u64 evq0_addrs
[EFX_MAX_VF_EVQ_SIZE
* sizeof(efx_qword_t
) /
114 wait_queue_head_t flush_waitq
;
115 struct mutex txq_lock
;
116 unsigned long txq_mask
[VI_MASK_LENGTH
];
118 unsigned long rxq_mask
[VI_MASK_LENGTH
];
120 unsigned long rxq_retry_mask
[VI_MASK_LENGTH
];
121 atomic_t rxq_retry_count
;
122 struct work_struct reset_work
;
125 struct efx_memcpy_req
{
126 unsigned int from_rid
;
135 * struct efx_local_addr - A MAC address on the vswitch without a VF.
137 * Siena does not have a switch, so VFs can't transmit data to each
138 * other. Instead the VFs must be made aware of the local addresses
139 * on the vswitch, so that they can arrange for an alternative
140 * software datapath to be used.
142 * @link: List head for insertion into efx->local_addr_list.
143 * @addr: Ethernet address
145 struct efx_local_addr
{
146 struct list_head link
;
151 * struct efx_endpoint_page - Page of vfdi_endpoint structures
153 * @link: List head for insertion into efx->local_page_list.
154 * @ptr: Pointer to page.
155 * @addr: DMA address of page.
157 struct efx_endpoint_page
{
158 struct list_head link
;
163 /* Buffer table entries are reserved txq0,rxq0,evq0,txq1,rxq1,evq1 */
164 #define EFX_BUFTBL_TXQ_BASE(_vf, _qid) \
165 ((_vf)->buftbl_base + EFX_VF_BUFTBL_PER_VI * (_qid))
166 #define EFX_BUFTBL_RXQ_BASE(_vf, _qid) \
167 (EFX_BUFTBL_TXQ_BASE(_vf, _qid) + \
168 (EFX_MAX_DMAQ_SIZE * sizeof(efx_qword_t) / EFX_BUF_SIZE))
169 #define EFX_BUFTBL_EVQ_BASE(_vf, _qid) \
170 (EFX_BUFTBL_TXQ_BASE(_vf, _qid) + \
171 (2 * EFX_MAX_DMAQ_SIZE * sizeof(efx_qword_t) / EFX_BUF_SIZE))
173 #define EFX_FIELD_MASK(_field) \
174 ((1 << _field ## _WIDTH) - 1)
176 /* VFs can only use this many transmit channels */
177 static unsigned int vf_max_tx_channels
= 2;
178 module_param(vf_max_tx_channels
, uint
, 0444);
179 MODULE_PARM_DESC(vf_max_tx_channels
,
180 "Limit the number of TX channels VFs can use");
182 static int max_vfs
= -1;
183 module_param(max_vfs
, int, 0444);
184 MODULE_PARM_DESC(max_vfs
,
185 "Reduce the number of VFs initialized by the driver");
187 /* Workqueue used by VFDI communication. We can't use the global
188 * workqueue because it may be running the VF driver's probe()
189 * routine, which will be blocked there waiting for a VFDI response.
191 static struct workqueue_struct
*vfdi_workqueue
;
193 static unsigned abs_index(struct siena_vf
*vf
, unsigned index
)
195 return EFX_VI_BASE
+ vf
->index
* efx_vf_size(vf
->efx
) + index
;
198 static int efx_siena_sriov_cmd(struct efx_nic
*efx
, bool enable
,
199 unsigned *vi_scale_out
, unsigned *vf_total_out
)
201 MCDI_DECLARE_BUF(inbuf
, MC_CMD_SRIOV_IN_LEN
);
202 MCDI_DECLARE_BUF(outbuf
, MC_CMD_SRIOV_OUT_LEN
);
203 unsigned vi_scale
, vf_total
;
207 MCDI_SET_DWORD(inbuf
, SRIOV_IN_ENABLE
, enable
? 1 : 0);
208 MCDI_SET_DWORD(inbuf
, SRIOV_IN_VI_BASE
, EFX_VI_BASE
);
209 MCDI_SET_DWORD(inbuf
, SRIOV_IN_VF_COUNT
, efx
->vf_count
);
211 rc
= efx_mcdi_rpc_quiet(efx
, MC_CMD_SRIOV
, inbuf
, MC_CMD_SRIOV_IN_LEN
,
212 outbuf
, MC_CMD_SRIOV_OUT_LEN
, &outlen
);
215 if (outlen
< MC_CMD_SRIOV_OUT_LEN
)
218 vf_total
= MCDI_DWORD(outbuf
, SRIOV_OUT_VF_TOTAL
);
219 vi_scale
= MCDI_DWORD(outbuf
, SRIOV_OUT_VI_SCALE
);
220 if (vi_scale
> EFX_VI_SCALE_MAX
)
224 *vi_scale_out
= vi_scale
;
226 *vf_total_out
= vf_total
;
231 static void efx_siena_sriov_usrev(struct efx_nic
*efx
, bool enabled
)
233 struct siena_nic_data
*nic_data
= efx
->nic_data
;
236 EFX_POPULATE_OWORD_2(reg
,
237 FRF_CZ_USREV_DIS
, enabled
? 0 : 1,
238 FRF_CZ_DFLT_EVQ
, nic_data
->vfdi_channel
->channel
);
239 efx_writeo(efx
, ®
, FR_CZ_USR_EV_CFG
);
242 static int efx_siena_sriov_memcpy(struct efx_nic
*efx
,
243 struct efx_memcpy_req
*req
,
246 MCDI_DECLARE_BUF(inbuf
, MCDI_CTL_SDU_LEN_MAX_V1
);
247 MCDI_DECLARE_STRUCT_PTR(record
);
248 unsigned int index
, used
;
253 mb(); /* Finish writing source/reading dest before DMA starts */
255 if (WARN_ON(count
> MC_CMD_MEMCPY_IN_RECORD_MAXNUM
))
257 used
= MC_CMD_MEMCPY_IN_LEN(count
);
259 for (index
= 0; index
< count
; index
++) {
260 record
= MCDI_ARRAY_STRUCT_PTR(inbuf
, MEMCPY_IN_RECORD
, index
);
261 MCDI_SET_DWORD(record
, MEMCPY_RECORD_TYPEDEF_NUM_RECORDS
,
263 MCDI_SET_DWORD(record
, MEMCPY_RECORD_TYPEDEF_TO_RID
,
265 MCDI_SET_QWORD(record
, MEMCPY_RECORD_TYPEDEF_TO_ADDR
,
267 if (req
->from_buf
== NULL
) {
268 from_rid
= req
->from_rid
;
269 from_addr
= req
->from_addr
;
271 if (WARN_ON(used
+ req
->length
>
272 MCDI_CTL_SDU_LEN_MAX_V1
)) {
277 from_rid
= MC_CMD_MEMCPY_RECORD_TYPEDEF_RID_INLINE
;
279 memcpy(_MCDI_PTR(inbuf
, used
), req
->from_buf
,
284 MCDI_SET_DWORD(record
, MEMCPY_RECORD_TYPEDEF_FROM_RID
, from_rid
);
285 MCDI_SET_QWORD(record
, MEMCPY_RECORD_TYPEDEF_FROM_ADDR
,
287 MCDI_SET_DWORD(record
, MEMCPY_RECORD_TYPEDEF_LENGTH
,
293 rc
= efx_mcdi_rpc(efx
, MC_CMD_MEMCPY
, inbuf
, used
, NULL
, 0, NULL
);
295 mb(); /* Don't write source/read dest before DMA is complete */
300 /* The TX filter is entirely controlled by this driver, and is modified
301 * underneath the feet of the VF
303 static void efx_siena_sriov_reset_tx_filter(struct siena_vf
*vf
)
305 struct efx_nic
*efx
= vf
->efx
;
306 struct efx_filter_spec filter
;
310 if (vf
->tx_filter_id
!= -1) {
311 efx_filter_remove_id_safe(efx
, EFX_FILTER_PRI_REQUIRED
,
313 netif_dbg(efx
, hw
, efx
->net_dev
, "Removed vf %s tx filter %d\n",
314 vf
->pci_name
, vf
->tx_filter_id
);
315 vf
->tx_filter_id
= -1;
318 if (is_zero_ether_addr(vf
->addr
.mac_addr
))
321 /* Turn on TX filtering automatically if not explicitly
322 * enabled or disabled.
324 if (vf
->tx_filter_mode
== VF_TX_FILTER_AUTO
&& vf_max_tx_channels
<= 2)
325 vf
->tx_filter_mode
= VF_TX_FILTER_ON
;
327 vlan
= ntohs(vf
->addr
.tci
) & VLAN_VID_MASK
;
328 efx_filter_init_tx(&filter
, abs_index(vf
, 0));
329 rc
= efx_filter_set_eth_local(&filter
,
330 vlan
? vlan
: EFX_FILTER_VID_UNSPEC
,
334 rc
= efx_filter_insert_filter(efx
, &filter
, true);
336 netif_warn(efx
, hw
, efx
->net_dev
,
337 "Unable to migrate tx filter for vf %s\n",
340 netif_dbg(efx
, hw
, efx
->net_dev
, "Inserted vf %s tx filter %d\n",
342 vf
->tx_filter_id
= rc
;
346 /* The RX filter is managed here on behalf of the VF driver */
347 static void efx_siena_sriov_reset_rx_filter(struct siena_vf
*vf
)
349 struct efx_nic
*efx
= vf
->efx
;
350 struct efx_filter_spec filter
;
354 if (vf
->rx_filter_id
!= -1) {
355 efx_filter_remove_id_safe(efx
, EFX_FILTER_PRI_REQUIRED
,
357 netif_dbg(efx
, hw
, efx
->net_dev
, "Removed vf %s rx filter %d\n",
358 vf
->pci_name
, vf
->rx_filter_id
);
359 vf
->rx_filter_id
= -1;
362 if (!vf
->rx_filtering
|| is_zero_ether_addr(vf
->addr
.mac_addr
))
365 vlan
= ntohs(vf
->addr
.tci
) & VLAN_VID_MASK
;
366 efx_filter_init_rx(&filter
, EFX_FILTER_PRI_REQUIRED
,
368 abs_index(vf
, vf
->rx_filter_qid
));
369 rc
= efx_filter_set_eth_local(&filter
,
370 vlan
? vlan
: EFX_FILTER_VID_UNSPEC
,
374 rc
= efx_filter_insert_filter(efx
, &filter
, true);
376 netif_warn(efx
, hw
, efx
->net_dev
,
377 "Unable to insert rx filter for vf %s\n",
380 netif_dbg(efx
, hw
, efx
->net_dev
, "Inserted vf %s rx filter %d\n",
382 vf
->rx_filter_id
= rc
;
386 static void __efx_siena_sriov_update_vf_addr(struct siena_vf
*vf
)
388 struct efx_nic
*efx
= vf
->efx
;
389 struct siena_nic_data
*nic_data
= efx
->nic_data
;
391 efx_siena_sriov_reset_tx_filter(vf
);
392 efx_siena_sriov_reset_rx_filter(vf
);
393 queue_work(vfdi_workqueue
, &nic_data
->peer_work
);
396 /* Push the peer list to this VF. The caller must hold status_lock to interlock
397 * with VFDI requests, and they must be serialised against manipulation of
398 * local_page_list, either by acquiring local_lock or by running from
399 * efx_siena_sriov_peer_work()
401 static void __efx_siena_sriov_push_vf_status(struct siena_vf
*vf
)
403 struct efx_nic
*efx
= vf
->efx
;
404 struct siena_nic_data
*nic_data
= efx
->nic_data
;
405 struct vfdi_status
*status
= nic_data
->vfdi_status
.addr
;
406 struct efx_memcpy_req copy
[4];
407 struct efx_endpoint_page
*epp
;
408 unsigned int pos
, count
;
409 unsigned data_offset
;
412 WARN_ON(!mutex_is_locked(&vf
->status_lock
));
413 WARN_ON(!vf
->status_addr
);
415 status
->local
= vf
->addr
;
416 status
->generation_end
= ++status
->generation_start
;
418 memset(copy
, '\0', sizeof(copy
));
419 /* Write generation_start */
420 copy
[0].from_buf
= &status
->generation_start
;
421 copy
[0].to_rid
= vf
->pci_rid
;
422 copy
[0].to_addr
= vf
->status_addr
+ offsetof(struct vfdi_status
,
424 copy
[0].length
= sizeof(status
->generation_start
);
425 /* DMA the rest of the structure (excluding the generations). This
426 * assumes that the non-generation portion of vfdi_status is in
427 * one chunk starting at the version member.
429 data_offset
= offsetof(struct vfdi_status
, version
);
430 copy
[1].from_rid
= efx
->pci_dev
->devfn
;
431 copy
[1].from_addr
= nic_data
->vfdi_status
.dma_addr
+ data_offset
;
432 copy
[1].to_rid
= vf
->pci_rid
;
433 copy
[1].to_addr
= vf
->status_addr
+ data_offset
;
434 copy
[1].length
= status
->length
- data_offset
;
436 /* Copy the peer pages */
439 list_for_each_entry(epp
, &nic_data
->local_page_list
, link
) {
440 if (count
== vf
->peer_page_count
) {
441 /* The VF driver will know they need to provide more
442 * pages because peer_addr_count is too large.
446 copy
[pos
].from_buf
= NULL
;
447 copy
[pos
].from_rid
= efx
->pci_dev
->devfn
;
448 copy
[pos
].from_addr
= epp
->addr
;
449 copy
[pos
].to_rid
= vf
->pci_rid
;
450 copy
[pos
].to_addr
= vf
->peer_page_addrs
[count
];
451 copy
[pos
].length
= EFX_PAGE_SIZE
;
453 if (++pos
== ARRAY_SIZE(copy
)) {
454 efx_siena_sriov_memcpy(efx
, copy
, ARRAY_SIZE(copy
));
460 /* Write generation_end */
461 copy
[pos
].from_buf
= &status
->generation_end
;
462 copy
[pos
].to_rid
= vf
->pci_rid
;
463 copy
[pos
].to_addr
= vf
->status_addr
+ offsetof(struct vfdi_status
,
465 copy
[pos
].length
= sizeof(status
->generation_end
);
466 efx_siena_sriov_memcpy(efx
, copy
, pos
+ 1);
468 /* Notify the guest */
469 EFX_POPULATE_QWORD_3(event
,
470 FSF_AZ_EV_CODE
, FSE_CZ_EV_CODE_USER_EV
,
471 VFDI_EV_SEQ
, (vf
->msg_seqno
& 0xff),
472 VFDI_EV_TYPE
, VFDI_EV_TYPE_STATUS
);
474 efx_farch_generate_event(efx
,
475 EFX_VI_BASE
+ vf
->index
* efx_vf_size(efx
),
479 static void efx_siena_sriov_bufs(struct efx_nic
*efx
, unsigned offset
,
480 u64
*addr
, unsigned count
)
485 for (pos
= 0; pos
< count
; ++pos
) {
486 EFX_POPULATE_QWORD_3(buf
,
487 FRF_AZ_BUF_ADR_REGION
, 0,
489 addr
? addr
[pos
] >> 12 : 0,
490 FRF_AZ_BUF_OWNER_ID_FBUF
, 0);
491 efx_sram_writeq(efx
, efx
->membase
+ FR_BZ_BUF_FULL_TBL
,
496 static bool bad_vf_index(struct efx_nic
*efx
, unsigned index
)
498 return index
>= efx_vf_size(efx
);
501 static bool bad_buf_count(unsigned buf_count
, unsigned max_entry_count
)
503 unsigned max_buf_count
= max_entry_count
*
504 sizeof(efx_qword_t
) / EFX_BUF_SIZE
;
506 return ((buf_count
& (buf_count
- 1)) || buf_count
> max_buf_count
);
509 /* Check that VI specified by per-port index belongs to a VF.
510 * Optionally set VF index and VI index within the VF.
512 static bool map_vi_index(struct efx_nic
*efx
, unsigned abs_index
,
513 struct siena_vf
**vf_out
, unsigned *rel_index_out
)
515 struct siena_nic_data
*nic_data
= efx
->nic_data
;
518 if (abs_index
< EFX_VI_BASE
)
520 vf_i
= (abs_index
- EFX_VI_BASE
) / efx_vf_size(efx
);
521 if (vf_i
>= efx
->vf_init_count
)
525 *vf_out
= nic_data
->vf
+ vf_i
;
527 *rel_index_out
= abs_index
% efx_vf_size(efx
);
531 static int efx_vfdi_init_evq(struct siena_vf
*vf
)
533 struct efx_nic
*efx
= vf
->efx
;
534 struct vfdi_req
*req
= vf
->buf
.addr
;
535 unsigned vf_evq
= req
->u
.init_evq
.index
;
536 unsigned buf_count
= req
->u
.init_evq
.buf_count
;
537 unsigned abs_evq
= abs_index(vf
, vf_evq
);
538 unsigned buftbl
= EFX_BUFTBL_EVQ_BASE(vf
, vf_evq
);
541 if (bad_vf_index(efx
, vf_evq
) ||
542 bad_buf_count(buf_count
, EFX_MAX_VF_EVQ_SIZE
)) {
544 netif_err(efx
, hw
, efx
->net_dev
,
545 "ERROR: Invalid INIT_EVQ from %s: evq %d bufs %d\n",
546 vf
->pci_name
, vf_evq
, buf_count
);
547 return VFDI_RC_EINVAL
;
550 efx_siena_sriov_bufs(efx
, buftbl
, req
->u
.init_evq
.addr
, buf_count
);
552 EFX_POPULATE_OWORD_3(reg
,
553 FRF_CZ_TIMER_Q_EN
, 1,
554 FRF_CZ_HOST_NOTIFY_MODE
, 0,
555 FRF_CZ_TIMER_MODE
, FFE_CZ_TIMER_MODE_DIS
);
556 efx_writeo_table(efx
, ®
, FR_BZ_TIMER_TBL
, abs_evq
);
557 EFX_POPULATE_OWORD_3(reg
,
559 FRF_AZ_EVQ_SIZE
, __ffs(buf_count
),
560 FRF_AZ_EVQ_BUF_BASE_ID
, buftbl
);
561 efx_writeo_table(efx
, ®
, FR_BZ_EVQ_PTR_TBL
, abs_evq
);
564 memcpy(vf
->evq0_addrs
, req
->u
.init_evq
.addr
,
565 buf_count
* sizeof(u64
));
566 vf
->evq0_count
= buf_count
;
569 return VFDI_RC_SUCCESS
;
572 static int efx_vfdi_init_rxq(struct siena_vf
*vf
)
574 struct efx_nic
*efx
= vf
->efx
;
575 struct vfdi_req
*req
= vf
->buf
.addr
;
576 unsigned vf_rxq
= req
->u
.init_rxq
.index
;
577 unsigned vf_evq
= req
->u
.init_rxq
.evq
;
578 unsigned buf_count
= req
->u
.init_rxq
.buf_count
;
579 unsigned buftbl
= EFX_BUFTBL_RXQ_BASE(vf
, vf_rxq
);
583 if (bad_vf_index(efx
, vf_evq
) || bad_vf_index(efx
, vf_rxq
) ||
584 vf_rxq
>= VF_MAX_RX_QUEUES
||
585 bad_buf_count(buf_count
, EFX_MAX_DMAQ_SIZE
)) {
587 netif_err(efx
, hw
, efx
->net_dev
,
588 "ERROR: Invalid INIT_RXQ from %s: rxq %d evq %d "
589 "buf_count %d\n", vf
->pci_name
, vf_rxq
,
591 return VFDI_RC_EINVAL
;
593 if (__test_and_set_bit(req
->u
.init_rxq
.index
, vf
->rxq_mask
))
595 efx_siena_sriov_bufs(efx
, buftbl
, req
->u
.init_rxq
.addr
, buf_count
);
597 label
= req
->u
.init_rxq
.label
& EFX_FIELD_MASK(FRF_AZ_RX_DESCQ_LABEL
);
598 EFX_POPULATE_OWORD_6(reg
,
599 FRF_AZ_RX_DESCQ_BUF_BASE_ID
, buftbl
,
600 FRF_AZ_RX_DESCQ_EVQ_ID
, abs_index(vf
, vf_evq
),
601 FRF_AZ_RX_DESCQ_LABEL
, label
,
602 FRF_AZ_RX_DESCQ_SIZE
, __ffs(buf_count
),
603 FRF_AZ_RX_DESCQ_JUMBO
,
604 !!(req
->u
.init_rxq
.flags
&
605 VFDI_RXQ_FLAG_SCATTER_EN
),
606 FRF_AZ_RX_DESCQ_EN
, 1);
607 efx_writeo_table(efx
, ®
, FR_BZ_RX_DESC_PTR_TBL
,
608 abs_index(vf
, vf_rxq
));
610 return VFDI_RC_SUCCESS
;
613 static int efx_vfdi_init_txq(struct siena_vf
*vf
)
615 struct efx_nic
*efx
= vf
->efx
;
616 struct vfdi_req
*req
= vf
->buf
.addr
;
617 unsigned vf_txq
= req
->u
.init_txq
.index
;
618 unsigned vf_evq
= req
->u
.init_txq
.evq
;
619 unsigned buf_count
= req
->u
.init_txq
.buf_count
;
620 unsigned buftbl
= EFX_BUFTBL_TXQ_BASE(vf
, vf_txq
);
621 unsigned label
, eth_filt_en
;
624 if (bad_vf_index(efx
, vf_evq
) || bad_vf_index(efx
, vf_txq
) ||
625 vf_txq
>= vf_max_tx_channels
||
626 bad_buf_count(buf_count
, EFX_MAX_DMAQ_SIZE
)) {
628 netif_err(efx
, hw
, efx
->net_dev
,
629 "ERROR: Invalid INIT_TXQ from %s: txq %d evq %d "
630 "buf_count %d\n", vf
->pci_name
, vf_txq
,
632 return VFDI_RC_EINVAL
;
635 mutex_lock(&vf
->txq_lock
);
636 if (__test_and_set_bit(req
->u
.init_txq
.index
, vf
->txq_mask
))
638 mutex_unlock(&vf
->txq_lock
);
639 efx_siena_sriov_bufs(efx
, buftbl
, req
->u
.init_txq
.addr
, buf_count
);
641 eth_filt_en
= vf
->tx_filter_mode
== VF_TX_FILTER_ON
;
643 label
= req
->u
.init_txq
.label
& EFX_FIELD_MASK(FRF_AZ_TX_DESCQ_LABEL
);
644 EFX_POPULATE_OWORD_8(reg
,
645 FRF_CZ_TX_DPT_Q_MASK_WIDTH
, min(efx
->vi_scale
, 1U),
646 FRF_CZ_TX_DPT_ETH_FILT_EN
, eth_filt_en
,
647 FRF_AZ_TX_DESCQ_EN
, 1,
648 FRF_AZ_TX_DESCQ_BUF_BASE_ID
, buftbl
,
649 FRF_AZ_TX_DESCQ_EVQ_ID
, abs_index(vf
, vf_evq
),
650 FRF_AZ_TX_DESCQ_LABEL
, label
,
651 FRF_AZ_TX_DESCQ_SIZE
, __ffs(buf_count
),
652 FRF_BZ_TX_NON_IP_DROP_DIS
, 1);
653 efx_writeo_table(efx
, ®
, FR_BZ_TX_DESC_PTR_TBL
,
654 abs_index(vf
, vf_txq
));
656 return VFDI_RC_SUCCESS
;
659 /* Returns true when efx_vfdi_fini_all_queues should wake */
660 static bool efx_vfdi_flush_wake(struct siena_vf
*vf
)
662 /* Ensure that all updates are visible to efx_vfdi_fini_all_queues() */
665 return (!vf
->txq_count
&& !vf
->rxq_count
) ||
666 atomic_read(&vf
->rxq_retry_count
);
669 static void efx_vfdi_flush_clear(struct siena_vf
*vf
)
671 memset(vf
->txq_mask
, 0, sizeof(vf
->txq_mask
));
673 memset(vf
->rxq_mask
, 0, sizeof(vf
->rxq_mask
));
675 memset(vf
->rxq_retry_mask
, 0, sizeof(vf
->rxq_retry_mask
));
676 atomic_set(&vf
->rxq_retry_count
, 0);
679 static int efx_vfdi_fini_all_queues(struct siena_vf
*vf
)
681 struct efx_nic
*efx
= vf
->efx
;
683 unsigned count
= efx_vf_size(efx
);
684 unsigned vf_offset
= EFX_VI_BASE
+ vf
->index
* efx_vf_size(efx
);
685 unsigned timeout
= HZ
;
686 unsigned index
, rxqs_count
;
687 MCDI_DECLARE_BUF(inbuf
, MC_CMD_FLUSH_RX_QUEUES_IN_LENMAX
);
690 BUILD_BUG_ON(VF_MAX_RX_QUEUES
>
691 MC_CMD_FLUSH_RX_QUEUES_IN_QID_OFST_MAXNUM
);
694 siena_prepare_flush(efx
);
697 /* Flush all the initialized queues */
699 for (index
= 0; index
< count
; ++index
) {
700 if (test_bit(index
, vf
->txq_mask
)) {
701 EFX_POPULATE_OWORD_2(reg
,
702 FRF_AZ_TX_FLUSH_DESCQ_CMD
, 1,
703 FRF_AZ_TX_FLUSH_DESCQ
,
705 efx_writeo(efx
, ®
, FR_AZ_TX_FLUSH_DESCQ
);
707 if (test_bit(index
, vf
->rxq_mask
)) {
708 MCDI_SET_ARRAY_DWORD(
709 inbuf
, FLUSH_RX_QUEUES_IN_QID_OFST
,
710 rxqs_count
, vf_offset
+ index
);
715 atomic_set(&vf
->rxq_retry_count
, 0);
716 while (timeout
&& (vf
->rxq_count
|| vf
->txq_count
)) {
717 rc
= efx_mcdi_rpc(efx
, MC_CMD_FLUSH_RX_QUEUES
, inbuf
,
718 MC_CMD_FLUSH_RX_QUEUES_IN_LEN(rxqs_count
),
722 timeout
= wait_event_timeout(vf
->flush_waitq
,
723 efx_vfdi_flush_wake(vf
),
726 for (index
= 0; index
< count
; ++index
) {
727 if (test_and_clear_bit(index
, vf
->rxq_retry_mask
)) {
728 atomic_dec(&vf
->rxq_retry_count
);
729 MCDI_SET_ARRAY_DWORD(
730 inbuf
, FLUSH_RX_QUEUES_IN_QID_OFST
,
731 rxqs_count
, vf_offset
+ index
);
738 siena_finish_flush(efx
);
741 /* Irrespective of success/failure, fini the queues */
743 for (index
= 0; index
< count
; ++index
) {
744 efx_writeo_table(efx
, ®
, FR_BZ_RX_DESC_PTR_TBL
,
746 efx_writeo_table(efx
, ®
, FR_BZ_TX_DESC_PTR_TBL
,
748 efx_writeo_table(efx
, ®
, FR_BZ_EVQ_PTR_TBL
,
750 efx_writeo_table(efx
, ®
, FR_BZ_TIMER_TBL
,
753 efx_siena_sriov_bufs(efx
, vf
->buftbl_base
, NULL
,
754 EFX_VF_BUFTBL_PER_VI
* efx_vf_size(efx
));
755 efx_vfdi_flush_clear(vf
);
759 return timeout
? 0 : VFDI_RC_ETIMEDOUT
;
762 static int efx_vfdi_insert_filter(struct siena_vf
*vf
)
764 struct efx_nic
*efx
= vf
->efx
;
765 struct siena_nic_data
*nic_data
= efx
->nic_data
;
766 struct vfdi_req
*req
= vf
->buf
.addr
;
767 unsigned vf_rxq
= req
->u
.mac_filter
.rxq
;
770 if (bad_vf_index(efx
, vf_rxq
) || vf
->rx_filtering
) {
772 netif_err(efx
, hw
, efx
->net_dev
,
773 "ERROR: Invalid INSERT_FILTER from %s: rxq %d "
774 "flags 0x%x\n", vf
->pci_name
, vf_rxq
,
775 req
->u
.mac_filter
.flags
);
776 return VFDI_RC_EINVAL
;
780 if (req
->u
.mac_filter
.flags
& VFDI_MAC_FILTER_FLAG_RSS
)
781 flags
|= EFX_FILTER_FLAG_RX_RSS
;
782 if (req
->u
.mac_filter
.flags
& VFDI_MAC_FILTER_FLAG_SCATTER
)
783 flags
|= EFX_FILTER_FLAG_RX_SCATTER
;
784 vf
->rx_filter_flags
= flags
;
785 vf
->rx_filter_qid
= vf_rxq
;
786 vf
->rx_filtering
= true;
788 efx_siena_sriov_reset_rx_filter(vf
);
789 queue_work(vfdi_workqueue
, &nic_data
->peer_work
);
791 return VFDI_RC_SUCCESS
;
794 static int efx_vfdi_remove_all_filters(struct siena_vf
*vf
)
796 struct efx_nic
*efx
= vf
->efx
;
797 struct siena_nic_data
*nic_data
= efx
->nic_data
;
799 vf
->rx_filtering
= false;
800 efx_siena_sriov_reset_rx_filter(vf
);
801 queue_work(vfdi_workqueue
, &nic_data
->peer_work
);
803 return VFDI_RC_SUCCESS
;
806 static int efx_vfdi_set_status_page(struct siena_vf
*vf
)
808 struct efx_nic
*efx
= vf
->efx
;
809 struct siena_nic_data
*nic_data
= efx
->nic_data
;
810 struct vfdi_req
*req
= vf
->buf
.addr
;
811 u64 page_count
= req
->u
.set_status_page
.peer_page_count
;
814 offsetof(struct vfdi_req
, u
.set_status_page
.peer_page_addr
[0]))
815 / sizeof(req
->u
.set_status_page
.peer_page_addr
[0]);
817 if (!req
->u
.set_status_page
.dma_addr
|| page_count
> max_page_count
) {
819 netif_err(efx
, hw
, efx
->net_dev
,
820 "ERROR: Invalid SET_STATUS_PAGE from %s\n",
822 return VFDI_RC_EINVAL
;
825 mutex_lock(&nic_data
->local_lock
);
826 mutex_lock(&vf
->status_lock
);
827 vf
->status_addr
= req
->u
.set_status_page
.dma_addr
;
829 kfree(vf
->peer_page_addrs
);
830 vf
->peer_page_addrs
= NULL
;
831 vf
->peer_page_count
= 0;
834 vf
->peer_page_addrs
= kcalloc(page_count
, sizeof(u64
),
836 if (vf
->peer_page_addrs
) {
837 memcpy(vf
->peer_page_addrs
,
838 req
->u
.set_status_page
.peer_page_addr
,
839 page_count
* sizeof(u64
));
840 vf
->peer_page_count
= page_count
;
844 __efx_siena_sriov_push_vf_status(vf
);
845 mutex_unlock(&vf
->status_lock
);
846 mutex_unlock(&nic_data
->local_lock
);
848 return VFDI_RC_SUCCESS
;
851 static int efx_vfdi_clear_status_page(struct siena_vf
*vf
)
853 mutex_lock(&vf
->status_lock
);
855 mutex_unlock(&vf
->status_lock
);
857 return VFDI_RC_SUCCESS
;
860 typedef int (*efx_vfdi_op_t
)(struct siena_vf
*vf
);
862 static const efx_vfdi_op_t vfdi_ops
[VFDI_OP_LIMIT
] = {
863 [VFDI_OP_INIT_EVQ
] = efx_vfdi_init_evq
,
864 [VFDI_OP_INIT_TXQ
] = efx_vfdi_init_txq
,
865 [VFDI_OP_INIT_RXQ
] = efx_vfdi_init_rxq
,
866 [VFDI_OP_FINI_ALL_QUEUES
] = efx_vfdi_fini_all_queues
,
867 [VFDI_OP_INSERT_FILTER
] = efx_vfdi_insert_filter
,
868 [VFDI_OP_REMOVE_ALL_FILTERS
] = efx_vfdi_remove_all_filters
,
869 [VFDI_OP_SET_STATUS_PAGE
] = efx_vfdi_set_status_page
,
870 [VFDI_OP_CLEAR_STATUS_PAGE
] = efx_vfdi_clear_status_page
,
873 static void efx_siena_sriov_vfdi(struct work_struct
*work
)
875 struct siena_vf
*vf
= container_of(work
, struct siena_vf
, req
);
876 struct efx_nic
*efx
= vf
->efx
;
877 struct vfdi_req
*req
= vf
->buf
.addr
;
878 struct efx_memcpy_req copy
[2];
881 /* Copy this page into the local address space */
882 memset(copy
, '\0', sizeof(copy
));
883 copy
[0].from_rid
= vf
->pci_rid
;
884 copy
[0].from_addr
= vf
->req_addr
;
885 copy
[0].to_rid
= efx
->pci_dev
->devfn
;
886 copy
[0].to_addr
= vf
->buf
.dma_addr
;
887 copy
[0].length
= EFX_PAGE_SIZE
;
888 rc
= efx_siena_sriov_memcpy(efx
, copy
, 1);
890 /* If we can't get the request, we can't reply to the caller */
892 netif_err(efx
, hw
, efx
->net_dev
,
893 "ERROR: Unable to fetch VFDI request from %s rc %d\n",
899 if (req
->op
< VFDI_OP_LIMIT
&& vfdi_ops
[req
->op
] != NULL
) {
900 rc
= vfdi_ops
[req
->op
](vf
);
902 netif_dbg(efx
, hw
, efx
->net_dev
,
903 "vfdi request %d from %s ok\n",
904 req
->op
, vf
->pci_name
);
907 netif_dbg(efx
, hw
, efx
->net_dev
,
908 "ERROR: Unrecognised request %d from VF %s addr "
909 "%llx\n", req
->op
, vf
->pci_name
,
910 (unsigned long long)vf
->req_addr
);
911 rc
= VFDI_RC_EOPNOTSUPP
;
914 /* Allow subsequent VF requests */
918 /* Respond to the request */
920 req
->op
= VFDI_OP_RESPONSE
;
922 memset(copy
, '\0', sizeof(copy
));
923 copy
[0].from_buf
= &req
->rc
;
924 copy
[0].to_rid
= vf
->pci_rid
;
925 copy
[0].to_addr
= vf
->req_addr
+ offsetof(struct vfdi_req
, rc
);
926 copy
[0].length
= sizeof(req
->rc
);
927 copy
[1].from_buf
= &req
->op
;
928 copy
[1].to_rid
= vf
->pci_rid
;
929 copy
[1].to_addr
= vf
->req_addr
+ offsetof(struct vfdi_req
, op
);
930 copy
[1].length
= sizeof(req
->op
);
932 (void)efx_siena_sriov_memcpy(efx
, copy
, ARRAY_SIZE(copy
));
937 /* After a reset the event queues inside the guests no longer exist. Fill the
938 * event ring in guest memory with VFDI reset events, then (re-initialise) the
939 * event queue to raise an interrupt. The guest driver will then recover.
942 static void efx_siena_sriov_reset_vf(struct siena_vf
*vf
,
943 struct efx_buffer
*buffer
)
945 struct efx_nic
*efx
= vf
->efx
;
946 struct efx_memcpy_req copy_req
[4];
948 unsigned int pos
, count
, k
, buftbl
, abs_evq
;
953 BUG_ON(buffer
->len
!= EFX_PAGE_SIZE
);
957 BUG_ON(vf
->evq0_count
& (vf
->evq0_count
- 1));
959 mutex_lock(&vf
->status_lock
);
960 EFX_POPULATE_QWORD_3(event
,
961 FSF_AZ_EV_CODE
, FSE_CZ_EV_CODE_USER_EV
,
962 VFDI_EV_SEQ
, vf
->msg_seqno
,
963 VFDI_EV_TYPE
, VFDI_EV_TYPE_RESET
);
965 for (pos
= 0; pos
< EFX_PAGE_SIZE
; pos
+= sizeof(event
))
966 memcpy(buffer
->addr
+ pos
, &event
, sizeof(event
));
968 for (pos
= 0; pos
< vf
->evq0_count
; pos
+= count
) {
969 count
= min_t(unsigned, vf
->evq0_count
- pos
,
970 ARRAY_SIZE(copy_req
));
971 for (k
= 0; k
< count
; k
++) {
972 copy_req
[k
].from_buf
= NULL
;
973 copy_req
[k
].from_rid
= efx
->pci_dev
->devfn
;
974 copy_req
[k
].from_addr
= buffer
->dma_addr
;
975 copy_req
[k
].to_rid
= vf
->pci_rid
;
976 copy_req
[k
].to_addr
= vf
->evq0_addrs
[pos
+ k
];
977 copy_req
[k
].length
= EFX_PAGE_SIZE
;
979 rc
= efx_siena_sriov_memcpy(efx
, copy_req
, count
);
982 netif_err(efx
, hw
, efx
->net_dev
,
983 "ERROR: Unable to notify %s of reset"
984 ": %d\n", vf
->pci_name
, -rc
);
989 /* Reinitialise, arm and trigger evq0 */
990 abs_evq
= abs_index(vf
, 0);
991 buftbl
= EFX_BUFTBL_EVQ_BASE(vf
, 0);
992 efx_siena_sriov_bufs(efx
, buftbl
, vf
->evq0_addrs
, vf
->evq0_count
);
994 EFX_POPULATE_OWORD_3(reg
,
995 FRF_CZ_TIMER_Q_EN
, 1,
996 FRF_CZ_HOST_NOTIFY_MODE
, 0,
997 FRF_CZ_TIMER_MODE
, FFE_CZ_TIMER_MODE_DIS
);
998 efx_writeo_table(efx
, ®
, FR_BZ_TIMER_TBL
, abs_evq
);
999 EFX_POPULATE_OWORD_3(reg
,
1001 FRF_AZ_EVQ_SIZE
, __ffs(vf
->evq0_count
),
1002 FRF_AZ_EVQ_BUF_BASE_ID
, buftbl
);
1003 efx_writeo_table(efx
, ®
, FR_BZ_EVQ_PTR_TBL
, abs_evq
);
1004 EFX_POPULATE_DWORD_1(ptr
, FRF_AZ_EVQ_RPTR
, 0);
1005 efx_writed(efx
, &ptr
, FR_BZ_EVQ_RPTR
+ FR_BZ_EVQ_RPTR_STEP
* abs_evq
);
1007 mutex_unlock(&vf
->status_lock
);
1010 static void efx_siena_sriov_reset_vf_work(struct work_struct
*work
)
1012 struct siena_vf
*vf
= container_of(work
, struct siena_vf
, req
);
1013 struct efx_nic
*efx
= vf
->efx
;
1014 struct efx_buffer buf
;
1016 if (!efx_nic_alloc_buffer(efx
, &buf
, EFX_PAGE_SIZE
, GFP_NOIO
)) {
1017 efx_siena_sriov_reset_vf(vf
, &buf
);
1018 efx_nic_free_buffer(efx
, &buf
);
1022 static void efx_siena_sriov_handle_no_channel(struct efx_nic
*efx
)
1024 netif_err(efx
, drv
, efx
->net_dev
,
1025 "ERROR: IOV requires MSI-X and 1 additional interrupt"
1026 "vector. IOV disabled\n");
1030 static int efx_siena_sriov_probe_channel(struct efx_channel
*channel
)
1032 struct siena_nic_data
*nic_data
= channel
->efx
->nic_data
;
1033 nic_data
->vfdi_channel
= channel
;
1039 efx_siena_sriov_get_channel_name(struct efx_channel
*channel
,
1040 char *buf
, size_t len
)
1042 snprintf(buf
, len
, "%s-iov", channel
->efx
->name
);
1045 static const struct efx_channel_type efx_siena_sriov_channel_type
= {
1046 .handle_no_channel
= efx_siena_sriov_handle_no_channel
,
1047 .pre_probe
= efx_siena_sriov_probe_channel
,
1048 .post_remove
= efx_channel_dummy_op_void
,
1049 .get_name
= efx_siena_sriov_get_channel_name
,
1050 /* no copy operation; channel must not be reallocated */
1051 .keep_eventq
= true,
1054 void efx_siena_sriov_probe(struct efx_nic
*efx
)
1061 if (efx_siena_sriov_cmd(efx
, false, &efx
->vi_scale
, &count
)) {
1062 netif_info(efx
, probe
, efx
->net_dev
, "no SR-IOV VFs probed\n");
1065 if (count
> 0 && count
> max_vfs
)
1068 /* efx_nic_dimension_resources() will reduce vf_count as appopriate */
1069 efx
->vf_count
= count
;
1071 efx
->extra_channel_type
[EFX_EXTRA_CHANNEL_IOV
] = &efx_siena_sriov_channel_type
;
1074 /* Copy the list of individual addresses into the vfdi_status.peers
1075 * array and auxiliary pages, protected by %local_lock. Drop that lock
1076 * and then broadcast the address list to every VF.
1078 static void efx_siena_sriov_peer_work(struct work_struct
*data
)
1080 struct siena_nic_data
*nic_data
= container_of(data
,
1081 struct siena_nic_data
,
1083 struct efx_nic
*efx
= nic_data
->efx
;
1084 struct vfdi_status
*vfdi_status
= nic_data
->vfdi_status
.addr
;
1085 struct siena_vf
*vf
;
1086 struct efx_local_addr
*local_addr
;
1087 struct vfdi_endpoint
*peer
;
1088 struct efx_endpoint_page
*epp
;
1089 struct list_head pages
;
1090 unsigned int peer_space
;
1091 unsigned int peer_count
;
1094 mutex_lock(&nic_data
->local_lock
);
1096 /* Move the existing peer pages off %local_page_list */
1097 INIT_LIST_HEAD(&pages
);
1098 list_splice_tail_init(&nic_data
->local_page_list
, &pages
);
1100 /* Populate the VF addresses starting from entry 1 (entry 0 is
1103 peer
= vfdi_status
->peers
+ 1;
1104 peer_space
= ARRAY_SIZE(vfdi_status
->peers
) - 1;
1106 for (pos
= 0; pos
< efx
->vf_count
; ++pos
) {
1107 vf
= nic_data
->vf
+ pos
;
1109 mutex_lock(&vf
->status_lock
);
1110 if (vf
->rx_filtering
&& !is_zero_ether_addr(vf
->addr
.mac_addr
)) {
1114 BUG_ON(peer_space
== 0);
1116 mutex_unlock(&vf
->status_lock
);
1119 /* Fill the remaining addresses */
1120 list_for_each_entry(local_addr
, &nic_data
->local_addr_list
, link
) {
1121 ether_addr_copy(peer
->mac_addr
, local_addr
->addr
);
1125 if (--peer_space
== 0) {
1126 if (list_empty(&pages
)) {
1127 epp
= kmalloc(sizeof(*epp
), GFP_KERNEL
);
1130 epp
->ptr
= dma_alloc_coherent(
1131 &efx
->pci_dev
->dev
, EFX_PAGE_SIZE
,
1132 &epp
->addr
, GFP_KERNEL
);
1138 epp
= list_first_entry(
1139 &pages
, struct efx_endpoint_page
, link
);
1140 list_del(&epp
->link
);
1143 list_add_tail(&epp
->link
, &nic_data
->local_page_list
);
1144 peer
= (struct vfdi_endpoint
*)epp
->ptr
;
1145 peer_space
= EFX_PAGE_SIZE
/ sizeof(struct vfdi_endpoint
);
1148 vfdi_status
->peer_count
= peer_count
;
1149 mutex_unlock(&nic_data
->local_lock
);
1151 /* Free any now unused endpoint pages */
1152 while (!list_empty(&pages
)) {
1153 epp
= list_first_entry(
1154 &pages
, struct efx_endpoint_page
, link
);
1155 list_del(&epp
->link
);
1156 dma_free_coherent(&efx
->pci_dev
->dev
, EFX_PAGE_SIZE
,
1157 epp
->ptr
, epp
->addr
);
1161 /* Finally, push the pages */
1162 for (pos
= 0; pos
< efx
->vf_count
; ++pos
) {
1163 vf
= nic_data
->vf
+ pos
;
1165 mutex_lock(&vf
->status_lock
);
1166 if (vf
->status_addr
)
1167 __efx_siena_sriov_push_vf_status(vf
);
1168 mutex_unlock(&vf
->status_lock
);
1172 static void efx_siena_sriov_free_local(struct efx_nic
*efx
)
1174 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1175 struct efx_local_addr
*local_addr
;
1176 struct efx_endpoint_page
*epp
;
1178 while (!list_empty(&nic_data
->local_addr_list
)) {
1179 local_addr
= list_first_entry(&nic_data
->local_addr_list
,
1180 struct efx_local_addr
, link
);
1181 list_del(&local_addr
->link
);
1185 while (!list_empty(&nic_data
->local_page_list
)) {
1186 epp
= list_first_entry(&nic_data
->local_page_list
,
1187 struct efx_endpoint_page
, link
);
1188 list_del(&epp
->link
);
1189 dma_free_coherent(&efx
->pci_dev
->dev
, EFX_PAGE_SIZE
,
1190 epp
->ptr
, epp
->addr
);
1195 static int efx_siena_sriov_vf_alloc(struct efx_nic
*efx
)
1198 struct siena_vf
*vf
;
1199 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1201 nic_data
->vf
= kcalloc(efx
->vf_count
, sizeof(*nic_data
->vf
),
1206 for (index
= 0; index
< efx
->vf_count
; ++index
) {
1207 vf
= nic_data
->vf
+ index
;
1211 vf
->rx_filter_id
= -1;
1212 vf
->tx_filter_mode
= VF_TX_FILTER_AUTO
;
1213 vf
->tx_filter_id
= -1;
1214 INIT_WORK(&vf
->req
, efx_siena_sriov_vfdi
);
1215 INIT_WORK(&vf
->reset_work
, efx_siena_sriov_reset_vf_work
);
1216 init_waitqueue_head(&vf
->flush_waitq
);
1217 mutex_init(&vf
->status_lock
);
1218 mutex_init(&vf
->txq_lock
);
1224 static void efx_siena_sriov_vfs_fini(struct efx_nic
*efx
)
1226 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1227 struct siena_vf
*vf
;
1230 for (pos
= 0; pos
< efx
->vf_count
; ++pos
) {
1231 vf
= nic_data
->vf
+ pos
;
1233 efx_nic_free_buffer(efx
, &vf
->buf
);
1234 kfree(vf
->peer_page_addrs
);
1235 vf
->peer_page_addrs
= NULL
;
1236 vf
->peer_page_count
= 0;
1242 static int efx_siena_sriov_vfs_init(struct efx_nic
*efx
)
1244 struct pci_dev
*pci_dev
= efx
->pci_dev
;
1245 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1246 unsigned index
, devfn
, sriov
, buftbl_base
;
1248 struct siena_vf
*vf
;
1251 sriov
= pci_find_ext_capability(pci_dev
, PCI_EXT_CAP_ID_SRIOV
);
1255 pci_read_config_word(pci_dev
, sriov
+ PCI_SRIOV_VF_OFFSET
, &offset
);
1256 pci_read_config_word(pci_dev
, sriov
+ PCI_SRIOV_VF_STRIDE
, &stride
);
1258 buftbl_base
= nic_data
->vf_buftbl_base
;
1259 devfn
= pci_dev
->devfn
+ offset
;
1260 for (index
= 0; index
< efx
->vf_count
; ++index
) {
1261 vf
= nic_data
->vf
+ index
;
1263 /* Reserve buffer entries */
1264 vf
->buftbl_base
= buftbl_base
;
1265 buftbl_base
+= EFX_VF_BUFTBL_PER_VI
* efx_vf_size(efx
);
1267 vf
->pci_rid
= devfn
;
1268 snprintf(vf
->pci_name
, sizeof(vf
->pci_name
),
1269 "%04x:%02x:%02x.%d",
1270 pci_domain_nr(pci_dev
->bus
), pci_dev
->bus
->number
,
1271 PCI_SLOT(devfn
), PCI_FUNC(devfn
));
1273 rc
= efx_nic_alloc_buffer(efx
, &vf
->buf
, EFX_PAGE_SIZE
,
1284 efx_siena_sriov_vfs_fini(efx
);
1288 int efx_siena_sriov_init(struct efx_nic
*efx
)
1290 struct net_device
*net_dev
= efx
->net_dev
;
1291 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1292 struct vfdi_status
*vfdi_status
;
1295 /* Ensure there's room for vf_channel */
1296 BUILD_BUG_ON(EFX_MAX_CHANNELS
+ 1 >= EFX_VI_BASE
);
1297 /* Ensure that VI_BASE is aligned on VI_SCALE */
1298 BUILD_BUG_ON(EFX_VI_BASE
& ((1 << EFX_VI_SCALE_MAX
) - 1));
1300 if (efx
->vf_count
== 0)
1303 rc
= efx_siena_sriov_cmd(efx
, true, NULL
, NULL
);
1307 rc
= efx_nic_alloc_buffer(efx
, &nic_data
->vfdi_status
,
1308 sizeof(*vfdi_status
), GFP_KERNEL
);
1311 vfdi_status
= nic_data
->vfdi_status
.addr
;
1312 memset(vfdi_status
, 0, sizeof(*vfdi_status
));
1313 vfdi_status
->version
= 1;
1314 vfdi_status
->length
= sizeof(*vfdi_status
);
1315 vfdi_status
->max_tx_channels
= vf_max_tx_channels
;
1316 vfdi_status
->vi_scale
= efx
->vi_scale
;
1317 vfdi_status
->rss_rxq_count
= efx
->rss_spread
;
1318 vfdi_status
->peer_count
= 1 + efx
->vf_count
;
1319 vfdi_status
->timer_quantum_ns
= efx
->timer_quantum_ns
;
1321 rc
= efx_siena_sriov_vf_alloc(efx
);
1325 mutex_init(&nic_data
->local_lock
);
1326 INIT_WORK(&nic_data
->peer_work
, efx_siena_sriov_peer_work
);
1327 INIT_LIST_HEAD(&nic_data
->local_addr_list
);
1328 INIT_LIST_HEAD(&nic_data
->local_page_list
);
1330 rc
= efx_siena_sriov_vfs_init(efx
);
1335 ether_addr_copy(vfdi_status
->peers
[0].mac_addr
, net_dev
->dev_addr
);
1336 efx
->vf_init_count
= efx
->vf_count
;
1339 efx_siena_sriov_usrev(efx
, true);
1341 /* At this point we must be ready to accept VFDI requests */
1343 rc
= pci_enable_sriov(efx
->pci_dev
, efx
->vf_count
);
1347 netif_info(efx
, probe
, net_dev
,
1348 "enabled SR-IOV for %d VFs, %d VI per VF\n",
1349 efx
->vf_count
, efx_vf_size(efx
));
1353 efx_siena_sriov_usrev(efx
, false);
1355 efx
->vf_init_count
= 0;
1357 efx_siena_sriov_vfs_fini(efx
);
1359 cancel_work_sync(&nic_data
->peer_work
);
1360 efx_siena_sriov_free_local(efx
);
1361 kfree(nic_data
->vf
);
1363 efx_nic_free_buffer(efx
, &nic_data
->vfdi_status
);
1365 efx_siena_sriov_cmd(efx
, false, NULL
, NULL
);
1370 void efx_siena_sriov_fini(struct efx_nic
*efx
)
1372 struct siena_vf
*vf
;
1374 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1376 if (efx
->vf_init_count
== 0)
1379 /* Disable all interfaces to reconfiguration */
1380 BUG_ON(nic_data
->vfdi_channel
->enabled
);
1381 efx_siena_sriov_usrev(efx
, false);
1383 efx
->vf_init_count
= 0;
1386 /* Flush all reconfiguration work */
1387 for (pos
= 0; pos
< efx
->vf_count
; ++pos
) {
1388 vf
= nic_data
->vf
+ pos
;
1389 cancel_work_sync(&vf
->req
);
1390 cancel_work_sync(&vf
->reset_work
);
1392 cancel_work_sync(&nic_data
->peer_work
);
1394 pci_disable_sriov(efx
->pci_dev
);
1396 /* Tear down back-end state */
1397 efx_siena_sriov_vfs_fini(efx
);
1398 efx_siena_sriov_free_local(efx
);
1399 kfree(nic_data
->vf
);
1400 efx_nic_free_buffer(efx
, &nic_data
->vfdi_status
);
1401 efx_siena_sriov_cmd(efx
, false, NULL
, NULL
);
1404 void efx_siena_sriov_event(struct efx_channel
*channel
, efx_qword_t
*event
)
1406 struct efx_nic
*efx
= channel
->efx
;
1407 struct siena_vf
*vf
;
1408 unsigned qid
, seq
, type
, data
;
1410 qid
= EFX_QWORD_FIELD(*event
, FSF_CZ_USER_QID
);
1412 /* USR_EV_REG_VALUE is dword0, so access the VFDI_EV fields directly */
1413 BUILD_BUG_ON(FSF_CZ_USER_EV_REG_VALUE_LBN
!= 0);
1414 seq
= EFX_QWORD_FIELD(*event
, VFDI_EV_SEQ
);
1415 type
= EFX_QWORD_FIELD(*event
, VFDI_EV_TYPE
);
1416 data
= EFX_QWORD_FIELD(*event
, VFDI_EV_DATA
);
1418 netif_vdbg(efx
, hw
, efx
->net_dev
,
1419 "USR_EV event from qid %d seq 0x%x type %d data 0x%x\n",
1420 qid
, seq
, type
, data
);
1422 if (map_vi_index(efx
, qid
, &vf
, NULL
))
1427 if (type
== VFDI_EV_TYPE_REQ_WORD0
) {
1429 vf
->req_type
= VFDI_EV_TYPE_REQ_WORD0
;
1430 vf
->req_seqno
= seq
+ 1;
1432 } else if (seq
!= (vf
->req_seqno
++ & 0xff) || type
!= vf
->req_type
)
1435 switch (vf
->req_type
) {
1436 case VFDI_EV_TYPE_REQ_WORD0
:
1437 case VFDI_EV_TYPE_REQ_WORD1
:
1438 case VFDI_EV_TYPE_REQ_WORD2
:
1439 vf
->req_addr
|= (u64
)data
<< (vf
->req_type
<< 4);
1443 case VFDI_EV_TYPE_REQ_WORD3
:
1444 vf
->req_addr
|= (u64
)data
<< 48;
1445 vf
->req_type
= VFDI_EV_TYPE_REQ_WORD0
;
1447 queue_work(vfdi_workqueue
, &vf
->req
);
1452 if (net_ratelimit())
1453 netif_err(efx
, hw
, efx
->net_dev
,
1454 "ERROR: Screaming VFDI request from %s\n",
1456 /* Reset the request and sequence number */
1457 vf
->req_type
= VFDI_EV_TYPE_REQ_WORD0
;
1458 vf
->req_seqno
= seq
+ 1;
1461 void efx_siena_sriov_flr(struct efx_nic
*efx
, unsigned vf_i
)
1463 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1464 struct siena_vf
*vf
;
1466 if (vf_i
> efx
->vf_init_count
)
1468 vf
= nic_data
->vf
+ vf_i
;
1469 netif_info(efx
, hw
, efx
->net_dev
,
1470 "FLR on VF %s\n", vf
->pci_name
);
1472 vf
->status_addr
= 0;
1473 efx_vfdi_remove_all_filters(vf
);
1474 efx_vfdi_flush_clear(vf
);
1479 int efx_siena_sriov_mac_address_changed(struct efx_nic
*efx
)
1481 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1482 struct vfdi_status
*vfdi_status
= nic_data
->vfdi_status
.addr
;
1484 if (!efx
->vf_init_count
)
1486 ether_addr_copy(vfdi_status
->peers
[0].mac_addr
,
1487 efx
->net_dev
->dev_addr
);
1488 queue_work(vfdi_workqueue
, &nic_data
->peer_work
);
1493 void efx_siena_sriov_tx_flush_done(struct efx_nic
*efx
, efx_qword_t
*event
)
1495 struct siena_vf
*vf
;
1496 unsigned queue
, qid
;
1498 queue
= EFX_QWORD_FIELD(*event
, FSF_AZ_DRIVER_EV_SUBDATA
);
1499 if (map_vi_index(efx
, queue
, &vf
, &qid
))
1501 /* Ignore flush completions triggered by an FLR */
1502 if (!test_bit(qid
, vf
->txq_mask
))
1505 __clear_bit(qid
, vf
->txq_mask
);
1508 if (efx_vfdi_flush_wake(vf
))
1509 wake_up(&vf
->flush_waitq
);
1512 void efx_siena_sriov_rx_flush_done(struct efx_nic
*efx
, efx_qword_t
*event
)
1514 struct siena_vf
*vf
;
1515 unsigned ev_failed
, queue
, qid
;
1517 queue
= EFX_QWORD_FIELD(*event
, FSF_AZ_DRIVER_EV_RX_DESCQ_ID
);
1518 ev_failed
= EFX_QWORD_FIELD(*event
,
1519 FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL
);
1520 if (map_vi_index(efx
, queue
, &vf
, &qid
))
1522 if (!test_bit(qid
, vf
->rxq_mask
))
1526 set_bit(qid
, vf
->rxq_retry_mask
);
1527 atomic_inc(&vf
->rxq_retry_count
);
1529 __clear_bit(qid
, vf
->rxq_mask
);
1532 if (efx_vfdi_flush_wake(vf
))
1533 wake_up(&vf
->flush_waitq
);
1536 /* Called from napi. Schedule the reset work item */
1537 void efx_siena_sriov_desc_fetch_err(struct efx_nic
*efx
, unsigned dmaq
)
1539 struct siena_vf
*vf
;
1542 if (map_vi_index(efx
, dmaq
, &vf
, &rel
))
1545 if (net_ratelimit())
1546 netif_err(efx
, hw
, efx
->net_dev
,
1547 "VF %d DMA Q %d reports descriptor fetch error.\n",
1549 queue_work(vfdi_workqueue
, &vf
->reset_work
);
1553 void efx_siena_sriov_reset(struct efx_nic
*efx
)
1555 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1557 struct efx_buffer buf
;
1558 struct siena_vf
*vf
;
1562 if (efx
->vf_init_count
== 0)
1565 efx_siena_sriov_usrev(efx
, true);
1566 (void)efx_siena_sriov_cmd(efx
, true, NULL
, NULL
);
1568 if (efx_nic_alloc_buffer(efx
, &buf
, EFX_PAGE_SIZE
, GFP_NOIO
))
1571 for (vf_i
= 0; vf_i
< efx
->vf_init_count
; ++vf_i
) {
1572 vf
= nic_data
->vf
+ vf_i
;
1573 efx_siena_sriov_reset_vf(vf
, &buf
);
1576 efx_nic_free_buffer(efx
, &buf
);
1579 int efx_init_sriov(void)
1581 /* A single threaded workqueue is sufficient. efx_siena_sriov_vfdi() and
1582 * efx_siena_sriov_peer_work() spend almost all their time sleeping for
1583 * MCDI to complete anyway
1585 vfdi_workqueue
= create_singlethread_workqueue("sfc_vfdi");
1586 if (!vfdi_workqueue
)
1591 void efx_fini_sriov(void)
1593 destroy_workqueue(vfdi_workqueue
);
1596 int efx_siena_sriov_set_vf_mac(struct efx_nic
*efx
, int vf_i
, u8
*mac
)
1598 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1599 struct siena_vf
*vf
;
1601 if (vf_i
>= efx
->vf_init_count
)
1603 vf
= nic_data
->vf
+ vf_i
;
1605 mutex_lock(&vf
->status_lock
);
1606 ether_addr_copy(vf
->addr
.mac_addr
, mac
);
1607 __efx_siena_sriov_update_vf_addr(vf
);
1608 mutex_unlock(&vf
->status_lock
);
1613 int efx_siena_sriov_set_vf_vlan(struct efx_nic
*efx
, int vf_i
,
1616 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1617 struct siena_vf
*vf
;
1620 if (vf_i
>= efx
->vf_init_count
)
1622 vf
= nic_data
->vf
+ vf_i
;
1624 mutex_lock(&vf
->status_lock
);
1625 tci
= (vlan
& VLAN_VID_MASK
) | ((qos
& 0x7) << VLAN_PRIO_SHIFT
);
1626 vf
->addr
.tci
= htons(tci
);
1627 __efx_siena_sriov_update_vf_addr(vf
);
1628 mutex_unlock(&vf
->status_lock
);
1633 int efx_siena_sriov_set_vf_spoofchk(struct efx_nic
*efx
, int vf_i
,
1636 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1637 struct siena_vf
*vf
;
1640 if (vf_i
>= efx
->vf_init_count
)
1642 vf
= nic_data
->vf
+ vf_i
;
1644 mutex_lock(&vf
->txq_lock
);
1645 if (vf
->txq_count
== 0) {
1646 vf
->tx_filter_mode
=
1647 spoofchk
? VF_TX_FILTER_ON
: VF_TX_FILTER_OFF
;
1650 /* This cannot be changed while TX queues are running */
1653 mutex_unlock(&vf
->txq_lock
);
1657 int efx_siena_sriov_get_vf_config(struct efx_nic
*efx
, int vf_i
,
1658 struct ifla_vf_info
*ivi
)
1660 struct siena_nic_data
*nic_data
= efx
->nic_data
;
1661 struct siena_vf
*vf
;
1664 if (vf_i
>= efx
->vf_init_count
)
1666 vf
= nic_data
->vf
+ vf_i
;
1669 ether_addr_copy(ivi
->mac
, vf
->addr
.mac_addr
);
1670 ivi
->max_tx_rate
= 0;
1671 ivi
->min_tx_rate
= 0;
1672 tci
= ntohs(vf
->addr
.tci
);
1673 ivi
->vlan
= tci
& VLAN_VID_MASK
;
1674 ivi
->qos
= (tci
>> VLAN_PRIO_SHIFT
) & 0x7;
1675 ivi
->spoofchk
= vf
->tx_filter_mode
== VF_TX_FILTER_ON
;
1680 bool efx_siena_sriov_wanted(struct efx_nic
*efx
)
1682 return efx
->vf_count
!= 0;
1685 int efx_siena_sriov_configure(struct efx_nic
*efx
, int num_vfs
)