GNU Linux-libre 4.9.337-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         if (!p_iov->bulletin.p_virt)
388                 goto free_pf2vf_reply;
389
390         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
391                    "VF's bulletin Board [%p virt 0x%llx phys 0x%08x bytes]\n",
392                    p_iov->bulletin.p_virt,
393                    (u64)p_iov->bulletin.phys, p_iov->bulletin.size);
394
395         mutex_init(&p_iov->mutex);
396
397         p_hwfn->vf_iov_info = p_iov;
398
399         p_hwfn->hw_info.personality = QED_PCI_ETH;
400
401         return qed_vf_pf_acquire(p_hwfn);
402
403 free_pf2vf_reply:
404         dma_free_coherent(&p_hwfn->cdev->pdev->dev,
405                           sizeof(union pfvf_tlvs),
406                           p_iov->pf2vf_reply, p_iov->pf2vf_reply_phys);
407 free_vf2pf_request:
408         dma_free_coherent(&p_hwfn->cdev->pdev->dev,
409                           sizeof(union vfpf_tlvs),
410                           p_iov->vf2pf_request, p_iov->vf2pf_request_phys);
411 free_p_iov:
412         kfree(p_iov);
413
414         return -ENOMEM;
415 }
416 #define TSTORM_QZONE_START   PXP_VF_BAR0_START_SDM_ZONE_A
417 #define MSTORM_QZONE_START(dev)   (TSTORM_QZONE_START + \
418                                    (TSTORM_QZONE_SIZE * NUM_OF_L2_QUEUES(dev)))
419
420 int qed_vf_pf_rxq_start(struct qed_hwfn *p_hwfn,
421                         u8 rx_qid,
422                         u16 sb,
423                         u8 sb_index,
424                         u16 bd_max_bytes,
425                         dma_addr_t bd_chain_phys_addr,
426                         dma_addr_t cqe_pbl_addr,
427                         u16 cqe_pbl_size, void __iomem **pp_prod)
428 {
429         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
430         struct pfvf_start_queue_resp_tlv *resp;
431         struct vfpf_start_rxq_tlv *req;
432         int rc;
433
434         /* clear mailbox and prep first tlv */
435         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_START_RXQ, sizeof(*req));
436
437         req->rx_qid = rx_qid;
438         req->cqe_pbl_addr = cqe_pbl_addr;
439         req->cqe_pbl_size = cqe_pbl_size;
440         req->rxq_addr = bd_chain_phys_addr;
441         req->hw_sb = sb;
442         req->sb_index = sb_index;
443         req->bd_max_bytes = bd_max_bytes;
444         req->stat_id = -1;
445
446         /* If PF is legacy, we'll need to calculate producers ourselves
447          * as well as clean them.
448          */
449         if (pp_prod && p_iov->b_pre_fp_hsi) {
450                 u8 hw_qid = p_iov->acquire_resp.resc.hw_qid[rx_qid];
451                 u32 init_prod_val = 0;
452
453                 *pp_prod = (u8 __iomem *)p_hwfn->regview +
454                                          MSTORM_QZONE_START(p_hwfn->cdev) +
455                                          hw_qid * MSTORM_QZONE_SIZE;
456
457                 /* Init the rcq, rx bd and rx sge (if valid) producers to 0 */
458                 __internal_ram_wr(p_hwfn, *pp_prod, sizeof(u32),
459                                   (u32 *)(&init_prod_val));
460         }
461         /* add list termination tlv */
462         qed_add_tlv(p_hwfn, &p_iov->offset,
463                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
464
465         resp = &p_iov->pf2vf_reply->queue_start;
466         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
467         if (rc)
468                 goto exit;
469
470         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
471                 rc = -EINVAL;
472                 goto exit;
473         }
474
475         /* Learn the address of the producer from the response */
476         if (pp_prod && !p_iov->b_pre_fp_hsi) {
477                 u32 init_prod_val = 0;
478
479                 *pp_prod = (u8 __iomem *)p_hwfn->regview + resp->offset;
480                 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
481                            "Rxq[0x%02x]: producer at %p [offset 0x%08x]\n",
482                            rx_qid, *pp_prod, resp->offset);
483
484                 /* Init the rcq, rx bd and rx sge (if valid) producers to 0 */
485                 __internal_ram_wr(p_hwfn, *pp_prod, sizeof(u32),
486                                   (u32 *)&init_prod_val);
487         }
488 exit:
489         qed_vf_pf_req_end(p_hwfn, rc);
490
491         return rc;
492 }
493
494 int qed_vf_pf_rxq_stop(struct qed_hwfn *p_hwfn, u16 rx_qid, bool cqe_completion)
495 {
496         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
497         struct vfpf_stop_rxqs_tlv *req;
498         struct pfvf_def_resp_tlv *resp;
499         int rc;
500
501         /* clear mailbox and prep first tlv */
502         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_STOP_RXQS, sizeof(*req));
503
504         req->rx_qid = rx_qid;
505         req->num_rxqs = 1;
506         req->cqe_completion = cqe_completion;
507
508         /* add list termination tlv */
509         qed_add_tlv(p_hwfn, &p_iov->offset,
510                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
511
512         resp = &p_iov->pf2vf_reply->default_resp;
513         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
514         if (rc)
515                 goto exit;
516
517         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
518                 rc = -EINVAL;
519                 goto exit;
520         }
521
522 exit:
523         qed_vf_pf_req_end(p_hwfn, rc);
524
525         return rc;
526 }
527
528 int qed_vf_pf_txq_start(struct qed_hwfn *p_hwfn,
529                         u16 tx_queue_id,
530                         u16 sb,
531                         u8 sb_index,
532                         dma_addr_t pbl_addr,
533                         u16 pbl_size, void __iomem **pp_doorbell)
534 {
535         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
536         struct pfvf_start_queue_resp_tlv *resp;
537         struct vfpf_start_txq_tlv *req;
538         int rc;
539
540         /* clear mailbox and prep first tlv */
541         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_START_TXQ, sizeof(*req));
542
543         req->tx_qid = tx_queue_id;
544
545         /* Tx */
546         req->pbl_addr = pbl_addr;
547         req->pbl_size = pbl_size;
548         req->hw_sb = sb;
549         req->sb_index = sb_index;
550
551         /* add list termination tlv */
552         qed_add_tlv(p_hwfn, &p_iov->offset,
553                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
554
555         resp = &p_iov->pf2vf_reply->queue_start;
556         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
557         if (rc)
558                 goto exit;
559
560         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
561                 rc = -EINVAL;
562                 goto exit;
563         }
564
565         if (pp_doorbell) {
566                 /* Modern PFs provide the actual offsets, while legacy
567                  * provided only the queue id.
568                  */
569                 if (!p_iov->b_pre_fp_hsi) {
570                         *pp_doorbell = (u8 __iomem *)p_hwfn->doorbells +
571                                                      resp->offset;
572                 } else {
573                         u8 cid = p_iov->acquire_resp.resc.cid[tx_queue_id];
574                         u32 db_addr;
575
576                         db_addr = qed_db_addr_vf(cid, DQ_DEMS_LEGACY);
577                         *pp_doorbell = (u8 __iomem *)p_hwfn->doorbells +
578                                                      db_addr;
579                 }
580
581                 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
582                            "Txq[0x%02x]: doorbell at %p [offset 0x%08x]\n",
583                            tx_queue_id, *pp_doorbell, resp->offset);
584         }
585 exit:
586         qed_vf_pf_req_end(p_hwfn, rc);
587
588         return rc;
589 }
590
591 int qed_vf_pf_txq_stop(struct qed_hwfn *p_hwfn, u16 tx_qid)
592 {
593         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
594         struct vfpf_stop_txqs_tlv *req;
595         struct pfvf_def_resp_tlv *resp;
596         int rc;
597
598         /* clear mailbox and prep first tlv */
599         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_STOP_TXQS, sizeof(*req));
600
601         req->tx_qid = tx_qid;
602         req->num_txqs = 1;
603
604         /* add list termination tlv */
605         qed_add_tlv(p_hwfn, &p_iov->offset,
606                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
607
608         resp = &p_iov->pf2vf_reply->default_resp;
609         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
610         if (rc)
611                 goto exit;
612
613         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
614                 rc = -EINVAL;
615                 goto exit;
616         }
617
618 exit:
619         qed_vf_pf_req_end(p_hwfn, rc);
620
621         return rc;
622 }
623
624 int qed_vf_pf_vport_start(struct qed_hwfn *p_hwfn,
625                           u8 vport_id,
626                           u16 mtu,
627                           u8 inner_vlan_removal,
628                           enum qed_tpa_mode tpa_mode,
629                           u8 max_buffers_per_cqe, u8 only_untagged)
630 {
631         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
632         struct vfpf_vport_start_tlv *req;
633         struct pfvf_def_resp_tlv *resp;
634         int rc, i;
635
636         /* clear mailbox and prep first tlv */
637         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_VPORT_START, sizeof(*req));
638
639         req->mtu = mtu;
640         req->vport_id = vport_id;
641         req->inner_vlan_removal = inner_vlan_removal;
642         req->tpa_mode = tpa_mode;
643         req->max_buffers_per_cqe = max_buffers_per_cqe;
644         req->only_untagged = only_untagged;
645
646         /* status blocks */
647         for (i = 0; i < p_hwfn->vf_iov_info->acquire_resp.resc.num_sbs; i++)
648                 if (p_hwfn->sbs_info[i])
649                         req->sb_addr[i] = p_hwfn->sbs_info[i]->sb_phys;
650
651         /* add list termination tlv */
652         qed_add_tlv(p_hwfn, &p_iov->offset,
653                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
654
655         resp = &p_iov->pf2vf_reply->default_resp;
656         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
657         if (rc)
658                 goto exit;
659
660         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
661                 rc = -EINVAL;
662                 goto exit;
663         }
664
665 exit:
666         qed_vf_pf_req_end(p_hwfn, rc);
667
668         return rc;
669 }
670
671 int qed_vf_pf_vport_stop(struct qed_hwfn *p_hwfn)
672 {
673         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
674         struct pfvf_def_resp_tlv *resp = &p_iov->pf2vf_reply->default_resp;
675         int rc;
676
677         /* clear mailbox and prep first tlv */
678         qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_VPORT_TEARDOWN,
679                        sizeof(struct vfpf_first_tlv));
680
681         /* add list termination tlv */
682         qed_add_tlv(p_hwfn, &p_iov->offset,
683                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
684
685         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
686         if (rc)
687                 goto exit;
688
689         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
690                 rc = -EINVAL;
691                 goto exit;
692         }
693
694 exit:
695         qed_vf_pf_req_end(p_hwfn, rc);
696
697         return rc;
698 }
699
700 static bool
701 qed_vf_handle_vp_update_is_needed(struct qed_hwfn *p_hwfn,
702                                   struct qed_sp_vport_update_params *p_data,
703                                   u16 tlv)
704 {
705         switch (tlv) {
706         case CHANNEL_TLV_VPORT_UPDATE_ACTIVATE:
707                 return !!(p_data->update_vport_active_rx_flg ||
708                           p_data->update_vport_active_tx_flg);
709         case CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH:
710                 return !!p_data->update_tx_switching_flg;
711         case CHANNEL_TLV_VPORT_UPDATE_VLAN_STRIP:
712                 return !!p_data->update_inner_vlan_removal_flg;
713         case CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN:
714                 return !!p_data->update_accept_any_vlan_flg;
715         case CHANNEL_TLV_VPORT_UPDATE_MCAST:
716                 return !!p_data->update_approx_mcast_flg;
717         case CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM:
718                 return !!(p_data->accept_flags.update_rx_mode_config ||
719                           p_data->accept_flags.update_tx_mode_config);
720         case CHANNEL_TLV_VPORT_UPDATE_RSS:
721                 return !!p_data->rss_params;
722         case CHANNEL_TLV_VPORT_UPDATE_SGE_TPA:
723                 return !!p_data->sge_tpa_params;
724         default:
725                 DP_INFO(p_hwfn, "Unexpected vport-update TLV[%d]\n",
726                         tlv);
727                 return false;
728         }
729 }
730
731 static void
732 qed_vf_handle_vp_update_tlvs_resp(struct qed_hwfn *p_hwfn,
733                                   struct qed_sp_vport_update_params *p_data)
734 {
735         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
736         struct pfvf_def_resp_tlv *p_resp;
737         u16 tlv;
738
739         for (tlv = CHANNEL_TLV_VPORT_UPDATE_ACTIVATE;
740              tlv < CHANNEL_TLV_VPORT_UPDATE_MAX; tlv++) {
741                 if (!qed_vf_handle_vp_update_is_needed(p_hwfn, p_data, tlv))
742                         continue;
743
744                 p_resp = (struct pfvf_def_resp_tlv *)
745                          qed_iov_search_list_tlvs(p_hwfn, p_iov->pf2vf_reply,
746                                                   tlv);
747                 if (p_resp && p_resp->hdr.status)
748                         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
749                                    "TLV[%d] Configuration %s\n",
750                                    tlv,
751                                    (p_resp && p_resp->hdr.status) ? "succeeded"
752                                                                   : "failed");
753         }
754 }
755
756 int qed_vf_pf_vport_update(struct qed_hwfn *p_hwfn,
757                            struct qed_sp_vport_update_params *p_params)
758 {
759         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
760         struct vfpf_vport_update_tlv *req;
761         struct pfvf_def_resp_tlv *resp;
762         u8 update_rx, update_tx;
763         u32 resp_size = 0;
764         u16 size, tlv;
765         int rc;
766
767         resp = &p_iov->pf2vf_reply->default_resp;
768         resp_size = sizeof(*resp);
769
770         update_rx = p_params->update_vport_active_rx_flg;
771         update_tx = p_params->update_vport_active_tx_flg;
772
773         /* clear mailbox and prep header tlv */
774         qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_VPORT_UPDATE, sizeof(*req));
775
776         /* Prepare extended tlvs */
777         if (update_rx || update_tx) {
778                 struct vfpf_vport_update_activate_tlv *p_act_tlv;
779
780                 size = sizeof(struct vfpf_vport_update_activate_tlv);
781                 p_act_tlv = qed_add_tlv(p_hwfn, &p_iov->offset,
782                                         CHANNEL_TLV_VPORT_UPDATE_ACTIVATE,
783                                         size);
784                 resp_size += sizeof(struct pfvf_def_resp_tlv);
785
786                 if (update_rx) {
787                         p_act_tlv->update_rx = update_rx;
788                         p_act_tlv->active_rx = p_params->vport_active_rx_flg;
789                 }
790
791                 if (update_tx) {
792                         p_act_tlv->update_tx = update_tx;
793                         p_act_tlv->active_tx = p_params->vport_active_tx_flg;
794                 }
795         }
796
797         if (p_params->update_tx_switching_flg) {
798                 struct vfpf_vport_update_tx_switch_tlv *p_tx_switch_tlv;
799
800                 size = sizeof(struct vfpf_vport_update_tx_switch_tlv);
801                 tlv = CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH;
802                 p_tx_switch_tlv = qed_add_tlv(p_hwfn, &p_iov->offset,
803                                               tlv, size);
804                 resp_size += sizeof(struct pfvf_def_resp_tlv);
805
806                 p_tx_switch_tlv->tx_switching = p_params->tx_switching_flg;
807         }
808
809         if (p_params->update_approx_mcast_flg) {
810                 struct vfpf_vport_update_mcast_bin_tlv *p_mcast_tlv;
811
812                 size = sizeof(struct vfpf_vport_update_mcast_bin_tlv);
813                 p_mcast_tlv = qed_add_tlv(p_hwfn, &p_iov->offset,
814                                           CHANNEL_TLV_VPORT_UPDATE_MCAST, size);
815                 resp_size += sizeof(struct pfvf_def_resp_tlv);
816
817                 memcpy(p_mcast_tlv->bins, p_params->bins,
818                        sizeof(u32) * ETH_MULTICAST_MAC_BINS_IN_REGS);
819         }
820
821         update_rx = p_params->accept_flags.update_rx_mode_config;
822         update_tx = p_params->accept_flags.update_tx_mode_config;
823
824         if (update_rx || update_tx) {
825                 struct vfpf_vport_update_accept_param_tlv *p_accept_tlv;
826
827                 tlv = CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM;
828                 size = sizeof(struct vfpf_vport_update_accept_param_tlv);
829                 p_accept_tlv = qed_add_tlv(p_hwfn, &p_iov->offset, tlv, size);
830                 resp_size += sizeof(struct pfvf_def_resp_tlv);
831
832                 if (update_rx) {
833                         p_accept_tlv->update_rx_mode = update_rx;
834                         p_accept_tlv->rx_accept_filter =
835                             p_params->accept_flags.rx_accept_filter;
836                 }
837
838                 if (update_tx) {
839                         p_accept_tlv->update_tx_mode = update_tx;
840                         p_accept_tlv->tx_accept_filter =
841                             p_params->accept_flags.tx_accept_filter;
842                 }
843         }
844
845         if (p_params->rss_params) {
846                 struct qed_rss_params *rss_params = p_params->rss_params;
847                 struct vfpf_vport_update_rss_tlv *p_rss_tlv;
848
849                 size = sizeof(struct vfpf_vport_update_rss_tlv);
850                 p_rss_tlv = qed_add_tlv(p_hwfn,
851                                         &p_iov->offset,
852                                         CHANNEL_TLV_VPORT_UPDATE_RSS, size);
853                 resp_size += sizeof(struct pfvf_def_resp_tlv);
854
855                 if (rss_params->update_rss_config)
856                         p_rss_tlv->update_rss_flags |=
857                             VFPF_UPDATE_RSS_CONFIG_FLAG;
858                 if (rss_params->update_rss_capabilities)
859                         p_rss_tlv->update_rss_flags |=
860                             VFPF_UPDATE_RSS_CAPS_FLAG;
861                 if (rss_params->update_rss_ind_table)
862                         p_rss_tlv->update_rss_flags |=
863                             VFPF_UPDATE_RSS_IND_TABLE_FLAG;
864                 if (rss_params->update_rss_key)
865                         p_rss_tlv->update_rss_flags |= VFPF_UPDATE_RSS_KEY_FLAG;
866
867                 p_rss_tlv->rss_enable = rss_params->rss_enable;
868                 p_rss_tlv->rss_caps = rss_params->rss_caps;
869                 p_rss_tlv->rss_table_size_log = rss_params->rss_table_size_log;
870                 memcpy(p_rss_tlv->rss_ind_table, rss_params->rss_ind_table,
871                        sizeof(rss_params->rss_ind_table));
872                 memcpy(p_rss_tlv->rss_key, rss_params->rss_key,
873                        sizeof(rss_params->rss_key));
874         }
875
876         if (p_params->update_accept_any_vlan_flg) {
877                 struct vfpf_vport_update_accept_any_vlan_tlv *p_any_vlan_tlv;
878
879                 size = sizeof(struct vfpf_vport_update_accept_any_vlan_tlv);
880                 tlv = CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN;
881                 p_any_vlan_tlv = qed_add_tlv(p_hwfn, &p_iov->offset, tlv, size);
882
883                 resp_size += sizeof(struct pfvf_def_resp_tlv);
884                 p_any_vlan_tlv->accept_any_vlan = p_params->accept_any_vlan;
885                 p_any_vlan_tlv->update_accept_any_vlan_flg =
886                     p_params->update_accept_any_vlan_flg;
887         }
888
889         /* add list termination tlv */
890         qed_add_tlv(p_hwfn, &p_iov->offset,
891                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
892
893         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, resp_size);
894         if (rc)
895                 goto exit;
896
897         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
898                 rc = -EINVAL;
899                 goto exit;
900         }
901
902         qed_vf_handle_vp_update_tlvs_resp(p_hwfn, p_params);
903
904 exit:
905         qed_vf_pf_req_end(p_hwfn, rc);
906
907         return rc;
908 }
909
910 int qed_vf_pf_reset(struct qed_hwfn *p_hwfn)
911 {
912         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
913         struct pfvf_def_resp_tlv *resp;
914         struct vfpf_first_tlv *req;
915         int rc;
916
917         /* clear mailbox and prep first tlv */
918         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_CLOSE, sizeof(*req));
919
920         /* add list termination tlv */
921         qed_add_tlv(p_hwfn, &p_iov->offset,
922                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
923
924         resp = &p_iov->pf2vf_reply->default_resp;
925         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
926         if (rc)
927                 goto exit;
928
929         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
930                 rc = -EAGAIN;
931                 goto exit;
932         }
933
934         p_hwfn->b_int_enabled = 0;
935
936 exit:
937         qed_vf_pf_req_end(p_hwfn, rc);
938
939         return rc;
940 }
941
942 int qed_vf_pf_release(struct qed_hwfn *p_hwfn)
943 {
944         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
945         struct pfvf_def_resp_tlv *resp;
946         struct vfpf_first_tlv *req;
947         u32 size;
948         int rc;
949
950         /* clear mailbox and prep first tlv */
951         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_RELEASE, sizeof(*req));
952
953         /* add list termination tlv */
954         qed_add_tlv(p_hwfn, &p_iov->offset,
955                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
956
957         resp = &p_iov->pf2vf_reply->default_resp;
958         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
959
960         if (!rc && resp->hdr.status != PFVF_STATUS_SUCCESS)
961                 rc = -EAGAIN;
962
963         qed_vf_pf_req_end(p_hwfn, rc);
964
965         p_hwfn->b_int_enabled = 0;
966
967         if (p_iov->vf2pf_request)
968                 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
969                                   sizeof(union vfpf_tlvs),
970                                   p_iov->vf2pf_request,
971                                   p_iov->vf2pf_request_phys);
972         if (p_iov->pf2vf_reply)
973                 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
974                                   sizeof(union pfvf_tlvs),
975                                   p_iov->pf2vf_reply, p_iov->pf2vf_reply_phys);
976
977         if (p_iov->bulletin.p_virt) {
978                 size = sizeof(struct qed_bulletin_content);
979                 dma_free_coherent(&p_hwfn->cdev->pdev->dev,
980                                   size,
981                                   p_iov->bulletin.p_virt, p_iov->bulletin.phys);
982         }
983
984         kfree(p_hwfn->vf_iov_info);
985         p_hwfn->vf_iov_info = NULL;
986
987         return rc;
988 }
989
990 void qed_vf_pf_filter_mcast(struct qed_hwfn *p_hwfn,
991                             struct qed_filter_mcast *p_filter_cmd)
992 {
993         struct qed_sp_vport_update_params sp_params;
994         int i;
995
996         memset(&sp_params, 0, sizeof(sp_params));
997         sp_params.update_approx_mcast_flg = 1;
998
999         if (p_filter_cmd->opcode == QED_FILTER_ADD) {
1000                 for (i = 0; i < p_filter_cmd->num_mc_addrs; i++) {
1001                         u32 bit;
1002
1003                         bit = qed_mcast_bin_from_mac(p_filter_cmd->mac[i]);
1004                         sp_params.bins[bit / 32] |= 1 << (bit % 32);
1005                 }
1006         }
1007
1008         qed_vf_pf_vport_update(p_hwfn, &sp_params);
1009 }
1010
1011 int qed_vf_pf_filter_ucast(struct qed_hwfn *p_hwfn,
1012                            struct qed_filter_ucast *p_ucast)
1013 {
1014         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
1015         struct vfpf_ucast_filter_tlv *req;
1016         struct pfvf_def_resp_tlv *resp;
1017         int rc;
1018
1019         /* clear mailbox and prep first tlv */
1020         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_UCAST_FILTER, sizeof(*req));
1021         req->opcode = (u8) p_ucast->opcode;
1022         req->type = (u8) p_ucast->type;
1023         memcpy(req->mac, p_ucast->mac, ETH_ALEN);
1024         req->vlan = p_ucast->vlan;
1025
1026         /* add list termination tlv */
1027         qed_add_tlv(p_hwfn, &p_iov->offset,
1028                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
1029
1030         resp = &p_iov->pf2vf_reply->default_resp;
1031         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
1032         if (rc)
1033                 goto exit;
1034
1035         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
1036                 rc = -EAGAIN;
1037                 goto exit;
1038         }
1039
1040 exit:
1041         qed_vf_pf_req_end(p_hwfn, rc);
1042
1043         return rc;
1044 }
1045
1046 int qed_vf_pf_int_cleanup(struct qed_hwfn *p_hwfn)
1047 {
1048         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
1049         struct pfvf_def_resp_tlv *resp = &p_iov->pf2vf_reply->default_resp;
1050         int rc;
1051
1052         /* clear mailbox and prep first tlv */
1053         qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_INT_CLEANUP,
1054                        sizeof(struct vfpf_first_tlv));
1055
1056         /* add list termination tlv */
1057         qed_add_tlv(p_hwfn, &p_iov->offset,
1058                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
1059
1060         rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
1061         if (rc)
1062                 goto exit;
1063
1064         if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
1065                 rc = -EINVAL;
1066                 goto exit;
1067         }
1068
1069 exit:
1070         qed_vf_pf_req_end(p_hwfn, rc);
1071
1072         return rc;
1073 }
1074
1075 u16 qed_vf_get_igu_sb_id(struct qed_hwfn *p_hwfn, u16 sb_id)
1076 {
1077         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
1078
1079         if (!p_iov) {
1080                 DP_NOTICE(p_hwfn, "vf_sriov_info isn't initialized\n");
1081                 return 0;
1082         }
1083
1084         return p_iov->acquire_resp.resc.hw_sbs[sb_id].hw_sb_id;
1085 }
1086
1087 int qed_vf_read_bulletin(struct qed_hwfn *p_hwfn, u8 *p_change)
1088 {
1089         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
1090         struct qed_bulletin_content shadow;
1091         u32 crc, crc_size;
1092
1093         crc_size = sizeof(p_iov->bulletin.p_virt->crc);
1094         *p_change = 0;
1095
1096         /* Need to guarantee PF is not in the middle of writing it */
1097         memcpy(&shadow, p_iov->bulletin.p_virt, p_iov->bulletin.size);
1098
1099         /* If version did not update, no need to do anything */
1100         if (shadow.version == p_iov->bulletin_shadow.version)
1101                 return 0;
1102
1103         /* Verify the bulletin we see is valid */
1104         crc = crc32(0, (u8 *)&shadow + crc_size,
1105                     p_iov->bulletin.size - crc_size);
1106         if (crc != shadow.crc)
1107                 return -EAGAIN;
1108
1109         /* Set the shadow bulletin and process it */
1110         memcpy(&p_iov->bulletin_shadow, &shadow, p_iov->bulletin.size);
1111
1112         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
1113                    "Read a bulletin update %08x\n", shadow.version);
1114
1115         *p_change = 1;
1116
1117         return 0;
1118 }
1119
1120 void __qed_vf_get_link_params(struct qed_hwfn *p_hwfn,
1121                               struct qed_mcp_link_params *p_params,
1122                               struct qed_bulletin_content *p_bulletin)
1123 {
1124         memset(p_params, 0, sizeof(*p_params));
1125
1126         p_params->speed.autoneg = p_bulletin->req_autoneg;
1127         p_params->speed.advertised_speeds = p_bulletin->req_adv_speed;
1128         p_params->speed.forced_speed = p_bulletin->req_forced_speed;
1129         p_params->pause.autoneg = p_bulletin->req_autoneg_pause;
1130         p_params->pause.forced_rx = p_bulletin->req_forced_rx;
1131         p_params->pause.forced_tx = p_bulletin->req_forced_tx;
1132         p_params->loopback_mode = p_bulletin->req_loopback;
1133 }
1134
1135 void qed_vf_get_link_params(struct qed_hwfn *p_hwfn,
1136                             struct qed_mcp_link_params *params)
1137 {
1138         __qed_vf_get_link_params(p_hwfn, params,
1139                                  &(p_hwfn->vf_iov_info->bulletin_shadow));
1140 }
1141
1142 void __qed_vf_get_link_state(struct qed_hwfn *p_hwfn,
1143                              struct qed_mcp_link_state *p_link,
1144                              struct qed_bulletin_content *p_bulletin)
1145 {
1146         memset(p_link, 0, sizeof(*p_link));
1147
1148         p_link->link_up = p_bulletin->link_up;
1149         p_link->speed = p_bulletin->speed;
1150         p_link->full_duplex = p_bulletin->full_duplex;
1151         p_link->an = p_bulletin->autoneg;
1152         p_link->an_complete = p_bulletin->autoneg_complete;
1153         p_link->parallel_detection = p_bulletin->parallel_detection;
1154         p_link->pfc_enabled = p_bulletin->pfc_enabled;
1155         p_link->partner_adv_speed = p_bulletin->partner_adv_speed;
1156         p_link->partner_tx_flow_ctrl_en = p_bulletin->partner_tx_flow_ctrl_en;
1157         p_link->partner_rx_flow_ctrl_en = p_bulletin->partner_rx_flow_ctrl_en;
1158         p_link->partner_adv_pause = p_bulletin->partner_adv_pause;
1159         p_link->sfp_tx_fault = p_bulletin->sfp_tx_fault;
1160 }
1161
1162 void qed_vf_get_link_state(struct qed_hwfn *p_hwfn,
1163                            struct qed_mcp_link_state *link)
1164 {
1165         __qed_vf_get_link_state(p_hwfn, link,
1166                                 &(p_hwfn->vf_iov_info->bulletin_shadow));
1167 }
1168
1169 void __qed_vf_get_link_caps(struct qed_hwfn *p_hwfn,
1170                             struct qed_mcp_link_capabilities *p_link_caps,
1171                             struct qed_bulletin_content *p_bulletin)
1172 {
1173         memset(p_link_caps, 0, sizeof(*p_link_caps));
1174         p_link_caps->speed_capabilities = p_bulletin->capability_speed;
1175 }
1176
1177 void qed_vf_get_link_caps(struct qed_hwfn *p_hwfn,
1178                           struct qed_mcp_link_capabilities *p_link_caps)
1179 {
1180         __qed_vf_get_link_caps(p_hwfn, p_link_caps,
1181                                &(p_hwfn->vf_iov_info->bulletin_shadow));
1182 }
1183
1184 void qed_vf_get_num_rxqs(struct qed_hwfn *p_hwfn, u8 *num_rxqs)
1185 {
1186         *num_rxqs = p_hwfn->vf_iov_info->acquire_resp.resc.num_rxqs;
1187 }
1188
1189 void qed_vf_get_port_mac(struct qed_hwfn *p_hwfn, u8 *port_mac)
1190 {
1191         memcpy(port_mac,
1192                p_hwfn->vf_iov_info->acquire_resp.pfdev_info.port_mac, ETH_ALEN);
1193 }
1194
1195 void qed_vf_get_num_vlan_filters(struct qed_hwfn *p_hwfn, u8 *num_vlan_filters)
1196 {
1197         struct qed_vf_iov *p_vf;
1198
1199         p_vf = p_hwfn->vf_iov_info;
1200         *num_vlan_filters = p_vf->acquire_resp.resc.num_vlan_filters;
1201 }
1202
1203 bool qed_vf_check_mac(struct qed_hwfn *p_hwfn, u8 *mac)
1204 {
1205         struct qed_bulletin_content *bulletin;
1206
1207         bulletin = &p_hwfn->vf_iov_info->bulletin_shadow;
1208         if (!(bulletin->valid_bitmap & (1 << MAC_ADDR_FORCED)))
1209                 return true;
1210
1211         /* Forbid VF from changing a MAC enforced by PF */
1212         if (ether_addr_equal(bulletin->mac, mac))
1213                 return false;
1214
1215         return false;
1216 }
1217
1218 static bool qed_vf_bulletin_get_forced_mac(struct qed_hwfn *hwfn,
1219                                            u8 *dst_mac, u8 *p_is_forced)
1220 {
1221         struct qed_bulletin_content *bulletin;
1222
1223         bulletin = &hwfn->vf_iov_info->bulletin_shadow;
1224
1225         if (bulletin->valid_bitmap & (1 << MAC_ADDR_FORCED)) {
1226                 if (p_is_forced)
1227                         *p_is_forced = 1;
1228         } else if (bulletin->valid_bitmap & (1 << VFPF_BULLETIN_MAC_ADDR)) {
1229                 if (p_is_forced)
1230                         *p_is_forced = 0;
1231         } else {
1232                 return false;
1233         }
1234
1235         ether_addr_copy(dst_mac, bulletin->mac);
1236
1237         return true;
1238 }
1239
1240 void qed_vf_get_fw_version(struct qed_hwfn *p_hwfn,
1241                            u16 *fw_major, u16 *fw_minor,
1242                            u16 *fw_rev, u16 *fw_eng)
1243 {
1244         struct pf_vf_pfdev_info *info;
1245
1246         info = &p_hwfn->vf_iov_info->acquire_resp.pfdev_info;
1247
1248         *fw_major = info->fw_major;
1249         *fw_minor = info->fw_minor;
1250         *fw_rev = info->fw_rev;
1251         *fw_eng = info->fw_eng;
1252 }
1253
1254 static void qed_handle_bulletin_change(struct qed_hwfn *hwfn)
1255 {
1256         struct qed_eth_cb_ops *ops = hwfn->cdev->protocol_ops.eth;
1257         u8 mac[ETH_ALEN], is_mac_exist, is_mac_forced;
1258         void *cookie = hwfn->cdev->ops_cookie;
1259
1260         is_mac_exist = qed_vf_bulletin_get_forced_mac(hwfn, mac,
1261                                                       &is_mac_forced);
1262         if (is_mac_exist && is_mac_forced && cookie)
1263                 ops->force_mac(cookie, mac);
1264
1265         /* Always update link configuration according to bulletin */
1266         qed_link_update(hwfn);
1267 }
1268
1269 void qed_iov_vf_task(struct work_struct *work)
1270 {
1271         struct qed_hwfn *hwfn = container_of(work, struct qed_hwfn,
1272                                              iov_task.work);
1273         u8 change = 0;
1274
1275         if (test_and_clear_bit(QED_IOV_WQ_STOP_WQ_FLAG, &hwfn->iov_task_flags))
1276                 return;
1277
1278         /* Handle bulletin board changes */
1279         qed_vf_read_bulletin(hwfn, &change);
1280         if (change)
1281                 qed_handle_bulletin_change(hwfn);
1282
1283         /* As VF is polling bulletin board, need to constantly re-schedule */
1284         queue_delayed_work(hwfn->iov_wq, &hwfn->iov_task, HZ);
1285 }