1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <linux/slab.h>
40#include <linux/string.h>
41
42#include "agent.h"
43#include "smi.h"
44#include "mad_priv.h"
45
46#define SPFX "ib_agent: "
47
48struct ib_agent_port_private {
49 struct list_head port_list;
50 struct ib_mad_agent *agent[2];
51};
52
53static DEFINE_SPINLOCK(ib_agent_port_list_lock);
54static LIST_HEAD(ib_agent_port_list);
55
56static struct ib_agent_port_private *
57__ib_get_agent_port(const struct ib_device *device, int port_num)
58{
59 struct ib_agent_port_private *entry;
60
61 list_for_each_entry(entry, &ib_agent_port_list, port_list) {
62 if (entry->agent[1]->device == device &&
63 entry->agent[1]->port_num == port_num)
64 return entry;
65 }
66 return NULL;
67}
68
69static struct ib_agent_port_private *
70ib_get_agent_port(const struct ib_device *device, int port_num)
71{
72 struct ib_agent_port_private *entry;
73 unsigned long flags;
74
75 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
76 entry = __ib_get_agent_port(device, port_num);
77 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
78 return entry;
79}
80
81void agent_send_response(const struct ib_mad_hdr *mad_hdr, const struct ib_grh *grh,
82 const struct ib_wc *wc, const struct ib_device *device,
83 int port_num, int qpn, size_t resp_mad_len, bool opa)
84{
85 struct ib_agent_port_private *port_priv;
86 struct ib_mad_agent *agent;
87 struct ib_mad_send_buf *send_buf;
88 struct ib_ah *ah;
89 struct ib_mad_send_wr_private *mad_send_wr;
90
91 if (rdma_cap_ib_switch(device))
92 port_priv = ib_get_agent_port(device, 0);
93 else
94 port_priv = ib_get_agent_port(device, port_num);
95
96 if (!port_priv) {
97 dev_err(&device->dev, "Unable to find port agent\n");
98 return;
99 }
100
101 agent = port_priv->agent[qpn];
102 ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
103 if (IS_ERR(ah)) {
104 dev_err(&device->dev, "ib_create_ah_from_wc error %ld\n",
105 PTR_ERR(ah));
106 return;
107 }
108
109 if (opa && mad_hdr->base_version != OPA_MGMT_BASE_VERSION)
110 resp_mad_len = IB_MGMT_MAD_SIZE;
111
112 send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
113 IB_MGMT_MAD_HDR,
114 resp_mad_len - IB_MGMT_MAD_HDR,
115 GFP_KERNEL,
116 mad_hdr->base_version);
117 if (IS_ERR(send_buf)) {
118 dev_err(&device->dev, "ib_create_send_mad error\n");
119 goto err1;
120 }
121
122 memcpy(send_buf->mad, mad_hdr, resp_mad_len);
123 send_buf->ah = ah;
124
125 if (rdma_cap_ib_switch(device)) {
126 mad_send_wr = container_of(send_buf,
127 struct ib_mad_send_wr_private,
128 send_buf);
129 mad_send_wr->send_wr.wr.ud.port_num = port_num;
130 }
131
132 if (ib_post_send_mad(send_buf, NULL)) {
133 dev_err(&device->dev, "ib_post_send_mad error\n");
134 goto err2;
135 }
136 return;
137err2:
138 ib_free_send_mad(send_buf);
139err1:
140 ib_destroy_ah(ah);
141}
142
143static void agent_send_handler(struct ib_mad_agent *mad_agent,
144 struct ib_mad_send_wc *mad_send_wc)
145{
146 ib_destroy_ah(mad_send_wc->send_buf->ah);
147 ib_free_send_mad(mad_send_wc->send_buf);
148}
149
150int ib_agent_port_open(struct ib_device *device, int port_num)
151{
152 struct ib_agent_port_private *port_priv;
153 unsigned long flags;
154 int ret;
155
156
157 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
158 if (!port_priv) {
159 dev_err(&device->dev, "No memory for ib_agent_port_private\n");
160 ret = -ENOMEM;
161 goto error1;
162 }
163
164 if (rdma_cap_ib_smi(device, port_num)) {
165
166 port_priv->agent[0] = ib_register_mad_agent(device, port_num,
167 IB_QPT_SMI, NULL, 0,
168 &agent_send_handler,
169 NULL, NULL, 0);
170 if (IS_ERR(port_priv->agent[0])) {
171 ret = PTR_ERR(port_priv->agent[0]);
172 goto error2;
173 }
174 }
175
176
177 port_priv->agent[1] = ib_register_mad_agent(device, port_num,
178 IB_QPT_GSI, NULL, 0,
179 &agent_send_handler,
180 NULL, NULL, 0);
181 if (IS_ERR(port_priv->agent[1])) {
182 ret = PTR_ERR(port_priv->agent[1]);
183 goto error3;
184 }
185
186 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
187 list_add_tail(&port_priv->port_list, &ib_agent_port_list);
188 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
189
190 return 0;
191
192error3:
193 if (port_priv->agent[0])
194 ib_unregister_mad_agent(port_priv->agent[0]);
195error2:
196 kfree(port_priv);
197error1:
198 return ret;
199}
200
201int ib_agent_port_close(struct ib_device *device, int port_num)
202{
203 struct ib_agent_port_private *port_priv;
204 unsigned long flags;
205
206 spin_lock_irqsave(&ib_agent_port_list_lock, flags);
207 port_priv = __ib_get_agent_port(device, port_num);
208 if (port_priv == NULL) {
209 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
210 dev_err(&device->dev, "Port %d not found\n", port_num);
211 return -ENODEV;
212 }
213 list_del(&port_priv->port_list);
214 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
215
216 ib_unregister_mad_agent(port_priv->agent[1]);
217 if (port_priv->agent[0])
218 ib_unregister_mad_agent(port_priv->agent[0]);
219
220 kfree(port_priv);
221 return 0;
222}
223