qed: Introduce VFs
[linux-2.6-microblaze.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 "qed.h"
10 #include "qed_sriov.h"
11 #include "qed_vf.h"
12
13 static void *qed_vf_pf_prep(struct qed_hwfn *p_hwfn, u16 type, u16 length)
14 {
15         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
16         void *p_tlv;
17
18         /* This lock is released when we receive PF's response
19          * in qed_send_msg2pf().
20          * So, qed_vf_pf_prep() and qed_send_msg2pf()
21          * must come in sequence.
22          */
23         mutex_lock(&(p_iov->mutex));
24
25         DP_VERBOSE(p_hwfn,
26                    QED_MSG_IOV,
27                    "preparing to send 0x%04x tlv over vf pf channel\n",
28                    type);
29
30         /* Reset Requst offset */
31         p_iov->offset = (u8 *)p_iov->vf2pf_request;
32
33         /* Clear mailbox - both request and reply */
34         memset(p_iov->vf2pf_request, 0, sizeof(union vfpf_tlvs));
35         memset(p_iov->pf2vf_reply, 0, sizeof(union pfvf_tlvs));
36
37         /* Init type and length */
38         p_tlv = qed_add_tlv(p_hwfn, &p_iov->offset, type, length);
39
40         /* Init first tlv header */
41         ((struct vfpf_first_tlv *)p_tlv)->reply_address =
42             (u64)p_iov->pf2vf_reply_phys;
43
44         return p_tlv;
45 }
46
47 static int qed_send_msg2pf(struct qed_hwfn *p_hwfn, u8 *done, u32 resp_size)
48 {
49         union vfpf_tlvs *p_req = p_hwfn->vf_iov_info->vf2pf_request;
50         struct ustorm_trigger_vf_zone trigger;
51         struct ustorm_vf_zone *zone_data;
52         int rc = 0, time = 100;
53
54         zone_data = (struct ustorm_vf_zone *)PXP_VF_BAR0_START_USDM_ZONE_B;
55
56         /* output tlvs list */
57         qed_dp_tlv_list(p_hwfn, p_req);
58
59         /* need to add the END TLV to the message size */
60         resp_size += sizeof(struct channel_list_end_tlv);
61
62         /* Send TLVs over HW channel */
63         memset(&trigger, 0, sizeof(struct ustorm_trigger_vf_zone));
64         trigger.vf_pf_msg_valid = 1;
65
66         DP_VERBOSE(p_hwfn,
67                    QED_MSG_IOV,
68                    "VF -> PF [%02x] message: [%08x, %08x] --> %p, %08x --> %p\n",
69                    GET_FIELD(p_hwfn->hw_info.concrete_fid,
70                              PXP_CONCRETE_FID_PFID),
71                    upper_32_bits(p_hwfn->vf_iov_info->vf2pf_request_phys),
72                    lower_32_bits(p_hwfn->vf_iov_info->vf2pf_request_phys),
73                    &zone_data->non_trigger.vf_pf_msg_addr,
74                    *((u32 *)&trigger), &zone_data->trigger);
75
76         REG_WR(p_hwfn,
77                (uintptr_t)&zone_data->non_trigger.vf_pf_msg_addr.lo,
78                lower_32_bits(p_hwfn->vf_iov_info->vf2pf_request_phys));
79
80         REG_WR(p_hwfn,
81                (uintptr_t)&zone_data->non_trigger.vf_pf_msg_addr.hi,
82                upper_32_bits(p_hwfn->vf_iov_info->vf2pf_request_phys));
83
84         /* The message data must be written first, to prevent trigger before
85          * data is written.
86          */
87         wmb();
88
89         REG_WR(p_hwfn, (uintptr_t)&zone_data->trigger, *((u32 *)&trigger));
90
91         /* When PF would be done with the response, it would write back to the
92          * `done' address. Poll until then.
93          */
94         while ((!*done) && time) {
95                 msleep(25);
96                 time--;
97         }
98
99         if (!*done) {
100                 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
101                            "VF <-- PF Timeout [Type %d]\n",
102                            p_req->first_tlv.tl.type);
103                 rc = -EBUSY;
104                 goto exit;
105         } else {
106                 DP_VERBOSE(p_hwfn, QED_MSG_IOV,
107                            "PF response: %d [Type %d]\n",
108                            *done, p_req->first_tlv.tl.type);
109         }
110
111 exit:
112         mutex_unlock(&(p_hwfn->vf_iov_info->mutex));
113
114         return rc;
115 }
116
117 #define VF_ACQUIRE_THRESH 3
118 #define VF_ACQUIRE_MAC_FILTERS 1
119
120 static int qed_vf_pf_acquire(struct qed_hwfn *p_hwfn)
121 {
122         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
123         struct pfvf_acquire_resp_tlv *resp = &p_iov->pf2vf_reply->acquire_resp;
124         struct pf_vf_pfdev_info *pfdev_info = &resp->pfdev_info;
125         u8 rx_count = 1, tx_count = 1, num_sbs = 1;
126         u8 num_mac = VF_ACQUIRE_MAC_FILTERS;
127         bool resources_acquired = false;
128         struct vfpf_acquire_tlv *req;
129         int rc = 0, attempts = 0;
130
131         /* clear mailbox and prep first tlv */
132         req = qed_vf_pf_prep(p_hwfn, CHANNEL_TLV_ACQUIRE, sizeof(*req));
133
134         /* starting filling the request */
135         req->vfdev_info.opaque_fid = p_hwfn->hw_info.opaque_fid;
136
137         req->resc_request.num_rxqs = rx_count;
138         req->resc_request.num_txqs = tx_count;
139         req->resc_request.num_sbs = num_sbs;
140         req->resc_request.num_mac_filters = num_mac;
141         req->resc_request.num_vlan_filters = QED_ETH_VF_NUM_VLAN_FILTERS;
142
143         req->vfdev_info.os_type = VFPF_ACQUIRE_OS_LINUX;
144         req->vfdev_info.fw_major = FW_MAJOR_VERSION;
145         req->vfdev_info.fw_minor = FW_MINOR_VERSION;
146         req->vfdev_info.fw_revision = FW_REVISION_VERSION;
147         req->vfdev_info.fw_engineering = FW_ENGINEERING_VERSION;
148
149         /* Fill capability field with any non-deprecated config we support */
150         req->vfdev_info.capabilities |= VFPF_ACQUIRE_CAP_100G;
151
152         /* pf 2 vf bulletin board address */
153         req->bulletin_addr = p_iov->bulletin.phys;
154         req->bulletin_size = p_iov->bulletin.size;
155
156         /* add list termination tlv */
157         qed_add_tlv(p_hwfn, &p_iov->offset,
158                     CHANNEL_TLV_LIST_END, sizeof(struct channel_list_end_tlv));
159
160         while (!resources_acquired) {
161                 DP_VERBOSE(p_hwfn,
162                            QED_MSG_IOV, "attempting to acquire resources\n");
163
164                 /* send acquire request */
165                 rc = qed_send_msg2pf(p_hwfn, &resp->hdr.status, sizeof(*resp));
166                 if (rc)
167                         return rc;
168
169                 /* copy acquire response from buffer to p_hwfn */
170                 memcpy(&p_iov->acquire_resp, resp, sizeof(p_iov->acquire_resp));
171
172                 attempts++;
173
174                 if (resp->hdr.status == PFVF_STATUS_SUCCESS) {
175                         /* PF agrees to allocate our resources */
176                         if (!(resp->pfdev_info.capabilities &
177                               PFVF_ACQUIRE_CAP_POST_FW_OVERRIDE)) {
178                                 DP_INFO(p_hwfn,
179                                         "PF is using old incompatible driver; Either downgrade driver or request provider to update hypervisor version\n");
180                                 return -EINVAL;
181                         }
182                         DP_VERBOSE(p_hwfn, QED_MSG_IOV, "resources acquired\n");
183                         resources_acquired = true;
184                 } else if (resp->hdr.status == PFVF_STATUS_NO_RESOURCE &&
185                            attempts < VF_ACQUIRE_THRESH) {
186                         DP_VERBOSE(p_hwfn,
187                                    QED_MSG_IOV,
188                                    "PF unwilling to fullfill resource request. Try PF recommended amount\n");
189
190                         /* humble our request */
191                         req->resc_request.num_txqs = resp->resc.num_txqs;
192                         req->resc_request.num_rxqs = resp->resc.num_rxqs;
193                         req->resc_request.num_sbs = resp->resc.num_sbs;
194                         req->resc_request.num_mac_filters =
195                             resp->resc.num_mac_filters;
196                         req->resc_request.num_vlan_filters =
197                             resp->resc.num_vlan_filters;
198
199                         /* Clear response buffer */
200                         memset(p_iov->pf2vf_reply, 0, sizeof(union pfvf_tlvs));
201                 } else {
202                         DP_ERR(p_hwfn,
203                                "PF returned error %d to VF acquisition request\n",
204                                resp->hdr.status);
205                         return -EAGAIN;
206                 }
207         }
208
209         /* Update bulletin board size with response from PF */
210         p_iov->bulletin.size = resp->bulletin_size;
211
212         /* get HW info */
213         p_hwfn->cdev->type = resp->pfdev_info.dev_type;
214         p_hwfn->cdev->chip_rev = resp->pfdev_info.chip_rev;
215
216         p_hwfn->cdev->chip_num = pfdev_info->chip_num & 0xffff;
217
218         /* Learn of the possibility of CMT */
219         if (IS_LEAD_HWFN(p_hwfn)) {
220                 if (resp->pfdev_info.capabilities & PFVF_ACQUIRE_CAP_100G) {
221                         DP_NOTICE(p_hwfn, "100g VF\n");
222                         p_hwfn->cdev->num_hwfns = 2;
223                 }
224         }
225
226         return 0;
227 }
228
229 int qed_vf_hw_prepare(struct qed_hwfn *p_hwfn)
230 {
231         struct qed_vf_iov *p_iov;
232         u32 reg;
233
234         /* Set number of hwfns - might be overriden once leading hwfn learns
235          * actual configuration from PF.
236          */
237         if (IS_LEAD_HWFN(p_hwfn))
238                 p_hwfn->cdev->num_hwfns = 1;
239
240         /* Set the doorbell bar. Assumption: regview is set */
241         p_hwfn->doorbells = (u8 __iomem *)p_hwfn->regview +
242                                           PXP_VF_BAR0_START_DQ;
243
244         reg = PXP_VF_BAR0_ME_OPAQUE_ADDRESS;
245         p_hwfn->hw_info.opaque_fid = (u16)REG_RD(p_hwfn, reg);
246
247         reg = PXP_VF_BAR0_ME_CONCRETE_ADDRESS;
248         p_hwfn->hw_info.concrete_fid = REG_RD(p_hwfn, reg);
249
250         /* Allocate vf sriov info */
251         p_iov = kzalloc(sizeof(*p_iov), GFP_KERNEL);
252         if (!p_iov) {
253                 DP_NOTICE(p_hwfn, "Failed to allocate `struct qed_sriov'\n");
254                 return -ENOMEM;
255         }
256
257         /* Allocate vf2pf msg */
258         p_iov->vf2pf_request = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
259                                                   sizeof(union vfpf_tlvs),
260                                                   &p_iov->vf2pf_request_phys,
261                                                   GFP_KERNEL);
262         if (!p_iov->vf2pf_request) {
263                 DP_NOTICE(p_hwfn,
264                           "Failed to allocate `vf2pf_request' DMA memory\n");
265                 goto free_p_iov;
266         }
267
268         p_iov->pf2vf_reply = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
269                                                 sizeof(union pfvf_tlvs),
270                                                 &p_iov->pf2vf_reply_phys,
271                                                 GFP_KERNEL);
272         if (!p_iov->pf2vf_reply) {
273                 DP_NOTICE(p_hwfn,
274                           "Failed to allocate `pf2vf_reply' DMA memory\n");
275                 goto free_vf2pf_request;
276         }
277
278         DP_VERBOSE(p_hwfn,
279                    QED_MSG_IOV,
280                    "VF's Request mailbox [%p virt 0x%llx phys], Response mailbox [%p virt 0x%llx phys]\n",
281                    p_iov->vf2pf_request,
282                    (u64) p_iov->vf2pf_request_phys,
283                    p_iov->pf2vf_reply, (u64)p_iov->pf2vf_reply_phys);
284
285         /* Allocate Bulletin board */
286         p_iov->bulletin.size = sizeof(struct qed_bulletin_content);
287         p_iov->bulletin.p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
288                                                     p_iov->bulletin.size,
289                                                     &p_iov->bulletin.phys,
290                                                     GFP_KERNEL);
291         DP_VERBOSE(p_hwfn, QED_MSG_IOV,
292                    "VF's bulletin Board [%p virt 0x%llx phys 0x%08x bytes]\n",
293                    p_iov->bulletin.p_virt,
294                    (u64)p_iov->bulletin.phys, p_iov->bulletin.size);
295
296         mutex_init(&p_iov->mutex);
297
298         p_hwfn->vf_iov_info = p_iov;
299
300         p_hwfn->hw_info.personality = QED_PCI_ETH;
301
302         return qed_vf_pf_acquire(p_hwfn);
303
304 free_vf2pf_request:
305         dma_free_coherent(&p_hwfn->cdev->pdev->dev,
306                           sizeof(union vfpf_tlvs),
307                           p_iov->vf2pf_request, p_iov->vf2pf_request_phys);
308 free_p_iov:
309         kfree(p_iov);
310
311         return -ENOMEM;
312 }
313
314 u16 qed_vf_get_igu_sb_id(struct qed_hwfn *p_hwfn, u16 sb_id)
315 {
316         struct qed_vf_iov *p_iov = p_hwfn->vf_iov_info;
317
318         if (!p_iov) {
319                 DP_NOTICE(p_hwfn, "vf_sriov_info isn't initialized\n");
320                 return 0;
321         }
322
323         return p_iov->acquire_resp.resc.hw_sbs[sb_id].hw_sb_id;
324 }
325
326 void qed_vf_get_num_rxqs(struct qed_hwfn *p_hwfn, u8 *num_rxqs)
327 {
328         *num_rxqs = p_hwfn->vf_iov_info->acquire_resp.resc.num_rxqs;
329 }
330
331 void qed_vf_get_port_mac(struct qed_hwfn *p_hwfn, u8 *port_mac)
332 {
333         memcpy(port_mac,
334                p_hwfn->vf_iov_info->acquire_resp.pfdev_info.port_mac, ETH_ALEN);
335 }
336
337 void qed_vf_get_num_vlan_filters(struct qed_hwfn *p_hwfn, u8 *num_vlan_filters)
338 {
339         struct qed_vf_iov *p_vf;
340
341         p_vf = p_hwfn->vf_iov_info;
342         *num_vlan_filters = p_vf->acquire_resp.resc.num_vlan_filters;
343 }
344
345 void qed_vf_get_fw_version(struct qed_hwfn *p_hwfn,
346                            u16 *fw_major, u16 *fw_minor,
347                            u16 *fw_rev, u16 *fw_eng)
348 {
349         struct pf_vf_pfdev_info *info;
350
351         info = &p_hwfn->vf_iov_info->acquire_resp.pfdev_info;
352
353         *fw_major = info->fw_major;
354         *fw_minor = info->fw_minor;
355         *fw_rev = info->fw_rev;
356         *fw_eng = info->fw_eng;
357 }