2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <rdma/ib_mad.h>
34 #include <rdma/ib_smi.h>
35 #include <rdma/ib_sa.h>
36 #include <rdma/ib_cache.h>
38 #include <linux/mlx4/cmd.h>
39 #include <linux/gfp.h>
40 #include <rdma/ib_pma.h>
45 MLX4_IB_VENDOR_CLASS1 = 0x9,
46 MLX4_IB_VENDOR_CLASS2 = 0xa
49 #define MLX4_TUN_SEND_WRID_SHIFT 34
50 #define MLX4_TUN_QPN_SHIFT 32
51 #define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
52 #define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
54 #define MLX4_TUN_IS_RECV(a) (((a) >> MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
55 #define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
57 /* Port mgmt change event handling */
59 #define GET_BLK_PTR_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.block_ptr)
60 #define GET_MASK_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.tbl_entries_mask)
61 #define NUM_IDX_IN_PKEY_TBL_BLK 32
62 #define GUID_TBL_ENTRY_SIZE 8 /* size in bytes */
63 #define GUID_TBL_BLK_NUM_ENTRIES 8
64 #define GUID_TBL_BLK_SIZE (GUID_TBL_ENTRY_SIZE * GUID_TBL_BLK_NUM_ENTRIES)
66 struct mlx4_mad_rcv_buf {
71 struct mlx4_mad_snd_buf {
75 struct mlx4_tunnel_mad {
77 struct mlx4_ib_tunnel_header hdr;
81 struct mlx4_rcv_tunnel_mad {
82 struct mlx4_rcv_tunnel_hdr hdr;
87 static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num);
88 static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num);
89 static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
90 int block, u32 change_bitmap);
92 __be64 mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx *ctx)
94 return cpu_to_be64(atomic_inc_return(&ctx->tid)) |
95 cpu_to_be64(0xff00000000000000LL);
98 int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int mad_ifc_flags,
99 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
100 void *in_mad, void *response_mad)
102 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
105 u32 in_modifier = port;
108 inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
109 if (IS_ERR(inmailbox))
110 return PTR_ERR(inmailbox);
111 inbox = inmailbox->buf;
113 outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
114 if (IS_ERR(outmailbox)) {
115 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
116 return PTR_ERR(outmailbox);
119 memcpy(inbox, in_mad, 256);
122 * Key check traps can't be generated unless we have in_wc to
123 * tell us where to send the trap.
125 if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_MKEY) || !in_wc)
127 if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_BKEY) || !in_wc)
129 if (mlx4_is_mfunc(dev->dev) &&
130 (mad_ifc_flags & MLX4_MAD_IFC_NET_VIEW || in_wc))
146 memset(inbox + 256, 0, 256);
147 ext_info = inbox + 256;
149 ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
150 ext_info->rqpn = cpu_to_be32(in_wc->src_qp);
151 ext_info->sl = in_wc->sl << 4;
152 ext_info->g_path = in_wc->dlid_path_bits |
153 (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
154 ext_info->pkey = cpu_to_be16(in_wc->pkey_index);
157 memcpy(ext_info->grh, in_grh, 40);
161 in_modifier |= in_wc->slid << 16;
164 err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma, in_modifier,
165 mlx4_is_master(dev->dev) ? (op_modifier & ~0x8) : op_modifier,
166 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
167 (op_modifier & 0x8) ? MLX4_CMD_NATIVE : MLX4_CMD_WRAPPED);
170 memcpy(response_mad, outmailbox->buf, 256);
172 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
173 mlx4_free_cmd_mailbox(dev->dev, outmailbox);
178 static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
180 struct ib_ah *new_ah;
181 struct ib_ah_attr ah_attr;
184 if (!dev->send_agent[port_num - 1][0])
187 memset(&ah_attr, 0, sizeof ah_attr);
190 ah_attr.port_num = port_num;
192 new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
197 spin_lock_irqsave(&dev->sm_lock, flags);
198 if (dev->sm_ah[port_num - 1])
199 ib_destroy_ah(dev->sm_ah[port_num - 1]);
200 dev->sm_ah[port_num - 1] = new_ah;
201 spin_unlock_irqrestore(&dev->sm_lock, flags);
205 * Snoop SM MADs for port info, GUID info, and P_Key table sets, so we can
206 * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
208 static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
211 struct ib_port_info *pinfo;
214 u32 bn, pkey_change_bitmap;
218 struct mlx4_ib_dev *dev = to_mdev(ibdev);
219 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
220 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
221 mad->mad_hdr.method == IB_MGMT_METHOD_SET)
222 switch (mad->mad_hdr.attr_id) {
223 case IB_SMP_ATTR_PORT_INFO:
224 pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
225 lid = be16_to_cpu(pinfo->lid);
227 update_sm_ah(dev, port_num,
228 be16_to_cpu(pinfo->sm_lid),
229 pinfo->neighbormtu_mastersmsl & 0xf);
231 if (pinfo->clientrereg_resv_subnetto & 0x80)
232 handle_client_rereg_event(dev, port_num);
235 handle_lid_change_event(dev, port_num);
238 case IB_SMP_ATTR_PKEY_TABLE:
239 if (!mlx4_is_mfunc(dev->dev)) {
240 mlx4_ib_dispatch_event(dev, port_num,
241 IB_EVENT_PKEY_CHANGE);
245 /* at this point, we are running in the master.
246 * Slaves do not receive SMPs.
248 bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
249 base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
250 pkey_change_bitmap = 0;
251 for (i = 0; i < 32; i++) {
252 pr_debug("PKEY[%d] = x%x\n",
253 i + bn*32, be16_to_cpu(base[i]));
254 if (be16_to_cpu(base[i]) !=
255 dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32]) {
256 pkey_change_bitmap |= (1 << i);
257 dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32] =
258 be16_to_cpu(base[i]);
261 pr_debug("PKEY Change event: port=%d, "
262 "block=0x%x, change_bitmap=0x%x\n",
263 port_num, bn, pkey_change_bitmap);
265 if (pkey_change_bitmap) {
266 mlx4_ib_dispatch_event(dev, port_num,
267 IB_EVENT_PKEY_CHANGE);
268 if (!dev->sriov.is_going_down)
269 __propagate_pkey_ev(dev, port_num, bn,
274 case IB_SMP_ATTR_GUID_INFO:
275 /* paravirtualized master's guid is guid 0 -- does not change */
276 if (!mlx4_is_master(dev->dev))
277 mlx4_ib_dispatch_event(dev, port_num,
278 IB_EVENT_GID_CHANGE);
279 /*if master, notify relevant slaves*/
280 if (mlx4_is_master(dev->dev) &&
281 !dev->sriov.is_going_down) {
282 bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod);
283 mlx4_ib_update_cache_on_guid_change(dev, bn, port_num,
284 (u8 *)(&((struct ib_smp *)mad)->data));
285 mlx4_ib_notify_slaves_on_guid_change(dev, bn, port_num,
286 (u8 *)(&((struct ib_smp *)mad)->data));
295 static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
296 int block, u32 change_bitmap)
298 int i, ix, slave, err;
301 for (slave = 0; slave < dev->dev->caps.sqp_demux; slave++) {
302 if (slave == mlx4_master_func_num(dev->dev))
304 if (!mlx4_is_slave_active(dev->dev, slave))
308 for (i = 0; i < 32; i++) {
309 if (!(change_bitmap & (1 << i)))
312 ix < dev->dev->caps.pkey_table_len[port_num]; ix++) {
313 if (dev->pkeys.virt2phys_pkey[slave][port_num - 1]
314 [ix] == i + 32 * block) {
315 err = mlx4_gen_pkey_eqe(dev->dev, slave, port_num);
316 pr_debug("propagate_pkey_ev: slave %d,"
317 " port %d, ix %d (%d)\n",
318 slave, port_num, ix, err);
329 static void node_desc_override(struct ib_device *dev,
334 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
335 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
336 mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
337 mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
338 spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
339 memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
340 spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
344 static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
346 int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
347 struct ib_mad_send_buf *send_buf;
348 struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
353 send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
354 IB_MGMT_MAD_DATA, GFP_ATOMIC);
355 if (IS_ERR(send_buf))
358 * We rely here on the fact that MLX QPs don't use the
359 * address handle after the send is posted (this is
360 * wrong following the IB spec strictly, but we know
361 * it's OK for our devices).
363 spin_lock_irqsave(&dev->sm_lock, flags);
364 memcpy(send_buf->mad, mad, sizeof *mad);
365 if ((send_buf->ah = dev->sm_ah[port_num - 1]))
366 ret = ib_post_send_mad(send_buf, NULL);
369 spin_unlock_irqrestore(&dev->sm_lock, flags);
372 ib_free_send_mad(send_buf);
376 static int mlx4_ib_demux_sa_handler(struct ib_device *ibdev, int port, int slave,
377 struct ib_sa_mad *sa_mad)
381 /* dispatch to different sa handlers */
382 switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
383 case IB_SA_ATTR_MC_MEMBER_REC:
384 ret = mlx4_ib_mcg_demux_handler(ibdev, port, slave, sa_mad);
392 int mlx4_ib_find_real_gid(struct ib_device *ibdev, u8 port, __be64 guid)
394 struct mlx4_ib_dev *dev = to_mdev(ibdev);
397 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
398 if (dev->sriov.demux[port - 1].guid_cache[i] == guid)
405 static int get_pkey_phys_indices(struct mlx4_ib_dev *ibdev, u8 port, u8 ph_pkey_ix,
406 u8 *full_pk_ix, u8 *partial_pk_ix,
414 err = ib_get_cached_pkey(&ibdev->ib_dev, port, ph_pkey_ix, &search_pkey);
418 fm = (search_pkey & 0x8000) ? 1 : 0;
420 *full_pk_ix = ph_pkey_ix;
421 search_pkey &= 0x7FFF;
423 *partial_pk_ix = ph_pkey_ix;
424 search_pkey |= 0x8000;
427 if (ib_find_exact_cached_pkey(&ibdev->ib_dev, port, search_pkey, &pk))
431 *partial_pk_ix = (pk & 0xFF);
433 *full_pk_ix = (pk & 0xFF);
435 *is_full_member = fm;
439 int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u8 port,
440 enum ib_qp_type dest_qpt, struct ib_wc *wc,
441 struct ib_grh *grh, struct ib_mad *mad)
444 struct ib_send_wr wr, *bad_wr;
445 struct mlx4_ib_demux_pv_ctx *tun_ctx;
446 struct mlx4_ib_demux_pv_qp *tun_qp;
447 struct mlx4_rcv_tunnel_mad *tun_mad;
448 struct ib_ah_attr attr;
450 struct ib_qp *src_qp = NULL;
451 unsigned tun_tx_ix = 0;
455 int is_full_member = 0;
457 u8 ph_pkey_ix, full_pk_ix = 0, partial_pk_ix = 0;
459 if (dest_qpt > IB_QPT_GSI)
462 tun_ctx = dev->sriov.demux[port-1].tun[slave];
464 /* check if proxy qp created */
465 if (!tun_ctx || tun_ctx->state != DEMUX_PV_STATE_ACTIVE)
468 /* QP0 forwarding only for Dom0 */
469 if (!dest_qpt && (mlx4_master_func_num(dev->dev) != slave))
473 tun_qp = &tun_ctx->qp[0];
475 tun_qp = &tun_ctx->qp[1];
477 /* compute pkey index for slave */
478 /* get physical pkey -- virtualized Dom0 pkey to phys*/
481 dev->pkeys.virt2phys_pkey[mlx4_master_func_num(dev->dev)][port - 1][wc->pkey_index];
483 /* now, translate this to the slave pkey index */
484 ret = get_pkey_phys_indices(dev, port, ph_pkey_ix, &full_pk_ix,
485 &partial_pk_ix, &is_full_member);
489 for (i = 0; i < dev->dev->caps.pkey_table_len[port]; i++) {
490 if ((dev->pkeys.virt2phys_pkey[slave][port - 1][i] == full_pk_ix) ||
492 (dev->pkeys.virt2phys_pkey[slave][port - 1][i] == partial_pk_ix)))
495 if (i == dev->dev->caps.pkey_table_len[port])
499 tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
501 dqpn = dev->dev->caps.sqp_start + 8 * slave + port + (dest_qpt * 2) - 1;
503 /* get tunnel tx data buf for slave */
506 /* create ah. Just need an empty one with the port num for the post send.
507 * The driver will set the force loopback bit in post_send */
508 memset(&attr, 0, sizeof attr);
509 attr.port_num = port;
510 ah = ib_create_ah(tun_ctx->pd, &attr);
514 /* allocate tunnel tx buf after pass failure returns */
515 spin_lock(&tun_qp->tx_lock);
516 if (tun_qp->tx_ix_head - tun_qp->tx_ix_tail >=
517 (MLX4_NUM_TUNNEL_BUFS - 1))
520 tun_tx_ix = (++tun_qp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
521 spin_unlock(&tun_qp->tx_lock);
525 tun_mad = (struct mlx4_rcv_tunnel_mad *) (tun_qp->tx_ring[tun_tx_ix].buf.addr);
526 if (tun_qp->tx_ring[tun_tx_ix].ah)
527 ib_destroy_ah(tun_qp->tx_ring[tun_tx_ix].ah);
528 tun_qp->tx_ring[tun_tx_ix].ah = ah;
529 ib_dma_sync_single_for_cpu(&dev->ib_dev,
530 tun_qp->tx_ring[tun_tx_ix].buf.map,
531 sizeof (struct mlx4_rcv_tunnel_mad),
534 /* copy over to tunnel buffer */
536 memcpy(&tun_mad->grh, grh, sizeof *grh);
537 memcpy(&tun_mad->mad, mad, sizeof *mad);
539 /* adjust tunnel data */
540 tun_mad->hdr.pkey_index = cpu_to_be16(tun_pkey_ix);
541 tun_mad->hdr.sl_vid = cpu_to_be16(((u16)(wc->sl)) << 12);
542 tun_mad->hdr.slid_mac_47_32 = cpu_to_be16(wc->slid);
543 tun_mad->hdr.flags_src_qp = cpu_to_be32(wc->src_qp & 0xFFFFFF);
544 tun_mad->hdr.g_ml_path = (grh && (wc->wc_flags & IB_WC_GRH)) ? 0x80 : 0;
546 ib_dma_sync_single_for_device(&dev->ib_dev,
547 tun_qp->tx_ring[tun_tx_ix].buf.map,
548 sizeof (struct mlx4_rcv_tunnel_mad),
551 list.addr = tun_qp->tx_ring[tun_tx_ix].buf.map;
552 list.length = sizeof (struct mlx4_rcv_tunnel_mad);
553 list.lkey = tun_ctx->mr->lkey;
556 wr.wr.ud.port_num = port;
557 wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
558 wr.wr.ud.remote_qpn = dqpn;
560 wr.wr_id = ((u64) tun_tx_ix) | MLX4_TUN_SET_WRID_QPN(dest_qpt);
563 wr.opcode = IB_WR_SEND;
564 wr.send_flags = IB_SEND_SIGNALED;
566 ret = ib_post_send(src_qp, &wr, &bad_wr);
573 static int mlx4_ib_demux_mad(struct ib_device *ibdev, u8 port,
574 struct ib_wc *wc, struct ib_grh *grh,
577 struct mlx4_ib_dev *dev = to_mdev(ibdev);
582 /* Initially assume that this mad is for us */
583 slave = mlx4_master_func_num(dev->dev);
585 /* See if the slave id is encoded in a response mad */
586 if (mad->mad_hdr.method & 0x80) {
587 slave_id = (u8 *) &mad->mad_hdr.tid;
589 if (slave != 255) /*255 indicates the dom0*/
590 *slave_id = 0; /* remap tid */
593 /* If a grh is present, we demux according to it */
594 if (wc->wc_flags & IB_WC_GRH) {
595 slave = mlx4_ib_find_real_gid(ibdev, port, grh->dgid.global.interface_id);
597 mlx4_ib_warn(ibdev, "failed matching grh\n");
601 /* Class-specific handling */
602 switch (mad->mad_hdr.mgmt_class) {
603 case IB_MGMT_CLASS_SUBN_ADM:
604 if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
605 (struct ib_sa_mad *) mad))
608 case IB_MGMT_CLASS_CM:
609 if (mlx4_ib_demux_cm_handler(ibdev, port, &slave, mad))
612 case IB_MGMT_CLASS_DEVICE_MGMT:
613 if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
617 /* Drop unsupported classes for slaves in tunnel mode */
618 if (slave != mlx4_master_func_num(dev->dev)) {
619 pr_debug("dropping unsupported ingress mad from class:%d "
620 "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
624 /*make sure that no slave==255 was not handled yet.*/
625 if (slave >= dev->dev->caps.sqp_demux) {
626 mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
627 slave, dev->dev->caps.sqp_demux);
631 err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
633 pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
638 static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
639 struct ib_wc *in_wc, struct ib_grh *in_grh,
640 struct ib_mad *in_mad, struct ib_mad *out_mad)
642 u16 slid, prev_lid = 0;
644 struct ib_port_attr pattr;
646 if (in_wc && in_wc->qp->qp_num) {
647 pr_debug("received MAD: slid:%d sqpn:%d "
648 "dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
649 in_wc->slid, in_wc->src_qp,
650 in_wc->dlid_path_bits,
653 in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
654 be16_to_cpu(in_mad->mad_hdr.attr_id));
655 if (in_wc->wc_flags & IB_WC_GRH) {
656 pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
657 be64_to_cpu(in_grh->sgid.global.subnet_prefix),
658 be64_to_cpu(in_grh->sgid.global.interface_id));
659 pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
660 be64_to_cpu(in_grh->dgid.global.subnet_prefix),
661 be64_to_cpu(in_grh->dgid.global.interface_id));
665 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
667 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
668 forward_trap(to_mdev(ibdev), port_num, in_mad);
669 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
672 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
673 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
674 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
675 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
676 in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
677 return IB_MAD_RESULT_SUCCESS;
680 * Don't process SMInfo queries -- the SMA can't handle them.
682 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
683 return IB_MAD_RESULT_SUCCESS;
684 } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
685 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
686 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
687 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
688 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
689 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
690 return IB_MAD_RESULT_SUCCESS;
692 return IB_MAD_RESULT_SUCCESS;
694 if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
695 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
696 in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
697 in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
698 !ib_query_port(ibdev, port_num, &pattr))
699 prev_lid = pattr.lid;
701 err = mlx4_MAD_IFC(to_mdev(ibdev),
702 (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
703 (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
704 MLX4_MAD_IFC_NET_VIEW,
705 port_num, in_wc, in_grh, in_mad, out_mad);
707 return IB_MAD_RESULT_FAILURE;
709 if (!out_mad->mad_hdr.status) {
710 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
711 smp_snoop(ibdev, port_num, in_mad, prev_lid);
712 /* slaves get node desc from FW */
713 if (!mlx4_is_slave(to_mdev(ibdev)->dev))
714 node_desc_override(ibdev, out_mad);
717 /* set return bit in status of directed route responses */
718 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
719 out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
721 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
722 /* no response for trap repress */
723 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
725 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
728 static void edit_counter(struct mlx4_counter *cnt,
729 struct ib_pma_portcounters *pma_cnt)
731 pma_cnt->port_xmit_data = cpu_to_be32((be64_to_cpu(cnt->tx_bytes)>>2));
732 pma_cnt->port_rcv_data = cpu_to_be32((be64_to_cpu(cnt->rx_bytes)>>2));
733 pma_cnt->port_xmit_packets = cpu_to_be32(be64_to_cpu(cnt->tx_frames));
734 pma_cnt->port_rcv_packets = cpu_to_be32(be64_to_cpu(cnt->rx_frames));
737 static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
738 struct ib_wc *in_wc, struct ib_grh *in_grh,
739 struct ib_mad *in_mad, struct ib_mad *out_mad)
741 struct mlx4_cmd_mailbox *mailbox;
742 struct mlx4_ib_dev *dev = to_mdev(ibdev);
744 u32 inmod = dev->counters[port_num - 1] & 0xffff;
747 if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
750 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
752 return IB_MAD_RESULT_FAILURE;
754 err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
755 MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
758 err = IB_MAD_RESULT_FAILURE;
760 memset(out_mad->data, 0, sizeof out_mad->data);
761 mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
762 switch (mode & 0xf) {
764 edit_counter(mailbox->buf,
765 (void *)(out_mad->data + 40));
766 err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
769 err = IB_MAD_RESULT_FAILURE;
773 mlx4_free_cmd_mailbox(dev->dev, mailbox);
778 int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
779 struct ib_wc *in_wc, struct ib_grh *in_grh,
780 struct ib_mad *in_mad, struct ib_mad *out_mad)
782 switch (rdma_port_get_link_layer(ibdev, port_num)) {
783 case IB_LINK_LAYER_INFINIBAND:
784 return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
785 in_grh, in_mad, out_mad);
786 case IB_LINK_LAYER_ETHERNET:
787 return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
788 in_grh, in_mad, out_mad);
794 static void send_handler(struct ib_mad_agent *agent,
795 struct ib_mad_send_wc *mad_send_wc)
797 if (mad_send_wc->send_buf->context[0])
798 ib_destroy_ah(mad_send_wc->send_buf->context[0]);
799 ib_free_send_mad(mad_send_wc->send_buf);
802 int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
804 struct ib_mad_agent *agent;
807 enum rdma_link_layer ll;
809 for (p = 0; p < dev->num_ports; ++p) {
810 ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
811 for (q = 0; q <= 1; ++q) {
812 if (ll == IB_LINK_LAYER_INFINIBAND) {
813 agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
814 q ? IB_QPT_GSI : IB_QPT_SMI,
815 NULL, 0, send_handler,
818 ret = PTR_ERR(agent);
821 dev->send_agent[p][q] = agent;
823 dev->send_agent[p][q] = NULL;
830 for (p = 0; p < dev->num_ports; ++p)
831 for (q = 0; q <= 1; ++q)
832 if (dev->send_agent[p][q])
833 ib_unregister_mad_agent(dev->send_agent[p][q]);
838 void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
840 struct ib_mad_agent *agent;
843 for (p = 0; p < dev->num_ports; ++p) {
844 for (q = 0; q <= 1; ++q) {
845 agent = dev->send_agent[p][q];
847 dev->send_agent[p][q] = NULL;
848 ib_unregister_mad_agent(agent);
853 ib_destroy_ah(dev->sm_ah[p]);
857 static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num)
859 mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_LID_CHANGE);
861 if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
862 mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
863 MLX4_EQ_PORT_INFO_LID_CHANGE_MASK);
866 static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num)
868 /* re-configure the alias-guid and mcg's */
869 if (mlx4_is_master(dev->dev)) {
870 mlx4_ib_invalidate_all_guid_record(dev, port_num);
872 if (!dev->sriov.is_going_down) {
873 mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
874 mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
875 MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK);
878 mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
881 static void propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
882 struct mlx4_eqe *eqe)
884 __propagate_pkey_ev(dev, port_num, GET_BLK_PTR_FROM_EQE(eqe),
885 GET_MASK_FROM_EQE(eqe));
888 static void handle_slaves_guid_change(struct mlx4_ib_dev *dev, u8 port_num,
889 u32 guid_tbl_blk_num, u32 change_bitmap)
891 struct ib_smp *in_mad = NULL;
892 struct ib_smp *out_mad = NULL;
895 if (!mlx4_is_mfunc(dev->dev) || !mlx4_is_master(dev->dev))
898 in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
899 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
900 if (!in_mad || !out_mad) {
901 mlx4_ib_warn(&dev->ib_dev, "failed to allocate memory for guid info mads\n");
905 guid_tbl_blk_num *= 4;
907 for (i = 0; i < 4; i++) {
908 if (change_bitmap && (!((change_bitmap >> (8 * i)) & 0xff)))
910 memset(in_mad, 0, sizeof *in_mad);
911 memset(out_mad, 0, sizeof *out_mad);
913 in_mad->base_version = 1;
914 in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
915 in_mad->class_version = 1;
916 in_mad->method = IB_MGMT_METHOD_GET;
917 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
918 in_mad->attr_mod = cpu_to_be32(guid_tbl_blk_num + i);
920 if (mlx4_MAD_IFC(dev,
921 MLX4_MAD_IFC_IGNORE_KEYS | MLX4_MAD_IFC_NET_VIEW,
922 port_num, NULL, NULL, in_mad, out_mad)) {
923 mlx4_ib_warn(&dev->ib_dev, "Failed in get GUID INFO MAD_IFC\n");
927 mlx4_ib_update_cache_on_guid_change(dev, guid_tbl_blk_num + i,
929 (u8 *)(&((struct ib_smp *)out_mad)->data));
930 mlx4_ib_notify_slaves_on_guid_change(dev, guid_tbl_blk_num + i,
932 (u8 *)(&((struct ib_smp *)out_mad)->data));
941 void handle_port_mgmt_change_event(struct work_struct *work)
943 struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
944 struct mlx4_ib_dev *dev = ew->ib_dev;
945 struct mlx4_eqe *eqe = &(ew->ib_eqe);
946 u8 port = eqe->event.port_mgmt_change.port;
951 switch (eqe->subtype) {
952 case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
953 changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
955 /* Update the SM ah - This should be done before handling
956 the other changed attributes so that MADs can be sent to the SM */
957 if (changed_attr & MSTR_SM_CHANGE_MASK) {
958 u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
959 u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
960 update_sm_ah(dev, port, lid, sl);
963 /* Check if it is a lid change event */
964 if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
965 handle_lid_change_event(dev, port);
967 /* Generate GUID changed event */
968 if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK) {
969 mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
970 /*if master, notify all slaves*/
971 if (mlx4_is_master(dev->dev))
972 mlx4_gen_slaves_port_mgt_ev(dev->dev, port,
973 MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK);
976 if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
977 handle_client_rereg_event(dev, port);
980 case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
981 mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
982 if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
983 propagate_pkey_ev(dev, port, eqe);
985 case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
986 /* paravirtualized master's guid is guid 0 -- does not change */
987 if (!mlx4_is_master(dev->dev))
988 mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
989 /*if master, notify relevant slaves*/
990 else if (!dev->sriov.is_going_down) {
991 tbl_block = GET_BLK_PTR_FROM_EQE(eqe);
992 change_bitmap = GET_MASK_FROM_EQE(eqe);
993 handle_slaves_guid_change(dev, port, tbl_block, change_bitmap);
997 pr_warn("Unsupported subtype 0x%x for "
998 "Port Management Change event\n", eqe->subtype);
1004 void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
1005 enum ib_event_type type)
1007 struct ib_event event;
1009 event.device = &dev->ib_dev;
1010 event.element.port_num = port_num;
1013 ib_dispatch_event(&event);
1016 static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
1018 unsigned long flags;
1019 struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
1020 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1021 spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
1022 if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
1023 queue_work(ctx->wq, &ctx->work);
1024 spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
1027 static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
1028 struct mlx4_ib_demux_pv_qp *tun_qp,
1031 struct ib_sge sg_list;
1032 struct ib_recv_wr recv_wr, *bad_recv_wr;
1035 size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
1036 sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
1038 sg_list.addr = tun_qp->ring[index].map;
1039 sg_list.length = size;
1040 sg_list.lkey = ctx->mr->lkey;
1042 recv_wr.next = NULL;
1043 recv_wr.sg_list = &sg_list;
1044 recv_wr.num_sge = 1;
1045 recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
1046 MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
1047 ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
1048 size, DMA_FROM_DEVICE);
1049 return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
1052 static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
1053 int slave, struct ib_sa_mad *sa_mad)
1057 /* dispatch to different sa handlers */
1058 switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
1059 case IB_SA_ATTR_MC_MEMBER_REC:
1060 ret = mlx4_ib_mcg_multiplex_handler(ibdev, port, slave, sa_mad);
1068 static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
1070 int slave_start = dev->dev->caps.sqp_start + 8 * slave;
1072 return (qpn >= slave_start && qpn <= slave_start + 1);
1076 int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u8 port,
1077 enum ib_qp_type dest_qpt, u16 pkey_index, u32 remote_qpn,
1078 u32 qkey, struct ib_ah_attr *attr, struct ib_mad *mad)
1081 struct ib_send_wr wr, *bad_wr;
1082 struct mlx4_ib_demux_pv_ctx *sqp_ctx;
1083 struct mlx4_ib_demux_pv_qp *sqp;
1084 struct mlx4_mad_snd_buf *sqp_mad;
1086 struct ib_qp *send_qp = NULL;
1087 unsigned wire_tx_ix = 0;
1094 sqp_ctx = dev->sriov.sqps[port-1];
1096 /* check if proxy qp created */
1097 if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
1100 /* QP0 forwarding only for Dom0 */
1101 if (dest_qpt == IB_QPT_SMI && (mlx4_master_func_num(dev->dev) != slave))
1104 if (dest_qpt == IB_QPT_SMI) {
1106 sqp = &sqp_ctx->qp[0];
1107 wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
1110 sqp = &sqp_ctx->qp[1];
1111 wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
1117 sgid_index = attr->grh.sgid_index;
1118 attr->grh.sgid_index = 0;
1119 ah = ib_create_ah(sqp_ctx->pd, attr);
1122 attr->grh.sgid_index = sgid_index;
1123 to_mah(ah)->av.ib.gid_index = sgid_index;
1124 /* get rid of force-loopback bit */
1125 to_mah(ah)->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
1126 spin_lock(&sqp->tx_lock);
1127 if (sqp->tx_ix_head - sqp->tx_ix_tail >=
1128 (MLX4_NUM_TUNNEL_BUFS - 1))
1131 wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
1132 spin_unlock(&sqp->tx_lock);
1136 sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
1137 if (sqp->tx_ring[wire_tx_ix].ah)
1138 ib_destroy_ah(sqp->tx_ring[wire_tx_ix].ah);
1139 sqp->tx_ring[wire_tx_ix].ah = ah;
1140 ib_dma_sync_single_for_cpu(&dev->ib_dev,
1141 sqp->tx_ring[wire_tx_ix].buf.map,
1142 sizeof (struct mlx4_mad_snd_buf),
1145 memcpy(&sqp_mad->payload, mad, sizeof *mad);
1147 ib_dma_sync_single_for_device(&dev->ib_dev,
1148 sqp->tx_ring[wire_tx_ix].buf.map,
1149 sizeof (struct mlx4_mad_snd_buf),
1152 list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
1153 list.length = sizeof (struct mlx4_mad_snd_buf);
1154 list.lkey = sqp_ctx->mr->lkey;
1157 wr.wr.ud.port_num = port;
1158 wr.wr.ud.pkey_index = wire_pkey_ix;
1159 wr.wr.ud.remote_qkey = qkey;
1160 wr.wr.ud.remote_qpn = remote_qpn;
1162 wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
1165 wr.opcode = IB_WR_SEND;
1166 wr.send_flags = IB_SEND_SIGNALED;
1168 ret = ib_post_send(send_qp, &wr, &bad_wr);
1175 static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
1177 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1178 struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
1179 int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
1180 struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
1181 struct mlx4_ib_ah ah;
1182 struct ib_ah_attr ah_attr;
1186 /* Get slave that sent this packet */
1187 if (wc->src_qp < dev->dev->caps.sqp_start ||
1188 wc->src_qp >= dev->dev->caps.base_tunnel_sqpn ||
1189 (wc->src_qp & 0x1) != ctx->port - 1 ||
1191 mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
1194 slave = ((wc->src_qp & ~0x7) - dev->dev->caps.sqp_start) / 8;
1195 if (slave != ctx->slave) {
1196 mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
1197 "belongs to another slave\n", wc->src_qp);
1200 if (slave != mlx4_master_func_num(dev->dev) && !(wc->src_qp & 0x2)) {
1201 mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
1202 "non-master trying to send QP0 packets\n", wc->src_qp);
1206 /* Map transaction ID */
1207 ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
1208 sizeof (struct mlx4_tunnel_mad),
1210 switch (tunnel->mad.mad_hdr.method) {
1211 case IB_MGMT_METHOD_SET:
1212 case IB_MGMT_METHOD_GET:
1213 case IB_MGMT_METHOD_REPORT:
1214 case IB_SA_METHOD_GET_TABLE:
1215 case IB_SA_METHOD_DELETE:
1216 case IB_SA_METHOD_GET_MULTI:
1217 case IB_SA_METHOD_GET_TRACE_TBL:
1218 slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
1220 mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
1221 "class:%d slave:%d\n", *slave_id,
1222 tunnel->mad.mad_hdr.mgmt_class, slave);
1230 /* Class-specific handling */
1231 switch (tunnel->mad.mad_hdr.mgmt_class) {
1232 case IB_MGMT_CLASS_SUBN_ADM:
1233 if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
1234 (struct ib_sa_mad *) &tunnel->mad))
1237 case IB_MGMT_CLASS_CM:
1238 if (mlx4_ib_multiplex_cm_handler(ctx->ib_dev, ctx->port, slave,
1239 (struct ib_mad *) &tunnel->mad))
1242 case IB_MGMT_CLASS_DEVICE_MGMT:
1243 if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
1244 tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
1248 /* Drop unsupported classes for slaves in tunnel mode */
1249 if (slave != mlx4_master_func_num(dev->dev)) {
1250 mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
1251 "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
1256 /* We are using standard ib_core services to send the mad, so generate a
1257 * stadard address handle by decoding the tunnelled mlx4_ah fields */
1258 memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
1259 ah.ibah.device = ctx->ib_dev;
1260 mlx4_ib_query_ah(&ah.ibah, &ah_attr);
1261 if ((ah_attr.ah_flags & IB_AH_GRH) &&
1262 (ah_attr.grh.sgid_index != slave)) {
1263 mlx4_ib_warn(ctx->ib_dev, "slave:%d accessed invalid sgid_index:%d\n",
1264 slave, ah_attr.grh.sgid_index);
1268 mlx4_ib_send_to_wire(dev, slave, ctx->port,
1269 is_proxy_qp0(dev, wc->src_qp, slave) ?
1270 IB_QPT_SMI : IB_QPT_GSI,
1271 be16_to_cpu(tunnel->hdr.pkey_index),
1272 be32_to_cpu(tunnel->hdr.remote_qpn),
1273 be32_to_cpu(tunnel->hdr.qkey),
1274 &ah_attr, &tunnel->mad);
1277 static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1278 enum ib_qp_type qp_type, int is_tun)
1281 struct mlx4_ib_demux_pv_qp *tun_qp;
1282 int rx_buf_size, tx_buf_size;
1284 if (qp_type > IB_QPT_GSI)
1287 tun_qp = &ctx->qp[qp_type];
1289 tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
1294 tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
1295 sizeof (struct mlx4_ib_tun_tx_buf),
1297 if (!tun_qp->tx_ring) {
1298 kfree(tun_qp->ring);
1299 tun_qp->ring = NULL;
1304 rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1305 tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1307 rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1308 tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1311 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1312 tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
1313 if (!tun_qp->ring[i].addr)
1315 tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
1316 tun_qp->ring[i].addr,
1321 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1322 tun_qp->tx_ring[i].buf.addr =
1323 kmalloc(tx_buf_size, GFP_KERNEL);
1324 if (!tun_qp->tx_ring[i].buf.addr)
1326 tun_qp->tx_ring[i].buf.map =
1327 ib_dma_map_single(ctx->ib_dev,
1328 tun_qp->tx_ring[i].buf.addr,
1331 tun_qp->tx_ring[i].ah = NULL;
1333 spin_lock_init(&tun_qp->tx_lock);
1334 tun_qp->tx_ix_head = 0;
1335 tun_qp->tx_ix_tail = 0;
1336 tun_qp->proxy_qpt = qp_type;
1343 ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1344 tx_buf_size, DMA_TO_DEVICE);
1345 kfree(tun_qp->tx_ring[i].buf.addr);
1347 kfree(tun_qp->tx_ring);
1348 tun_qp->tx_ring = NULL;
1349 i = MLX4_NUM_TUNNEL_BUFS;
1353 ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1354 rx_buf_size, DMA_FROM_DEVICE);
1355 kfree(tun_qp->ring[i].addr);
1357 kfree(tun_qp->ring);
1358 tun_qp->ring = NULL;
1362 static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1363 enum ib_qp_type qp_type, int is_tun)
1366 struct mlx4_ib_demux_pv_qp *tun_qp;
1367 int rx_buf_size, tx_buf_size;
1369 if (qp_type > IB_QPT_GSI)
1372 tun_qp = &ctx->qp[qp_type];
1374 rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1375 tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1377 rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1378 tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1382 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1383 ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1384 rx_buf_size, DMA_FROM_DEVICE);
1385 kfree(tun_qp->ring[i].addr);
1388 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1389 ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1390 tx_buf_size, DMA_TO_DEVICE);
1391 kfree(tun_qp->tx_ring[i].buf.addr);
1392 if (tun_qp->tx_ring[i].ah)
1393 ib_destroy_ah(tun_qp->tx_ring[i].ah);
1395 kfree(tun_qp->tx_ring);
1396 kfree(tun_qp->ring);
1399 static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
1401 struct mlx4_ib_demux_pv_ctx *ctx;
1402 struct mlx4_ib_demux_pv_qp *tun_qp;
1405 ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
1406 ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1408 while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
1409 tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
1410 if (wc.status == IB_WC_SUCCESS) {
1411 switch (wc.opcode) {
1413 mlx4_ib_multiplex_mad(ctx, &wc);
1414 ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
1416 (MLX4_NUM_TUNNEL_BUFS - 1));
1418 pr_err("Failed reposting tunnel "
1419 "buf:%lld\n", wc.wr_id);
1422 pr_debug("received tunnel send completion:"
1423 "wrid=0x%llx, status=0x%x\n",
1424 wc.wr_id, wc.status);
1425 ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
1426 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1427 tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1429 spin_lock(&tun_qp->tx_lock);
1430 tun_qp->tx_ix_tail++;
1431 spin_unlock(&tun_qp->tx_lock);
1438 pr_debug("mlx4_ib: completion error in tunnel: %d."
1439 " status = %d, wrid = 0x%llx\n",
1440 ctx->slave, wc.status, wc.wr_id);
1441 if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
1442 ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
1443 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1444 tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1446 spin_lock(&tun_qp->tx_lock);
1447 tun_qp->tx_ix_tail++;
1448 spin_unlock(&tun_qp->tx_lock);
1454 static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
1456 struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
1458 /* It's worse than that! He's dead, Jim! */
1459 pr_err("Fatal error (%d) on a MAD QP on port %d\n",
1460 event->event, sqp->port);
1463 static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
1464 enum ib_qp_type qp_type, int create_tun)
1467 struct mlx4_ib_demux_pv_qp *tun_qp;
1468 struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
1469 struct ib_qp_attr attr;
1470 int qp_attr_mask_INIT;
1472 if (qp_type > IB_QPT_GSI)
1475 tun_qp = &ctx->qp[qp_type];
1477 memset(&qp_init_attr, 0, sizeof qp_init_attr);
1478 qp_init_attr.init_attr.send_cq = ctx->cq;
1479 qp_init_attr.init_attr.recv_cq = ctx->cq;
1480 qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
1481 qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
1482 qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
1483 qp_init_attr.init_attr.cap.max_send_sge = 1;
1484 qp_init_attr.init_attr.cap.max_recv_sge = 1;
1486 qp_init_attr.init_attr.qp_type = IB_QPT_UD;
1487 qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
1488 qp_init_attr.port = ctx->port;
1489 qp_init_attr.slave = ctx->slave;
1490 qp_init_attr.proxy_qp_type = qp_type;
1491 qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
1492 IB_QP_QKEY | IB_QP_PORT;
1494 qp_init_attr.init_attr.qp_type = qp_type;
1495 qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
1496 qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
1498 qp_init_attr.init_attr.port_num = ctx->port;
1499 qp_init_attr.init_attr.qp_context = ctx;
1500 qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
1501 tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
1502 if (IS_ERR(tun_qp->qp)) {
1503 ret = PTR_ERR(tun_qp->qp);
1505 pr_err("Couldn't create %s QP (%d)\n",
1506 create_tun ? "tunnel" : "special", ret);
1510 memset(&attr, 0, sizeof attr);
1511 attr.qp_state = IB_QPS_INIT;
1513 to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
1514 attr.qkey = IB_QP1_QKEY;
1515 attr.port_num = ctx->port;
1516 ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
1518 pr_err("Couldn't change %s qp state to INIT (%d)\n",
1519 create_tun ? "tunnel" : "special", ret);
1522 attr.qp_state = IB_QPS_RTR;
1523 ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
1525 pr_err("Couldn't change %s qp state to RTR (%d)\n",
1526 create_tun ? "tunnel" : "special", ret);
1529 attr.qp_state = IB_QPS_RTS;
1531 ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
1533 pr_err("Couldn't change %s qp state to RTS (%d)\n",
1534 create_tun ? "tunnel" : "special", ret);
1538 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1539 ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
1541 pr_err(" mlx4_ib_post_pv_buf error"
1542 " (err = %d, i = %d)\n", ret, i);
1549 ib_destroy_qp(tun_qp->qp);
1555 * IB MAD completion callback for real SQPs
1557 static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
1559 struct mlx4_ib_demux_pv_ctx *ctx;
1560 struct mlx4_ib_demux_pv_qp *sqp;
1565 ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
1566 ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1568 while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
1569 sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
1570 if (wc.status == IB_WC_SUCCESS) {
1571 switch (wc.opcode) {
1573 ib_destroy_ah(sqp->tx_ring[wc.wr_id &
1574 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1575 sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1577 spin_lock(&sqp->tx_lock);
1579 spin_unlock(&sqp->tx_lock);
1582 mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
1583 (sqp->ring[wc.wr_id &
1584 (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->payload);
1585 grh = &(((struct mlx4_mad_rcv_buf *)
1586 (sqp->ring[wc.wr_id &
1587 (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->grh);
1588 mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
1589 if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
1590 (MLX4_NUM_TUNNEL_BUFS - 1)))
1591 pr_err("Failed reposting SQP "
1592 "buf:%lld\n", wc.wr_id);
1599 pr_debug("mlx4_ib: completion error in tunnel: %d."
1600 " status = %d, wrid = 0x%llx\n",
1601 ctx->slave, wc.status, wc.wr_id);
1602 if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
1603 ib_destroy_ah(sqp->tx_ring[wc.wr_id &
1604 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1605 sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1607 spin_lock(&sqp->tx_lock);
1609 spin_unlock(&sqp->tx_lock);
1615 static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
1616 struct mlx4_ib_demux_pv_ctx **ret_ctx)
1618 struct mlx4_ib_demux_pv_ctx *ctx;
1621 ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
1623 pr_err("failed allocating pv resource context "
1624 "for port %d, slave %d\n", port, slave);
1628 ctx->ib_dev = &dev->ib_dev;
1635 static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
1637 if (dev->sriov.demux[port - 1].tun[slave]) {
1638 kfree(dev->sriov.demux[port - 1].tun[slave]);
1639 dev->sriov.demux[port - 1].tun[slave] = NULL;
1643 static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
1644 int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
1648 ctx->state = DEMUX_PV_STATE_STARTING;
1649 /* have QP0 only on port owner, and only if link layer is IB */
1650 if (ctx->slave == mlx4_master_func_num(to_mdev(ctx->ib_dev)->dev) &&
1651 rdma_port_get_link_layer(ibdev, ctx->port) == IB_LINK_LAYER_INFINIBAND)
1655 ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
1657 pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
1662 ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
1664 pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
1668 cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
1672 ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
1673 NULL, ctx, cq_size, 0);
1674 if (IS_ERR(ctx->cq)) {
1675 ret = PTR_ERR(ctx->cq);
1676 pr_err("Couldn't create tunnel CQ (%d)\n", ret);
1680 ctx->pd = ib_alloc_pd(ctx->ib_dev);
1681 if (IS_ERR(ctx->pd)) {
1682 ret = PTR_ERR(ctx->pd);
1683 pr_err("Couldn't create tunnel PD (%d)\n", ret);
1687 ctx->mr = ib_get_dma_mr(ctx->pd, IB_ACCESS_LOCAL_WRITE);
1688 if (IS_ERR(ctx->mr)) {
1689 ret = PTR_ERR(ctx->mr);
1690 pr_err("Couldn't get tunnel DMA MR (%d)\n", ret);
1695 ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
1697 pr_err("Couldn't create %s QP0 (%d)\n",
1698 create_tun ? "tunnel for" : "", ret);
1703 ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
1705 pr_err("Couldn't create %s QP1 (%d)\n",
1706 create_tun ? "tunnel for" : "", ret);
1711 INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
1713 INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
1715 ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
1717 ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1719 pr_err("Couldn't arm tunnel cq (%d)\n", ret);
1722 ctx->state = DEMUX_PV_STATE_ACTIVE;
1727 ib_destroy_qp(ctx->qp[1].qp);
1728 ctx->qp[1].qp = NULL;
1733 ib_destroy_qp(ctx->qp[0].qp);
1734 ctx->qp[0].qp = NULL;
1737 ib_dereg_mr(ctx->mr);
1741 ib_dealloc_pd(ctx->pd);
1745 ib_destroy_cq(ctx->cq);
1749 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
1753 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
1755 ctx->state = DEMUX_PV_STATE_DOWN;
1759 static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
1760 struct mlx4_ib_demux_pv_ctx *ctx, int flush)
1764 if (ctx->state > DEMUX_PV_STATE_DOWN) {
1765 ctx->state = DEMUX_PV_STATE_DOWNING;
1767 flush_workqueue(ctx->wq);
1769 ib_destroy_qp(ctx->qp[0].qp);
1770 ctx->qp[0].qp = NULL;
1771 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
1773 ib_destroy_qp(ctx->qp[1].qp);
1774 ctx->qp[1].qp = NULL;
1775 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
1776 ib_dereg_mr(ctx->mr);
1778 ib_dealloc_pd(ctx->pd);
1780 ib_destroy_cq(ctx->cq);
1782 ctx->state = DEMUX_PV_STATE_DOWN;
1786 static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
1787 int port, int do_init)
1792 clean_vf_mcast(&dev->sriov.demux[port - 1], slave);
1793 /* for master, destroy real sqp resources */
1794 if (slave == mlx4_master_func_num(dev->dev))
1795 destroy_pv_resources(dev, slave, port,
1796 dev->sriov.sqps[port - 1], 1);
1797 /* destroy the tunnel qp resources */
1798 destroy_pv_resources(dev, slave, port,
1799 dev->sriov.demux[port - 1].tun[slave], 1);
1803 /* create the tunnel qp resources */
1804 ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
1805 dev->sriov.demux[port - 1].tun[slave]);
1807 /* for master, create the real sqp resources */
1808 if (!ret && slave == mlx4_master_func_num(dev->dev))
1809 ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
1810 dev->sriov.sqps[port - 1]);
1814 void mlx4_ib_tunnels_update_work(struct work_struct *work)
1816 struct mlx4_ib_demux_work *dmxw;
1818 dmxw = container_of(work, struct mlx4_ib_demux_work, work);
1819 mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
1825 static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
1826 struct mlx4_ib_demux_ctx *ctx,
1833 ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
1834 sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
1840 ctx->ib_dev = &dev->ib_dev;
1842 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1843 ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
1850 ret = mlx4_ib_mcg_port_init(ctx);
1852 pr_err("Failed initializing mcg para-virt (%d)\n", ret);
1856 snprintf(name, sizeof name, "mlx4_ibt%d", port);
1857 ctx->wq = create_singlethread_workqueue(name);
1859 pr_err("Failed to create tunnelling WQ for port %d\n", port);
1864 snprintf(name, sizeof name, "mlx4_ibud%d", port);
1865 ctx->ud_wq = create_singlethread_workqueue(name);
1867 pr_err("Failed to create up/down WQ for port %d\n", port);
1875 destroy_workqueue(ctx->wq);
1879 mlx4_ib_mcg_port_cleanup(ctx, 1);
1881 for (i = 0; i < dev->dev->caps.sqp_demux; i++)
1882 free_pv_object(dev, i, port);
1888 static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
1890 if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
1891 sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
1892 flush_workqueue(sqp_ctx->wq);
1893 if (sqp_ctx->has_smi) {
1894 ib_destroy_qp(sqp_ctx->qp[0].qp);
1895 sqp_ctx->qp[0].qp = NULL;
1896 mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
1898 ib_destroy_qp(sqp_ctx->qp[1].qp);
1899 sqp_ctx->qp[1].qp = NULL;
1900 mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
1901 ib_dereg_mr(sqp_ctx->mr);
1903 ib_dealloc_pd(sqp_ctx->pd);
1905 ib_destroy_cq(sqp_ctx->cq);
1907 sqp_ctx->state = DEMUX_PV_STATE_DOWN;
1911 static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
1915 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1916 mlx4_ib_mcg_port_cleanup(ctx, 1);
1917 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1920 if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
1921 ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
1923 flush_workqueue(ctx->wq);
1924 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1925 destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
1926 free_pv_object(dev, i, ctx->port);
1929 destroy_workqueue(ctx->ud_wq);
1930 destroy_workqueue(ctx->wq);
1934 static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
1938 if (!mlx4_is_master(dev->dev))
1940 /* initialize or tear down tunnel QPs for the master */
1941 for (i = 0; i < dev->dev->caps.num_ports; i++)
1942 mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
1946 int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
1951 if (!mlx4_is_mfunc(dev->dev))
1954 dev->sriov.is_going_down = 0;
1955 spin_lock_init(&dev->sriov.going_down_lock);
1956 mlx4_ib_cm_paravirt_init(dev);
1958 mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
1960 if (mlx4_is_slave(dev->dev)) {
1961 mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
1965 err = mlx4_ib_init_alias_guid_service(dev);
1967 mlx4_ib_warn(&dev->ib_dev, "Failed init alias guid process.\n");
1970 err = mlx4_ib_device_register_sysfs(dev);
1972 mlx4_ib_warn(&dev->ib_dev, "Failed to register sysfs\n");
1976 mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
1977 dev->dev->caps.sqp_demux);
1978 for (i = 0; i < dev->num_ports; i++) {
1980 err = __mlx4_ib_query_gid(&dev->ib_dev, i + 1, 0, &gid, 1);
1983 dev->sriov.demux[i].guid_cache[0] = gid.global.interface_id;
1984 err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
1985 &dev->sriov.sqps[i]);
1988 err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
1992 mlx4_ib_master_tunnels(dev, 1);
1997 free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
1998 mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
2001 mlx4_ib_device_unregister_sysfs(dev);
2004 mlx4_ib_destroy_alias_guid_service(dev);
2007 mlx4_ib_cm_paravirt_clean(dev, -1);
2012 void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
2015 unsigned long flags;
2017 if (!mlx4_is_mfunc(dev->dev))
2020 spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
2021 dev->sriov.is_going_down = 1;
2022 spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
2023 if (mlx4_is_master(dev->dev)) {
2024 for (i = 0; i < dev->num_ports; i++) {
2025 flush_workqueue(dev->sriov.demux[i].ud_wq);
2026 mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
2027 kfree(dev->sriov.sqps[i]);
2028 dev->sriov.sqps[i] = NULL;
2029 mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
2032 mlx4_ib_cm_paravirt_clean(dev, -1);
2033 mlx4_ib_destroy_alias_guid_service(dev);
2034 mlx4_ib_device_unregister_sysfs(dev);