1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel IFC VF NIC driver for virtio dataplane offloading
5 * Copyright (C) 2020 Intel Corporation.
7 * Author: Zhu Lingshan <lingshan.zhu@intel.com>
11 #include "ifcvf_base.h"
13 struct ifcvf_adapter *vf_to_adapter(struct ifcvf_hw *hw)
15 return container_of(hw, struct ifcvf_adapter, vf);
18 u16 ifcvf_set_vq_vector(struct ifcvf_hw *hw, u16 qid, int vector)
20 struct virtio_pci_common_cfg __iomem *cfg = hw->common_cfg;
22 vp_iowrite16(qid, &cfg->queue_select);
23 vp_iowrite16(vector, &cfg->queue_msix_vector);
25 return vp_ioread16(&cfg->queue_msix_vector);
28 u16 ifcvf_set_config_vector(struct ifcvf_hw *hw, int vector)
30 struct virtio_pci_common_cfg __iomem *cfg = hw->common_cfg;
33 vp_iowrite16(vector, &cfg->msix_config);
35 return vp_ioread16(&cfg->msix_config);
38 static void __iomem *get_cap_addr(struct ifcvf_hw *hw,
39 struct virtio_pci_cap *cap)
41 struct ifcvf_adapter *ifcvf;
46 length = le32_to_cpu(cap->length);
47 offset = le32_to_cpu(cap->offset);
50 ifcvf= vf_to_adapter(hw);
53 if (bar >= IFCVF_PCI_MAX_RESOURCE) {
55 "Invalid bar number %u to get capabilities\n", bar);
59 if (offset + length > pci_resource_len(pdev, bar)) {
61 "offset(%u) + len(%u) overflows bar%u's capability\n",
66 return hw->base[bar] + offset;
69 static int ifcvf_read_config_range(struct pci_dev *dev,
70 uint32_t *val, int size, int where)
74 for (i = 0; i < size; i += 4) {
75 ret = pci_read_config_dword(dev, where + i, val + i / 4);
83 int ifcvf_init_hw(struct ifcvf_hw *hw, struct pci_dev *pdev)
85 struct virtio_pci_cap cap;
91 ret = pci_read_config_byte(pdev, PCI_CAPABILITY_LIST, &pos);
93 IFCVF_ERR(pdev, "Failed to read PCI capability list\n");
98 ret = ifcvf_read_config_range(pdev, (u32 *)&cap,
102 "Failed to get PCI capability at %x\n", pos);
106 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
109 switch (cap.cfg_type) {
110 case VIRTIO_PCI_CAP_COMMON_CFG:
111 hw->common_cfg = get_cap_addr(hw, &cap);
112 IFCVF_DBG(pdev, "hw->common_cfg = %p\n",
115 case VIRTIO_PCI_CAP_NOTIFY_CFG:
116 pci_read_config_dword(pdev, pos + sizeof(cap),
117 &hw->notify_off_multiplier);
118 hw->notify_bar = cap.bar;
119 hw->notify_base = get_cap_addr(hw, &cap);
120 hw->notify_base_pa = pci_resource_start(pdev, cap.bar) +
121 le32_to_cpu(cap.offset);
122 IFCVF_DBG(pdev, "hw->notify_base = %p\n",
125 case VIRTIO_PCI_CAP_ISR_CFG:
126 hw->isr = get_cap_addr(hw, &cap);
127 IFCVF_DBG(pdev, "hw->isr = %p\n", hw->isr);
129 case VIRTIO_PCI_CAP_DEVICE_CFG:
130 hw->dev_cfg = get_cap_addr(hw, &cap);
131 IFCVF_DBG(pdev, "hw->dev_cfg = %p\n", hw->dev_cfg);
139 if (hw->common_cfg == NULL || hw->notify_base == NULL ||
140 hw->isr == NULL || hw->dev_cfg == NULL) {
141 IFCVF_ERR(pdev, "Incomplete PCI capabilities\n");
145 hw->nr_vring = vp_ioread16(&hw->common_cfg->num_queues);
147 for (i = 0; i < hw->nr_vring; i++) {
148 vp_iowrite16(i, &hw->common_cfg->queue_select);
149 notify_off = vp_ioread16(&hw->common_cfg->queue_notify_off);
150 hw->vring[i].notify_addr = hw->notify_base +
151 notify_off * hw->notify_off_multiplier;
152 hw->vring[i].notify_pa = hw->notify_base_pa +
153 notify_off * hw->notify_off_multiplier;
154 hw->vring[i].irq = -EINVAL;
157 hw->lm_cfg = hw->base[IFCVF_LM_BAR];
160 "PCI capability mapping: common cfg: %p, notify base: %p\n, isr cfg: %p, device cfg: %p, multiplier: %u\n",
161 hw->common_cfg, hw->notify_base, hw->isr,
162 hw->dev_cfg, hw->notify_off_multiplier);
164 hw->vqs_reused_irq = -EINVAL;
165 hw->config_irq = -EINVAL;
170 u8 ifcvf_get_status(struct ifcvf_hw *hw)
172 return vp_ioread8(&hw->common_cfg->device_status);
175 void ifcvf_set_status(struct ifcvf_hw *hw, u8 status)
177 vp_iowrite8(status, &hw->common_cfg->device_status);
180 void ifcvf_reset(struct ifcvf_hw *hw)
182 hw->config_cb.callback = NULL;
183 hw->config_cb.private = NULL;
185 ifcvf_set_status(hw, 0);
186 /* flush set_status, make sure VF is stopped, reset */
187 ifcvf_get_status(hw);
190 static void ifcvf_add_status(struct ifcvf_hw *hw, u8 status)
193 status |= ifcvf_get_status(hw);
195 ifcvf_set_status(hw, status);
196 ifcvf_get_status(hw);
199 u64 ifcvf_get_hw_features(struct ifcvf_hw *hw)
201 struct virtio_pci_common_cfg __iomem *cfg = hw->common_cfg;
202 u32 features_lo, features_hi;
205 vp_iowrite32(0, &cfg->device_feature_select);
206 features_lo = vp_ioread32(&cfg->device_feature);
208 vp_iowrite32(1, &cfg->device_feature_select);
209 features_hi = vp_ioread32(&cfg->device_feature);
211 features = ((u64)features_hi << 32) | features_lo;
216 u64 ifcvf_get_features(struct ifcvf_hw *hw)
218 return hw->hw_features;
221 int ifcvf_verify_min_features(struct ifcvf_hw *hw, u64 features)
223 struct ifcvf_adapter *ifcvf = vf_to_adapter(hw);
225 if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM)) && features) {
226 IFCVF_ERR(ifcvf->pdev, "VIRTIO_F_ACCESS_PLATFORM is not negotiated\n");
233 u32 ifcvf_get_config_size(struct ifcvf_hw *hw)
235 struct ifcvf_adapter *adapter;
238 adapter = vf_to_adapter(hw);
239 switch (hw->dev_type) {
241 config_size = sizeof(struct virtio_net_config);
243 case VIRTIO_ID_BLOCK:
244 config_size = sizeof(struct virtio_blk_config);
248 IFCVF_ERR(adapter->pdev, "VIRTIO ID %u not supported\n", hw->dev_type);
254 void ifcvf_read_dev_config(struct ifcvf_hw *hw, u64 offset,
255 void *dst, int length)
257 u8 old_gen, new_gen, *p;
260 WARN_ON(offset + length > hw->config_size);
262 old_gen = vp_ioread8(&hw->common_cfg->config_generation);
264 for (i = 0; i < length; i++)
265 *p++ = vp_ioread8(hw->dev_cfg + offset + i);
267 new_gen = vp_ioread8(&hw->common_cfg->config_generation);
268 } while (old_gen != new_gen);
271 void ifcvf_write_dev_config(struct ifcvf_hw *hw, u64 offset,
272 const void *src, int length)
278 WARN_ON(offset + length > hw->config_size);
279 for (i = 0; i < length; i++)
280 vp_iowrite8(*p++, hw->dev_cfg + offset + i);
283 static void ifcvf_set_features(struct ifcvf_hw *hw, u64 features)
285 struct virtio_pci_common_cfg __iomem *cfg = hw->common_cfg;
287 vp_iowrite32(0, &cfg->guest_feature_select);
288 vp_iowrite32((u32)features, &cfg->guest_feature);
290 vp_iowrite32(1, &cfg->guest_feature_select);
291 vp_iowrite32(features >> 32, &cfg->guest_feature);
294 static int ifcvf_config_features(struct ifcvf_hw *hw)
296 struct ifcvf_adapter *ifcvf;
298 ifcvf = vf_to_adapter(hw);
299 ifcvf_set_features(hw, hw->req_features);
300 ifcvf_add_status(hw, VIRTIO_CONFIG_S_FEATURES_OK);
302 if (!(ifcvf_get_status(hw) & VIRTIO_CONFIG_S_FEATURES_OK)) {
303 IFCVF_ERR(ifcvf->pdev, "Failed to set FEATURES_OK status\n");
310 u16 ifcvf_get_vq_state(struct ifcvf_hw *hw, u16 qid)
312 struct ifcvf_lm_cfg __iomem *ifcvf_lm;
313 void __iomem *avail_idx_addr;
317 ifcvf_lm = (struct ifcvf_lm_cfg __iomem *)hw->lm_cfg;
318 q_pair_id = qid / hw->nr_vring;
319 avail_idx_addr = &ifcvf_lm->vring_lm_cfg[q_pair_id].idx_addr[qid % 2];
320 last_avail_idx = vp_ioread16(avail_idx_addr);
322 return last_avail_idx;
325 int ifcvf_set_vq_state(struct ifcvf_hw *hw, u16 qid, u16 num)
327 struct ifcvf_lm_cfg __iomem *ifcvf_lm;
328 void __iomem *avail_idx_addr;
331 ifcvf_lm = (struct ifcvf_lm_cfg __iomem *)hw->lm_cfg;
332 q_pair_id = qid / hw->nr_vring;
333 avail_idx_addr = &ifcvf_lm->vring_lm_cfg[q_pair_id].idx_addr[qid % 2];
334 hw->vring[qid].last_avail_idx = num;
335 vp_iowrite16(num, avail_idx_addr);
340 static int ifcvf_hw_enable(struct ifcvf_hw *hw)
342 struct virtio_pci_common_cfg __iomem *cfg;
345 cfg = hw->common_cfg;
346 for (i = 0; i < hw->nr_vring; i++) {
347 if (!hw->vring[i].ready)
350 vp_iowrite16(i, &cfg->queue_select);
351 vp_iowrite64_twopart(hw->vring[i].desc, &cfg->queue_desc_lo,
352 &cfg->queue_desc_hi);
353 vp_iowrite64_twopart(hw->vring[i].avail, &cfg->queue_avail_lo,
354 &cfg->queue_avail_hi);
355 vp_iowrite64_twopart(hw->vring[i].used, &cfg->queue_used_lo,
356 &cfg->queue_used_hi);
357 vp_iowrite16(hw->vring[i].size, &cfg->queue_size);
358 ifcvf_set_vq_state(hw, i, hw->vring[i].last_avail_idx);
359 vp_iowrite16(1, &cfg->queue_enable);
365 static void ifcvf_hw_disable(struct ifcvf_hw *hw)
369 ifcvf_set_config_vector(hw, VIRTIO_MSI_NO_VECTOR);
370 for (i = 0; i < hw->nr_vring; i++) {
371 ifcvf_set_vq_vector(hw, i, VIRTIO_MSI_NO_VECTOR);
375 int ifcvf_start_hw(struct ifcvf_hw *hw)
378 ifcvf_add_status(hw, VIRTIO_CONFIG_S_ACKNOWLEDGE);
379 ifcvf_add_status(hw, VIRTIO_CONFIG_S_DRIVER);
381 if (ifcvf_config_features(hw) < 0)
384 if (ifcvf_hw_enable(hw) < 0)
387 ifcvf_add_status(hw, VIRTIO_CONFIG_S_DRIVER_OK);
392 void ifcvf_stop_hw(struct ifcvf_hw *hw)
394 ifcvf_hw_disable(hw);
398 void ifcvf_notify_queue(struct ifcvf_hw *hw, u16 qid)
400 vp_iowrite16(qid, hw->vring[qid].notify_addr);