18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright(c) 2016 - 2019 Intel Corporation. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license. When using or 58c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify 108c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as 118c2ecf20Sopenharmony_ci * published by the Free Software Foundation. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but 148c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of 158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 168c2ecf20Sopenharmony_ci * General Public License for more details. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * BSD LICENSE 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 218c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 228c2ecf20Sopenharmony_ci * are met: 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above copyright 258c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 268c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above copyright 278c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in 288c2ecf20Sopenharmony_ci * the documentation and/or other materials provided with the 298c2ecf20Sopenharmony_ci * distribution. 308c2ecf20Sopenharmony_ci * - Neither the name of Intel Corporation nor the names of its 318c2ecf20Sopenharmony_ci * contributors may be used to endorse or promote products derived 328c2ecf20Sopenharmony_ci * from this software without specific prior written permission. 338c2ecf20Sopenharmony_ci * 348c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 358c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 368c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 378c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 388c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 398c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 408c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 418c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 428c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 438c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 448c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 458c2ecf20Sopenharmony_ci * 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci#include <linux/slab.h> 498c2ecf20Sopenharmony_ci#include "ah.h" 508c2ecf20Sopenharmony_ci#include "vt.h" /* for prints */ 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/** 538c2ecf20Sopenharmony_ci * rvt_check_ah - validate the attributes of AH 548c2ecf20Sopenharmony_ci * @ibdev: the ib device 558c2ecf20Sopenharmony_ci * @ah_attr: the attributes of the AH 568c2ecf20Sopenharmony_ci * 578c2ecf20Sopenharmony_ci * If driver supports a more detailed check_ah function call back to it 588c2ecf20Sopenharmony_ci * otherwise just check the basics. 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * Return: 0 on success 618c2ecf20Sopenharmony_ci */ 628c2ecf20Sopenharmony_ciint rvt_check_ah(struct ib_device *ibdev, 638c2ecf20Sopenharmony_ci struct rdma_ah_attr *ah_attr) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci int err; 668c2ecf20Sopenharmony_ci int port_num = rdma_ah_get_port_num(ah_attr); 678c2ecf20Sopenharmony_ci struct ib_port_attr port_attr; 688c2ecf20Sopenharmony_ci struct rvt_dev_info *rdi = ib_to_rvt(ibdev); 698c2ecf20Sopenharmony_ci u8 ah_flags = rdma_ah_get_ah_flags(ah_attr); 708c2ecf20Sopenharmony_ci u8 static_rate = rdma_ah_get_static_rate(ah_attr); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci err = ib_query_port(ibdev, port_num, &port_attr); 738c2ecf20Sopenharmony_ci if (err) 748c2ecf20Sopenharmony_ci return -EINVAL; 758c2ecf20Sopenharmony_ci if (port_num < 1 || 768c2ecf20Sopenharmony_ci port_num > ibdev->phys_port_cnt) 778c2ecf20Sopenharmony_ci return -EINVAL; 788c2ecf20Sopenharmony_ci if (static_rate != IB_RATE_PORT_CURRENT && 798c2ecf20Sopenharmony_ci ib_rate_to_mbps(static_rate) < 0) 808c2ecf20Sopenharmony_ci return -EINVAL; 818c2ecf20Sopenharmony_ci if ((ah_flags & IB_AH_GRH) && 828c2ecf20Sopenharmony_ci rdma_ah_read_grh(ah_attr)->sgid_index >= port_attr.gid_tbl_len) 838c2ecf20Sopenharmony_ci return -EINVAL; 848c2ecf20Sopenharmony_ci if (rdi->driver_f.check_ah) 858c2ecf20Sopenharmony_ci return rdi->driver_f.check_ah(ibdev, ah_attr); 868c2ecf20Sopenharmony_ci return 0; 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rvt_check_ah); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/** 918c2ecf20Sopenharmony_ci * rvt_create_ah - create an address handle 928c2ecf20Sopenharmony_ci * @ibah: the IB address handle 938c2ecf20Sopenharmony_ci * @init_attr: the attributes of the AH 948c2ecf20Sopenharmony_ci * @udata: pointer to user's input output buffer information. 958c2ecf20Sopenharmony_ci * 968c2ecf20Sopenharmony_ci * This may be called from interrupt context. 978c2ecf20Sopenharmony_ci * 988c2ecf20Sopenharmony_ci * Return: 0 on success 998c2ecf20Sopenharmony_ci */ 1008c2ecf20Sopenharmony_ciint rvt_create_ah(struct ib_ah *ibah, struct rdma_ah_init_attr *init_attr, 1018c2ecf20Sopenharmony_ci struct ib_udata *udata) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci struct rvt_ah *ah = ibah_to_rvtah(ibah); 1048c2ecf20Sopenharmony_ci struct rvt_dev_info *dev = ib_to_rvt(ibah->device); 1058c2ecf20Sopenharmony_ci unsigned long flags; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci if (rvt_check_ah(ibah->device, init_attr->ah_attr)) 1088c2ecf20Sopenharmony_ci return -EINVAL; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci spin_lock_irqsave(&dev->n_ahs_lock, flags); 1118c2ecf20Sopenharmony_ci if (dev->n_ahs_allocated == dev->dparms.props.max_ah) { 1128c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->n_ahs_lock, flags); 1138c2ecf20Sopenharmony_ci return -ENOMEM; 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci dev->n_ahs_allocated++; 1178c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->n_ahs_lock, flags); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci rdma_copy_ah_attr(&ah->attr, init_attr->ah_attr); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (dev->driver_f.notify_new_ah) 1228c2ecf20Sopenharmony_ci dev->driver_f.notify_new_ah(ibah->device, 1238c2ecf20Sopenharmony_ci init_attr->ah_attr, ah); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci return 0; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci/** 1298c2ecf20Sopenharmony_ci * rvt_destory_ah - Destory an address handle 1308c2ecf20Sopenharmony_ci * @ibah: address handle 1318c2ecf20Sopenharmony_ci * @destroy_flags: destroy address handle flags (see enum rdma_destroy_ah_flags) 1328c2ecf20Sopenharmony_ci * 1338c2ecf20Sopenharmony_ci * Return: 0 on success 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_ciint rvt_destroy_ah(struct ib_ah *ibah, u32 destroy_flags) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci struct rvt_dev_info *dev = ib_to_rvt(ibah->device); 1388c2ecf20Sopenharmony_ci struct rvt_ah *ah = ibah_to_rvtah(ibah); 1398c2ecf20Sopenharmony_ci unsigned long flags; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci spin_lock_irqsave(&dev->n_ahs_lock, flags); 1428c2ecf20Sopenharmony_ci dev->n_ahs_allocated--; 1438c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->n_ahs_lock, flags); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci rdma_destroy_ah_attr(&ah->attr); 1468c2ecf20Sopenharmony_ci return 0; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci/** 1508c2ecf20Sopenharmony_ci * rvt_modify_ah - modify an ah with given attrs 1518c2ecf20Sopenharmony_ci * @ibah: address handle to modify 1528c2ecf20Sopenharmony_ci * @ah_attr: attrs to apply 1538c2ecf20Sopenharmony_ci * 1548c2ecf20Sopenharmony_ci * Return: 0 on success 1558c2ecf20Sopenharmony_ci */ 1568c2ecf20Sopenharmony_ciint rvt_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci struct rvt_ah *ah = ibah_to_rvtah(ibah); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (rvt_check_ah(ibah->device, ah_attr)) 1618c2ecf20Sopenharmony_ci return -EINVAL; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci ah->attr = *ah_attr; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci return 0; 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci/** 1698c2ecf20Sopenharmony_ci * rvt_query_ah - return attrs for ah 1708c2ecf20Sopenharmony_ci * @ibah: address handle to query 1718c2ecf20Sopenharmony_ci * @ah_attr: return info in this 1728c2ecf20Sopenharmony_ci * 1738c2ecf20Sopenharmony_ci * Return: always 0 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ciint rvt_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct rvt_ah *ah = ibah_to_rvtah(ibah); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci *ah_attr = ah->attr; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci return 0; 1828c2ecf20Sopenharmony_ci} 183