blob: 0c95668fe7ad7c51704908caf80957eec910bc12 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Tom Duffycd4e8fb2005-06-27 14:36:37 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
Tim Schmielau4e57b682005-10-30 15:03:48 -080034#include <linux/string.h>
35#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Roland Dreiera4d61e82005-08-25 13:40:04 -070037#include <rdma/ib_verbs.h>
38#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include "mthca_dev.h"
41
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -070042enum {
43 MTHCA_RATE_TAVOR_FULL = 0,
44 MTHCA_RATE_TAVOR_1X = 1,
45 MTHCA_RATE_TAVOR_4X = 2,
46 MTHCA_RATE_TAVOR_1X_DDR = 3
47};
48
49enum {
50 MTHCA_RATE_MEMFREE_FULL = 0,
51 MTHCA_RATE_MEMFREE_QUARTER = 1,
52 MTHCA_RATE_MEMFREE_EIGHTH = 2,
53 MTHCA_RATE_MEMFREE_HALF = 3
54};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056struct mthca_av {
Sean Hefty97f52eb2005-08-13 21:05:57 -070057 __be32 port_pd;
58 u8 reserved1;
59 u8 g_slid;
60 __be16 dlid;
61 u8 reserved2;
62 u8 gid_index;
63 u8 msg_sr;
64 u8 hop_limit;
65 __be32 sl_tclass_flowlabel;
66 __be32 dgid[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -070069static enum ib_rate memfree_rate_to_ib(u8 mthca_rate, u8 port_rate)
70{
71 switch (mthca_rate) {
72 case MTHCA_RATE_MEMFREE_EIGHTH:
73 return mult_to_ib_rate(port_rate >> 3);
74 case MTHCA_RATE_MEMFREE_QUARTER:
75 return mult_to_ib_rate(port_rate >> 2);
76 case MTHCA_RATE_MEMFREE_HALF:
77 return mult_to_ib_rate(port_rate >> 1);
78 case MTHCA_RATE_MEMFREE_FULL:
79 default:
80 return mult_to_ib_rate(port_rate);
81 }
82}
83
84static enum ib_rate tavor_rate_to_ib(u8 mthca_rate, u8 port_rate)
85{
86 switch (mthca_rate) {
87 case MTHCA_RATE_TAVOR_1X: return IB_RATE_2_5_GBPS;
88 case MTHCA_RATE_TAVOR_1X_DDR: return IB_RATE_5_GBPS;
89 case MTHCA_RATE_TAVOR_4X: return IB_RATE_10_GBPS;
Jack Morgensteinb046a042006-08-28 19:08:53 +030090 default: return mult_to_ib_rate(port_rate);
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -070091 }
92}
93
94enum ib_rate mthca_rate_to_ib(struct mthca_dev *dev, u8 mthca_rate, u8 port)
95{
96 if (mthca_is_memfree(dev)) {
97 /* Handle old Arbel FW */
98 if (dev->limits.stat_rate_support == 0x3 && mthca_rate)
99 return IB_RATE_2_5_GBPS;
100
101 return memfree_rate_to_ib(mthca_rate, dev->rate[port - 1]);
102 } else
103 return tavor_rate_to_ib(mthca_rate, dev->rate[port - 1]);
104}
105
106static u8 ib_rate_to_memfree(u8 req_rate, u8 cur_rate)
107{
108 if (cur_rate <= req_rate)
109 return 0;
110
111 /*
112 * Inter-packet delay (IPD) to get from rate X down to a rate
113 * no more than Y is (X - 1) / Y.
114 */
115 switch ((cur_rate - 1) / req_rate) {
116 case 0: return MTHCA_RATE_MEMFREE_FULL;
117 case 1: return MTHCA_RATE_MEMFREE_HALF;
118 case 2: /* fall through */
119 case 3: return MTHCA_RATE_MEMFREE_QUARTER;
120 default: return MTHCA_RATE_MEMFREE_EIGHTH;
121 }
122}
123
124static u8 ib_rate_to_tavor(u8 static_rate)
125{
126 switch (static_rate) {
127 case IB_RATE_2_5_GBPS: return MTHCA_RATE_TAVOR_1X;
128 case IB_RATE_5_GBPS: return MTHCA_RATE_TAVOR_1X_DDR;
129 case IB_RATE_10_GBPS: return MTHCA_RATE_TAVOR_4X;
130 default: return MTHCA_RATE_TAVOR_FULL;
131 }
132}
133
134u8 mthca_get_rate(struct mthca_dev *dev, int static_rate, u8 port)
135{
136 u8 rate;
137
138 if (!static_rate || ib_rate_to_mult(static_rate) >= dev->rate[port - 1])
139 return 0;
140
141 if (mthca_is_memfree(dev))
142 rate = ib_rate_to_memfree(ib_rate_to_mult(static_rate),
143 dev->rate[port - 1]);
144 else
145 rate = ib_rate_to_tavor(static_rate);
146
147 if (!(dev->limits.stat_rate_support & (1 << rate)))
148 rate = 1;
149
150 return rate;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153int mthca_create_ah(struct mthca_dev *dev,
154 struct mthca_pd *pd,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400155 struct rdma_ah_attr *ah_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct mthca_ah *ah)
157{
158 u32 index = -1;
159 struct mthca_av *av = NULL;
160
161 ah->type = MTHCA_AH_PCI_POOL;
162
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700163 if (mthca_is_memfree(dev)) {
Roland Dreier8df8a342005-04-16 15:26:26 -0700164 ah->av = kmalloc(sizeof *ah->av, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!ah->av)
166 return -ENOMEM;
167
168 ah->type = MTHCA_AH_KMALLOC;
169 av = ah->av;
170 } else if (!atomic_read(&pd->sqp_count) &&
171 !(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
172 index = mthca_alloc(&dev->av_table.alloc);
173
174 /* fall back to allocate in host memory */
175 if (index == -1)
176 goto on_hca_fail;
177
Roland Dreier8df8a342005-04-16 15:26:26 -0700178 av = kmalloc(sizeof *av, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!av)
180 goto on_hca_fail;
181
182 ah->type = MTHCA_AH_ON_HCA;
183 ah->avdma = dev->av_table.ddr_av_base +
184 index * MTHCA_AV_SIZE;
185 }
186
187on_hca_fail:
188 if (ah->type == MTHCA_AH_PCI_POOL) {
Souptick Joarder7ceb7402016-12-02 00:11:59 +0530189 ah->av = pci_pool_zalloc(dev->av_table.pool,
190 GFP_ATOMIC, &ah->avdma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!ah->av)
192 return -ENOMEM;
193
194 av = ah->av;
195 }
196
197 ah->key = pd->ntmr.ibmr.lkey;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 av->port_pd = cpu_to_be32(pd->pd_num | (ah_attr->port_num << 24));
200 av->g_slid = ah_attr->src_path_bits;
201 av->dlid = cpu_to_be16(ah_attr->dlid);
202 av->msg_sr = (3 << 4) | /* 2K message */
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700203 mthca_get_rate(dev, ah_attr->static_rate, ah_attr->port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 av->sl_tclass_flowlabel = cpu_to_be32(ah_attr->sl << 28);
205 if (ah_attr->ah_flags & IB_AH_GRH) {
206 av->g_slid |= 0x80;
207 av->gid_index = (ah_attr->port_num - 1) * dev->limits.gid_table_len +
208 ah_attr->grh.sgid_index;
209 av->hop_limit = ah_attr->grh.hop_limit;
210 av->sl_tclass_flowlabel |=
211 cpu_to_be32((ah_attr->grh.traffic_class << 20) |
212 ah_attr->grh.flow_label);
213 memcpy(av->dgid, ah_attr->grh.dgid.raw, 16);
214 } else {
215 /* Arbel workaround -- low byte of GID must be 2 */
216 av->dgid[3] = cpu_to_be32(2);
217 }
218
219 if (0) {
220 int j;
221
222 mthca_dbg(dev, "Created UDAV at %p/%08lx:\n",
223 av, (unsigned long) ah->avdma);
224 for (j = 0; j < 8; ++j)
225 printk(KERN_DEBUG " [%2x] %08x\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700226 j * 4, be32_to_cpu(((__be32 *) av)[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
229 if (ah->type == MTHCA_AH_ON_HCA) {
230 memcpy_toio(dev->av_table.av_map + index * MTHCA_AV_SIZE,
231 av, MTHCA_AV_SIZE);
232 kfree(av);
233 }
234
235 return 0;
236}
237
238int mthca_destroy_ah(struct mthca_dev *dev, struct mthca_ah *ah)
239{
240 switch (ah->type) {
241 case MTHCA_AH_ON_HCA:
242 mthca_free(&dev->av_table.alloc,
Roland Dreier2fa5e2e2006-02-01 13:38:24 -0800243 (ah->avdma - dev->av_table.ddr_av_base) /
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 MTHCA_AV_SIZE);
245 break;
246
247 case MTHCA_AH_PCI_POOL:
248 pci_pool_free(dev->av_table.pool, ah->av, ah->avdma);
249 break;
250
251 case MTHCA_AH_KMALLOC:
252 kfree(ah->av);
253 break;
254 }
255
256 return 0;
257}
258
Michael S. Tsirkin9eacee22006-01-12 15:55:41 -0800259int mthca_ah_grh_present(struct mthca_ah *ah)
260{
261 return !!(ah->av->g_slid & 0x80);
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264int mthca_read_ah(struct mthca_dev *dev, struct mthca_ah *ah,
265 struct ib_ud_header *header)
266{
267 if (ah->type == MTHCA_AH_ON_HCA)
268 return -EINVAL;
269
270 header->lrh.service_level = be32_to_cpu(ah->av->sl_tclass_flowlabel) >> 28;
271 header->lrh.destination_lid = ah->av->dlid;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700272 header->lrh.source_lid = cpu_to_be16(ah->av->g_slid & 0x7f);
Michael S. Tsirkin9eacee22006-01-12 15:55:41 -0800273 if (mthca_ah_grh_present(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 header->grh.traffic_class =
275 (be32_to_cpu(ah->av->sl_tclass_flowlabel) >> 20) & 0xff;
276 header->grh.flow_label =
277 ah->av->sl_tclass_flowlabel & cpu_to_be32(0xfffff);
Rolf Manderscheid3f37cae2007-05-17 09:45:48 -0600278 header->grh.hop_limit = ah->av->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ib_get_cached_gid(&dev->ib_dev,
280 be32_to_cpu(ah->av->port_pd) >> 24,
Michael S. Tsirkinf9e61922006-01-21 14:02:59 -0800281 ah->av->gid_index % dev->limits.gid_table_len,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300282 &header->grh.source_gid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 memcpy(header->grh.destination_gid.raw,
284 ah->av->dgid, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
287 return 0;
288}
289
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400290int mthca_ah_query(struct ib_ah *ibah, struct rdma_ah_attr *attr)
Jack Morgenstein1d89b1a2006-02-26 16:05:59 -0800291{
292 struct mthca_ah *ah = to_mah(ibah);
293 struct mthca_dev *dev = to_mdev(ibah->device);
294
295 /* Only implement for MAD and memfree ah for now. */
296 if (ah->type == MTHCA_AH_ON_HCA)
297 return -ENOSYS;
298
299 memset(attr, 0, sizeof *attr);
300 attr->dlid = be16_to_cpu(ah->av->dlid);
301 attr->sl = be32_to_cpu(ah->av->sl_tclass_flowlabel) >> 28;
Jack Morgenstein1d89b1a2006-02-26 16:05:59 -0800302 attr->port_num = be32_to_cpu(ah->av->port_pd) >> 24;
Jack Morgenstein2290d2c2006-07-14 00:23:50 -0700303 attr->static_rate = mthca_rate_to_ib(dev, ah->av->msg_sr & 0x7,
304 attr->port_num);
305 attr->src_path_bits = ah->av->g_slid & 0x7F;
Jack Morgenstein1d89b1a2006-02-26 16:05:59 -0800306 attr->ah_flags = mthca_ah_grh_present(ah) ? IB_AH_GRH : 0;
307
308 if (attr->ah_flags) {
309 attr->grh.traffic_class =
310 be32_to_cpu(ah->av->sl_tclass_flowlabel) >> 20;
311 attr->grh.flow_label =
312 be32_to_cpu(ah->av->sl_tclass_flowlabel) & 0xfffff;
313 attr->grh.hop_limit = ah->av->hop_limit;
314 attr->grh.sgid_index = ah->av->gid_index &
315 (dev->limits.gid_table_len - 1);
316 memcpy(attr->grh.dgid.raw, ah->av->dgid, 16);
317 }
318
319 return 0;
320}
321
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800322int mthca_init_av_table(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int err;
325
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700326 if (mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328
329 err = mthca_alloc_init(&dev->av_table.alloc,
330 dev->av_table.num_ddr_avs,
331 dev->av_table.num_ddr_avs - 1,
332 0);
333 if (err)
334 return err;
335
336 dev->av_table.pool = pci_pool_create("mthca_av", dev->pdev,
337 MTHCA_AV_SIZE,
338 MTHCA_AV_SIZE, 0);
339 if (!dev->av_table.pool)
340 goto out_free_alloc;
341
342 if (!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
343 dev->av_table.av_map = ioremap(pci_resource_start(dev->pdev, 4) +
344 dev->av_table.ddr_av_base -
345 dev->ddr_start,
346 dev->av_table.num_ddr_avs *
347 MTHCA_AV_SIZE);
348 if (!dev->av_table.av_map)
349 goto out_free_pool;
350 } else
351 dev->av_table.av_map = NULL;
352
353 return 0;
354
355 out_free_pool:
356 pci_pool_destroy(dev->av_table.pool);
357
358 out_free_alloc:
359 mthca_alloc_cleanup(&dev->av_table.alloc);
360 return -ENOMEM;
361}
362
Roland Dreiere1f78682006-03-29 09:36:46 -0800363void mthca_cleanup_av_table(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700365 if (mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return;
367
368 if (dev->av_table.av_map)
369 iounmap(dev->av_table.av_map);
370 pci_pool_destroy(dev->av_table.pool);
371 mthca_alloc_cleanup(&dev->av_table.alloc);
372}