]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/ofed/management/opensm/opensm/osm_sa_slvl_record.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / ofed / management / opensm / opensm / osm_sa_slvl_record.c
1 /*
2  * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4  * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  */
35
36 /*
37  * Abstract:
38  *    Implementation of osm_slvl_rec_rcv_t.
39  * This object represents the SLtoVL Mapping Query Receiver object.
40  * This object is part of the opensm family of objects.
41  */
42
43 #if HAVE_CONFIG_H
44 #  include <config.h>
45 #endif                          /* HAVE_CONFIG_H */
46
47 #include <string.h>
48 #include <iba/ib_types.h>
49 #include <complib/cl_qmap.h>
50 #include <complib/cl_passivelock.h>
51 #include <complib/cl_debug.h>
52 #include <complib/cl_qlist.h>
53 #include <vendor/osm_vendor_api.h>
54 #include <opensm/osm_port.h>
55 #include <opensm/osm_node.h>
56 #include <opensm/osm_helper.h>
57 #include <opensm/osm_pkey.h>
58 #include <opensm/osm_sa.h>
59
60 typedef struct osm_slvl_item {
61         cl_list_item_t list_item;
62         ib_slvl_table_record_t rec;
63 } osm_slvl_item_t;
64
65 typedef struct osm_slvl_search_ctxt {
66         const ib_slvl_table_record_t *p_rcvd_rec;
67         ib_net64_t comp_mask;
68         uint8_t in_port_num;
69         cl_qlist_t *p_list;
70         osm_sa_t *sa;
71         const osm_physp_t *p_req_physp;
72 } osm_slvl_search_ctxt_t;
73
74 /**********************************************************************
75  **********************************************************************/
76 static void
77 __osm_sa_slvl_create(IN osm_sa_t * sa,
78                      IN const osm_physp_t * const p_physp,
79                      IN osm_slvl_search_ctxt_t * const p_ctxt,
80                      IN uint8_t in_port_idx)
81 {
82         osm_slvl_item_t *p_rec_item;
83         uint16_t lid;
84         ib_api_status_t status = IB_SUCCESS;
85
86         OSM_LOG_ENTER(sa->p_log);
87
88         p_rec_item = malloc(sizeof(*p_rec_item));
89         if (p_rec_item == NULL) {
90                 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2602: "
91                         "rec_item alloc failed\n");
92                 status = IB_INSUFFICIENT_RESOURCES;
93                 goto Exit;
94         }
95
96         if (p_physp->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH)
97                 lid = p_physp->port_info.base_lid;
98         else
99                 lid = osm_node_get_base_lid(p_physp->p_node, 0);
100
101         OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
102                 "New SLtoVL Map for: OUT port 0x%016" PRIx64
103                 ", lid 0x%X, port %u to In Port:%u\n",
104                 cl_ntoh64(osm_physp_get_port_guid(p_physp)),
105                 cl_ntoh16(lid), osm_physp_get_port_num(p_physp), in_port_idx);
106
107         memset(p_rec_item, 0, sizeof(*p_rec_item));
108
109         p_rec_item->rec.lid = lid;
110         p_rec_item->rec.out_port_num = osm_physp_get_port_num(p_physp);
111         p_rec_item->rec.in_port_num = in_port_idx;
112         p_rec_item->rec.slvl_tbl =
113             *(osm_physp_get_slvl_tbl(p_physp, in_port_idx));
114
115         cl_qlist_insert_tail(p_ctxt->p_list, &p_rec_item->list_item);
116
117 Exit:
118         OSM_LOG_EXIT(sa->p_log);
119 }
120
121 /**********************************************************************
122  **********************************************************************/
123 static void
124 __osm_sa_slvl_by_comp_mask(IN osm_sa_t * sa,
125                            IN const osm_port_t * const p_port,
126                            osm_slvl_search_ctxt_t * const p_ctxt)
127 {
128         const ib_slvl_table_record_t *p_rcvd_rec;
129         ib_net64_t comp_mask;
130         const osm_physp_t *p_out_physp, *p_in_physp;
131         uint8_t in_port_num, out_port_num;
132         uint8_t num_ports;
133         uint8_t in_port_start, in_port_end;
134         uint8_t out_port_start, out_port_end;
135         const osm_physp_t *p_req_physp;
136
137         OSM_LOG_ENTER(sa->p_log);
138
139         p_rcvd_rec = p_ctxt->p_rcvd_rec;
140         comp_mask = p_ctxt->comp_mask;
141         num_ports = osm_node_get_num_physp(p_port->p_node);
142         in_port_start = 0;
143         in_port_end = num_ports - 1;
144         out_port_start = 0;
145         out_port_end = num_ports - 1;
146         p_req_physp = p_ctxt->p_req_physp;
147
148         if (p_port->p_node->node_info.node_type != IB_NODE_TYPE_SWITCH) {
149                 OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
150                         "Using Physical Default Port Number: 0x%X (for End Node)\n",
151                         p_port->p_physp->port_num);
152                 p_out_physp = p_port->p_physp;
153                 /* check that the p_out_physp and the p_req_physp share a pkey */
154                 if (osm_physp_share_pkey
155                     (sa->p_log, p_req_physp, p_out_physp))
156                         __osm_sa_slvl_create(sa, p_out_physp, p_ctxt, 0);
157         } else {
158                 if (comp_mask & IB_SLVL_COMPMASK_OUT_PORT)
159                         out_port_start = out_port_end =
160                             p_rcvd_rec->out_port_num;
161                 if (comp_mask & IB_SLVL_COMPMASK_IN_PORT)
162                         in_port_start = in_port_end = p_rcvd_rec->in_port_num;
163
164                 for (out_port_num = out_port_start;
165                      out_port_num <= out_port_end; out_port_num++) {
166                         p_out_physp =
167                             osm_node_get_physp_ptr(p_port->p_node,
168                                                    out_port_num);
169                         if (!p_out_physp)
170                                 continue;
171
172                         for (in_port_num = in_port_start;
173                              in_port_num <= in_port_end; in_port_num++) {
174 #if 0
175                                 if (out_port_num && out_port_num == in_port_num)
176                                         continue;
177 #endif
178
179                                 p_in_physp =
180                                     osm_node_get_physp_ptr(p_port->p_node,
181                                                            in_port_num);
182                                 if (!p_in_physp)
183                                         continue;
184
185                                 /* if the requester and the p_out_physp don't share a pkey -
186                                    continue */
187                                 if (!osm_physp_share_pkey
188                                     (sa->p_log, p_req_physp, p_out_physp))
189                                         continue;
190
191                                 __osm_sa_slvl_create(sa, p_out_physp, p_ctxt,
192                                                      in_port_num);
193                         }
194                 }
195         }
196         OSM_LOG_EXIT(sa->p_log);
197 }
198
199 /**********************************************************************
200  **********************************************************************/
201 static void
202 __osm_sa_slvl_by_comp_mask_cb(IN cl_map_item_t * const p_map_item,
203                               IN void *context)
204 {
205         const osm_port_t *const p_port = (osm_port_t *) p_map_item;
206         osm_slvl_search_ctxt_t *const p_ctxt =
207             (osm_slvl_search_ctxt_t *) context;
208
209         __osm_sa_slvl_by_comp_mask(p_ctxt->sa, p_port, p_ctxt);
210 }
211
212 /**********************************************************************
213  **********************************************************************/
214 void osm_slvl_rec_rcv_process(IN void *ctx, IN void *data)
215 {
216         osm_sa_t *sa = ctx;
217         osm_madw_t *p_madw = data;
218         const ib_sa_mad_t *p_rcvd_mad;
219         const ib_slvl_table_record_t *p_rcvd_rec;
220         const osm_port_t *p_port = NULL;
221         cl_qlist_t rec_list;
222         osm_slvl_search_ctxt_t context;
223         ib_api_status_t status = IB_SUCCESS;
224         ib_net64_t comp_mask;
225         osm_physp_t *p_req_physp;
226
227         CL_ASSERT(sa);
228
229         OSM_LOG_ENTER(sa->p_log);
230
231         CL_ASSERT(p_madw);
232
233         p_rcvd_mad = osm_madw_get_sa_mad_ptr(p_madw);
234         p_rcvd_rec =
235             (ib_slvl_table_record_t *) ib_sa_mad_get_payload_ptr(p_rcvd_mad);
236         comp_mask = p_rcvd_mad->comp_mask;
237
238         CL_ASSERT(p_rcvd_mad->attr_id == IB_MAD_ATTR_SLVL_RECORD);
239
240         /* we only support SubnAdmGet and SubnAdmGetTable methods */
241         if (p_rcvd_mad->method != IB_MAD_METHOD_GET &&
242             p_rcvd_mad->method != IB_MAD_METHOD_GETTABLE) {
243                 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2604: "
244                         "Unsupported Method (%s)\n",
245                         ib_get_sa_method_str(p_rcvd_mad->method));
246                 osm_sa_send_error(sa, p_madw, IB_MAD_STATUS_UNSUP_METHOD_ATTR);
247                 goto Exit;
248         }
249
250         /* update the requester physical port. */
251         p_req_physp = osm_get_physp_by_mad_addr(sa->p_log, sa->p_subn,
252                                                 osm_madw_get_mad_addr_ptr
253                                                 (p_madw));
254         if (p_req_physp == NULL) {
255                 OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2603: "
256                         "Cannot find requester physical port\n");
257                 goto Exit;
258         }
259
260         cl_qlist_init(&rec_list);
261
262         context.p_rcvd_rec = p_rcvd_rec;
263         context.p_list = &rec_list;
264         context.comp_mask = p_rcvd_mad->comp_mask;
265         context.sa = sa;
266         context.in_port_num = p_rcvd_rec->in_port_num;
267         context.p_req_physp = p_req_physp;
268
269         cl_plock_acquire(sa->p_lock);
270
271         OSM_LOG(sa->p_log, OSM_LOG_DEBUG,
272                 "Got Query Lid:%u(%02X), In-Port:0x%02X(%02X), Out-Port:0x%02X(%02X)\n",
273                 cl_ntoh16(p_rcvd_rec->lid),
274                 (comp_mask & IB_SLVL_COMPMASK_LID) != 0,
275                 p_rcvd_rec->in_port_num,
276                 (comp_mask & IB_SLVL_COMPMASK_IN_PORT) != 0,
277                 p_rcvd_rec->out_port_num,
278                 (comp_mask & IB_SLVL_COMPMASK_OUT_PORT) != 0);
279
280         /*
281            If the user specified a LID, it obviously narrows our
282            work load, since we don't have to search every port
283          */
284         if (comp_mask & IB_SLVL_COMPMASK_LID) {
285                 status =
286                     osm_get_port_by_base_lid(sa->p_subn, p_rcvd_rec->lid,
287                                              &p_port);
288                 if ((status != IB_SUCCESS) || (p_port == NULL)) {
289                         status = IB_NOT_FOUND;
290                         OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 2608: "
291                                 "No port found with LID %u\n",
292                                 cl_ntoh16(p_rcvd_rec->lid));
293                 }
294         }
295
296         if (status == IB_SUCCESS) {
297                 /* if we have a unique port - no need for a port search */
298                 if (p_port)
299                         /*  this does the loop on all the port phys ports */
300                         __osm_sa_slvl_by_comp_mask(sa, p_port, &context);
301                 else
302                         cl_qmap_apply_func(&sa->p_subn->port_guid_tbl,
303                                            __osm_sa_slvl_by_comp_mask_cb,
304                                            &context);
305         }
306
307         cl_plock_release(sa->p_lock);
308
309         osm_sa_respond(sa, p_madw, sizeof(ib_slvl_table_record_t), &rec_list);
310
311 Exit:
312         OSM_LOG_EXIT(sa->p_log);
313 }