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