]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/infiniband/ulp/opa_vnic/opa_vnic_vema.c
6ff7be6113cd3a5af3c7786af5de007bad59acd3
[karo-tx-linux.git] / drivers / infiniband / ulp / opa_vnic / opa_vnic_vema.c
1 /*
2  * Copyright(c) 2017 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47
48 /*
49  * This file contains OPA Virtual Network Interface Controller (VNIC)
50  * Ethernet Management Agent (EMA) driver
51  */
52
53 #include <linux/module.h>
54 #include <rdma/ib_addr.h>
55 #include <rdma/ib_smi.h>
56
57 #include "opa_vnic_internal.h"
58
59 #define DRV_VERSION "1.0"
60 char opa_vnic_driver_name[] = "opa_vnic";
61 const char opa_vnic_driver_version[] = DRV_VERSION;
62
63 /*
64  * The trap service level is kept in bits 3 to 7 in the trap_sl_rsvd
65  * field in the class port info MAD.
66  */
67 #define GET_TRAP_SL_FROM_CLASS_PORT_INFO(x)  (((x) >> 3) & 0x1f)
68
69 /* Cap trap bursts to a reasonable limit good for normal cases */
70 #define OPA_VNIC_TRAP_BURST_LIMIT 4
71
72 /*
73  * VNIC trap limit timeout.
74  * Inverse of cap2_mask response time out (1.0737 secs) = 0.9
75  * secs approx IB spec 13.4.6.2.1 PortInfoSubnetTimeout and
76  * 13.4.9 Traps.
77  */
78 #define OPA_VNIC_TRAP_TIMEOUT  ((4096 * (1UL << 18)) / 1000)
79
80 #define OPA_VNIC_UNSUP_ATTR  \
81                 cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB)
82
83 #define OPA_VNIC_INVAL_ATTR  \
84                 cpu_to_be16(IB_MGMT_MAD_STATUS_INVALID_ATTRIB_VALUE)
85
86 #define OPA_VNIC_CLASS_CAP_TRAP   0x1
87
88 /* Maximum number of VNIC ports supported */
89 #define OPA_VNIC_MAX_NUM_VPORT    255
90
91 struct opa_class_port_info {
92         u8 base_version;
93         u8 class_version;
94         __be16 cap_mask;
95         __be32 cap_mask2_resp_time;
96
97         u8 redirect_gid[16];
98         __be32 redirect_tc_fl;
99         __be32 redirect_lid;
100         __be32 redirect_sl_qp;
101         __be32 redirect_qkey;
102
103         u8 trap_gid[16];
104         __be32 trap_tc_fl;
105         __be32 trap_lid;
106         __be32 trap_hl_qp;
107         __be32 trap_qkey;
108
109         __be16 trap_pkey;
110         __be16 redirect_pkey;
111
112         u8 trap_sl_rsvd;
113         u8 reserved[3];
114 } __packed;
115
116 /**
117  * struct opa_vnic_vema_port -- VNIC VEMA port details
118  * @cport: pointer to port
119  * @mad_agent: pointer to mad agent for port
120  * @class_port_info: Class port info information.
121  * @tid: Transaction id
122  * @port_num: OPA port number
123  * @vport_idr: vnic ports idr
124  * @event_handler: ib event handler
125  * @lock: adapter interface lock
126  */
127 struct opa_vnic_vema_port {
128         struct opa_vnic_ctrl_port      *cport;
129         struct ib_mad_agent            *mad_agent;
130         struct opa_class_port_info      class_port_info;
131         u64                             tid;
132         u8                              port_num;
133         struct idr                      vport_idr;
134         struct ib_event_handler         event_handler;
135
136         /* Lock to query/update network adapter */
137         struct mutex                    lock;
138 };
139
140 static void opa_vnic_vema_add_one(struct ib_device *device);
141 static void opa_vnic_vema_rem_one(struct ib_device *device,
142                                   void *client_data);
143
144 static struct ib_client opa_vnic_client = {
145         .name   = opa_vnic_driver_name,
146         .add    = opa_vnic_vema_add_one,
147         .remove = opa_vnic_vema_rem_one,
148 };
149
150 /**
151  * vema_get_vport_num -- Get the vnic from the mad
152  * @recvd_mad:  Received mad
153  *
154  * Return: returns value of the vnic port number
155  */
156 static inline u8 vema_get_vport_num(struct opa_vnic_vema_mad *recvd_mad)
157 {
158         return be32_to_cpu(recvd_mad->mad_hdr.attr_mod) & 0xff;
159 }
160
161 /**
162  * vema_get_vport_adapter -- Get vnic port adapter from recvd mad
163  * @recvd_mad: received mad
164  * @port: ptr to port struct on which MAD was recvd
165  *
166  * Return: vnic adapter
167  */
168 static inline struct opa_vnic_adapter *
169 vema_get_vport_adapter(struct opa_vnic_vema_mad *recvd_mad,
170                        struct opa_vnic_vema_port *port)
171 {
172         u8 vport_num = vema_get_vport_num(recvd_mad);
173
174         return idr_find(&port->vport_idr, vport_num);
175 }
176
177 /**
178  * vema_mac_tbl_req_ok -- Check if mac request has correct values
179  * @mac_tbl: mac table
180  *
181  * This function checks for the validity of the offset and number of
182  * entries required.
183  *
184  * Return: true if offset and num_entries are valid
185  */
186 static inline bool vema_mac_tbl_req_ok(struct opa_veswport_mactable *mac_tbl)
187 {
188         u16 offset, num_entries;
189         u16 req_entries = ((OPA_VNIC_EMA_DATA - sizeof(*mac_tbl)) /
190                            sizeof(mac_tbl->tbl_entries[0]));
191
192         offset = be16_to_cpu(mac_tbl->offset);
193         num_entries = be16_to_cpu(mac_tbl->num_entries);
194
195         return ((num_entries <= req_entries) &&
196                 (offset + num_entries <= OPA_VNIC_MAC_TBL_MAX_ENTRIES));
197 }
198
199 /*
200  * Return the power on default values in the port info structure
201  * in big endian format as required by MAD.
202  */
203 static inline void vema_get_pod_values(struct opa_veswport_info *port_info)
204 {
205         memset(port_info, 0, sizeof(*port_info));
206         port_info->vport.max_mac_tbl_ent =
207                 cpu_to_be16(OPA_VNIC_MAC_TBL_MAX_ENTRIES);
208         port_info->vport.max_smac_ent =
209                 cpu_to_be16(OPA_VNIC_MAX_SMAC_LIMIT);
210         port_info->vport.oper_state = OPA_VNIC_STATE_DROP_ALL;
211         port_info->vport.config_state = OPA_VNIC_STATE_DROP_ALL;
212 }
213
214 /**
215  * vema_add_vport -- Add a new vnic port
216  * @port: ptr to opa_vnic_vema_port struct
217  * @vport_num: vnic port number (to be added)
218  *
219  * Return a pointer to the vnic adapter structure
220  */
221 static struct opa_vnic_adapter *vema_add_vport(struct opa_vnic_vema_port *port,
222                                                u8 vport_num)
223 {
224         struct opa_vnic_ctrl_port *cport = port->cport;
225         struct opa_vnic_adapter *adapter;
226
227         adapter = opa_vnic_add_netdev(cport->ibdev, port->port_num, vport_num);
228         if (!IS_ERR(adapter)) {
229                 int rc;
230
231                 adapter->cport = cport;
232                 rc = idr_alloc(&port->vport_idr, adapter, vport_num,
233                                vport_num + 1, GFP_NOWAIT);
234                 if (rc < 0) {
235                         opa_vnic_rem_netdev(adapter);
236                         adapter = ERR_PTR(rc);
237                 }
238         }
239
240         return adapter;
241 }
242
243 /**
244  * vema_get_class_port_info -- Get class info for port
245  * @port:  Port on whic MAD was received
246  * @recvd_mad: pointer to the received mad
247  * @rsp_mad:   pointer to respose mad
248  *
249  * This function copies the latest class port info value set for the
250  * port and stores it for generating traps
251  */
252 static void vema_get_class_port_info(struct opa_vnic_vema_port *port,
253                                      struct opa_vnic_vema_mad *recvd_mad,
254                                      struct opa_vnic_vema_mad *rsp_mad)
255 {
256         struct opa_class_port_info *port_info;
257
258         port_info = (struct opa_class_port_info *)rsp_mad->data;
259         memcpy(port_info, &port->class_port_info, sizeof(*port_info));
260         port_info->base_version = OPA_MGMT_BASE_VERSION,
261         port_info->class_version = OPA_EMA_CLASS_VERSION;
262
263         /*
264          * Set capability mask bit indicating agent generates traps,
265          * and set the maximum number of VNIC ports supported.
266          */
267         port_info->cap_mask = cpu_to_be16((OPA_VNIC_CLASS_CAP_TRAP |
268                                            (OPA_VNIC_MAX_NUM_VPORT << 8)));
269
270         /*
271          * Since a get routine is always sent by the EM first we
272          * set the expected response time to
273          * 4.096 usec * 2^18 == 1.0737 sec here.
274          */
275         port_info->cap_mask2_resp_time = cpu_to_be32(18);
276 }
277
278 /**
279  * vema_set_class_port_info -- Get class info for port
280  * @port:  Port on whic MAD was received
281  * @recvd_mad: pointer to the received mad
282  * @rsp_mad:   pointer to respose mad
283  *
284  * This function updates the port class info for the specific vnic
285  * and sets up the response mad data
286  */
287 static void vema_set_class_port_info(struct opa_vnic_vema_port *port,
288                                      struct opa_vnic_vema_mad *recvd_mad,
289                                      struct opa_vnic_vema_mad *rsp_mad)
290 {
291         memcpy(&port->class_port_info, recvd_mad->data,
292                sizeof(port->class_port_info));
293
294         vema_get_class_port_info(port, recvd_mad, rsp_mad);
295 }
296
297 /**
298  * vema_get_veswport_info -- Get veswport info
299  * @port:      source port on which MAD was received
300  * @recvd_mad: pointer to the received mad
301  * @rsp_mad:   pointer to respose mad
302  */
303 static void vema_get_veswport_info(struct opa_vnic_vema_port *port,
304                                    struct opa_vnic_vema_mad *recvd_mad,
305                                    struct opa_vnic_vema_mad *rsp_mad)
306 {
307         struct opa_veswport_info *port_info =
308                                   (struct opa_veswport_info *)rsp_mad->data;
309         struct opa_vnic_adapter *adapter;
310
311         adapter = vema_get_vport_adapter(recvd_mad, port);
312         if (adapter) {
313                 memset(port_info, 0, sizeof(*port_info));
314                 opa_vnic_get_vesw_info(adapter, &port_info->vesw);
315                 opa_vnic_get_per_veswport_info(adapter,
316                                                &port_info->vport);
317         } else {
318                 vema_get_pod_values(port_info);
319         }
320 }
321
322 /**
323  * vema_set_veswport_info -- Set veswport info
324  * @port:      source port on which MAD was received
325  * @recvd_mad: pointer to the received mad
326  * @rsp_mad:   pointer to respose mad
327  *
328  * This function gets the port class infor for vnic
329  */
330 static void vema_set_veswport_info(struct opa_vnic_vema_port *port,
331                                    struct opa_vnic_vema_mad *recvd_mad,
332                                    struct opa_vnic_vema_mad *rsp_mad)
333 {
334         struct opa_vnic_ctrl_port *cport = port->cport;
335         struct opa_veswport_info *port_info;
336         struct opa_vnic_adapter *adapter;
337         u8 vport_num;
338
339         vport_num = vema_get_vport_num(recvd_mad);
340
341         adapter = vema_get_vport_adapter(recvd_mad, port);
342         if (!adapter) {
343                 adapter = vema_add_vport(port, vport_num);
344                 if (IS_ERR(adapter)) {
345                         c_err("failed to add vport %d: %ld\n",
346                               vport_num, PTR_ERR(adapter));
347                         goto err_exit;
348                 }
349         }
350
351         port_info = (struct opa_veswport_info *)recvd_mad->data;
352         opa_vnic_set_vesw_info(adapter, &port_info->vesw);
353         opa_vnic_set_per_veswport_info(adapter, &port_info->vport);
354
355         /* Process the new config settings */
356         opa_vnic_process_vema_config(adapter);
357
358         vema_get_veswport_info(port, recvd_mad, rsp_mad);
359         return;
360
361 err_exit:
362         rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
363 }
364
365 /**
366  * vema_get_mac_entries -- Get MAC entries in VNIC MAC table
367  * @port:      source port on which MAD was received
368  * @recvd_mad: pointer to the received mad
369  * @rsp_mad:   pointer to respose mad
370  *
371  * This function gets the MAC entries that are programmed into
372  * the VNIC MAC forwarding table. It checks for the validity of
373  * the index into the MAC table and the number of entries that
374  * are to be retrieved.
375  */
376 static void vema_get_mac_entries(struct opa_vnic_vema_port *port,
377                                  struct opa_vnic_vema_mad *recvd_mad,
378                                  struct opa_vnic_vema_mad *rsp_mad)
379 {
380         struct opa_veswport_mactable *mac_tbl_in, *mac_tbl_out;
381         struct opa_vnic_adapter *adapter;
382
383         adapter = vema_get_vport_adapter(recvd_mad, port);
384         if (!adapter) {
385                 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
386                 return;
387         }
388
389         mac_tbl_in = (struct opa_veswport_mactable *)recvd_mad->data;
390         mac_tbl_out = (struct opa_veswport_mactable *)rsp_mad->data;
391
392         if (vema_mac_tbl_req_ok(mac_tbl_in)) {
393                 mac_tbl_out->offset = mac_tbl_in->offset;
394                 mac_tbl_out->num_entries = mac_tbl_in->num_entries;
395                 opa_vnic_query_mac_tbl(adapter, mac_tbl_out);
396         } else {
397                 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
398         }
399 }
400
401 /**
402  * vema_set_mac_entries -- Set MAC entries in VNIC MAC table
403  * @port:      source port on which MAD was received
404  * @recvd_mad: pointer to the received mad
405  * @rsp_mad:   pointer to respose mad
406  *
407  * This function sets the MAC entries in the VNIC forwarding table
408  * It checks for the validity of the index and the number of forwarding
409  * table entries to be programmed.
410  */
411 static void vema_set_mac_entries(struct opa_vnic_vema_port *port,
412                                  struct opa_vnic_vema_mad *recvd_mad,
413                                  struct opa_vnic_vema_mad *rsp_mad)
414 {
415         struct opa_veswport_mactable *mac_tbl;
416         struct opa_vnic_adapter *adapter;
417
418         adapter = vema_get_vport_adapter(recvd_mad, port);
419         if (!adapter) {
420                 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
421                 return;
422         }
423
424         mac_tbl = (struct opa_veswport_mactable *)recvd_mad->data;
425         if (vema_mac_tbl_req_ok(mac_tbl)) {
426                 if (opa_vnic_update_mac_tbl(adapter, mac_tbl))
427                         rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
428         } else {
429                 rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
430         }
431         vema_get_mac_entries(port, recvd_mad, rsp_mad);
432 }
433
434 /**
435  * vema_set_delete_vesw -- Reset VESW info to POD values
436  * @port:      source port on which MAD was received
437  * @recvd_mad: pointer to the received mad
438  * @rsp_mad:   pointer to respose mad
439  *
440  * This function clears all the fields of veswport info for the requested vesw
441  * and sets them back to the power-on default values. It does not delete the
442  * vesw.
443  */
444 static void vema_set_delete_vesw(struct opa_vnic_vema_port *port,
445                                  struct opa_vnic_vema_mad *recvd_mad,
446                                  struct opa_vnic_vema_mad *rsp_mad)
447 {
448         struct opa_veswport_info *port_info =
449                                   (struct opa_veswport_info *)rsp_mad->data;
450         struct opa_vnic_adapter *adapter;
451
452         adapter = vema_get_vport_adapter(recvd_mad, port);
453         if (!adapter) {
454                 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
455                 return;
456         }
457
458         vema_get_pod_values(port_info);
459         opa_vnic_set_vesw_info(adapter, &port_info->vesw);
460         opa_vnic_set_per_veswport_info(adapter, &port_info->vport);
461
462         /* Process the new config settings */
463         opa_vnic_process_vema_config(adapter);
464
465         opa_vnic_release_mac_tbl(adapter);
466
467         vema_get_veswport_info(port, recvd_mad, rsp_mad);
468 }
469
470 /**
471  * vema_get_mac_list -- Get the unicast/multicast macs.
472  * @port:      source port on which MAD was received
473  * @recvd_mad: Received mad contains fields to set vnic parameters
474  * @rsp_mad:   Response mad to be built
475  * @attr_id:   Attribute ID indicating multicast or unicast mac list
476  */
477 static void vema_get_mac_list(struct opa_vnic_vema_port *port,
478                               struct opa_vnic_vema_mad *recvd_mad,
479                               struct opa_vnic_vema_mad *rsp_mad,
480                               u16 attr_id)
481 {
482         struct opa_veswport_iface_macs *macs_in, *macs_out;
483         int max_entries = (OPA_VNIC_EMA_DATA - sizeof(*macs_out)) / ETH_ALEN;
484         struct opa_vnic_adapter *adapter;
485
486         adapter = vema_get_vport_adapter(recvd_mad, port);
487         if (!adapter) {
488                 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
489                 return;
490         }
491
492         macs_in = (struct opa_veswport_iface_macs *)recvd_mad->data;
493         macs_out = (struct opa_veswport_iface_macs *)rsp_mad->data;
494
495         macs_out->start_idx = macs_in->start_idx;
496         if (macs_in->num_macs_in_msg)
497                 macs_out->num_macs_in_msg = macs_in->num_macs_in_msg;
498         else
499                 macs_out->num_macs_in_msg = cpu_to_be16(max_entries);
500
501         if (attr_id == OPA_EM_ATTR_IFACE_MCAST_MACS)
502                 opa_vnic_query_mcast_macs(adapter, macs_out);
503         else
504                 opa_vnic_query_ucast_macs(adapter, macs_out);
505 }
506
507 /**
508  * vema_get_summary_counters -- Gets summary counters.
509  * @port:      source port on which MAD was received
510  * @recvd_mad: Received mad contains fields to set vnic parameters
511  * @rsp_mad:   Response mad to be built
512  */
513 static void vema_get_summary_counters(struct opa_vnic_vema_port *port,
514                                       struct opa_vnic_vema_mad *recvd_mad,
515                                       struct opa_vnic_vema_mad *rsp_mad)
516 {
517         struct opa_veswport_summary_counters *cntrs;
518         struct opa_vnic_adapter *adapter;
519
520         adapter = vema_get_vport_adapter(recvd_mad, port);
521         if (adapter) {
522                 cntrs = (struct opa_veswport_summary_counters *)rsp_mad->data;
523                 opa_vnic_get_summary_counters(adapter, cntrs);
524         } else {
525                 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
526         }
527 }
528
529 /**
530  * vema_get_error_counters -- Gets summary counters.
531  * @port:      source port on which MAD was received
532  * @recvd_mad: Received mad contains fields to set vnic parameters
533  * @rsp_mad:   Response mad to be built
534  */
535 static void vema_get_error_counters(struct opa_vnic_vema_port *port,
536                                     struct opa_vnic_vema_mad *recvd_mad,
537                                     struct opa_vnic_vema_mad *rsp_mad)
538 {
539         struct opa_veswport_error_counters *cntrs;
540         struct opa_vnic_adapter *adapter;
541
542         adapter = vema_get_vport_adapter(recvd_mad, port);
543         if (adapter) {
544                 cntrs = (struct opa_veswport_error_counters *)rsp_mad->data;
545                 opa_vnic_get_error_counters(adapter, cntrs);
546         } else {
547                 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
548         }
549 }
550
551 /**
552  * vema_get -- Process received get MAD
553  * @port:      source port on which MAD was received
554  * @recvd_mad: Received mad
555  * @rsp_mad:   Response mad to be built
556  */
557 static void vema_get(struct opa_vnic_vema_port *port,
558                      struct opa_vnic_vema_mad *recvd_mad,
559                      struct opa_vnic_vema_mad *rsp_mad)
560 {
561         u16 attr_id = be16_to_cpu(recvd_mad->mad_hdr.attr_id);
562
563         switch (attr_id) {
564         case OPA_EM_ATTR_CLASS_PORT_INFO:
565                 vema_get_class_port_info(port, recvd_mad, rsp_mad);
566                 break;
567         case OPA_EM_ATTR_VESWPORT_INFO:
568                 vema_get_veswport_info(port, recvd_mad, rsp_mad);
569                 break;
570         case OPA_EM_ATTR_VESWPORT_MAC_ENTRIES:
571                 vema_get_mac_entries(port, recvd_mad, rsp_mad);
572                 break;
573         case OPA_EM_ATTR_IFACE_UCAST_MACS:
574                 /* fall through */
575         case OPA_EM_ATTR_IFACE_MCAST_MACS:
576                 vema_get_mac_list(port, recvd_mad, rsp_mad, attr_id);
577                 break;
578         case OPA_EM_ATTR_VESWPORT_SUMMARY_COUNTERS:
579                 vema_get_summary_counters(port, recvd_mad, rsp_mad);
580                 break;
581         case OPA_EM_ATTR_VESWPORT_ERROR_COUNTERS:
582                 vema_get_error_counters(port, recvd_mad, rsp_mad);
583                 break;
584         default:
585                 rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
586                 break;
587         }
588 }
589
590 /**
591  * vema_set -- Process received set MAD
592  * @port:      source port on which MAD was received
593  * @recvd_mad: Received mad contains fields to set vnic parameters
594  * @rsp_mad:   Response mad to be built
595  */
596 static void vema_set(struct opa_vnic_vema_port *port,
597                      struct opa_vnic_vema_mad *recvd_mad,
598                      struct opa_vnic_vema_mad *rsp_mad)
599 {
600         u16 attr_id = be16_to_cpu(recvd_mad->mad_hdr.attr_id);
601
602         switch (attr_id) {
603         case OPA_EM_ATTR_CLASS_PORT_INFO:
604                 vema_set_class_port_info(port, recvd_mad, rsp_mad);
605                 break;
606         case OPA_EM_ATTR_VESWPORT_INFO:
607                 vema_set_veswport_info(port, recvd_mad, rsp_mad);
608                 break;
609         case OPA_EM_ATTR_VESWPORT_MAC_ENTRIES:
610                 vema_set_mac_entries(port, recvd_mad, rsp_mad);
611                 break;
612         case OPA_EM_ATTR_DELETE_VESW:
613                 vema_set_delete_vesw(port, recvd_mad, rsp_mad);
614                 break;
615         default:
616                 rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
617                 break;
618         }
619 }
620
621 /**
622  * vema_send -- Send handler for VEMA MAD agent
623  * @mad_agent: pointer to the mad agent
624  * @mad_wc:    pointer to mad send work completion information
625  *
626  * Free all the data structures associated with the sent MAD
627  */
628 static void vema_send(struct ib_mad_agent *mad_agent,
629                       struct ib_mad_send_wc *mad_wc)
630 {
631         ib_destroy_ah(mad_wc->send_buf->ah);
632         ib_free_send_mad(mad_wc->send_buf);
633 }
634
635 /**
636  * vema_recv -- Recv handler for VEMA MAD agent
637  * @mad_agent: pointer to the mad agent
638  * @send_buf: Send buffer if found, else NULL
639  * @mad_wc:    pointer to mad send work completion information
640  *
641  * Handle only set and get methods and respond to other methods
642  * as unsupported. Allocate response buffer and address handle
643  * for the response MAD.
644  */
645 static void vema_recv(struct ib_mad_agent *mad_agent,
646                       struct ib_mad_send_buf *send_buf,
647                       struct ib_mad_recv_wc *mad_wc)
648 {
649         struct opa_vnic_vema_port *port;
650         struct ib_ah              *ah;
651         struct ib_mad_send_buf    *rsp;
652         struct opa_vnic_vema_mad  *vema_mad;
653
654         if (!mad_wc || !mad_wc->recv_buf.mad)
655                 return;
656
657         port = mad_agent->context;
658         ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
659                                   mad_wc->recv_buf.grh, mad_agent->port_num);
660         if (IS_ERR(ah))
661                 goto free_recv_mad;
662
663         rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
664                                  mad_wc->wc->pkey_index, 0,
665                                  IB_MGMT_VENDOR_HDR, OPA_VNIC_EMA_DATA,
666                                  GFP_KERNEL, OPA_MGMT_BASE_VERSION);
667         if (IS_ERR(rsp))
668                 goto err_rsp;
669
670         rsp->ah = ah;
671         vema_mad = rsp->mad;
672         memcpy(vema_mad, mad_wc->recv_buf.mad, IB_MGMT_VENDOR_HDR);
673         vema_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
674         vema_mad->mad_hdr.status = 0;
675
676         /* Lock ensures network adapter is not removed */
677         mutex_lock(&port->lock);
678
679         switch (mad_wc->recv_buf.mad->mad_hdr.method) {
680         case IB_MGMT_METHOD_GET:
681                 vema_get(port, (struct opa_vnic_vema_mad *)mad_wc->recv_buf.mad,
682                          vema_mad);
683                 break;
684         case IB_MGMT_METHOD_SET:
685                 vema_set(port, (struct opa_vnic_vema_mad *)mad_wc->recv_buf.mad,
686                          vema_mad);
687                 break;
688         default:
689                 vema_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
690                 break;
691         }
692         mutex_unlock(&port->lock);
693
694         if (!ib_post_send_mad(rsp, NULL)) {
695                 /*
696                  * with post send successful ah and send mad
697                  * will be destroyed in send handler
698                  */
699                 goto free_recv_mad;
700         }
701
702         ib_free_send_mad(rsp);
703
704 err_rsp:
705         ib_destroy_ah(ah);
706 free_recv_mad:
707         ib_free_recv_mad(mad_wc);
708 }
709
710 /**
711  * vema_get_port -- Gets the opa_vnic_vema_port
712  * @cport: pointer to control dev
713  * @port_num: Port number
714  *
715  * This function loops through the ports and returns
716  * the opa_vnic_vema port structure that is associated
717  * with the OPA port number
718  *
719  * Return: ptr to requested opa_vnic_vema_port strucure
720  *         if success, NULL if not
721  */
722 static struct opa_vnic_vema_port *
723 vema_get_port(struct opa_vnic_ctrl_port *cport, u8 port_num)
724 {
725         struct opa_vnic_vema_port *port = (void *)cport + sizeof(*cport);
726
727         if (port_num > cport->num_ports)
728                 return NULL;
729
730         return port + (port_num - 1);
731 }
732
733 /**
734  * opa_vnic_vema_send_trap -- This function sends a trap to the EM
735  * @cport: pointer to vnic control port
736  * @data: pointer to trap data filled by calling function
737  * @lid:  issuers lid (encap_slid from vesw_port_info)
738  *
739  * This function is called from the VNIC driver to send a trap if there
740  * is somethng the EM should be notified about. These events currently
741  * are
742  * 1) UNICAST INTERFACE MACADDRESS changes
743  * 2) MULTICAST INTERFACE MACADDRESS changes
744  * 3) ETHERNET LINK STATUS changes
745  * While allocating the send mad the remote site qpn used is 1
746  * as this is the well known QP.
747  *
748  */
749 void opa_vnic_vema_send_trap(struct opa_vnic_adapter *adapter,
750                              struct __opa_veswport_trap *data, u32 lid)
751 {
752         struct opa_vnic_ctrl_port *cport = adapter->cport;
753         struct ib_mad_send_buf *send_buf;
754         struct opa_vnic_vema_port *port;
755         struct ib_device *ibp;
756         struct opa_vnic_vema_mad_trap *trap_mad;
757         struct opa_class_port_info *class;
758         struct ib_ah_attr ah_attr;
759         struct ib_ah *ah;
760         struct opa_veswport_trap *trap;
761         u32 trap_lid;
762         u16 pkey_idx;
763
764         if (!cport)
765                 goto err_exit;
766         ibp = cport->ibdev;
767         port = vema_get_port(cport, data->opaportnum);
768         if (!port || !port->mad_agent)
769                 goto err_exit;
770
771         if (time_before(jiffies, adapter->trap_timeout)) {
772                 if (adapter->trap_count == OPA_VNIC_TRAP_BURST_LIMIT) {
773                         v_warn("Trap rate exceeded\n");
774                         goto err_exit;
775                 } else {
776                         adapter->trap_count++;
777                 }
778         } else {
779                 adapter->trap_count = 0;
780         }
781
782         class = &port->class_port_info;
783         /* Set up address handle */
784         memset(&ah_attr, 0, sizeof(ah_attr));
785         ah_attr.sl = GET_TRAP_SL_FROM_CLASS_PORT_INFO(class->trap_sl_rsvd);
786         ah_attr.port_num = port->port_num;
787         trap_lid = be32_to_cpu(class->trap_lid);
788         /*
789          * check for trap lid validity, must not be zero
790          * The trap sink could change after we fashion the MAD but since traps
791          * are not guaranteed we won't use a lock as anyway the change will take
792          * place even with locking.
793          */
794         if (!trap_lid) {
795                 c_err("%s: Invalid dlid\n", __func__);
796                 goto err_exit;
797         }
798
799         ah_attr.dlid = trap_lid;
800         ah = ib_create_ah(port->mad_agent->qp->pd, &ah_attr);
801         if (IS_ERR(ah)) {
802                 c_err("%s:Couldn't create new AH = %p\n", __func__, ah);
803                 c_err("%s:dlid = %d, sl = %d, port = %d\n", __func__,
804                       ah_attr.dlid, ah_attr.sl, ah_attr.port_num);
805                 goto err_exit;
806         }
807
808         if (ib_find_pkey(ibp, data->opaportnum, IB_DEFAULT_PKEY_FULL,
809                          &pkey_idx) < 0) {
810                 c_err("%s:full key not found, defaulting to partial\n",
811                       __func__);
812                 if (ib_find_pkey(ibp, data->opaportnum, IB_DEFAULT_PKEY_PARTIAL,
813                                  &pkey_idx) < 0)
814                         pkey_idx = 1;
815         }
816
817         send_buf = ib_create_send_mad(port->mad_agent, 1, pkey_idx, 0,
818                                       IB_MGMT_VENDOR_HDR, IB_MGMT_MAD_DATA,
819                                       GFP_KERNEL, OPA_MGMT_BASE_VERSION);
820         if (IS_ERR(send_buf)) {
821                 c_err("%s:Couldn't allocate send buf\n", __func__);
822                 goto err_sndbuf;
823         }
824
825         send_buf->ah = ah;
826
827         /* Set up common MAD hdr */
828         trap_mad = send_buf->mad;
829         trap_mad->mad_hdr.base_version = OPA_MGMT_BASE_VERSION;
830         trap_mad->mad_hdr.mgmt_class = OPA_MGMT_CLASS_INTEL_EMA;
831         trap_mad->mad_hdr.class_version = OPA_EMA_CLASS_VERSION;
832         trap_mad->mad_hdr.method = IB_MGMT_METHOD_TRAP;
833         port->tid++;
834         trap_mad->mad_hdr.tid = cpu_to_be64(port->tid);
835         trap_mad->mad_hdr.attr_id = IB_SMP_ATTR_NOTICE;
836
837         /* Set up vendor OUI */
838         trap_mad->oui[0] = INTEL_OUI_1;
839         trap_mad->oui[1] = INTEL_OUI_2;
840         trap_mad->oui[2] = INTEL_OUI_3;
841
842         /* Setup notice attribute portion */
843         trap_mad->notice.gen_type = OPA_INTEL_EMA_NOTICE_TYPE_INFO << 1;
844         trap_mad->notice.oui_1 = INTEL_OUI_1;
845         trap_mad->notice.oui_2 = INTEL_OUI_2;
846         trap_mad->notice.oui_3 = INTEL_OUI_3;
847         trap_mad->notice.issuer_lid = cpu_to_be32(lid);
848
849         /* copy the actual trap data */
850         trap = (struct opa_veswport_trap *)trap_mad->notice.raw_data;
851         trap->fabric_id = cpu_to_be16(data->fabric_id);
852         trap->veswid = cpu_to_be16(data->veswid);
853         trap->veswportnum = cpu_to_be32(data->veswportnum);
854         trap->opaportnum = cpu_to_be16(data->opaportnum);
855         trap->veswportindex = data->veswportindex;
856         trap->opcode = data->opcode;
857
858         /* If successful send set up rate limit timeout else bail */
859         if (ib_post_send_mad(send_buf, NULL)) {
860                 ib_free_send_mad(send_buf);
861         } else {
862                 if (adapter->trap_count)
863                         return;
864                 adapter->trap_timeout = jiffies +
865                                         usecs_to_jiffies(OPA_VNIC_TRAP_TIMEOUT);
866                 return;
867         }
868
869 err_sndbuf:
870         ib_destroy_ah(ah);
871 err_exit:
872         v_err("Aborting trap\n");
873 }
874
875 static int vema_rem_vport(int id, void *p, void *data)
876 {
877         struct opa_vnic_adapter *adapter = p;
878
879         opa_vnic_rem_netdev(adapter);
880         return 0;
881 }
882
883 static int vema_enable_vport(int id, void *p, void *data)
884 {
885         struct opa_vnic_adapter *adapter = p;
886
887         netif_carrier_on(adapter->netdev);
888         return 0;
889 }
890
891 static int vema_disable_vport(int id, void *p, void *data)
892 {
893         struct opa_vnic_adapter *adapter = p;
894
895         netif_carrier_off(adapter->netdev);
896         return 0;
897 }
898
899 static void opa_vnic_event(struct ib_event_handler *handler,
900                            struct ib_event *record)
901 {
902         struct opa_vnic_vema_port *port =
903                 container_of(handler, struct opa_vnic_vema_port, event_handler);
904         struct opa_vnic_ctrl_port *cport = port->cport;
905
906         if (record->element.port_num != port->port_num)
907                 return;
908
909         c_dbg("OPA_VNIC received event %d on device %s port %d\n",
910               record->event, record->device->name, record->element.port_num);
911
912         if (record->event == IB_EVENT_PORT_ERR)
913                 idr_for_each(&port->vport_idr, vema_disable_vport, NULL);
914         if (record->event == IB_EVENT_PORT_ACTIVE)
915                 idr_for_each(&port->vport_idr, vema_enable_vport, NULL);
916 }
917
918 /**
919  * vema_unregister -- Unregisters agent
920  * @cport: pointer to control port
921  *
922  * This deletes the registration by VEMA for MADs
923  */
924 static void vema_unregister(struct opa_vnic_ctrl_port *cport)
925 {
926         int i;
927
928         for (i = 1; i <= cport->num_ports; i++) {
929                 struct opa_vnic_vema_port *port = vema_get_port(cport, i);
930
931                 if (!port->mad_agent)
932                         continue;
933
934                 /* Lock ensures no MAD is being processed */
935                 mutex_lock(&port->lock);
936                 idr_for_each(&port->vport_idr, vema_rem_vport, NULL);
937                 mutex_unlock(&port->lock);
938
939                 ib_unregister_mad_agent(port->mad_agent);
940                 port->mad_agent = NULL;
941                 mutex_destroy(&port->lock);
942                 idr_destroy(&port->vport_idr);
943                 ib_unregister_event_handler(&port->event_handler);
944         }
945 }
946
947 /**
948  * vema_register -- Registers agent
949  * @cport: pointer to control port
950  *
951  * This function registers the handlers for the VEMA MADs
952  *
953  * Return: returns 0 on success. non zero otherwise
954  */
955 static int vema_register(struct opa_vnic_ctrl_port *cport)
956 {
957         struct ib_mad_reg_req reg_req = {
958                 .mgmt_class = OPA_MGMT_CLASS_INTEL_EMA,
959                 .mgmt_class_version = OPA_MGMT_BASE_VERSION,
960                 .oui = { INTEL_OUI_1, INTEL_OUI_2, INTEL_OUI_3 }
961         };
962         int i;
963
964         set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
965         set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
966
967         /* register ib event handler and mad agent for each port on dev */
968         for (i = 1; i <= cport->num_ports; i++) {
969                 struct opa_vnic_vema_port *port = vema_get_port(cport, i);
970                 int ret;
971
972                 port->cport = cport;
973                 port->port_num = i;
974
975                 INIT_IB_EVENT_HANDLER(&port->event_handler,
976                                       cport->ibdev, opa_vnic_event);
977                 ret = ib_register_event_handler(&port->event_handler);
978                 if (ret) {
979                         c_err("port %d: event handler register failed\n", i);
980                         vema_unregister(cport);
981                         return ret;
982                 }
983
984                 idr_init(&port->vport_idr);
985                 mutex_init(&port->lock);
986                 port->mad_agent = ib_register_mad_agent(cport->ibdev, i,
987                                                         IB_QPT_GSI, &reg_req,
988                                                         IB_MGMT_RMPP_VERSION,
989                                                         vema_send, vema_recv,
990                                                         port, 0);
991                 if (IS_ERR(port->mad_agent)) {
992                         ret = PTR_ERR(port->mad_agent);
993                         port->mad_agent = NULL;
994                         mutex_destroy(&port->lock);
995                         idr_destroy(&port->vport_idr);
996                         vema_unregister(cport);
997                         return ret;
998                 }
999         }
1000
1001         return 0;
1002 }
1003
1004 /**
1005  * opa_vnic_vema_add_one -- Handle new ib device
1006  * @device: ib device pointer
1007  *
1008  * Allocate the vnic control port and initialize it.
1009  */
1010 static void opa_vnic_vema_add_one(struct ib_device *device)
1011 {
1012         struct opa_vnic_ctrl_port *cport;
1013         int rc, size = sizeof(*cport);
1014
1015         if (!rdma_cap_opa_vnic(device))
1016                 return;
1017
1018         size += device->phys_port_cnt * sizeof(struct opa_vnic_vema_port);
1019         cport = kzalloc(size, GFP_KERNEL);
1020         if (!cport)
1021                 return;
1022
1023         cport->num_ports = device->phys_port_cnt;
1024         cport->ibdev = device;
1025
1026         /* Initialize opa vnic management agent (vema) */
1027         rc = vema_register(cport);
1028         if (!rc)
1029                 c_info("VNIC client initialized\n");
1030
1031         ib_set_client_data(device, &opa_vnic_client, cport);
1032 }
1033
1034 /**
1035  * opa_vnic_vema_rem_one -- Handle ib device removal
1036  * @device: ib device pointer
1037  * @client_data: ib client data
1038  *
1039  * Uninitialize and free the vnic control port.
1040  */
1041 static void opa_vnic_vema_rem_one(struct ib_device *device,
1042                                   void *client_data)
1043 {
1044         struct opa_vnic_ctrl_port *cport = client_data;
1045
1046         if (!cport)
1047                 return;
1048
1049         c_info("removing VNIC client\n");
1050         vema_unregister(cport);
1051         kfree(cport);
1052 }
1053
1054 static int __init opa_vnic_init(void)
1055 {
1056         int rc;
1057
1058         pr_info("OPA Virtual Network Driver - v%s\n",
1059                 opa_vnic_driver_version);
1060
1061         rc = ib_register_client(&opa_vnic_client);
1062         if (rc)
1063                 pr_err("VNIC driver register failed %d\n", rc);
1064
1065         return rc;
1066 }
1067 module_init(opa_vnic_init);
1068
1069 static void opa_vnic_deinit(void)
1070 {
1071         ib_unregister_client(&opa_vnic_client);
1072 }
1073 module_exit(opa_vnic_deinit);
1074
1075 MODULE_LICENSE("Dual BSD/GPL");
1076 MODULE_AUTHOR("Intel Corporation");
1077 MODULE_DESCRIPTION("Intel OPA Virtual Network driver");
1078 MODULE_VERSION(DRV_VERSION);