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