bnx2x: Support of PF driver of a VF acquire request
[linux-2.6-block.git] / drivers / net / ethernet / broadcom / bnx2x / bnx2x_vfpf.c
CommitLineData
be1f1ffa
AE
1/* bnx2x_vfpf.c: Broadcom Everest network driver.
2 *
3 * Copyright 2009-2012 Broadcom Corporation
4 *
5 * Unless you and Broadcom execute a separate written software license
6 * agreement governing use of this software, this software is licensed to you
7 * under the terms of the GNU General Public License version 2, available
8 * at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html (the "GPL").
9 *
10 * Notwithstanding the above, under no circumstances may you combine this
11 * software in any way with any other Broadcom software provided under a
12 * license other than the GPL, without Broadcom's express prior written
13 * consent.
14 *
15 * Maintained by: Eilon Greenstein <eilong@broadcom.com>
16 * Written by: Shmulik Ravid <shmulikr@broadcom.com>
17 * Ariel Elior <ariele@broadcom.com>
18 */
19
20#include "bnx2x.h"
21#include "bnx2x_sriov.h"
22
23/* place a given tlv on the tlv buffer at a given offset */
24void bnx2x_add_tlv(struct bnx2x *bp, void *tlvs_list, u16 offset, u16 type,
25 u16 length)
26{
27 struct channel_tlv *tl =
28 (struct channel_tlv *)(tlvs_list + offset);
29
30 tl->type = type;
31 tl->length = length;
32}
33
34/* Clear the mailbox and init the header of the first tlv */
35void bnx2x_vfpf_prep(struct bnx2x *bp, struct vfpf_first_tlv *first_tlv,
36 u16 type, u16 length)
37{
38 DP(BNX2X_MSG_IOV, "preparing to send %d tlv over vf pf channel\n",
39 type);
40
41 /* Clear mailbox */
42 memset(bp->vf2pf_mbox, 0, sizeof(struct bnx2x_vf_mbx_msg));
43
44 /* init type and length */
45 bnx2x_add_tlv(bp, &first_tlv->tl, 0, type, length);
46
47 /* init first tlv header */
48 first_tlv->resp_msg_offset = sizeof(bp->vf2pf_mbox->req);
49}
50
51/* list the types and lengths of the tlvs on the buffer */
52void bnx2x_dp_tlv_list(struct bnx2x *bp, void *tlvs_list)
53{
54 int i = 1;
55 struct channel_tlv *tlv = (struct channel_tlv *)tlvs_list;
56
57 while (tlv->type != CHANNEL_TLV_LIST_END) {
58 /* output tlv */
59 DP(BNX2X_MSG_IOV, "TLV number %d: type %d, length %d\n", i,
60 tlv->type, tlv->length);
61
62 /* advance to next tlv */
63 tlvs_list += tlv->length;
64
65 /* cast general tlv list pointer to channel tlv header*/
66 tlv = (struct channel_tlv *)tlvs_list;
67
68 i++;
69
70 /* break condition for this loop */
71 if (i > MAX_TLVS_IN_LIST) {
72 WARN(true, "corrupt tlvs");
73 return;
74 }
75 }
76
77 /* output last tlv */
78 DP(BNX2X_MSG_IOV, "TLV number %d: type %d, length %d\n", i,
79 tlv->type, tlv->length);
80}
b56e9670 81
fd1fc79d
AE
82/* test whether we support a tlv type */
83bool bnx2x_tlv_supported(u16 tlvtype)
84{
85 return CHANNEL_TLV_NONE < tlvtype && tlvtype < CHANNEL_TLV_MAX;
86}
87
88static inline int bnx2x_pfvf_status_codes(int rc)
89{
90 switch (rc) {
91 case 0:
92 return PFVF_STATUS_SUCCESS;
93 case -ENOMEM:
94 return PFVF_STATUS_NO_RESOURCE;
95 default:
96 return PFVF_STATUS_FAILURE;
97 }
98}
99
b56e9670
AE
100/* General service functions */
101static void storm_memset_vf_mbx_ack(struct bnx2x *bp, u16 abs_fid)
102{
103 u32 addr = BAR_CSTRORM_INTMEM +
104 CSTORM_VF_PF_CHANNEL_STATE_OFFSET(abs_fid);
105
106 REG_WR8(bp, addr, VF_PF_CHANNEL_STATE_READY);
107}
108
109static void storm_memset_vf_mbx_valid(struct bnx2x *bp, u16 abs_fid)
110{
111 u32 addr = BAR_CSTRORM_INTMEM +
112 CSTORM_VF_PF_CHANNEL_VALID_OFFSET(abs_fid);
113
114 REG_WR8(bp, addr, 1);
115}
116
117static inline void bnx2x_set_vf_mbxs_valid(struct bnx2x *bp)
118{
119 int i;
120
121 for_each_vf(bp, i)
122 storm_memset_vf_mbx_valid(bp, bnx2x_vf(bp, i, abs_vfid));
123}
124
125/* enable vf_pf mailbox (aka vf-pf-chanell) */
126void bnx2x_vf_enable_mbx(struct bnx2x *bp, u8 abs_vfid)
127{
128 bnx2x_vf_flr_clnup_epilog(bp, abs_vfid);
129
130 /* enable the mailbox in the FW */
131 storm_memset_vf_mbx_ack(bp, abs_vfid);
132 storm_memset_vf_mbx_valid(bp, abs_vfid);
133
134 /* enable the VF access to the mailbox */
135 bnx2x_vf_enable_access(bp, abs_vfid);
136}
fd1fc79d
AE
137
138/* this works only on !E1h */
139static int bnx2x_copy32_vf_dmae(struct bnx2x *bp, u8 from_vf,
140 dma_addr_t pf_addr, u8 vfid, u32 vf_addr_hi,
141 u32 vf_addr_lo, u32 len32)
142{
143 struct dmae_command dmae;
144
145 if (CHIP_IS_E1x(bp)) {
146 BNX2X_ERR("Chip revision does not support VFs\n");
147 return DMAE_NOT_RDY;
148 }
149
150 if (!bp->dmae_ready) {
151 BNX2X_ERR("DMAE is not ready, can not copy\n");
152 return DMAE_NOT_RDY;
153 }
154
155 /* set opcode and fixed command fields */
156 bnx2x_prep_dmae_with_comp(bp, &dmae, DMAE_SRC_PCI, DMAE_DST_PCI);
157
158 if (from_vf) {
159 dmae.opcode_iov = (vfid << DMAE_COMMAND_SRC_VFID_SHIFT) |
160 (DMAE_SRC_VF << DMAE_COMMAND_SRC_VFPF_SHIFT) |
161 (DMAE_DST_PF << DMAE_COMMAND_DST_VFPF_SHIFT);
162
163 dmae.opcode |= (DMAE_C_DST << DMAE_COMMAND_C_FUNC_SHIFT);
164
165 dmae.src_addr_lo = vf_addr_lo;
166 dmae.src_addr_hi = vf_addr_hi;
167 dmae.dst_addr_lo = U64_LO(pf_addr);
168 dmae.dst_addr_hi = U64_HI(pf_addr);
169 } else {
170 dmae.opcode_iov = (vfid << DMAE_COMMAND_DST_VFID_SHIFT) |
171 (DMAE_DST_VF << DMAE_COMMAND_DST_VFPF_SHIFT) |
172 (DMAE_SRC_PF << DMAE_COMMAND_SRC_VFPF_SHIFT);
173
174 dmae.opcode |= (DMAE_C_SRC << DMAE_COMMAND_C_FUNC_SHIFT);
175
176 dmae.src_addr_lo = U64_LO(pf_addr);
177 dmae.src_addr_hi = U64_HI(pf_addr);
178 dmae.dst_addr_lo = vf_addr_lo;
179 dmae.dst_addr_hi = vf_addr_hi;
180 }
181 dmae.len = len32;
182 bnx2x_dp_dmae(bp, &dmae, BNX2X_MSG_DMAE);
183
184 /* issue the command and wait for completion */
185 return bnx2x_issue_dmae_with_comp(bp, &dmae);
186}
187
8ca5e17e
AE
188static void bnx2x_vf_mbx_resp(struct bnx2x *bp, struct bnx2x_virtf *vf)
189{
190 struct bnx2x_vf_mbx *mbx = BP_VF_MBX(bp, vf->index);
191 u64 vf_addr;
192 dma_addr_t pf_addr;
193 u16 length, type;
194 int rc;
195 struct pfvf_general_resp_tlv *resp = &mbx->msg->resp.general_resp;
196
197 /* prepare response */
198 type = mbx->first_tlv.tl.type;
199 length = type == CHANNEL_TLV_ACQUIRE ?
200 sizeof(struct pfvf_acquire_resp_tlv) :
201 sizeof(struct pfvf_general_resp_tlv);
202 bnx2x_add_tlv(bp, resp, 0, type, length);
203 resp->hdr.status = bnx2x_pfvf_status_codes(vf->op_rc);
204 bnx2x_add_tlv(bp, resp, length, CHANNEL_TLV_LIST_END,
205 sizeof(struct channel_list_end_tlv));
206 bnx2x_dp_tlv_list(bp, resp);
207 DP(BNX2X_MSG_IOV, "mailbox vf address hi 0x%x, lo 0x%x, offset 0x%x\n",
208 mbx->vf_addr_hi, mbx->vf_addr_lo, mbx->first_tlv.resp_msg_offset);
209
210 /* send response */
211 vf_addr = HILO_U64(mbx->vf_addr_hi, mbx->vf_addr_lo) +
212 mbx->first_tlv.resp_msg_offset;
213 pf_addr = mbx->msg_mapping +
214 offsetof(struct bnx2x_vf_mbx_msg, resp);
215
216 /* copy the response body, if there is one, before the header, as the vf
217 * is sensitive to the header being written
218 */
219 if (resp->hdr.tl.length > sizeof(u64)) {
220 length = resp->hdr.tl.length - sizeof(u64);
221 vf_addr += sizeof(u64);
222 pf_addr += sizeof(u64);
223 rc = bnx2x_copy32_vf_dmae(bp, false, pf_addr, vf->abs_vfid,
224 U64_HI(vf_addr),
225 U64_LO(vf_addr),
226 length/4);
227 if (rc) {
228 BNX2X_ERR("Failed to copy response body to VF %d\n",
229 vf->abs_vfid);
230 return;
231 }
232 vf_addr -= sizeof(u64);
233 pf_addr -= sizeof(u64);
234 }
235
236 /* ack the FW */
237 storm_memset_vf_mbx_ack(bp, vf->abs_vfid);
238 mmiowb();
239
240 /* initiate dmae to send the response */
241 mbx->flags &= ~VF_MSG_INPROCESS;
242
243 /* copy the response header including status-done field,
244 * must be last dmae, must be after FW is acked
245 */
246 rc = bnx2x_copy32_vf_dmae(bp, false, pf_addr, vf->abs_vfid,
247 U64_HI(vf_addr),
248 U64_LO(vf_addr),
249 sizeof(u64)/4);
250
251 /* unlock channel mutex */
252 bnx2x_unlock_vf_pf_channel(bp, vf, mbx->first_tlv.tl.type);
253
254 if (rc) {
255 BNX2X_ERR("Failed to copy response status to VF %d\n",
256 vf->abs_vfid);
257 }
258 return;
259}
260
261static void bnx2x_vf_mbx_acquire_resp(struct bnx2x *bp, struct bnx2x_virtf *vf,
262 struct bnx2x_vf_mbx *mbx, int vfop_status)
263{
264 int i;
265 struct pfvf_acquire_resp_tlv *resp = &mbx->msg->resp.acquire_resp;
266 struct pf_vf_resc *resc = &resp->resc;
267 u8 status = bnx2x_pfvf_status_codes(vfop_status);
268
269 memset(resp, 0, sizeof(*resp));
270
271 /* fill in pfdev info */
272 resp->pfdev_info.chip_num = bp->common.chip_id;
273 resp->pfdev_info.db_size = (1 << BNX2X_DB_SHIFT);
274 resp->pfdev_info.indices_per_sb = HC_SB_MAX_INDICES_E2;
275 resp->pfdev_info.pf_cap = (PFVF_CAP_RSS |
276 /* PFVF_CAP_DHC |*/ PFVF_CAP_TPA);
277 bnx2x_fill_fw_str(bp, resp->pfdev_info.fw_ver,
278 sizeof(resp->pfdev_info.fw_ver));
279
280 if (status == PFVF_STATUS_NO_RESOURCE ||
281 status == PFVF_STATUS_SUCCESS) {
282 /* set resources numbers, if status equals NO_RESOURCE these
283 * are max possible numbers
284 */
285 resc->num_rxqs = vf_rxq_count(vf) ? :
286 bnx2x_vf_max_queue_cnt(bp, vf);
287 resc->num_txqs = vf_txq_count(vf) ? :
288 bnx2x_vf_max_queue_cnt(bp, vf);
289 resc->num_sbs = vf_sb_count(vf);
290 resc->num_mac_filters = vf_mac_rules_cnt(vf);
291 resc->num_vlan_filters = vf_vlan_rules_cnt(vf);
292 resc->num_mc_filters = 0;
293
294 if (status == PFVF_STATUS_SUCCESS) {
295 for_each_vfq(vf, i)
296 resc->hw_qid[i] =
297 vfq_qzone_id(vf, vfq_get(vf, i));
298
299 for_each_vf_sb(vf, i) {
300 resc->hw_sbs[i].hw_sb_id = vf_igu_sb(vf, i);
301 resc->hw_sbs[i].sb_qid = vf_hc_qzone(vf, i);
302 }
303 }
304 }
305
306 DP(BNX2X_MSG_IOV, "VF[%d] ACQUIRE_RESPONSE: pfdev_info- chip_num=0x%x, db_size=%d, idx_per_sb=%d, pf_cap=0x%x\n"
307 "resources- n_rxq-%d, n_txq-%d, n_sbs-%d, n_macs-%d, n_vlans-%d, n_mcs-%d, fw_ver: '%s'\n",
308 vf->abs_vfid,
309 resp->pfdev_info.chip_num,
310 resp->pfdev_info.db_size,
311 resp->pfdev_info.indices_per_sb,
312 resp->pfdev_info.pf_cap,
313 resc->num_rxqs,
314 resc->num_txqs,
315 resc->num_sbs,
316 resc->num_mac_filters,
317 resc->num_vlan_filters,
318 resc->num_mc_filters,
319 resp->pfdev_info.fw_ver);
320
321 DP_CONT(BNX2X_MSG_IOV, "hw_qids- [ ");
322 for (i = 0; i < vf_rxq_count(vf); i++)
323 DP_CONT(BNX2X_MSG_IOV, "%d ", resc->hw_qid[i]);
324 DP_CONT(BNX2X_MSG_IOV, "], sb_info- [ ");
325 for (i = 0; i < vf_sb_count(vf); i++)
326 DP_CONT(BNX2X_MSG_IOV, "%d:%d ",
327 resc->hw_sbs[i].hw_sb_id,
328 resc->hw_sbs[i].sb_qid);
329 DP_CONT(BNX2X_MSG_IOV, "]\n");
330
331 /* send the response */
332 vf->op_rc = vfop_status;
333 bnx2x_vf_mbx_resp(bp, vf);
334}
335
336static void bnx2x_vf_mbx_acquire(struct bnx2x *bp, struct bnx2x_virtf *vf,
337 struct bnx2x_vf_mbx *mbx)
338{
339 int rc;
340 struct vfpf_acquire_tlv *acquire = &mbx->msg->req.acquire;
341
342 /* log vfdef info */
343 DP(BNX2X_MSG_IOV,
344 "VF[%d] ACQUIRE: vfdev_info- vf_id %d, vf_os %d resources- n_rxq-%d, n_txq-%d, n_sbs-%d, n_macs-%d, n_vlans-%d, n_mcs-%d\n",
345 vf->abs_vfid, acquire->vfdev_info.vf_id, acquire->vfdev_info.vf_os,
346 acquire->resc_request.num_rxqs, acquire->resc_request.num_txqs,
347 acquire->resc_request.num_sbs, acquire->resc_request.num_mac_filters,
348 acquire->resc_request.num_vlan_filters,
349 acquire->resc_request.num_mc_filters);
350
351 /* acquire the resources */
352 rc = bnx2x_vf_acquire(bp, vf, &acquire->resc_request);
353
354 /* response */
355 bnx2x_vf_mbx_acquire_resp(bp, vf, mbx, rc);
356}
357
fd1fc79d
AE
358/* dispatch request */
359static void bnx2x_vf_mbx_request(struct bnx2x *bp, struct bnx2x_virtf *vf,
360 struct bnx2x_vf_mbx *mbx)
361{
362 int i;
363
364 /* check if tlv type is known */
365 if (bnx2x_tlv_supported(mbx->first_tlv.tl.type)) {
8ca5e17e
AE
366 /* Lock the per vf op mutex and note the locker's identity.
367 * The unlock will take place in mbx response.
368 */
369 bnx2x_lock_vf_pf_channel(bp, vf, mbx->first_tlv.tl.type);
370
fd1fc79d
AE
371 /* switch on the opcode */
372 switch (mbx->first_tlv.tl.type) {
8ca5e17e
AE
373 case CHANNEL_TLV_ACQUIRE:
374 bnx2x_vf_mbx_acquire(bp, vf, mbx);
375 break;
fd1fc79d
AE
376 }
377 } else {
378 /* unknown TLV - this may belong to a VF driver from the future
379 * - a version written after this PF driver was written, which
380 * supports features unknown as of yet. Too bad since we don't
381 * support them. Or this may be because someone wrote a crappy
382 * VF driver and is sending garbage over the channel.
383 */
384 BNX2X_ERR("unknown TLV. type %d length %d. first 20 bytes of mailbox buffer:\n",
385 mbx->first_tlv.tl.type, mbx->first_tlv.tl.length);
386 for (i = 0; i < 20; i++)
387 DP_CONT(BNX2X_MSG_IOV, "%x ",
388 mbx->msg->req.tlv_buf_size.tlv_buffer[i]);
8ca5e17e
AE
389
390 /* test whether we can respond to the VF (do we have an address
391 * for it?)
392 */
393 if (vf->state == VF_ACQUIRED) {
394 /* mbx_resp uses the op_rc of the VF */
395 vf->op_rc = PFVF_STATUS_NOT_SUPPORTED;
396
397 /* notify the VF that we do not support this request */
398 bnx2x_vf_mbx_resp(bp, vf);
399 } else {
400 /* can't send a response since this VF is unknown to us
401 * just unlock the channel and be done with.
402 */
403 bnx2x_unlock_vf_pf_channel(bp, vf,
404 mbx->first_tlv.tl.type);
405 }
fd1fc79d
AE
406 }
407}
408
409/* handle new vf-pf message */
410void bnx2x_vf_mbx(struct bnx2x *bp, struct vf_pf_event_data *vfpf_event)
411{
412 struct bnx2x_virtf *vf;
413 struct bnx2x_vf_mbx *mbx;
414 u8 vf_idx;
415 int rc;
416
417 DP(BNX2X_MSG_IOV,
418 "vf pf event received: vfid %d, address_hi %x, address lo %x",
419 vfpf_event->vf_id, vfpf_event->msg_addr_hi, vfpf_event->msg_addr_lo);
420 /* Sanity checks consider removing later */
421
422 /* check if the vf_id is valid */
423 if (vfpf_event->vf_id - BP_VFDB(bp)->sriov.first_vf_in_pf >
424 BNX2X_NR_VIRTFN(bp)) {
425 BNX2X_ERR("Illegal vf_id %d max allowed: %d\n",
426 vfpf_event->vf_id, BNX2X_NR_VIRTFN(bp));
427 goto mbx_done;
428 }
429 vf_idx = bnx2x_vf_idx_by_abs_fid(bp, vfpf_event->vf_id);
430 mbx = BP_VF_MBX(bp, vf_idx);
431
432 /* verify an event is not currently being processed -
433 * debug failsafe only
434 */
435 if (mbx->flags & VF_MSG_INPROCESS) {
436 BNX2X_ERR("Previous message is still being processed, vf_id %d\n",
437 vfpf_event->vf_id);
438 goto mbx_done;
439 }
440 vf = BP_VF(bp, vf_idx);
441
442 /* save the VF message address */
443 mbx->vf_addr_hi = vfpf_event->msg_addr_hi;
444 mbx->vf_addr_lo = vfpf_event->msg_addr_lo;
445 DP(BNX2X_MSG_IOV, "mailbox vf address hi 0x%x, lo 0x%x, offset 0x%x\n",
446 mbx->vf_addr_hi, mbx->vf_addr_lo, mbx->first_tlv.resp_msg_offset);
447
448 /* dmae to get the VF request */
449 rc = bnx2x_copy32_vf_dmae(bp, true, mbx->msg_mapping, vf->abs_vfid,
450 mbx->vf_addr_hi, mbx->vf_addr_lo,
451 sizeof(union vfpf_tlvs)/4);
452 if (rc) {
453 BNX2X_ERR("Failed to copy request VF %d\n", vf->abs_vfid);
454 goto mbx_error;
455 }
456
457 /* process the VF message header */
458 mbx->first_tlv = mbx->msg->req.first_tlv;
459
460 /* dispatch the request (will prepare the response) */
461 bnx2x_vf_mbx_request(bp, vf, mbx);
462 goto mbx_done;
463
464mbx_error:
465mbx_done:
466 return;
467}