2 * Copyright(c) 2017 Intel Corporation.
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
49 * This file contains OPA Virtual Network Interface Controller (VNIC)
50 * Ethernet Management Agent (EMA) driver
53 #include <linux/module.h>
54 #include <rdma/ib_addr.h>
55 #include <rdma/ib_smi.h>
57 #include "opa_vnic_internal.h"
59 #define DRV_VERSION "1.0"
60 char opa_vnic_driver_name[] = "opa_vnic";
61 const char opa_vnic_driver_version[] = DRV_VERSION;
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.
67 #define GET_TRAP_SL_FROM_CLASS_PORT_INFO(x) (((x) >> 3) & 0x1f)
69 /* Cap trap bursts to a reasonable limit good for normal cases */
70 #define OPA_VNIC_TRAP_BURST_LIMIT 4
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
78 #define OPA_VNIC_TRAP_TIMEOUT ((4096 * (1UL << 18)) / 1000)
80 #define OPA_VNIC_UNSUP_ATTR \
81 cpu_to_be16(IB_MGMT_MAD_STATUS_UNSUPPORTED_METHOD_ATTRIB)
83 #define OPA_VNIC_INVAL_ATTR \
84 cpu_to_be16(IB_MGMT_MAD_STATUS_INVALID_ATTRIB_VALUE)
86 #define OPA_VNIC_CLASS_CAP_TRAP 0x1
88 /* Maximum number of VNIC ports supported */
89 #define OPA_VNIC_MAX_NUM_VPORT 255
92 * struct opa_vnic_vema_port -- VNIC VEMA port details
93 * @cport: pointer to port
94 * @mad_agent: pointer to mad agent for port
95 * @class_port_info: Class port info information.
96 * @tid: Transaction id
97 * @port_num: OPA port number
98 * @vport_idr: vnic ports idr
99 * @event_handler: ib event handler
100 * @lock: adapter interface lock
102 struct opa_vnic_vema_port {
103 struct opa_vnic_ctrl_port *cport;
104 struct ib_mad_agent *mad_agent;
105 struct opa_class_port_info class_port_info;
108 struct idr vport_idr;
109 struct ib_event_handler event_handler;
111 /* Lock to query/update network adapter */
115 static void opa_vnic_vema_add_one(struct ib_device *device);
116 static void opa_vnic_vema_rem_one(struct ib_device *device,
119 static struct ib_client opa_vnic_client = {
120 .name = opa_vnic_driver_name,
121 .add = opa_vnic_vema_add_one,
122 .remove = opa_vnic_vema_rem_one,
126 * vema_get_vport_num -- Get the vnic from the mad
127 * @recvd_mad: Received mad
129 * Return: returns value of the vnic port number
131 static inline u8 vema_get_vport_num(struct opa_vnic_vema_mad *recvd_mad)
133 return be32_to_cpu(recvd_mad->mad_hdr.attr_mod) & 0xff;
137 * vema_get_vport_adapter -- Get vnic port adapter from recvd mad
138 * @recvd_mad: received mad
139 * @port: ptr to port struct on which MAD was recvd
141 * Return: vnic adapter
143 static inline struct opa_vnic_adapter *
144 vema_get_vport_adapter(struct opa_vnic_vema_mad *recvd_mad,
145 struct opa_vnic_vema_port *port)
147 u8 vport_num = vema_get_vport_num(recvd_mad);
149 return idr_find(&port->vport_idr, vport_num);
153 * vema_mac_tbl_req_ok -- Check if mac request has correct values
154 * @mac_tbl: mac table
156 * This function checks for the validity of the offset and number of
159 * Return: true if offset and num_entries are valid
161 static inline bool vema_mac_tbl_req_ok(struct opa_veswport_mactable *mac_tbl)
163 u16 offset, num_entries;
164 u16 req_entries = ((OPA_VNIC_EMA_DATA - sizeof(*mac_tbl)) /
165 sizeof(mac_tbl->tbl_entries[0]));
167 offset = be16_to_cpu(mac_tbl->offset);
168 num_entries = be16_to_cpu(mac_tbl->num_entries);
170 return ((num_entries <= req_entries) &&
171 (offset + num_entries <= OPA_VNIC_MAC_TBL_MAX_ENTRIES));
175 * Return the power on default values in the port info structure
176 * in big endian format as required by MAD.
178 static inline void vema_get_pod_values(struct opa_veswport_info *port_info)
180 memset(port_info, 0, sizeof(*port_info));
181 port_info->vport.max_mac_tbl_ent =
182 cpu_to_be16(OPA_VNIC_MAC_TBL_MAX_ENTRIES);
183 port_info->vport.max_smac_ent =
184 cpu_to_be16(OPA_VNIC_MAX_SMAC_LIMIT);
185 port_info->vport.oper_state = OPA_VNIC_STATE_DROP_ALL;
186 port_info->vport.config_state = OPA_VNIC_STATE_DROP_ALL;
190 * vema_add_vport -- Add a new vnic port
191 * @port: ptr to opa_vnic_vema_port struct
192 * @vport_num: vnic port number (to be added)
194 * Return a pointer to the vnic adapter structure
196 static struct opa_vnic_adapter *vema_add_vport(struct opa_vnic_vema_port *port,
199 struct opa_vnic_ctrl_port *cport = port->cport;
200 struct opa_vnic_adapter *adapter;
202 adapter = opa_vnic_add_netdev(cport->ibdev, port->port_num, vport_num);
203 if (!IS_ERR(adapter)) {
206 adapter->cport = cport;
207 rc = idr_alloc(&port->vport_idr, adapter, vport_num,
208 vport_num + 1, GFP_NOWAIT);
210 opa_vnic_rem_netdev(adapter);
211 adapter = ERR_PTR(rc);
219 * vema_get_class_port_info -- Get class info for port
220 * @port: Port on whic MAD was received
221 * @recvd_mad: pointer to the received mad
222 * @rsp_mad: pointer to respose mad
224 * This function copies the latest class port info value set for the
225 * port and stores it for generating traps
227 static void vema_get_class_port_info(struct opa_vnic_vema_port *port,
228 struct opa_vnic_vema_mad *recvd_mad,
229 struct opa_vnic_vema_mad *rsp_mad)
231 struct opa_class_port_info *port_info;
233 port_info = (struct opa_class_port_info *)rsp_mad->data;
234 memcpy(port_info, &port->class_port_info, sizeof(*port_info));
235 port_info->base_version = OPA_MGMT_BASE_VERSION,
236 port_info->class_version = OPA_EMA_CLASS_VERSION;
239 * Set capability mask bit indicating agent generates traps,
240 * and set the maximum number of VNIC ports supported.
242 port_info->cap_mask = cpu_to_be16((OPA_VNIC_CLASS_CAP_TRAP |
243 (OPA_VNIC_MAX_NUM_VPORT << 8)));
246 * Since a get routine is always sent by the EM first we
247 * set the expected response time to
248 * 4.096 usec * 2^18 == 1.0737 sec here.
250 port_info->cap_mask2_resp_time = cpu_to_be32(18);
254 * vema_set_class_port_info -- Get class info for port
255 * @port: Port on whic MAD was received
256 * @recvd_mad: pointer to the received mad
257 * @rsp_mad: pointer to respose mad
259 * This function updates the port class info for the specific vnic
260 * and sets up the response mad data
262 static void vema_set_class_port_info(struct opa_vnic_vema_port *port,
263 struct opa_vnic_vema_mad *recvd_mad,
264 struct opa_vnic_vema_mad *rsp_mad)
266 memcpy(&port->class_port_info, recvd_mad->data,
267 sizeof(port->class_port_info));
269 vema_get_class_port_info(port, recvd_mad, rsp_mad);
273 * vema_get_veswport_info -- Get veswport info
274 * @port: source port on which MAD was received
275 * @recvd_mad: pointer to the received mad
276 * @rsp_mad: pointer to respose mad
278 static void vema_get_veswport_info(struct opa_vnic_vema_port *port,
279 struct opa_vnic_vema_mad *recvd_mad,
280 struct opa_vnic_vema_mad *rsp_mad)
282 struct opa_veswport_info *port_info =
283 (struct opa_veswport_info *)rsp_mad->data;
284 struct opa_vnic_adapter *adapter;
286 adapter = vema_get_vport_adapter(recvd_mad, port);
288 memset(port_info, 0, sizeof(*port_info));
289 opa_vnic_get_vesw_info(adapter, &port_info->vesw);
290 opa_vnic_get_per_veswport_info(adapter,
293 vema_get_pod_values(port_info);
298 * vema_set_veswport_info -- Set 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
303 * This function gets the port class infor for vnic
305 static void vema_set_veswport_info(struct opa_vnic_vema_port *port,
306 struct opa_vnic_vema_mad *recvd_mad,
307 struct opa_vnic_vema_mad *rsp_mad)
309 struct opa_vnic_ctrl_port *cport = port->cport;
310 struct opa_veswport_info *port_info;
311 struct opa_vnic_adapter *adapter;
314 vport_num = vema_get_vport_num(recvd_mad);
316 adapter = vema_get_vport_adapter(recvd_mad, port);
318 adapter = vema_add_vport(port, vport_num);
319 if (IS_ERR(adapter)) {
320 c_err("failed to add vport %d: %ld\n",
321 vport_num, PTR_ERR(adapter));
326 port_info = (struct opa_veswport_info *)recvd_mad->data;
327 opa_vnic_set_vesw_info(adapter, &port_info->vesw);
328 opa_vnic_set_per_veswport_info(adapter, &port_info->vport);
330 /* Process the new config settings */
331 opa_vnic_process_vema_config(adapter);
333 vema_get_veswport_info(port, recvd_mad, rsp_mad);
337 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
341 * vema_get_mac_entries -- Get MAC entries in VNIC MAC table
342 * @port: source port on which MAD was received
343 * @recvd_mad: pointer to the received mad
344 * @rsp_mad: pointer to respose mad
346 * This function gets the MAC entries that are programmed into
347 * the VNIC MAC forwarding table. It checks for the validity of
348 * the index into the MAC table and the number of entries that
349 * are to be retrieved.
351 static void vema_get_mac_entries(struct opa_vnic_vema_port *port,
352 struct opa_vnic_vema_mad *recvd_mad,
353 struct opa_vnic_vema_mad *rsp_mad)
355 struct opa_veswport_mactable *mac_tbl_in, *mac_tbl_out;
356 struct opa_vnic_adapter *adapter;
358 adapter = vema_get_vport_adapter(recvd_mad, port);
360 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
364 mac_tbl_in = (struct opa_veswport_mactable *)recvd_mad->data;
365 mac_tbl_out = (struct opa_veswport_mactable *)rsp_mad->data;
367 if (vema_mac_tbl_req_ok(mac_tbl_in)) {
368 mac_tbl_out->offset = mac_tbl_in->offset;
369 mac_tbl_out->num_entries = mac_tbl_in->num_entries;
370 opa_vnic_query_mac_tbl(adapter, mac_tbl_out);
372 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
377 * vema_set_mac_entries -- Set MAC entries in VNIC MAC table
378 * @port: source port on which MAD was received
379 * @recvd_mad: pointer to the received mad
380 * @rsp_mad: pointer to respose mad
382 * This function sets the MAC entries in the VNIC forwarding table
383 * It checks for the validity of the index and the number of forwarding
384 * table entries to be programmed.
386 static void vema_set_mac_entries(struct opa_vnic_vema_port *port,
387 struct opa_vnic_vema_mad *recvd_mad,
388 struct opa_vnic_vema_mad *rsp_mad)
390 struct opa_veswport_mactable *mac_tbl;
391 struct opa_vnic_adapter *adapter;
393 adapter = vema_get_vport_adapter(recvd_mad, port);
395 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
399 mac_tbl = (struct opa_veswport_mactable *)recvd_mad->data;
400 if (vema_mac_tbl_req_ok(mac_tbl)) {
401 if (opa_vnic_update_mac_tbl(adapter, mac_tbl))
402 rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
404 rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
406 vema_get_mac_entries(port, recvd_mad, rsp_mad);
410 * vema_set_delete_vesw -- Reset VESW info to POD values
411 * @port: source port on which MAD was received
412 * @recvd_mad: pointer to the received mad
413 * @rsp_mad: pointer to respose mad
415 * This function clears all the fields of veswport info for the requested vesw
416 * and sets them back to the power-on default values. It does not delete the
419 static void vema_set_delete_vesw(struct opa_vnic_vema_port *port,
420 struct opa_vnic_vema_mad *recvd_mad,
421 struct opa_vnic_vema_mad *rsp_mad)
423 struct opa_veswport_info *port_info =
424 (struct opa_veswport_info *)rsp_mad->data;
425 struct opa_vnic_adapter *adapter;
427 adapter = vema_get_vport_adapter(recvd_mad, port);
429 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
433 vema_get_pod_values(port_info);
434 opa_vnic_set_vesw_info(adapter, &port_info->vesw);
435 opa_vnic_set_per_veswport_info(adapter, &port_info->vport);
437 /* Process the new config settings */
438 opa_vnic_process_vema_config(adapter);
440 opa_vnic_release_mac_tbl(adapter);
442 vema_get_veswport_info(port, recvd_mad, rsp_mad);
446 * vema_get_mac_list -- Get the unicast/multicast macs.
447 * @port: source port on which MAD was received
448 * @recvd_mad: Received mad contains fields to set vnic parameters
449 * @rsp_mad: Response mad to be built
450 * @attr_id: Attribute ID indicating multicast or unicast mac list
452 static void vema_get_mac_list(struct opa_vnic_vema_port *port,
453 struct opa_vnic_vema_mad *recvd_mad,
454 struct opa_vnic_vema_mad *rsp_mad,
457 struct opa_veswport_iface_macs *macs_in, *macs_out;
458 int max_entries = (OPA_VNIC_EMA_DATA - sizeof(*macs_out)) / ETH_ALEN;
459 struct opa_vnic_adapter *adapter;
461 adapter = vema_get_vport_adapter(recvd_mad, port);
463 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
467 macs_in = (struct opa_veswport_iface_macs *)recvd_mad->data;
468 macs_out = (struct opa_veswport_iface_macs *)rsp_mad->data;
470 macs_out->start_idx = macs_in->start_idx;
471 if (macs_in->num_macs_in_msg)
472 macs_out->num_macs_in_msg = macs_in->num_macs_in_msg;
474 macs_out->num_macs_in_msg = cpu_to_be16(max_entries);
476 if (attr_id == OPA_EM_ATTR_IFACE_MCAST_MACS)
477 opa_vnic_query_mcast_macs(adapter, macs_out);
479 opa_vnic_query_ucast_macs(adapter, macs_out);
483 * vema_get_summary_counters -- Gets summary counters.
484 * @port: source port on which MAD was received
485 * @recvd_mad: Received mad contains fields to set vnic parameters
486 * @rsp_mad: Response mad to be built
488 static void vema_get_summary_counters(struct opa_vnic_vema_port *port,
489 struct opa_vnic_vema_mad *recvd_mad,
490 struct opa_vnic_vema_mad *rsp_mad)
492 struct opa_veswport_summary_counters *cntrs;
493 struct opa_vnic_adapter *adapter;
495 adapter = vema_get_vport_adapter(recvd_mad, port);
497 cntrs = (struct opa_veswport_summary_counters *)rsp_mad->data;
498 opa_vnic_get_summary_counters(adapter, cntrs);
500 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
505 * vema_get_error_counters -- Gets summary counters.
506 * @port: source port on which MAD was received
507 * @recvd_mad: Received mad contains fields to set vnic parameters
508 * @rsp_mad: Response mad to be built
510 static void vema_get_error_counters(struct opa_vnic_vema_port *port,
511 struct opa_vnic_vema_mad *recvd_mad,
512 struct opa_vnic_vema_mad *rsp_mad)
514 struct opa_veswport_error_counters *cntrs;
515 struct opa_vnic_adapter *adapter;
517 adapter = vema_get_vport_adapter(recvd_mad, port);
519 cntrs = (struct opa_veswport_error_counters *)rsp_mad->data;
520 opa_vnic_get_error_counters(adapter, cntrs);
522 rsp_mad->mad_hdr.status = OPA_VNIC_INVAL_ATTR;
527 * vema_get -- Process received get MAD
528 * @port: source port on which MAD was received
529 * @recvd_mad: Received mad
530 * @rsp_mad: Response mad to be built
532 static void vema_get(struct opa_vnic_vema_port *port,
533 struct opa_vnic_vema_mad *recvd_mad,
534 struct opa_vnic_vema_mad *rsp_mad)
536 u16 attr_id = be16_to_cpu(recvd_mad->mad_hdr.attr_id);
539 case OPA_EM_ATTR_CLASS_PORT_INFO:
540 vema_get_class_port_info(port, recvd_mad, rsp_mad);
542 case OPA_EM_ATTR_VESWPORT_INFO:
543 vema_get_veswport_info(port, recvd_mad, rsp_mad);
545 case OPA_EM_ATTR_VESWPORT_MAC_ENTRIES:
546 vema_get_mac_entries(port, recvd_mad, rsp_mad);
548 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);
553 case OPA_EM_ATTR_VESWPORT_SUMMARY_COUNTERS:
554 vema_get_summary_counters(port, recvd_mad, rsp_mad);
556 case OPA_EM_ATTR_VESWPORT_ERROR_COUNTERS:
557 vema_get_error_counters(port, recvd_mad, rsp_mad);
560 rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
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
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)
575 u16 attr_id = be16_to_cpu(recvd_mad->mad_hdr.attr_id);
578 case OPA_EM_ATTR_CLASS_PORT_INFO:
579 vema_set_class_port_info(port, recvd_mad, rsp_mad);
581 case OPA_EM_ATTR_VESWPORT_INFO:
582 vema_set_veswport_info(port, recvd_mad, rsp_mad);
584 case OPA_EM_ATTR_VESWPORT_MAC_ENTRIES:
585 vema_set_mac_entries(port, recvd_mad, rsp_mad);
587 case OPA_EM_ATTR_DELETE_VESW:
588 vema_set_delete_vesw(port, recvd_mad, rsp_mad);
591 rsp_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
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
601 * Free all the data structures associated with the sent MAD
603 static void vema_send(struct ib_mad_agent *mad_agent,
604 struct ib_mad_send_wc *mad_wc)
606 ib_destroy_ah(mad_wc->send_buf->ah);
607 ib_free_send_mad(mad_wc->send_buf);
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
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.
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)
624 struct opa_vnic_vema_port *port;
626 struct ib_mad_send_buf *rsp;
627 struct opa_vnic_vema_mad *vema_mad;
629 if (!mad_wc || !mad_wc->recv_buf.mad)
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);
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);
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;
651 /* Lock ensures network adapter is not removed */
652 mutex_lock(&port->lock);
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,
659 case IB_MGMT_METHOD_SET:
660 vema_set(port, (struct opa_vnic_vema_mad *)mad_wc->recv_buf.mad,
664 vema_mad->mad_hdr.status = OPA_VNIC_UNSUP_ATTR;
667 mutex_unlock(&port->lock);
669 if (!ib_post_send_mad(rsp, NULL)) {
671 * with post send successful ah and send mad
672 * will be destroyed in send handler
677 ib_free_send_mad(rsp);
682 ib_free_recv_mad(mad_wc);
686 * vema_get_port -- Gets the opa_vnic_vema_port
687 * @cport: pointer to control dev
688 * @port_num: Port number
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
694 * Return: ptr to requested opa_vnic_vema_port strucure
695 * if success, NULL if not
697 static struct opa_vnic_vema_port *
698 vema_get_port(struct opa_vnic_ctrl_port *cport, u8 port_num)
700 struct opa_vnic_vema_port *port = (void *)cport + sizeof(*cport);
702 if (port_num > cport->num_ports)
705 return port + (port_num - 1);
709 * opa_vnic_vema_send_trap -- This function sends a trap to the EM
710 * @cport: pointer to vnic control port
711 * @data: pointer to trap data filled by calling function
712 * @lid: issuers lid (encap_slid from vesw_port_info)
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
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.
724 void opa_vnic_vema_send_trap(struct opa_vnic_adapter *adapter,
725 struct __opa_veswport_trap *data, u32 lid)
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 ib_ah_attr ah_attr;
735 struct opa_veswport_trap *trap;
742 port = vema_get_port(cport, data->opaportnum);
743 if (!port || !port->mad_agent)
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");
751 adapter->trap_count++;
754 adapter->trap_count = 0;
757 class = &port->class_port_info;
758 /* Set up address handle */
759 memset(&ah_attr, 0, sizeof(ah_attr));
760 ah_attr.sl = GET_TRAP_SL_FROM_CLASS_PORT_INFO(class->trap_sl_rsvd);
761 ah_attr.port_num = port->port_num;
762 trap_lid = be32_to_cpu(class->trap_lid);
764 * check for trap lid validity, must not be zero
765 * The trap sink could change after we fashion the MAD but since traps
766 * are not guaranteed we won't use a lock as anyway the change will take
767 * place even with locking.
770 c_err("%s: Invalid dlid\n", __func__);
774 ah_attr.dlid = trap_lid;
775 ah = ib_create_ah(port->mad_agent->qp->pd, &ah_attr);
777 c_err("%s:Couldn't create new AH = %p\n", __func__, ah);
778 c_err("%s:dlid = %d, sl = %d, port = %d\n", __func__,
779 ah_attr.dlid, ah_attr.sl, ah_attr.port_num);
783 if (ib_find_pkey(ibp, data->opaportnum, IB_DEFAULT_PKEY_FULL,
785 c_err("%s:full key not found, defaulting to partial\n",
787 if (ib_find_pkey(ibp, data->opaportnum, IB_DEFAULT_PKEY_PARTIAL,
792 send_buf = ib_create_send_mad(port->mad_agent, 1, pkey_idx, 0,
793 IB_MGMT_VENDOR_HDR, IB_MGMT_MAD_DATA,
794 GFP_KERNEL, OPA_MGMT_BASE_VERSION);
795 if (IS_ERR(send_buf)) {
796 c_err("%s:Couldn't allocate send buf\n", __func__);
802 /* Set up common MAD hdr */
803 trap_mad = send_buf->mad;
804 trap_mad->mad_hdr.base_version = OPA_MGMT_BASE_VERSION;
805 trap_mad->mad_hdr.mgmt_class = OPA_MGMT_CLASS_INTEL_EMA;
806 trap_mad->mad_hdr.class_version = OPA_EMA_CLASS_VERSION;
807 trap_mad->mad_hdr.method = IB_MGMT_METHOD_TRAP;
809 trap_mad->mad_hdr.tid = cpu_to_be64(port->tid);
810 trap_mad->mad_hdr.attr_id = IB_SMP_ATTR_NOTICE;
812 /* Set up vendor OUI */
813 trap_mad->oui[0] = INTEL_OUI_1;
814 trap_mad->oui[1] = INTEL_OUI_2;
815 trap_mad->oui[2] = INTEL_OUI_3;
817 /* Setup notice attribute portion */
818 trap_mad->notice.gen_type = OPA_INTEL_EMA_NOTICE_TYPE_INFO << 1;
819 trap_mad->notice.oui_1 = INTEL_OUI_1;
820 trap_mad->notice.oui_2 = INTEL_OUI_2;
821 trap_mad->notice.oui_3 = INTEL_OUI_3;
822 trap_mad->notice.issuer_lid = cpu_to_be32(lid);
824 /* copy the actual trap data */
825 trap = (struct opa_veswport_trap *)trap_mad->notice.raw_data;
826 trap->fabric_id = cpu_to_be16(data->fabric_id);
827 trap->veswid = cpu_to_be16(data->veswid);
828 trap->veswportnum = cpu_to_be32(data->veswportnum);
829 trap->opaportnum = cpu_to_be16(data->opaportnum);
830 trap->veswportindex = data->veswportindex;
831 trap->opcode = data->opcode;
833 /* If successful send set up rate limit timeout else bail */
834 if (ib_post_send_mad(send_buf, NULL)) {
835 ib_free_send_mad(send_buf);
837 if (adapter->trap_count)
839 adapter->trap_timeout = jiffies +
840 usecs_to_jiffies(OPA_VNIC_TRAP_TIMEOUT);
847 v_err("Aborting trap\n");
850 static int vema_rem_vport(int id, void *p, void *data)
852 struct opa_vnic_adapter *adapter = p;
854 opa_vnic_rem_netdev(adapter);
858 static int vema_enable_vport(int id, void *p, void *data)
860 struct opa_vnic_adapter *adapter = p;
862 netif_carrier_on(adapter->netdev);
866 static int vema_disable_vport(int id, void *p, void *data)
868 struct opa_vnic_adapter *adapter = p;
870 netif_carrier_off(adapter->netdev);
874 static void opa_vnic_event(struct ib_event_handler *handler,
875 struct ib_event *record)
877 struct opa_vnic_vema_port *port =
878 container_of(handler, struct opa_vnic_vema_port, event_handler);
879 struct opa_vnic_ctrl_port *cport = port->cport;
881 if (record->element.port_num != port->port_num)
884 c_dbg("OPA_VNIC received event %d on device %s port %d\n",
885 record->event, record->device->name, record->element.port_num);
887 if (record->event == IB_EVENT_PORT_ERR)
888 idr_for_each(&port->vport_idr, vema_disable_vport, NULL);
889 if (record->event == IB_EVENT_PORT_ACTIVE)
890 idr_for_each(&port->vport_idr, vema_enable_vport, NULL);
894 * vema_unregister -- Unregisters agent
895 * @cport: pointer to control port
897 * This deletes the registration by VEMA for MADs
899 static void vema_unregister(struct opa_vnic_ctrl_port *cport)
903 for (i = 1; i <= cport->num_ports; i++) {
904 struct opa_vnic_vema_port *port = vema_get_port(cport, i);
906 if (!port->mad_agent)
909 /* Lock ensures no MAD is being processed */
910 mutex_lock(&port->lock);
911 idr_for_each(&port->vport_idr, vema_rem_vport, NULL);
912 mutex_unlock(&port->lock);
914 ib_unregister_mad_agent(port->mad_agent);
915 port->mad_agent = NULL;
916 mutex_destroy(&port->lock);
917 idr_destroy(&port->vport_idr);
918 ib_unregister_event_handler(&port->event_handler);
923 * vema_register -- Registers agent
924 * @cport: pointer to control port
926 * This function registers the handlers for the VEMA MADs
928 * Return: returns 0 on success. non zero otherwise
930 static int vema_register(struct opa_vnic_ctrl_port *cport)
932 struct ib_mad_reg_req reg_req = {
933 .mgmt_class = OPA_MGMT_CLASS_INTEL_EMA,
934 .mgmt_class_version = OPA_MGMT_BASE_VERSION,
935 .oui = { INTEL_OUI_1, INTEL_OUI_2, INTEL_OUI_3 }
939 set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
940 set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
942 /* register ib event handler and mad agent for each port on dev */
943 for (i = 1; i <= cport->num_ports; i++) {
944 struct opa_vnic_vema_port *port = vema_get_port(cport, i);
950 INIT_IB_EVENT_HANDLER(&port->event_handler,
951 cport->ibdev, opa_vnic_event);
952 ret = ib_register_event_handler(&port->event_handler);
954 c_err("port %d: event handler register failed\n", i);
955 vema_unregister(cport);
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, ®_req,
963 IB_MGMT_RMPP_VERSION,
964 vema_send, vema_recv,
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);
980 * opa_vnic_vema_add_one -- Handle new ib device
981 * @device: ib device pointer
983 * Allocate the vnic control port and initialize it.
985 static void opa_vnic_vema_add_one(struct ib_device *device)
987 struct opa_vnic_ctrl_port *cport;
988 int rc, size = sizeof(*cport);
990 if (!rdma_cap_opa_vnic(device))
993 size += device->phys_port_cnt * sizeof(struct opa_vnic_vema_port);
994 cport = kzalloc(size, GFP_KERNEL);
998 cport->num_ports = device->phys_port_cnt;
999 cport->ibdev = device;
1001 /* Initialize opa vnic management agent (vema) */
1002 rc = vema_register(cport);
1004 c_info("VNIC client initialized\n");
1006 ib_set_client_data(device, &opa_vnic_client, cport);
1010 * opa_vnic_vema_rem_one -- Handle ib device removal
1011 * @device: ib device pointer
1012 * @client_data: ib client data
1014 * Uninitialize and free the vnic control port.
1016 static void opa_vnic_vema_rem_one(struct ib_device *device,
1019 struct opa_vnic_ctrl_port *cport = client_data;
1024 c_info("removing VNIC client\n");
1025 vema_unregister(cport);
1029 static int __init opa_vnic_init(void)
1033 pr_info("OPA Virtual Network Driver - v%s\n",
1034 opa_vnic_driver_version);
1036 rc = ib_register_client(&opa_vnic_client);
1038 pr_err("VNIC driver register failed %d\n", rc);
1042 module_init(opa_vnic_init);
1044 static void opa_vnic_deinit(void)
1046 ib_unregister_client(&opa_vnic_client);
1048 module_exit(opa_vnic_deinit);
1050 MODULE_LICENSE("Dual BSD/GPL");
1051 MODULE_AUTHOR("Intel Corporation");
1052 MODULE_DESCRIPTION("Intel OPA Virtual Network driver");
1053 MODULE_VERSION(DRV_VERSION);