]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
de493d47 | 2 | * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved. |
fa619a77 HR |
3 | * Copyright (c) 2005 Intel Corporation. All rights reserved. |
4 | * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved. | |
b76aabc3 | 5 | * Copyright (c) 2009 HNR Consulting. All rights reserved. |
1da177e4 LT |
6 | * |
7 | * This software is available to you under a choice of one of two | |
8 | * licenses. You may choose to be licensed under the terms of the GNU | |
9 | * General Public License (GPL) Version 2, available from the file | |
10 | * COPYING in the main directory of this source tree, or the | |
11 | * OpenIB.org BSD license below: | |
12 | * | |
13 | * Redistribution and use in source and binary forms, with or | |
14 | * without modification, are permitted provided that the following | |
15 | * conditions are met: | |
16 | * | |
17 | * - Redistributions of source code must retain the above | |
18 | * copyright notice, this list of conditions and the following | |
19 | * disclaimer. | |
20 | * | |
21 | * - Redistributions in binary form must reproduce the above | |
22 | * copyright notice, this list of conditions and the following | |
23 | * disclaimer in the documentation and/or other materials | |
24 | * provided with the distribution. | |
25 | * | |
26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
27 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
28 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
29 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
30 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
31 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
32 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
33 | * SOFTWARE. | |
34 | * | |
1da177e4 | 35 | */ |
1da177e4 | 36 | #include <linux/dma-mapping.h> |
5a0e3ad6 | 37 | #include <linux/slab.h> |
9874e746 | 38 | #include <rdma/ib_cache.h> |
1da177e4 LT |
39 | |
40 | #include "mad_priv.h" | |
fa619a77 | 41 | #include "mad_rmpp.h" |
1da177e4 LT |
42 | #include "smi.h" |
43 | #include "agent.h" | |
44 | ||
45 | MODULE_LICENSE("Dual BSD/GPL"); | |
46 | MODULE_DESCRIPTION("kernel IB MAD API"); | |
47 | MODULE_AUTHOR("Hal Rosenstock"); | |
48 | MODULE_AUTHOR("Sean Hefty"); | |
49 | ||
16933955 RD |
50 | static int mad_sendq_size = IB_MAD_QP_SEND_SIZE; |
51 | static int mad_recvq_size = IB_MAD_QP_RECV_SIZE; | |
b76aabc3 HR |
52 | |
53 | module_param_named(send_queue_size, mad_sendq_size, int, 0444); | |
54 | MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests"); | |
55 | module_param_named(recv_queue_size, mad_recvq_size, int, 0444); | |
56 | MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests"); | |
57 | ||
e54f8188 | 58 | static struct kmem_cache *ib_mad_cache; |
fa619a77 | 59 | |
1da177e4 LT |
60 | static struct list_head ib_mad_port_list; |
61 | static u32 ib_mad_client_id = 0; | |
62 | ||
63 | /* Port list lock */ | |
6276e08a | 64 | static DEFINE_SPINLOCK(ib_mad_port_list_lock); |
1da177e4 LT |
65 | |
66 | /* Forward declarations */ | |
67 | static int method_in_use(struct ib_mad_mgmt_method_table **method, | |
68 | struct ib_mad_reg_req *mad_reg_req); | |
69 | static void remove_mad_reg_req(struct ib_mad_agent_private *priv); | |
70 | static struct ib_mad_agent_private *find_mad_agent( | |
71 | struct ib_mad_port_private *port_priv, | |
4a0754fa | 72 | struct ib_mad *mad); |
1da177e4 LT |
73 | static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info, |
74 | struct ib_mad_private *mad); | |
75 | static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv); | |
c4028958 DH |
76 | static void timeout_sends(struct work_struct *work); |
77 | static void local_completions(struct work_struct *work); | |
1da177e4 LT |
78 | static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req, |
79 | struct ib_mad_agent_private *agent_priv, | |
80 | u8 mgmt_class); | |
81 | static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req, | |
82 | struct ib_mad_agent_private *agent_priv); | |
83 | ||
84 | /* | |
85 | * Returns a ib_mad_port_private structure or NULL for a device/port | |
86 | * Assumes ib_mad_port_list_lock is being held | |
87 | */ | |
88 | static inline struct ib_mad_port_private * | |
89 | __ib_get_mad_port(struct ib_device *device, int port_num) | |
90 | { | |
91 | struct ib_mad_port_private *entry; | |
92 | ||
93 | list_for_each_entry(entry, &ib_mad_port_list, port_list) { | |
94 | if (entry->device == device && entry->port_num == port_num) | |
95 | return entry; | |
96 | } | |
97 | return NULL; | |
98 | } | |
99 | ||
100 | /* | |
101 | * Wrapper function to return a ib_mad_port_private structure or NULL | |
102 | * for a device/port | |
103 | */ | |
104 | static inline struct ib_mad_port_private * | |
105 | ib_get_mad_port(struct ib_device *device, int port_num) | |
106 | { | |
107 | struct ib_mad_port_private *entry; | |
108 | unsigned long flags; | |
109 | ||
110 | spin_lock_irqsave(&ib_mad_port_list_lock, flags); | |
111 | entry = __ib_get_mad_port(device, port_num); | |
112 | spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); | |
113 | ||
114 | return entry; | |
115 | } | |
116 | ||
117 | static inline u8 convert_mgmt_class(u8 mgmt_class) | |
118 | { | |
119 | /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */ | |
120 | return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ? | |
121 | 0 : mgmt_class; | |
122 | } | |
123 | ||
124 | static int get_spl_qp_index(enum ib_qp_type qp_type) | |
125 | { | |
126 | switch (qp_type) | |
127 | { | |
128 | case IB_QPT_SMI: | |
129 | return 0; | |
130 | case IB_QPT_GSI: | |
131 | return 1; | |
132 | default: | |
133 | return -1; | |
134 | } | |
135 | } | |
136 | ||
137 | static int vendor_class_index(u8 mgmt_class) | |
138 | { | |
139 | return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START; | |
140 | } | |
141 | ||
142 | static int is_vendor_class(u8 mgmt_class) | |
143 | { | |
144 | if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) || | |
145 | (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END)) | |
146 | return 0; | |
147 | return 1; | |
148 | } | |
149 | ||
150 | static int is_vendor_oui(char *oui) | |
151 | { | |
152 | if (oui[0] || oui[1] || oui[2]) | |
153 | return 1; | |
154 | return 0; | |
155 | } | |
156 | ||
157 | static int is_vendor_method_in_use( | |
158 | struct ib_mad_mgmt_vendor_class *vendor_class, | |
159 | struct ib_mad_reg_req *mad_reg_req) | |
160 | { | |
161 | struct ib_mad_mgmt_method_table *method; | |
162 | int i; | |
163 | ||
164 | for (i = 0; i < MAX_MGMT_OUI; i++) { | |
165 | if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) { | |
166 | method = vendor_class->method_table[i]; | |
167 | if (method) { | |
168 | if (method_in_use(&method, mad_reg_req)) | |
169 | return 1; | |
170 | else | |
171 | break; | |
172 | } | |
173 | } | |
174 | } | |
175 | return 0; | |
176 | } | |
177 | ||
2527e681 SH |
178 | int ib_response_mad(struct ib_mad *mad) |
179 | { | |
180 | return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) || | |
181 | (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) || | |
182 | ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) && | |
183 | (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP))); | |
184 | } | |
185 | EXPORT_SYMBOL(ib_response_mad); | |
186 | ||
1da177e4 LT |
187 | /* |
188 | * ib_register_mad_agent - Register to send/receive MADs | |
189 | */ | |
190 | struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device, | |
191 | u8 port_num, | |
192 | enum ib_qp_type qp_type, | |
193 | struct ib_mad_reg_req *mad_reg_req, | |
194 | u8 rmpp_version, | |
195 | ib_mad_send_handler send_handler, | |
196 | ib_mad_recv_handler recv_handler, | |
197 | void *context) | |
198 | { | |
199 | struct ib_mad_port_private *port_priv; | |
200 | struct ib_mad_agent *ret = ERR_PTR(-EINVAL); | |
201 | struct ib_mad_agent_private *mad_agent_priv; | |
202 | struct ib_mad_reg_req *reg_req = NULL; | |
203 | struct ib_mad_mgmt_class_table *class; | |
204 | struct ib_mad_mgmt_vendor_class_table *vendor; | |
205 | struct ib_mad_mgmt_vendor_class *vendor_class; | |
206 | struct ib_mad_mgmt_method_table *method; | |
207 | int ret2, qpn; | |
208 | unsigned long flags; | |
209 | u8 mgmt_class, vclass; | |
210 | ||
211 | /* Validate parameters */ | |
212 | qpn = get_spl_qp_index(qp_type); | |
213 | if (qpn == -1) | |
214 | goto error1; | |
215 | ||
fa619a77 HR |
216 | if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) |
217 | goto error1; | |
1da177e4 LT |
218 | |
219 | /* Validate MAD registration request if supplied */ | |
220 | if (mad_reg_req) { | |
221 | if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION) | |
222 | goto error1; | |
223 | if (!recv_handler) | |
224 | goto error1; | |
225 | if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) { | |
226 | /* | |
227 | * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only | |
228 | * one in this range currently allowed | |
229 | */ | |
230 | if (mad_reg_req->mgmt_class != | |
231 | IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) | |
232 | goto error1; | |
233 | } else if (mad_reg_req->mgmt_class == 0) { | |
234 | /* | |
235 | * Class 0 is reserved in IBA and is used for | |
236 | * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE | |
237 | */ | |
238 | goto error1; | |
239 | } else if (is_vendor_class(mad_reg_req->mgmt_class)) { | |
240 | /* | |
241 | * If class is in "new" vendor range, | |
242 | * ensure supplied OUI is not zero | |
243 | */ | |
244 | if (!is_vendor_oui(mad_reg_req->oui)) | |
245 | goto error1; | |
246 | } | |
618a3c03 | 247 | /* Make sure class supplied is consistent with RMPP */ |
64cb9c6a | 248 | if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) { |
618a3c03 HR |
249 | if (rmpp_version) |
250 | goto error1; | |
251 | } | |
1da177e4 LT |
252 | /* Make sure class supplied is consistent with QP type */ |
253 | if (qp_type == IB_QPT_SMI) { | |
254 | if ((mad_reg_req->mgmt_class != | |
255 | IB_MGMT_CLASS_SUBN_LID_ROUTED) && | |
256 | (mad_reg_req->mgmt_class != | |
257 | IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) | |
258 | goto error1; | |
259 | } else { | |
260 | if ((mad_reg_req->mgmt_class == | |
261 | IB_MGMT_CLASS_SUBN_LID_ROUTED) || | |
262 | (mad_reg_req->mgmt_class == | |
263 | IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) | |
264 | goto error1; | |
265 | } | |
266 | } else { | |
267 | /* No registration request supplied */ | |
268 | if (!send_handler) | |
269 | goto error1; | |
270 | } | |
271 | ||
272 | /* Validate device and port */ | |
273 | port_priv = ib_get_mad_port(device, port_num); | |
274 | if (!port_priv) { | |
275 | ret = ERR_PTR(-ENODEV); | |
276 | goto error1; | |
277 | } | |
278 | ||
279 | /* Allocate structures */ | |
de6eb66b | 280 | mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL); |
1da177e4 LT |
281 | if (!mad_agent_priv) { |
282 | ret = ERR_PTR(-ENOMEM); | |
283 | goto error1; | |
284 | } | |
b82cab6b HR |
285 | |
286 | mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd, | |
287 | IB_ACCESS_LOCAL_WRITE); | |
288 | if (IS_ERR(mad_agent_priv->agent.mr)) { | |
289 | ret = ERR_PTR(-ENOMEM); | |
290 | goto error2; | |
291 | } | |
1da177e4 LT |
292 | |
293 | if (mad_reg_req) { | |
9893e742 | 294 | reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL); |
1da177e4 LT |
295 | if (!reg_req) { |
296 | ret = ERR_PTR(-ENOMEM); | |
b82cab6b | 297 | goto error3; |
1da177e4 | 298 | } |
1da177e4 LT |
299 | } |
300 | ||
301 | /* Now, fill in the various structures */ | |
1da177e4 LT |
302 | mad_agent_priv->qp_info = &port_priv->qp_info[qpn]; |
303 | mad_agent_priv->reg_req = reg_req; | |
fa619a77 | 304 | mad_agent_priv->agent.rmpp_version = rmpp_version; |
1da177e4 LT |
305 | mad_agent_priv->agent.device = device; |
306 | mad_agent_priv->agent.recv_handler = recv_handler; | |
307 | mad_agent_priv->agent.send_handler = send_handler; | |
308 | mad_agent_priv->agent.context = context; | |
309 | mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp; | |
310 | mad_agent_priv->agent.port_num = port_num; | |
d9620a4c RC |
311 | spin_lock_init(&mad_agent_priv->lock); |
312 | INIT_LIST_HEAD(&mad_agent_priv->send_list); | |
313 | INIT_LIST_HEAD(&mad_agent_priv->wait_list); | |
314 | INIT_LIST_HEAD(&mad_agent_priv->done_list); | |
315 | INIT_LIST_HEAD(&mad_agent_priv->rmpp_list); | |
316 | INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends); | |
317 | INIT_LIST_HEAD(&mad_agent_priv->local_list); | |
318 | INIT_WORK(&mad_agent_priv->local_work, local_completions); | |
319 | atomic_set(&mad_agent_priv->refcount, 1); | |
320 | init_completion(&mad_agent_priv->comp); | |
1da177e4 LT |
321 | |
322 | spin_lock_irqsave(&port_priv->reg_lock, flags); | |
323 | mad_agent_priv->agent.hi_tid = ++ib_mad_client_id; | |
324 | ||
325 | /* | |
326 | * Make sure MAD registration (if supplied) | |
327 | * is non overlapping with any existing ones | |
328 | */ | |
329 | if (mad_reg_req) { | |
330 | mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class); | |
331 | if (!is_vendor_class(mgmt_class)) { | |
332 | class = port_priv->version[mad_reg_req-> | |
333 | mgmt_class_version].class; | |
334 | if (class) { | |
335 | method = class->method_table[mgmt_class]; | |
336 | if (method) { | |
337 | if (method_in_use(&method, | |
338 | mad_reg_req)) | |
b82cab6b | 339 | goto error4; |
1da177e4 LT |
340 | } |
341 | } | |
342 | ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv, | |
343 | mgmt_class); | |
344 | } else { | |
345 | /* "New" vendor class range */ | |
346 | vendor = port_priv->version[mad_reg_req-> | |
347 | mgmt_class_version].vendor; | |
348 | if (vendor) { | |
349 | vclass = vendor_class_index(mgmt_class); | |
350 | vendor_class = vendor->vendor_class[vclass]; | |
351 | if (vendor_class) { | |
352 | if (is_vendor_method_in_use( | |
353 | vendor_class, | |
354 | mad_reg_req)) | |
b82cab6b | 355 | goto error4; |
1da177e4 LT |
356 | } |
357 | } | |
358 | ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv); | |
359 | } | |
360 | if (ret2) { | |
361 | ret = ERR_PTR(ret2); | |
b82cab6b | 362 | goto error4; |
1da177e4 LT |
363 | } |
364 | } | |
365 | ||
366 | /* Add mad agent into port's agent list */ | |
367 | list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list); | |
368 | spin_unlock_irqrestore(&port_priv->reg_lock, flags); | |
369 | ||
1da177e4 LT |
370 | return &mad_agent_priv->agent; |
371 | ||
b82cab6b | 372 | error4: |
1da177e4 LT |
373 | spin_unlock_irqrestore(&port_priv->reg_lock, flags); |
374 | kfree(reg_req); | |
b82cab6b | 375 | error3: |
b82cab6b | 376 | ib_dereg_mr(mad_agent_priv->agent.mr); |
2012a116 AB |
377 | error2: |
378 | kfree(mad_agent_priv); | |
1da177e4 LT |
379 | error1: |
380 | return ret; | |
381 | } | |
382 | EXPORT_SYMBOL(ib_register_mad_agent); | |
383 | ||
384 | static inline int is_snooping_sends(int mad_snoop_flags) | |
385 | { | |
386 | return (mad_snoop_flags & | |
387 | (/*IB_MAD_SNOOP_POSTED_SENDS | | |
388 | IB_MAD_SNOOP_RMPP_SENDS |*/ | |
389 | IB_MAD_SNOOP_SEND_COMPLETIONS /*| | |
390 | IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/)); | |
391 | } | |
392 | ||
393 | static inline int is_snooping_recvs(int mad_snoop_flags) | |
394 | { | |
395 | return (mad_snoop_flags & | |
396 | (IB_MAD_SNOOP_RECVS /*| | |
397 | IB_MAD_SNOOP_RMPP_RECVS*/)); | |
398 | } | |
399 | ||
400 | static int register_snoop_agent(struct ib_mad_qp_info *qp_info, | |
401 | struct ib_mad_snoop_private *mad_snoop_priv) | |
402 | { | |
403 | struct ib_mad_snoop_private **new_snoop_table; | |
404 | unsigned long flags; | |
405 | int i; | |
406 | ||
407 | spin_lock_irqsave(&qp_info->snoop_lock, flags); | |
408 | /* Check for empty slot in array. */ | |
409 | for (i = 0; i < qp_info->snoop_table_size; i++) | |
410 | if (!qp_info->snoop_table[i]) | |
411 | break; | |
412 | ||
413 | if (i == qp_info->snoop_table_size) { | |
414 | /* Grow table. */ | |
52805174 RD |
415 | new_snoop_table = krealloc(qp_info->snoop_table, |
416 | sizeof mad_snoop_priv * | |
417 | (qp_info->snoop_table_size + 1), | |
418 | GFP_ATOMIC); | |
1da177e4 LT |
419 | if (!new_snoop_table) { |
420 | i = -ENOMEM; | |
421 | goto out; | |
422 | } | |
52805174 | 423 | |
1da177e4 LT |
424 | qp_info->snoop_table = new_snoop_table; |
425 | qp_info->snoop_table_size++; | |
426 | } | |
427 | qp_info->snoop_table[i] = mad_snoop_priv; | |
428 | atomic_inc(&qp_info->snoop_count); | |
429 | out: | |
430 | spin_unlock_irqrestore(&qp_info->snoop_lock, flags); | |
431 | return i; | |
432 | } | |
433 | ||
434 | struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device, | |
435 | u8 port_num, | |
436 | enum ib_qp_type qp_type, | |
437 | int mad_snoop_flags, | |
438 | ib_mad_snoop_handler snoop_handler, | |
439 | ib_mad_recv_handler recv_handler, | |
440 | void *context) | |
441 | { | |
442 | struct ib_mad_port_private *port_priv; | |
443 | struct ib_mad_agent *ret; | |
444 | struct ib_mad_snoop_private *mad_snoop_priv; | |
445 | int qpn; | |
446 | ||
447 | /* Validate parameters */ | |
448 | if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) || | |
449 | (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) { | |
450 | ret = ERR_PTR(-EINVAL); | |
451 | goto error1; | |
452 | } | |
453 | qpn = get_spl_qp_index(qp_type); | |
454 | if (qpn == -1) { | |
455 | ret = ERR_PTR(-EINVAL); | |
456 | goto error1; | |
457 | } | |
458 | port_priv = ib_get_mad_port(device, port_num); | |
459 | if (!port_priv) { | |
460 | ret = ERR_PTR(-ENODEV); | |
461 | goto error1; | |
462 | } | |
463 | /* Allocate structures */ | |
de6eb66b | 464 | mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL); |
1da177e4 LT |
465 | if (!mad_snoop_priv) { |
466 | ret = ERR_PTR(-ENOMEM); | |
467 | goto error1; | |
468 | } | |
469 | ||
470 | /* Now, fill in the various structures */ | |
1da177e4 LT |
471 | mad_snoop_priv->qp_info = &port_priv->qp_info[qpn]; |
472 | mad_snoop_priv->agent.device = device; | |
473 | mad_snoop_priv->agent.recv_handler = recv_handler; | |
474 | mad_snoop_priv->agent.snoop_handler = snoop_handler; | |
475 | mad_snoop_priv->agent.context = context; | |
476 | mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp; | |
477 | mad_snoop_priv->agent.port_num = port_num; | |
478 | mad_snoop_priv->mad_snoop_flags = mad_snoop_flags; | |
1b52fa98 | 479 | init_completion(&mad_snoop_priv->comp); |
1da177e4 LT |
480 | mad_snoop_priv->snoop_index = register_snoop_agent( |
481 | &port_priv->qp_info[qpn], | |
482 | mad_snoop_priv); | |
483 | if (mad_snoop_priv->snoop_index < 0) { | |
484 | ret = ERR_PTR(mad_snoop_priv->snoop_index); | |
485 | goto error2; | |
486 | } | |
487 | ||
488 | atomic_set(&mad_snoop_priv->refcount, 1); | |
489 | return &mad_snoop_priv->agent; | |
490 | ||
491 | error2: | |
492 | kfree(mad_snoop_priv); | |
493 | error1: | |
494 | return ret; | |
495 | } | |
496 | EXPORT_SYMBOL(ib_register_mad_snoop); | |
497 | ||
1b52fa98 SH |
498 | static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv) |
499 | { | |
500 | if (atomic_dec_and_test(&mad_agent_priv->refcount)) | |
501 | complete(&mad_agent_priv->comp); | |
502 | } | |
503 | ||
504 | static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv) | |
505 | { | |
506 | if (atomic_dec_and_test(&mad_snoop_priv->refcount)) | |
507 | complete(&mad_snoop_priv->comp); | |
508 | } | |
509 | ||
1da177e4 LT |
510 | static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv) |
511 | { | |
512 | struct ib_mad_port_private *port_priv; | |
513 | unsigned long flags; | |
514 | ||
515 | /* Note that we could still be handling received MADs */ | |
516 | ||
517 | /* | |
518 | * Canceling all sends results in dropping received response | |
519 | * MADs, preventing us from queuing additional work | |
520 | */ | |
521 | cancel_mads(mad_agent_priv); | |
1da177e4 | 522 | port_priv = mad_agent_priv->qp_info->port_priv; |
1da177e4 | 523 | cancel_delayed_work(&mad_agent_priv->timed_work); |
1da177e4 LT |
524 | |
525 | spin_lock_irqsave(&port_priv->reg_lock, flags); | |
526 | remove_mad_reg_req(mad_agent_priv); | |
527 | list_del(&mad_agent_priv->agent_list); | |
528 | spin_unlock_irqrestore(&port_priv->reg_lock, flags); | |
529 | ||
b82cab6b | 530 | flush_workqueue(port_priv->wq); |
fa619a77 | 531 | ib_cancel_rmpp_recvs(mad_agent_priv); |
1da177e4 | 532 | |
1b52fa98 SH |
533 | deref_mad_agent(mad_agent_priv); |
534 | wait_for_completion(&mad_agent_priv->comp); | |
1da177e4 | 535 | |
6044ec88 | 536 | kfree(mad_agent_priv->reg_req); |
b82cab6b | 537 | ib_dereg_mr(mad_agent_priv->agent.mr); |
1da177e4 LT |
538 | kfree(mad_agent_priv); |
539 | } | |
540 | ||
541 | static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv) | |
542 | { | |
543 | struct ib_mad_qp_info *qp_info; | |
544 | unsigned long flags; | |
545 | ||
546 | qp_info = mad_snoop_priv->qp_info; | |
547 | spin_lock_irqsave(&qp_info->snoop_lock, flags); | |
548 | qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL; | |
549 | atomic_dec(&qp_info->snoop_count); | |
550 | spin_unlock_irqrestore(&qp_info->snoop_lock, flags); | |
551 | ||
1b52fa98 SH |
552 | deref_snoop_agent(mad_snoop_priv); |
553 | wait_for_completion(&mad_snoop_priv->comp); | |
1da177e4 LT |
554 | |
555 | kfree(mad_snoop_priv); | |
556 | } | |
557 | ||
558 | /* | |
559 | * ib_unregister_mad_agent - Unregisters a client from using MAD services | |
560 | */ | |
561 | int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent) | |
562 | { | |
563 | struct ib_mad_agent_private *mad_agent_priv; | |
564 | struct ib_mad_snoop_private *mad_snoop_priv; | |
565 | ||
566 | /* If the TID is zero, the agent can only snoop. */ | |
567 | if (mad_agent->hi_tid) { | |
568 | mad_agent_priv = container_of(mad_agent, | |
569 | struct ib_mad_agent_private, | |
570 | agent); | |
571 | unregister_mad_agent(mad_agent_priv); | |
572 | } else { | |
573 | mad_snoop_priv = container_of(mad_agent, | |
574 | struct ib_mad_snoop_private, | |
575 | agent); | |
576 | unregister_mad_snoop(mad_snoop_priv); | |
577 | } | |
578 | return 0; | |
579 | } | |
580 | EXPORT_SYMBOL(ib_unregister_mad_agent); | |
581 | ||
582 | static void dequeue_mad(struct ib_mad_list_head *mad_list) | |
583 | { | |
584 | struct ib_mad_queue *mad_queue; | |
585 | unsigned long flags; | |
586 | ||
587 | BUG_ON(!mad_list->mad_queue); | |
588 | mad_queue = mad_list->mad_queue; | |
589 | spin_lock_irqsave(&mad_queue->lock, flags); | |
590 | list_del(&mad_list->list); | |
591 | mad_queue->count--; | |
592 | spin_unlock_irqrestore(&mad_queue->lock, flags); | |
593 | } | |
594 | ||
595 | static void snoop_send(struct ib_mad_qp_info *qp_info, | |
34816ad9 | 596 | struct ib_mad_send_buf *send_buf, |
1da177e4 LT |
597 | struct ib_mad_send_wc *mad_send_wc, |
598 | int mad_snoop_flags) | |
599 | { | |
600 | struct ib_mad_snoop_private *mad_snoop_priv; | |
601 | unsigned long flags; | |
602 | int i; | |
603 | ||
604 | spin_lock_irqsave(&qp_info->snoop_lock, flags); | |
605 | for (i = 0; i < qp_info->snoop_table_size; i++) { | |
606 | mad_snoop_priv = qp_info->snoop_table[i]; | |
607 | if (!mad_snoop_priv || | |
608 | !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags)) | |
609 | continue; | |
610 | ||
611 | atomic_inc(&mad_snoop_priv->refcount); | |
612 | spin_unlock_irqrestore(&qp_info->snoop_lock, flags); | |
613 | mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent, | |
34816ad9 | 614 | send_buf, mad_send_wc); |
1b52fa98 | 615 | deref_snoop_agent(mad_snoop_priv); |
1da177e4 LT |
616 | spin_lock_irqsave(&qp_info->snoop_lock, flags); |
617 | } | |
618 | spin_unlock_irqrestore(&qp_info->snoop_lock, flags); | |
619 | } | |
620 | ||
621 | static void snoop_recv(struct ib_mad_qp_info *qp_info, | |
622 | struct ib_mad_recv_wc *mad_recv_wc, | |
623 | int mad_snoop_flags) | |
624 | { | |
625 | struct ib_mad_snoop_private *mad_snoop_priv; | |
626 | unsigned long flags; | |
627 | int i; | |
628 | ||
629 | spin_lock_irqsave(&qp_info->snoop_lock, flags); | |
630 | for (i = 0; i < qp_info->snoop_table_size; i++) { | |
631 | mad_snoop_priv = qp_info->snoop_table[i]; | |
632 | if (!mad_snoop_priv || | |
633 | !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags)) | |
634 | continue; | |
635 | ||
636 | atomic_inc(&mad_snoop_priv->refcount); | |
637 | spin_unlock_irqrestore(&qp_info->snoop_lock, flags); | |
638 | mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent, | |
639 | mad_recv_wc); | |
1b52fa98 | 640 | deref_snoop_agent(mad_snoop_priv); |
1da177e4 LT |
641 | spin_lock_irqsave(&qp_info->snoop_lock, flags); |
642 | } | |
643 | spin_unlock_irqrestore(&qp_info->snoop_lock, flags); | |
644 | } | |
645 | ||
062dbb69 MT |
646 | static void build_smp_wc(struct ib_qp *qp, |
647 | u64 wr_id, u16 slid, u16 pkey_index, u8 port_num, | |
1da177e4 LT |
648 | struct ib_wc *wc) |
649 | { | |
650 | memset(wc, 0, sizeof *wc); | |
651 | wc->wr_id = wr_id; | |
652 | wc->status = IB_WC_SUCCESS; | |
653 | wc->opcode = IB_WC_RECV; | |
654 | wc->pkey_index = pkey_index; | |
655 | wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh); | |
656 | wc->src_qp = IB_QP0; | |
062dbb69 | 657 | wc->qp = qp; |
1da177e4 LT |
658 | wc->slid = slid; |
659 | wc->sl = 0; | |
660 | wc->dlid_path_bits = 0; | |
661 | wc->port_num = port_num; | |
662 | } | |
663 | ||
664 | /* | |
665 | * Return 0 if SMP is to be sent | |
666 | * Return 1 if SMP was consumed locally (whether or not solicited) | |
667 | * Return < 0 if error | |
668 | */ | |
669 | static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv, | |
34816ad9 | 670 | struct ib_mad_send_wr_private *mad_send_wr) |
1da177e4 | 671 | { |
de493d47 | 672 | int ret = 0; |
34816ad9 | 673 | struct ib_smp *smp = mad_send_wr->send_buf.mad; |
1da177e4 LT |
674 | unsigned long flags; |
675 | struct ib_mad_local_private *local; | |
676 | struct ib_mad_private *mad_priv; | |
677 | struct ib_mad_port_private *port_priv; | |
678 | struct ib_mad_agent_private *recv_mad_agent = NULL; | |
679 | struct ib_device *device = mad_agent_priv->agent.device; | |
1bae4dbf | 680 | u8 port_num; |
1da177e4 | 681 | struct ib_wc mad_wc; |
34816ad9 | 682 | struct ib_send_wr *send_wr = &mad_send_wr->send_wr; |
1da177e4 | 683 | |
1bae4dbf HR |
684 | if (device->node_type == RDMA_NODE_IB_SWITCH && |
685 | smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) | |
686 | port_num = send_wr->wr.ud.port_num; | |
687 | else | |
688 | port_num = mad_agent_priv->agent.port_num; | |
689 | ||
8cf3f04f RC |
690 | /* |
691 | * Directed route handling starts if the initial LID routed part of | |
692 | * a request or the ending LID routed part of a response is empty. | |
693 | * If we are at the start of the LID routed part, don't update the | |
694 | * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec. | |
695 | */ | |
696 | if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) == | |
697 | IB_LID_PERMISSIVE && | |
de493d47 HR |
698 | smi_handle_dr_smp_send(smp, device->node_type, port_num) == |
699 | IB_SMI_DISCARD) { | |
1da177e4 LT |
700 | ret = -EINVAL; |
701 | printk(KERN_ERR PFX "Invalid directed route\n"); | |
702 | goto out; | |
703 | } | |
de493d47 | 704 | |
1da177e4 | 705 | /* Check to post send on QP or process locally */ |
727792da SW |
706 | if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD && |
707 | smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD) | |
1da177e4 LT |
708 | goto out; |
709 | ||
710 | local = kmalloc(sizeof *local, GFP_ATOMIC); | |
711 | if (!local) { | |
712 | ret = -ENOMEM; | |
713 | printk(KERN_ERR PFX "No memory for ib_mad_local_private\n"); | |
714 | goto out; | |
715 | } | |
716 | local->mad_priv = NULL; | |
717 | local->recv_mad_agent = NULL; | |
718 | mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC); | |
719 | if (!mad_priv) { | |
720 | ret = -ENOMEM; | |
721 | printk(KERN_ERR PFX "No memory for local response MAD\n"); | |
722 | kfree(local); | |
723 | goto out; | |
724 | } | |
725 | ||
062dbb69 MT |
726 | build_smp_wc(mad_agent_priv->agent.qp, |
727 | send_wr->wr_id, be16_to_cpu(smp->dr_slid), | |
97f52eb4 | 728 | send_wr->wr.ud.pkey_index, |
1da177e4 LT |
729 | send_wr->wr.ud.port_num, &mad_wc); |
730 | ||
731 | /* No GRH for DR SMP */ | |
732 | ret = device->process_mad(device, 0, port_num, &mad_wc, NULL, | |
733 | (struct ib_mad *)smp, | |
734 | (struct ib_mad *)&mad_priv->mad); | |
735 | switch (ret) | |
736 | { | |
737 | case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY: | |
2527e681 | 738 | if (ib_response_mad(&mad_priv->mad.mad) && |
1da177e4 LT |
739 | mad_agent_priv->agent.recv_handler) { |
740 | local->mad_priv = mad_priv; | |
741 | local->recv_mad_agent = mad_agent_priv; | |
742 | /* | |
743 | * Reference MAD agent until receive | |
744 | * side of local completion handled | |
745 | */ | |
746 | atomic_inc(&mad_agent_priv->refcount); | |
747 | } else | |
748 | kmem_cache_free(ib_mad_cache, mad_priv); | |
749 | break; | |
750 | case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED: | |
751 | kmem_cache_free(ib_mad_cache, mad_priv); | |
4780c195 | 752 | break; |
1da177e4 LT |
753 | case IB_MAD_RESULT_SUCCESS: |
754 | /* Treat like an incoming receive MAD */ | |
1da177e4 LT |
755 | port_priv = ib_get_mad_port(mad_agent_priv->agent.device, |
756 | mad_agent_priv->agent.port_num); | |
757 | if (port_priv) { | |
727792da | 758 | memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad)); |
1da177e4 | 759 | recv_mad_agent = find_mad_agent(port_priv, |
4a0754fa | 760 | &mad_priv->mad.mad); |
1da177e4 LT |
761 | } |
762 | if (!port_priv || !recv_mad_agent) { | |
4780c195 RC |
763 | /* |
764 | * No receiving agent so drop packet and | |
765 | * generate send completion. | |
766 | */ | |
1da177e4 | 767 | kmem_cache_free(ib_mad_cache, mad_priv); |
4780c195 | 768 | break; |
1da177e4 LT |
769 | } |
770 | local->mad_priv = mad_priv; | |
771 | local->recv_mad_agent = recv_mad_agent; | |
772 | break; | |
773 | default: | |
774 | kmem_cache_free(ib_mad_cache, mad_priv); | |
775 | kfree(local); | |
776 | ret = -EINVAL; | |
777 | goto out; | |
778 | } | |
779 | ||
34816ad9 | 780 | local->mad_send_wr = mad_send_wr; |
1da177e4 LT |
781 | /* Reference MAD agent until send side of local completion handled */ |
782 | atomic_inc(&mad_agent_priv->refcount); | |
783 | /* Queue local completion to local list */ | |
784 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
785 | list_add_tail(&local->completion_list, &mad_agent_priv->local_list); | |
786 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
787 | queue_work(mad_agent_priv->qp_info->port_priv->wq, | |
b82cab6b | 788 | &mad_agent_priv->local_work); |
1da177e4 LT |
789 | ret = 1; |
790 | out: | |
791 | return ret; | |
792 | } | |
793 | ||
f36e1793 | 794 | static int get_pad_size(int hdr_len, int data_len) |
824c8ae7 HR |
795 | { |
796 | int seg_size, pad; | |
797 | ||
798 | seg_size = sizeof(struct ib_mad) - hdr_len; | |
799 | if (data_len && seg_size) { | |
800 | pad = seg_size - data_len % seg_size; | |
f36e1793 | 801 | return pad == seg_size ? 0 : pad; |
824c8ae7 | 802 | } else |
f36e1793 JM |
803 | return seg_size; |
804 | } | |
805 | ||
806 | static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr) | |
807 | { | |
808 | struct ib_rmpp_segment *s, *t; | |
809 | ||
810 | list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) { | |
811 | list_del(&s->list); | |
812 | kfree(s); | |
813 | } | |
814 | } | |
815 | ||
816 | static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr, | |
817 | gfp_t gfp_mask) | |
818 | { | |
819 | struct ib_mad_send_buf *send_buf = &send_wr->send_buf; | |
820 | struct ib_rmpp_mad *rmpp_mad = send_buf->mad; | |
821 | struct ib_rmpp_segment *seg = NULL; | |
822 | int left, seg_size, pad; | |
823 | ||
824 | send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len; | |
825 | seg_size = send_buf->seg_size; | |
826 | pad = send_wr->pad; | |
827 | ||
828 | /* Allocate data segments. */ | |
829 | for (left = send_buf->data_len + pad; left > 0; left -= seg_size) { | |
830 | seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask); | |
831 | if (!seg) { | |
832 | printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem " | |
833 | "alloc failed for len %zd, gfp %#x\n", | |
834 | sizeof (*seg) + seg_size, gfp_mask); | |
835 | free_send_rmpp_list(send_wr); | |
836 | return -ENOMEM; | |
837 | } | |
838 | seg->num = ++send_buf->seg_count; | |
839 | list_add_tail(&seg->list, &send_wr->rmpp_list); | |
840 | } | |
841 | ||
842 | /* Zero any padding */ | |
843 | if (pad) | |
844 | memset(seg->data + seg_size - pad, 0, pad); | |
845 | ||
846 | rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv-> | |
847 | agent.rmpp_version; | |
848 | rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA; | |
849 | ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE); | |
850 | ||
851 | send_wr->cur_seg = container_of(send_wr->rmpp_list.next, | |
852 | struct ib_rmpp_segment, list); | |
853 | send_wr->last_ack_seg = send_wr->cur_seg; | |
854 | return 0; | |
824c8ae7 HR |
855 | } |
856 | ||
857 | struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent, | |
858 | u32 remote_qpn, u16 pkey_index, | |
34816ad9 | 859 | int rmpp_active, |
824c8ae7 | 860 | int hdr_len, int data_len, |
dd0fc66f | 861 | gfp_t gfp_mask) |
824c8ae7 HR |
862 | { |
863 | struct ib_mad_agent_private *mad_agent_priv; | |
34816ad9 | 864 | struct ib_mad_send_wr_private *mad_send_wr; |
f36e1793 | 865 | int pad, message_size, ret, size; |
824c8ae7 HR |
866 | void *buf; |
867 | ||
34816ad9 SH |
868 | mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private, |
869 | agent); | |
f36e1793 JM |
870 | pad = get_pad_size(hdr_len, data_len); |
871 | message_size = hdr_len + data_len + pad; | |
824c8ae7 | 872 | |
fa619a77 | 873 | if ((!mad_agent->rmpp_version && |
f36e1793 JM |
874 | (rmpp_active || message_size > sizeof(struct ib_mad))) || |
875 | (!rmpp_active && message_size > sizeof(struct ib_mad))) | |
fa619a77 HR |
876 | return ERR_PTR(-EINVAL); |
877 | ||
f36e1793 JM |
878 | size = rmpp_active ? hdr_len : sizeof(struct ib_mad); |
879 | buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask); | |
824c8ae7 HR |
880 | if (!buf) |
881 | return ERR_PTR(-ENOMEM); | |
34816ad9 | 882 | |
f36e1793 JM |
883 | mad_send_wr = buf + size; |
884 | INIT_LIST_HEAD(&mad_send_wr->rmpp_list); | |
34816ad9 | 885 | mad_send_wr->send_buf.mad = buf; |
f36e1793 JM |
886 | mad_send_wr->send_buf.hdr_len = hdr_len; |
887 | mad_send_wr->send_buf.data_len = data_len; | |
888 | mad_send_wr->pad = pad; | |
34816ad9 SH |
889 | |
890 | mad_send_wr->mad_agent_priv = mad_agent_priv; | |
f36e1793 | 891 | mad_send_wr->sg_list[0].length = hdr_len; |
34816ad9 | 892 | mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey; |
f36e1793 JM |
893 | mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len; |
894 | mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey; | |
34816ad9 SH |
895 | |
896 | mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr; | |
897 | mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list; | |
f36e1793 | 898 | mad_send_wr->send_wr.num_sge = 2; |
34816ad9 SH |
899 | mad_send_wr->send_wr.opcode = IB_WR_SEND; |
900 | mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED; | |
901 | mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn; | |
902 | mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY; | |
903 | mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index; | |
fa619a77 HR |
904 | |
905 | if (rmpp_active) { | |
f36e1793 JM |
906 | ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask); |
907 | if (ret) { | |
908 | kfree(buf); | |
909 | return ERR_PTR(ret); | |
910 | } | |
fa619a77 HR |
911 | } |
912 | ||
34816ad9 | 913 | mad_send_wr->send_buf.mad_agent = mad_agent; |
824c8ae7 | 914 | atomic_inc(&mad_agent_priv->refcount); |
34816ad9 | 915 | return &mad_send_wr->send_buf; |
824c8ae7 HR |
916 | } |
917 | EXPORT_SYMBOL(ib_create_send_mad); | |
918 | ||
618a3c03 HR |
919 | int ib_get_mad_data_offset(u8 mgmt_class) |
920 | { | |
921 | if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM) | |
922 | return IB_MGMT_SA_HDR; | |
923 | else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) || | |
924 | (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) || | |
925 | (mgmt_class == IB_MGMT_CLASS_BIS)) | |
926 | return IB_MGMT_DEVICE_HDR; | |
927 | else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) && | |
928 | (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)) | |
929 | return IB_MGMT_VENDOR_HDR; | |
930 | else | |
931 | return IB_MGMT_MAD_HDR; | |
932 | } | |
933 | EXPORT_SYMBOL(ib_get_mad_data_offset); | |
934 | ||
935 | int ib_is_mad_class_rmpp(u8 mgmt_class) | |
936 | { | |
937 | if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) || | |
938 | (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) || | |
939 | (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) || | |
940 | (mgmt_class == IB_MGMT_CLASS_BIS) || | |
941 | ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) && | |
942 | (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))) | |
943 | return 1; | |
944 | return 0; | |
945 | } | |
946 | EXPORT_SYMBOL(ib_is_mad_class_rmpp); | |
947 | ||
f36e1793 JM |
948 | void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num) |
949 | { | |
950 | struct ib_mad_send_wr_private *mad_send_wr; | |
951 | struct list_head *list; | |
952 | ||
953 | mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private, | |
954 | send_buf); | |
955 | list = &mad_send_wr->cur_seg->list; | |
956 | ||
957 | if (mad_send_wr->cur_seg->num < seg_num) { | |
958 | list_for_each_entry(mad_send_wr->cur_seg, list, list) | |
959 | if (mad_send_wr->cur_seg->num == seg_num) | |
960 | break; | |
961 | } else if (mad_send_wr->cur_seg->num > seg_num) { | |
962 | list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list) | |
963 | if (mad_send_wr->cur_seg->num == seg_num) | |
964 | break; | |
965 | } | |
966 | return mad_send_wr->cur_seg->data; | |
967 | } | |
968 | EXPORT_SYMBOL(ib_get_rmpp_segment); | |
969 | ||
970 | static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr) | |
971 | { | |
972 | if (mad_send_wr->send_buf.seg_count) | |
973 | return ib_get_rmpp_segment(&mad_send_wr->send_buf, | |
974 | mad_send_wr->seg_num); | |
975 | else | |
976 | return mad_send_wr->send_buf.mad + | |
977 | mad_send_wr->send_buf.hdr_len; | |
978 | } | |
979 | ||
824c8ae7 HR |
980 | void ib_free_send_mad(struct ib_mad_send_buf *send_buf) |
981 | { | |
982 | struct ib_mad_agent_private *mad_agent_priv; | |
f36e1793 | 983 | struct ib_mad_send_wr_private *mad_send_wr; |
824c8ae7 HR |
984 | |
985 | mad_agent_priv = container_of(send_buf->mad_agent, | |
986 | struct ib_mad_agent_private, agent); | |
f36e1793 JM |
987 | mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private, |
988 | send_buf); | |
824c8ae7 | 989 | |
f36e1793 JM |
990 | free_send_rmpp_list(mad_send_wr); |
991 | kfree(send_buf->mad); | |
1b52fa98 | 992 | deref_mad_agent(mad_agent_priv); |
824c8ae7 HR |
993 | } |
994 | EXPORT_SYMBOL(ib_free_send_mad); | |
995 | ||
fa619a77 | 996 | int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr) |
1da177e4 LT |
997 | { |
998 | struct ib_mad_qp_info *qp_info; | |
cabe3cbc | 999 | struct list_head *list; |
34816ad9 SH |
1000 | struct ib_send_wr *bad_send_wr; |
1001 | struct ib_mad_agent *mad_agent; | |
1002 | struct ib_sge *sge; | |
1da177e4 LT |
1003 | unsigned long flags; |
1004 | int ret; | |
1005 | ||
f8197a4e | 1006 | /* Set WR ID to find mad_send_wr upon completion */ |
d760ce8f | 1007 | qp_info = mad_send_wr->mad_agent_priv->qp_info; |
1da177e4 LT |
1008 | mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list; |
1009 | mad_send_wr->mad_list.mad_queue = &qp_info->send_queue; | |
1010 | ||
34816ad9 SH |
1011 | mad_agent = mad_send_wr->send_buf.mad_agent; |
1012 | sge = mad_send_wr->sg_list; | |
1527106f RC |
1013 | sge[0].addr = ib_dma_map_single(mad_agent->device, |
1014 | mad_send_wr->send_buf.mad, | |
1015 | sge[0].length, | |
1016 | DMA_TO_DEVICE); | |
1017 | mad_send_wr->header_mapping = sge[0].addr; | |
1018 | ||
1019 | sge[1].addr = ib_dma_map_single(mad_agent->device, | |
1020 | ib_get_payload(mad_send_wr), | |
1021 | sge[1].length, | |
1022 | DMA_TO_DEVICE); | |
1023 | mad_send_wr->payload_mapping = sge[1].addr; | |
34816ad9 | 1024 | |
1da177e4 | 1025 | spin_lock_irqsave(&qp_info->send_queue.lock, flags); |
cabe3cbc | 1026 | if (qp_info->send_queue.count < qp_info->send_queue.max_active) { |
34816ad9 SH |
1027 | ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr, |
1028 | &bad_send_wr); | |
cabe3cbc | 1029 | list = &qp_info->send_queue.list; |
1da177e4 | 1030 | } else { |
1da177e4 | 1031 | ret = 0; |
cabe3cbc | 1032 | list = &qp_info->overflow_list; |
1da177e4 | 1033 | } |
cabe3cbc HR |
1034 | |
1035 | if (!ret) { | |
1036 | qp_info->send_queue.count++; | |
1037 | list_add_tail(&mad_send_wr->mad_list.list, list); | |
1038 | } | |
1039 | spin_unlock_irqrestore(&qp_info->send_queue.lock, flags); | |
f36e1793 | 1040 | if (ret) { |
1527106f RC |
1041 | ib_dma_unmap_single(mad_agent->device, |
1042 | mad_send_wr->header_mapping, | |
1043 | sge[0].length, DMA_TO_DEVICE); | |
1044 | ib_dma_unmap_single(mad_agent->device, | |
1045 | mad_send_wr->payload_mapping, | |
1046 | sge[1].length, DMA_TO_DEVICE); | |
f36e1793 | 1047 | } |
1da177e4 LT |
1048 | return ret; |
1049 | } | |
1050 | ||
1051 | /* | |
1052 | * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated | |
1053 | * with the registered client | |
1054 | */ | |
34816ad9 SH |
1055 | int ib_post_send_mad(struct ib_mad_send_buf *send_buf, |
1056 | struct ib_mad_send_buf **bad_send_buf) | |
1da177e4 | 1057 | { |
1da177e4 | 1058 | struct ib_mad_agent_private *mad_agent_priv; |
34816ad9 SH |
1059 | struct ib_mad_send_buf *next_send_buf; |
1060 | struct ib_mad_send_wr_private *mad_send_wr; | |
1061 | unsigned long flags; | |
1062 | int ret = -EINVAL; | |
1da177e4 LT |
1063 | |
1064 | /* Walk list of send WRs and post each on send list */ | |
34816ad9 | 1065 | for (; send_buf; send_buf = next_send_buf) { |
1da177e4 | 1066 | |
34816ad9 SH |
1067 | mad_send_wr = container_of(send_buf, |
1068 | struct ib_mad_send_wr_private, | |
1069 | send_buf); | |
1070 | mad_agent_priv = mad_send_wr->mad_agent_priv; | |
1da177e4 | 1071 | |
34816ad9 SH |
1072 | if (!send_buf->mad_agent->send_handler || |
1073 | (send_buf->timeout_ms && | |
1074 | !send_buf->mad_agent->recv_handler)) { | |
1075 | ret = -EINVAL; | |
1076 | goto error; | |
1da177e4 LT |
1077 | } |
1078 | ||
618a3c03 HR |
1079 | if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) { |
1080 | if (mad_agent_priv->agent.rmpp_version) { | |
1081 | ret = -EINVAL; | |
1082 | goto error; | |
1083 | } | |
1084 | } | |
1085 | ||
1da177e4 LT |
1086 | /* |
1087 | * Save pointer to next work request to post in case the | |
1088 | * current one completes, and the user modifies the work | |
1089 | * request associated with the completion | |
1090 | */ | |
34816ad9 SH |
1091 | next_send_buf = send_buf->next; |
1092 | mad_send_wr->send_wr.wr.ud.ah = send_buf->ah; | |
1da177e4 | 1093 | |
34816ad9 SH |
1094 | if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class == |
1095 | IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) { | |
1096 | ret = handle_outgoing_dr_smp(mad_agent_priv, | |
1097 | mad_send_wr); | |
1da177e4 | 1098 | if (ret < 0) /* error */ |
34816ad9 | 1099 | goto error; |
1da177e4 | 1100 | else if (ret == 1) /* locally consumed */ |
34816ad9 | 1101 | continue; |
1da177e4 LT |
1102 | } |
1103 | ||
34816ad9 | 1104 | mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid; |
1da177e4 | 1105 | /* Timeout will be updated after send completes */ |
34816ad9 | 1106 | mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms); |
4fc8cd49 SH |
1107 | mad_send_wr->max_retries = send_buf->retries; |
1108 | mad_send_wr->retries_left = send_buf->retries; | |
1109 | send_buf->retries = 0; | |
34816ad9 | 1110 | /* Reference for work request to QP + response */ |
1da177e4 LT |
1111 | mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0); |
1112 | mad_send_wr->status = IB_WC_SUCCESS; | |
1113 | ||
1114 | /* Reference MAD agent until send completes */ | |
1115 | atomic_inc(&mad_agent_priv->refcount); | |
1116 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
1117 | list_add_tail(&mad_send_wr->agent_list, | |
1118 | &mad_agent_priv->send_list); | |
1119 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
1120 | ||
fa619a77 HR |
1121 | if (mad_agent_priv->agent.rmpp_version) { |
1122 | ret = ib_send_rmpp_mad(mad_send_wr); | |
1123 | if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED) | |
1124 | ret = ib_send_mad(mad_send_wr); | |
1125 | } else | |
1126 | ret = ib_send_mad(mad_send_wr); | |
1127 | if (ret < 0) { | |
1da177e4 LT |
1128 | /* Fail send request */ |
1129 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
1130 | list_del(&mad_send_wr->agent_list); | |
1131 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
1132 | atomic_dec(&mad_agent_priv->refcount); | |
34816ad9 | 1133 | goto error; |
1da177e4 | 1134 | } |
1da177e4 LT |
1135 | } |
1136 | return 0; | |
34816ad9 SH |
1137 | error: |
1138 | if (bad_send_buf) | |
1139 | *bad_send_buf = send_buf; | |
1da177e4 LT |
1140 | return ret; |
1141 | } | |
1142 | EXPORT_SYMBOL(ib_post_send_mad); | |
1143 | ||
1144 | /* | |
1145 | * ib_free_recv_mad - Returns data buffers used to receive | |
1146 | * a MAD to the access layer | |
1147 | */ | |
1148 | void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc) | |
1149 | { | |
fa619a77 | 1150 | struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf; |
1da177e4 LT |
1151 | struct ib_mad_private_header *mad_priv_hdr; |
1152 | struct ib_mad_private *priv; | |
fa619a77 | 1153 | struct list_head free_list; |
1da177e4 | 1154 | |
fa619a77 HR |
1155 | INIT_LIST_HEAD(&free_list); |
1156 | list_splice_init(&mad_recv_wc->rmpp_list, &free_list); | |
1da177e4 | 1157 | |
fa619a77 HR |
1158 | list_for_each_entry_safe(mad_recv_buf, temp_recv_buf, |
1159 | &free_list, list) { | |
1160 | mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc, | |
1161 | recv_buf); | |
1da177e4 LT |
1162 | mad_priv_hdr = container_of(mad_recv_wc, |
1163 | struct ib_mad_private_header, | |
1164 | recv_wc); | |
1165 | priv = container_of(mad_priv_hdr, struct ib_mad_private, | |
1166 | header); | |
fa619a77 | 1167 | kmem_cache_free(ib_mad_cache, priv); |
1da177e4 | 1168 | } |
1da177e4 LT |
1169 | } |
1170 | EXPORT_SYMBOL(ib_free_recv_mad); | |
1171 | ||
1da177e4 LT |
1172 | struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp, |
1173 | u8 rmpp_version, | |
1174 | ib_mad_send_handler send_handler, | |
1175 | ib_mad_recv_handler recv_handler, | |
1176 | void *context) | |
1177 | { | |
1178 | return ERR_PTR(-EINVAL); /* XXX: for now */ | |
1179 | } | |
1180 | EXPORT_SYMBOL(ib_redirect_mad_qp); | |
1181 | ||
1182 | int ib_process_mad_wc(struct ib_mad_agent *mad_agent, | |
1183 | struct ib_wc *wc) | |
1184 | { | |
1185 | printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n"); | |
1186 | return 0; | |
1187 | } | |
1188 | EXPORT_SYMBOL(ib_process_mad_wc); | |
1189 | ||
1190 | static int method_in_use(struct ib_mad_mgmt_method_table **method, | |
1191 | struct ib_mad_reg_req *mad_reg_req) | |
1192 | { | |
1193 | int i; | |
1194 | ||
19b629f5 | 1195 | for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) { |
1da177e4 LT |
1196 | if ((*method)->agent[i]) { |
1197 | printk(KERN_ERR PFX "Method %d already in use\n", i); | |
1198 | return -EINVAL; | |
1199 | } | |
1200 | } | |
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | static int allocate_method_table(struct ib_mad_mgmt_method_table **method) | |
1205 | { | |
1206 | /* Allocate management method table */ | |
de6eb66b | 1207 | *method = kzalloc(sizeof **method, GFP_ATOMIC); |
1da177e4 LT |
1208 | if (!*method) { |
1209 | printk(KERN_ERR PFX "No memory for " | |
1210 | "ib_mad_mgmt_method_table\n"); | |
1211 | return -ENOMEM; | |
1212 | } | |
1da177e4 LT |
1213 | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | /* | |
1218 | * Check to see if there are any methods still in use | |
1219 | */ | |
1220 | static int check_method_table(struct ib_mad_mgmt_method_table *method) | |
1221 | { | |
1222 | int i; | |
1223 | ||
1224 | for (i = 0; i < IB_MGMT_MAX_METHODS; i++) | |
1225 | if (method->agent[i]) | |
1226 | return 1; | |
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | /* | |
1231 | * Check to see if there are any method tables for this class still in use | |
1232 | */ | |
1233 | static int check_class_table(struct ib_mad_mgmt_class_table *class) | |
1234 | { | |
1235 | int i; | |
1236 | ||
1237 | for (i = 0; i < MAX_MGMT_CLASS; i++) | |
1238 | if (class->method_table[i]) | |
1239 | return 1; | |
1240 | return 0; | |
1241 | } | |
1242 | ||
1243 | static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class) | |
1244 | { | |
1245 | int i; | |
1246 | ||
1247 | for (i = 0; i < MAX_MGMT_OUI; i++) | |
1248 | if (vendor_class->method_table[i]) | |
1249 | return 1; | |
1250 | return 0; | |
1251 | } | |
1252 | ||
1253 | static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class, | |
1254 | char *oui) | |
1255 | { | |
1256 | int i; | |
1257 | ||
1258 | for (i = 0; i < MAX_MGMT_OUI; i++) | |
3cd96564 RD |
1259 | /* Is there matching OUI for this vendor class ? */ |
1260 | if (!memcmp(vendor_class->oui[i], oui, 3)) | |
1da177e4 LT |
1261 | return i; |
1262 | ||
1263 | return -1; | |
1264 | } | |
1265 | ||
1266 | static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor) | |
1267 | { | |
1268 | int i; | |
1269 | ||
1270 | for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++) | |
1271 | if (vendor->vendor_class[i]) | |
1272 | return 1; | |
1273 | ||
1274 | return 0; | |
1275 | } | |
1276 | ||
1277 | static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method, | |
1278 | struct ib_mad_agent_private *agent) | |
1279 | { | |
1280 | int i; | |
1281 | ||
1282 | /* Remove any methods for this mad agent */ | |
1283 | for (i = 0; i < IB_MGMT_MAX_METHODS; i++) { | |
1284 | if (method->agent[i] == agent) { | |
1285 | method->agent[i] = NULL; | |
1286 | } | |
1287 | } | |
1288 | } | |
1289 | ||
1290 | static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req, | |
1291 | struct ib_mad_agent_private *agent_priv, | |
1292 | u8 mgmt_class) | |
1293 | { | |
1294 | struct ib_mad_port_private *port_priv; | |
1295 | struct ib_mad_mgmt_class_table **class; | |
1296 | struct ib_mad_mgmt_method_table **method; | |
1297 | int i, ret; | |
1298 | ||
1299 | port_priv = agent_priv->qp_info->port_priv; | |
1300 | class = &port_priv->version[mad_reg_req->mgmt_class_version].class; | |
1301 | if (!*class) { | |
1302 | /* Allocate management class table for "new" class version */ | |
de6eb66b | 1303 | *class = kzalloc(sizeof **class, GFP_ATOMIC); |
1da177e4 LT |
1304 | if (!*class) { |
1305 | printk(KERN_ERR PFX "No memory for " | |
1306 | "ib_mad_mgmt_class_table\n"); | |
1307 | ret = -ENOMEM; | |
1308 | goto error1; | |
1309 | } | |
de6eb66b | 1310 | |
1da177e4 LT |
1311 | /* Allocate method table for this management class */ |
1312 | method = &(*class)->method_table[mgmt_class]; | |
1313 | if ((ret = allocate_method_table(method))) | |
1314 | goto error2; | |
1315 | } else { | |
1316 | method = &(*class)->method_table[mgmt_class]; | |
1317 | if (!*method) { | |
1318 | /* Allocate method table for this management class */ | |
1319 | if ((ret = allocate_method_table(method))) | |
1320 | goto error1; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | /* Now, make sure methods are not already in use */ | |
1325 | if (method_in_use(method, mad_reg_req)) | |
1326 | goto error3; | |
1327 | ||
1328 | /* Finally, add in methods being registered */ | |
19b629f5 | 1329 | for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) |
1da177e4 | 1330 | (*method)->agent[i] = agent_priv; |
19b629f5 | 1331 | |
1da177e4 LT |
1332 | return 0; |
1333 | ||
1334 | error3: | |
1335 | /* Remove any methods for this mad agent */ | |
1336 | remove_methods_mad_agent(*method, agent_priv); | |
1337 | /* Now, check to see if there are any methods in use */ | |
1338 | if (!check_method_table(*method)) { | |
1339 | /* If not, release management method table */ | |
1340 | kfree(*method); | |
1341 | *method = NULL; | |
1342 | } | |
1343 | ret = -EINVAL; | |
1344 | goto error1; | |
1345 | error2: | |
1346 | kfree(*class); | |
1347 | *class = NULL; | |
1348 | error1: | |
1349 | return ret; | |
1350 | } | |
1351 | ||
1352 | static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req, | |
1353 | struct ib_mad_agent_private *agent_priv) | |
1354 | { | |
1355 | struct ib_mad_port_private *port_priv; | |
1356 | struct ib_mad_mgmt_vendor_class_table **vendor_table; | |
1357 | struct ib_mad_mgmt_vendor_class_table *vendor = NULL; | |
1358 | struct ib_mad_mgmt_vendor_class *vendor_class = NULL; | |
1359 | struct ib_mad_mgmt_method_table **method; | |
1360 | int i, ret = -ENOMEM; | |
1361 | u8 vclass; | |
1362 | ||
1363 | /* "New" vendor (with OUI) class */ | |
1364 | vclass = vendor_class_index(mad_reg_req->mgmt_class); | |
1365 | port_priv = agent_priv->qp_info->port_priv; | |
1366 | vendor_table = &port_priv->version[ | |
1367 | mad_reg_req->mgmt_class_version].vendor; | |
1368 | if (!*vendor_table) { | |
1369 | /* Allocate mgmt vendor class table for "new" class version */ | |
de6eb66b | 1370 | vendor = kzalloc(sizeof *vendor, GFP_ATOMIC); |
1da177e4 LT |
1371 | if (!vendor) { |
1372 | printk(KERN_ERR PFX "No memory for " | |
1373 | "ib_mad_mgmt_vendor_class_table\n"); | |
1374 | goto error1; | |
1375 | } | |
de6eb66b | 1376 | |
1da177e4 LT |
1377 | *vendor_table = vendor; |
1378 | } | |
1379 | if (!(*vendor_table)->vendor_class[vclass]) { | |
1380 | /* Allocate table for this management vendor class */ | |
de6eb66b | 1381 | vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC); |
1da177e4 LT |
1382 | if (!vendor_class) { |
1383 | printk(KERN_ERR PFX "No memory for " | |
1384 | "ib_mad_mgmt_vendor_class\n"); | |
1385 | goto error2; | |
1386 | } | |
de6eb66b | 1387 | |
1da177e4 LT |
1388 | (*vendor_table)->vendor_class[vclass] = vendor_class; |
1389 | } | |
1390 | for (i = 0; i < MAX_MGMT_OUI; i++) { | |
1391 | /* Is there matching OUI for this vendor class ? */ | |
1392 | if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i], | |
1393 | mad_reg_req->oui, 3)) { | |
1394 | method = &(*vendor_table)->vendor_class[ | |
1395 | vclass]->method_table[i]; | |
1396 | BUG_ON(!*method); | |
1397 | goto check_in_use; | |
1398 | } | |
1399 | } | |
1400 | for (i = 0; i < MAX_MGMT_OUI; i++) { | |
1401 | /* OUI slot available ? */ | |
1402 | if (!is_vendor_oui((*vendor_table)->vendor_class[ | |
1403 | vclass]->oui[i])) { | |
1404 | method = &(*vendor_table)->vendor_class[ | |
1405 | vclass]->method_table[i]; | |
1406 | BUG_ON(*method); | |
1407 | /* Allocate method table for this OUI */ | |
1408 | if ((ret = allocate_method_table(method))) | |
1409 | goto error3; | |
1410 | memcpy((*vendor_table)->vendor_class[vclass]->oui[i], | |
1411 | mad_reg_req->oui, 3); | |
1412 | goto check_in_use; | |
1413 | } | |
1414 | } | |
1415 | printk(KERN_ERR PFX "All OUI slots in use\n"); | |
1416 | goto error3; | |
1417 | ||
1418 | check_in_use: | |
1419 | /* Now, make sure methods are not already in use */ | |
1420 | if (method_in_use(method, mad_reg_req)) | |
1421 | goto error4; | |
1422 | ||
1423 | /* Finally, add in methods being registered */ | |
19b629f5 | 1424 | for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) |
1da177e4 | 1425 | (*method)->agent[i] = agent_priv; |
19b629f5 | 1426 | |
1da177e4 LT |
1427 | return 0; |
1428 | ||
1429 | error4: | |
1430 | /* Remove any methods for this mad agent */ | |
1431 | remove_methods_mad_agent(*method, agent_priv); | |
1432 | /* Now, check to see if there are any methods in use */ | |
1433 | if (!check_method_table(*method)) { | |
1434 | /* If not, release management method table */ | |
1435 | kfree(*method); | |
1436 | *method = NULL; | |
1437 | } | |
1438 | ret = -EINVAL; | |
1439 | error3: | |
1440 | if (vendor_class) { | |
1441 | (*vendor_table)->vendor_class[vclass] = NULL; | |
1442 | kfree(vendor_class); | |
1443 | } | |
1444 | error2: | |
1445 | if (vendor) { | |
1446 | *vendor_table = NULL; | |
1447 | kfree(vendor); | |
1448 | } | |
1449 | error1: | |
1450 | return ret; | |
1451 | } | |
1452 | ||
1453 | static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv) | |
1454 | { | |
1455 | struct ib_mad_port_private *port_priv; | |
1456 | struct ib_mad_mgmt_class_table *class; | |
1457 | struct ib_mad_mgmt_method_table *method; | |
1458 | struct ib_mad_mgmt_vendor_class_table *vendor; | |
1459 | struct ib_mad_mgmt_vendor_class *vendor_class; | |
1460 | int index; | |
1461 | u8 mgmt_class; | |
1462 | ||
1463 | /* | |
1464 | * Was MAD registration request supplied | |
1465 | * with original registration ? | |
1466 | */ | |
1467 | if (!agent_priv->reg_req) { | |
1468 | goto out; | |
1469 | } | |
1470 | ||
1471 | port_priv = agent_priv->qp_info->port_priv; | |
1472 | mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class); | |
1473 | class = port_priv->version[ | |
1474 | agent_priv->reg_req->mgmt_class_version].class; | |
1475 | if (!class) | |
1476 | goto vendor_check; | |
1477 | ||
1478 | method = class->method_table[mgmt_class]; | |
1479 | if (method) { | |
1480 | /* Remove any methods for this mad agent */ | |
1481 | remove_methods_mad_agent(method, agent_priv); | |
1482 | /* Now, check to see if there are any methods still in use */ | |
1483 | if (!check_method_table(method)) { | |
1484 | /* If not, release management method table */ | |
1485 | kfree(method); | |
1486 | class->method_table[mgmt_class] = NULL; | |
1487 | /* Any management classes left ? */ | |
1488 | if (!check_class_table(class)) { | |
1489 | /* If not, release management class table */ | |
1490 | kfree(class); | |
1491 | port_priv->version[ | |
1492 | agent_priv->reg_req-> | |
1493 | mgmt_class_version].class = NULL; | |
1494 | } | |
1495 | } | |
1496 | } | |
1497 | ||
1498 | vendor_check: | |
1499 | if (!is_vendor_class(mgmt_class)) | |
1500 | goto out; | |
1501 | ||
1502 | /* normalize mgmt_class to vendor range 2 */ | |
1503 | mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class); | |
1504 | vendor = port_priv->version[ | |
1505 | agent_priv->reg_req->mgmt_class_version].vendor; | |
1506 | ||
1507 | if (!vendor) | |
1508 | goto out; | |
1509 | ||
1510 | vendor_class = vendor->vendor_class[mgmt_class]; | |
1511 | if (vendor_class) { | |
1512 | index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui); | |
1513 | if (index < 0) | |
1514 | goto out; | |
1515 | method = vendor_class->method_table[index]; | |
1516 | if (method) { | |
1517 | /* Remove any methods for this mad agent */ | |
1518 | remove_methods_mad_agent(method, agent_priv); | |
1519 | /* | |
1520 | * Now, check to see if there are | |
1521 | * any methods still in use | |
1522 | */ | |
1523 | if (!check_method_table(method)) { | |
1524 | /* If not, release management method table */ | |
1525 | kfree(method); | |
1526 | vendor_class->method_table[index] = NULL; | |
1527 | memset(vendor_class->oui[index], 0, 3); | |
1528 | /* Any OUIs left ? */ | |
1529 | if (!check_vendor_class(vendor_class)) { | |
1530 | /* If not, release vendor class table */ | |
1531 | kfree(vendor_class); | |
1532 | vendor->vendor_class[mgmt_class] = NULL; | |
1533 | /* Any other vendor classes left ? */ | |
1534 | if (!check_vendor_table(vendor)) { | |
1535 | kfree(vendor); | |
1536 | port_priv->version[ | |
1537 | agent_priv->reg_req-> | |
1538 | mgmt_class_version]. | |
1539 | vendor = NULL; | |
1540 | } | |
1541 | } | |
1542 | } | |
1543 | } | |
1544 | } | |
1545 | ||
1546 | out: | |
1547 | return; | |
1548 | } | |
1549 | ||
1da177e4 LT |
1550 | static struct ib_mad_agent_private * |
1551 | find_mad_agent(struct ib_mad_port_private *port_priv, | |
4a0754fa | 1552 | struct ib_mad *mad) |
1da177e4 LT |
1553 | { |
1554 | struct ib_mad_agent_private *mad_agent = NULL; | |
1555 | unsigned long flags; | |
1556 | ||
1557 | spin_lock_irqsave(&port_priv->reg_lock, flags); | |
2527e681 | 1558 | if (ib_response_mad(mad)) { |
1da177e4 LT |
1559 | u32 hi_tid; |
1560 | struct ib_mad_agent_private *entry; | |
1561 | ||
1562 | /* | |
1563 | * Routing is based on high 32 bits of transaction ID | |
1564 | * of MAD. | |
1565 | */ | |
1566 | hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32; | |
34816ad9 | 1567 | list_for_each_entry(entry, &port_priv->agent_list, agent_list) { |
1da177e4 LT |
1568 | if (entry->agent.hi_tid == hi_tid) { |
1569 | mad_agent = entry; | |
1570 | break; | |
1571 | } | |
1572 | } | |
1573 | } else { | |
1574 | struct ib_mad_mgmt_class_table *class; | |
1575 | struct ib_mad_mgmt_method_table *method; | |
1576 | struct ib_mad_mgmt_vendor_class_table *vendor; | |
1577 | struct ib_mad_mgmt_vendor_class *vendor_class; | |
1578 | struct ib_vendor_mad *vendor_mad; | |
1579 | int index; | |
1580 | ||
1581 | /* | |
1582 | * Routing is based on version, class, and method | |
1583 | * For "newer" vendor MADs, also based on OUI | |
1584 | */ | |
1585 | if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION) | |
1586 | goto out; | |
1587 | if (!is_vendor_class(mad->mad_hdr.mgmt_class)) { | |
1588 | class = port_priv->version[ | |
1589 | mad->mad_hdr.class_version].class; | |
1590 | if (!class) | |
1591 | goto out; | |
1592 | method = class->method_table[convert_mgmt_class( | |
1593 | mad->mad_hdr.mgmt_class)]; | |
1594 | if (method) | |
1595 | mad_agent = method->agent[mad->mad_hdr.method & | |
1596 | ~IB_MGMT_METHOD_RESP]; | |
1597 | } else { | |
1598 | vendor = port_priv->version[ | |
1599 | mad->mad_hdr.class_version].vendor; | |
1600 | if (!vendor) | |
1601 | goto out; | |
1602 | vendor_class = vendor->vendor_class[vendor_class_index( | |
1603 | mad->mad_hdr.mgmt_class)]; | |
1604 | if (!vendor_class) | |
1605 | goto out; | |
1606 | /* Find matching OUI */ | |
1607 | vendor_mad = (struct ib_vendor_mad *)mad; | |
1608 | index = find_vendor_oui(vendor_class, vendor_mad->oui); | |
1609 | if (index == -1) | |
1610 | goto out; | |
1611 | method = vendor_class->method_table[index]; | |
1612 | if (method) { | |
1613 | mad_agent = method->agent[mad->mad_hdr.method & | |
1614 | ~IB_MGMT_METHOD_RESP]; | |
1615 | } | |
1616 | } | |
1617 | } | |
1618 | ||
1619 | if (mad_agent) { | |
1620 | if (mad_agent->agent.recv_handler) | |
1621 | atomic_inc(&mad_agent->refcount); | |
1622 | else { | |
1623 | printk(KERN_NOTICE PFX "No receive handler for client " | |
1624 | "%p on port %d\n", | |
1625 | &mad_agent->agent, port_priv->port_num); | |
1626 | mad_agent = NULL; | |
1627 | } | |
1628 | } | |
1629 | out: | |
1630 | spin_unlock_irqrestore(&port_priv->reg_lock, flags); | |
1631 | ||
1632 | return mad_agent; | |
1633 | } | |
1634 | ||
1635 | static int validate_mad(struct ib_mad *mad, u32 qp_num) | |
1636 | { | |
1637 | int valid = 0; | |
1638 | ||
1639 | /* Make sure MAD base version is understood */ | |
1640 | if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) { | |
1641 | printk(KERN_ERR PFX "MAD received with unsupported base " | |
1642 | "version %d\n", mad->mad_hdr.base_version); | |
1643 | goto out; | |
1644 | } | |
1645 | ||
1646 | /* Filter SMI packets sent to other than QP0 */ | |
1647 | if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) || | |
1648 | (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) { | |
1649 | if (qp_num == 0) | |
1650 | valid = 1; | |
1651 | } else { | |
1652 | /* Filter GSI packets sent to QP0 */ | |
1653 | if (qp_num != 0) | |
1654 | valid = 1; | |
1655 | } | |
1656 | ||
1657 | out: | |
1658 | return valid; | |
1659 | } | |
1660 | ||
fa619a77 HR |
1661 | static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv, |
1662 | struct ib_mad_hdr *mad_hdr) | |
1663 | { | |
1664 | struct ib_rmpp_mad *rmpp_mad; | |
1665 | ||
1666 | rmpp_mad = (struct ib_rmpp_mad *)mad_hdr; | |
1667 | return !mad_agent_priv->agent.rmpp_version || | |
1668 | !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & | |
1669 | IB_MGMT_RMPP_FLAG_ACTIVE) || | |
1670 | (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA); | |
1671 | } | |
1672 | ||
fa9656bb JM |
1673 | static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr, |
1674 | struct ib_mad_recv_wc *rwc) | |
1675 | { | |
1676 | return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class == | |
1677 | rwc->recv_buf.mad->mad_hdr.mgmt_class; | |
1678 | } | |
1679 | ||
9874e746 JM |
1680 | static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv, |
1681 | struct ib_mad_send_wr_private *wr, | |
fa9656bb JM |
1682 | struct ib_mad_recv_wc *rwc ) |
1683 | { | |
1684 | struct ib_ah_attr attr; | |
1685 | u8 send_resp, rcv_resp; | |
9874e746 JM |
1686 | union ib_gid sgid; |
1687 | struct ib_device *device = mad_agent_priv->agent.device; | |
1688 | u8 port_num = mad_agent_priv->agent.port_num; | |
1689 | u8 lmc; | |
fa9656bb | 1690 | |
7097228c MB |
1691 | send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad); |
1692 | rcv_resp = ib_response_mad(rwc->recv_buf.mad); | |
fa9656bb | 1693 | |
fa9656bb JM |
1694 | if (send_resp == rcv_resp) |
1695 | /* both requests, or both responses. GIDs different */ | |
1696 | return 0; | |
1697 | ||
1698 | if (ib_query_ah(wr->send_buf.ah, &attr)) | |
1699 | /* Assume not equal, to avoid false positives. */ | |
1700 | return 0; | |
1701 | ||
9874e746 JM |
1702 | if (!!(attr.ah_flags & IB_AH_GRH) != |
1703 | !!(rwc->wc->wc_flags & IB_WC_GRH)) | |
fa9656bb JM |
1704 | /* one has GID, other does not. Assume different */ |
1705 | return 0; | |
9874e746 JM |
1706 | |
1707 | if (!send_resp && rcv_resp) { | |
1708 | /* is request/response. */ | |
1709 | if (!(attr.ah_flags & IB_AH_GRH)) { | |
1710 | if (ib_get_cached_lmc(device, port_num, &lmc)) | |
1711 | return 0; | |
1712 | return (!lmc || !((attr.src_path_bits ^ | |
1713 | rwc->wc->dlid_path_bits) & | |
1714 | ((1 << lmc) - 1))); | |
1715 | } else { | |
1716 | if (ib_get_cached_gid(device, port_num, | |
1717 | attr.grh.sgid_index, &sgid)) | |
1718 | return 0; | |
1719 | return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw, | |
1720 | 16); | |
1721 | } | |
1722 | } | |
1723 | ||
1724 | if (!(attr.ah_flags & IB_AH_GRH)) | |
1725 | return attr.dlid == rwc->wc->slid; | |
1726 | else | |
1727 | return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw, | |
1728 | 16); | |
1729 | } | |
1730 | ||
1731 | static inline int is_direct(u8 class) | |
1732 | { | |
1733 | return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE); | |
fa9656bb | 1734 | } |
9874e746 | 1735 | |
fa619a77 | 1736 | struct ib_mad_send_wr_private* |
fa9656bb | 1737 | ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv, |
9874e746 | 1738 | struct ib_mad_recv_wc *wc) |
1da177e4 | 1739 | { |
9874e746 | 1740 | struct ib_mad_send_wr_private *wr; |
fa9656bb JM |
1741 | struct ib_mad *mad; |
1742 | ||
9874e746 JM |
1743 | mad = (struct ib_mad *)wc->recv_buf.mad; |
1744 | ||
1745 | list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) { | |
1746 | if ((wr->tid == mad->mad_hdr.tid) && | |
1747 | rcv_has_same_class(wr, wc) && | |
1748 | /* | |
1749 | * Don't check GID for direct routed MADs. | |
1750 | * These might have permissive LIDs. | |
1751 | */ | |
1752 | (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) || | |
1753 | rcv_has_same_gid(mad_agent_priv, wr, wc))) | |
39798695 | 1754 | return (wr->status == IB_WC_SUCCESS) ? wr : NULL; |
1da177e4 LT |
1755 | } |
1756 | ||
1757 | /* | |
1758 | * It's possible to receive the response before we've | |
1759 | * been notified that the send has completed | |
1760 | */ | |
9874e746 JM |
1761 | list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) { |
1762 | if (is_data_mad(mad_agent_priv, wr->send_buf.mad) && | |
1763 | wr->tid == mad->mad_hdr.tid && | |
1764 | wr->timeout && | |
1765 | rcv_has_same_class(wr, wc) && | |
1766 | /* | |
1767 | * Don't check GID for direct routed MADs. | |
1768 | * These might have permissive LIDs. | |
1769 | */ | |
1770 | (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) || | |
1771 | rcv_has_same_gid(mad_agent_priv, wr, wc))) | |
1da177e4 | 1772 | /* Verify request has not been canceled */ |
9874e746 | 1773 | return (wr->status == IB_WC_SUCCESS) ? wr : NULL; |
1da177e4 LT |
1774 | } |
1775 | return NULL; | |
1776 | } | |
1777 | ||
fa619a77 | 1778 | void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr) |
6a0c435e HR |
1779 | { |
1780 | mad_send_wr->timeout = 0; | |
179e0917 AM |
1781 | if (mad_send_wr->refcount == 1) |
1782 | list_move_tail(&mad_send_wr->agent_list, | |
6a0c435e | 1783 | &mad_send_wr->mad_agent_priv->done_list); |
6a0c435e HR |
1784 | } |
1785 | ||
1da177e4 | 1786 | static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv, |
4a0754fa | 1787 | struct ib_mad_recv_wc *mad_recv_wc) |
1da177e4 LT |
1788 | { |
1789 | struct ib_mad_send_wr_private *mad_send_wr; | |
1790 | struct ib_mad_send_wc mad_send_wc; | |
1791 | unsigned long flags; | |
1792 | ||
fa619a77 HR |
1793 | INIT_LIST_HEAD(&mad_recv_wc->rmpp_list); |
1794 | list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list); | |
1795 | if (mad_agent_priv->agent.rmpp_version) { | |
1796 | mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv, | |
1797 | mad_recv_wc); | |
1798 | if (!mad_recv_wc) { | |
1b52fa98 | 1799 | deref_mad_agent(mad_agent_priv); |
fa619a77 HR |
1800 | return; |
1801 | } | |
1802 | } | |
1803 | ||
1da177e4 | 1804 | /* Complete corresponding request */ |
2527e681 | 1805 | if (ib_response_mad(mad_recv_wc->recv_buf.mad)) { |
1da177e4 | 1806 | spin_lock_irqsave(&mad_agent_priv->lock, flags); |
fa9656bb | 1807 | mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc); |
1da177e4 LT |
1808 | if (!mad_send_wr) { |
1809 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
4a0754fa | 1810 | ib_free_recv_mad(mad_recv_wc); |
1b52fa98 | 1811 | deref_mad_agent(mad_agent_priv); |
1da177e4 LT |
1812 | return; |
1813 | } | |
fa619a77 | 1814 | ib_mark_mad_done(mad_send_wr); |
1da177e4 LT |
1815 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
1816 | ||
1817 | /* Defined behavior is to complete response before request */ | |
34816ad9 | 1818 | mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf; |
4a0754fa HR |
1819 | mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, |
1820 | mad_recv_wc); | |
1da177e4 LT |
1821 | atomic_dec(&mad_agent_priv->refcount); |
1822 | ||
1823 | mad_send_wc.status = IB_WC_SUCCESS; | |
1824 | mad_send_wc.vendor_err = 0; | |
34816ad9 | 1825 | mad_send_wc.send_buf = &mad_send_wr->send_buf; |
1da177e4 LT |
1826 | ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc); |
1827 | } else { | |
4a0754fa HR |
1828 | mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, |
1829 | mad_recv_wc); | |
1b52fa98 | 1830 | deref_mad_agent(mad_agent_priv); |
1da177e4 LT |
1831 | } |
1832 | } | |
1833 | ||
1834 | static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv, | |
1835 | struct ib_wc *wc) | |
1836 | { | |
1837 | struct ib_mad_qp_info *qp_info; | |
1838 | struct ib_mad_private_header *mad_priv_hdr; | |
445d6807 | 1839 | struct ib_mad_private *recv, *response = NULL; |
1da177e4 LT |
1840 | struct ib_mad_list_head *mad_list; |
1841 | struct ib_mad_agent_private *mad_agent; | |
1bae4dbf | 1842 | int port_num; |
1da177e4 | 1843 | |
1da177e4 LT |
1844 | mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id; |
1845 | qp_info = mad_list->mad_queue->qp_info; | |
1846 | dequeue_mad(mad_list); | |
1847 | ||
1848 | mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header, | |
1849 | mad_list); | |
1850 | recv = container_of(mad_priv_hdr, struct ib_mad_private, header); | |
1527106f RC |
1851 | ib_dma_unmap_single(port_priv->device, |
1852 | recv->header.mapping, | |
1853 | sizeof(struct ib_mad_private) - | |
1854 | sizeof(struct ib_mad_private_header), | |
1855 | DMA_FROM_DEVICE); | |
1da177e4 LT |
1856 | |
1857 | /* Setup MAD receive work completion from "normal" work completion */ | |
24239aff SH |
1858 | recv->header.wc = *wc; |
1859 | recv->header.recv_wc.wc = &recv->header.wc; | |
1da177e4 LT |
1860 | recv->header.recv_wc.mad_len = sizeof(struct ib_mad); |
1861 | recv->header.recv_wc.recv_buf.mad = &recv->mad.mad; | |
1862 | recv->header.recv_wc.recv_buf.grh = &recv->grh; | |
1863 | ||
1864 | if (atomic_read(&qp_info->snoop_count)) | |
1865 | snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS); | |
1866 | ||
1867 | /* Validate MAD */ | |
1868 | if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num)) | |
1869 | goto out; | |
1870 | ||
445d6807 HR |
1871 | response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL); |
1872 | if (!response) { | |
1873 | printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory " | |
1874 | "for response buffer\n"); | |
1875 | goto out; | |
1876 | } | |
1877 | ||
1bae4dbf HR |
1878 | if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) |
1879 | port_num = wc->port_num; | |
1880 | else | |
1881 | port_num = port_priv->port_num; | |
1882 | ||
1da177e4 LT |
1883 | if (recv->mad.mad.mad_hdr.mgmt_class == |
1884 | IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) { | |
1bae4dbf HR |
1885 | enum smi_forward_action retsmi; |
1886 | ||
de493d47 HR |
1887 | if (smi_handle_dr_smp_recv(&recv->mad.smp, |
1888 | port_priv->device->node_type, | |
1bae4dbf | 1889 | port_num, |
de493d47 HR |
1890 | port_priv->device->phys_port_cnt) == |
1891 | IB_SMI_DISCARD) | |
1da177e4 | 1892 | goto out; |
de493d47 | 1893 | |
1bae4dbf HR |
1894 | retsmi = smi_check_forward_dr_smp(&recv->mad.smp); |
1895 | if (retsmi == IB_SMI_LOCAL) | |
1da177e4 | 1896 | goto local; |
de493d47 | 1897 | |
1bae4dbf HR |
1898 | if (retsmi == IB_SMI_SEND) { /* don't forward */ |
1899 | if (smi_handle_dr_smp_send(&recv->mad.smp, | |
1900 | port_priv->device->node_type, | |
1901 | port_num) == IB_SMI_DISCARD) | |
1902 | goto out; | |
1903 | ||
1904 | if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD) | |
1905 | goto out; | |
1906 | } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) { | |
1907 | /* forward case for switches */ | |
1908 | memcpy(response, recv, sizeof(*response)); | |
1909 | response->header.recv_wc.wc = &response->header.wc; | |
1910 | response->header.recv_wc.recv_buf.mad = &response->mad.mad; | |
1911 | response->header.recv_wc.recv_buf.grh = &response->grh; | |
1912 | ||
86dfbecd HR |
1913 | agent_send_response(&response->mad.mad, |
1914 | &response->grh, wc, | |
1915 | port_priv->device, | |
1916 | smi_get_fwd_port(&recv->mad.smp), | |
1917 | qp_info->qp->qp_num); | |
de493d47 | 1918 | |
1da177e4 | 1919 | goto out; |
1bae4dbf | 1920 | } |
1da177e4 LT |
1921 | } |
1922 | ||
1923 | local: | |
1924 | /* Give driver "right of first refusal" on incoming MAD */ | |
1925 | if (port_priv->device->process_mad) { | |
1926 | int ret; | |
1927 | ||
1da177e4 LT |
1928 | ret = port_priv->device->process_mad(port_priv->device, 0, |
1929 | port_priv->port_num, | |
1930 | wc, &recv->grh, | |
1931 | &recv->mad.mad, | |
1932 | &response->mad.mad); | |
1933 | if (ret & IB_MAD_RESULT_SUCCESS) { | |
1934 | if (ret & IB_MAD_RESULT_CONSUMED) | |
1935 | goto out; | |
1936 | if (ret & IB_MAD_RESULT_REPLY) { | |
34816ad9 SH |
1937 | agent_send_response(&response->mad.mad, |
1938 | &recv->grh, wc, | |
1939 | port_priv->device, | |
1bae4dbf | 1940 | port_num, |
34816ad9 | 1941 | qp_info->qp->qp_num); |
1da177e4 LT |
1942 | goto out; |
1943 | } | |
1944 | } | |
1945 | } | |
1946 | ||
4a0754fa | 1947 | mad_agent = find_mad_agent(port_priv, &recv->mad.mad); |
1da177e4 | 1948 | if (mad_agent) { |
4a0754fa | 1949 | ib_mad_complete_recv(mad_agent, &recv->header.recv_wc); |
1da177e4 LT |
1950 | /* |
1951 | * recv is freed up in error cases in ib_mad_complete_recv | |
1952 | * or via recv_handler in ib_mad_complete_recv() | |
1953 | */ | |
1954 | recv = NULL; | |
1955 | } | |
1956 | ||
1957 | out: | |
1958 | /* Post another receive request for this QP */ | |
1959 | if (response) { | |
1960 | ib_mad_post_receive_mads(qp_info, response); | |
1961 | if (recv) | |
1962 | kmem_cache_free(ib_mad_cache, recv); | |
1963 | } else | |
1964 | ib_mad_post_receive_mads(qp_info, recv); | |
1965 | } | |
1966 | ||
1967 | static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv) | |
1968 | { | |
1969 | struct ib_mad_send_wr_private *mad_send_wr; | |
1970 | unsigned long delay; | |
1971 | ||
1972 | if (list_empty(&mad_agent_priv->wait_list)) { | |
6b2eef8f | 1973 | __cancel_delayed_work(&mad_agent_priv->timed_work); |
1da177e4 LT |
1974 | } else { |
1975 | mad_send_wr = list_entry(mad_agent_priv->wait_list.next, | |
1976 | struct ib_mad_send_wr_private, | |
1977 | agent_list); | |
1978 | ||
1979 | if (time_after(mad_agent_priv->timeout, | |
1980 | mad_send_wr->timeout)) { | |
1981 | mad_agent_priv->timeout = mad_send_wr->timeout; | |
6b2eef8f | 1982 | __cancel_delayed_work(&mad_agent_priv->timed_work); |
1da177e4 LT |
1983 | delay = mad_send_wr->timeout - jiffies; |
1984 | if ((long)delay <= 0) | |
1985 | delay = 1; | |
1986 | queue_delayed_work(mad_agent_priv->qp_info-> | |
1987 | port_priv->wq, | |
1988 | &mad_agent_priv->timed_work, delay); | |
1989 | } | |
1990 | } | |
1991 | } | |
1992 | ||
d760ce8f | 1993 | static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr) |
1da177e4 | 1994 | { |
d760ce8f | 1995 | struct ib_mad_agent_private *mad_agent_priv; |
1da177e4 LT |
1996 | struct ib_mad_send_wr_private *temp_mad_send_wr; |
1997 | struct list_head *list_item; | |
1998 | unsigned long delay; | |
1999 | ||
d760ce8f | 2000 | mad_agent_priv = mad_send_wr->mad_agent_priv; |
1da177e4 LT |
2001 | list_del(&mad_send_wr->agent_list); |
2002 | ||
2003 | delay = mad_send_wr->timeout; | |
2004 | mad_send_wr->timeout += jiffies; | |
2005 | ||
29bb33dd HR |
2006 | if (delay) { |
2007 | list_for_each_prev(list_item, &mad_agent_priv->wait_list) { | |
2008 | temp_mad_send_wr = list_entry(list_item, | |
2009 | struct ib_mad_send_wr_private, | |
2010 | agent_list); | |
2011 | if (time_after(mad_send_wr->timeout, | |
2012 | temp_mad_send_wr->timeout)) | |
2013 | break; | |
2014 | } | |
1da177e4 | 2015 | } |
29bb33dd HR |
2016 | else |
2017 | list_item = &mad_agent_priv->wait_list; | |
1da177e4 LT |
2018 | list_add(&mad_send_wr->agent_list, list_item); |
2019 | ||
2020 | /* Reschedule a work item if we have a shorter timeout */ | |
2021 | if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) { | |
6b2eef8f | 2022 | __cancel_delayed_work(&mad_agent_priv->timed_work); |
1da177e4 LT |
2023 | queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq, |
2024 | &mad_agent_priv->timed_work, delay); | |
2025 | } | |
2026 | } | |
2027 | ||
03b61ad2 HR |
2028 | void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr, |
2029 | int timeout_ms) | |
2030 | { | |
2031 | mad_send_wr->timeout = msecs_to_jiffies(timeout_ms); | |
2032 | wait_for_response(mad_send_wr); | |
2033 | } | |
2034 | ||
1da177e4 LT |
2035 | /* |
2036 | * Process a send work completion | |
2037 | */ | |
fa619a77 HR |
2038 | void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr, |
2039 | struct ib_mad_send_wc *mad_send_wc) | |
1da177e4 LT |
2040 | { |
2041 | struct ib_mad_agent_private *mad_agent_priv; | |
2042 | unsigned long flags; | |
fa619a77 | 2043 | int ret; |
1da177e4 | 2044 | |
d760ce8f | 2045 | mad_agent_priv = mad_send_wr->mad_agent_priv; |
1da177e4 | 2046 | spin_lock_irqsave(&mad_agent_priv->lock, flags); |
fa619a77 HR |
2047 | if (mad_agent_priv->agent.rmpp_version) { |
2048 | ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc); | |
2049 | if (ret == IB_RMPP_RESULT_CONSUMED) | |
2050 | goto done; | |
2051 | } else | |
2052 | ret = IB_RMPP_RESULT_UNHANDLED; | |
2053 | ||
1da177e4 LT |
2054 | if (mad_send_wc->status != IB_WC_SUCCESS && |
2055 | mad_send_wr->status == IB_WC_SUCCESS) { | |
2056 | mad_send_wr->status = mad_send_wc->status; | |
2057 | mad_send_wr->refcount -= (mad_send_wr->timeout > 0); | |
2058 | } | |
2059 | ||
2060 | if (--mad_send_wr->refcount > 0) { | |
2061 | if (mad_send_wr->refcount == 1 && mad_send_wr->timeout && | |
2062 | mad_send_wr->status == IB_WC_SUCCESS) { | |
d760ce8f | 2063 | wait_for_response(mad_send_wr); |
1da177e4 | 2064 | } |
fa619a77 | 2065 | goto done; |
1da177e4 LT |
2066 | } |
2067 | ||
2068 | /* Remove send from MAD agent and notify client of completion */ | |
2069 | list_del(&mad_send_wr->agent_list); | |
2070 | adjust_timeout(mad_agent_priv); | |
2071 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
2072 | ||
2073 | if (mad_send_wr->status != IB_WC_SUCCESS ) | |
2074 | mad_send_wc->status = mad_send_wr->status; | |
34816ad9 SH |
2075 | if (ret == IB_RMPP_RESULT_INTERNAL) |
2076 | ib_rmpp_send_handler(mad_send_wc); | |
2077 | else | |
fa619a77 HR |
2078 | mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, |
2079 | mad_send_wc); | |
1da177e4 LT |
2080 | |
2081 | /* Release reference on agent taken when sending */ | |
1b52fa98 | 2082 | deref_mad_agent(mad_agent_priv); |
fa619a77 HR |
2083 | return; |
2084 | done: | |
2085 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
1da177e4 LT |
2086 | } |
2087 | ||
2088 | static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv, | |
2089 | struct ib_wc *wc) | |
2090 | { | |
2091 | struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr; | |
2092 | struct ib_mad_list_head *mad_list; | |
2093 | struct ib_mad_qp_info *qp_info; | |
2094 | struct ib_mad_queue *send_queue; | |
2095 | struct ib_send_wr *bad_send_wr; | |
34816ad9 | 2096 | struct ib_mad_send_wc mad_send_wc; |
1da177e4 LT |
2097 | unsigned long flags; |
2098 | int ret; | |
2099 | ||
2100 | mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id; | |
2101 | mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private, | |
2102 | mad_list); | |
2103 | send_queue = mad_list->mad_queue; | |
2104 | qp_info = send_queue->qp_info; | |
2105 | ||
2106 | retry: | |
1527106f RC |
2107 | ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device, |
2108 | mad_send_wr->header_mapping, | |
2109 | mad_send_wr->sg_list[0].length, DMA_TO_DEVICE); | |
2110 | ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device, | |
2111 | mad_send_wr->payload_mapping, | |
2112 | mad_send_wr->sg_list[1].length, DMA_TO_DEVICE); | |
1da177e4 LT |
2113 | queued_send_wr = NULL; |
2114 | spin_lock_irqsave(&send_queue->lock, flags); | |
2115 | list_del(&mad_list->list); | |
2116 | ||
2117 | /* Move queued send to the send queue */ | |
2118 | if (send_queue->count-- > send_queue->max_active) { | |
2119 | mad_list = container_of(qp_info->overflow_list.next, | |
2120 | struct ib_mad_list_head, list); | |
2121 | queued_send_wr = container_of(mad_list, | |
2122 | struct ib_mad_send_wr_private, | |
2123 | mad_list); | |
179e0917 | 2124 | list_move_tail(&mad_list->list, &send_queue->list); |
1da177e4 LT |
2125 | } |
2126 | spin_unlock_irqrestore(&send_queue->lock, flags); | |
2127 | ||
34816ad9 SH |
2128 | mad_send_wc.send_buf = &mad_send_wr->send_buf; |
2129 | mad_send_wc.status = wc->status; | |
2130 | mad_send_wc.vendor_err = wc->vendor_err; | |
1da177e4 | 2131 | if (atomic_read(&qp_info->snoop_count)) |
34816ad9 | 2132 | snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc, |
1da177e4 | 2133 | IB_MAD_SNOOP_SEND_COMPLETIONS); |
34816ad9 | 2134 | ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc); |
1da177e4 LT |
2135 | |
2136 | if (queued_send_wr) { | |
2137 | ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr, | |
34816ad9 | 2138 | &bad_send_wr); |
1da177e4 LT |
2139 | if (ret) { |
2140 | printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret); | |
2141 | mad_send_wr = queued_send_wr; | |
2142 | wc->status = IB_WC_LOC_QP_OP_ERR; | |
2143 | goto retry; | |
2144 | } | |
2145 | } | |
2146 | } | |
2147 | ||
2148 | static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info) | |
2149 | { | |
2150 | struct ib_mad_send_wr_private *mad_send_wr; | |
2151 | struct ib_mad_list_head *mad_list; | |
2152 | unsigned long flags; | |
2153 | ||
2154 | spin_lock_irqsave(&qp_info->send_queue.lock, flags); | |
2155 | list_for_each_entry(mad_list, &qp_info->send_queue.list, list) { | |
2156 | mad_send_wr = container_of(mad_list, | |
2157 | struct ib_mad_send_wr_private, | |
2158 | mad_list); | |
2159 | mad_send_wr->retry = 1; | |
2160 | } | |
2161 | spin_unlock_irqrestore(&qp_info->send_queue.lock, flags); | |
2162 | } | |
2163 | ||
2164 | static void mad_error_handler(struct ib_mad_port_private *port_priv, | |
2165 | struct ib_wc *wc) | |
2166 | { | |
2167 | struct ib_mad_list_head *mad_list; | |
2168 | struct ib_mad_qp_info *qp_info; | |
2169 | struct ib_mad_send_wr_private *mad_send_wr; | |
2170 | int ret; | |
2171 | ||
2172 | /* Determine if failure was a send or receive */ | |
2173 | mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id; | |
2174 | qp_info = mad_list->mad_queue->qp_info; | |
2175 | if (mad_list->mad_queue == &qp_info->recv_queue) | |
2176 | /* | |
2177 | * Receive errors indicate that the QP has entered the error | |
2178 | * state - error handling/shutdown code will cleanup | |
2179 | */ | |
2180 | return; | |
2181 | ||
2182 | /* | |
2183 | * Send errors will transition the QP to SQE - move | |
2184 | * QP to RTS and repost flushed work requests | |
2185 | */ | |
2186 | mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private, | |
2187 | mad_list); | |
2188 | if (wc->status == IB_WC_WR_FLUSH_ERR) { | |
2189 | if (mad_send_wr->retry) { | |
2190 | /* Repost send */ | |
2191 | struct ib_send_wr *bad_send_wr; | |
2192 | ||
2193 | mad_send_wr->retry = 0; | |
2194 | ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr, | |
2195 | &bad_send_wr); | |
2196 | if (ret) | |
2197 | ib_mad_send_done_handler(port_priv, wc); | |
2198 | } else | |
2199 | ib_mad_send_done_handler(port_priv, wc); | |
2200 | } else { | |
2201 | struct ib_qp_attr *attr; | |
2202 | ||
2203 | /* Transition QP to RTS and fail offending send */ | |
2204 | attr = kmalloc(sizeof *attr, GFP_KERNEL); | |
2205 | if (attr) { | |
2206 | attr->qp_state = IB_QPS_RTS; | |
2207 | attr->cur_qp_state = IB_QPS_SQE; | |
2208 | ret = ib_modify_qp(qp_info->qp, attr, | |
2209 | IB_QP_STATE | IB_QP_CUR_STATE); | |
2210 | kfree(attr); | |
2211 | if (ret) | |
2212 | printk(KERN_ERR PFX "mad_error_handler - " | |
2213 | "ib_modify_qp to RTS : %d\n", ret); | |
2214 | else | |
2215 | mark_sends_for_retry(qp_info); | |
2216 | } | |
2217 | ib_mad_send_done_handler(port_priv, wc); | |
2218 | } | |
2219 | } | |
2220 | ||
2221 | /* | |
2222 | * IB MAD completion callback | |
2223 | */ | |
c4028958 | 2224 | static void ib_mad_completion_handler(struct work_struct *work) |
1da177e4 LT |
2225 | { |
2226 | struct ib_mad_port_private *port_priv; | |
2227 | struct ib_wc wc; | |
2228 | ||
c4028958 | 2229 | port_priv = container_of(work, struct ib_mad_port_private, work); |
1da177e4 LT |
2230 | ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP); |
2231 | ||
2232 | while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) { | |
2233 | if (wc.status == IB_WC_SUCCESS) { | |
2234 | switch (wc.opcode) { | |
2235 | case IB_WC_SEND: | |
2236 | ib_mad_send_done_handler(port_priv, &wc); | |
2237 | break; | |
2238 | case IB_WC_RECV: | |
2239 | ib_mad_recv_done_handler(port_priv, &wc); | |
2240 | break; | |
2241 | default: | |
2242 | BUG_ON(1); | |
2243 | break; | |
2244 | } | |
2245 | } else | |
2246 | mad_error_handler(port_priv, &wc); | |
2247 | } | |
2248 | } | |
2249 | ||
2250 | static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv) | |
2251 | { | |
2252 | unsigned long flags; | |
2253 | struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr; | |
2254 | struct ib_mad_send_wc mad_send_wc; | |
2255 | struct list_head cancel_list; | |
2256 | ||
2257 | INIT_LIST_HEAD(&cancel_list); | |
2258 | ||
2259 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
2260 | list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr, | |
2261 | &mad_agent_priv->send_list, agent_list) { | |
2262 | if (mad_send_wr->status == IB_WC_SUCCESS) { | |
3cd96564 | 2263 | mad_send_wr->status = IB_WC_WR_FLUSH_ERR; |
1da177e4 LT |
2264 | mad_send_wr->refcount -= (mad_send_wr->timeout > 0); |
2265 | } | |
2266 | } | |
2267 | ||
2268 | /* Empty wait list to prevent receives from finding a request */ | |
2269 | list_splice_init(&mad_agent_priv->wait_list, &cancel_list); | |
2270 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
2271 | ||
2272 | /* Report all cancelled requests */ | |
2273 | mad_send_wc.status = IB_WC_WR_FLUSH_ERR; | |
2274 | mad_send_wc.vendor_err = 0; | |
2275 | ||
2276 | list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr, | |
2277 | &cancel_list, agent_list) { | |
34816ad9 SH |
2278 | mad_send_wc.send_buf = &mad_send_wr->send_buf; |
2279 | list_del(&mad_send_wr->agent_list); | |
1da177e4 LT |
2280 | mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, |
2281 | &mad_send_wc); | |
1da177e4 LT |
2282 | atomic_dec(&mad_agent_priv->refcount); |
2283 | } | |
2284 | } | |
2285 | ||
2286 | static struct ib_mad_send_wr_private* | |
34816ad9 SH |
2287 | find_send_wr(struct ib_mad_agent_private *mad_agent_priv, |
2288 | struct ib_mad_send_buf *send_buf) | |
1da177e4 LT |
2289 | { |
2290 | struct ib_mad_send_wr_private *mad_send_wr; | |
2291 | ||
2292 | list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list, | |
2293 | agent_list) { | |
34816ad9 | 2294 | if (&mad_send_wr->send_buf == send_buf) |
1da177e4 LT |
2295 | return mad_send_wr; |
2296 | } | |
2297 | ||
2298 | list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list, | |
2299 | agent_list) { | |
34816ad9 SH |
2300 | if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) && |
2301 | &mad_send_wr->send_buf == send_buf) | |
1da177e4 LT |
2302 | return mad_send_wr; |
2303 | } | |
2304 | return NULL; | |
2305 | } | |
2306 | ||
34816ad9 SH |
2307 | int ib_modify_mad(struct ib_mad_agent *mad_agent, |
2308 | struct ib_mad_send_buf *send_buf, u32 timeout_ms) | |
1da177e4 LT |
2309 | { |
2310 | struct ib_mad_agent_private *mad_agent_priv; | |
2311 | struct ib_mad_send_wr_private *mad_send_wr; | |
2312 | unsigned long flags; | |
cabe3cbc | 2313 | int active; |
1da177e4 LT |
2314 | |
2315 | mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private, | |
2316 | agent); | |
2317 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
34816ad9 | 2318 | mad_send_wr = find_send_wr(mad_agent_priv, send_buf); |
03b61ad2 | 2319 | if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) { |
1da177e4 | 2320 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
03b61ad2 | 2321 | return -EINVAL; |
1da177e4 LT |
2322 | } |
2323 | ||
cabe3cbc | 2324 | active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1); |
03b61ad2 | 2325 | if (!timeout_ms) { |
1da177e4 | 2326 | mad_send_wr->status = IB_WC_WR_FLUSH_ERR; |
03b61ad2 | 2327 | mad_send_wr->refcount -= (mad_send_wr->timeout > 0); |
1da177e4 LT |
2328 | } |
2329 | ||
34816ad9 | 2330 | mad_send_wr->send_buf.timeout_ms = timeout_ms; |
cabe3cbc | 2331 | if (active) |
03b61ad2 HR |
2332 | mad_send_wr->timeout = msecs_to_jiffies(timeout_ms); |
2333 | else | |
2334 | ib_reset_mad_timeout(mad_send_wr, timeout_ms); | |
2335 | ||
1da177e4 | 2336 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
03b61ad2 HR |
2337 | return 0; |
2338 | } | |
2339 | EXPORT_SYMBOL(ib_modify_mad); | |
1da177e4 | 2340 | |
34816ad9 SH |
2341 | void ib_cancel_mad(struct ib_mad_agent *mad_agent, |
2342 | struct ib_mad_send_buf *send_buf) | |
03b61ad2 | 2343 | { |
34816ad9 | 2344 | ib_modify_mad(mad_agent, send_buf, 0); |
1da177e4 LT |
2345 | } |
2346 | EXPORT_SYMBOL(ib_cancel_mad); | |
2347 | ||
c4028958 | 2348 | static void local_completions(struct work_struct *work) |
1da177e4 LT |
2349 | { |
2350 | struct ib_mad_agent_private *mad_agent_priv; | |
2351 | struct ib_mad_local_private *local; | |
2352 | struct ib_mad_agent_private *recv_mad_agent; | |
2353 | unsigned long flags; | |
1d9bc6d6 | 2354 | int free_mad; |
1da177e4 LT |
2355 | struct ib_wc wc; |
2356 | struct ib_mad_send_wc mad_send_wc; | |
2357 | ||
c4028958 DH |
2358 | mad_agent_priv = |
2359 | container_of(work, struct ib_mad_agent_private, local_work); | |
1da177e4 LT |
2360 | |
2361 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
2362 | while (!list_empty(&mad_agent_priv->local_list)) { | |
2363 | local = list_entry(mad_agent_priv->local_list.next, | |
2364 | struct ib_mad_local_private, | |
2365 | completion_list); | |
37289efe | 2366 | list_del(&local->completion_list); |
1da177e4 | 2367 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
1d9bc6d6 | 2368 | free_mad = 0; |
1da177e4 LT |
2369 | if (local->mad_priv) { |
2370 | recv_mad_agent = local->recv_mad_agent; | |
2371 | if (!recv_mad_agent) { | |
2372 | printk(KERN_ERR PFX "No receive MAD agent for local completion\n"); | |
1d9bc6d6 | 2373 | free_mad = 1; |
1da177e4 LT |
2374 | goto local_send_completion; |
2375 | } | |
2376 | ||
2377 | /* | |
2378 | * Defined behavior is to complete response | |
2379 | * before request | |
2380 | */ | |
062dbb69 MT |
2381 | build_smp_wc(recv_mad_agent->agent.qp, |
2382 | (unsigned long) local->mad_send_wr, | |
97f52eb4 | 2383 | be16_to_cpu(IB_LID_PERMISSIVE), |
34816ad9 | 2384 | 0, recv_mad_agent->agent.port_num, &wc); |
1da177e4 LT |
2385 | |
2386 | local->mad_priv->header.recv_wc.wc = &wc; | |
2387 | local->mad_priv->header.recv_wc.mad_len = | |
2388 | sizeof(struct ib_mad); | |
fa619a77 HR |
2389 | INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list); |
2390 | list_add(&local->mad_priv->header.recv_wc.recv_buf.list, | |
2391 | &local->mad_priv->header.recv_wc.rmpp_list); | |
1da177e4 LT |
2392 | local->mad_priv->header.recv_wc.recv_buf.grh = NULL; |
2393 | local->mad_priv->header.recv_wc.recv_buf.mad = | |
2394 | &local->mad_priv->mad.mad; | |
2395 | if (atomic_read(&recv_mad_agent->qp_info->snoop_count)) | |
2396 | snoop_recv(recv_mad_agent->qp_info, | |
2397 | &local->mad_priv->header.recv_wc, | |
2398 | IB_MAD_SNOOP_RECVS); | |
2399 | recv_mad_agent->agent.recv_handler( | |
2400 | &recv_mad_agent->agent, | |
2401 | &local->mad_priv->header.recv_wc); | |
2402 | spin_lock_irqsave(&recv_mad_agent->lock, flags); | |
2403 | atomic_dec(&recv_mad_agent->refcount); | |
2404 | spin_unlock_irqrestore(&recv_mad_agent->lock, flags); | |
2405 | } | |
2406 | ||
2407 | local_send_completion: | |
2408 | /* Complete send */ | |
2409 | mad_send_wc.status = IB_WC_SUCCESS; | |
2410 | mad_send_wc.vendor_err = 0; | |
34816ad9 | 2411 | mad_send_wc.send_buf = &local->mad_send_wr->send_buf; |
1da177e4 | 2412 | if (atomic_read(&mad_agent_priv->qp_info->snoop_count)) |
34816ad9 SH |
2413 | snoop_send(mad_agent_priv->qp_info, |
2414 | &local->mad_send_wr->send_buf, | |
2415 | &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS); | |
1da177e4 LT |
2416 | mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, |
2417 | &mad_send_wc); | |
2418 | ||
2419 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
1da177e4 | 2420 | atomic_dec(&mad_agent_priv->refcount); |
1d9bc6d6 | 2421 | if (free_mad) |
2c153b93 | 2422 | kmem_cache_free(ib_mad_cache, local->mad_priv); |
1da177e4 LT |
2423 | kfree(local); |
2424 | } | |
2425 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
2426 | } | |
2427 | ||
f75b7a52 HR |
2428 | static int retry_send(struct ib_mad_send_wr_private *mad_send_wr) |
2429 | { | |
2430 | int ret; | |
2431 | ||
4fc8cd49 | 2432 | if (!mad_send_wr->retries_left) |
f75b7a52 HR |
2433 | return -ETIMEDOUT; |
2434 | ||
4fc8cd49 SH |
2435 | mad_send_wr->retries_left--; |
2436 | mad_send_wr->send_buf.retries++; | |
2437 | ||
34816ad9 | 2438 | mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms); |
f75b7a52 | 2439 | |
fa619a77 HR |
2440 | if (mad_send_wr->mad_agent_priv->agent.rmpp_version) { |
2441 | ret = ib_retry_rmpp(mad_send_wr); | |
2442 | switch (ret) { | |
2443 | case IB_RMPP_RESULT_UNHANDLED: | |
2444 | ret = ib_send_mad(mad_send_wr); | |
2445 | break; | |
2446 | case IB_RMPP_RESULT_CONSUMED: | |
2447 | ret = 0; | |
2448 | break; | |
2449 | default: | |
2450 | ret = -ECOMM; | |
2451 | break; | |
2452 | } | |
2453 | } else | |
2454 | ret = ib_send_mad(mad_send_wr); | |
f75b7a52 HR |
2455 | |
2456 | if (!ret) { | |
2457 | mad_send_wr->refcount++; | |
f75b7a52 HR |
2458 | list_add_tail(&mad_send_wr->agent_list, |
2459 | &mad_send_wr->mad_agent_priv->send_list); | |
2460 | } | |
2461 | return ret; | |
2462 | } | |
2463 | ||
c4028958 | 2464 | static void timeout_sends(struct work_struct *work) |
1da177e4 LT |
2465 | { |
2466 | struct ib_mad_agent_private *mad_agent_priv; | |
2467 | struct ib_mad_send_wr_private *mad_send_wr; | |
2468 | struct ib_mad_send_wc mad_send_wc; | |
2469 | unsigned long flags, delay; | |
2470 | ||
c4028958 DH |
2471 | mad_agent_priv = container_of(work, struct ib_mad_agent_private, |
2472 | timed_work.work); | |
1da177e4 LT |
2473 | mad_send_wc.vendor_err = 0; |
2474 | ||
2475 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
2476 | while (!list_empty(&mad_agent_priv->wait_list)) { | |
2477 | mad_send_wr = list_entry(mad_agent_priv->wait_list.next, | |
2478 | struct ib_mad_send_wr_private, | |
2479 | agent_list); | |
2480 | ||
2481 | if (time_after(mad_send_wr->timeout, jiffies)) { | |
2482 | delay = mad_send_wr->timeout - jiffies; | |
2483 | if ((long)delay <= 0) | |
2484 | delay = 1; | |
2485 | queue_delayed_work(mad_agent_priv->qp_info-> | |
2486 | port_priv->wq, | |
2487 | &mad_agent_priv->timed_work, delay); | |
2488 | break; | |
2489 | } | |
2490 | ||
dbf9227b | 2491 | list_del(&mad_send_wr->agent_list); |
29bb33dd HR |
2492 | if (mad_send_wr->status == IB_WC_SUCCESS && |
2493 | !retry_send(mad_send_wr)) | |
f75b7a52 HR |
2494 | continue; |
2495 | ||
1da177e4 LT |
2496 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
2497 | ||
03b61ad2 HR |
2498 | if (mad_send_wr->status == IB_WC_SUCCESS) |
2499 | mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR; | |
2500 | else | |
2501 | mad_send_wc.status = mad_send_wr->status; | |
34816ad9 | 2502 | mad_send_wc.send_buf = &mad_send_wr->send_buf; |
1da177e4 LT |
2503 | mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, |
2504 | &mad_send_wc); | |
2505 | ||
1da177e4 LT |
2506 | atomic_dec(&mad_agent_priv->refcount); |
2507 | spin_lock_irqsave(&mad_agent_priv->lock, flags); | |
2508 | } | |
2509 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); | |
2510 | } | |
2511 | ||
5dd2ce12 | 2512 | static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg) |
1da177e4 LT |
2513 | { |
2514 | struct ib_mad_port_private *port_priv = cq->cq_context; | |
dc05980d | 2515 | unsigned long flags; |
1da177e4 | 2516 | |
dc05980d MT |
2517 | spin_lock_irqsave(&ib_mad_port_list_lock, flags); |
2518 | if (!list_empty(&port_priv->port_list)) | |
2519 | queue_work(port_priv->wq, &port_priv->work); | |
2520 | spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); | |
1da177e4 LT |
2521 | } |
2522 | ||
2523 | /* | |
2524 | * Allocate receive MADs and post receive WRs for them | |
2525 | */ | |
2526 | static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info, | |
2527 | struct ib_mad_private *mad) | |
2528 | { | |
2529 | unsigned long flags; | |
2530 | int post, ret; | |
2531 | struct ib_mad_private *mad_priv; | |
2532 | struct ib_sge sg_list; | |
2533 | struct ib_recv_wr recv_wr, *bad_recv_wr; | |
2534 | struct ib_mad_queue *recv_queue = &qp_info->recv_queue; | |
2535 | ||
2536 | /* Initialize common scatter list fields */ | |
2537 | sg_list.length = sizeof *mad_priv - sizeof mad_priv->header; | |
2538 | sg_list.lkey = (*qp_info->port_priv->mr).lkey; | |
2539 | ||
2540 | /* Initialize common receive WR fields */ | |
2541 | recv_wr.next = NULL; | |
2542 | recv_wr.sg_list = &sg_list; | |
2543 | recv_wr.num_sge = 1; | |
2544 | ||
2545 | do { | |
2546 | /* Allocate and map receive buffer */ | |
2547 | if (mad) { | |
2548 | mad_priv = mad; | |
2549 | mad = NULL; | |
2550 | } else { | |
2551 | mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL); | |
2552 | if (!mad_priv) { | |
2553 | printk(KERN_ERR PFX "No memory for receive buffer\n"); | |
2554 | ret = -ENOMEM; | |
2555 | break; | |
2556 | } | |
2557 | } | |
1527106f RC |
2558 | sg_list.addr = ib_dma_map_single(qp_info->port_priv->device, |
2559 | &mad_priv->grh, | |
2560 | sizeof *mad_priv - | |
2561 | sizeof mad_priv->header, | |
2562 | DMA_FROM_DEVICE); | |
2563 | mad_priv->header.mapping = sg_list.addr; | |
1da177e4 LT |
2564 | recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list; |
2565 | mad_priv->header.mad_list.mad_queue = recv_queue; | |
2566 | ||
2567 | /* Post receive WR */ | |
2568 | spin_lock_irqsave(&recv_queue->lock, flags); | |
2569 | post = (++recv_queue->count < recv_queue->max_active); | |
2570 | list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list); | |
2571 | spin_unlock_irqrestore(&recv_queue->lock, flags); | |
2572 | ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr); | |
2573 | if (ret) { | |
2574 | spin_lock_irqsave(&recv_queue->lock, flags); | |
2575 | list_del(&mad_priv->header.mad_list.list); | |
2576 | recv_queue->count--; | |
2577 | spin_unlock_irqrestore(&recv_queue->lock, flags); | |
1527106f RC |
2578 | ib_dma_unmap_single(qp_info->port_priv->device, |
2579 | mad_priv->header.mapping, | |
2580 | sizeof *mad_priv - | |
2581 | sizeof mad_priv->header, | |
2582 | DMA_FROM_DEVICE); | |
1da177e4 LT |
2583 | kmem_cache_free(ib_mad_cache, mad_priv); |
2584 | printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret); | |
2585 | break; | |
2586 | } | |
2587 | } while (post); | |
2588 | ||
2589 | return ret; | |
2590 | } | |
2591 | ||
2592 | /* | |
2593 | * Return all the posted receive MADs | |
2594 | */ | |
2595 | static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info) | |
2596 | { | |
2597 | struct ib_mad_private_header *mad_priv_hdr; | |
2598 | struct ib_mad_private *recv; | |
2599 | struct ib_mad_list_head *mad_list; | |
2600 | ||
2601 | while (!list_empty(&qp_info->recv_queue.list)) { | |
2602 | ||
2603 | mad_list = list_entry(qp_info->recv_queue.list.next, | |
2604 | struct ib_mad_list_head, list); | |
2605 | mad_priv_hdr = container_of(mad_list, | |
2606 | struct ib_mad_private_header, | |
2607 | mad_list); | |
2608 | recv = container_of(mad_priv_hdr, struct ib_mad_private, | |
2609 | header); | |
2610 | ||
2611 | /* Remove from posted receive MAD list */ | |
2612 | list_del(&mad_list->list); | |
2613 | ||
1527106f RC |
2614 | ib_dma_unmap_single(qp_info->port_priv->device, |
2615 | recv->header.mapping, | |
2616 | sizeof(struct ib_mad_private) - | |
2617 | sizeof(struct ib_mad_private_header), | |
2618 | DMA_FROM_DEVICE); | |
1da177e4 LT |
2619 | kmem_cache_free(ib_mad_cache, recv); |
2620 | } | |
2621 | ||
2622 | qp_info->recv_queue.count = 0; | |
2623 | } | |
2624 | ||
2625 | /* | |
2626 | * Start the port | |
2627 | */ | |
2628 | static int ib_mad_port_start(struct ib_mad_port_private *port_priv) | |
2629 | { | |
2630 | int ret, i; | |
2631 | struct ib_qp_attr *attr; | |
2632 | struct ib_qp *qp; | |
2633 | ||
2634 | attr = kmalloc(sizeof *attr, GFP_KERNEL); | |
3cd96564 | 2635 | if (!attr) { |
1da177e4 LT |
2636 | printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n"); |
2637 | return -ENOMEM; | |
2638 | } | |
2639 | ||
2640 | for (i = 0; i < IB_MAD_QPS_CORE; i++) { | |
2641 | qp = port_priv->qp_info[i].qp; | |
2642 | /* | |
2643 | * PKey index for QP1 is irrelevant but | |
2644 | * one is needed for the Reset to Init transition | |
2645 | */ | |
2646 | attr->qp_state = IB_QPS_INIT; | |
2647 | attr->pkey_index = 0; | |
2648 | attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY; | |
2649 | ret = ib_modify_qp(qp, attr, IB_QP_STATE | | |
2650 | IB_QP_PKEY_INDEX | IB_QP_QKEY); | |
2651 | if (ret) { | |
2652 | printk(KERN_ERR PFX "Couldn't change QP%d state to " | |
2653 | "INIT: %d\n", i, ret); | |
2654 | goto out; | |
2655 | } | |
2656 | ||
2657 | attr->qp_state = IB_QPS_RTR; | |
2658 | ret = ib_modify_qp(qp, attr, IB_QP_STATE); | |
2659 | if (ret) { | |
2660 | printk(KERN_ERR PFX "Couldn't change QP%d state to " | |
2661 | "RTR: %d\n", i, ret); | |
2662 | goto out; | |
2663 | } | |
2664 | ||
2665 | attr->qp_state = IB_QPS_RTS; | |
2666 | attr->sq_psn = IB_MAD_SEND_Q_PSN; | |
2667 | ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN); | |
2668 | if (ret) { | |
2669 | printk(KERN_ERR PFX "Couldn't change QP%d state to " | |
2670 | "RTS: %d\n", i, ret); | |
2671 | goto out; | |
2672 | } | |
2673 | } | |
2674 | ||
2675 | ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP); | |
2676 | if (ret) { | |
2677 | printk(KERN_ERR PFX "Failed to request completion " | |
2678 | "notification: %d\n", ret); | |
2679 | goto out; | |
2680 | } | |
2681 | ||
2682 | for (i = 0; i < IB_MAD_QPS_CORE; i++) { | |
2683 | ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL); | |
2684 | if (ret) { | |
2685 | printk(KERN_ERR PFX "Couldn't post receive WRs\n"); | |
2686 | goto out; | |
2687 | } | |
2688 | } | |
2689 | out: | |
2690 | kfree(attr); | |
2691 | return ret; | |
2692 | } | |
2693 | ||
2694 | static void qp_event_handler(struct ib_event *event, void *qp_context) | |
2695 | { | |
2696 | struct ib_mad_qp_info *qp_info = qp_context; | |
2697 | ||
2698 | /* It's worse than that! He's dead, Jim! */ | |
2699 | printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n", | |
2700 | event->event, qp_info->qp->qp_num); | |
2701 | } | |
2702 | ||
2703 | static void init_mad_queue(struct ib_mad_qp_info *qp_info, | |
2704 | struct ib_mad_queue *mad_queue) | |
2705 | { | |
2706 | mad_queue->qp_info = qp_info; | |
2707 | mad_queue->count = 0; | |
2708 | spin_lock_init(&mad_queue->lock); | |
2709 | INIT_LIST_HEAD(&mad_queue->list); | |
2710 | } | |
2711 | ||
2712 | static void init_mad_qp(struct ib_mad_port_private *port_priv, | |
2713 | struct ib_mad_qp_info *qp_info) | |
2714 | { | |
2715 | qp_info->port_priv = port_priv; | |
2716 | init_mad_queue(qp_info, &qp_info->send_queue); | |
2717 | init_mad_queue(qp_info, &qp_info->recv_queue); | |
2718 | INIT_LIST_HEAD(&qp_info->overflow_list); | |
2719 | spin_lock_init(&qp_info->snoop_lock); | |
2720 | qp_info->snoop_table = NULL; | |
2721 | qp_info->snoop_table_size = 0; | |
2722 | atomic_set(&qp_info->snoop_count, 0); | |
2723 | } | |
2724 | ||
2725 | static int create_mad_qp(struct ib_mad_qp_info *qp_info, | |
2726 | enum ib_qp_type qp_type) | |
2727 | { | |
2728 | struct ib_qp_init_attr qp_init_attr; | |
2729 | int ret; | |
2730 | ||
2731 | memset(&qp_init_attr, 0, sizeof qp_init_attr); | |
2732 | qp_init_attr.send_cq = qp_info->port_priv->cq; | |
2733 | qp_init_attr.recv_cq = qp_info->port_priv->cq; | |
2734 | qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR; | |
b76aabc3 HR |
2735 | qp_init_attr.cap.max_send_wr = mad_sendq_size; |
2736 | qp_init_attr.cap.max_recv_wr = mad_recvq_size; | |
1da177e4 LT |
2737 | qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG; |
2738 | qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG; | |
2739 | qp_init_attr.qp_type = qp_type; | |
2740 | qp_init_attr.port_num = qp_info->port_priv->port_num; | |
2741 | qp_init_attr.qp_context = qp_info; | |
2742 | qp_init_attr.event_handler = qp_event_handler; | |
2743 | qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr); | |
2744 | if (IS_ERR(qp_info->qp)) { | |
2745 | printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n", | |
2746 | get_spl_qp_index(qp_type)); | |
2747 | ret = PTR_ERR(qp_info->qp); | |
2748 | goto error; | |
2749 | } | |
2750 | /* Use minimum queue sizes unless the CQ is resized */ | |
b76aabc3 HR |
2751 | qp_info->send_queue.max_active = mad_sendq_size; |
2752 | qp_info->recv_queue.max_active = mad_recvq_size; | |
1da177e4 LT |
2753 | return 0; |
2754 | ||
2755 | error: | |
2756 | return ret; | |
2757 | } | |
2758 | ||
2759 | static void destroy_mad_qp(struct ib_mad_qp_info *qp_info) | |
2760 | { | |
2761 | ib_destroy_qp(qp_info->qp); | |
6044ec88 | 2762 | kfree(qp_info->snoop_table); |
1da177e4 LT |
2763 | } |
2764 | ||
2765 | /* | |
2766 | * Open the port | |
2767 | * Create the QP, PD, MR, and CQ if needed | |
2768 | */ | |
2769 | static int ib_mad_port_open(struct ib_device *device, | |
2770 | int port_num) | |
2771 | { | |
2772 | int ret, cq_size; | |
2773 | struct ib_mad_port_private *port_priv; | |
2774 | unsigned long flags; | |
2775 | char name[sizeof "ib_mad123"]; | |
2776 | ||
1da177e4 | 2777 | /* Create new device info */ |
de6eb66b | 2778 | port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL); |
1da177e4 LT |
2779 | if (!port_priv) { |
2780 | printk(KERN_ERR PFX "No memory for ib_mad_port_private\n"); | |
2781 | return -ENOMEM; | |
2782 | } | |
de6eb66b | 2783 | |
1da177e4 LT |
2784 | port_priv->device = device; |
2785 | port_priv->port_num = port_num; | |
2786 | spin_lock_init(&port_priv->reg_lock); | |
2787 | INIT_LIST_HEAD(&port_priv->agent_list); | |
2788 | init_mad_qp(port_priv, &port_priv->qp_info[0]); | |
2789 | init_mad_qp(port_priv, &port_priv->qp_info[1]); | |
2790 | ||
b76aabc3 | 2791 | cq_size = (mad_sendq_size + mad_recvq_size) * 2; |
1da177e4 | 2792 | port_priv->cq = ib_create_cq(port_priv->device, |
5dd2ce12 | 2793 | ib_mad_thread_completion_handler, |
f4fd0b22 | 2794 | NULL, port_priv, cq_size, 0); |
1da177e4 LT |
2795 | if (IS_ERR(port_priv->cq)) { |
2796 | printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n"); | |
2797 | ret = PTR_ERR(port_priv->cq); | |
2798 | goto error3; | |
2799 | } | |
2800 | ||
2801 | port_priv->pd = ib_alloc_pd(device); | |
2802 | if (IS_ERR(port_priv->pd)) { | |
2803 | printk(KERN_ERR PFX "Couldn't create ib_mad PD\n"); | |
2804 | ret = PTR_ERR(port_priv->pd); | |
2805 | goto error4; | |
2806 | } | |
2807 | ||
2808 | port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE); | |
2809 | if (IS_ERR(port_priv->mr)) { | |
2810 | printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n"); | |
2811 | ret = PTR_ERR(port_priv->mr); | |
2812 | goto error5; | |
2813 | } | |
2814 | ||
2815 | ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI); | |
2816 | if (ret) | |
2817 | goto error6; | |
2818 | ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI); | |
2819 | if (ret) | |
2820 | goto error7; | |
2821 | ||
2822 | snprintf(name, sizeof name, "ib_mad%d", port_num); | |
2823 | port_priv->wq = create_singlethread_workqueue(name); | |
2824 | if (!port_priv->wq) { | |
2825 | ret = -ENOMEM; | |
2826 | goto error8; | |
2827 | } | |
c4028958 | 2828 | INIT_WORK(&port_priv->work, ib_mad_completion_handler); |
1da177e4 | 2829 | |
dc05980d MT |
2830 | spin_lock_irqsave(&ib_mad_port_list_lock, flags); |
2831 | list_add_tail(&port_priv->port_list, &ib_mad_port_list); | |
2832 | spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); | |
2833 | ||
1da177e4 LT |
2834 | ret = ib_mad_port_start(port_priv); |
2835 | if (ret) { | |
2836 | printk(KERN_ERR PFX "Couldn't start port\n"); | |
2837 | goto error9; | |
2838 | } | |
2839 | ||
1da177e4 LT |
2840 | return 0; |
2841 | ||
2842 | error9: | |
dc05980d MT |
2843 | spin_lock_irqsave(&ib_mad_port_list_lock, flags); |
2844 | list_del_init(&port_priv->port_list); | |
2845 | spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); | |
2846 | ||
1da177e4 LT |
2847 | destroy_workqueue(port_priv->wq); |
2848 | error8: | |
2849 | destroy_mad_qp(&port_priv->qp_info[1]); | |
2850 | error7: | |
2851 | destroy_mad_qp(&port_priv->qp_info[0]); | |
2852 | error6: | |
2853 | ib_dereg_mr(port_priv->mr); | |
2854 | error5: | |
2855 | ib_dealloc_pd(port_priv->pd); | |
2856 | error4: | |
2857 | ib_destroy_cq(port_priv->cq); | |
2858 | cleanup_recv_queue(&port_priv->qp_info[1]); | |
2859 | cleanup_recv_queue(&port_priv->qp_info[0]); | |
2860 | error3: | |
2861 | kfree(port_priv); | |
2862 | ||
2863 | return ret; | |
2864 | } | |
2865 | ||
2866 | /* | |
2867 | * Close the port | |
2868 | * If there are no classes using the port, free the port | |
2869 | * resources (CQ, MR, PD, QP) and remove the port's info structure | |
2870 | */ | |
2871 | static int ib_mad_port_close(struct ib_device *device, int port_num) | |
2872 | { | |
2873 | struct ib_mad_port_private *port_priv; | |
2874 | unsigned long flags; | |
2875 | ||
2876 | spin_lock_irqsave(&ib_mad_port_list_lock, flags); | |
2877 | port_priv = __ib_get_mad_port(device, port_num); | |
2878 | if (port_priv == NULL) { | |
2879 | spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); | |
2880 | printk(KERN_ERR PFX "Port %d not found\n", port_num); | |
2881 | return -ENODEV; | |
2882 | } | |
dc05980d | 2883 | list_del_init(&port_priv->port_list); |
1da177e4 LT |
2884 | spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); |
2885 | ||
1da177e4 LT |
2886 | destroy_workqueue(port_priv->wq); |
2887 | destroy_mad_qp(&port_priv->qp_info[1]); | |
2888 | destroy_mad_qp(&port_priv->qp_info[0]); | |
2889 | ib_dereg_mr(port_priv->mr); | |
2890 | ib_dealloc_pd(port_priv->pd); | |
2891 | ib_destroy_cq(port_priv->cq); | |
2892 | cleanup_recv_queue(&port_priv->qp_info[1]); | |
2893 | cleanup_recv_queue(&port_priv->qp_info[0]); | |
2894 | /* XXX: Handle deallocation of MAD registration tables */ | |
2895 | ||
2896 | kfree(port_priv); | |
2897 | ||
2898 | return 0; | |
2899 | } | |
2900 | ||
2901 | static void ib_mad_init_device(struct ib_device *device) | |
2902 | { | |
4ab6fb7e | 2903 | int start, end, i; |
1da177e4 | 2904 | |
07ebafba TT |
2905 | if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) |
2906 | return; | |
2907 | ||
2908 | if (device->node_type == RDMA_NODE_IB_SWITCH) { | |
4ab6fb7e RD |
2909 | start = 0; |
2910 | end = 0; | |
1da177e4 | 2911 | } else { |
4ab6fb7e RD |
2912 | start = 1; |
2913 | end = device->phys_port_cnt; | |
1da177e4 | 2914 | } |
4ab6fb7e RD |
2915 | |
2916 | for (i = start; i <= end; i++) { | |
2917 | if (ib_mad_port_open(device, i)) { | |
1da177e4 | 2918 | printk(KERN_ERR PFX "Couldn't open %s port %d\n", |
4ab6fb7e RD |
2919 | device->name, i); |
2920 | goto error; | |
1da177e4 | 2921 | } |
4ab6fb7e | 2922 | if (ib_agent_port_open(device, i)) { |
1da177e4 LT |
2923 | printk(KERN_ERR PFX "Couldn't open %s port %d " |
2924 | "for agents\n", | |
4ab6fb7e RD |
2925 | device->name, i); |
2926 | goto error_agent; | |
1da177e4 LT |
2927 | } |
2928 | } | |
f68bcc2d | 2929 | return; |
1da177e4 | 2930 | |
4ab6fb7e RD |
2931 | error_agent: |
2932 | if (ib_mad_port_close(device, i)) | |
2933 | printk(KERN_ERR PFX "Couldn't close %s port %d\n", | |
2934 | device->name, i); | |
2935 | ||
2936 | error: | |
2937 | i--; | |
2938 | ||
2939 | while (i >= start) { | |
2940 | if (ib_agent_port_close(device, i)) | |
1da177e4 LT |
2941 | printk(KERN_ERR PFX "Couldn't close %s port %d " |
2942 | "for agents\n", | |
4ab6fb7e RD |
2943 | device->name, i); |
2944 | if (ib_mad_port_close(device, i)) | |
1da177e4 | 2945 | printk(KERN_ERR PFX "Couldn't close %s port %d\n", |
4ab6fb7e | 2946 | device->name, i); |
1da177e4 LT |
2947 | i--; |
2948 | } | |
1da177e4 LT |
2949 | } |
2950 | ||
2951 | static void ib_mad_remove_device(struct ib_device *device) | |
2952 | { | |
f68bcc2d | 2953 | int i, num_ports, cur_port; |
1da177e4 | 2954 | |
070e140c SW |
2955 | if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) |
2956 | return; | |
2957 | ||
07ebafba | 2958 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
1da177e4 LT |
2959 | num_ports = 1; |
2960 | cur_port = 0; | |
2961 | } else { | |
2962 | num_ports = device->phys_port_cnt; | |
2963 | cur_port = 1; | |
2964 | } | |
2965 | for (i = 0; i < num_ports; i++, cur_port++) { | |
f68bcc2d | 2966 | if (ib_agent_port_close(device, cur_port)) |
1da177e4 LT |
2967 | printk(KERN_ERR PFX "Couldn't close %s port %d " |
2968 | "for agents\n", | |
2969 | device->name, cur_port); | |
f68bcc2d | 2970 | if (ib_mad_port_close(device, cur_port)) |
1da177e4 LT |
2971 | printk(KERN_ERR PFX "Couldn't close %s port %d\n", |
2972 | device->name, cur_port); | |
1da177e4 LT |
2973 | } |
2974 | } | |
2975 | ||
2976 | static struct ib_client mad_client = { | |
2977 | .name = "mad", | |
2978 | .add = ib_mad_init_device, | |
2979 | .remove = ib_mad_remove_device | |
2980 | }; | |
2981 | ||
2982 | static int __init ib_mad_init_module(void) | |
2983 | { | |
2984 | int ret; | |
2985 | ||
b76aabc3 HR |
2986 | mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE); |
2987 | mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE); | |
2988 | ||
2989 | mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE); | |
2990 | mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE); | |
2991 | ||
1da177e4 LT |
2992 | ib_mad_cache = kmem_cache_create("ib_mad", |
2993 | sizeof(struct ib_mad_private), | |
2994 | 0, | |
2995 | SLAB_HWCACHE_ALIGN, | |
1da177e4 LT |
2996 | NULL); |
2997 | if (!ib_mad_cache) { | |
2998 | printk(KERN_ERR PFX "Couldn't create ib_mad cache\n"); | |
2999 | ret = -ENOMEM; | |
3000 | goto error1; | |
3001 | } | |
3002 | ||
3003 | INIT_LIST_HEAD(&ib_mad_port_list); | |
3004 | ||
3005 | if (ib_register_client(&mad_client)) { | |
3006 | printk(KERN_ERR PFX "Couldn't register ib_mad client\n"); | |
3007 | ret = -EINVAL; | |
3008 | goto error2; | |
3009 | } | |
3010 | ||
3011 | return 0; | |
3012 | ||
3013 | error2: | |
3014 | kmem_cache_destroy(ib_mad_cache); | |
3015 | error1: | |
3016 | return ret; | |
3017 | } | |
3018 | ||
3019 | static void __exit ib_mad_cleanup_module(void) | |
3020 | { | |
3021 | ib_unregister_client(&mad_client); | |
1a1d92c1 | 3022 | kmem_cache_destroy(ib_mad_cache); |
1da177e4 LT |
3023 | } |
3024 | ||
3025 | module_init(ib_mad_init_module); | |
3026 | module_exit(ib_mad_cleanup_module); |