1 /******************************************************************* 2 * This file is part of the Emulex RoCE Device Driver for * 3 * RoCE (RDMA over Converged Ethernet) adapters. * 4 * Copyright (C) 2008-2012 Emulex. All rights reserved. * 5 * EMULEX and SLI are trademarks of Emulex. * 6 * www.emulex.com * 7 * * 8 * This program is free software; you can redistribute it and/or * 9 * modify it under the terms of version 2 of the GNU General * 10 * Public License as published by the Free Software Foundation. * 11 * This program is distributed in the hope that it will be useful. * 12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * 13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * 14 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE * 15 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD * 16 * TO BE LEGALLY INVALID. See the GNU General Public License for * 17 * more details, a copy of which can be found in the file COPYING * 18 * included with this package. * 19 * 20 * Contact Information: 21 * linux-drivers@emulex.com 22 * 23 * Emulex 24 * 3333 Susan Street 25 * Costa Mesa, CA 92626 26 *******************************************************************/ 27 28 #include <net/neighbour.h> 29 #include <net/netevent.h> 30 31 #include <rdma/ib_addr.h> 32 33 #include "ocrdma.h" 34 #include "ocrdma_verbs.h" 35 #include "ocrdma_ah.h" 36 #include "ocrdma_hw.h" 37 38 #define OCRDMA_VID_PCP_SHIFT 0xD 39 40 static inline int set_av_attr(struct ocrdma_dev *dev, struct ocrdma_ah *ah, 41 struct ib_ah_attr *attr, int pdid) 42 { 43 int status = 0; 44 u16 vlan_tag; bool vlan_enabled = false; 45 struct ocrdma_eth_vlan eth; 46 struct ocrdma_grh grh; 47 int eth_sz; 48 49 memset(ð, 0, sizeof(eth)); 50 memset(&grh, 0, sizeof(grh)); 51 52 ah->sgid_index = attr->grh.sgid_index; 53 54 vlan_tag = attr->vlan_id; 55 if (!vlan_tag || (vlan_tag > 0xFFF)) 56 vlan_tag = dev->pvid; 57 if (vlan_tag && (vlan_tag < 0x1000)) { 58 eth.eth_type = cpu_to_be16(0x8100); 59 eth.roce_eth_type = cpu_to_be16(OCRDMA_ROCE_ETH_TYPE); 60 vlan_tag |= (dev->sl & 0x07) << OCRDMA_VID_PCP_SHIFT; 61 eth.vlan_tag = cpu_to_be16(vlan_tag); 62 eth_sz = sizeof(struct ocrdma_eth_vlan); 63 vlan_enabled = true; 64 } else { 65 eth.eth_type = cpu_to_be16(OCRDMA_ROCE_ETH_TYPE); 66 eth_sz = sizeof(struct ocrdma_eth_basic); 67 } 68 memcpy(ð.smac[0], &dev->nic_info.mac_addr[0], ETH_ALEN); 69 memcpy(ð.dmac[0], attr->dmac, ETH_ALEN); 70 status = ocrdma_resolve_dmac(dev, attr, ð.dmac[0]); 71 if (status) 72 return status; 73 status = ocrdma_query_gid(&dev->ibdev, 1, attr->grh.sgid_index, 74 (union ib_gid *)&grh.sgid[0]); 75 if (status) 76 return status; 77 78 grh.tclass_flow = cpu_to_be32((6 << 28) | 79 (attr->grh.traffic_class << 24) | 80 attr->grh.flow_label); 81 /* 0x1b is next header value in GRH */ 82 grh.pdid_hoplimit = cpu_to_be32((pdid << 16) | 83 (0x1b << 8) | attr->grh.hop_limit); 84 85 memcpy(&grh.dgid[0], attr->grh.dgid.raw, sizeof(attr->grh.dgid.raw)); 86 memcpy(&ah->av->eth_hdr, ð, eth_sz); 87 memcpy((u8 *)ah->av + eth_sz, &grh, sizeof(struct ocrdma_grh)); 88 if (vlan_enabled) 89 ah->av->valid |= OCRDMA_AV_VLAN_VALID; 90 ah->av->valid = cpu_to_le32(ah->av->valid); 91 return status; 92 } 93 94 struct ib_ah *ocrdma_create_ah(struct ib_pd *ibpd, struct ib_ah_attr *attr) 95 { 96 u32 *ahid_addr; 97 int status; 98 struct ocrdma_ah *ah; 99 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd); 100 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device); 101 102 if (!(attr->ah_flags & IB_AH_GRH)) 103 return ERR_PTR(-EINVAL); 104 105 if (atomic_cmpxchg(&dev->update_sl, 1, 0)) 106 ocrdma_init_service_level(dev); 107 ah = kzalloc(sizeof(*ah), GFP_ATOMIC); 108 if (!ah) 109 return ERR_PTR(-ENOMEM); 110 111 status = ocrdma_alloc_av(dev, ah); 112 if (status) 113 goto av_err; 114 status = set_av_attr(dev, ah, attr, pd->id); 115 if (status) 116 goto av_conf_err; 117 118 /* if pd is for the user process, pass the ah_id to user space */ 119 if ((pd->uctx) && (pd->uctx->ah_tbl.va)) { 120 ahid_addr = pd->uctx->ah_tbl.va + attr->dlid; 121 *ahid_addr = ah->id; 122 } 123 return &ah->ibah; 124 125 av_conf_err: 126 ocrdma_free_av(dev, ah); 127 av_err: 128 kfree(ah); 129 return ERR_PTR(status); 130 } 131 132 int ocrdma_destroy_ah(struct ib_ah *ibah) 133 { 134 struct ocrdma_ah *ah = get_ocrdma_ah(ibah); 135 struct ocrdma_dev *dev = get_ocrdma_dev(ibah->device); 136 137 ocrdma_free_av(dev, ah); 138 kfree(ah); 139 return 0; 140 } 141 142 int ocrdma_query_ah(struct ib_ah *ibah, struct ib_ah_attr *attr) 143 { 144 struct ocrdma_ah *ah = get_ocrdma_ah(ibah); 145 struct ocrdma_av *av = ah->av; 146 struct ocrdma_grh *grh; 147 attr->ah_flags |= IB_AH_GRH; 148 if (ah->av->valid & Bit(1)) { 149 grh = (struct ocrdma_grh *)((u8 *)ah->av + 150 sizeof(struct ocrdma_eth_vlan)); 151 attr->sl = be16_to_cpu(av->eth_hdr.vlan_tag) >> 13; 152 } else { 153 grh = (struct ocrdma_grh *)((u8 *)ah->av + 154 sizeof(struct ocrdma_eth_basic)); 155 attr->sl = 0; 156 } 157 memcpy(&attr->grh.dgid.raw[0], &grh->dgid[0], sizeof(grh->dgid)); 158 attr->grh.sgid_index = ah->sgid_index; 159 attr->grh.hop_limit = be32_to_cpu(grh->pdid_hoplimit) & 0xff; 160 attr->grh.traffic_class = be32_to_cpu(grh->tclass_flow) >> 24; 161 attr->grh.flow_label = be32_to_cpu(grh->tclass_flow) & 0x00ffffffff; 162 return 0; 163 } 164 165 int ocrdma_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *attr) 166 { 167 /* modify_ah is unsupported */ 168 return -ENOSYS; 169 } 170 171 int ocrdma_process_mad(struct ib_device *ibdev, 172 int process_mad_flags, 173 u8 port_num, 174 struct ib_wc *in_wc, 175 struct ib_grh *in_grh, 176 struct ib_mad *in_mad, struct ib_mad *out_mad) 177 { 178 return IB_MAD_RESULT_SUCCESS; 179 } 180