1 /* QLogic qed NIC Driver
2 * Copyright (c) 2015 QLogic Corporation
4 * This software is available under the terms of the GNU General Public License
5 * (GPL) Version 2, available from the file COPYING in the main directory of
12 #include "qed_init_ops.h"
15 #include "qed_reg_addr.h"
17 #include "qed_sriov.h"
21 static int qed_sp_vf_start(struct qed_hwfn *p_hwfn,
22 u32 concrete_vfid, u16 opaque_vfid)
24 struct vf_start_ramrod_data *p_ramrod = NULL;
25 struct qed_spq_entry *p_ent = NULL;
26 struct qed_sp_init_data init_data;
30 memset(&init_data, 0, sizeof(init_data));
31 init_data.cid = qed_spq_get_cid(p_hwfn);
32 init_data.opaque_fid = opaque_vfid;
33 init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
35 rc = qed_sp_init_request(p_hwfn, &p_ent,
36 COMMON_RAMROD_VF_START,
37 PROTOCOLID_COMMON, &init_data);
41 p_ramrod = &p_ent->ramrod.vf_start;
43 p_ramrod->vf_id = GET_FIELD(concrete_vfid, PXP_CONCRETE_FID_VFID);
44 p_ramrod->opaque_fid = cpu_to_le16(opaque_vfid);
46 p_ramrod->personality = PERSONALITY_ETH;
48 return qed_spq_post(p_hwfn, p_ent, NULL);
51 bool qed_iov_is_valid_vfid(struct qed_hwfn *p_hwfn,
52 int rel_vf_id, bool b_enabled_only)
54 if (!p_hwfn->pf_iov_info) {
55 DP_NOTICE(p_hwfn->cdev, "No iov info\n");
59 if ((rel_vf_id >= p_hwfn->cdev->p_iov_info->total_vfs) ||
63 if ((!p_hwfn->pf_iov_info->vfs_array[rel_vf_id].b_init) &&
70 static struct qed_vf_info *qed_iov_get_vf_info(struct qed_hwfn *p_hwfn,
74 struct qed_vf_info *vf = NULL;
76 if (!p_hwfn->pf_iov_info) {
77 DP_NOTICE(p_hwfn->cdev, "No iov info\n");
81 if (qed_iov_is_valid_vfid(p_hwfn, relative_vf_id, b_enabled_only))
82 vf = &p_hwfn->pf_iov_info->vfs_array[relative_vf_id];
84 DP_ERR(p_hwfn, "qed_iov_get_vf_info: VF[%d] is not enabled\n",
90 static int qed_iov_pci_cfg_info(struct qed_dev *cdev)
92 struct qed_hw_sriov_info *iov = cdev->p_iov_info;
95 DP_VERBOSE(cdev, QED_MSG_IOV, "sriov ext pos %d\n", pos);
96 pci_read_config_word(cdev->pdev, pos + PCI_SRIOV_CTRL, &iov->ctrl);
98 pci_read_config_word(cdev->pdev,
99 pos + PCI_SRIOV_TOTAL_VF, &iov->total_vfs);
100 pci_read_config_word(cdev->pdev,
101 pos + PCI_SRIOV_INITIAL_VF, &iov->initial_vfs);
103 pci_read_config_word(cdev->pdev, pos + PCI_SRIOV_NUM_VF, &iov->num_vfs);
107 "Number of VFs are already set to non-zero value. Ignoring PCI configuration value\n");
111 pci_read_config_word(cdev->pdev,
112 pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
114 pci_read_config_word(cdev->pdev,
115 pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
117 pci_read_config_word(cdev->pdev,
118 pos + PCI_SRIOV_VF_DID, &iov->vf_device_id);
120 pci_read_config_dword(cdev->pdev,
121 pos + PCI_SRIOV_SUP_PGSIZE, &iov->pgsz);
123 pci_read_config_dword(cdev->pdev, pos + PCI_SRIOV_CAP, &iov->cap);
125 pci_read_config_byte(cdev->pdev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
129 "IOV info: nres %d, cap 0x%x, ctrl 0x%x, total %d, initial %d, num vfs %d, offset %d, stride %d, page size 0x%x\n",
135 iov->nr_virtfn, iov->offset, iov->stride, iov->pgsz);
137 /* Some sanity checks */
138 if (iov->num_vfs > NUM_OF_VFS(cdev) ||
139 iov->total_vfs > NUM_OF_VFS(cdev)) {
140 /* This can happen only due to a bug. In this case we set
141 * num_vfs to zero to avoid memory corruption in the code that
142 * assumes max number of vfs
145 "IOV: Unexpected number of vfs set: %d setting num_vf to zero\n",
155 static void qed_iov_clear_vf_igu_blocks(struct qed_hwfn *p_hwfn,
156 struct qed_ptt *p_ptt)
158 struct qed_igu_block *p_sb;
162 if (!p_hwfn->hw_info.p_igu_info) {
164 "qed_iov_clear_vf_igu_blocks IGU Info not initialized\n");
168 for (sb_id = 0; sb_id < QED_MAPPING_MEMORY_SIZE(p_hwfn->cdev);
170 p_sb = &p_hwfn->hw_info.p_igu_info->igu_map.igu_blocks[sb_id];
171 if ((p_sb->status & QED_IGU_STATUS_FREE) &&
172 !(p_sb->status & QED_IGU_STATUS_PF)) {
173 val = qed_rd(p_hwfn, p_ptt,
174 IGU_REG_MAPPING_MEMORY + sb_id * 4);
175 SET_FIELD(val, IGU_MAPPING_LINE_VALID, 0);
176 qed_wr(p_hwfn, p_ptt,
177 IGU_REG_MAPPING_MEMORY + 4 * sb_id, val);
182 static void qed_iov_setup_vfdb(struct qed_hwfn *p_hwfn)
184 struct qed_hw_sriov_info *p_iov = p_hwfn->cdev->p_iov_info;
185 struct qed_pf_iov *p_iov_info = p_hwfn->pf_iov_info;
186 struct qed_bulletin_content *p_bulletin_virt;
187 dma_addr_t req_p, rply_p, bulletin_p;
188 union pfvf_tlvs *p_reply_virt_addr;
189 union vfpf_tlvs *p_req_virt_addr;
192 memset(p_iov_info->vfs_array, 0, sizeof(p_iov_info->vfs_array));
194 p_req_virt_addr = p_iov_info->mbx_msg_virt_addr;
195 req_p = p_iov_info->mbx_msg_phys_addr;
196 p_reply_virt_addr = p_iov_info->mbx_reply_virt_addr;
197 rply_p = p_iov_info->mbx_reply_phys_addr;
198 p_bulletin_virt = p_iov_info->p_bulletins;
199 bulletin_p = p_iov_info->bulletins_phys;
200 if (!p_req_virt_addr || !p_reply_virt_addr || !p_bulletin_virt) {
202 "qed_iov_setup_vfdb called without allocating mem first\n");
206 for (idx = 0; idx < p_iov->total_vfs; idx++) {
207 struct qed_vf_info *vf = &p_iov_info->vfs_array[idx];
210 vf->vf_mbx.req_virt = p_req_virt_addr + idx;
211 vf->vf_mbx.req_phys = req_p + idx * sizeof(union vfpf_tlvs);
212 vf->vf_mbx.reply_virt = p_reply_virt_addr + idx;
213 vf->vf_mbx.reply_phys = rply_p + idx * sizeof(union pfvf_tlvs);
215 vf->state = VF_STOPPED;
218 vf->bulletin.phys = idx *
219 sizeof(struct qed_bulletin_content) +
221 vf->bulletin.p_virt = p_bulletin_virt + idx;
222 vf->bulletin.size = sizeof(struct qed_bulletin_content);
224 vf->relative_vf_id = idx;
225 vf->abs_vf_id = idx + p_iov->first_vf_in_pf;
226 concrete = qed_vfid_to_concrete(p_hwfn, vf->abs_vf_id);
227 vf->concrete_fid = concrete;
228 vf->opaque_fid = (p_hwfn->hw_info.opaque_fid & 0xff) |
229 (vf->abs_vf_id << 8);
230 vf->vport_id = idx + 1;
234 static int qed_iov_allocate_vfdb(struct qed_hwfn *p_hwfn)
236 struct qed_pf_iov *p_iov_info = p_hwfn->pf_iov_info;
240 num_vfs = p_hwfn->cdev->p_iov_info->total_vfs;
242 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
243 "qed_iov_allocate_vfdb for %d VFs\n", num_vfs);
245 /* Allocate PF Mailbox buffer (per-VF) */
246 p_iov_info->mbx_msg_size = sizeof(union vfpf_tlvs) * num_vfs;
247 p_v_addr = &p_iov_info->mbx_msg_virt_addr;
248 *p_v_addr = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
249 p_iov_info->mbx_msg_size,
250 &p_iov_info->mbx_msg_phys_addr,
255 /* Allocate PF Mailbox Reply buffer (per-VF) */
256 p_iov_info->mbx_reply_size = sizeof(union pfvf_tlvs) * num_vfs;
257 p_v_addr = &p_iov_info->mbx_reply_virt_addr;
258 *p_v_addr = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
259 p_iov_info->mbx_reply_size,
260 &p_iov_info->mbx_reply_phys_addr,
265 p_iov_info->bulletins_size = sizeof(struct qed_bulletin_content) *
267 p_v_addr = &p_iov_info->p_bulletins;
268 *p_v_addr = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
269 p_iov_info->bulletins_size,
270 &p_iov_info->bulletins_phys,
277 "PF's Requests mailbox [%p virt 0x%llx phys], Response mailbox [%p virt 0x%llx phys] Bulletins [%p virt 0x%llx phys]\n",
278 p_iov_info->mbx_msg_virt_addr,
279 (u64) p_iov_info->mbx_msg_phys_addr,
280 p_iov_info->mbx_reply_virt_addr,
281 (u64) p_iov_info->mbx_reply_phys_addr,
282 p_iov_info->p_bulletins, (u64) p_iov_info->bulletins_phys);
287 static void qed_iov_free_vfdb(struct qed_hwfn *p_hwfn)
289 struct qed_pf_iov *p_iov_info = p_hwfn->pf_iov_info;
291 if (p_hwfn->pf_iov_info->mbx_msg_virt_addr)
292 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
293 p_iov_info->mbx_msg_size,
294 p_iov_info->mbx_msg_virt_addr,
295 p_iov_info->mbx_msg_phys_addr);
297 if (p_hwfn->pf_iov_info->mbx_reply_virt_addr)
298 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
299 p_iov_info->mbx_reply_size,
300 p_iov_info->mbx_reply_virt_addr,
301 p_iov_info->mbx_reply_phys_addr);
303 if (p_iov_info->p_bulletins)
304 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
305 p_iov_info->bulletins_size,
306 p_iov_info->p_bulletins,
307 p_iov_info->bulletins_phys);
310 int qed_iov_alloc(struct qed_hwfn *p_hwfn)
312 struct qed_pf_iov *p_sriov;
314 if (!IS_PF_SRIOV(p_hwfn)) {
315 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
316 "No SR-IOV - no need for IOV db\n");
320 p_sriov = kzalloc(sizeof(*p_sriov), GFP_KERNEL);
322 DP_NOTICE(p_hwfn, "Failed to allocate `struct qed_sriov'\n");
326 p_hwfn->pf_iov_info = p_sriov;
328 return qed_iov_allocate_vfdb(p_hwfn);
331 void qed_iov_setup(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
333 if (!IS_PF_SRIOV(p_hwfn) || !IS_PF_SRIOV_ALLOC(p_hwfn))
336 qed_iov_setup_vfdb(p_hwfn);
337 qed_iov_clear_vf_igu_blocks(p_hwfn, p_ptt);
340 void qed_iov_free(struct qed_hwfn *p_hwfn)
342 if (IS_PF_SRIOV_ALLOC(p_hwfn)) {
343 qed_iov_free_vfdb(p_hwfn);
344 kfree(p_hwfn->pf_iov_info);
348 void qed_iov_free_hw_info(struct qed_dev *cdev)
350 kfree(cdev->p_iov_info);
351 cdev->p_iov_info = NULL;
354 int qed_iov_hw_info(struct qed_hwfn *p_hwfn)
356 struct qed_dev *cdev = p_hwfn->cdev;
360 if (IS_VF(p_hwfn->cdev))
363 /* Learn the PCI configuration */
364 pos = pci_find_ext_capability(p_hwfn->cdev->pdev,
365 PCI_EXT_CAP_ID_SRIOV);
367 DP_VERBOSE(p_hwfn, QED_MSG_IOV, "No PCIe IOV support\n");
371 /* Allocate a new struct for IOV information */
372 cdev->p_iov_info = kzalloc(sizeof(*cdev->p_iov_info), GFP_KERNEL);
373 if (!cdev->p_iov_info) {
374 DP_NOTICE(p_hwfn, "Can't support IOV due to lack of memory\n");
377 cdev->p_iov_info->pos = pos;
379 rc = qed_iov_pci_cfg_info(cdev);
383 /* We want PF IOV to be synonemous with the existance of p_iov_info;
384 * In case the capability is published but there are no VFs, simply
385 * de-allocate the struct.
387 if (!cdev->p_iov_info->total_vfs) {
388 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
389 "IOV capabilities, but no VFs are published\n");
390 kfree(cdev->p_iov_info);
391 cdev->p_iov_info = NULL;
395 /* Calculate the first VF index - this is a bit tricky; Basically,
396 * VFs start at offset 16 relative to PF0, and 2nd engine VFs begin
397 * after the first engine's VFs.
399 cdev->p_iov_info->first_vf_in_pf = p_hwfn->cdev->p_iov_info->offset +
400 p_hwfn->abs_pf_id - 16;
401 if (QED_PATH_ID(p_hwfn))
402 cdev->p_iov_info->first_vf_in_pf -= MAX_NUM_VFS_BB;
404 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
405 "First VF in hwfn 0x%08x\n",
406 cdev->p_iov_info->first_vf_in_pf);
411 static bool qed_iov_pf_sanity_check(struct qed_hwfn *p_hwfn, int vfid)
413 /* Check PF supports sriov */
414 if (!IS_QED_SRIOV(p_hwfn->cdev) || !IS_PF_SRIOV_ALLOC(p_hwfn))
417 /* Check VF validity */
418 if (IS_VF(p_hwfn->cdev) || !IS_QED_SRIOV(p_hwfn->cdev) ||
419 !IS_PF_SRIOV_ALLOC(p_hwfn))
425 static void qed_iov_vf_pglue_clear_err(struct qed_hwfn *p_hwfn,
426 struct qed_ptt *p_ptt, u8 abs_vfid)
428 qed_wr(p_hwfn, p_ptt,
429 PGLUE_B_REG_WAS_ERROR_VF_31_0_CLR + (abs_vfid >> 5) * 4,
430 1 << (abs_vfid & 0x1f));
433 static int qed_iov_enable_vf_access(struct qed_hwfn *p_hwfn,
434 struct qed_ptt *p_ptt,
435 struct qed_vf_info *vf)
437 u32 igu_vf_conf = IGU_VF_CONF_FUNC_EN;
442 "Enable internal access for vf %x [abs %x]\n",
443 vf->abs_vf_id, QED_VF_ABS_ID(p_hwfn, vf));
445 qed_iov_vf_pglue_clear_err(p_hwfn, p_ptt, QED_VF_ABS_ID(p_hwfn, vf));
447 rc = qed_mcp_config_vf_msix(p_hwfn, p_ptt, vf->abs_vf_id, vf->num_sbs);
451 qed_fid_pretend(p_hwfn, p_ptt, (u16) vf->concrete_fid);
453 SET_FIELD(igu_vf_conf, IGU_VF_CONF_PARENT, p_hwfn->rel_pf_id);
454 STORE_RT_REG(p_hwfn, IGU_REG_VF_CONFIGURATION_RT_OFFSET, igu_vf_conf);
456 qed_init_run(p_hwfn, p_ptt, PHASE_VF, vf->abs_vf_id,
457 p_hwfn->hw_info.hw_mode);
460 qed_fid_pretend(p_hwfn, p_ptt, (u16) p_hwfn->hw_info.concrete_fid);
462 if (vf->state != VF_STOPPED) {
463 DP_NOTICE(p_hwfn, "VF[%02x] is already started\n",
469 rc = qed_sp_vf_start(p_hwfn, vf->concrete_fid, vf->opaque_fid);
471 DP_NOTICE(p_hwfn, "Failed to start VF[%02x]\n", vf->abs_vf_id);
478 static u8 qed_iov_alloc_vf_igu_sbs(struct qed_hwfn *p_hwfn,
479 struct qed_ptt *p_ptt,
480 struct qed_vf_info *vf, u16 num_rx_queues)
482 struct qed_igu_block *igu_blocks;
483 int qid = 0, igu_id = 0;
486 igu_blocks = p_hwfn->hw_info.p_igu_info->igu_map.igu_blocks;
488 if (num_rx_queues > p_hwfn->hw_info.p_igu_info->free_blks)
489 num_rx_queues = p_hwfn->hw_info.p_igu_info->free_blks;
490 p_hwfn->hw_info.p_igu_info->free_blks -= num_rx_queues;
492 SET_FIELD(val, IGU_MAPPING_LINE_FUNCTION_NUMBER, vf->abs_vf_id);
493 SET_FIELD(val, IGU_MAPPING_LINE_VALID, 1);
494 SET_FIELD(val, IGU_MAPPING_LINE_PF_VALID, 0);
496 while ((qid < num_rx_queues) &&
497 (igu_id < QED_MAPPING_MEMORY_SIZE(p_hwfn->cdev))) {
498 if (igu_blocks[igu_id].status & QED_IGU_STATUS_FREE) {
499 struct cau_sb_entry sb_entry;
501 vf->igu_sbs[qid] = (u16)igu_id;
502 igu_blocks[igu_id].status &= ~QED_IGU_STATUS_FREE;
504 SET_FIELD(val, IGU_MAPPING_LINE_VECTOR_NUMBER, qid);
506 qed_wr(p_hwfn, p_ptt,
507 IGU_REG_MAPPING_MEMORY + sizeof(u32) * igu_id,
510 /* Configure igu sb in CAU which were marked valid */
511 qed_init_cau_sb_entry(p_hwfn, &sb_entry,
514 qed_dmae_host2grc(p_hwfn, p_ptt,
515 (u64)(uintptr_t)&sb_entry,
516 CAU_REG_SB_VAR_MEMORY +
517 igu_id * sizeof(u64), 2, 0);
523 vf->num_sbs = (u8) num_rx_queues;
528 static int qed_iov_init_hw_for_vf(struct qed_hwfn *p_hwfn,
529 struct qed_ptt *p_ptt,
530 u16 rel_vf_id, u16 num_rx_queues)
532 u8 num_of_vf_avaiable_chains = 0;
533 struct qed_vf_info *vf = NULL;
538 vf = qed_iov_get_vf_info(p_hwfn, rel_vf_id, false);
540 DP_ERR(p_hwfn, "qed_iov_init_hw_for_vf : vf is NULL\n");
545 DP_NOTICE(p_hwfn, "VF[%d] is already active.\n", rel_vf_id);
549 /* Limit number of queues according to number of CIDs */
550 qed_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_ETH, &cids);
553 "VF[%d] - requesting to initialize for 0x%04x queues [0x%04x CIDs available]\n",
554 vf->relative_vf_id, num_rx_queues, (u16) cids);
555 num_rx_queues = min_t(u16, num_rx_queues, ((u16) cids));
557 num_of_vf_avaiable_chains = qed_iov_alloc_vf_igu_sbs(p_hwfn,
561 if (!num_of_vf_avaiable_chains) {
562 DP_ERR(p_hwfn, "no available igu sbs\n");
566 /* Choose queue number and index ranges */
567 vf->num_rxqs = num_of_vf_avaiable_chains;
568 vf->num_txqs = num_of_vf_avaiable_chains;
570 for (i = 0; i < vf->num_rxqs; i++) {
571 u16 queue_id = qed_int_queue_id_from_sb_id(p_hwfn,
574 if (queue_id > RESC_NUM(p_hwfn, QED_L2_QUEUE)) {
576 "VF[%d] will require utilizing of out-of-bounds queues - %04x\n",
577 vf->relative_vf_id, queue_id);
581 /* CIDs are per-VF, so no problem having them 0-based. */
582 vf->vf_queues[i].fw_rx_qid = queue_id;
583 vf->vf_queues[i].fw_tx_qid = queue_id;
584 vf->vf_queues[i].fw_cid = i;
586 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
587 "VF[%d] - [%d] SB %04x, Tx/Rx queue %04x CID %04x\n",
588 vf->relative_vf_id, i, vf->igu_sbs[i], queue_id, i);
590 rc = qed_iov_enable_vf_access(p_hwfn, p_ptt, vf);
594 if (IS_LEAD_HWFN(p_hwfn))
595 p_hwfn->cdev->p_iov_info->num_vfs++;
601 static bool qed_iov_tlv_supported(u16 tlvtype)
603 return CHANNEL_TLV_NONE < tlvtype && tlvtype < CHANNEL_TLV_MAX;
606 /* place a given tlv on the tlv buffer, continuing current tlv list */
607 void *qed_add_tlv(struct qed_hwfn *p_hwfn, u8 **offset, u16 type, u16 length)
609 struct channel_tlv *tl = (struct channel_tlv *)*offset;
614 /* Offset should keep pointing to next TLV (the end of the last) */
617 /* Return a pointer to the start of the added tlv */
618 return *offset - length;
621 /* list the types and lengths of the tlvs on the buffer */
622 void qed_dp_tlv_list(struct qed_hwfn *p_hwfn, void *tlvs_list)
624 u16 i = 1, total_length = 0;
625 struct channel_tlv *tlv;
628 tlv = (struct channel_tlv *)((u8 *)tlvs_list + total_length);
631 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
632 "TLV number %d: type %d, length %d\n",
633 i, tlv->type, tlv->length);
635 if (tlv->type == CHANNEL_TLV_LIST_END)
638 /* Validate entry - protect against malicious VFs */
640 DP_NOTICE(p_hwfn, "TLV of length 0 found\n");
644 total_length += tlv->length;
646 if (total_length >= sizeof(struct tlv_buffer_size)) {
647 DP_NOTICE(p_hwfn, "TLV ==> Buffer overflow\n");
655 static void qed_iov_send_response(struct qed_hwfn *p_hwfn,
656 struct qed_ptt *p_ptt,
657 struct qed_vf_info *p_vf,
658 u16 length, u8 status)
660 struct qed_iov_vf_mbx *mbx = &p_vf->vf_mbx;
661 struct qed_dmae_params params;
664 mbx->reply_virt->default_resp.hdr.status = status;
666 qed_dp_tlv_list(p_hwfn, mbx->reply_virt);
668 eng_vf_id = p_vf->abs_vf_id;
670 memset(¶ms, 0, sizeof(struct qed_dmae_params));
671 params.flags = QED_DMAE_FLAG_VF_DST;
672 params.dst_vfid = eng_vf_id;
674 qed_dmae_host2host(p_hwfn, p_ptt, mbx->reply_phys + sizeof(u64),
675 mbx->req_virt->first_tlv.reply_address +
677 (sizeof(union pfvf_tlvs) - sizeof(u64)) / 4,
680 qed_dmae_host2host(p_hwfn, p_ptt, mbx->reply_phys,
681 mbx->req_virt->first_tlv.reply_address,
682 sizeof(u64) / 4, ¶ms);
685 GTT_BAR0_MAP_REG_USDM_RAM +
686 USTORM_VF_PF_CHANNEL_READY_OFFSET(eng_vf_id), 1);
689 static void qed_iov_prepare_resp(struct qed_hwfn *p_hwfn,
690 struct qed_ptt *p_ptt,
691 struct qed_vf_info *vf_info,
692 u16 type, u16 length, u8 status)
694 struct qed_iov_vf_mbx *mbx = &vf_info->vf_mbx;
696 mbx->offset = (u8 *)mbx->reply_virt;
698 qed_add_tlv(p_hwfn, &mbx->offset, type, length);
699 qed_add_tlv(p_hwfn, &mbx->offset, CHANNEL_TLV_LIST_END,
700 sizeof(struct channel_list_end_tlv));
702 qed_iov_send_response(p_hwfn, p_ptt, vf_info, length, status);
705 static void qed_iov_vf_mbx_acquire(struct qed_hwfn *p_hwfn,
706 struct qed_ptt *p_ptt,
707 struct qed_vf_info *vf)
709 struct qed_iov_vf_mbx *mbx = &vf->vf_mbx;
710 struct pfvf_acquire_resp_tlv *resp = &mbx->reply_virt->acquire_resp;
711 struct pf_vf_pfdev_info *pfdev_info = &resp->pfdev_info;
712 struct vfpf_acquire_tlv *req = &mbx->req_virt->acquire;
713 u8 i, vfpf_status = PFVF_STATUS_SUCCESS;
714 struct pf_vf_resc *resc = &resp->resc;
716 /* Validate FW compatibility */
717 if (req->vfdev_info.fw_major != FW_MAJOR_VERSION ||
718 req->vfdev_info.fw_minor != FW_MINOR_VERSION ||
719 req->vfdev_info.fw_revision != FW_REVISION_VERSION ||
720 req->vfdev_info.fw_engineering != FW_ENGINEERING_VERSION) {
722 "VF[%d] is running an incompatible driver [VF needs FW %02x:%02x:%02x:%02x but Hypervisor is using %02x:%02x:%02x:%02x]\n",
724 req->vfdev_info.fw_major,
725 req->vfdev_info.fw_minor,
726 req->vfdev_info.fw_revision,
727 req->vfdev_info.fw_engineering,
730 FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
731 vfpf_status = PFVF_STATUS_NOT_SUPPORTED;
735 /* On 100g PFs, prevent old VFs from loading */
736 if ((p_hwfn->cdev->num_hwfns > 1) &&
737 !(req->vfdev_info.capabilities & VFPF_ACQUIRE_CAP_100G)) {
739 "VF[%d] is running an old driver that doesn't support 100g\n",
741 vfpf_status = PFVF_STATUS_NOT_SUPPORTED;
745 memset(resp, 0, sizeof(*resp));
747 /* Fill in vf info stuff */
748 vf->opaque_fid = req->vfdev_info.opaque_fid;
749 vf->num_mac_filters = 1;
750 vf->num_vlan_filters = QED_ETH_VF_NUM_VLAN_FILTERS;
752 vf->vf_bulletin = req->bulletin_addr;
753 vf->bulletin.size = (vf->bulletin.size < req->bulletin_size) ?
754 vf->bulletin.size : req->bulletin_size;
756 /* fill in pfdev info */
757 pfdev_info->chip_num = p_hwfn->cdev->chip_num;
758 pfdev_info->db_size = 0;
759 pfdev_info->indices_per_sb = PIS_PER_SB;
761 pfdev_info->capabilities = PFVF_ACQUIRE_CAP_DEFAULT_UNTAGGED |
762 PFVF_ACQUIRE_CAP_POST_FW_OVERRIDE;
763 if (p_hwfn->cdev->num_hwfns > 1)
764 pfdev_info->capabilities |= PFVF_ACQUIRE_CAP_100G;
766 pfdev_info->stats_info.mstats.address =
767 PXP_VF_BAR0_START_MSDM_ZONE_B +
768 offsetof(struct mstorm_vf_zone, non_trigger.eth_queue_stat);
769 pfdev_info->stats_info.mstats.len =
770 sizeof(struct eth_mstorm_per_queue_stat);
772 pfdev_info->stats_info.ustats.address =
773 PXP_VF_BAR0_START_USDM_ZONE_B +
774 offsetof(struct ustorm_vf_zone, non_trigger.eth_queue_stat);
775 pfdev_info->stats_info.ustats.len =
776 sizeof(struct eth_ustorm_per_queue_stat);
778 pfdev_info->stats_info.pstats.address =
779 PXP_VF_BAR0_START_PSDM_ZONE_B +
780 offsetof(struct pstorm_vf_zone, non_trigger.eth_queue_stat);
781 pfdev_info->stats_info.pstats.len =
782 sizeof(struct eth_pstorm_per_queue_stat);
784 pfdev_info->stats_info.tstats.address = 0;
785 pfdev_info->stats_info.tstats.len = 0;
787 memcpy(pfdev_info->port_mac, p_hwfn->hw_info.hw_mac_addr, ETH_ALEN);
789 pfdev_info->fw_major = FW_MAJOR_VERSION;
790 pfdev_info->fw_minor = FW_MINOR_VERSION;
791 pfdev_info->fw_rev = FW_REVISION_VERSION;
792 pfdev_info->fw_eng = FW_ENGINEERING_VERSION;
793 pfdev_info->os_type = VFPF_ACQUIRE_OS_LINUX;
794 qed_mcp_get_mfw_ver(p_hwfn, p_ptt, &pfdev_info->mfw_ver, NULL);
796 pfdev_info->dev_type = p_hwfn->cdev->type;
797 pfdev_info->chip_rev = p_hwfn->cdev->chip_rev;
799 resc->num_rxqs = vf->num_rxqs;
800 resc->num_txqs = vf->num_txqs;
801 resc->num_sbs = vf->num_sbs;
802 for (i = 0; i < resc->num_sbs; i++) {
803 resc->hw_sbs[i].hw_sb_id = vf->igu_sbs[i];
804 resc->hw_sbs[i].sb_qid = 0;
807 for (i = 0; i < resc->num_rxqs; i++) {
808 qed_fw_l2_queue(p_hwfn, vf->vf_queues[i].fw_rx_qid,
809 (u16 *)&resc->hw_qid[i]);
810 resc->cid[i] = vf->vf_queues[i].fw_cid;
813 resc->num_mac_filters = min_t(u8, vf->num_mac_filters,
814 req->resc_request.num_mac_filters);
815 resc->num_vlan_filters = min_t(u8, vf->num_vlan_filters,
816 req->resc_request.num_vlan_filters);
818 /* This isn't really required as VF isn't limited, but some VFs might
819 * actually test this value, so need to provide it.
821 resc->num_mc_filters = req->resc_request.num_mc_filters;
823 /* Fill agreed size of bulletin board in response */
824 resp->bulletin_size = vf->bulletin.size;
828 "VF[%d] ACQUIRE_RESPONSE: pfdev_info- chip_num=0x%x, db_size=%d, idx_per_sb=%d, pf_cap=0x%llx\n"
829 "resources- n_rxq-%d, n_txq-%d, n_sbs-%d, n_macs-%d, n_vlans-%d\n",
831 resp->pfdev_info.chip_num,
832 resp->pfdev_info.db_size,
833 resp->pfdev_info.indices_per_sb,
834 resp->pfdev_info.capabilities,
838 resc->num_mac_filters,
839 resc->num_vlan_filters);
840 vf->state = VF_ACQUIRED;
842 /* Prepare Response */
844 qed_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_ACQUIRE,
845 sizeof(struct pfvf_acquire_resp_tlv), vfpf_status);
848 static void qed_iov_process_mbx_req(struct qed_hwfn *p_hwfn,
849 struct qed_ptt *p_ptt, int vfid)
851 struct qed_iov_vf_mbx *mbx;
852 struct qed_vf_info *p_vf;
855 p_vf = qed_iov_get_vf_info(p_hwfn, (u16) vfid, true);
861 /* qed_iov_process_mbx_request */
864 "qed_iov_process_mbx_req vfid %d\n", p_vf->abs_vf_id);
866 mbx->first_tlv = mbx->req_virt->first_tlv;
868 /* check if tlv type is known */
869 if (qed_iov_tlv_supported(mbx->first_tlv.tl.type)) {
870 switch (mbx->first_tlv.tl.type) {
871 case CHANNEL_TLV_ACQUIRE:
872 qed_iov_vf_mbx_acquire(p_hwfn, p_ptt, p_vf);
876 /* unknown TLV - this may belong to a VF driver from the future
877 * - a version written after this PF driver was written, which
878 * supports features unknown as of yet. Too bad since we don't
879 * support them. Or this may be because someone wrote a crappy
880 * VF driver and is sending garbage over the channel.
883 "unknown TLV. type %d length %d. first 20 bytes of mailbox buffer:\n",
884 mbx->first_tlv.tl.type, mbx->first_tlv.tl.length);
886 for (i = 0; i < 20; i++) {
890 mbx->req_virt->tlv_buf_size.tlv_buffer[i]);
895 void qed_iov_pf_add_pending_events(struct qed_hwfn *p_hwfn, u8 vfid)
897 u64 add_bit = 1ULL << (vfid % 64);
899 p_hwfn->pf_iov_info->pending_events[vfid / 64] |= add_bit;
902 static void qed_iov_pf_get_and_clear_pending_events(struct qed_hwfn *p_hwfn,
905 u64 *p_pending_events = p_hwfn->pf_iov_info->pending_events;
907 memcpy(events, p_pending_events, sizeof(u64) * QED_VF_ARRAY_LENGTH);
908 memset(p_pending_events, 0, sizeof(u64) * QED_VF_ARRAY_LENGTH);
911 static int qed_sriov_vfpf_msg(struct qed_hwfn *p_hwfn,
912 u16 abs_vfid, struct regpair *vf_msg)
914 u8 min = (u8)p_hwfn->cdev->p_iov_info->first_vf_in_pf;
915 struct qed_vf_info *p_vf;
917 if (!qed_iov_pf_sanity_check(p_hwfn, (int)abs_vfid - min)) {
920 "Got a message from VF [abs 0x%08x] that cannot be handled by PF\n",
924 p_vf = &p_hwfn->pf_iov_info->vfs_array[(u8)abs_vfid - min];
926 /* List the physical address of the request so that handler
927 * could later on copy the message from it.
929 p_vf->vf_mbx.pending_req = (((u64)vf_msg->hi) << 32) | vf_msg->lo;
931 /* Mark the event and schedule the workqueue */
932 qed_iov_pf_add_pending_events(p_hwfn, p_vf->relative_vf_id);
933 qed_schedule_iov(p_hwfn, QED_IOV_WQ_MSG_FLAG);
938 int qed_sriov_eqe_event(struct qed_hwfn *p_hwfn,
939 u8 opcode, __le16 echo, union event_ring_data *data)
942 case COMMON_EVENT_VF_PF_CHANNEL:
943 return qed_sriov_vfpf_msg(p_hwfn, le16_to_cpu(echo),
944 &data->vf_pf_channel.msg_addr);
946 DP_INFO(p_hwfn->cdev, "Unknown sriov eqe event 0x%02x\n",
952 u16 qed_iov_get_next_active_vf(struct qed_hwfn *p_hwfn, u16 rel_vf_id)
954 struct qed_hw_sriov_info *p_iov = p_hwfn->cdev->p_iov_info;
960 for (i = rel_vf_id; i < p_iov->total_vfs; i++)
961 if (qed_iov_is_valid_vfid(p_hwfn, rel_vf_id, true))
968 static int qed_iov_copy_vf_msg(struct qed_hwfn *p_hwfn, struct qed_ptt *ptt,
971 struct qed_dmae_params params;
972 struct qed_vf_info *vf_info;
974 vf_info = qed_iov_get_vf_info(p_hwfn, (u16) vfid, true);
978 memset(¶ms, 0, sizeof(struct qed_dmae_params));
979 params.flags = QED_DMAE_FLAG_VF_SRC | QED_DMAE_FLAG_COMPLETION_DST;
980 params.src_vfid = vf_info->abs_vf_id;
982 if (qed_dmae_host2host(p_hwfn, ptt,
983 vf_info->vf_mbx.pending_req,
984 vf_info->vf_mbx.req_phys,
985 sizeof(union vfpf_tlvs) / 4, ¶ms)) {
986 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
987 "Failed to copy message from VF 0x%02x\n", vfid);
996 * qed_schedule_iov - schedules IOV task for VF and PF
997 * @hwfn: hardware function pointer
998 * @flag: IOV flag for VF/PF
1000 void qed_schedule_iov(struct qed_hwfn *hwfn, enum qed_iov_wq_flag flag)
1002 smp_mb__before_atomic();
1003 set_bit(flag, &hwfn->iov_task_flags);
1004 smp_mb__after_atomic();
1005 DP_VERBOSE(hwfn, QED_MSG_IOV, "Scheduling iov task [Flag: %d]\n", flag);
1006 queue_delayed_work(hwfn->iov_wq, &hwfn->iov_task, 0);
1009 void qed_vf_start_iov_wq(struct qed_dev *cdev)
1013 for_each_hwfn(cdev, i)
1014 queue_delayed_work(cdev->hwfns[i].iov_wq,
1015 &cdev->hwfns[i].iov_task, 0);
1018 static void qed_handle_vf_msg(struct qed_hwfn *hwfn)
1020 u64 events[QED_VF_ARRAY_LENGTH];
1021 struct qed_ptt *ptt;
1024 ptt = qed_ptt_acquire(hwfn);
1026 DP_VERBOSE(hwfn, QED_MSG_IOV,
1027 "Can't acquire PTT; re-scheduling\n");
1028 qed_schedule_iov(hwfn, QED_IOV_WQ_MSG_FLAG);
1032 qed_iov_pf_get_and_clear_pending_events(hwfn, events);
1034 DP_VERBOSE(hwfn, QED_MSG_IOV,
1035 "Event mask of VF events: 0x%llx 0x%llx 0x%llx\n",
1036 events[0], events[1], events[2]);
1038 qed_for_each_vf(hwfn, i) {
1039 /* Skip VFs with no pending messages */
1040 if (!(events[i / 64] & (1ULL << (i % 64))))
1043 DP_VERBOSE(hwfn, QED_MSG_IOV,
1044 "Handling VF message from VF 0x%02x [Abs 0x%02x]\n",
1045 i, hwfn->cdev->p_iov_info->first_vf_in_pf + i);
1047 /* Copy VF's message to PF's request buffer for that VF */
1048 if (qed_iov_copy_vf_msg(hwfn, ptt, i))
1051 qed_iov_process_mbx_req(hwfn, ptt, i);
1054 qed_ptt_release(hwfn, ptt);
1057 void qed_iov_pf_task(struct work_struct *work)
1059 struct qed_hwfn *hwfn = container_of(work, struct qed_hwfn,
1062 if (test_and_clear_bit(QED_IOV_WQ_STOP_WQ_FLAG, &hwfn->iov_task_flags))
1065 if (test_and_clear_bit(QED_IOV_WQ_MSG_FLAG, &hwfn->iov_task_flags))
1066 qed_handle_vf_msg(hwfn);
1069 void qed_iov_wq_stop(struct qed_dev *cdev, bool schedule_first)
1073 for_each_hwfn(cdev, i) {
1074 if (!cdev->hwfns[i].iov_wq)
1077 if (schedule_first) {
1078 qed_schedule_iov(&cdev->hwfns[i],
1079 QED_IOV_WQ_STOP_WQ_FLAG);
1080 cancel_delayed_work_sync(&cdev->hwfns[i].iov_task);
1083 flush_workqueue(cdev->hwfns[i].iov_wq);
1084 destroy_workqueue(cdev->hwfns[i].iov_wq);
1088 int qed_iov_wq_start(struct qed_dev *cdev)
1090 char name[NAME_SIZE];
1093 for_each_hwfn(cdev, i) {
1094 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1096 /* PFs needs a dedicated workqueue only if they support IOV. */
1097 if (!IS_PF_SRIOV(p_hwfn))
1100 snprintf(name, NAME_SIZE, "iov-%02x:%02x.%02x",
1101 cdev->pdev->bus->number,
1102 PCI_SLOT(cdev->pdev->devfn), p_hwfn->abs_pf_id);
1104 p_hwfn->iov_wq = create_singlethread_workqueue(name);
1105 if (!p_hwfn->iov_wq) {
1106 DP_NOTICE(p_hwfn, "Cannot create iov workqueue\n");
1110 INIT_DELAYED_WORK(&p_hwfn->iov_task, qed_iov_pf_task);