[PATCH] IB: Add ib_modify_mad API to MAD
[safe/jmp/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         list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
1758                 temp_mad_send_wr = list_entry(list_item,
1759                                               struct ib_mad_send_wr_private,
1760                                               agent_list);
1761                 if (time_after(mad_send_wr->timeout,
1762                                temp_mad_send_wr->timeout))
1763                         break;
1764         }
1765         list_add(&mad_send_wr->agent_list, list_item);
1766
1767         /* Reschedule a work item if we have a shorter timeout */
1768         if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
1769                 cancel_delayed_work(&mad_agent_priv->timed_work);
1770                 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
1771                                    &mad_agent_priv->timed_work, delay);
1772         }
1773 }
1774
1775 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
1776                           int timeout_ms)
1777 {
1778         mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
1779         wait_for_response(mad_send_wr);
1780 }
1781
1782 /*
1783  * Process a send work completion
1784  */
1785 static void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1786                                     struct ib_mad_send_wc *mad_send_wc)
1787 {
1788         struct ib_mad_agent_private     *mad_agent_priv;
1789         unsigned long                   flags;
1790
1791         mad_agent_priv = mad_send_wr->mad_agent_priv;
1792         spin_lock_irqsave(&mad_agent_priv->lock, flags);
1793         if (mad_send_wc->status != IB_WC_SUCCESS &&
1794             mad_send_wr->status == IB_WC_SUCCESS) {
1795                 mad_send_wr->status = mad_send_wc->status;
1796                 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
1797         }
1798
1799         if (--mad_send_wr->refcount > 0) {
1800                 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
1801                     mad_send_wr->status == IB_WC_SUCCESS) {
1802                         wait_for_response(mad_send_wr);
1803                 }
1804                 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1805                 return;
1806         }
1807
1808         /* Remove send from MAD agent and notify client of completion */
1809         list_del(&mad_send_wr->agent_list);
1810         adjust_timeout(mad_agent_priv);
1811         spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1812
1813         if (mad_send_wr->status != IB_WC_SUCCESS )
1814                 mad_send_wc->status = mad_send_wr->status;
1815         mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
1816                                             mad_send_wc);
1817
1818         /* Release reference on agent taken when sending */
1819         if (atomic_dec_and_test(&mad_agent_priv->refcount))
1820                 wake_up(&mad_agent_priv->wait);
1821
1822         kfree(mad_send_wr);
1823 }
1824
1825 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
1826                                      struct ib_wc *wc)
1827 {
1828         struct ib_mad_send_wr_private   *mad_send_wr, *queued_send_wr;
1829         struct ib_mad_list_head         *mad_list;
1830         struct ib_mad_qp_info           *qp_info;
1831         struct ib_mad_queue             *send_queue;
1832         struct ib_send_wr               *bad_send_wr;
1833         unsigned long flags;
1834         int ret;
1835
1836         mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1837         mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
1838                                    mad_list);
1839         send_queue = mad_list->mad_queue;
1840         qp_info = send_queue->qp_info;
1841
1842 retry:
1843         queued_send_wr = NULL;
1844         spin_lock_irqsave(&send_queue->lock, flags);
1845         list_del(&mad_list->list);
1846
1847         /* Move queued send to the send queue */
1848         if (send_queue->count-- > send_queue->max_active) {
1849                 mad_list = container_of(qp_info->overflow_list.next,
1850                                         struct ib_mad_list_head, list);
1851                 queued_send_wr = container_of(mad_list,
1852                                         struct ib_mad_send_wr_private,
1853                                         mad_list);
1854                 list_del(&mad_list->list);
1855                 list_add_tail(&mad_list->list, &send_queue->list);
1856         }
1857         spin_unlock_irqrestore(&send_queue->lock, flags);
1858
1859         /* Restore client wr_id in WC and complete send */
1860         wc->wr_id = mad_send_wr->wr_id;
1861         if (atomic_read(&qp_info->snoop_count))
1862                 snoop_send(qp_info, &mad_send_wr->send_wr,
1863                            (struct ib_mad_send_wc *)wc,
1864                            IB_MAD_SNOOP_SEND_COMPLETIONS);
1865         ib_mad_complete_send_wr(mad_send_wr, (struct ib_mad_send_wc *)wc);
1866
1867         if (queued_send_wr) {
1868                 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
1869                                 &bad_send_wr);
1870                 if (ret) {
1871                         printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
1872                         mad_send_wr = queued_send_wr;
1873                         wc->status = IB_WC_LOC_QP_OP_ERR;
1874                         goto retry;
1875                 }
1876         }
1877 }
1878
1879 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
1880 {
1881         struct ib_mad_send_wr_private *mad_send_wr;
1882         struct ib_mad_list_head *mad_list;
1883         unsigned long flags;
1884
1885         spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1886         list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
1887                 mad_send_wr = container_of(mad_list,
1888                                            struct ib_mad_send_wr_private,
1889                                            mad_list);
1890                 mad_send_wr->retry = 1;
1891         }
1892         spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1893 }
1894
1895 static void mad_error_handler(struct ib_mad_port_private *port_priv,
1896                               struct ib_wc *wc)
1897 {
1898         struct ib_mad_list_head *mad_list;
1899         struct ib_mad_qp_info *qp_info;
1900         struct ib_mad_send_wr_private *mad_send_wr;
1901         int ret;
1902
1903         /* Determine if failure was a send or receive */
1904         mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1905         qp_info = mad_list->mad_queue->qp_info;
1906         if (mad_list->mad_queue == &qp_info->recv_queue)
1907                 /*
1908                  * Receive errors indicate that the QP has entered the error
1909                  * state - error handling/shutdown code will cleanup
1910                  */
1911                 return;
1912
1913         /*
1914          * Send errors will transition the QP to SQE - move
1915          * QP to RTS and repost flushed work requests
1916          */
1917         mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
1918                                    mad_list);
1919         if (wc->status == IB_WC_WR_FLUSH_ERR) {
1920                 if (mad_send_wr->retry) {
1921                         /* Repost send */
1922                         struct ib_send_wr *bad_send_wr;
1923
1924                         mad_send_wr->retry = 0;
1925                         ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
1926                                         &bad_send_wr);
1927                         if (ret)
1928                                 ib_mad_send_done_handler(port_priv, wc);
1929                 } else
1930                         ib_mad_send_done_handler(port_priv, wc);
1931         } else {
1932                 struct ib_qp_attr *attr;
1933
1934                 /* Transition QP to RTS and fail offending send */
1935                 attr = kmalloc(sizeof *attr, GFP_KERNEL);
1936                 if (attr) {
1937                         attr->qp_state = IB_QPS_RTS;
1938                         attr->cur_qp_state = IB_QPS_SQE;
1939                         ret = ib_modify_qp(qp_info->qp, attr,
1940                                            IB_QP_STATE | IB_QP_CUR_STATE);
1941                         kfree(attr);
1942                         if (ret)
1943                                 printk(KERN_ERR PFX "mad_error_handler - "
1944                                        "ib_modify_qp to RTS : %d\n", ret);
1945                         else
1946                                 mark_sends_for_retry(qp_info);
1947                 }
1948                 ib_mad_send_done_handler(port_priv, wc);
1949         }
1950 }
1951
1952 /*
1953  * IB MAD completion callback
1954  */
1955 static void ib_mad_completion_handler(void *data)
1956 {
1957         struct ib_mad_port_private *port_priv;
1958         struct ib_wc wc;
1959
1960         port_priv = (struct ib_mad_port_private *)data;
1961         ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
1962
1963         while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
1964                 if (wc.status == IB_WC_SUCCESS) {
1965                         switch (wc.opcode) {
1966                         case IB_WC_SEND:
1967                                 ib_mad_send_done_handler(port_priv, &wc);
1968                                 break;
1969                         case IB_WC_RECV:
1970                                 ib_mad_recv_done_handler(port_priv, &wc);
1971                                 break;
1972                         default:
1973                                 BUG_ON(1);
1974                                 break;
1975                         }
1976                 } else
1977                         mad_error_handler(port_priv, &wc);
1978         }
1979 }
1980
1981 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
1982 {
1983         unsigned long flags;
1984         struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
1985         struct ib_mad_send_wc mad_send_wc;
1986         struct list_head cancel_list;
1987
1988         INIT_LIST_HEAD(&cancel_list);
1989
1990         spin_lock_irqsave(&mad_agent_priv->lock, flags);
1991         list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
1992                                  &mad_agent_priv->send_list, agent_list) {
1993                 if (mad_send_wr->status == IB_WC_SUCCESS) {
1994                         mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
1995                         mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
1996                 }
1997         }
1998
1999         /* Empty wait list to prevent receives from finding a request */
2000         list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2001         /* Empty local completion list as well */
2002         list_splice_init(&mad_agent_priv->local_list, &cancel_list);
2003         spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2004
2005         /* Report all cancelled requests */
2006         mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2007         mad_send_wc.vendor_err = 0;
2008
2009         list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2010                                  &cancel_list, agent_list) {
2011                 mad_send_wc.wr_id = mad_send_wr->wr_id;
2012                 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2013                                                    &mad_send_wc);
2014
2015                 list_del(&mad_send_wr->agent_list);
2016                 kfree(mad_send_wr);
2017                 atomic_dec(&mad_agent_priv->refcount);
2018         }
2019 }
2020
2021 static struct ib_mad_send_wr_private*
2022 find_send_by_wr_id(struct ib_mad_agent_private *mad_agent_priv,
2023                    u64 wr_id)
2024 {
2025         struct ib_mad_send_wr_private *mad_send_wr;
2026
2027         list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2028                             agent_list) {
2029                 if (mad_send_wr->wr_id == wr_id)
2030                         return mad_send_wr;
2031         }
2032
2033         list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2034                             agent_list) {
2035                 if (mad_send_wr->wr_id == wr_id)
2036                         return mad_send_wr;
2037         }
2038         return NULL;
2039 }
2040
2041 int ib_modify_mad(struct ib_mad_agent *mad_agent, u64 wr_id, u32 timeout_ms)
2042 {
2043         struct ib_mad_agent_private *mad_agent_priv;
2044         struct ib_mad_send_wr_private *mad_send_wr;
2045         unsigned long flags;
2046
2047         mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2048                                       agent);
2049         spin_lock_irqsave(&mad_agent_priv->lock, flags);
2050         mad_send_wr = find_send_by_wr_id(mad_agent_priv, wr_id);
2051         if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2052                 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2053                 return -EINVAL;
2054         }
2055
2056         if (!timeout_ms) {
2057                 mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2058                 mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2059         }
2060
2061         mad_send_wr->send_wr.wr.ud.timeout_ms = timeout_ms;
2062         if (!mad_send_wr->timeout || mad_send_wr->refcount > 1)
2063                 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2064         else
2065                 ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2066
2067         spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2068         return 0;
2069 }
2070 EXPORT_SYMBOL(ib_modify_mad);
2071
2072 void ib_cancel_mad(struct ib_mad_agent *mad_agent, u64 wr_id)
2073 {
2074         ib_modify_mad(mad_agent, wr_id, 0);
2075 }
2076 EXPORT_SYMBOL(ib_cancel_mad);
2077
2078 static void local_completions(void *data)
2079 {
2080         struct ib_mad_agent_private *mad_agent_priv;
2081         struct ib_mad_local_private *local;
2082         struct ib_mad_agent_private *recv_mad_agent;
2083         unsigned long flags;
2084         int recv = 0;
2085         struct ib_wc wc;
2086         struct ib_mad_send_wc mad_send_wc;
2087
2088         mad_agent_priv = (struct ib_mad_agent_private *)data;
2089
2090         spin_lock_irqsave(&mad_agent_priv->lock, flags);
2091         while (!list_empty(&mad_agent_priv->local_list)) {
2092                 local = list_entry(mad_agent_priv->local_list.next,
2093                                    struct ib_mad_local_private,
2094                                    completion_list);
2095                 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2096                 if (local->mad_priv) {
2097                         recv_mad_agent = local->recv_mad_agent;
2098                         if (!recv_mad_agent) {
2099                                 printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
2100                                 goto local_send_completion;
2101                         }
2102
2103                         recv = 1;
2104                         /*
2105                          * Defined behavior is to complete response
2106                          * before request
2107                          */
2108                         build_smp_wc(local->wr_id, IB_LID_PERMISSIVE,
2109                                      0 /* pkey index */,
2110                                      recv_mad_agent->agent.port_num, &wc);
2111
2112                         local->mad_priv->header.recv_wc.wc = &wc;
2113                         local->mad_priv->header.recv_wc.mad_len =
2114                                                 sizeof(struct ib_mad);
2115                         INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.recv_buf.list);
2116                         local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2117                         local->mad_priv->header.recv_wc.recv_buf.mad =
2118                                                 &local->mad_priv->mad.mad;
2119                         if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2120                                 snoop_recv(recv_mad_agent->qp_info,
2121                                           &local->mad_priv->header.recv_wc,
2122                                            IB_MAD_SNOOP_RECVS);
2123                         recv_mad_agent->agent.recv_handler(
2124                                                 &recv_mad_agent->agent,
2125                                                 &local->mad_priv->header.recv_wc);
2126                         spin_lock_irqsave(&recv_mad_agent->lock, flags);
2127                         atomic_dec(&recv_mad_agent->refcount);
2128                         spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2129                 }
2130
2131 local_send_completion:
2132                 /* Complete send */
2133                 mad_send_wc.status = IB_WC_SUCCESS;
2134                 mad_send_wc.vendor_err = 0;
2135                 mad_send_wc.wr_id = local->wr_id;
2136                 if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2137                         snoop_send(mad_agent_priv->qp_info, &local->send_wr,
2138                                   &mad_send_wc,
2139                                    IB_MAD_SNOOP_SEND_COMPLETIONS);
2140                 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2141                                                    &mad_send_wc);
2142
2143                 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2144                 list_del(&local->completion_list);
2145                 atomic_dec(&mad_agent_priv->refcount);
2146                 if (!recv)
2147                         kmem_cache_free(ib_mad_cache, local->mad_priv);
2148                 kfree(local);
2149         }
2150         spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2151 }
2152
2153 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2154 {
2155         int ret;
2156
2157         if (!mad_send_wr->retries--)
2158                 return -ETIMEDOUT;
2159
2160         mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_wr.
2161                                                 wr.ud.timeout_ms);
2162
2163         ret = ib_send_mad(mad_send_wr);
2164
2165         if (!ret) {
2166                 mad_send_wr->refcount++;
2167                 list_add_tail(&mad_send_wr->agent_list,
2168                               &mad_send_wr->mad_agent_priv->send_list);
2169         }
2170         return ret;
2171 }
2172
2173 static void timeout_sends(void *data)
2174 {
2175         struct ib_mad_agent_private *mad_agent_priv;
2176         struct ib_mad_send_wr_private *mad_send_wr;
2177         struct ib_mad_send_wc mad_send_wc;
2178         unsigned long flags, delay;
2179
2180         mad_agent_priv = (struct ib_mad_agent_private *)data;
2181         mad_send_wc.vendor_err = 0;
2182
2183         spin_lock_irqsave(&mad_agent_priv->lock, flags);
2184         while (!list_empty(&mad_agent_priv->wait_list)) {
2185                 mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2186                                          struct ib_mad_send_wr_private,
2187                                          agent_list);
2188
2189                 if (time_after(mad_send_wr->timeout, jiffies)) {
2190                         delay = mad_send_wr->timeout - jiffies;
2191                         if ((long)delay <= 0)
2192                                 delay = 1;
2193                         queue_delayed_work(mad_agent_priv->qp_info->
2194                                            port_priv->wq,
2195                                            &mad_agent_priv->timed_work, delay);
2196                         break;
2197                 }
2198
2199                 list_del(&mad_send_wr->agent_list);
2200                 if (!retry_send(mad_send_wr))
2201                         continue;
2202
2203                 spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2204
2205                 if (mad_send_wr->status == IB_WC_SUCCESS)
2206                         mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2207                 else
2208                         mad_send_wc.status = mad_send_wr->status;
2209                 mad_send_wc.wr_id = mad_send_wr->wr_id;
2210                 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2211                                                    &mad_send_wc);
2212
2213                 kfree(mad_send_wr);
2214                 atomic_dec(&mad_agent_priv->refcount);
2215                 spin_lock_irqsave(&mad_agent_priv->lock, flags);
2216         }
2217         spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2218 }
2219
2220 static void ib_mad_thread_completion_handler(struct ib_cq *cq)
2221 {
2222         struct ib_mad_port_private *port_priv = cq->cq_context;
2223
2224         queue_work(port_priv->wq, &port_priv->work);
2225 }
2226
2227 /*
2228  * Allocate receive MADs and post receive WRs for them
2229  */
2230 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2231                                     struct ib_mad_private *mad)
2232 {
2233         unsigned long flags;
2234         int post, ret;
2235         struct ib_mad_private *mad_priv;
2236         struct ib_sge sg_list;
2237         struct ib_recv_wr recv_wr, *bad_recv_wr;
2238         struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2239
2240         /* Initialize common scatter list fields */
2241         sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2242         sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2243
2244         /* Initialize common receive WR fields */
2245         recv_wr.next = NULL;
2246         recv_wr.sg_list = &sg_list;
2247         recv_wr.num_sge = 1;
2248
2249         do {
2250                 /* Allocate and map receive buffer */
2251                 if (mad) {
2252                         mad_priv = mad;
2253                         mad = NULL;
2254                 } else {
2255                         mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2256                         if (!mad_priv) {
2257                                 printk(KERN_ERR PFX "No memory for receive buffer\n");
2258                                 ret = -ENOMEM;
2259                                 break;
2260                         }
2261                 }
2262                 sg_list.addr = dma_map_single(qp_info->port_priv->
2263                                                 device->dma_device,
2264                                         &mad_priv->grh,
2265                                         sizeof *mad_priv -
2266                                                 sizeof mad_priv->header,
2267                                         DMA_FROM_DEVICE);
2268                 pci_unmap_addr_set(&mad_priv->header, mapping, sg_list.addr);
2269                 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2270                 mad_priv->header.mad_list.mad_queue = recv_queue;
2271
2272                 /* Post receive WR */
2273                 spin_lock_irqsave(&recv_queue->lock, flags);
2274                 post = (++recv_queue->count < recv_queue->max_active);
2275                 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2276                 spin_unlock_irqrestore(&recv_queue->lock, flags);
2277                 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2278                 if (ret) {
2279                         spin_lock_irqsave(&recv_queue->lock, flags);
2280                         list_del(&mad_priv->header.mad_list.list);
2281                         recv_queue->count--;
2282                         spin_unlock_irqrestore(&recv_queue->lock, flags);
2283                         dma_unmap_single(qp_info->port_priv->device->dma_device,
2284                                          pci_unmap_addr(&mad_priv->header,
2285                                                         mapping),
2286                                          sizeof *mad_priv -
2287                                            sizeof mad_priv->header,
2288                                          DMA_FROM_DEVICE);
2289                         kmem_cache_free(ib_mad_cache, mad_priv);
2290                         printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2291                         break;
2292                 }
2293         } while (post);
2294
2295         return ret;
2296 }
2297
2298 /*
2299  * Return all the posted receive MADs
2300  */
2301 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2302 {
2303         struct ib_mad_private_header *mad_priv_hdr;
2304         struct ib_mad_private *recv;
2305         struct ib_mad_list_head *mad_list;
2306
2307         while (!list_empty(&qp_info->recv_queue.list)) {
2308
2309                 mad_list = list_entry(qp_info->recv_queue.list.next,
2310                                       struct ib_mad_list_head, list);
2311                 mad_priv_hdr = container_of(mad_list,
2312                                             struct ib_mad_private_header,
2313                                             mad_list);
2314                 recv = container_of(mad_priv_hdr, struct ib_mad_private,
2315                                     header);
2316
2317                 /* Remove from posted receive MAD list */
2318                 list_del(&mad_list->list);
2319
2320                 dma_unmap_single(qp_info->port_priv->device->dma_device,
2321                                  pci_unmap_addr(&recv->header, mapping),
2322                                  sizeof(struct ib_mad_private) -
2323                                  sizeof(struct ib_mad_private_header),
2324                                  DMA_FROM_DEVICE);
2325                 kmem_cache_free(ib_mad_cache, recv);
2326         }
2327
2328         qp_info->recv_queue.count = 0;
2329 }
2330
2331 /*
2332  * Start the port
2333  */
2334 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2335 {
2336         int ret, i;
2337         struct ib_qp_attr *attr;
2338         struct ib_qp *qp;
2339
2340         attr = kmalloc(sizeof *attr, GFP_KERNEL);
2341         if (!attr) {
2342                 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2343                 return -ENOMEM;
2344         }
2345
2346         for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2347                 qp = port_priv->qp_info[i].qp;
2348                 /*
2349                  * PKey index for QP1 is irrelevant but
2350                  * one is needed for the Reset to Init transition
2351                  */
2352                 attr->qp_state = IB_QPS_INIT;
2353                 attr->pkey_index = 0;
2354                 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2355                 ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2356                                              IB_QP_PKEY_INDEX | IB_QP_QKEY);
2357                 if (ret) {
2358                         printk(KERN_ERR PFX "Couldn't change QP%d state to "
2359                                "INIT: %d\n", i, ret);
2360                         goto out;
2361                 }
2362
2363                 attr->qp_state = IB_QPS_RTR;
2364                 ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2365                 if (ret) {
2366                         printk(KERN_ERR PFX "Couldn't change QP%d state to "
2367                                "RTR: %d\n", i, ret);
2368                         goto out;
2369                 }
2370
2371                 attr->qp_state = IB_QPS_RTS;
2372                 attr->sq_psn = IB_MAD_SEND_Q_PSN;
2373                 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2374                 if (ret) {
2375                         printk(KERN_ERR PFX "Couldn't change QP%d state to "
2376                                "RTS: %d\n", i, ret);
2377                         goto out;
2378                 }
2379         }
2380
2381         ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2382         if (ret) {
2383                 printk(KERN_ERR PFX "Failed to request completion "
2384                        "notification: %d\n", ret);
2385                 goto out;
2386         }
2387
2388         for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2389                 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2390                 if (ret) {
2391                         printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2392                         goto out;
2393                 }
2394         }
2395 out:
2396         kfree(attr);
2397         return ret;
2398 }
2399
2400 static void qp_event_handler(struct ib_event *event, void *qp_context)
2401 {
2402         struct ib_mad_qp_info   *qp_info = qp_context;
2403
2404         /* It's worse than that! He's dead, Jim! */
2405         printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2406                 event->event, qp_info->qp->qp_num);
2407 }
2408
2409 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2410                            struct ib_mad_queue *mad_queue)
2411 {
2412         mad_queue->qp_info = qp_info;
2413         mad_queue->count = 0;
2414         spin_lock_init(&mad_queue->lock);
2415         INIT_LIST_HEAD(&mad_queue->list);
2416 }
2417
2418 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2419                         struct ib_mad_qp_info *qp_info)
2420 {
2421         qp_info->port_priv = port_priv;
2422         init_mad_queue(qp_info, &qp_info->send_queue);
2423         init_mad_queue(qp_info, &qp_info->recv_queue);
2424         INIT_LIST_HEAD(&qp_info->overflow_list);
2425         spin_lock_init(&qp_info->snoop_lock);
2426         qp_info->snoop_table = NULL;
2427         qp_info->snoop_table_size = 0;
2428         atomic_set(&qp_info->snoop_count, 0);
2429 }
2430
2431 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2432                          enum ib_qp_type qp_type)
2433 {
2434         struct ib_qp_init_attr  qp_init_attr;
2435         int ret;
2436
2437         memset(&qp_init_attr, 0, sizeof qp_init_attr);
2438         qp_init_attr.send_cq = qp_info->port_priv->cq;
2439         qp_init_attr.recv_cq = qp_info->port_priv->cq;
2440         qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2441         qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
2442         qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
2443         qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2444         qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2445         qp_init_attr.qp_type = qp_type;
2446         qp_init_attr.port_num = qp_info->port_priv->port_num;
2447         qp_init_attr.qp_context = qp_info;
2448         qp_init_attr.event_handler = qp_event_handler;
2449         qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2450         if (IS_ERR(qp_info->qp)) {
2451                 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2452                        get_spl_qp_index(qp_type));
2453                 ret = PTR_ERR(qp_info->qp);
2454                 goto error;
2455         }
2456         /* Use minimum queue sizes unless the CQ is resized */
2457         qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
2458         qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
2459         return 0;
2460
2461 error:
2462         return ret;
2463 }
2464
2465 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2466 {
2467         ib_destroy_qp(qp_info->qp);
2468         if (qp_info->snoop_table)
2469                 kfree(qp_info->snoop_table);
2470 }
2471
2472 /*
2473  * Open the port
2474  * Create the QP, PD, MR, and CQ if needed
2475  */
2476 static int ib_mad_port_open(struct ib_device *device,
2477                             int port_num)
2478 {
2479         int ret, cq_size;
2480         struct ib_mad_port_private *port_priv;
2481         unsigned long flags;
2482         char name[sizeof "ib_mad123"];
2483
2484         /* Create new device info */
2485         port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL);
2486         if (!port_priv) {
2487                 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2488                 return -ENOMEM;
2489         }
2490         memset(port_priv, 0, sizeof *port_priv);
2491         port_priv->device = device;
2492         port_priv->port_num = port_num;
2493         spin_lock_init(&port_priv->reg_lock);
2494         INIT_LIST_HEAD(&port_priv->agent_list);
2495         init_mad_qp(port_priv, &port_priv->qp_info[0]);
2496         init_mad_qp(port_priv, &port_priv->qp_info[1]);
2497
2498         cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
2499         port_priv->cq = ib_create_cq(port_priv->device,
2500                                      (ib_comp_handler)
2501                                         ib_mad_thread_completion_handler,
2502                                      NULL, port_priv, cq_size);
2503         if (IS_ERR(port_priv->cq)) {
2504                 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2505                 ret = PTR_ERR(port_priv->cq);
2506                 goto error3;
2507         }
2508
2509         port_priv->pd = ib_alloc_pd(device);
2510         if (IS_ERR(port_priv->pd)) {
2511                 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2512                 ret = PTR_ERR(port_priv->pd);
2513                 goto error4;
2514         }
2515
2516         port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2517         if (IS_ERR(port_priv->mr)) {
2518                 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2519                 ret = PTR_ERR(port_priv->mr);
2520                 goto error5;
2521         }
2522
2523         ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2524         if (ret)
2525                 goto error6;
2526         ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2527         if (ret)
2528                 goto error7;
2529
2530         snprintf(name, sizeof name, "ib_mad%d", port_num);
2531         port_priv->wq = create_singlethread_workqueue(name);
2532         if (!port_priv->wq) {
2533                 ret = -ENOMEM;
2534                 goto error8;
2535         }
2536         INIT_WORK(&port_priv->work, ib_mad_completion_handler, port_priv);
2537
2538         ret = ib_mad_port_start(port_priv);
2539         if (ret) {
2540                 printk(KERN_ERR PFX "Couldn't start port\n");
2541                 goto error9;
2542         }
2543
2544         spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2545         list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2546         spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2547         return 0;
2548
2549 error9:
2550         destroy_workqueue(port_priv->wq);
2551 error8:
2552         destroy_mad_qp(&port_priv->qp_info[1]);
2553 error7:
2554         destroy_mad_qp(&port_priv->qp_info[0]);
2555 error6:
2556         ib_dereg_mr(port_priv->mr);
2557 error5:
2558         ib_dealloc_pd(port_priv->pd);
2559 error4:
2560         ib_destroy_cq(port_priv->cq);
2561         cleanup_recv_queue(&port_priv->qp_info[1]);
2562         cleanup_recv_queue(&port_priv->qp_info[0]);
2563 error3:
2564         kfree(port_priv);
2565
2566         return ret;
2567 }
2568
2569 /*
2570  * Close the port
2571  * If there are no classes using the port, free the port
2572  * resources (CQ, MR, PD, QP) and remove the port's info structure
2573  */
2574 static int ib_mad_port_close(struct ib_device *device, int port_num)
2575 {
2576         struct ib_mad_port_private *port_priv;
2577         unsigned long flags;
2578
2579         spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2580         port_priv = __ib_get_mad_port(device, port_num);
2581         if (port_priv == NULL) {
2582                 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2583                 printk(KERN_ERR PFX "Port %d not found\n", port_num);
2584                 return -ENODEV;
2585         }
2586         list_del(&port_priv->port_list);
2587         spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2588
2589         /* Stop processing completions. */
2590         flush_workqueue(port_priv->wq);
2591         destroy_workqueue(port_priv->wq);
2592         destroy_mad_qp(&port_priv->qp_info[1]);
2593         destroy_mad_qp(&port_priv->qp_info[0]);
2594         ib_dereg_mr(port_priv->mr);
2595         ib_dealloc_pd(port_priv->pd);
2596         ib_destroy_cq(port_priv->cq);
2597         cleanup_recv_queue(&port_priv->qp_info[1]);
2598         cleanup_recv_queue(&port_priv->qp_info[0]);
2599         /* XXX: Handle deallocation of MAD registration tables */
2600
2601         kfree(port_priv);
2602
2603         return 0;
2604 }
2605
2606 static void ib_mad_init_device(struct ib_device *device)
2607 {
2608         int num_ports, cur_port, i;
2609
2610         if (device->node_type == IB_NODE_SWITCH) {
2611                 num_ports = 1;
2612                 cur_port = 0;
2613         } else {
2614                 num_ports = device->phys_port_cnt;
2615                 cur_port = 1;
2616         }
2617         for (i = 0; i < num_ports; i++, cur_port++) {
2618                 if (ib_mad_port_open(device, cur_port)) {
2619                         printk(KERN_ERR PFX "Couldn't open %s port %d\n",
2620                                device->name, cur_port);
2621                         goto error_device_open;
2622                 }
2623                 if (ib_agent_port_open(device, cur_port)) {
2624                         printk(KERN_ERR PFX "Couldn't open %s port %d "
2625                                "for agents\n",
2626                                device->name, cur_port);
2627                         goto error_device_open;
2628                 }
2629         }
2630         return;
2631
2632 error_device_open:
2633         while (i > 0) {
2634                 cur_port--;
2635                 if (ib_agent_port_close(device, cur_port))
2636                         printk(KERN_ERR PFX "Couldn't close %s port %d "
2637                                "for agents\n",
2638                                device->name, cur_port);
2639                 if (ib_mad_port_close(device, cur_port))
2640                         printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2641                                device->name, cur_port);
2642                 i--;
2643         }
2644 }
2645
2646 static void ib_mad_remove_device(struct ib_device *device)
2647 {
2648         int i, num_ports, cur_port;
2649
2650         if (device->node_type == IB_NODE_SWITCH) {
2651                 num_ports = 1;
2652                 cur_port = 0;
2653         } else {
2654                 num_ports = device->phys_port_cnt;
2655                 cur_port = 1;
2656         }
2657         for (i = 0; i < num_ports; i++, cur_port++) {
2658                 if (ib_agent_port_close(device, cur_port))
2659                         printk(KERN_ERR PFX "Couldn't close %s port %d "
2660                                "for agents\n",
2661                                device->name, cur_port);
2662                 if (ib_mad_port_close(device, cur_port))
2663                         printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2664                                device->name, cur_port);
2665         }
2666 }
2667
2668 static struct ib_client mad_client = {
2669         .name   = "mad",
2670         .add = ib_mad_init_device,
2671         .remove = ib_mad_remove_device
2672 };
2673
2674 static int __init ib_mad_init_module(void)
2675 {
2676         int ret;
2677
2678         spin_lock_init(&ib_mad_port_list_lock);
2679         spin_lock_init(&ib_agent_port_list_lock);
2680
2681         ib_mad_cache = kmem_cache_create("ib_mad",
2682                                          sizeof(struct ib_mad_private),
2683                                          0,
2684                                          SLAB_HWCACHE_ALIGN,
2685                                          NULL,
2686                                          NULL);
2687         if (!ib_mad_cache) {
2688                 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
2689                 ret = -ENOMEM;
2690                 goto error1;
2691         }
2692
2693         INIT_LIST_HEAD(&ib_mad_port_list);
2694
2695         if (ib_register_client(&mad_client)) {
2696                 printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
2697                 ret = -EINVAL;
2698                 goto error2;
2699         }
2700
2701         return 0;
2702
2703 error2:
2704         kmem_cache_destroy(ib_mad_cache);
2705 error1:
2706         return ret;
2707 }
2708
2709 static void __exit ib_mad_cleanup_module(void)
2710 {
2711         ib_unregister_client(&mad_client);
2712
2713         if (kmem_cache_destroy(ib_mad_cache)) {
2714                 printk(KERN_DEBUG PFX "Failed to destroy ib_mad cache\n");
2715         }
2716 }
2717
2718 module_init(ib_mad_init_module);
2719 module_exit(ib_mad_cleanup_module);