Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 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> |
Mitesh Ahuja | cad1fbb | 2014-12-18 14:12:55 +0530 | [diff] [blame] | 32 | #include <rdma/ib_mad.h> |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 33 | |
| 34 | #include "ocrdma.h" |
| 35 | #include "ocrdma_verbs.h" |
| 36 | #include "ocrdma_ah.h" |
| 37 | #include "ocrdma_hw.h" |
Mitesh Ahuja | cad1fbb | 2014-12-18 14:12:55 +0530 | [diff] [blame] | 38 | #include "ocrdma_stats.h" |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 39 | |
Devesh Sharma | 0ea8726 | 2014-07-02 11:36:04 +0530 | [diff] [blame] | 40 | #define OCRDMA_VID_PCP_SHIFT 0xD |
| 41 | |
Naresh Gottumukkala | 1afc045 | 2013-08-07 12:52:33 +0530 | [diff] [blame] | 42 | static inline int set_av_attr(struct ocrdma_dev *dev, struct ocrdma_ah *ah, |
Devesh Sharma | 29565f2 | 2014-12-18 14:13:07 +0530 | [diff] [blame] | 43 | struct ib_ah_attr *attr, union ib_gid *sgid, |
| 44 | int pdid, bool *isvlan) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 45 | { |
| 46 | int status = 0; |
Devesh Sharma | 29565f2 | 2014-12-18 14:13:07 +0530 | [diff] [blame] | 47 | u16 vlan_tag; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 48 | struct ocrdma_eth_vlan eth; |
| 49 | struct ocrdma_grh grh; |
| 50 | int eth_sz; |
| 51 | |
| 52 | memset(ð, 0, sizeof(eth)); |
| 53 | memset(&grh, 0, sizeof(grh)); |
| 54 | |
devesh.sharma@emulex.com | 1be528b | 2014-09-05 15:09:48 +0530 | [diff] [blame] | 55 | /* VLAN */ |
Moni Shoua | 40aca6f | 2013-12-12 18:03:15 +0200 | [diff] [blame] | 56 | vlan_tag = attr->vlan_id; |
Naresh Gottumukkala | 84b105d | 2013-08-26 15:27:50 +0530 | [diff] [blame] | 57 | if (!vlan_tag || (vlan_tag > 0xFFF)) |
| 58 | vlan_tag = dev->pvid; |
Devesh Sharma | 6f5deab | 2015-05-19 11:32:35 +0530 | [diff] [blame] | 59 | if (vlan_tag || dev->pfc_state) { |
| 60 | if (!vlan_tag) { |
| 61 | pr_err("ocrdma%d:Using VLAN with PFC is recommended\n", |
| 62 | dev->id); |
| 63 | pr_err("ocrdma%d:Using VLAN 0 for this connection\n", |
| 64 | dev->id); |
| 65 | } |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 66 | eth.eth_type = cpu_to_be16(0x8100); |
| 67 | eth.roce_eth_type = cpu_to_be16(OCRDMA_ROCE_ETH_TYPE); |
Devesh Sharma | 0ea8726 | 2014-07-02 11:36:04 +0530 | [diff] [blame] | 68 | vlan_tag |= (dev->sl & 0x07) << OCRDMA_VID_PCP_SHIFT; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 69 | eth.vlan_tag = cpu_to_be16(vlan_tag); |
| 70 | eth_sz = sizeof(struct ocrdma_eth_vlan); |
Devesh Sharma | 29565f2 | 2014-12-18 14:13:07 +0530 | [diff] [blame] | 71 | *isvlan = true; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 72 | } else { |
| 73 | eth.eth_type = cpu_to_be16(OCRDMA_ROCE_ETH_TYPE); |
| 74 | eth_sz = sizeof(struct ocrdma_eth_basic); |
| 75 | } |
devesh.sharma@emulex.com | 1be528b | 2014-09-05 15:09:48 +0530 | [diff] [blame] | 76 | /* MAC */ |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 77 | memcpy(ð.smac[0], &dev->nic_info.mac_addr[0], ETH_ALEN); |
Moni Shoua | 40aca6f | 2013-12-12 18:03:15 +0200 | [diff] [blame] | 78 | status = ocrdma_resolve_dmac(dev, attr, ð.dmac[0]); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 79 | if (status) |
| 80 | return status; |
devesh.sharma@emulex.com | 1be528b | 2014-09-05 15:09:48 +0530 | [diff] [blame] | 81 | ah->sgid_index = attr->grh.sgid_index; |
| 82 | memcpy(&grh.sgid[0], sgid->raw, sizeof(union ib_gid)); |
| 83 | memcpy(&grh.dgid[0], attr->grh.dgid.raw, sizeof(attr->grh.dgid.raw)); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 84 | |
| 85 | grh.tclass_flow = cpu_to_be32((6 << 28) | |
| 86 | (attr->grh.traffic_class << 24) | |
| 87 | attr->grh.flow_label); |
| 88 | /* 0x1b is next header value in GRH */ |
| 89 | grh.pdid_hoplimit = cpu_to_be32((pdid << 16) | |
| 90 | (0x1b << 8) | attr->grh.hop_limit); |
devesh.sharma@emulex.com | 1be528b | 2014-09-05 15:09:48 +0530 | [diff] [blame] | 91 | /* Eth HDR */ |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 92 | memcpy(&ah->av->eth_hdr, ð, eth_sz); |
| 93 | memcpy((u8 *)ah->av + eth_sz, &grh, sizeof(struct ocrdma_grh)); |
Devesh Sharma | 29565f2 | 2014-12-18 14:13:07 +0530 | [diff] [blame] | 94 | if (*isvlan) |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 95 | ah->av->valid |= OCRDMA_AV_VLAN_VALID; |
Devesh Sharma | fe5e8a1 | 2013-12-05 15:48:01 +0530 | [diff] [blame] | 96 | ah->av->valid = cpu_to_le32(ah->av->valid); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 97 | return status; |
| 98 | } |
| 99 | |
| 100 | struct ib_ah *ocrdma_create_ah(struct ib_pd *ibpd, struct ib_ah_attr *attr) |
| 101 | { |
| 102 | u32 *ahid_addr; |
Devesh Sharma | 29565f2 | 2014-12-18 14:13:07 +0530 | [diff] [blame] | 103 | bool isvlan = false; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 104 | int status; |
| 105 | struct ocrdma_ah *ah; |
| 106 | struct ocrdma_pd *pd = get_ocrdma_pd(ibpd); |
Naresh Gottumukkala | f99b164 | 2013-08-07 12:52:32 +0530 | [diff] [blame] | 107 | struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device); |
devesh.sharma@emulex.com | 1be528b | 2014-09-05 15:09:48 +0530 | [diff] [blame] | 108 | union ib_gid sgid; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 109 | |
| 110 | if (!(attr->ah_flags & IB_AH_GRH)) |
| 111 | return ERR_PTR(-EINVAL); |
| 112 | |
Selvin Xavier | 31dbdd9 | 2014-06-10 19:32:13 +0530 | [diff] [blame] | 113 | if (atomic_cmpxchg(&dev->update_sl, 1, 0)) |
| 114 | ocrdma_init_service_level(dev); |
Devesh Sharma | fad51b7 | 2014-02-04 11:57:10 +0530 | [diff] [blame] | 115 | ah = kzalloc(sizeof(*ah), GFP_ATOMIC); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 116 | if (!ah) |
| 117 | return ERR_PTR(-ENOMEM); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 118 | |
| 119 | status = ocrdma_alloc_av(dev, ah); |
| 120 | if (status) |
| 121 | goto av_err; |
devesh.sharma@emulex.com | 1be528b | 2014-09-05 15:09:48 +0530 | [diff] [blame] | 122 | |
| 123 | status = ocrdma_query_gid(&dev->ibdev, 1, attr->grh.sgid_index, &sgid); |
| 124 | if (status) { |
| 125 | pr_err("%s(): Failed to query sgid, status = %d\n", |
| 126 | __func__, status); |
| 127 | goto av_conf_err; |
| 128 | } |
| 129 | |
Mitesh Ahuja | d27b2f1 | 2015-05-19 11:32:38 +0530 | [diff] [blame] | 130 | if ((pd->uctx) && |
| 131 | (!rdma_is_multicast_addr((struct in6_addr *)attr->grh.dgid.raw)) && |
| 132 | (!rdma_link_local_addr((struct in6_addr *)attr->grh.dgid.raw))) { |
devesh.sharma@emulex.com | 1be528b | 2014-09-05 15:09:48 +0530 | [diff] [blame] | 133 | status = rdma_addr_find_dmac_by_grh(&sgid, &attr->grh.dgid, |
| 134 | attr->dmac, &attr->vlan_id); |
| 135 | if (status) { |
| 136 | pr_err("%s(): Failed to resolve dmac from gid." |
| 137 | "status = %d\n", __func__, status); |
| 138 | goto av_conf_err; |
| 139 | } |
| 140 | } |
| 141 | |
Devesh Sharma | 29565f2 | 2014-12-18 14:13:07 +0530 | [diff] [blame] | 142 | status = set_av_attr(dev, ah, attr, &sgid, pd->id, &isvlan); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 143 | if (status) |
| 144 | goto av_conf_err; |
| 145 | |
| 146 | /* if pd is for the user process, pass the ah_id to user space */ |
| 147 | if ((pd->uctx) && (pd->uctx->ah_tbl.va)) { |
| 148 | ahid_addr = pd->uctx->ah_tbl.va + attr->dlid; |
Devesh Sharma | 29565f2 | 2014-12-18 14:13:07 +0530 | [diff] [blame] | 149 | *ahid_addr = 0; |
| 150 | *ahid_addr |= ah->id & OCRDMA_AH_ID_MASK; |
| 151 | if (isvlan) |
| 152 | *ahid_addr |= (OCRDMA_AH_VLAN_VALID_MASK << |
| 153 | OCRDMA_AH_VLAN_VALID_SHIFT); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 154 | } |
Devesh Sharma | 29565f2 | 2014-12-18 14:13:07 +0530 | [diff] [blame] | 155 | |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 156 | return &ah->ibah; |
| 157 | |
| 158 | av_conf_err: |
| 159 | ocrdma_free_av(dev, ah); |
| 160 | av_err: |
| 161 | kfree(ah); |
| 162 | return ERR_PTR(status); |
| 163 | } |
| 164 | |
| 165 | int ocrdma_destroy_ah(struct ib_ah *ibah) |
| 166 | { |
| 167 | struct ocrdma_ah *ah = get_ocrdma_ah(ibah); |
Naresh Gottumukkala | 1afc045 | 2013-08-07 12:52:33 +0530 | [diff] [blame] | 168 | struct ocrdma_dev *dev = get_ocrdma_dev(ibah->device); |
| 169 | |
| 170 | ocrdma_free_av(dev, ah); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 171 | kfree(ah); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int ocrdma_query_ah(struct ib_ah *ibah, struct ib_ah_attr *attr) |
| 176 | { |
| 177 | struct ocrdma_ah *ah = get_ocrdma_ah(ibah); |
| 178 | struct ocrdma_av *av = ah->av; |
| 179 | struct ocrdma_grh *grh; |
| 180 | attr->ah_flags |= IB_AH_GRH; |
devesh.sharma@emulex.com | f0c2c22 | 2014-09-05 15:09:49 +0530 | [diff] [blame] | 181 | if (ah->av->valid & OCRDMA_AV_VALID) { |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 182 | grh = (struct ocrdma_grh *)((u8 *)ah->av + |
| 183 | sizeof(struct ocrdma_eth_vlan)); |
| 184 | attr->sl = be16_to_cpu(av->eth_hdr.vlan_tag) >> 13; |
| 185 | } else { |
| 186 | grh = (struct ocrdma_grh *)((u8 *)ah->av + |
| 187 | sizeof(struct ocrdma_eth_basic)); |
| 188 | attr->sl = 0; |
| 189 | } |
| 190 | memcpy(&attr->grh.dgid.raw[0], &grh->dgid[0], sizeof(grh->dgid)); |
| 191 | attr->grh.sgid_index = ah->sgid_index; |
| 192 | attr->grh.hop_limit = be32_to_cpu(grh->pdid_hoplimit) & 0xff; |
| 193 | attr->grh.traffic_class = be32_to_cpu(grh->tclass_flow) >> 24; |
| 194 | attr->grh.flow_label = be32_to_cpu(grh->tclass_flow) & 0x00ffffffff; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int ocrdma_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *attr) |
| 199 | { |
| 200 | /* modify_ah is unsupported */ |
| 201 | return -ENOSYS; |
| 202 | } |
| 203 | |
| 204 | int ocrdma_process_mad(struct ib_device *ibdev, |
| 205 | int process_mad_flags, |
| 206 | u8 port_num, |
| 207 | struct ib_wc *in_wc, |
| 208 | struct ib_grh *in_grh, |
| 209 | struct ib_mad *in_mad, struct ib_mad *out_mad) |
| 210 | { |
Mitesh Ahuja | cad1fbb | 2014-12-18 14:12:55 +0530 | [diff] [blame] | 211 | int status; |
| 212 | struct ocrdma_dev *dev; |
| 213 | |
| 214 | switch (in_mad->mad_hdr.mgmt_class) { |
| 215 | case IB_MGMT_CLASS_PERF_MGMT: |
| 216 | dev = get_ocrdma_dev(ibdev); |
| 217 | if (!ocrdma_pma_counters(dev, out_mad)) |
| 218 | status = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY; |
| 219 | else |
| 220 | status = IB_MAD_RESULT_SUCCESS; |
| 221 | break; |
| 222 | default: |
| 223 | status = IB_MAD_RESULT_SUCCESS; |
| 224 | break; |
| 225 | } |
| 226 | return status; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 227 | } |