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