162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2004, 2005 Mellanox Technologies Ltd.  All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2004, 2005 Infinicon Corporation.  All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2004, 2005 Intel Corporation.  All rights reserved.
562306a36Sopenharmony_ci * Copyright (c) 2004, 2005 Topspin Corporation.  All rights reserved.
662306a36Sopenharmony_ci * Copyright (c) 2004-2007 Voltaire Corporation.  All rights reserved.
762306a36Sopenharmony_ci * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This software is available to you under a choice of one of two
1062306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
1162306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
1262306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1362306a36Sopenharmony_ci * OpenIB.org BSD license below:
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1662306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1762306a36Sopenharmony_ci *     conditions are met:
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
2062306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2162306a36Sopenharmony_ci *        disclaimer.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2462306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2562306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2662306a36Sopenharmony_ci *        provided with the distribution.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2962306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3062306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3162306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
3262306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3362306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3462306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3562306a36Sopenharmony_ci * SOFTWARE.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci */
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#include <linux/slab.h>
4062306a36Sopenharmony_ci#include <linux/string.h>
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#include "agent.h"
4362306a36Sopenharmony_ci#include "smi.h"
4462306a36Sopenharmony_ci#include "mad_priv.h"
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define SPFX "ib_agent: "
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistruct ib_agent_port_private {
4962306a36Sopenharmony_ci	struct list_head port_list;
5062306a36Sopenharmony_ci	struct ib_mad_agent *agent[2];
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic DEFINE_SPINLOCK(ib_agent_port_list_lock);
5462306a36Sopenharmony_cistatic LIST_HEAD(ib_agent_port_list);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic struct ib_agent_port_private *
5762306a36Sopenharmony_ci__ib_get_agent_port(const struct ib_device *device, int port_num)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	struct ib_agent_port_private *entry;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	list_for_each_entry(entry, &ib_agent_port_list, port_list) {
6262306a36Sopenharmony_ci		if (entry->agent[1]->device == device &&
6362306a36Sopenharmony_ci		    entry->agent[1]->port_num == port_num)
6462306a36Sopenharmony_ci			return entry;
6562306a36Sopenharmony_ci	}
6662306a36Sopenharmony_ci	return NULL;
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic struct ib_agent_port_private *
7062306a36Sopenharmony_ciib_get_agent_port(const struct ib_device *device, int port_num)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	struct ib_agent_port_private *entry;
7362306a36Sopenharmony_ci	unsigned long flags;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	spin_lock_irqsave(&ib_agent_port_list_lock, flags);
7662306a36Sopenharmony_ci	entry = __ib_get_agent_port(device, port_num);
7762306a36Sopenharmony_ci	spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
7862306a36Sopenharmony_ci	return entry;
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_civoid agent_send_response(const struct ib_mad_hdr *mad_hdr, const struct ib_grh *grh,
8262306a36Sopenharmony_ci			 const struct ib_wc *wc, const struct ib_device *device,
8362306a36Sopenharmony_ci			 int port_num, int qpn, size_t resp_mad_len, bool opa)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	struct ib_agent_port_private *port_priv;
8662306a36Sopenharmony_ci	struct ib_mad_agent *agent;
8762306a36Sopenharmony_ci	struct ib_mad_send_buf *send_buf;
8862306a36Sopenharmony_ci	struct ib_ah *ah;
8962306a36Sopenharmony_ci	struct ib_mad_send_wr_private *mad_send_wr;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	if (rdma_cap_ib_switch(device))
9262306a36Sopenharmony_ci		port_priv = ib_get_agent_port(device, 0);
9362306a36Sopenharmony_ci	else
9462306a36Sopenharmony_ci		port_priv = ib_get_agent_port(device, port_num);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	if (!port_priv) {
9762306a36Sopenharmony_ci		dev_err(&device->dev, "Unable to find port agent\n");
9862306a36Sopenharmony_ci		return;
9962306a36Sopenharmony_ci	}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	agent = port_priv->agent[qpn];
10262306a36Sopenharmony_ci	ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
10362306a36Sopenharmony_ci	if (IS_ERR(ah)) {
10462306a36Sopenharmony_ci		dev_err(&device->dev, "ib_create_ah_from_wc error %ld\n",
10562306a36Sopenharmony_ci			PTR_ERR(ah));
10662306a36Sopenharmony_ci		return;
10762306a36Sopenharmony_ci	}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	if (opa && mad_hdr->base_version != OPA_MGMT_BASE_VERSION)
11062306a36Sopenharmony_ci		resp_mad_len = IB_MGMT_MAD_SIZE;
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
11362306a36Sopenharmony_ci				      IB_MGMT_MAD_HDR,
11462306a36Sopenharmony_ci				      resp_mad_len - IB_MGMT_MAD_HDR,
11562306a36Sopenharmony_ci				      GFP_KERNEL,
11662306a36Sopenharmony_ci				      mad_hdr->base_version);
11762306a36Sopenharmony_ci	if (IS_ERR(send_buf)) {
11862306a36Sopenharmony_ci		dev_err(&device->dev, "ib_create_send_mad error\n");
11962306a36Sopenharmony_ci		goto err1;
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	memcpy(send_buf->mad, mad_hdr, resp_mad_len);
12362306a36Sopenharmony_ci	send_buf->ah = ah;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	if (rdma_cap_ib_switch(device)) {
12662306a36Sopenharmony_ci		mad_send_wr = container_of(send_buf,
12762306a36Sopenharmony_ci					   struct ib_mad_send_wr_private,
12862306a36Sopenharmony_ci					   send_buf);
12962306a36Sopenharmony_ci		mad_send_wr->send_wr.port_num = port_num;
13062306a36Sopenharmony_ci	}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	if (ib_post_send_mad(send_buf, NULL)) {
13362306a36Sopenharmony_ci		dev_err(&device->dev, "ib_post_send_mad error\n");
13462306a36Sopenharmony_ci		goto err2;
13562306a36Sopenharmony_ci	}
13662306a36Sopenharmony_ci	return;
13762306a36Sopenharmony_cierr2:
13862306a36Sopenharmony_ci	ib_free_send_mad(send_buf);
13962306a36Sopenharmony_cierr1:
14062306a36Sopenharmony_ci	rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cistatic void agent_send_handler(struct ib_mad_agent *mad_agent,
14462306a36Sopenharmony_ci			       struct ib_mad_send_wc *mad_send_wc)
14562306a36Sopenharmony_ci{
14662306a36Sopenharmony_ci	rdma_destroy_ah(mad_send_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE);
14762306a36Sopenharmony_ci	ib_free_send_mad(mad_send_wc->send_buf);
14862306a36Sopenharmony_ci}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciint ib_agent_port_open(struct ib_device *device, int port_num)
15162306a36Sopenharmony_ci{
15262306a36Sopenharmony_ci	struct ib_agent_port_private *port_priv;
15362306a36Sopenharmony_ci	unsigned long flags;
15462306a36Sopenharmony_ci	int ret;
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	/* Create new device info */
15762306a36Sopenharmony_ci	port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
15862306a36Sopenharmony_ci	if (!port_priv) {
15962306a36Sopenharmony_ci		ret = -ENOMEM;
16062306a36Sopenharmony_ci		goto error1;
16162306a36Sopenharmony_ci	}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	if (rdma_cap_ib_smi(device, port_num)) {
16462306a36Sopenharmony_ci		/* Obtain send only MAD agent for SMI QP */
16562306a36Sopenharmony_ci		port_priv->agent[0] = ib_register_mad_agent(device, port_num,
16662306a36Sopenharmony_ci							    IB_QPT_SMI, NULL, 0,
16762306a36Sopenharmony_ci							    &agent_send_handler,
16862306a36Sopenharmony_ci							    NULL, NULL, 0);
16962306a36Sopenharmony_ci		if (IS_ERR(port_priv->agent[0])) {
17062306a36Sopenharmony_ci			ret = PTR_ERR(port_priv->agent[0]);
17162306a36Sopenharmony_ci			goto error2;
17262306a36Sopenharmony_ci		}
17362306a36Sopenharmony_ci	}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	/* Obtain send only MAD agent for GSI QP */
17662306a36Sopenharmony_ci	port_priv->agent[1] = ib_register_mad_agent(device, port_num,
17762306a36Sopenharmony_ci						    IB_QPT_GSI, NULL, 0,
17862306a36Sopenharmony_ci						    &agent_send_handler,
17962306a36Sopenharmony_ci						    NULL, NULL, 0);
18062306a36Sopenharmony_ci	if (IS_ERR(port_priv->agent[1])) {
18162306a36Sopenharmony_ci		ret = PTR_ERR(port_priv->agent[1]);
18262306a36Sopenharmony_ci		goto error3;
18362306a36Sopenharmony_ci	}
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	spin_lock_irqsave(&ib_agent_port_list_lock, flags);
18662306a36Sopenharmony_ci	list_add_tail(&port_priv->port_list, &ib_agent_port_list);
18762306a36Sopenharmony_ci	spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	return 0;
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_cierror3:
19262306a36Sopenharmony_ci	if (port_priv->agent[0])
19362306a36Sopenharmony_ci		ib_unregister_mad_agent(port_priv->agent[0]);
19462306a36Sopenharmony_cierror2:
19562306a36Sopenharmony_ci	kfree(port_priv);
19662306a36Sopenharmony_cierror1:
19762306a36Sopenharmony_ci	return ret;
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ciint ib_agent_port_close(struct ib_device *device, int port_num)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci	struct ib_agent_port_private *port_priv;
20362306a36Sopenharmony_ci	unsigned long flags;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	spin_lock_irqsave(&ib_agent_port_list_lock, flags);
20662306a36Sopenharmony_ci	port_priv = __ib_get_agent_port(device, port_num);
20762306a36Sopenharmony_ci	if (port_priv == NULL) {
20862306a36Sopenharmony_ci		spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
20962306a36Sopenharmony_ci		dev_err(&device->dev, "Port %d not found\n", port_num);
21062306a36Sopenharmony_ci		return -ENODEV;
21162306a36Sopenharmony_ci	}
21262306a36Sopenharmony_ci	list_del(&port_priv->port_list);
21362306a36Sopenharmony_ci	spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	ib_unregister_mad_agent(port_priv->agent[1]);
21662306a36Sopenharmony_ci	if (port_priv->agent[0])
21762306a36Sopenharmony_ci		ib_unregister_mad_agent(port_priv->agent[0]);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	kfree(port_priv);
22062306a36Sopenharmony_ci	return 0;
22162306a36Sopenharmony_ci}
222