This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / infiniband / core / agent.c
1 /*
2  * Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
3  * Copyright (c) 2004 Infinicon Corporation.  All rights reserved.
4  * Copyright (c) 2004 Intel Corporation.  All rights reserved.
5  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
6  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
7  *
8  * This software is available to you under a choice of one of two
9  * licenses.  You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * OpenIB.org BSD license below:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
22  *      - Redistributions in binary form must reproduce the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer in the documentation and/or other materials
25  *        provided with the distribution.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  *
36  * $Id: agent.c 1389 2004-12-27 22:56:47Z roland $
37  */
38
39 #include <linux/dma-mapping.h>
40
41 #include <asm/bug.h>
42
43 #include <ib_smi.h>
44
45 #include "smi.h"
46 #include "agent_priv.h"
47 #include "mad_priv.h"
48
49
50 spinlock_t ib_agent_port_list_lock;
51 static LIST_HEAD(ib_agent_port_list);
52
53 extern kmem_cache_t *ib_mad_cache;
54
55
56 /*
57  * Caller must hold ib_agent_port_list_lock
58  */
59 static inline struct ib_agent_port_private *
60 __ib_get_agent_port(struct ib_device *device, int port_num,
61                     struct ib_mad_agent *mad_agent)
62 {
63         struct ib_agent_port_private *entry;
64
65         BUG_ON(!(!!device ^ !!mad_agent));  /* Exactly one MUST be (!NULL) */
66
67         if (device) {
68                 list_for_each_entry(entry, &ib_agent_port_list, port_list) {
69                         if (entry->dr_smp_agent->device == device &&
70                             entry->port_num == port_num)
71                                 return entry;
72                 }
73         } else {
74                 list_for_each_entry(entry, &ib_agent_port_list, port_list) {
75                         if ((entry->dr_smp_agent == mad_agent) ||
76                             (entry->lr_smp_agent == mad_agent) ||
77                             (entry->perf_mgmt_agent == mad_agent))
78                                 return entry;
79                 }
80         }
81         return NULL;
82 }
83
84 static inline struct ib_agent_port_private *
85 ib_get_agent_port(struct ib_device *device, int port_num,
86                   struct ib_mad_agent *mad_agent)
87 {
88         struct ib_agent_port_private *entry;
89         unsigned long flags;
90
91         spin_lock_irqsave(&ib_agent_port_list_lock, flags);
92         entry = __ib_get_agent_port(device, port_num, mad_agent);
93         spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
94
95         return entry;
96 }
97
98 int smi_check_local_dr_smp(struct ib_smp *smp,
99                            struct ib_device *device,
100                            int port_num)
101 {
102         struct ib_agent_port_private *port_priv;
103
104         if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
105                 return 1;
106         port_priv = ib_get_agent_port(device, port_num, NULL);
107         if (!port_priv) {
108                 printk(KERN_DEBUG SPFX "smi_check_local_dr_smp %s port %d "
109                        "not open\n",
110                        device->name, port_num);
111                 return 1;
112         }
113
114         return smi_check_local_smp(port_priv->dr_smp_agent, smp);
115 }
116
117 static int agent_mad_send(struct ib_mad_agent *mad_agent,
118                           struct ib_agent_port_private *port_priv,
119                           struct ib_mad_private *mad_priv,
120                           struct ib_grh *grh,
121                           struct ib_wc *wc)
122 {
123         struct ib_agent_send_wr *agent_send_wr;
124         struct ib_sge gather_list;
125         struct ib_send_wr send_wr;
126         struct ib_send_wr *bad_send_wr;
127         struct ib_ah_attr ah_attr;
128         unsigned long flags;
129         int ret = 1;
130
131         agent_send_wr = kmalloc(sizeof(*agent_send_wr), GFP_KERNEL);
132         if (!agent_send_wr)
133                 goto out;
134         agent_send_wr->mad = mad_priv;
135
136         /* PCI mapping */
137         gather_list.addr = dma_map_single(mad_agent->device->dma_device,
138                                           &mad_priv->mad,
139                                           sizeof(mad_priv->mad),
140                                           DMA_TO_DEVICE);
141         gather_list.length = sizeof(mad_priv->mad);
142         gather_list.lkey = (*port_priv->mr).lkey;
143
144         send_wr.next = NULL;
145         send_wr.opcode = IB_WR_SEND;
146         send_wr.sg_list = &gather_list;
147         send_wr.num_sge = 1;
148         send_wr.wr.ud.remote_qpn = wc->src_qp; /* DQPN */
149         send_wr.wr.ud.timeout_ms = 0;
150         send_wr.send_flags = IB_SEND_SIGNALED | IB_SEND_SOLICITED;
151
152         ah_attr.dlid = wc->slid;
153         ah_attr.port_num = mad_agent->port_num;
154         ah_attr.src_path_bits = wc->dlid_path_bits;
155         ah_attr.sl = wc->sl;
156         ah_attr.static_rate = 0;
157         ah_attr.ah_flags = 0; /* No GRH */
158         if (mad_priv->mad.mad.mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT) {
159                 if (wc->wc_flags & IB_WC_GRH) {
160                         ah_attr.ah_flags = IB_AH_GRH;
161                         /* Should sgid be looked up ? */
162                         ah_attr.grh.sgid_index = 0;
163                         ah_attr.grh.hop_limit = grh->hop_limit;
164                         ah_attr.grh.flow_label = be32_to_cpup(
165                                 &grh->version_tclass_flow)  & 0xfffff;
166                         ah_attr.grh.traffic_class = (be32_to_cpup(
167                                 &grh->version_tclass_flow) >> 20) & 0xff;
168                         memcpy(ah_attr.grh.dgid.raw,
169                                grh->sgid.raw,
170                                sizeof(ah_attr.grh.dgid));
171                 }
172         }
173
174         agent_send_wr->ah = ib_create_ah(mad_agent->qp->pd, &ah_attr);
175         if (IS_ERR(agent_send_wr->ah)) {
176                 printk(KERN_ERR SPFX "No memory for address handle\n");
177                 kfree(agent_send_wr);
178                 goto out;
179         }
180
181         send_wr.wr.ud.ah = agent_send_wr->ah;
182         if (mad_priv->mad.mad.mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT) {
183                 send_wr.wr.ud.pkey_index = wc->pkey_index;
184                 send_wr.wr.ud.remote_qkey = IB_QP1_QKEY;
185         } else {        /* for SMPs */
186                 send_wr.wr.ud.pkey_index = 0;
187                 send_wr.wr.ud.remote_qkey = 0;
188         }
189         send_wr.wr.ud.mad_hdr = &mad_priv->mad.mad.mad_hdr;
190         send_wr.wr_id = (unsigned long)agent_send_wr;
191
192         pci_unmap_addr_set(agent_send_wr, mapping, gather_list.addr);
193
194         /* Send */
195         spin_lock_irqsave(&port_priv->send_list_lock, flags);
196         if (ib_post_send_mad(mad_agent, &send_wr, &bad_send_wr)) {
197                 spin_unlock_irqrestore(&port_priv->send_list_lock, flags);
198                 dma_unmap_single(mad_agent->device->dma_device,
199                                  pci_unmap_addr(agent_send_wr, mapping),
200                                  sizeof(mad_priv->mad),
201                                  DMA_TO_DEVICE);
202                 ib_destroy_ah(agent_send_wr->ah);
203                 kfree(agent_send_wr);
204         } else {
205                 list_add_tail(&agent_send_wr->send_list,
206                               &port_priv->send_posted_list);
207                 spin_unlock_irqrestore(&port_priv->send_list_lock, flags);
208                 ret = 0;
209         }
210
211 out:
212         return ret;
213 }
214
215 int agent_send(struct ib_mad_private *mad,
216                struct ib_grh *grh,
217                struct ib_wc *wc,
218                struct ib_device *device,
219                int port_num)
220 {
221         struct ib_agent_port_private *port_priv;
222         struct ib_mad_agent *mad_agent;
223
224         port_priv = ib_get_agent_port(device, port_num, NULL);
225         if (!port_priv) {
226                 printk(KERN_DEBUG SPFX "agent_send %s port %d not open\n",
227                        device->name, port_num);
228                 return 1;
229         }
230
231         /* Get mad agent based on mgmt_class in MAD */
232         switch (mad->mad.mad.mad_hdr.mgmt_class) {
233                 case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
234                         mad_agent = port_priv->dr_smp_agent;
235                         break;
236                 case IB_MGMT_CLASS_SUBN_LID_ROUTED:
237                         mad_agent = port_priv->lr_smp_agent;
238                         break;
239                 case IB_MGMT_CLASS_PERF_MGMT:
240                         mad_agent = port_priv->perf_mgmt_agent;
241                         break;
242                 default:
243                         return 1;
244         }
245
246         return agent_mad_send(mad_agent, port_priv, mad, grh, wc);
247 }
248
249 static void agent_send_handler(struct ib_mad_agent *mad_agent,
250                                struct ib_mad_send_wc *mad_send_wc)
251 {
252         struct ib_agent_port_private    *port_priv;
253         struct ib_agent_send_wr         *agent_send_wr;
254         unsigned long                   flags;
255
256         /* Find matching MAD agent */
257         port_priv = ib_get_agent_port(NULL, 0, mad_agent);
258         if (!port_priv) {
259                 printk(KERN_ERR SPFX "agent_send_handler: no matching MAD "
260                        "agent %p\n", mad_agent);
261                 return;
262         }
263
264         agent_send_wr = (struct ib_agent_send_wr *)(unsigned long)mad_send_wc->wr_id;
265         spin_lock_irqsave(&port_priv->send_list_lock, flags);
266         /* Remove completed send from posted send MAD list */
267         list_del(&agent_send_wr->send_list);
268         spin_unlock_irqrestore(&port_priv->send_list_lock, flags);
269
270         /* Unmap PCI */
271         dma_unmap_single(mad_agent->device->dma_device,
272                          pci_unmap_addr(agent_send_wr, mapping),
273                          sizeof(agent_send_wr->mad->mad),
274                          DMA_TO_DEVICE);
275
276         ib_destroy_ah(agent_send_wr->ah);
277
278         /* Release allocated memory */
279         kmem_cache_free(ib_mad_cache, agent_send_wr->mad);
280         kfree(agent_send_wr);
281 }
282
283 int ib_agent_port_open(struct ib_device *device, int port_num)
284 {
285         int ret;
286         struct ib_agent_port_private *port_priv;
287         struct ib_mad_reg_req reg_req;
288         unsigned long flags;
289
290         /* First, check if port already open for SMI */
291         port_priv = ib_get_agent_port(device, port_num, NULL);
292         if (port_priv) {
293                 printk(KERN_DEBUG SPFX "%s port %d already open\n",
294                        device->name, port_num);
295                 return 0;
296         }
297
298         /* Create new device info */
299         port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL);
300         if (!port_priv) {
301                 printk(KERN_ERR SPFX "No memory for ib_agent_port_private\n");
302                 ret = -ENOMEM;
303                 goto error1;
304         }
305
306         memset(port_priv, 0, sizeof *port_priv);
307         port_priv->port_num = port_num;
308         spin_lock_init(&port_priv->send_list_lock);
309         INIT_LIST_HEAD(&port_priv->send_posted_list);
310
311         /* Obtain MAD agent for directed route SM class */
312         reg_req.mgmt_class = IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE;
313         reg_req.mgmt_class_version = 1;
314
315         port_priv->dr_smp_agent = ib_register_mad_agent(device, port_num,
316                                                         IB_QPT_SMI,
317                                                         NULL, 0,
318                                                        &agent_send_handler,
319                                                         NULL, NULL);
320
321         if (IS_ERR(port_priv->dr_smp_agent)) {
322                 ret = PTR_ERR(port_priv->dr_smp_agent);
323                 goto error2;
324         }
325
326         /* Obtain MAD agent for LID routed SM class */
327         reg_req.mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
328         port_priv->lr_smp_agent = ib_register_mad_agent(device, port_num,
329                                                         IB_QPT_SMI,
330                                                         NULL, 0,
331                                                        &agent_send_handler,
332                                                         NULL, NULL);
333         if (IS_ERR(port_priv->lr_smp_agent)) {
334                 ret = PTR_ERR(port_priv->lr_smp_agent);
335                 goto error3;
336         }
337
338         /* Obtain MAD agent for PerfMgmt class */
339         reg_req.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
340         port_priv->perf_mgmt_agent = ib_register_mad_agent(device, port_num,
341                                                            IB_QPT_GSI,
342                                                            NULL, 0,
343                                                           &agent_send_handler,
344                                                            NULL, NULL);
345         if (IS_ERR(port_priv->perf_mgmt_agent)) {
346                 ret = PTR_ERR(port_priv->perf_mgmt_agent);
347                 goto error4;
348         }
349
350         port_priv->mr = ib_get_dma_mr(port_priv->dr_smp_agent->qp->pd,
351                                       IB_ACCESS_LOCAL_WRITE);
352         if (IS_ERR(port_priv->mr)) {
353                 printk(KERN_ERR SPFX "Couldn't get DMA MR\n");
354                 ret = PTR_ERR(port_priv->mr);
355                 goto error5;
356         }
357
358         spin_lock_irqsave(&ib_agent_port_list_lock, flags);
359         list_add_tail(&port_priv->port_list, &ib_agent_port_list);
360         spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
361
362         return 0;
363
364 error5:
365         ib_unregister_mad_agent(port_priv->perf_mgmt_agent);
366 error4:
367         ib_unregister_mad_agent(port_priv->lr_smp_agent);
368 error3:
369         ib_unregister_mad_agent(port_priv->dr_smp_agent);
370 error2:
371         kfree(port_priv);
372 error1:
373         return ret;
374 }
375
376 int ib_agent_port_close(struct ib_device *device, int port_num)
377 {
378         struct ib_agent_port_private *port_priv;
379         unsigned long flags;
380
381         spin_lock_irqsave(&ib_agent_port_list_lock, flags);
382         port_priv = __ib_get_agent_port(device, port_num, NULL);
383         if (port_priv == NULL) {
384                 spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
385                 printk(KERN_ERR SPFX "Port %d not found\n", port_num);
386                 return -ENODEV;
387         }
388         list_del(&port_priv->port_list);
389         spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
390
391         ib_dereg_mr(port_priv->mr);
392
393         ib_unregister_mad_agent(port_priv->perf_mgmt_agent);
394         ib_unregister_mad_agent(port_priv->lr_smp_agent);
395         ib_unregister_mad_agent(port_priv->dr_smp_agent);
396         kfree(port_priv);
397
398         return 0;
399 }