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