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