GNU Linux-libre 4.9.297-gnu1
[releases.git] / drivers / net / ethernet / qlogic / qed / qed_vf.c
1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015 QLogic Corporation
3  *
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
6  * this source tree.
7  */
8
9 #include <linux/crc32.h>
10 #include <linux/etherdevice.h>
11 #include "qed.h"
12 #include "qed_sriov.h"
13 #include "qed_vf.h"
14
15 static void *qed_vf_pf_prep(struct qed_hwfn *p_hwfn, u16 type, u16 length)
16 {
17         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
18         void *p_tlv;
19
20         /* This lock is released when we receive PF's response
21          * in qed_send_msg2pf().
22          * So, qed_vf_pf_prep() and qed_send_msg2pf()
23          * must come in sequence.
24          */
25         mutex_lock(&(p_iov->mutex));
26
27         DP_VERBOSE(p_hwfn,
28                    QED_MSG_IOV,
29                    "preparing to send 0x%04x tlv over vf pf channel\n",
30                    type);
31
32         /* Reset Requst offset */
33         p_iov->offset = (u8 *)p_iov->vf2pf_request;
34
35         /* Clear mailbox - both request and reply */
36         memset(p_iov->vf2pf_request, 0, sizeof(union vfpf_tlvs));
37         memset(p_iov->pf2vf_reply, 0, sizeof(union pfvf_tlvs));
38
39         /* Init type and length */
40         p_tlv = qed_add_tlv(p_hwfn, &p_iov->offset, type, length);
41
42         /* Init first tlv header */
43         ((struct vfpf_first_tlv *)p_tlv)->reply_address =
44             (u64)p_iov->pf2vf_reply_phys;
45
46         return p_tlv;
47 }
48
49 static void qed_vf_pf_req_end(struct qed_hwfn *p_hwfn, int req_status)
50 {
51         union pfvf_tlvs *resp = p_hwfn->vf_iov_info->pf2vf_reply;
52
53         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
54                    "VF request status = 0x%x, PF reply status = 0x%x\n",
55                    req_status, resp->default_resp.hdr.status);
56
57         mutex_unlock(&(p_hwfn->vf_iov_info->mutex));
58 }
59
60 #define QED_VF_CHANNEL_USLEEP_ITERATIONS        90
61 #define QED_VF_CHANNEL_USLEEP_DELAY             100
62 #define QED_VF_CHANNEL_MSLEEP_ITERATIONS        10
63 #define QED_VF_CHANNEL_MSLEEP_DELAY             25
64
65 static int qed_send_msg2pf(struct qed_hwfn *p_hwfn, u8 *done, u32 resp_size)
66 {
67         union vfpf_tlvs *p_req = p_hwfn->vf_iov_info->vf2pf_request;
68         struct ustorm_trigger_vf_zone trigger;
69         struct ustorm_vf_zone *zone_data;
70         int iter, rc = 0;
71
72         zone_data = (struct ustorm_vf_zone *)PXP_VF_BAR0_START_USDM_ZONE_B;
73
74         /* output tlvs list */
75         qed_dp_tlv_list(p_hwfn, p_req);
76
77         /* need to add the END TLV to the message size */
78         resp_size += sizeof(struct channel_list_end_tlv);
79
80         /* Send TLVs over HW channel */
81         memset(&trigger, 0, sizeof(struct ustorm_trigger_vf_zone));
82         trigger.vf_pf_msg_valid = 1;
83
84         DP_VERBOSE(p_hwfn,
85                    QED_MSG_IOV,
86                    "VF -> PF [%02x] message: [%08x, %08x] --> %p, %08x --> %p\n",
87                    GET_FIELD(p_hwfn->hw_info.concrete_fid,
88                              PXP_CONCRETE_FID_PFID),
89                    upper_32_bits(p_hwfn->vf_iov_info->vf2pf_request_phys),
90                    lower_32_bits(p_hwfn->vf_iov_info->vf2pf_request_phys),
91                    &zone_data->non_trigger.vf_pf_msg_addr,
92                    *((u32 *)&trigger), &zone_data->trigger);
93
94         REG_WR(p_hwfn,
95                (uintptr_t)&zone_data->non_trigger.vf_pf_msg_addr.lo,
96                lower_32_bits(p_hwfn->vf_iov_info->vf2pf_request_phys));
97
98         REG_WR(p_hwfn,
99                (uintptr_t)&zone_data->non_trigger.vf_pf_msg_addr.hi,
100                upper_32_bits(p_hwfn->vf_iov_info->vf2pf_request_phys));
101
102         /* The message data must be written first, to prevent trigger before
103          * data is written.
104          */
105         wmb();
106
107         REG_WR(p_hwfn, (uintptr_t)&zone_data->trigger, *((u32 *)&trigger));
108
109         /* When PF would be done with the response, it would write back to the
110          * `done' address from a coherent DMA zone. Poll until then.
111          */
112
113         iter = QED_VF_CHANNEL_USLEEP_ITERATIONS;
114         while (!*done && iter--) {
115                 udelay(QED_VF_CHANNEL_USLEEP_DELAY);
116                 dma_rmb();
117         }
118
119         iter = QED_VF_CHANNEL_MSLEEP_ITERATIONS;
120         while (!*done && iter--) {
121                 msleep(QED_VF_CHANNEL_MSLEEP_DELAY);
122                 dma_rmb();
123         }
124
125         if (!*done) {
126                 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
127                            "VF <-- PF Timeout [Type %d]\n",
128                            p_req->first_tlv.tl.type);
129                 rc = -EBUSY;
130         } else {
131                 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
132                            "PF response: %d [Type %d]\n",
133                            *done, p_req->first_tlv.tl.type);
134         }
135
136         return rc;
137 }
138
139 #define VF_ACQUIRE_THRESH 3
140 static void qed_vf_pf_acquire_reduce_resc(struct qed_hwfn *p_hwfn,
141                                           struct vf_pf_resc_request *p_req,
142                                           struct pf_vf_resc *p_resp)
143 {
144         DP_VERBOSE(p_hwfn,
145                    QED_MSG_IOV,
146                    "PF unwilling to fullill resource request: rxq [%02x/%02x] txq [%02x/%02x] sbs [%02x/%02x] mac [%02x/%02x] vlan [%02x/%02x] mc [%02x/%02x]. Try PF recommended amount\n",
147                    p_req->num_rxqs,
148                    p_resp->num_rxqs,
149                    p_req->num_rxqs,
150                    p_resp->num_txqs,
151                    p_req->num_sbs,
152                    p_resp->num_sbs,
153                    p_req->num_mac_filters,
154                    p_resp->num_mac_filters,
155                    p_req->num_vlan_filters,
156                    p_resp->num_vlan_filters,
157                    p_req->num_mc_filters, p_resp->num_mc_filters);
158
159         /* humble our request */
160         p_req->num_txqs = p_resp->num_txqs;
161         p_req->num_rxqs = p_resp->num_rxqs;
162         p_req->num_sbs = p_resp->num_sbs;
163         p_req->num_mac_filters = p_resp->num_mac_filters;
164         p_req->num_vlan_filters = p_resp->num_vlan_filters;
165         p_req->num_mc_filters = p_resp->num_mc_filters;
166 }
167
168 static int qed_vf_pf_acquire(struct qed_hwfn *p_hwfn)
169 {
170         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
171         struct pfvf_acquire_resp_tlv *resp = &p_iov->pf2vf_reply->acquire_resp;
172         struct pf_vf_pfdev_info *pfdev_info = &resp->pfdev_info;
173         struct vf_pf_resc_request *p_resc;
174         u8 retry_cnt = VF_ACQUIRE_THRESH;
175         bool resources_acquired = false;
176         struct vfpf_acquire_tlv *req;
177         int rc = 0, attempts = 0;
178
179         /* clear mailbox and prep first tlv */
180         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_ACQUIRE, sizeof(*req));
181         p_resc = &req->resc_request;
182
183         /* starting filling the request */
184         req->vfdev_info.opaque_fid = p_hwfn->hw_info.opaque_fid;
185
186         p_resc->num_rxqs = QED_MAX_VF_CHAINS_PER_PF;
187         p_resc->num_txqs = QED_MAX_VF_CHAINS_PER_PF;
188         p_resc->num_sbs = QED_MAX_VF_CHAINS_PER_PF;
189         p_resc->num_mac_filters = QED_ETH_VF_NUM_MAC_FILTERS;
190         p_resc->num_vlan_filters = QED_ETH_VF_NUM_VLAN_FILTERS;
191
192         req->vfdev_info.os_type = VFPF_ACQUIRE_OS_LINUX;
193         req->vfdev_info.fw_major = FW_MAJOR_VERSION;
194         req->vfdev_info.fw_minor = FW_MINOR_VERSION;
195         req->vfdev_info.fw_revision = FW_REVISION_VERSION;
196         req->vfdev_info.fw_engineering = FW_ENGINEERING_VERSION;
197         req->vfdev_info.eth_fp_hsi_major = ETH_HSI_VER_MAJOR;
198         req->vfdev_info.eth_fp_hsi_minor = ETH_HSI_VER_MINOR;
199
200         /* Fill capability field with any non-deprecated config we support */
201         req->vfdev_info.capabilities |= VFPF_ACQUIRE_CAP_100G;
202
203         /* pf 2 vf bulletin board address */
204         req->bulletin_addr = p_iov->bulletin.phys;
205         req->bulletin_size = p_iov->bulletin.size;
206
207         /* add list termination tlv */
208         qed_add_tlv(p_hwfn, &p_iov->offset,
209                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
210
211         while (!resources_acquired) {
212                 DP_VERBOSE(p_hwfn,
213                            QED_MSG_IOV, "attempting to acquire resources\n");
214
215                 /* Clear response buffer, as this might be a re-send */
216                 memset(p_iov->pf2vf_reply, 0, sizeof(union pfvf_tlvs));
217
218                 /* send acquire request */
219                 rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
220
221                 /* Re-try acquire in case of vf-pf hw channel timeout */
222                 if (retry_cnt && rc == -EBUSY) {
223                         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
224                                    "VF retrying to acquire due to VPC timeout\n");
225                         retry_cnt--;
226                         continue;
227                 }
228
229                 if (rc)
230                         goto exit;
231
232                 /* copy acquire response from buffer to p_hwfn */
233                 memcpy(&p_iov->acquire_resp, resp, sizeof(p_iov->acquire_resp));
234
235                 attempts++;
236
237                 if (resp->hdr.status == PFVF_STATUS_SUCCESS) {
238                         /* PF agrees to allocate our resources */
239                         if (!(resp->pfdev_info.capabilities &
240                               PFVF_ACQUIRE_CAP_POST_FW_OVERRIDE)) {
241                                 /* It's possible legacy PF mistakenly accepted;
242                                  * but we don't care - simply mark it as
243                                  * legacy and continue.
244                                  */
245                                 req->vfdev_info.capabilities |=
246                                     VFPF_ACQUIRE_CAP_PRE_FP_HSI;
247                         }
248                         DP_VERBOSE(p_hwfn, QED_MSG_IOV, "resources acquired\n");
249                         resources_acquired = true;
250                 } else if (resp->hdr.status == PFVF_STATUS_NO_RESOURCE &&
251                            attempts < VF_ACQUIRE_THRESH) {
252                         qed_vf_pf_acquire_reduce_resc(p_hwfn, p_resc,
253                                                       &resp->resc);
254                 } else if (resp->hdr.status == PFVF_STATUS_NOT_SUPPORTED) {
255                         if (pfdev_info->major_fp_hsi &&
256                             (pfdev_info->major_fp_hsi != ETH_HSI_VER_MAJOR)) {
257                                 DP_NOTICE(p_hwfn,
258                                           "PF uses an incompatible fastpath HSI %02x.%02x [VF requires %02x.%02x]. Please change to a VF driver using %02x.xx.\n",
259                                           pfdev_info->major_fp_hsi,
260                                           pfdev_info->minor_fp_hsi,
261                                           ETH_HSI_VER_MAJOR,
262                                           ETH_HSI_VER_MINOR,
263                                           pfdev_info->major_fp_hsi);
264                                 rc = -EINVAL;
265                                 goto exit;
266                         }
267
268                         if (!pfdev_info->major_fp_hsi) {
269                                 if (req->vfdev_info.capabilities &
270                                     VFPF_ACQUIRE_CAP_PRE_FP_HSI) {
271                                         DP_NOTICE(p_hwfn,
272                                                   "PF uses very old drivers. Please change to a VF driver using no later than 8.8.x.x.\n");
273                                         rc = -EINVAL;
274                                         goto exit;
275                                 } else {
276                                         DP_INFO(p_hwfn,
277                                                 "PF is old - try re-acquire to see if it supports FW-version override\n");
278                                         req->vfdev_info.capabilities |=
279                                             VFPF_ACQUIRE_CAP_PRE_FP_HSI;
280                                         continue;
281                                 }
282                         }
283
284                         /* If PF/VF are using same Major, PF must have had
285                          * it's reasons. Simply fail.
286                          */
287                         DP_NOTICE(p_hwfn, "PF rejected acquisition by VF\n");
288                         rc = -EINVAL;
289                         goto exit;
290                 } else {
291                         DP_ERR(p_hwfn,
292                                "PF returned error %d to VF acquisition request\n",
293                                resp->hdr.status);
294                         rc = -EAGAIN;
295                         goto exit;
296                 }
297         }
298
299         /* Mark the PF as legacy, if needed */
300         if (req->vfdev_info.capabilities & VFPF_ACQUIRE_CAP_PRE_FP_HSI)
301                 p_iov->b_pre_fp_hsi = true;
302
303         /* Update bulletin board size with response from PF */
304         p_iov->bulletin.size = resp->bulletin_size;
305
306         /* get HW info */
307         p_hwfn->cdev->type = resp->pfdev_info.dev_type;
308         p_hwfn->cdev->chip_rev = resp->pfdev_info.chip_rev;
309
310         p_hwfn->cdev->chip_num = pfdev_info->chip_num & 0xffff;
311
312         /* Learn of the possibility of CMT */
313         if (IS_LEAD_HWFN(p_hwfn)) {
314                 if (resp->pfdev_info.capabilities & PFVF_ACQUIRE_CAP_100G) {
315                         DP_NOTICE(p_hwfn, "100g VF\n");
316                         p_hwfn->cdev->num_hwfns = 2;
317                 }
318         }
319
320         if (!p_iov->b_pre_fp_hsi &&
321             (resp->pfdev_info.minor_fp_hsi < ETH_HSI_VER_MINOR)) {
322                 DP_INFO(p_hwfn,
323                         "PF is using older fastpath HSI; %02x.%02x is configured\n",
324                         ETH_HSI_VER_MAJOR, resp->pfdev_info.minor_fp_hsi);
325         }
326
327 exit:
328         qed_vf_pf_req_end(p_hwfn, rc);
329
330         return rc;
331 }
332
333 int qed_vf_hw_prepare(struct qed_hwfn *p_hwfn)
334 {
335         struct qed_vf_iov *p_iov;
336         u32 reg;
337
338         /* Set number of hwfns - might be overriden once leading hwfn learns
339          * actual configuration from PF.
340          */
341         if (IS_LEAD_HWFN(p_hwfn))
342                 p_hwfn->cdev->num_hwfns = 1;
343
344         /* Set the doorbell bar. Assumption: regview is set */
345         p_hwfn->doorbells = (u8 __iomem *)p_hwfn->regview +
346                                           PXP_VF_BAR0_START_DQ;
347
348         reg = PXP_VF_BAR0_ME_OPAQUE_ADDRESS;
349         p_hwfn->hw_info.opaque_fid = (u16)REG_RD(p_hwfn, reg);
350
351         reg = PXP_VF_BAR0_ME_CONCRETE_ADDRESS;
352         p_hwfn->hw_info.concrete_fid = REG_RD(p_hwfn, reg);
353
354         /* Allocate vf sriov info */
355         p_iov = kzalloc(sizeof(*p_iov), GFP_KERNEL);
356         if (!p_iov)
357                 return -ENOMEM;
358
359         /* Allocate vf2pf msg */
360         p_iov->vf2pf_request = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
361                                                   sizeof(union vfpf_tlvs),
362                                                   &p_iov->vf2pf_request_phys,
363                                                   GFP_KERNEL);
364         if (!p_iov->vf2pf_request)
365                 goto free_p_iov;
366
367         p_iov->pf2vf_reply = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
368                                                 sizeof(union pfvf_tlvs),
369                                                 &p_iov->pf2vf_reply_phys,
370                                                 GFP_KERNEL);
371         if (!p_iov->pf2vf_reply)
372                 goto free_vf2pf_request;
373
374         DP_VERBOSE(p_hwfn,
375                    QED_MSG_IOV,
376                    "VF's Request mailbox [%p virt 0x%llx phys], Response mailbox [%p virt 0x%llx phys]\n",
377                    p_iov->vf2pf_request,
378                    (u64) p_iov->vf2pf_request_phys,
379                    p_iov->pf2vf_reply, (u64)p_iov->pf2vf_reply_phys);
380
381         /* Allocate Bulletin board */
382         p_iov->bulletin.size = sizeof(struct qed_bulletin_content);
383         p_iov->bulletin.p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
384                                                     p_iov->bulletin.size,
385                                                     &p_iov->bulletin.phys,
386                                                     GFP_KERNEL);
387         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
388                    "VF's bulletin Board [%p virt 0x%llx phys 0x%08x bytes]\n",
389                    p_iov->bulletin.p_virt,
390                    (u64)p_iov->bulletin.phys, p_iov->bulletin.size);
391
392         mutex_init(&p_iov->mutex);
393
394         p_hwfn->vf_iov_info = p_iov;
395
396         p_hwfn->hw_info.personality = QED_PCI_ETH;
397
398         return qed_vf_pf_acquire(p_hwfn);
399
400 free_vf2pf_request:
401         dma_free_coherent(&p_hwfn->cdev->pdev->dev,
402                           sizeof(union vfpf_tlvs),
403                           p_iov->vf2pf_request, p_iov->vf2pf_request_phys);
404 free_p_iov:
405         kfree(p_iov);
406
407         return -ENOMEM;
408 }
409 #define TSTORM_QZONE_START   PXP_VF_BAR0_START_SDM_ZONE_A
410 #define MSTORM_QZONE_START(dev)   (TSTORM_QZONE_START + \
411                                    (TSTORM_QZONE_SIZE * NUM_OF_L2_QUEUES(dev)))
412
413 int qed_vf_pf_rxq_start(struct qed_hwfn *p_hwfn,
414                         u8 rx_qid,
415                         u16 sb,
416                         u8 sb_index,
417                         u16 bd_max_bytes,
418                         dma_addr_t bd_chain_phys_addr,
419                         dma_addr_t cqe_pbl_addr,
420                         u16 cqe_pbl_size, void __iomem **pp_prod)
421 {
422         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
423         struct pfvf_start_queue_resp_tlv *resp;
424         struct vfpf_start_rxq_tlv *req;
425         int rc;
426
427         /* clear mailbox and prep first tlv */
428         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_START_RXQ, sizeof(*req));
429
430         req->rx_qid = rx_qid;
431         req->cqe_pbl_addr = cqe_pbl_addr;
432         req->cqe_pbl_size = cqe_pbl_size;
433         req->rxq_addr = bd_chain_phys_addr;
434         req->hw_sb = sb;
435         req->sb_index = sb_index;
436         req->bd_max_bytes = bd_max_bytes;
437         req->stat_id = -1;
438
439         /* If PF is legacy, we'll need to calculate producers ourselves
440          * as well as clean them.
441          */
442         if (pp_prod && p_iov->b_pre_fp_hsi) {
443                 u8 hw_qid = p_iov->acquire_resp.resc.hw_qid[rx_qid];
444                 u32 init_prod_val = 0;
445
446                 *pp_prod = (u8 __iomem *)p_hwfn->regview +
447                                          MSTORM_QZONE_START(p_hwfn->cdev) +
448                                          hw_qid * MSTORM_QZONE_SIZE;
449
450                 /* Init the rcq, rx bd and rx sge (if valid) producers to 0 */
451                 __internal_ram_wr(p_hwfn, *pp_prod, sizeof(u32),
452                                   (u32 *)(&init_prod_val));
453         }
454         /* add list termination tlv */
455         qed_add_tlv(p_hwfn, &p_iov->offset,
456                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
457
458         resp = &p_iov->pf2vf_reply->queue_start;
459         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
460         if (rc)
461                 goto exit;
462
463         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
464                 rc = -EINVAL;
465                 goto exit;
466         }
467
468         /* Learn the address of the producer from the response */
469         if (pp_prod && !p_iov->b_pre_fp_hsi) {
470                 u32 init_prod_val = 0;
471
472                 *pp_prod = (u8 __iomem *)p_hwfn->regview + resp->offset;
473                 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
474                            "Rxq[0x%02x]: producer at %p [offset 0x%08x]\n",
475                            rx_qid, *pp_prod, resp->offset);
476
477                 /* Init the rcq, rx bd and rx sge (if valid) producers to 0 */
478                 __internal_ram_wr(p_hwfn, *pp_prod, sizeof(u32),
479                                   (u32 *)&init_prod_val);
480         }
481 exit:
482         qed_vf_pf_req_end(p_hwfn, rc);
483
484         return rc;
485 }
486
487 int qed_vf_pf_rxq_stop(struct qed_hwfn *p_hwfn, u16 rx_qid, bool cqe_completion)
488 {
489         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
490         struct vfpf_stop_rxqs_tlv *req;
491         struct pfvf_def_resp_tlv *resp;
492         int rc;
493
494         /* clear mailbox and prep first tlv */
495         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_STOP_RXQS, sizeof(*req));
496
497         req->rx_qid = rx_qid;
498         req->num_rxqs = 1;
499         req->cqe_completion = cqe_completion;
500
501         /* add list termination tlv */
502         qed_add_tlv(p_hwfn, &p_iov->offset,
503                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
504
505         resp = &p_iov->pf2vf_reply->default_resp;
506         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
507         if (rc)
508                 goto exit;
509
510         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
511                 rc = -EINVAL;
512                 goto exit;
513         }
514
515 exit:
516         qed_vf_pf_req_end(p_hwfn, rc);
517
518         return rc;
519 }
520
521 int qed_vf_pf_txq_start(struct qed_hwfn *p_hwfn,
522                         u16 tx_queue_id,
523                         u16 sb,
524                         u8 sb_index,
525                         dma_addr_t pbl_addr,
526                         u16 pbl_size, void __iomem **pp_doorbell)
527 {
528         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
529         struct pfvf_start_queue_resp_tlv *resp;
530         struct vfpf_start_txq_tlv *req;
531         int rc;
532
533         /* clear mailbox and prep first tlv */
534         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_START_TXQ, sizeof(*req));
535
536         req->tx_qid = tx_queue_id;
537
538         /* Tx */
539         req->pbl_addr = pbl_addr;
540         req->pbl_size = pbl_size;
541         req->hw_sb = sb;
542         req->sb_index = sb_index;
543
544         /* add list termination tlv */
545         qed_add_tlv(p_hwfn, &p_iov->offset,
546                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
547
548         resp = &p_iov->pf2vf_reply->queue_start;
549         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
550         if (rc)
551                 goto exit;
552
553         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
554                 rc = -EINVAL;
555                 goto exit;
556         }
557
558         if (pp_doorbell) {
559                 /* Modern PFs provide the actual offsets, while legacy
560                  * provided only the queue id.
561                  */
562                 if (!p_iov->b_pre_fp_hsi) {
563                         *pp_doorbell = (u8 __iomem *)p_hwfn->doorbells +
564                                                      resp->offset;
565                 } else {
566                         u8 cid = p_iov->acquire_resp.resc.cid[tx_queue_id];
567                         u32 db_addr;
568
569                         db_addr = qed_db_addr_vf(cid, DQ_DEMS_LEGACY);
570                         *pp_doorbell = (u8 __iomem *)p_hwfn->doorbells +
571                                                      db_addr;
572                 }
573
574                 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
575                            "Txq[0x%02x]: doorbell at %p [offset 0x%08x]\n",
576                            tx_queue_id, *pp_doorbell, resp->offset);
577         }
578 exit:
579         qed_vf_pf_req_end(p_hwfn, rc);
580
581         return rc;
582 }
583
584 int qed_vf_pf_txq_stop(struct qed_hwfn *p_hwfn, u16 tx_qid)
585 {
586         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
587         struct vfpf_stop_txqs_tlv *req;
588         struct pfvf_def_resp_tlv *resp;
589         int rc;
590
591         /* clear mailbox and prep first tlv */
592         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_STOP_TXQS, sizeof(*req));
593
594         req->tx_qid = tx_qid;
595         req->num_txqs = 1;
596
597         /* add list termination tlv */
598         qed_add_tlv(p_hwfn, &p_iov->offset,
599                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
600
601         resp = &p_iov->pf2vf_reply->default_resp;
602         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
603         if (rc)
604                 goto exit;
605
606         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
607                 rc = -EINVAL;
608                 goto exit;
609         }
610
611 exit:
612         qed_vf_pf_req_end(p_hwfn, rc);
613
614         return rc;
615 }
616
617 int qed_vf_pf_vport_start(struct qed_hwfn *p_hwfn,
618                           u8 vport_id,
619                           u16 mtu,
620                           u8 inner_vlan_removal,
621                           enum qed_tpa_mode tpa_mode,
622                           u8 max_buffers_per_cqe, u8 only_untagged)
623 {
624         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
625         struct vfpf_vport_start_tlv *req;
626         struct pfvf_def_resp_tlv *resp;
627         int rc, i;
628
629         /* clear mailbox and prep first tlv */
630         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_VPORT_START, sizeof(*req));
631
632         req->mtu = mtu;
633         req->vport_id = vport_id;
634         req->inner_vlan_removal = inner_vlan_removal;
635         req->tpa_mode = tpa_mode;
636         req->max_buffers_per_cqe = max_buffers_per_cqe;
637         req->only_untagged = only_untagged;
638
639         /* status blocks */
640         for (i = 0; i < p_hwfn->vf_iov_info->acquire_resp.resc.num_sbs; i++)
641                 if (p_hwfn->sbs_info[i])
642                         req->sb_addr[i] = p_hwfn->sbs_info[i]->sb_phys;
643
644         /* add list termination tlv */
645         qed_add_tlv(p_hwfn, &p_iov->offset,
646                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
647
648         resp = &p_iov->pf2vf_reply->default_resp;
649         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
650         if (rc)
651                 goto exit;
652
653         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
654                 rc = -EINVAL;
655                 goto exit;
656         }
657
658 exit:
659         qed_vf_pf_req_end(p_hwfn, rc);
660
661         return rc;
662 }
663
664 int qed_vf_pf_vport_stop(struct qed_hwfn *p_hwfn)
665 {
666         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
667         struct pfvf_def_resp_tlv *resp = &p_iov->pf2vf_reply->default_resp;
668         int rc;
669
670         /* clear mailbox and prep first tlv */
671         qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_VPORT_TEARDOWN,
672                        sizeof(struct vfpf_first_tlv));
673
674         /* add list termination tlv */
675         qed_add_tlv(p_hwfn, &p_iov->offset,
676                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
677
678         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
679         if (rc)
680                 goto exit;
681
682         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
683                 rc = -EINVAL;
684                 goto exit;
685         }
686
687 exit:
688         qed_vf_pf_req_end(p_hwfn, rc);
689
690         return rc;
691 }
692
693 static bool
694 qed_vf_handle_vp_update_is_needed(struct qed_hwfn *p_hwfn,
695                                   struct qed_sp_vport_update_params *p_data,
696                                   u16 tlv)
697 {
698         switch (tlv) {
699         case CHANNEL_TLV_VPORT_UPDATE_ACTIVATE:
700                 return !!(p_data->update_vport_active_rx_flg ||
701                           p_data->update_vport_active_tx_flg);
702         case CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH:
703                 return !!p_data->update_tx_switching_flg;
704         case CHANNEL_TLV_VPORT_UPDATE_VLAN_STRIP:
705                 return !!p_data->update_inner_vlan_removal_flg;
706         case CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN:
707                 return !!p_data->update_accept_any_vlan_flg;
708         case CHANNEL_TLV_VPORT_UPDATE_MCAST:
709                 return !!p_data->update_approx_mcast_flg;
710         case CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM:
711                 return !!(p_data->accept_flags.update_rx_mode_config ||
712                           p_data->accept_flags.update_tx_mode_config);
713         case CHANNEL_TLV_VPORT_UPDATE_RSS:
714                 return !!p_data->rss_params;
715         case CHANNEL_TLV_VPORT_UPDATE_SGE_TPA:
716                 return !!p_data->sge_tpa_params;
717         default:
718                 DP_INFO(p_hwfn, "Unexpected vport-update TLV[%d]\n",
719                         tlv);
720                 return false;
721         }
722 }
723
724 static void
725 qed_vf_handle_vp_update_tlvs_resp(struct qed_hwfn *p_hwfn,
726                                   struct qed_sp_vport_update_params *p_data)
727 {
728         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
729         struct pfvf_def_resp_tlv *p_resp;
730         u16 tlv;
731
732         for (tlv = CHANNEL_TLV_VPORT_UPDATE_ACTIVATE;
733              tlv < CHANNEL_TLV_VPORT_UPDATE_MAX; tlv++) {
734                 if (!qed_vf_handle_vp_update_is_needed(p_hwfn, p_data, tlv))
735                         continue;
736
737                 p_resp = (struct pfvf_def_resp_tlv *)
738                          qed_iov_search_list_tlvs(p_hwfn, p_iov->pf2vf_reply,
739                                                   tlv);
740                 if (p_resp && p_resp->hdr.status)
741                         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
742                                    "TLV[%d] Configuration %s\n",
743                                    tlv,
744                                    (p_resp && p_resp->hdr.status) ? "succeeded"
745                                                                   : "failed");
746         }
747 }
748
749 int qed_vf_pf_vport_update(struct qed_hwfn *p_hwfn,
750                            struct qed_sp_vport_update_params *p_params)
751 {
752         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
753         struct vfpf_vport_update_tlv *req;
754         struct pfvf_def_resp_tlv *resp;
755         u8 update_rx, update_tx;
756         u32 resp_size = 0;
757         u16 size, tlv;
758         int rc;
759
760         resp = &p_iov->pf2vf_reply->default_resp;
761         resp_size = sizeof(*resp);
762
763         update_rx = p_params->update_vport_active_rx_flg;
764         update_tx = p_params->update_vport_active_tx_flg;
765
766         /* clear mailbox and prep header tlv */
767         qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_VPORT_UPDATE, sizeof(*req));
768
769         /* Prepare extended tlvs */
770         if (update_rx || update_tx) {
771                 struct vfpf_vport_update_activate_tlv *p_act_tlv;
772
773                 size = sizeof(struct vfpf_vport_update_activate_tlv);
774                 p_act_tlv = qed_add_tlv(p_hwfn, &p_iov->offset,
775                                         CHANNEL_TLV_VPORT_UPDATE_ACTIVATE,
776                                         size);
777                 resp_size += sizeof(struct pfvf_def_resp_tlv);
778
779                 if (update_rx) {
780                         p_act_tlv->update_rx = update_rx;
781                         p_act_tlv->active_rx = p_params->vport_active_rx_flg;
782                 }
783
784                 if (update_tx) {
785                         p_act_tlv->update_tx = update_tx;
786                         p_act_tlv->active_tx = p_params->vport_active_tx_flg;
787                 }
788         }
789
790         if (p_params->update_tx_switching_flg) {
791                 struct vfpf_vport_update_tx_switch_tlv *p_tx_switch_tlv;
792
793                 size = sizeof(struct vfpf_vport_update_tx_switch_tlv);
794                 tlv = CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH;
795                 p_tx_switch_tlv = qed_add_tlv(p_hwfn, &p_iov->offset,
796                                               tlv, size);
797                 resp_size += sizeof(struct pfvf_def_resp_tlv);
798
799                 p_tx_switch_tlv->tx_switching = p_params->tx_switching_flg;
800         }
801
802         if (p_params->update_approx_mcast_flg) {
803                 struct vfpf_vport_update_mcast_bin_tlv *p_mcast_tlv;
804
805                 size = sizeof(struct vfpf_vport_update_mcast_bin_tlv);
806                 p_mcast_tlv = qed_add_tlv(p_hwfn, &p_iov->offset,
807                                           CHANNEL_TLV_VPORT_UPDATE_MCAST, size);
808                 resp_size += sizeof(struct pfvf_def_resp_tlv);
809
810                 memcpy(p_mcast_tlv->bins, p_params->bins,
811                        sizeof(u32) * ETH_MULTICAST_MAC_BINS_IN_REGS);
812         }
813
814         update_rx = p_params->accept_flags.update_rx_mode_config;
815         update_tx = p_params->accept_flags.update_tx_mode_config;
816
817         if (update_rx || update_tx) {
818                 struct vfpf_vport_update_accept_param_tlv *p_accept_tlv;
819
820                 tlv = CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM;
821                 size = sizeof(struct vfpf_vport_update_accept_param_tlv);
822                 p_accept_tlv = qed_add_tlv(p_hwfn, &p_iov->offset, tlv, size);
823                 resp_size += sizeof(struct pfvf_def_resp_tlv);
824
825                 if (update_rx) {
826                         p_accept_tlv->update_rx_mode = update_rx;
827                         p_accept_tlv->rx_accept_filter =
828                             p_params->accept_flags.rx_accept_filter;
829                 }
830
831                 if (update_tx) {
832                         p_accept_tlv->update_tx_mode = update_tx;
833                         p_accept_tlv->tx_accept_filter =
834                             p_params->accept_flags.tx_accept_filter;
835                 }
836         }
837
838         if (p_params->rss_params) {
839                 struct qed_rss_params *rss_params = p_params->rss_params;
840                 struct vfpf_vport_update_rss_tlv *p_rss_tlv;
841
842                 size = sizeof(struct vfpf_vport_update_rss_tlv);
843                 p_rss_tlv = qed_add_tlv(p_hwfn,
844                                         &p_iov->offset,
845                                         CHANNEL_TLV_VPORT_UPDATE_RSS, size);
846                 resp_size += sizeof(struct pfvf_def_resp_tlv);
847
848                 if (rss_params->update_rss_config)
849                         p_rss_tlv->update_rss_flags |=
850                             VFPF_UPDATE_RSS_CONFIG_FLAG;
851                 if (rss_params->update_rss_capabilities)
852                         p_rss_tlv->update_rss_flags |=
853                             VFPF_UPDATE_RSS_CAPS_FLAG;
854                 if (rss_params->update_rss_ind_table)
855                         p_rss_tlv->update_rss_flags |=
856                             VFPF_UPDATE_RSS_IND_TABLE_FLAG;
857                 if (rss_params->update_rss_key)
858                         p_rss_tlv->update_rss_flags |= VFPF_UPDATE_RSS_KEY_FLAG;
859
860                 p_rss_tlv->rss_enable = rss_params->rss_enable;
861                 p_rss_tlv->rss_caps = rss_params->rss_caps;
862                 p_rss_tlv->rss_table_size_log = rss_params->rss_table_size_log;
863                 memcpy(p_rss_tlv->rss_ind_table, rss_params->rss_ind_table,
864                        sizeof(rss_params->rss_ind_table));
865                 memcpy(p_rss_tlv->rss_key, rss_params->rss_key,
866                        sizeof(rss_params->rss_key));
867         }
868
869         if (p_params->update_accept_any_vlan_flg) {
870                 struct vfpf_vport_update_accept_any_vlan_tlv *p_any_vlan_tlv;
871
872                 size = sizeof(struct vfpf_vport_update_accept_any_vlan_tlv);
873                 tlv = CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN;
874                 p_any_vlan_tlv = qed_add_tlv(p_hwfn, &p_iov->offset, tlv, size);
875
876                 resp_size += sizeof(struct pfvf_def_resp_tlv);
877                 p_any_vlan_tlv->accept_any_vlan = p_params->accept_any_vlan;
878                 p_any_vlan_tlv->update_accept_any_vlan_flg =
879                     p_params->update_accept_any_vlan_flg;
880         }
881
882         /* add list termination tlv */
883         qed_add_tlv(p_hwfn, &p_iov->offset,
884                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
885
886         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, resp_size);
887         if (rc)
888                 goto exit;
889
890         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
891                 rc = -EINVAL;
892                 goto exit;
893         }
894
895         qed_vf_handle_vp_update_tlvs_resp(p_hwfn, p_params);
896
897 exit:
898         qed_vf_pf_req_end(p_hwfn, rc);
899
900         return rc;
901 }
902
903 int qed_vf_pf_reset(struct qed_hwfn *p_hwfn)
904 {
905         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
906         struct pfvf_def_resp_tlv *resp;
907         struct vfpf_first_tlv *req;
908         int rc;
909
910         /* clear mailbox and prep first tlv */
911         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_CLOSE, sizeof(*req));
912
913         /* add list termination tlv */
914         qed_add_tlv(p_hwfn, &p_iov->offset,
915                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
916
917         resp = &p_iov->pf2vf_reply->default_resp;
918         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
919         if (rc)
920                 goto exit;
921
922         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
923                 rc = -EAGAIN;
924                 goto exit;
925         }
926
927         p_hwfn->b_int_enabled = 0;
928
929 exit:
930         qed_vf_pf_req_end(p_hwfn, rc);
931
932         return rc;
933 }
934
935 int qed_vf_pf_release(struct qed_hwfn *p_hwfn)
936 {
937         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
938         struct pfvf_def_resp_tlv *resp;
939         struct vfpf_first_tlv *req;
940         u32 size;
941         int rc;
942
943         /* clear mailbox and prep first tlv */
944         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_RELEASE, sizeof(*req));
945
946         /* add list termination tlv */
947         qed_add_tlv(p_hwfn, &p_iov->offset,
948                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
949
950         resp = &p_iov->pf2vf_reply->default_resp;
951         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
952
953         if (!rc && resp->hdr.status != PFVF_STATUS_SUCCESS)
954                 rc = -EAGAIN;
955
956         qed_vf_pf_req_end(p_hwfn, rc);
957
958         p_hwfn->b_int_enabled = 0;
959
960         if (p_iov->vf2pf_request)
961                 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
962                                   sizeof(union vfpf_tlvs),
963                                   p_iov->vf2pf_request,
964                                   p_iov->vf2pf_request_phys);
965         if (p_iov->pf2vf_reply)
966                 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
967                                   sizeof(union pfvf_tlvs),
968                                   p_iov->pf2vf_reply, p_iov->pf2vf_reply_phys);
969
970         if (p_iov->bulletin.p_virt) {
971                 size = sizeof(struct qed_bulletin_content);
972                 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
973                                   size,
974                                   p_iov->bulletin.p_virt, p_iov->bulletin.phys);
975         }
976
977         kfree(p_hwfn->vf_iov_info);
978         p_hwfn->vf_iov_info = NULL;
979
980         return rc;
981 }
982
983 void qed_vf_pf_filter_mcast(struct qed_hwfn *p_hwfn,
984                             struct qed_filter_mcast *p_filter_cmd)
985 {
986         struct qed_sp_vport_update_params sp_params;
987         int i;
988
989         memset(&sp_params, 0, sizeof(sp_params));
990         sp_params.update_approx_mcast_flg = 1;
991
992         if (p_filter_cmd->opcode == QED_FILTER_ADD) {
993                 for (i = 0; i < p_filter_cmd->num_mc_addrs; i++) {
994                         u32 bit;
995
996                         bit = qed_mcast_bin_from_mac(p_filter_cmd->mac[i]);
997                         sp_params.bins[bit / 32] |= 1 << (bit % 32);
998                 }
999         }
1000
1001         qed_vf_pf_vport_update(p_hwfn, &sp_params);
1002 }
1003
1004 int qed_vf_pf_filter_ucast(struct qed_hwfn *p_hwfn,
1005                            struct qed_filter_ucast *p_ucast)
1006 {
1007         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
1008         struct vfpf_ucast_filter_tlv *req;
1009         struct pfvf_def_resp_tlv *resp;
1010         int rc;
1011
1012         /* clear mailbox and prep first tlv */
1013         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_UCAST_FILTER, sizeof(*req));
1014         req->opcode = (u8) p_ucast->opcode;
1015         req->type = (u8) p_ucast->type;
1016         memcpy(req->mac, p_ucast->mac, ETH_ALEN);
1017         req->vlan = p_ucast->vlan;
1018
1019         /* add list termination tlv */
1020         qed_add_tlv(p_hwfn, &p_iov->offset,
1021                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
1022
1023         resp = &p_iov->pf2vf_reply->default_resp;
1024         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
1025         if (rc)
1026                 goto exit;
1027
1028         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
1029                 rc = -EAGAIN;
1030                 goto exit;
1031         }
1032
1033 exit:
1034         qed_vf_pf_req_end(p_hwfn, rc);
1035
1036         return rc;
1037 }
1038
1039 int qed_vf_pf_int_cleanup(struct qed_hwfn *p_hwfn)
1040 {
1041         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
1042         struct pfvf_def_resp_tlv *resp = &p_iov->pf2vf_reply->default_resp;
1043         int rc;
1044
1045         /* clear mailbox and prep first tlv */
1046         qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_INT_CLEANUP,
1047                        sizeof(struct vfpf_first_tlv));
1048
1049         /* add list termination tlv */
1050         qed_add_tlv(p_hwfn, &p_iov->offset,
1051                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
1052
1053         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
1054         if (rc)
1055                 goto exit;
1056
1057         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
1058                 rc = -EINVAL;
1059                 goto exit;
1060         }
1061
1062 exit:
1063         qed_vf_pf_req_end(p_hwfn, rc);
1064
1065         return rc;
1066 }
1067
1068 u16 qed_vf_get_igu_sb_id(struct qed_hwfn *p_hwfn, u16 sb_id)
1069 {
1070         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
1071
1072         if (!p_iov) {
1073                 DP_NOTICE(p_hwfn, "vf_sriov_info isn't initialized\n");
1074                 return 0;
1075         }
1076
1077         return p_iov->acquire_resp.resc.hw_sbs[sb_id].hw_sb_id;
1078 }
1079
1080 int qed_vf_read_bulletin(struct qed_hwfn *p_hwfn, u8 *p_change)
1081 {
1082         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
1083         struct qed_bulletin_content shadow;
1084         u32 crc, crc_size;
1085
1086         crc_size = sizeof(p_iov->bulletin.p_virt->crc);
1087         *p_change = 0;
1088
1089         /* Need to guarantee PF is not in the middle of writing it */
1090         memcpy(&shadow, p_iov->bulletin.p_virt, p_iov->bulletin.size);
1091
1092         /* If version did not update, no need to do anything */
1093         if (shadow.version == p_iov->bulletin_shadow.version)
1094                 return 0;
1095
1096         /* Verify the bulletin we see is valid */
1097         crc = crc32(0, (u8 *)&shadow + crc_size,
1098                     p_iov->bulletin.size - crc_size);
1099         if (crc != shadow.crc)
1100                 return -EAGAIN;
1101
1102         /* Set the shadow bulletin and process it */
1103         memcpy(&p_iov->bulletin_shadow, &shadow, p_iov->bulletin.size);
1104
1105         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
1106                    "Read a bulletin update %08x\n", shadow.version);
1107
1108         *p_change = 1;
1109
1110         return 0;
1111 }
1112
1113 void __qed_vf_get_link_params(struct qed_hwfn *p_hwfn,
1114                               struct qed_mcp_link_params *p_params,
1115                               struct qed_bulletin_content *p_bulletin)
1116 {
1117         memset(p_params, 0, sizeof(*p_params));
1118
1119         p_params->speed.autoneg = p_bulletin->req_autoneg;
1120         p_params->speed.advertised_speeds = p_bulletin->req_adv_speed;
1121         p_params->speed.forced_speed = p_bulletin->req_forced_speed;
1122         p_params->pause.autoneg = p_bulletin->req_autoneg_pause;
1123         p_params->pause.forced_rx = p_bulletin->req_forced_rx;
1124         p_params->pause.forced_tx = p_bulletin->req_forced_tx;
1125         p_params->loopback_mode = p_bulletin->req_loopback;
1126 }
1127
1128 void qed_vf_get_link_params(struct qed_hwfn *p_hwfn,
1129                             struct qed_mcp_link_params *params)
1130 {
1131         __qed_vf_get_link_params(p_hwfn, params,
1132                                  &(p_hwfn->vf_iov_info->bulletin_shadow));
1133 }
1134
1135 void __qed_vf_get_link_state(struct qed_hwfn *p_hwfn,
1136                              struct qed_mcp_link_state *p_link,
1137                              struct qed_bulletin_content *p_bulletin)
1138 {
1139         memset(p_link, 0, sizeof(*p_link));
1140
1141         p_link->link_up = p_bulletin->link_up;
1142         p_link->speed = p_bulletin->speed;
1143         p_link->full_duplex = p_bulletin->full_duplex;
1144         p_link->an = p_bulletin->autoneg;
1145         p_link->an_complete = p_bulletin->autoneg_complete;
1146         p_link->parallel_detection = p_bulletin->parallel_detection;
1147         p_link->pfc_enabled = p_bulletin->pfc_enabled;
1148         p_link->partner_adv_speed = p_bulletin->partner_adv_speed;
1149         p_link->partner_tx_flow_ctrl_en = p_bulletin->partner_tx_flow_ctrl_en;
1150         p_link->partner_rx_flow_ctrl_en = p_bulletin->partner_rx_flow_ctrl_en;
1151         p_link->partner_adv_pause = p_bulletin->partner_adv_pause;
1152         p_link->sfp_tx_fault = p_bulletin->sfp_tx_fault;
1153 }
1154
1155 void qed_vf_get_link_state(struct qed_hwfn *p_hwfn,
1156                            struct qed_mcp_link_state *link)
1157 {
1158         __qed_vf_get_link_state(p_hwfn, link,
1159                                 &(p_hwfn->vf_iov_info->bulletin_shadow));
1160 }
1161
1162 void __qed_vf_get_link_caps(struct qed_hwfn *p_hwfn,
1163                             struct qed_mcp_link_capabilities *p_link_caps,
1164                             struct qed_bulletin_content *p_bulletin)
1165 {
1166         memset(p_link_caps, 0, sizeof(*p_link_caps));
1167         p_link_caps->speed_capabilities = p_bulletin->capability_speed;
1168 }
1169
1170 void qed_vf_get_link_caps(struct qed_hwfn *p_hwfn,
1171                           struct qed_mcp_link_capabilities *p_link_caps)
1172 {
1173         __qed_vf_get_link_caps(p_hwfn, p_link_caps,
1174                                &(p_hwfn->vf_iov_info->bulletin_shadow));
1175 }
1176
1177 void qed_vf_get_num_rxqs(struct qed_hwfn *p_hwfn, u8 *num_rxqs)
1178 {
1179         *num_rxqs = p_hwfn->vf_iov_info->acquire_resp.resc.num_rxqs;
1180 }
1181
1182 void qed_vf_get_port_mac(struct qed_hwfn *p_hwfn, u8 *port_mac)
1183 {
1184         memcpy(port_mac,
1185                p_hwfn->vf_iov_info->acquire_resp.pfdev_info.port_mac, ETH_ALEN);
1186 }
1187
1188 void qed_vf_get_num_vlan_filters(struct qed_hwfn *p_hwfn, u8 *num_vlan_filters)
1189 {
1190         struct qed_vf_iov *p_vf;
1191
1192         p_vf = p_hwfn->vf_iov_info;
1193         *num_vlan_filters = p_vf->acquire_resp.resc.num_vlan_filters;
1194 }
1195
1196 bool qed_vf_check_mac(struct qed_hwfn *p_hwfn, u8 *mac)
1197 {
1198         struct qed_bulletin_content *bulletin;
1199
1200         bulletin = &p_hwfn->vf_iov_info->bulletin_shadow;
1201         if (!(bulletin->valid_bitmap & (1 << MAC_ADDR_FORCED)))
1202                 return true;
1203
1204         /* Forbid VF from changing a MAC enforced by PF */
1205         if (ether_addr_equal(bulletin->mac, mac))
1206                 return false;
1207
1208         return false;
1209 }
1210
1211 static bool qed_vf_bulletin_get_forced_mac(struct qed_hwfn *hwfn,
1212                                            u8 *dst_mac, u8 *p_is_forced)
1213 {
1214         struct qed_bulletin_content *bulletin;
1215
1216         bulletin = &hwfn->vf_iov_info->bulletin_shadow;
1217
1218         if (bulletin->valid_bitmap & (1 << MAC_ADDR_FORCED)) {
1219                 if (p_is_forced)
1220                         *p_is_forced = 1;
1221         } else if (bulletin->valid_bitmap & (1 << VFPF_BULLETIN_MAC_ADDR)) {
1222                 if (p_is_forced)
1223                         *p_is_forced = 0;
1224         } else {
1225                 return false;
1226         }
1227
1228         ether_addr_copy(dst_mac, bulletin->mac);
1229
1230         return true;
1231 }
1232
1233 void qed_vf_get_fw_version(struct qed_hwfn *p_hwfn,
1234                            u16 *fw_major, u16 *fw_minor,
1235                            u16 *fw_rev, u16 *fw_eng)
1236 {
1237         struct pf_vf_pfdev_info *info;
1238
1239         info = &p_hwfn->vf_iov_info->acquire_resp.pfdev_info;
1240
1241         *fw_major = info->fw_major;
1242         *fw_minor = info->fw_minor;
1243         *fw_rev = info->fw_rev;
1244         *fw_eng = info->fw_eng;
1245 }
1246
1247 static void qed_handle_bulletin_change(struct qed_hwfn *hwfn)
1248 {
1249         struct qed_eth_cb_ops *ops = hwfn->cdev->protocol_ops.eth;
1250         u8 mac[ETH_ALEN], is_mac_exist, is_mac_forced;
1251         void *cookie = hwfn->cdev->ops_cookie;
1252
1253         is_mac_exist = qed_vf_bulletin_get_forced_mac(hwfn, mac,
1254                                                       &is_mac_forced);
1255         if (is_mac_exist && is_mac_forced && cookie)
1256                 ops->force_mac(cookie, mac);
1257
1258         /* Always update link configuration according to bulletin */
1259         qed_link_update(hwfn);
1260 }
1261
1262 void qed_iov_vf_task(struct work_struct *work)
1263 {
1264         struct qed_hwfn *hwfn = container_of(work, struct qed_hwfn,
1265                                              iov_task.work);
1266         u8 change = 0;
1267
1268         if (test_and_clear_bit(QED_IOV_WQ_STOP_WQ_FLAG, &hwfn->iov_task_flags))
1269                 return;
1270
1271         /* Handle bulletin board changes */
1272         qed_vf_read_bulletin(hwfn, &change);
1273         if (change)
1274                 qed_handle_bulletin_change(hwfn);
1275
1276         /* As VF is polling bulletin board, need to constantly re-schedule */
1277         queue_delayed_work(hwfn->iov_wq, &hwfn->iov_task, HZ);
1278 }