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